Do real testing of handles for encryption side
diff --git a/src/Cose.c b/src/Cose.c index e6c627d..23b40e3 100644 --- a/src/Cose.c +++ b/src/Cose.c
@@ -374,3 +374,48 @@ return COSE_ERR_CBOR; } } + +void _COSE_InsertInList(COSE ** root, COSE * newMsg) +{ + if (*root == NULL) { + *root = newMsg; + return; + } + + newMsg->m_handleList = *root; + *root = newMsg; + return; +} + +bool _COSE_IsInList(COSE * root, COSE * thisMsg) +{ + COSE * walk; + + if (root == NULL) return false; + if (thisMsg == NULL) return false; + + for (walk = root; walk != NULL; walk = walk->m_handleList) { + if (walk == thisMsg) return true; + } + return false; +} + +void _COSE_RemoveFromList(COSE ** root, COSE * thisMsg) +{ + COSE * walk; + + if (*root == thisMsg) { + *root = thisMsg->m_handleList; + thisMsg->m_handleList = NULL; + return; + } + + for (walk = *root; walk->m_handleList != NULL; walk = walk->m_handleList) { + if (walk->m_handleList == thisMsg) { + walk->m_handleList = thisMsg->m_handleList; + thisMsg->m_handleList = NULL; + return; + } + } + return; +}
diff --git a/src/Encrypt.c b/src/Encrypt.c index 3e89366..26b0223 100644 --- a/src/Encrypt.c +++ b/src/Encrypt.c
@@ -10,12 +10,12 @@ byte RgbDontUse[8 * 1024]; // Remove this array when we can compute the size of a cbor serialization without this hack. +COSE * EnvelopedRoot = NULL; bool IsValidEnvelopedHandle(HCOSE_ENVELOPED h) { COSE_Enveloped * p = (COSE_Enveloped *)h; - if (p == NULL) return false; - return true; + return _COSE_IsInList(EnvelopedRoot, p); } @@ -38,6 +38,8 @@ return NULL; } + _COSE_InsertInList(&EnvelopedRoot, pobj); + return (HCOSE_ENVELOPED) pobj; } @@ -75,6 +77,8 @@ } } + if (pIn == NULL) _COSE_InsertInList(&EnvelopedRoot, pobj); + return(HCOSE_ENVELOPED) pobj; } @@ -90,6 +94,8 @@ context = ((COSE_Enveloped *)h)->m_message.m_allocContext; #endif + _COSE_RemoveFromList(&EnvelopedRoot, (COSE_Enveloped *)h); + _COSE_Enveloped_Release((COSE_Enveloped *)h); COSE_FREE((COSE_Enveloped *)h, &context);
diff --git a/src/Encrypt0.c b/src/Encrypt0.c index 939d2ff..1d5d7a8 100644 --- a/src/Encrypt0.c +++ b/src/Encrypt0.c
@@ -10,12 +10,13 @@ byte RgbDontUse[8 * 1024]; // Remove this array when we can compute the size of a cbor serialization without this hack. +COSE * EncryptRoot = NULL; + bool IsValidEncryptHandle(HCOSE_ENCRYPT h) { COSE_Encrypt * p = (COSE_Encrypt *)h; - if (p == NULL) return false; - return true; + return _COSE_IsInList(EncryptRoot, &p->m_message); } @@ -32,6 +33,8 @@ return NULL; } + _COSE_InsertInList(&EncryptRoot, &pobj->m_message); + return (HCOSE_ENCRYPT) pobj; } @@ -57,6 +60,8 @@ pRecipients = _COSE_arrayget_int(&pobj->m_message, INDEX_RECIPIENTS); CHECK_CONDITION(pRecipients == NULL, COSE_ERR_INVALID_PARAMETER); + _COSE_InsertInList(&EncryptRoot, &pobj->m_message); + return(HCOSE_ENCRYPT) pobj; } @@ -65,6 +70,7 @@ #ifdef USE_CBOR_CONTEXT cn_cbor_context context; #endif + COSE_Encrypt * pEncrypt = (COSE_Encrypt *)h; if (!IsValidEncryptHandle(h)) return false; @@ -72,8 +78,10 @@ context = ((COSE_Encrypt *)h)->m_message.m_allocContext; #endif - _COSE_Encrypt_Release((COSE_Encrypt *)h); + _COSE_Encrypt_Release(pEncrypt); + _COSE_RemoveFromList(&EncryptRoot, &pEncrypt->m_message); + COSE_FREE((COSE_Encrypt *)h, &context); return true;
diff --git a/src/cose.h b/src/cose.h index 9430f3a..99e47ae 100644 --- a/src/cose.h +++ b/src/cose.h
@@ -143,6 +143,7 @@ COSE_Parameter_KID = 4, } COSE_Constants; + /* * messages dealing with the Enveloped message type */
diff --git a/src/cose_int.h b/src/cose_int.h index d46f501..5640946 100644 --- a/src/cose_int.h +++ b/src/cose_int.h
@@ -3,7 +3,7 @@ // These definitions are here because they aren't required for the public // interface, and they were quite confusing in cn-cbor.h -typedef struct { +typedef struct _COSE { int m_flags; // Not sure what goes here yet int m_ownMsg; // Do I own the pointer @ m_cbor? int m_ownUnprotectedMap; // Do I own the pointer @ m_unportectedMap? @@ -17,6 +17,7 @@ #ifdef USE_CBOR_CONTEXT cn_cbor_context m_allocContext; #endif + struct _COSE * m_handleList; } COSE; struct _SignerInfo; @@ -125,6 +126,14 @@ extern cose_error _MapFromCBOR(cn_cbor_errback err); +/* + * Set of routines for handle checking + */ + +extern void _COSE_InsertInList(COSE ** rootNode, COSE * newMsg); +extern bool _COSE_IsInList(COSE * rootNode, COSE * thisMsg); +extern void _COSE_RemoveFromList(COSE ** rootNode, COSE * thisMsg); + extern bool IsValidEncryptHandle(HCOSE_ENCRYPT h); extern bool IsValidEnvelopedHandle(HCOSE_ENVELOPED h); extern bool IsValidRecipientHandle(HCOSE_RECIPIENT h);
diff --git a/test/test.c b/test/test.c index 69117c5..0774d6a 100644 --- a/test/test.c +++ b/test/test.c
@@ -197,7 +197,7 @@ } else if (strcmp(pKey->v.str, "IV_hex") == 0) { keyNew = COSE_Header_IV; - pValueNew = cn_cbor_data_create(FromHex(pValue->v.str, pValue->length), pValue->length / 2, CBOR_CONTEXT_PARAM_COMMA NULL); + pValueNew = cn_cbor_data_create(FromHex(pValue->v.str, pValue->length), (int) pValue->length / 2, CBOR_CONTEXT_PARAM_COMMA NULL); } else { continue;