From ee664d58dccd1506be375709af52ad3313a87073 Mon Sep 17 00:00:00 2001 From: jimsch Date: Wed, 1 Aug 2018 10:27:22 -0700 Subject: [PATCH 01/38] FIx the unit test --- test/encrypt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/encrypt.c b/test/encrypt.c index 434b4da0..e4418b1b 100644 --- a/test/encrypt.c +++ b/test/encrypt.c @@ -429,7 +429,7 @@ int _ValidateEncrypt(const cn_cbor * pControl, const byte * pbEncoded, size_t cb else fFail = false; } - if (fFail && fAlgSupport) CFails += 1; + if (fFail) CFails += 1; return fAlgSupport ? 1 : 0; returnError: From 301449aef03b4379942c558365511dade2864b53 Mon Sep 17 00:00:00 2001 From: jimsch Date: Wed, 1 Aug 2018 10:49:59 -0700 Subject: [PATCH 02/38] Correct the string for encrypt0 Failing unit tests did not find this --- src/Encrypt0.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Encrypt0.c b/src/Encrypt0.c index 2e228f4f..b6a85ec1 100644 --- a/src/Encrypt0.c +++ b/src/Encrypt0.c @@ -136,7 +136,7 @@ bool COSE_Encrypt_decrypt(HCOSE_ENCRYPT h, const byte * pbKey, size_t cbKey, cos return false; } - f = _COSE_Enveloped_decrypt(pcose, NULL, pbKey, cbKey, "Encrypt1", perr); + f = _COSE_Enveloped_decrypt(pcose, NULL, pbKey, cbKey, "Encrypt0", perr); return f; } @@ -145,7 +145,7 @@ bool COSE_Encrypt_encrypt(HCOSE_ENCRYPT h, const byte * pbKey, size_t cbKey, cos CHECK_CONDITION(IsValidEncryptHandle(h), COSE_ERR_INVALID_HANDLE); CHECK_CONDITION(pbKey != NULL, COSE_ERR_INVALID_PARAMETER); - return _COSE_Enveloped_encrypt((COSE_Encrypt *)h, pbKey, cbKey, "Encrypt1", perr); + return _COSE_Enveloped_encrypt((COSE_Encrypt *)h, pbKey, cbKey, "Encrypt0", perr); errorReturn: return false; From 38bcb457f7e58c4963c6d7609a3cf9877deca0dd Mon Sep 17 00:00:00 2001 From: jimsch Date: Wed, 1 Aug 2018 11:19:17 -0700 Subject: [PATCH 03/38] Second pass at getting this right --- test/encrypt.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/test/encrypt.c b/test/encrypt.c index e4418b1b..cf3a2835 100644 --- a/test/encrypt.c +++ b/test/encrypt.c @@ -402,7 +402,11 @@ int _ValidateEncrypt(const cn_cbor * pControl, const byte * pbEncoded, size_t cb } cn_cbor * alg = COSE_Encrypt_map_get_int(hEnc, COSE_Header_Algorithm, COSE_BOTH, NULL); - if (!IsAlgorithmSupported(alg)) fAlgSupport = false; + if (!IsAlgorithmSupported(alg)) { + fAlgSupport = false; + fFail = false; + goto exitHere; + } pFail = cn_cbor_mapget_string(pRecipients, "fail"); if (COSE_Encrypt_decrypt(hEnc, k->v.bytes, k->length, NULL)) { @@ -424,9 +428,14 @@ int _ValidateEncrypt(const cn_cbor * pControl, const byte * pbEncoded, size_t cb exitHere: - if (fFailBody) { - if (!fFail) fFail = true; - else fFail = false; + if (fAlgSupport) { + if (fFailBody) { + if (!fFail) fFail = true; + else fFail = false; + } + } + else { + fFail = false; } if (fFail) CFails += 1; From cdbbb38b79b2443ce1aa28bad26977b6666e4011 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Sat, 18 Apr 2020 08:02:38 -0700 Subject: [PATCH 04/38] checkpoint --- include/cose/cose_configure.h | 2 +- src/Cose.c | 117 ++++--------------------------- src/CounterSign.c | 127 ++++++++++++++++++++++++++++++++++ src/CounterSign0.c | 97 ++++++++++++++++++++++++++ src/cose_int.h | 2 +- test/encrypt.c | 3 +- 6 files changed, 243 insertions(+), 105 deletions(-) create mode 100644 src/CounterSign.c create mode 100644 src/CounterSign0.c diff --git a/include/cose/cose_configure.h b/include/cose/cose_configure.h index 7a1ce446..af4b8b09 100644 --- a/include/cose/cose_configure.h +++ b/include/cose/cose_configure.h @@ -148,7 +148,7 @@ #endif #endif // !defined (USE_MBED_TLS) -//#define USE_COUNTER_SIGNATURES +#define USE_COUNTER_SIGNATURES // // Define which COSE objects are included diff --git a/src/Cose.c b/src/Cose.c index 28e0469a..332f979f 100644 --- a/src/Cose.c +++ b/src/Cose.c @@ -124,6 +124,21 @@ bool _COSE_Init_From_Object(COSE *pobj, cn_cbor_map_create(CBOR_CONTEXT_PARAM_COMMA & cbor_error); CHECK_CONDITION_CBOR(pobj->m_dontSendMap != NULL, cbor_error); +#ifdef USE_COUNTER_SIGNATURES + cn_cbor* pCounter = cn_cbor_mapget_int(pobj->m_unprotectMap, COSE_Header_CounterSign); + if (pCounter != NULL) { + int i; + CHECK_CONDITION(pCounter->type == CN_CBOR_ARRAY, COSE_ERR_INVALID_PARAMETER); + cn_cbor* pSig = pCounter->first_child; + for (i=0; ilength; i++, pCounter = pCounter->next) { + cn_cbor* p = cn_cbor_decode(pSig->v.bytes, pSig->length, CBOR_CONTEXT_PARAM_COMMA & errState); + COSE_CounterSign* cs = COSE_CounterSign_Init_FromObject(p, CBOR_CONTEXT_PARAM_COMMA & errState); + + + } + } +#endif + pobj->m_ownMsg = true; pobj->m_refCount = 1; @@ -446,108 +461,6 @@ cn_cbor *_COSE_encode_protected(COSE *pMessage, cose_errback *perr) return pProtected; } -#ifdef USE_COUNTER_SIGNATURES -bool _COSE_CounterSign_add(COSE *pMessage, - HCOSE_COUNTERSIGN hSigner, - cose_errback *perr) -{ - COSE_CounterSign *pSigner = (COSE_CounterSign *)hSigner; - - CHECK_CONDITION(IsValidCounterSignHandle(hSigner), COSE_ERR_INVALID_HANDLE); - CHECK_CONDITION( - pSigner->m_signer.m_signerNext == NULL, COSE_ERR_INVALID_PARAMETER); - - pSigner = pMessage->m_counterSigners; - pMessage->m_counterSigners = pSigner; - return true; - -errorReturn: - return false; -} - -HCOSE_COUNTERSIGN _COSE_CounterSign_get(COSE *pMessage, - int iSigner, - cose_errback *perr) -{ - COSE_CounterSign *pSigner = pMessage->m_counterSigners; - int i; - - for (i = 0; i < iSigner; i++, pSigner = pSigner->m_next) { - CHECK_CONDITION(pSigner != NULL, COSE_ERR_INVALID_PARAMETER); - } - - return (HCOSE_COUNTERSIGN)pSigner; - -errorReturn: - return false; -} - -bool _COSE_CountSign_create(COSE *pMessage, - cn_cbor *pcnBody, - CBOR_CONTEXT_COMMA cose_errback *perr) -{ - cn_cbor *pArray = NULL; - cn_cbor_errback cbor_err; - COSE_CounterSign *pSigner = NULL; - cn_cbor *pcnProtected = NULL; - cn_cbor *pcn = NULL; - cn_cbor *pcn2 = NULL; - - if (pMessage->m_counterSigners == NULL) - return true; - - // One or more than one? - if (pMessage->m_counterSigners->m_signer.m_signerNext != NULL) { - pArray = cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA & cbor_err); - CHECK_CONDITION_CBOR(pArray != NULL, cbor_err); - } - - pcnProtected = _COSE_arrayget_int(pMessage, INDEX_PROTECTED); - CHECK_CONDITION(pcnProtected != NULL, COSE_ERR_INTERNAL); - - for (pSigner = pMessage->m_counterSigners; pSigner != NULL; - pSigner = pSigner->m_next) { - CHECK_CONDITION( - pSigner->m_signer.m_signerNext == NULL, COSE_ERR_INTERNAL); - - pcn = cn_cbor_data_create(pcnProtected->v.bytes, pcnProtected->v.count, - CBOR_CONTEXT_PARAM_COMMA & cbor_err); - CHECK_CONDITION_CBOR(pcnProtected != NULL, cbor_err); - - pcn2 = cn_cbor_clone(pcnBody, CBOR_CONTEXT_PARAM_COMMA & cbor_err); - CHECK_CONDITION_CBOR(pcnBody != NULL, cbor_err); - - if (!_COSE_Signer_sign(&pSigner->m_signer, pcnBody, pcn2, perr)) - goto errorReturn; - pcn = NULL; - pcn2 = NULL; - - if (pArray != NULL) { - bool f = cn_cbor_array_append( - pArray, pSigner->m_signer.m_message.m_cborRoot, &cbor_err); - CHECK_CONDITION_CBOR(f, cbor_err); - } else { - pArray = pSigner->m_signer.m_message.m_cborRoot; - } - } - - if (!_COSE_map_put(pMessage, COSE_Header_CounterSign, pArray, - COSE_UNPROTECT_ONLY, perr)) - goto errorReturn; - - return true; - -errorReturn: - if (pArray != NULL) - CN_CBOR_FREE(pArray, context); - if ((pcn != NULL) && (pcn->parent != NULL)) - CN_CBOR_FREE(pcn, context); - if ((pcn2 != NULL) && (pcn2->parent != NULL)) - CN_CBOR_FREE(pcn2, context); - return false; -} - -#endif bool _COSE_array_replace(COSE *pMessage, cn_cbor *cb_value, diff --git a/src/CounterSign.c b/src/CounterSign.c new file mode 100644 index 00000000..2950b255 --- /dev/null +++ b/src/CounterSign.c @@ -0,0 +1,127 @@ +#include +#ifndef __MBED__ +#include +#endif + +#include "cose/cose.h" +#include "cose_int.h" +#include "cose/cose_configure.h" +#include "crypto.h" + +#ifdef USE_COUNTER_SIGNATURES + +HCOSE_COUNTERSIGN COSE_CounterSign_Init_From_Object(cn_cbor* cbor, CBOR_CONTEXT_COMMA cose_errback* perr) +{ + COSE_Encrypt* pobj; + + cose_errback error = { 0 }; + if (perr == NULL) perr = &error; + + pobj = (COSE_Encrypt*)COSE_CALLOC(1, sizeof(COSE_Encrypt), context); + if (pobj == NULL) { + perr->err = COSE_ERR_OUT_OF_MEMORY; + errorReturn: + if (pobj != NULL) { + _COSE_Encrypt_Release(pobj); + COSE_FREE(pobj, context); + } + return NULL; + } + + + + +/// Add a countersignature to the list used to create the attribute +/// +bool _COSE_CounterSign_add(COSE* pMessage, HCOSE_COUNTERSIGN hSigner, cose_errback* perr) +{ + COSE_CounterSign* pSigner = (COSE_CounterSign*)hSigner; + + CHECK_CONDITION(IsValidCounterSignHandle(hSigner), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION(pSigner->m_signer.m_signerNext == NULL, COSE_ERR_INVALID_PARAMETER); + + pSigner = pMessage->m_counterSigners; + pMessage->m_counterSigners = pSigner; + return true; + +errorReturn: + return false; +} + +/// Get the n-th counter signature from the attribute. +/// +HCOSE_COUNTERSIGN _COSE_CounterSign_get(COSE* pMessage, int iSigner, cose_errback* perr) +{ + COSE_CounterSign* pSigner = pMessage->m_counterSigners; + int i; + + for (i = 0; i < iSigner; i++, pSigner = pSigner->m_next) { + CHECK_CONDITION(pSigner != NULL, COSE_ERR_INVALID_PARAMETER); + } + + return (HCOSE_COUNTERSIGN)pSigner; + +errorReturn: + return false; +} + +/// _COSE_CountSign_create +/// +/// Create the CounterSign attribute based on the set of countersignatures added to the message. +/// + +bool _COSE_CountSign_create(COSE* pMessage, cn_cbor* pcnBody, CBOR_CONTEXT_COMMA cose_errback* perr) +{ + cn_cbor* pArray = NULL; + cn_cbor_errback cbor_err; + COSE_CounterSign* pSigner = NULL; + cn_cbor* pcnProtected = NULL; + cn_cbor* pcn = NULL; + cn_cbor* pcn2 = NULL; + + if (pMessage->m_counterSigners == NULL) return true; + + // One or more than one? + if (pMessage->m_counterSigners->m_signer.m_signerNext != NULL) { + pArray = cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA & cbor_err); + CHECK_CONDITION_CBOR(pArray != NULL, cbor_err); + } + + pcnProtected = _COSE_arrayget_int(pMessage, INDEX_PROTECTED); + CHECK_CONDITION(pcnProtected != NULL, COSE_ERR_INTERNAL); + + for (pSigner = pMessage->m_counterSigners; pSigner != NULL; pSigner = pSigner->m_next) { + CHECK_CONDITION(pSigner->m_signer.m_signerNext == NULL, COSE_ERR_INTERNAL); + + pcn = cn_cbor_data_create(pcnProtected->v.bytes, pcnProtected->length, CBOR_CONTEXT_PARAM_COMMA & cbor_err); + CHECK_CONDITION_CBOR(pcnProtected != NULL, cbor_err); + + pcn2 = cn_cbor_clone(pcnBody, CBOR_CONTEXT_PARAM_COMMA & cbor_err); + CHECK_CONDITION_CBOR(pcnBody != NULL, cbor_err); + + if (!_COSE_Signer_sign(&pSigner->m_signer, pcnBody, pcn2, perr)) goto errorReturn; + pcn = NULL; + pcn2 = NULL; + + if (pArray != NULL) { + bool f = cn_cbor_array_append(pArray, pSigner->m_signer.m_message.m_cborRoot, &cbor_err); + CHECK_CONDITION_CBOR(f, cbor_err); + } + else { + pArray = pSigner->m_signer.m_message.m_cborRoot; + } + } + + if (!_COSE_map_put(pMessage, COSE_Header_CounterSign, pArray, COSE_UNPROTECT_ONLY, perr)) goto errorReturn; + + return true; + +errorReturn: + if (pArray != NULL) CN_CBOR_FREE(pArray, context); + if ((pcn != NULL) && (pcn->parent != NULL)) CN_CBOR_FREE(pcn, context); + if ((pcn2 != NULL) && (pcn2->parent != NULL)) CN_CBOR_FREE(pcn2, context); + return false; +} + +#endif + diff --git a/src/CounterSign0.c b/src/CounterSign0.c new file mode 100644 index 00000000..c88ffe17 --- /dev/null +++ b/src/CounterSign0.c @@ -0,0 +1,97 @@ +#include +#ifndef __MBED__ +#include +#endif + +#include "cose/cose.h" +#include "cose_int.h" +#include "cose/cose_configure.h" +#include "crypto.h" + +#ifdef USE_COUNTER_SIGNATURE0 +bool _COSE_CounterSign_add(COSE* pMessage, HCOSE_COUNTERSIGN hSigner, cose_errback* perr) +{ + COSE_CounterSign* pSigner = (COSE_CounterSign*)hSigner; + + CHECK_CONDITION(IsValidCounterSignHandle(hSigner), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION(pSigner->m_signer.m_signerNext == NULL, COSE_ERR_INVALID_PARAMETER); + + pSigner = pMessage->m_counterSigners; + pMessage->m_counterSigners = pSigner; + return true; + +errorReturn: + return false; +} + +HCOSE_COUNTERSIGN _COSE_CounterSign_get(COSE* pMessage, int iSigner, cose_errback* perr) +{ + COSE_CounterSign* pSigner = pMessage->m_counterSigners; + int i; + + for (i = 0; i < iSigner; i++, pSigner = pSigner->m_next) { + CHECK_CONDITION(pSigner != NULL, COSE_ERR_INVALID_PARAMETER); + } + + return (HCOSE_COUNTERSIGN)pSigner; + +errorReturn: + return false; +} + +bool _COSE_CountSign_create(COSE* pMessage, cn_cbor* pcnBody, CBOR_CONTEXT_COMMA cose_errback* perr) +{ + cn_cbor* pArray = NULL; + cn_cbor_errback cbor_err; + COSE_CounterSign* pSigner = NULL; + cn_cbor* pcnProtected = NULL; + cn_cbor* pcn = NULL; + cn_cbor* pcn2 = NULL; + + if (pMessage->m_counterSigners == NULL) return true; + + // One or more than one? + if (pMessage->m_counterSigners->m_signer.m_signerNext != NULL) { + pArray = cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA & cbor_err); + CHECK_CONDITION_CBOR(pArray != NULL, cbor_err); + } + + pcnProtected = _COSE_arrayget_int(pMessage, INDEX_PROTECTED); + CHECK_CONDITION(pcnProtected != NULL, COSE_ERR_INTERNAL); + + for (pSigner = pMessage->m_counterSigners; pSigner != NULL; pSigner = pSigner->m_next) { + CHECK_CONDITION(pSigner->m_signer.m_signerNext == NULL, COSE_ERR_INTERNAL); + + pcn = cn_cbor_data_create(pcnProtected->v.bytes, pcnProtected->v.count, CBOR_CONTEXT_PARAM_COMMA & cbor_err); + CHECK_CONDITION_CBOR(pcnProtected != NULL, cbor_err); + + pcn2 = cn_cbor_clone(pcnBody, CBOR_CONTEXT_PARAM_COMMA & cbor_err); + CHECK_CONDITION_CBOR(pcnBody != NULL, cbor_err); + + if (!_COSE_Signer_sign(&pSigner->m_signer, pcnBody, pcn2, perr)) goto errorReturn; + pcn = NULL; + pcn2 = NULL; + + if (pArray != NULL) { + bool f = cn_cbor_array_append(pArray, pSigner->m_signer.m_message.m_cborRoot, &cbor_err); + CHECK_CONDITION_CBOR(f, cbor_err); + } + else { + pArray = pSigner->m_signer.m_message.m_cborRoot; + } + } + + if (!_COSE_map_put(pMessage, COSE_Header_CounterSign, pArray, COSE_UNPROTECT_ONLY, perr)) goto errorReturn; + + return true; + +errorReturn: + if (pArray != NULL) CN_CBOR_FREE(pArray, context); + if ((pcn != NULL) && (pcn->parent != NULL)) CN_CBOR_FREE(pcn, context); + if ((pcn2 != NULL) && (pcn2->parent != NULL)) CN_CBOR_FREE(pcn2, context); + return false; +} + +#endif + + diff --git a/src/cose_int.h b/src/cose_int.h index 4e3b14a2..e9a6e566 100644 --- a/src/cose_int.h +++ b/src/cose_int.h @@ -37,7 +37,7 @@ typedef struct _COSE { #endif struct _COSE *m_handleList; #ifdef USE_COUNTER_SIGNATURES - COSE_CounterSign *m_counterSigners; + COSE_CounterSign *m_counterSigners; // Linked list of all counter signatures #endif } COSE; diff --git a/test/encrypt.c b/test/encrypt.c index 98e40298..43a9b928 100644 --- a/test/encrypt.c +++ b/test/encrypt.c @@ -543,7 +543,8 @@ int _ValidateEncrypt(const cn_cbor *pControl, size_t cb; byte *pb; pb = COSE_Encrypt_GetContent(hEnc, &cb, NULL); - } else { + } + else { if (fAlgSupport) { fFail = true; fAlgSupport = false; From 906e8c62fb13cc6e80357fce20fc417dd32839d1 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Fri, 24 Apr 2020 09:39:19 -0700 Subject: [PATCH 05/38] checkpoint --- include/cose/cose.h | 15 ++- include/cose/cose_configure.h | 4 +- src/CMakeLists.txt | 2 + src/Cose.c | 31 ++++-- src/CounterSign.c | 198 +++++++++++++++++++++++++++++++--- src/CounterSign0.c | 30 +++--- src/Encrypt.c | 2 + src/Sign.c | 10 +- src/SignerInfo.c | 36 +++++-- src/cose_int.h | 15 ++- test/context.c | 51 +++++++++ test/sign.c | 77 +++++++++++++ test/test.c | 59 +++++++++- test/test.h | 1 + 14 files changed, 475 insertions(+), 56 deletions(-) diff --git a/include/cose/cose.h b/include/cose/cose.h index 471a102a..753ff86b 100644 --- a/include/cose/cose.h +++ b/include/cose/cose.h @@ -20,6 +20,7 @@ typedef struct _cose_recipient* HCOSE_RECIPIENT; typedef struct _cose_mac* HCOSE_MAC; typedef struct _cose_mac0* HCOSE_MAC0; typedef struct _cose_counterSignature* HCOSE_COUNTERSIGN; +typedef struct _cose_counterSignature1* HCOSE_COUNTERSIGN1; /** * All of the different kinds of errors @@ -70,7 +71,8 @@ typedef enum { COSE_encrypt_object = 16, COSE_mac_object = 97, COSE_mac0_object = 17, - COSE_recipient_object = -1 + COSE_recipient_object = -1, + COSE_countersign_object = -2, } COSE_object_type; // Generic functions for the COSE library @@ -538,7 +540,7 @@ bool COSE_Sign1_map_put_int(HCOSE_SIGN1 cose, * Counter Signature Routines */ -HCOSE_COUNTERSIGN COSE_CounterSign_Init(COSE_INIT_FLAGS flags, +HCOSE_COUNTERSIGN COSE_CounterSign_Init( CBOR_CONTEXT_COMMA cose_errback* perr); bool COSE_CounterSign_Free(HCOSE_COUNTERSIGN cose); @@ -552,6 +554,15 @@ bool COSE_CounterSign_map_put_int(HCOSE_COUNTERSIGN cose, int flags, cose_errback* errp); +bool COSE_CounterSign_SetExternal(HCOSE_COUNTERSIGN cose, const byte* pbExternalData, size_t cbExternalData, cose_errback* perr); + +// HCOSE_COUNTERSIGN COSE_Signer_add_countersignature(HCOSE_SIGNER hSigner, const cn_cbor* pkey, cose_errback* perr); +HCOSE_COUNTERSIGN COSE_Signer_add_countersignature(HCOSE_SIGNER hSigner, HCOSE_COUNTERSIGN hCountersignature, cose_errback* perr); +bool COSE_CounterSign_validate(HCOSE_SIGNER hSigner, HCOSE_COUNTERSIGN hCountersignature, cose_errback* perr); +bool COSE_CounterSign_SetKey(HCOSE_COUNTERSIGN, const cn_cbor* pkey, cose_errback* perr); + +HCOSE_COUNTERSIGN COSE_Signer_get_countersignature(HCOSE_SIGNER hSigner, int index, cose_errback* perr); + /* */ diff --git a/include/cose/cose_configure.h b/include/cose/cose_configure.h index af4b8b09..5569b479 100644 --- a/include/cose/cose_configure.h +++ b/include/cose/cose_configure.h @@ -143,12 +143,12 @@ // Requires OPEN SSL 1.1.1 to build #define USE_EDDSA #else -#pragma message("OPENSSL VERSION IS ") -#pragma message(OPENSSL_VERISON_NUMBER) +#pragma message("OPENSSL VERSION IS " #OPENSSL_VERISON_NUMBER) #endif #endif // !defined (USE_MBED_TLS) #define USE_COUNTER_SIGNATURES +// #define USE_COUNTER_SIGNATURE1 // // Define which COSE objects are included diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c41e5556..e1a379f4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -32,6 +32,8 @@ set(cose_sources cose_int.h crypto.h Cose.c + CounterSign.c + CounterSign0.c MacMessage.c MacMessage0.c Sign.c diff --git a/src/Cose.c b/src/Cose.c index 332f979f..31a4cc18 100644 --- a/src/Cose.c +++ b/src/Cose.c @@ -129,12 +129,20 @@ bool _COSE_Init_From_Object(COSE *pobj, if (pCounter != NULL) { int i; CHECK_CONDITION(pCounter->type == CN_CBOR_ARRAY, COSE_ERR_INVALID_PARAMETER); - cn_cbor* pSig = pCounter->first_child; - for (i=0; ilength; i++, pCounter = pCounter->next) { - cn_cbor* p = cn_cbor_decode(pSig->v.bytes, pSig->length, CBOR_CONTEXT_PARAM_COMMA & errState); - COSE_CounterSign* cs = COSE_CounterSign_Init_FromObject(p, CBOR_CONTEXT_PARAM_COMMA & errState); - - + CHECK_CONDITION(pCounter->length > 0, COSE_ERR_INVALID_PARAMETER); + if (pCounter->first_child->type == CN_CBOR_ARRAY) { + + cn_cbor* pSig = pCounter->first_child; + for (i = 0; i < pCounter->length; i++, pCounter = pCounter->next) { + cn_cbor* p = cn_cbor_decode(pSig->v.bytes, pSig->length, CBOR_CONTEXT_PARAM_COMMA & errState); + COSE_CounterSign* cs = _COSE_CounterSign_Init_From_Object(p, NULL, CBOR_CONTEXT_PARAM_COMMA perr); + cs = pobj->m_counterSigners; + pobj->m_counterSigners = cs; + } + } + else { + COSE_CounterSign* cs = _COSE_CounterSign_Init_From_Object(pCounter, NULL, CBOR_CONTEXT_PARAM_COMMA perr); + pobj->m_counterSigners = cs; } } #endif @@ -163,6 +171,17 @@ void _COSE_Release(COSE *pobj) if (pobj->m_ownMsg && (pobj->m_cborRoot != NULL) && (pobj->m_cborRoot->parent == NULL)) CN_CBOR_FREE(pobj->m_cborRoot, context); + + if (pobj->m_counterSigners != NULL) { + COSE_CounterSign* p = pobj->m_counterSigners; + COSE_CounterSign* p2 = NULL; + + while (p != NULL) { + p2 = p->m_next; + COSE_CounterSign_Free((HCOSE_COUNTERSIGN)p); + p = p2; + } + } } HCOSE COSE_Decode(const byte *rgbData, diff --git a/src/CounterSign.c b/src/CounterSign.c index 2950b255..a45f91f8 100644 --- a/src/CounterSign.c +++ b/src/CounterSign.c @@ -10,25 +10,102 @@ #ifdef USE_COUNTER_SIGNATURES -HCOSE_COUNTERSIGN COSE_CounterSign_Init_From_Object(cn_cbor* cbor, CBOR_CONTEXT_COMMA cose_errback* perr) +COSE* CountersignRoot = NULL; + +bool IsValidCounterSignHandle(HCOSE_COUNTERSIGN h) +{ + COSE_CounterSign* p = (COSE_CounterSign*)h; + return _COSE_IsInList(CountersignRoot, &p->m_signer.m_message); +} + +bool _COSE_CounterSign_Free(COSE_CounterSign* pSigner) { - COSE_Encrypt* pobj; + if (pSigner->m_signer.m_message.m_refCount > 1) { + pSigner->m_signer.m_message.m_refCount--; + return true; + } + + _COSE_RemoveFromList(&CountersignRoot, &pSigner->m_signer.m_message); + + _COSE_SignerInfo_Release(&pSigner->m_signer); + + COSE_FREE(pSigner, &pSigner->m_signer.m_message.m_allocContext); + + return true; +} + +COSE_CounterSign * _COSE_CounterSign_Init_From_Object(cn_cbor* cbor, COSE_CounterSign * pIn, CBOR_CONTEXT_COMMA cose_errback* perr) +{ + COSE_CounterSign* pobj = pIn; cose_errback error = { 0 }; if (perr == NULL) perr = &error; - pobj = (COSE_Encrypt*)COSE_CALLOC(1, sizeof(COSE_Encrypt), context); if (pobj == NULL) { - perr->err = COSE_ERR_OUT_OF_MEMORY; - errorReturn: - if (pobj != NULL) { - _COSE_Encrypt_Release(pobj); - COSE_FREE(pobj, context); - } + pobj = (COSE_CounterSign*)COSE_CALLOC(1, sizeof(COSE_CounterSign), context); + CHECK_CONDITION(pobj != NULL, COSE_ERR_OUT_OF_MEMORY); + } + + CHECK_CONDITION(cbor->type == CN_CBOR_ARRAY, COSE_ERR_INVALID_PARAMETER); + if (!_COSE_SignerInfo_Init_From_Object(cbor, &pobj->m_signer, CBOR_CONTEXT_PARAM_COMMA perr)) { + goto errorReturn; + } + + _COSE_InsertInList(&CountersignRoot, &pobj->m_signer.m_message); + + return pobj; + +errorReturn: + if (pobj != NULL) { + _COSE_CounterSign_Free(pobj); + } + return NULL; +} + +bool _COSE_CounterSign_Init(COSE_CounterSign* pobject, CBOR_CONTEXT_COMMA cose_errback* perror) +{ + return _COSE_SignerInfo_Init(COSE_INIT_FLAGS_NO_CBOR_TAG, &pobject->m_signer, COSE_countersign_object, CBOR_CONTEXT_PARAM_COMMA perror); +} + +HCOSE_COUNTERSIGN COSE_CounterSign_Init(CBOR_CONTEXT_COMMA cose_errback* perror) +{ + COSE_CounterSign* pobject = (COSE_CounterSign*)COSE_CALLOC(1, sizeof(COSE_CounterSign), context); + if (pobject == NULL) { + if (perror != NULL) { + perror->err = COSE_ERR_OUT_OF_MEMORY; + } return NULL; } + if (!_COSE_CounterSign_Init(pobject, CBOR_CONTEXT_PARAM_COMMA perror)) { + _COSE_CounterSign_Free(pobject); + return NULL; + } + + _COSE_InsertInList(&CountersignRoot, &pobject->m_signer.m_message); + return (HCOSE_COUNTERSIGN) pobject; +} + + +bool COSE_CounterSign_Free(HCOSE_COUNTERSIGN h) +{ + COSE_CounterSign* p = (COSE_CounterSign*)h; + bool fRet = false; + + if (!IsValidCounterSignHandle(h)) { + goto errorReturn; + } + + if (p->m_signer.m_message.m_refCount > 1) { + p->m_signer.m_message.m_refCount--; + return true; + } + fRet = _COSE_CounterSign_Free(p); + +errorReturn: + return fRet; +} /// Add a countersignature to the list used to create the attribute @@ -38,9 +115,9 @@ bool _COSE_CounterSign_add(COSE* pMessage, HCOSE_COUNTERSIGN hSigner, cose_errba COSE_CounterSign* pSigner = (COSE_CounterSign*)hSigner; CHECK_CONDITION(IsValidCounterSignHandle(hSigner), COSE_ERR_INVALID_HANDLE); - CHECK_CONDITION(pSigner->m_signer.m_signerNext == NULL, COSE_ERR_INVALID_PARAMETER); + CHECK_CONDITION(pSigner->m_signer.m_message.m_counterSigners == NULL, COSE_ERR_INVALID_PARAMETER); - pSigner = pMessage->m_counterSigners; + pSigner->m_signer.m_message.m_counterSigners = pMessage->m_counterSigners; pMessage->m_counterSigners = pSigner; return true; @@ -48,6 +125,23 @@ bool _COSE_CounterSign_add(COSE* pMessage, HCOSE_COUNTERSIGN hSigner, cose_errba return false; } + +HCOSE_COUNTERSIGN COSE_Signer_add_countersignature(HCOSE_SIGNER hSigner, HCOSE_COUNTERSIGN hCountersign, cose_errback* perr) +{ + CHECK_CONDITION(IsValidSignerHandle(hSigner), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION(IsValidCounterSignHandle(hCountersign), COSE_ERR_INVALID_HANDLE); + + if (!_COSE_CounterSign_add(&((COSE_SignerInfo*)hSigner)->m_message, hCountersign, perr)) { + goto errorReturn; + } + + return hCountersign; + +errorReturn: + return NULL; +} + + /// Get the n-th counter signature from the attribute. /// HCOSE_COUNTERSIGN _COSE_CounterSign_get(COSE* pMessage, int iSigner, cose_errback* perr) @@ -65,12 +159,12 @@ HCOSE_COUNTERSIGN _COSE_CounterSign_get(COSE* pMessage, int iSigner, cose_errbac return false; } -/// _COSE_CountSign_create +/// _COSE_CounterSign_create /// /// Create the CounterSign attribute based on the set of countersignatures added to the message. /// -bool _COSE_CountSign_create(COSE* pMessage, cn_cbor* pcnBody, CBOR_CONTEXT_COMMA cose_errback* perr) +bool _COSE_CounterSign_create(COSE* pMessage, cn_cbor* pcnBody, CBOR_CONTEXT_COMMA cose_errback* perr) { cn_cbor* pArray = NULL; cn_cbor_errback cbor_err; @@ -99,7 +193,7 @@ bool _COSE_CountSign_create(COSE* pMessage, cn_cbor* pcnBody, CBOR_CONTEXT_COMMA pcn2 = cn_cbor_clone(pcnBody, CBOR_CONTEXT_PARAM_COMMA & cbor_err); CHECK_CONDITION_CBOR(pcnBody != NULL, cbor_err); - if (!_COSE_Signer_sign(&pSigner->m_signer, pcnBody, pcn2, perr)) goto errorReturn; + if (!_COSE_Signer_sign(&pSigner->m_signer, pcnBody, pcn2, "CounterSignature", perr)) goto errorReturn; pcn = NULL; pcn2 = NULL; @@ -123,5 +217,81 @@ bool _COSE_CountSign_create(COSE* pMessage, cn_cbor* pcnBody, CBOR_CONTEXT_COMMA return false; } +bool COSE_CounterSign_SetKey(HCOSE_COUNTERSIGN h, const cn_cbor* pkey, cose_errback* perr) +{ + bool fRet = false; + CHECK_CONDITION(IsValidCounterSignHandle(h), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION(pkey != NULL, COSE_ERR_INVALID_PARAMETER); + + COSE_CounterSign* p = (COSE_CounterSign*)h; + p->m_signer.m_pkey = pkey; + + fRet = true; +errorReturn: + return fRet; +} + + +COSE_CounterSign* _COSE_Message_get_countersignature(COSE* pMessage, int index, cose_errback* perr) +{ + CHECK_CONDITION(pMessage->m_counterSigners != NULL, COSE_ERR_INVALID_PARAMETER); + + COSE_CounterSign* pCounterSign = pMessage->m_counterSigners; + + for (int i = 0; i < index; i++) { + pCounterSign = pCounterSign->m_next; + CHECK_CONDITION(pCounterSign != NULL, COSE_ERR_INVALID_PARAMETER); + + } + + pCounterSign->m_signer.m_message.m_refCount += 1; + + return pCounterSign; + +errorReturn: + return NULL; +} + +HCOSE_COUNTERSIGN COSE_Signer_get_countersignature(HCOSE_SIGNER hSigner, int index, cose_errback* perr) +{ + COSE_CounterSign* p = NULL; + + CHECK_CONDITION(IsValidSignerHandle(hSigner), COSE_ERR_INVALID_HANDLE); + + p = _COSE_Message_get_countersignature(&((COSE_SignerInfo*)hSigner)->m_message, index, perr); + + errorReturn: + return (HCOSE_COUNTERSIGN)p; +} + +bool COSE_CounterSign_validate(HCOSE_SIGNER hSigner, HCOSE_COUNTERSIGN hCountersignature, cose_errback* perr) +{ + bool f; + COSE_SignerInfo* pSigner; + COSE_CounterSign* pCountersign; + const cn_cbor* cnContent; + const cn_cbor* cnProtected; + + CHECK_CONDITION(IsValidSignerHandle(hSigner), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION(IsValidCounterSignHandle(hCountersignature), COSE_ERR_INVALID_HANDLE); + + pSigner = (COSE_SignerInfo*)hSigner; + pCountersign = (COSE_CounterSign*)hCountersignature; + + cnContent = _COSE_arrayget_int(&pSigner->m_message, INDEX_BODY); + CHECK_CONDITION(cnContent != NULL && cnContent->type == CN_CBOR_BYTES, COSE_ERR_INVALID_PARAMETER); + + cnProtected = _COSE_arrayget_int(&pSigner->m_message, INDEX_PROTECTED); + CHECK_CONDITION(cnProtected != NULL && cnProtected->type == CN_CBOR_BYTES, COSE_ERR_INVALID_PARAMETER); + + f = _COSE_Signer_validate(&pCountersign->m_signer, cnContent, cnProtected, "CounterSignature", perr); + + return f; + +errorReturn: + return false; +} + + #endif diff --git a/src/CounterSign0.c b/src/CounterSign0.c index c88ffe17..649370f2 100644 --- a/src/CounterSign0.c +++ b/src/CounterSign0.c @@ -8,42 +8,38 @@ #include "cose/cose_configure.h" #include "crypto.h" -#ifdef USE_COUNTER_SIGNATURE0 -bool _COSE_CounterSign_add(COSE* pMessage, HCOSE_COUNTERSIGN hSigner, cose_errback* perr) +#ifdef USE_COUNTER_SIGNATURE1 + +extern bool IsValidCounterSign1Handle(HCOSE_COUNTERSIGN1 h); + + +bool _COSE_CounterSign1_add(COSE* pMessage, HCOSE_COUNTERSIGN1 hSigner, cose_errback* perr) { - COSE_CounterSign* pSigner = (COSE_CounterSign*)hSigner; + COSE_CounterSign1* pSigner = (COSE_CounterSign1*)hSigner; - CHECK_CONDITION(IsValidCounterSignHandle(hSigner), COSE_ERR_INVALID_HANDLE); - CHECK_CONDITION(pSigner->m_signer.m_signerNext == NULL, COSE_ERR_INVALID_PARAMETER); + CHECK_CONDITION(IsValidCounterSign1Handle(hSigner), COSE_ERR_INVALID_HANDLE); - pSigner = pMessage->m_counterSigners; - pMessage->m_counterSigners = pSigner; + pMessage->m_counterSign1 = pSigner; return true; errorReturn: return false; } -HCOSE_COUNTERSIGN _COSE_CounterSign_get(COSE* pMessage, int iSigner, cose_errback* perr) +HCOSE_COUNTERSIGN _COSE_CounterSign1_get(COSE* pMessage, cose_errback* perr) { - COSE_CounterSign* pSigner = pMessage->m_counterSigners; - int i; + UNUSED(perr); - for (i = 0; i < iSigner; i++, pSigner = pSigner->m_next) { - CHECK_CONDITION(pSigner != NULL, COSE_ERR_INVALID_PARAMETER); - } + COSE_CounterSign1* pSigner = pMessage->m_counterSign1; return (HCOSE_COUNTERSIGN)pSigner; - -errorReturn: - return false; } bool _COSE_CountSign_create(COSE* pMessage, cn_cbor* pcnBody, CBOR_CONTEXT_COMMA cose_errback* perr) { cn_cbor* pArray = NULL; cn_cbor_errback cbor_err; - COSE_CounterSign* pSigner = NULL; + COSE_CounterSign1* pSigner = NULL; cn_cbor* pcnProtected = NULL; cn_cbor* pcn = NULL; cn_cbor* pcn2 = NULL; diff --git a/src/Encrypt.c b/src/Encrypt.c index 1d36a22b..6d290176 100644 --- a/src/Encrypt.c +++ b/src/Encrypt.c @@ -655,11 +655,13 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, goto errorReturn; #ifdef USE_COUNTER_SIGNATURES + /* // Setup Counter Signatures if (!_COSE_CountSign_create( &pcose->m_message, NULL, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; } + */ #endif // Build authenticated data diff --git a/src/Sign.c b/src/Sign.c index f4d3c23b..766f53fb 100644 --- a/src/Sign.c +++ b/src/Sign.c @@ -156,7 +156,10 @@ void _COSE_Sign_Release(COSE_SignMessage *p) for (pSigner = p->m_signerFirst; pSigner != NULL; pSigner = pSigner2) { pSigner2 = pSigner->m_signerNext; - _COSE_SignerInfo_Free(pSigner); + _COSE_SignerInfo_Release(pSigner); + if (pSigner->m_message.m_refCount == 0) { + COSE_FREE(pSigner, &pSigner->m_message.m_allocContext); + } } _COSE_Release(&p->m_message); @@ -287,8 +290,9 @@ bool COSE_Sign_Sign(HCOSE_SIGN h, cose_errback *perr) for (pSigner = pMessage->m_signerFirst; pSigner != NULL; pSigner = pSigner->m_signerNext) { - if (!_COSE_Signer_sign(pSigner, pcborBody, pcborProtected, perr)) + if (!_COSE_Signer_sign(pSigner, pcborBody, pcborProtected, "Signature", perr)) { goto errorReturn; + } } return true; @@ -318,7 +322,7 @@ bool COSE_Sign_validate(HCOSE_SIGN hSign, CHECK_CONDITION(cnProtected != NULL && cnProtected->type == CN_CBOR_BYTES, COSE_ERR_INVALID_PARAMETER); - f = _COSE_Signer_validate(pSign, pSigner, cnContent, cnProtected, perr); + f = _COSE_Signer_validate(pSigner, cnContent, cnProtected, "Signature", perr); return f; diff --git a/src/SignerInfo.c b/src/SignerInfo.c index 824c7961..dafcea1a 100644 --- a/src/SignerInfo.c +++ b/src/SignerInfo.c @@ -49,7 +49,7 @@ bool COSE_Signer_Free(HCOSE_SIGNER hSigner) return true; } - _COSE_SignerInfo_Free(pSigner); + _COSE_SignerInfo_Release(pSigner); _COSE_RemoveFromList(&SignerRoot, &pSigner->m_message); @@ -72,7 +72,7 @@ HCOSE_SIGNER COSE_Signer_Init(CBOR_CONTEXT_COMMA cose_errback *perror) if (!_COSE_SignerInfo_Init(COSE_INIT_FLAGS_NO_CBOR_TAG, pobj, COSE_recipient_object, CBOR_CONTEXT_PARAM_COMMA perror)) { - _COSE_SignerInfo_Free(pobj); + _COSE_SignerInfo_Release(pobj); COSE_FREE(pobj, context); return NULL; } @@ -108,14 +108,17 @@ COSE_SignerInfo *_COSE_SignerInfo_Init_From_Object(cn_cbor *cbor, &pSigner->m_message, cbor, CBOR_CONTEXT_PARAM_COMMA perr)) goto errorReturn; - _COSE_InsertInList(&SignerRoot, &pSigner->m_message); + if (pIn == NULL) { + _COSE_InsertInList(&SignerRoot, &pSigner->m_message); + } return pSigner; errorReturn: if (pSigner != NULL) { - _COSE_SignerInfo_Free(pSigner); - if (pIn == NULL) + _COSE_SignerInfo_Release(pSigner); + if (pIn == NULL) { COSE_FREE(pSigner, context); + } } return NULL; } @@ -126,6 +129,7 @@ static bool BuildToBeSigned(byte **ppbToSign, const cn_cbor *pcborProtected, const cn_cbor *pcborProtectedSign, const byte *pbExternal, + const char const *contextString, size_t cbExternal, CBOR_CONTEXT_COMMA cose_errback *perr) { @@ -140,7 +144,7 @@ static bool BuildToBeSigned(byte **ppbToSign, CHECK_CONDITION_CBOR(pArray != NULL, cbor_error); cn = cn_cbor_string_create( - "Signature", CBOR_CONTEXT_PARAM_COMMA & cbor_error); + contextString, CBOR_CONTEXT_PARAM_COMMA & cbor_error); CHECK_CONDITION_CBOR(cn != NULL, cbor_error); CHECK_CONDITION_CBOR( cn_cbor_array_append(pArray, cn, &cbor_error), cbor_error); @@ -211,6 +215,7 @@ static bool BuildToBeSigned(byte **ppbToSign, bool _COSE_Signer_sign(COSE_SignerInfo *pSigner, const cn_cbor *pcborBody, const cn_cbor *pcborProtected, + const char const * contextString, cose_errback *perr) { #ifdef USE_CBOR_CONTEXT @@ -248,7 +253,7 @@ bool _COSE_Signer_sign(COSE_SignerInfo *pSigner, if (!BuildToBeSigned(&pbToSign, &cbToSign, pcborBody, pcborProtected, pcborProtectedSign, pSigner->m_message.m_pbExternal, - pSigner->m_message.m_cbExternal, CBOR_CONTEXT_PARAM_COMMA perr)) + pSigner->m_message.m_cbExternal, contextString, CBOR_CONTEXT_PARAM_COMMA perr)) goto errorReturn; switch (alg) { @@ -288,6 +293,20 @@ bool _COSE_Signer_sign(COSE_SignerInfo *pSigner, FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM); } + if (pSigner->m_message.m_counterSigners != NULL) { + cn_cbor* pSignature = _COSE_arrayget_int(&pSigner->m_message, INDEX_SIGNATURE); + + + COSE_CounterSign* pCountersign = pSigner->m_message.m_counterSigners; + for (; pCountersign != NULL; pCountersign = pCountersign->m_signer.m_message.m_counterSigners) { + pcborProtectedSign = _COSE_encode_protected(&pSigner->m_message, perr); + if (pcborProtectedSign == NULL) goto errorReturn; + if (!_COSE_Signer_sign(&pCountersign->m_signer, pSignature, pcborProtectedSign, "CounterSignature", perr)) { + goto errorReturn; + } + } + } + fRet = true; errorReturn: @@ -349,6 +368,7 @@ bool _COSE_Signer_validate(COSE_SignMessage *pSign, COSE_SignerInfo *pSigner, const cn_cbor *pcborBody, const cn_cbor *pcborProtected, + const char const * contextString, cose_errback *perr) { byte *pbToBeSigned = NULL; @@ -386,7 +406,7 @@ bool _COSE_Signer_validate(COSE_SignMessage *pSign, // Build authenticated data if (!BuildToBeSigned(&pbToBeSigned, &cbToBeSigned, pcborBody, pcborProtected, cnProtected, pSigner->m_message.m_pbExternal, - pSigner->m_message.m_cbExternal, CBOR_CONTEXT_PARAM_COMMA perr)) + pSigner->m_message.m_cbExternal, contextString, CBOR_CONTEXT_PARAM_COMMA perr)) goto errorReturn; cn_cbor *cnSignature = diff --git a/src/cose_int.h b/src/cose_int.h index e9a6e566..d26fe0ee 100644 --- a/src/cose_int.h +++ b/src/cose_int.h @@ -12,6 +12,10 @@ struct _COSE_COUNTER_SIGN; typedef struct _COSE_COUNTER_SIGN COSE_CounterSign; #endif +#ifdef USE_COUNTER_SIGNATURE1 +struct _COSE_COUNTER_SIGN1; +typedef struct _COSE_COUNTER_SIGN0 COSE_CounterSign1; +#endif #define UNUSED(x) ((void)(x)) @@ -39,6 +43,9 @@ typedef struct _COSE { #ifdef USE_COUNTER_SIGNATURES COSE_CounterSign *m_counterSigners; // Linked list of all counter signatures #endif +#ifdef USE_COUNTER_SIGNATURE1 + COSE_CounterSign1* m_counterSign1; +#endif } COSE; struct _SignerInfo; @@ -257,6 +264,7 @@ void _COSE_Sign_Release(COSE_SignMessage *p); // Signer items +<<<<<<< HEAD bool _COSE_SignerInfo_Init(COSE_INIT_FLAGS flags, COSE_SignerInfo *pcose, int msgType, @@ -264,15 +272,17 @@ bool _COSE_SignerInfo_Init(COSE_INIT_FLAGS flags, bool _COSE_Signer_sign(COSE_SignerInfo *pSigner, const cn_cbor *pcborBody, const cn_cbor *pcborProtected, - cose_errback *perr); + const char * const contextString, + cose_errback *perr); COSE_SignerInfo *_COSE_SignerInfo_Init_From_Object(cn_cbor *cbor, COSE_SignerInfo *pIn, CBOR_CONTEXT_COMMA cose_errback *perr); -bool _COSE_SignerInfo_Free(COSE_SignerInfo *pSigner); +bool _COSE_SignerInfo_Release(COSE_SignerInfo *pSigner); bool _COSE_Signer_validate(COSE_SignMessage *pSign, COSE_SignerInfo *pSigner, const cn_cbor *pbContent, const cn_cbor *pbProtected, + const char * const contextString, cose_errback *perr); // Sign1 items @@ -319,6 +329,7 @@ bool _COSE_CounterSign_add(COSE *pMessage, bool _COSE_CountSign_create(COSE *pMessage, cn_cbor *pcnBody, CBOR_CONTEXT_COMMA cose_errback *perr); +COSE_CounterSign * _COSE_CounterSign_Init_From_Object(cn_cbor* cbor, COSE_CounterSign *, CBOR_CONTEXT_COMMA cose_errback* perr); // // Debugging Items diff --git a/test/context.c b/test/context.c index 7a89f6f3..727e253d 100644 --- a/test/context.c +++ b/test/context.c @@ -16,9 +16,11 @@ typedef struct { cn_cbor_context context; byte *pFirst; unsigned int iFailLeft; + int allocCount; } MyContext; typedef struct _MyItem { + int allocNumber; struct _MyItem *pNext; size_t size; byte pad[4]; @@ -39,11 +41,19 @@ bool CheckMemory(MyContext *pContext) assert(false); } } +<<<<<<< HEAD } else if (p->pad[0] == (byte)0xef) { for (unsigned i = 0; i < 4; i++) { if ((p->pad[i] != (byte)0xef) || (p->pad[i + 4 + p->size] != (byte)0xef)) { fprintf(stderr, "Curent block was overrun"); +======= + } + else if (p->pad[0] == (byte) 0xef) { + for (i = 0; i < 4; i++) { + if ((p->pad[i] != (byte) 0xef) || (p->pad[i + 4 + p->size] != (byte) 0xef)) { + fprintf(stderr, "Current block was overrun"); +>>>>>>> checkpoint assert(false); } } @@ -63,9 +73,18 @@ void *MyCalloc(size_t count, size_t size, void *context) CheckMemory(myContext); +<<<<<<< HEAD if (myContext->iFailLeft == 0) return NULL; myContext->iFailLeft--; +======= + if (myContext->iFailLeft != -1) { + if (myContext->iFailLeft == 0) { + return NULL; + } + myContext->iFailLeft--; + } +>>>>>>> checkpoint pb = (MyItem *)malloc(sizeof(MyItem) + count * size); @@ -74,7 +93,12 @@ void *MyCalloc(size_t count, size_t size, void *context) pb->pNext = (struct _MyItem *)myContext->pFirst; myContext->pFirst = (byte *)pb; +<<<<<<< HEAD pb->size = count * size; +======= + pb->size = count*size; + pb->allocNumber = myContext->allocCount++; +>>>>>>> checkpoint return &pb->data; } @@ -100,6 +124,7 @@ cn_cbor_context *CreateContext(unsigned int iFailPoint) p->context.context = p; p->pFirst = NULL; p->iFailLeft = iFailPoint; + p->allocCount = 0; return &p->context; } @@ -122,4 +147,30 @@ void FreeContext(cn_cbor_context *pContext) return; } +<<<<<<< HEAD #endif // USE_CBOR_CONTEXT +======= +int IsContextEmpty(cn_cbor_context * pContext) +{ + MyContext* myContext = (MyContext*)pContext; + MyItem* p; + int i = 0; + + + // Walk memory and check every block + + for (p = (MyItem*)myContext->pFirst; p != NULL; p = p->pNext) { + if (p->pad[0] == (byte)0xab) { + // Block has been freed + } + else { + // This block has not been freed + i += 1; + } + } + + return i; +} + +#endif // USE_CBOR_CONTEXT +>>>>>>> checkpoint diff --git a/test/sign.c b/test/sign.c index 22a949de..b0091630 100644 --- a/test/sign.c +++ b/test/sign.c @@ -16,6 +16,7 @@ #include "json.h" #include "test.h" #include "context.h" +#include "cose_int.h" #ifdef _MSC_VER #pragma warning(disable : 4127) @@ -106,6 +107,50 @@ int _ValidateSigned(const cn_cbor *pControl, fFail = true; } + cn_cbor* counter = cn_cbor_mapget_string(pSigners, "countersign"); + if (counter != NULL) { + counter = cn_cbor_mapget_string(counter, "signers"); + if (counter == NULL) { + fFail = true; + continue; + } + int count = counter->length; + cn_cbor* counterSigners = counter->first_child; + if (COSE_Signer_map_get_int(hSigner, COSE_Header_CounterSign, COSE_UNPROTECT_ONLY, 0) == NULL) { + goto returnError; + } + + for (int counterNo=0; counterNo < count; counterNo++) { + HCOSE_COUNTERSIGN h = COSE_Signer_get_countersignature(hSigner, counterNo, 0); + if (h == NULL) { + fFail = true; + continue; + } + + cn_cbor* pkeyCountersign = BuildKey(cn_cbor_mapget_string(pSigners, "key"), false); + if (pkeyCountersign == NULL) { + fFail = true; + continue; + } + + if (!COSE_CounterSign_SetKey(h, pkeyCountersign, 0)) { + fFail = true; + continue; + } + + if (COSE_CounterSign_validate(hSigner, h, 0)) { + // I don't think we have any forced errors yet. + } + else { + fFail = true; + } + + CN_CBOR_FREE(pkeyCountersign, context); + COSE_CounterSign_Free(h); + } + } + + COSE_Sign_Free(hSig); COSE_Signer_Free(hSigner); } @@ -188,6 +233,38 @@ int BuildSignedMessage(const cn_cbor *pControl) if (!COSE_Sign_AddSigner(hSignObj, hSigner, NULL)) goto returnError; + cn_cbor* countersigns = cn_cbor_mapget_string(pSigners, "countersign"); + if (countersigns != NULL) { + countersigns = cn_cbor_mapget_string(countersigns, "signers"); + cn_cbor* countersign = countersigns->first_child; + + for (; countersign != NULL; countersign = countersign->next) { + cn_cbor* pkeyCountersign = BuildKey(cn_cbor_mapget_string(countersign, "key"), false); + if (pkeyCountersign == NULL) { + goto returnError; + } + + HCOSE_COUNTERSIGN hCountersign = COSE_CounterSign_Init(CBOR_CONTEXT_PARAM_COMMA NULL); + if (hCountersign == NULL) { + goto returnError; + } + + if (!SetSendingAttributes((HCOSE)hCountersign, countersign, Attributes_Signer_protected)) { + goto returnError; + } + + if (!COSE_Signer_SetKey(hCountersign, pkeyCountersign, NULL)) { + goto returnError; + } + + if (!COSE_Signer_add_countersignature(hSigner, hCountersign, NULL)) { + goto returnError; + } + + COSE_CounterSign_Free(hCountersign); + } + } + COSE_Signer_Free(hSigner); } diff --git a/test/test.c b/test/test.c index 237114d1..bfbb74f9 100644 --- a/test/test.c +++ b/test/test.c @@ -450,10 +450,15 @@ bool SetAttributes(HCOSE hHandle, break; #endif assert(fRet); + + default: + f = false; + break; } + assert(f); } - return fRet; + return f; } bool SetSendingAttributes(HCOSE hMsg, const cn_cbor* pIn, int base) @@ -805,7 +810,9 @@ void RunMemoryTest(const char* szFileName) bool fValidateDone = false; bool fBuildDone = false; - for (iFail = 0; (!fValidateDone || !fBuildDone) && (iFail < 3); iFail++) { + + + for (iFail = 0; (!fValidateDone || !fBuildDone) && (iFail < 100000); iFail++) { if (cn_cbor_mapget_string(pInput, "mac") != NULL) { #if INCLUDE_MAC if (!fValidateDone) { @@ -814,6 +821,9 @@ void RunMemoryTest(const char* szFileName) ValidateMAC(pControl); if (CFails == 0) fValidateDone = true; + if (IsContextEmpty(context) != 0) { + CFails += 1; + } FreeContext(context); } @@ -823,6 +833,9 @@ void RunMemoryTest(const char* szFileName) BuildMacMessage(pControl); if (CFails == 0) fBuildDone = true; + if (IsContextEmpty(context) != 0) { + CFails += 1; + } FreeContext(context); } #else @@ -837,6 +850,9 @@ void RunMemoryTest(const char* szFileName) ValidateMac0(pControl); if (CFails == 0) fValidateDone = true; + if (IsContextEmpty(context) != 0) { + CFails += 1; + } FreeContext(context); } @@ -846,6 +862,9 @@ void RunMemoryTest(const char* szFileName) BuildMac0Message(pControl); if (CFails == 0) fBuildDone = true; + if (IsContextEmpty(context) != 0) { + CFails += 1; + } FreeContext(context); } #else @@ -860,6 +879,9 @@ void RunMemoryTest(const char* szFileName) ValidateEncrypt(pControl); if (CFails == 0) fValidateDone = true; + if (IsContextEmpty(context) != 0) { + CFails += 1; + } FreeContext(context); } @@ -869,6 +891,9 @@ void RunMemoryTest(const char* szFileName) BuildEncryptMessage(pControl); if (CFails == 0) fBuildDone = true; + if (IsContextEmpty(context) != 0) { + CFails += 1; + } FreeContext(context); } #else @@ -883,6 +908,9 @@ void RunMemoryTest(const char* szFileName) ValidateEnveloped(pControl); if (CFails == 0) fValidateDone = true; + if (IsContextEmpty(context) != 0) { + CFails += 1; + } FreeContext(context); } @@ -892,6 +920,9 @@ void RunMemoryTest(const char* szFileName) BuildEnvelopedMessage(pControl); if (CFails == 0) fBuildDone = true; + if (IsContextEmpty(context) != 0) { + CFails += 1; + } FreeContext(context); } #else @@ -906,6 +937,9 @@ void RunMemoryTest(const char* szFileName) ValidateSigned(pControl); if (CFails == 0) fValidateDone = true; + if (IsContextEmpty(context) != 0) { + CFails += 1; + } FreeContext(context); } @@ -915,6 +949,9 @@ void RunMemoryTest(const char* szFileName) BuildSignedMessage(pControl); if (CFails == 0) fBuildDone = true; + if (IsContextEmpty(context) != 0) { + CFails += 1; + } FreeContext(context); } #else @@ -929,6 +966,9 @@ void RunMemoryTest(const char* szFileName) ValidateSign1(pControl); if (CFails == 0) fValidateDone = true; + if (IsContextEmpty(context) != 0) { + CFails += 1; + } FreeContext(context); } @@ -938,6 +978,9 @@ void RunMemoryTest(const char* szFileName) BuildSign1Message(pControl); if (CFails == 0) fBuildDone = true; + if (IsContextEmpty(context) != 0) { + CFails += 1; + } FreeContext(context); } #else @@ -997,9 +1040,21 @@ void RunFileTest(const char* szFileName) #endif } else if (cn_cbor_mapget_string(pInput, "sign") != NULL) { #if INCLUDE_SIGN + context = CreateContext(-1); if (ValidateSigned(pControl)) { + if (IsContextEmpty(context) != 0) { + printf("Memory Cleanup Failure - Validate"); + // CFails += 1; + } + FreeContext(context); + context = CreateContext(-1); BuildSignedMessage(pControl); + if (IsContextEmpty(context) != 0) { + printf("Memory Cleanup Failure - Build"); + // CFails += 1; + } } + FreeContext(context); #endif } else if (cn_cbor_mapget_string(pInput, "sign0") != NULL) { #if INCLUDE_SIGN1 diff --git a/test/test.h b/test/test.h index 1e0a2926..a1e7db94 100644 --- a/test/test.h +++ b/test/test.h @@ -46,6 +46,7 @@ void MAC0_Corners(); // context.c extern cn_cbor_context* CreateContext(unsigned int iFailPoint); void FreeContext(cn_cbor_context* pContext); +int IsContextEmpty(cn_cbor_context* pContext); #endif // test.c From 957729ec46628d29ac0121874c3bb82f021a10d0 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Sat, 25 Apr 2020 11:13:10 -0700 Subject: [PATCH 06/38] checkpoint --- .clang-format | 2 +- dumper/dumper.c | 4 +- include/cose/cose_configure.h | 4 +- src/Cose.c | 21 +++++-- src/CounterSign.c | 57 +++++++++++++++++- src/Encrypt.c | 2 +- src/Recipient.c | 7 +++ src/Sign.c | 7 +-- src/SignerInfo.c | 65 ++++++++++++++++----- src/cose_int.h | 20 +++---- test/sign.c | 10 ++-- test/test.c | 106 +++++++++++++++++++++++----------- test/test.h | 1 + 13 files changed, 223 insertions(+), 83 deletions(-) diff --git a/.clang-format b/.clang-format index 955f6adf..4ec66cab 100644 --- a/.clang-format +++ b/.clang-format @@ -8,6 +8,6 @@ BreakBeforeBraces: Custom BraceWrapping: AfterFunction: true BeforeCatch: false - BeforeElse: false + BeforeElse: true AlignAfterOpenBracket: false SortIncludes: false \ No newline at end of file diff --git a/dumper/dumper.c b/dumper/dumper.c index ab45b2f5..4e800792 100644 --- a/dumper/dumper.c +++ b/dumper/dumper.c @@ -31,9 +31,7 @@ FOO Recipients; extern FOO Signer[]; #ifdef USE_CBOR_CONTEXT -#define CBOR_CONTEXT_PARAM , NULL -#else -#define CBOR_CONTEXT_PARAM +#define context NULL #endif FOO AlgorithmMap[38] = { diff --git a/include/cose/cose_configure.h b/include/cose/cose_configure.h index 5569b479..8515346a 100644 --- a/include/cose/cose_configure.h +++ b/include/cose/cose_configure.h @@ -147,7 +147,6 @@ #endif #endif // !defined (USE_MBED_TLS) -#define USE_COUNTER_SIGNATURES // #define USE_COUNTER_SIGNATURE1 // @@ -172,3 +171,6 @@ #ifndef INCLUDE_SIGN1 #define INCLUDE_SIGN1 1 #endif +#ifndef INCLUDE_COUNTERSIGNATURE +#define INCLUDE_COUNTERSIGNATURE 1 +#endif diff --git a/src/Cose.c b/src/Cose.c index 31a4cc18..658a355d 100644 --- a/src/Cose.c +++ b/src/Cose.c @@ -124,7 +124,7 @@ bool _COSE_Init_From_Object(COSE *pobj, cn_cbor_map_create(CBOR_CONTEXT_PARAM_COMMA & cbor_error); CHECK_CONDITION_CBOR(pobj->m_dontSendMap != NULL, cbor_error); -#ifdef USE_COUNTER_SIGNATURES +#ifdef INCLUDE_COUNTERSIGNATURE cn_cbor* pCounter = cn_cbor_mapget_int(pobj->m_unprotectMap, COSE_Header_CounterSign); if (pCounter != NULL) { int i; @@ -133,10 +133,9 @@ bool _COSE_Init_From_Object(COSE *pobj, if (pCounter->first_child->type == CN_CBOR_ARRAY) { cn_cbor* pSig = pCounter->first_child; - for (i = 0; i < pCounter->length; i++, pCounter = pCounter->next) { - cn_cbor* p = cn_cbor_decode(pSig->v.bytes, pSig->length, CBOR_CONTEXT_PARAM_COMMA & errState); - COSE_CounterSign* cs = _COSE_CounterSign_Init_From_Object(p, NULL, CBOR_CONTEXT_PARAM_COMMA perr); - cs = pobj->m_counterSigners; + for (i = 0; i < pCounter->length; i++, pSig = pSig->next) { + COSE_CounterSign* cs = _COSE_CounterSign_Init_From_Object(pSig, NULL, CBOR_CONTEXT_PARAM_COMMA perr); + cs->m_next = pobj->m_counterSigners; pobj->m_counterSigners = cs; } } @@ -557,3 +556,15 @@ void _COSE_RemoveFromList(COSE **root, COSE *thisMsg) } return; } + +#ifdef DEBUG +extern COSE *CountersignRoot; +extern COSE *SignerRoot; +extern COSE *SignRoot; + +bool AreListsEmpty() +{ + return CountersignRoot == NULL && SignerRoot == NULL && SignRoot == NULL; +} + +#endif diff --git a/src/CounterSign.c b/src/CounterSign.c index a45f91f8..f9368c5c 100644 --- a/src/CounterSign.c +++ b/src/CounterSign.c @@ -8,7 +8,7 @@ #include "cose/cose_configure.h" #include "crypto.h" -#ifdef USE_COUNTER_SIGNATURES +#ifdef INCLUDE_COUNTERSIGNATURE COSE* CountersignRoot = NULL; @@ -51,7 +51,9 @@ COSE_CounterSign * _COSE_CounterSign_Init_From_Object(cn_cbor* cbor, COSE_Counte goto errorReturn; } - _COSE_InsertInList(&CountersignRoot, &pobj->m_signer.m_message); + if (pIn == NULL) { + _COSE_InsertInList(&CountersignRoot, &pobj->m_signer.m_message); + } return pobj; @@ -117,8 +119,9 @@ bool _COSE_CounterSign_add(COSE* pMessage, HCOSE_COUNTERSIGN hSigner, cose_errba CHECK_CONDITION(IsValidCounterSignHandle(hSigner), COSE_ERR_INVALID_HANDLE); CHECK_CONDITION(pSigner->m_signer.m_message.m_counterSigners == NULL, COSE_ERR_INVALID_PARAMETER); - pSigner->m_signer.m_message.m_counterSigners = pMessage->m_counterSigners; + pSigner->m_next = pMessage->m_counterSigners; pMessage->m_counterSigners = pSigner; + pSigner->m_signer.m_message.m_refCount += 1; return true; errorReturn: @@ -292,6 +295,54 @@ bool COSE_CounterSign_validate(HCOSE_SIGNER hSigner, HCOSE_COUNTERSIGN hCounters return false; } +bool COSE_CounterSign_map_put_int(HCOSE_COUNTERSIGN h, + int key, + cn_cbor* value, + int flags, + cose_errback* perr) +{ + CHECK_CONDITION(IsValidCounterSignHandle(h), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION(value != NULL, COSE_ERR_INVALID_PARAMETER); + + return _COSE_map_put( + &((COSE_CounterSign*)h)->m_signer.m_message, key, value, flags, perr); + +errorReturn: + return false; +} + + +/*! + * @brief Set the application external data for authentication + * + * Signer data objects support the authentication of external application + * supplied data. This function is provided to supply that data to the library. + * + * The external data is not copied, nor will be it freed when the handle is + * released. + * + * @param hcose Handle for the COSE MAC data object + * @param pbEternalData point to the external data + * @param cbExternalData size of the external data + * @param perr location to return errors + * @return result of the operation. + */ + +bool COSE_CounterSign_SetExternal(HCOSE_COUNTERSIGN hcose, + const byte* pbExternalData, + size_t cbExternalData, + cose_errback* perr) +{ + if (!IsValidCounterSignHandle(hcose)) { + if (perr != NULL) + perr->err = COSE_ERR_INVALID_HANDLE; + return false; + } + + return _COSE_SetExternal(&((COSE_CounterSign*)hcose)->m_signer.m_message, + pbExternalData, cbExternalData, perr); +} + #endif diff --git a/src/Encrypt.c b/src/Encrypt.c index 6d290176..5192e663 100644 --- a/src/Encrypt.c +++ b/src/Encrypt.c @@ -654,7 +654,7 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, if (cbProtected == NULL) goto errorReturn; -#ifdef USE_COUNTER_SIGNATURES +#ifdef INCLUDE_COUNTERSIGNATURE /* // Setup Counter Signatures if (!_COSE_CountSign_create( diff --git a/src/Recipient.c b/src/Recipient.c index b597a575..724e7112 100644 --- a/src/Recipient.c +++ b/src/Recipient.c @@ -68,6 +68,12 @@ bool COSE_Recipient_Free(HCOSE_RECIPIENT hRecipient) { if (IsValidRecipientHandle(hRecipient)) { COSE_RecipientInfo *p = (COSE_RecipientInfo *)hRecipient; + + if (p->m_encrypt.m_message.m_refCount > 1) { + p->m_encrypt.m_message.m_refCount--; + return true; + } + _COSE_RemoveFromList(&RecipientRoot, &p->m_encrypt.m_message); _COSE_Recipient_Free(p); @@ -134,6 +140,7 @@ void _COSE_Recipient_Free(COSE_RecipientInfo *pRecipient) return; } + _COSE_Encrypt_Release(&pRecipient->m_encrypt); COSE_FREE(pRecipient, &pRecipient->m_encrypt.m_message.m_allocContext); return; diff --git a/src/Sign.c b/src/Sign.c index 766f53fb..f97dd595 100644 --- a/src/Sign.c +++ b/src/Sign.c @@ -11,7 +11,7 @@ #if INCLUDE_SIGN -static COSE *SignRoot = NULL; +COSE *SignRoot = NULL; /*! \private * @brief Test if a HCOSE_SIGN handle is valid @@ -156,10 +156,7 @@ void _COSE_Sign_Release(COSE_SignMessage *p) for (pSigner = p->m_signerFirst; pSigner != NULL; pSigner = pSigner2) { pSigner2 = pSigner->m_signerNext; - _COSE_SignerInfo_Release(pSigner); - if (pSigner->m_message.m_refCount == 0) { - COSE_FREE(pSigner, &pSigner->m_message.m_allocContext); - } + COSE_Signer_Free((HCOSE_SIGNER) pSigner); } _COSE_Release(&p->m_message); diff --git a/src/SignerInfo.c b/src/SignerInfo.c index dafcea1a..78166446 100644 --- a/src/SignerInfo.c +++ b/src/SignerInfo.c @@ -15,7 +15,7 @@ #if INCLUDE_SIGN -static COSE *SignerRoot = NULL; +COSE *SignerRoot = NULL; bool IsValidSignerHandle(HCOSE_SIGNER h) { @@ -131,6 +131,7 @@ static bool BuildToBeSigned(byte **ppbToSign, const byte *pbExternal, const char const *contextString, size_t cbExternal, + const char * const contextString, CBOR_CONTEXT_COMMA cose_errback *perr) { cn_cbor *pArray = NULL; @@ -215,7 +216,7 @@ static bool BuildToBeSigned(byte **ppbToSign, bool _COSE_Signer_sign(COSE_SignerInfo *pSigner, const cn_cbor *pcborBody, const cn_cbor *pcborProtected, - const char const * contextString, + const char * const contextString, cose_errback *perr) { #ifdef USE_CBOR_CONTEXT @@ -253,7 +254,8 @@ bool _COSE_Signer_sign(COSE_SignerInfo *pSigner, if (!BuildToBeSigned(&pbToSign, &cbToSign, pcborBody, pcborProtected, pcborProtectedSign, pSigner->m_message.m_pbExternal, - pSigner->m_message.m_cbExternal, contextString, CBOR_CONTEXT_PARAM_COMMA perr)) + pSigner->m_message.m_cbExternal, + contextString, CBOR_CONTEXT_PARAM_COMMA perr)) goto errorReturn; switch (alg) { @@ -294,17 +296,51 @@ bool _COSE_Signer_sign(COSE_SignerInfo *pSigner, } if (pSigner->m_message.m_counterSigners != NULL) { - cn_cbor* pSignature = _COSE_arrayget_int(&pSigner->m_message, INDEX_SIGNATURE); - - - COSE_CounterSign* pCountersign = pSigner->m_message.m_counterSigners; - for (; pCountersign != NULL; pCountersign = pCountersign->m_signer.m_message.m_counterSigners) { - pcborProtectedSign = _COSE_encode_protected(&pSigner->m_message, perr); - if (pcborProtectedSign == NULL) goto errorReturn; - if (!_COSE_Signer_sign(&pCountersign->m_signer, pSignature, pcborProtectedSign, "CounterSignature", perr)) { + cn_cbor *pSignature = + _COSE_arrayget_int(&pSigner->m_message, INDEX_SIGNATURE); + int count = 0; + + COSE_CounterSign *pCountersign = pSigner->m_message.m_counterSigners; + for (; pCountersign != NULL; + pCountersign = pCountersign->m_next, + count += 1) { + pcborProtectedSign = + _COSE_encode_protected(&pSigner->m_message, perr); + if (pcborProtectedSign == NULL) { + goto errorReturn; + } + if (!_COSE_Signer_sign(&pCountersign->m_signer, pSignature, + pcborProtectedSign, "CounterSignature", perr)) { goto errorReturn; } } + + if (count == 1) { + cn_cbor * cn = COSE_get_cbor((HCOSE)pSigner->m_message.m_counterSigners); + CHECK_CONDITION( + COSE_Signer_map_put_int((HCOSE_SIGNER)pSigner, + COSE_Header_CounterSign, cn, COSE_UNPROTECT_ONLY, perr), + COSE_ERR_OUT_OF_MEMORY); + } + else { + cn_cbor_errback cn_error; + cn_cbor *cn_counterSign = + cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA & cn_error); + CHECK_CONDITION_CBOR(cn_counterSign, cn_error); + + for (pCountersign = pSigner->m_message.m_counterSigners; + pCountersign != NULL; + pCountersign = pCountersign->m_next) { + cn_cbor *cn = COSE_get_cbor((HCOSE) pCountersign); + CHECK_CONDITION_CBOR( + cn_cbor_array_append(cn_counterSign, cn, &cn_error), + cn_error); + } + CHECK_CONDITION( + COSE_Signer_map_put_int((HCOSE_SIGNER)pSigner, + COSE_Header_CounterSign, cn_counterSign, COSE_UNPROTECT_ONLY, perr), + COSE_ERR_OUT_OF_MEMORY); + } } fRet = true; @@ -364,17 +400,16 @@ bool COSE_Signer_SetExternal(HCOSE_SIGNER hcose, pbExternalData, cbExternalData, perr); } -bool _COSE_Signer_validate(COSE_SignMessage *pSign, - COSE_SignerInfo *pSigner, +bool _COSE_Signer_validate(COSE_SignerInfo *pSigner, const cn_cbor *pcborBody, const cn_cbor *pcborProtected, - const char const * contextString, + const char * const contextString, cose_errback *perr) { byte *pbToBeSigned = NULL; int alg = 0; #ifdef USE_CBOR_CONTEXT - cn_cbor_context *context = &pSign->m_message.m_allocContext; + cn_cbor_context *context = &pSigner->m_message.m_allocContext; #else UNUSED(pSign); #endif diff --git a/src/cose_int.h b/src/cose_int.h index d26fe0ee..6e965644 100644 --- a/src/cose_int.h +++ b/src/cose_int.h @@ -8,7 +8,7 @@ // These definitions are here because they aren't required for the public // interface, and they were quite confusing in cn-cbor.h -#ifdef USE_COUNTER_SIGNATURES +#ifdef INCLUDE_COUNTERSIGNATURE struct _COSE_COUNTER_SIGN; typedef struct _COSE_COUNTER_SIGN COSE_CounterSign; #endif @@ -40,10 +40,10 @@ typedef struct _COSE { cn_cbor_context m_allocContext; #endif struct _COSE *m_handleList; -#ifdef USE_COUNTER_SIGNATURES - COSE_CounterSign *m_counterSigners; // Linked list of all counter signatures +#ifdef INCLUDE_COUNTERSIGNATURE + COSE_CounterSign *m_counterSigners; // Linked list of all counter signatures #endif -#ifdef USE_COUNTER_SIGNATURE1 +#ifdef INCLUDE_COUNTERSIGNATURE1 COSE_CounterSign1* m_counterSign1; #endif } COSE; @@ -105,7 +105,7 @@ typedef struct { #endif typedef COSE_MacMessage COSE_Mac0Message; -#ifdef USE_COUNTER_SIGNATURES +#ifdef INCLUDE_COUNTERSIGNATURE typedef struct _COSE_COUNTER_SIGN { COSE_SignerInfo m_signer; COSE_CounterSign *m_next; @@ -264,7 +264,6 @@ void _COSE_Sign_Release(COSE_SignMessage *p); // Signer items -<<<<<<< HEAD bool _COSE_SignerInfo_Init(COSE_INIT_FLAGS flags, COSE_SignerInfo *pcose, int msgType, @@ -278,11 +277,10 @@ COSE_SignerInfo *_COSE_SignerInfo_Init_From_Object(cn_cbor *cbor, COSE_SignerInfo *pIn, CBOR_CONTEXT_COMMA cose_errback *perr); bool _COSE_SignerInfo_Release(COSE_SignerInfo *pSigner); -bool _COSE_Signer_validate(COSE_SignMessage *pSign, - COSE_SignerInfo *pSigner, +bool _COSE_Signer_validate(COSE_SignerInfo *pSigner, const cn_cbor *pbContent, const cn_cbor *pbProtected, - const char * const contextString, + const char * const szContext, cose_errback *perr); // Sign1 items @@ -329,7 +327,9 @@ bool _COSE_CounterSign_add(COSE *pMessage, bool _COSE_CountSign_create(COSE *pMessage, cn_cbor *pcnBody, CBOR_CONTEXT_COMMA cose_errback *perr); -COSE_CounterSign * _COSE_CounterSign_Init_From_Object(cn_cbor* cbor, COSE_CounterSign *, CBOR_CONTEXT_COMMA cose_errback* perr); +COSE_CounterSign * _COSE_CounterSign_Init_From_Object(cn_cbor* cbor, + COSE_CounterSign *, + CBOR_CONTEXT_COMMA cose_errback* perr); // // Debugging Items diff --git a/test/sign.c b/test/sign.c index b0091630..c17ebd13 100644 --- a/test/sign.c +++ b/test/sign.c @@ -120,14 +120,16 @@ int _ValidateSigned(const cn_cbor *pControl, goto returnError; } - for (int counterNo=0; counterNo < count; counterNo++) { + for (int counterNo = 0; counterNo < count && counterSigners != NULL; + counterNo++, counterSigners = counterSigners->next) { HCOSE_COUNTERSIGN h = COSE_Signer_get_countersignature(hSigner, counterNo, 0); if (h == NULL) { fFail = true; continue; } - cn_cbor* pkeyCountersign = BuildKey(cn_cbor_mapget_string(pSigners, "key"), false); + cn_cbor *pkeyCountersign = BuildKey( + cn_cbor_mapget_string(counterSigners, "key"), false); if (pkeyCountersign == NULL) { fFail = true; continue; @@ -249,11 +251,11 @@ int BuildSignedMessage(const cn_cbor *pControl) goto returnError; } - if (!SetSendingAttributes((HCOSE)hCountersign, countersign, Attributes_Signer_protected)) { + if (!SetSendingAttributes((HCOSE)hCountersign, countersign, Attributes_Countersign_protected)) { goto returnError; } - if (!COSE_Signer_SetKey(hCountersign, pkeyCountersign, NULL)) { + if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) { goto returnError; } diff --git a/test/test.c b/test/test.c index bfbb74f9..8e36111f 100644 --- a/test/test.c +++ b/test/test.c @@ -449,13 +449,19 @@ bool SetAttributes(HCOSE hHandle, (HCOSE_SIGN1)hHandle, keyNew, pValueNew, which, NULL); break; #endif - assert(fRet); - default: - f = false; - break; +#if INCLUDE_COUNTERSIGNATURE + case Attributes_Countersign_protected: + fRet &= COSE_CounterSign_map_put_int( + (HCOSE_COUNTERSIGN)hHandle, keyNew, pValueNew, which, NULL); + break; +#endif + + default: + assert(false); + break; } - assert(f); + assert(fRet); } return f; @@ -534,6 +540,18 @@ bool SetSendingAttributes(HCOSE hMsg, const cn_cbor* pIn, int base) goto returnError; break; #endif +#if INCLUDE_COUNTERSIGNATURE + case Attributes_Countersign_protected: + if (!COSE_CounterSign_SetExternal((HCOSE_COUNTERSIGN)hMsg, + FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, + NULL)) { + goto returnError; + } + break; +#endif + default: + assert(false); + break; } } @@ -608,6 +626,15 @@ bool SetReceivingAttributes(HCOSE hMsg, const cn_cbor* pIn, int base) NULL)) goto returnError; break; +#endif +#if INCLUDE_COUNTERSIGNATURE + case Attributes_Countersign_protected: + if (!COSE_CounterSign_SetExternal((HCOSE_COUNTERSIGN)hMsg, + FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, + NULL)) { + goto returnError; + } + break; #endif } } @@ -759,6 +786,8 @@ bool Test_cn_cbor_array_replace() return true; } +bool AreListsEmpty(); + void RunCorners() { Test_cn_cbor_array_replace(); @@ -996,6 +1025,37 @@ void RunMemoryTest(const char* szFileName) #endif } +typedef bool (__cdecl *ValidatePtr)(const cn_cbor* pControl); + +bool ProcessFile(const cn_cbor* pControl, ValidatePtr validateFunction, ValidatePtr buildFunction) +{ + context = CreateContext(-1); + if (validateFunction(pControl)) { + if (IsContextEmpty(context) != 0) { + printf("Memory Cleanup Failure - Validate\n"); + // CFails += 1; + } + if (!AreListsEmpty()) { + printf("Left over handle - P1\n"); + CFails += 1; + } + FreeContext(context); + context = CreateContext(-1); + buildFunction(pControl); + if (IsContextEmpty(context) != 0) { + printf("Memory Cleanup Failure - Build\n"); + // CFails += 1; + } + } + if (!AreListsEmpty()) { + printf("Left over handle - P2\n"); + CFails += 1; + } + FreeContext(context); + context = NULL; + return true; +} + void RunFileTest(const char* szFileName) { const cn_cbor* pControl = NULL; @@ -1022,51 +1082,27 @@ void RunFileTest(const char* szFileName) if (cn_cbor_mapget_string(pInput, "mac") != NULL) { #if INCLUDE_MAC - if (ValidateMAC(pControl)) { - BuildMacMessage(pControl); - } + ProcessFile(pControl, ValidateMAC, BuildMacMessage); #endif } else if (cn_cbor_mapget_string(pInput, "mac0") != NULL) { #if INCLUDE_MAC0 - if (ValidateMac0(pControl)) { - BuildMac0Message(pControl); - } + ProcessFile(pControl, ValidateMac0, BuildMac0Message); #endif } else if (cn_cbor_mapget_string(pInput, "enveloped") != NULL) { #if INCLUDE_ENCRYPT - if (ValidateEnveloped(pControl)) { - BuildEnvelopedMessage(pControl); - } + ProcessFile(pControl, ValidateEnveloped, BuildEnvelopedMessage); #endif } else if (cn_cbor_mapget_string(pInput, "sign") != NULL) { #if INCLUDE_SIGN - context = CreateContext(-1); - if (ValidateSigned(pControl)) { - if (IsContextEmpty(context) != 0) { - printf("Memory Cleanup Failure - Validate"); - // CFails += 1; - } - FreeContext(context); - context = CreateContext(-1); - BuildSignedMessage(pControl); - if (IsContextEmpty(context) != 0) { - printf("Memory Cleanup Failure - Build"); - // CFails += 1; - } - } - FreeContext(context); + ProcessFile(pControl, ValidateSigned, BuildSignedMessage); #endif } else if (cn_cbor_mapget_string(pInput, "sign0") != NULL) { #if INCLUDE_SIGN1 - if (ValidateSign1(pControl)) { - BuildSign1Message(pControl); - } + ProcessFile(pControl, ValidateSign1, BuildSign1Message); #endif } else if (cn_cbor_mapget_string(pInput, "encrypted") != NULL) { #if INCLUDE_ENCRYPT0 - if (ValidateEncrypt(pControl)) { - BuildEncryptMessage(pControl); - } + ProcessFile(pControl, ValidateEncrypt, BuildEncryptMessage); #endif } } diff --git a/test/test.h b/test/test.h index a1e7db94..8a2a1935 100644 --- a/test/test.h +++ b/test/test.h @@ -59,6 +59,7 @@ enum { Attributes_Sign_protected, Attributes_Signer_protected, Attributes_Sign1_protected, + Attributes_Countersign_protected } whichSet; extern int CFails; From 31c8d1b57230f85af200b4504e9ba2641edec1d5 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Sat, 25 Apr 2020 14:44:24 -0700 Subject: [PATCH 07/38] Fix workflow and merge errors --- include/cose/cose.h | 4 ++-- src/CMakeLists.txt | 3 +++ src/Cose.c | 2 +- src/SignerInfo.c | 3 +-- test/context.c | 22 ---------------------- test/test.c | 2 +- 6 files changed, 8 insertions(+), 28 deletions(-) diff --git a/include/cose/cose.h b/include/cose/cose.h index 753ff86b..a90ff922 100644 --- a/include/cose/cose.h +++ b/include/cose/cose.h @@ -79,12 +79,12 @@ typedef enum { HCOSE COSE_Decode(const byte* rgbData, size_t cbData, - int* type, + int* ptype, COSE_object_type struct_type, CBOR_CONTEXT_COMMA cose_errback* perr); // Decode the object size_t COSE_Encode(HCOSE msg, byte* rgb, size_t ib, size_t cb); -cn_cbor* COSE_get_cbor(HCOSE hmsg); +cn_cbor* COSE_get_cbor(HCOSE h); // Functions for the signing object diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e1a379f4..5957f32a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,6 +13,9 @@ elseif(MSVC) if(COSE_C_FATAL_WARNINGS) add_definitions(/WX) endif() + if(COSE_C_OPTIMIZE) + add_definitions(-DNDEBUG) + endif() else() message(FATAL_ERROR "unhandled compiler id: ${CMAKE_C_COMPILER_ID}") endif() diff --git a/src/Cose.c b/src/Cose.c index 658a355d..4c247337 100644 --- a/src/Cose.c +++ b/src/Cose.c @@ -557,7 +557,7 @@ void _COSE_RemoveFromList(COSE **root, COSE *thisMsg) return; } -#ifdef DEBUG +#ifndef NDEBUG extern COSE *CountersignRoot; extern COSE *SignerRoot; extern COSE *SignRoot; diff --git a/src/SignerInfo.c b/src/SignerInfo.c index 78166446..abadbe97 100644 --- a/src/SignerInfo.c +++ b/src/SignerInfo.c @@ -23,7 +23,7 @@ bool IsValidSignerHandle(HCOSE_SIGNER h) return _COSE_IsInList(SignerRoot, (COSE *)p); } -bool _COSE_SignerInfo_Free(COSE_SignerInfo *pSigner) +bool _COSE_SignerInfo_Release(COSE_SignerInfo *pSigner) { // Check ref counting if (pSigner->m_message.m_refCount > 1) { @@ -129,7 +129,6 @@ static bool BuildToBeSigned(byte **ppbToSign, const cn_cbor *pcborProtected, const cn_cbor *pcborProtectedSign, const byte *pbExternal, - const char const *contextString, size_t cbExternal, const char * const contextString, CBOR_CONTEXT_COMMA cose_errback *perr) diff --git a/test/context.c b/test/context.c index 727e253d..99f36bdc 100644 --- a/test/context.c +++ b/test/context.c @@ -41,19 +41,11 @@ bool CheckMemory(MyContext *pContext) assert(false); } } -<<<<<<< HEAD } else if (p->pad[0] == (byte)0xef) { for (unsigned i = 0; i < 4; i++) { if ((p->pad[i] != (byte)0xef) || (p->pad[i + 4 + p->size] != (byte)0xef)) { - fprintf(stderr, "Curent block was overrun"); -======= - } - else if (p->pad[0] == (byte) 0xef) { - for (i = 0; i < 4; i++) { - if ((p->pad[i] != (byte) 0xef) || (p->pad[i + 4 + p->size] != (byte) 0xef)) { fprintf(stderr, "Current block was overrun"); ->>>>>>> checkpoint assert(false); } } @@ -73,18 +65,12 @@ void *MyCalloc(size_t count, size_t size, void *context) CheckMemory(myContext); -<<<<<<< HEAD - if (myContext->iFailLeft == 0) - return NULL; - myContext->iFailLeft--; -======= if (myContext->iFailLeft != -1) { if (myContext->iFailLeft == 0) { return NULL; } myContext->iFailLeft--; } ->>>>>>> checkpoint pb = (MyItem *)malloc(sizeof(MyItem) + count * size); @@ -93,12 +79,8 @@ void *MyCalloc(size_t count, size_t size, void *context) pb->pNext = (struct _MyItem *)myContext->pFirst; myContext->pFirst = (byte *)pb; -<<<<<<< HEAD pb->size = count * size; -======= - pb->size = count*size; pb->allocNumber = myContext->allocCount++; ->>>>>>> checkpoint return &pb->data; } @@ -147,9 +129,6 @@ void FreeContext(cn_cbor_context *pContext) return; } -<<<<<<< HEAD -#endif // USE_CBOR_CONTEXT -======= int IsContextEmpty(cn_cbor_context * pContext) { MyContext* myContext = (MyContext*)pContext; @@ -173,4 +152,3 @@ int IsContextEmpty(cn_cbor_context * pContext) } #endif // USE_CBOR_CONTEXT ->>>>>>> checkpoint diff --git a/test/test.c b/test/test.c index 8e36111f..acf2f598 100644 --- a/test/test.c +++ b/test/test.c @@ -464,7 +464,7 @@ bool SetAttributes(HCOSE hHandle, assert(fRet); } - return f; + return fRet; } bool SetSendingAttributes(HCOSE hMsg, const cn_cbor* pIn, int base) From 3a362cc889f4e25e3e4946dcdc96e2df01a3290a Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Sun, 26 Apr 2020 10:38:15 -0700 Subject: [PATCH 08/38] Add countersignatures on the Sign message object Do some movement of code to extract what is going to be really common. Fix some of the errors from ctidy. --- .github/workflows/main.yml | 1 + .travis.yml | 6 +- CMakeLists.txt | 2 + include/cose/cose.h | 24 +++- src/CounterSign.c | 246 +++++++++++++++++++++++++++++-------- src/Sign.c | 15 ++- src/SignerInfo.c | 50 +------- src/cose_int.h | 16 ++- test/CMakeLists.txt | 6 + test/encrypt.c | 2 +- test/sign.c | 146 ++++++++++++++++++++-- test/test.c | 8 +- 12 files changed, 391 insertions(+), 131 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6ea987b2..ceaa9df2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,6 +20,7 @@ jobs: "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=ON -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF", "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=ON -DCOSE_C_INCLUDE_SIGN1=OFF", "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=ON", + "-DCOSE_C_INCLUDE_COUNTERSIGN=ON", ] steps: diff --git a/.travis.yml b/.travis.yml index ae3e6361..588ee63f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,11 +9,11 @@ env: OPENSSL_INSTALL_DIR="${HOME}/opt" jobs: - USE_CONTEXT=ON - COVERALLS="-DCOSE_C_COVERALLS_SEND=ON" + COVERALLS="-DCOSE_C_COVERALLS_SEND=ON -DCOSE_C_INCLUDE_COUNTERSIGN=ON" - USE_CONTEXT=OFF - COVERALLS="-DCOSE_C_COVERALLS_SEND=ON" + COVERALLS="-DCOSE_C_COVERALLS_SEND=ON -DCOSE_C_INCLUDE_COUNTERSIGN=ON" - USE_CONTEXT=OFF USE_EMBEDTLS=ON - COVERALLS="-DCOSE_C_COVERALLS_SEND=ON" + COVERALLS="-DCOSE_C_COVERALLS_SEND=ON -DCOSE_C_INCLUDE_COUNTERSIGN=ON" - CMAKE_OPTIONS="-DCOSE_C_INCLUDE_ENCRYPT=ON -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF" - CMAKE_OPTIONS="-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=ON -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF" - CMAKE_OPTIONS="-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=ON -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF" diff --git a/CMakeLists.txt b/CMakeLists.txt index 215201f5..d21aa4d9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,7 @@ option(COSE_C_INCLUDE_MAC "Include COSE_MAC" ON) option(COSE_C_INCLUDE_MAC0 "Include COSE_MAC0" ON) option(COSE_C_INCLUDE_SIGN "Include COSE_SIGN" ON) option(COSE_C_INCLUDE_SIGN1 "Include COSE_SIGN1" ON) +option(COSE_C_INCLUDE_COUNTERSIGN "Include COSE_COUNTERSIGN" OFF) # Set the output of the libraries and executables. set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) @@ -243,6 +244,7 @@ message(STATUS "COSE_C_INCLUDE_MAC:..............${COSE_C_INCLUDE_MAC}") message(STATUS "COSE_C_INCLUDE_MAC0:.............${COSE_C_INCLUDE_MAC0}") message(STATUS "COSE_C_INCLUDE_SIGN:.............${COSE_C_INCLUDE_SIGN}") message(STATUS "COSE_C_INCLUDE_SIGN1:............${COSE_C_INCLUDE_SIGN1}") +message(STATUS "COSE_C_INCLUDE_COUNTERSIGN:......${COSE_C_INCLUDE_COUNTERSIGN}") message(STATUS "COSE_C_USE_CONTEXT:..............${COSE_C_USE_CONTEXT}") message(STATUS "COSE_C_FATAL_WARNINGS:...........${COSE_C_FATAL_WARNINGS}") message(STATUS "COSE_C_BUILD_TESTS:..............${COSE_C_BUILD_TESTS}") diff --git a/include/cose/cose.h b/include/cose/cose.h index a90ff922..7b933be9 100644 --- a/include/cose/cose.h +++ b/include/cose/cose.h @@ -555,13 +555,29 @@ bool COSE_CounterSign_map_put_int(HCOSE_COUNTERSIGN cose, cose_errback* errp); bool COSE_CounterSign_SetExternal(HCOSE_COUNTERSIGN cose, const byte* pbExternalData, size_t cbExternalData, cose_errback* perr); +bool COSE_CounterSign_SetKey(HCOSE_COUNTERSIGN, + const cn_cbor* pkey, + cose_errback* perr); -// HCOSE_COUNTERSIGN COSE_Signer_add_countersignature(HCOSE_SIGNER hSigner, const cn_cbor* pkey, cose_errback* perr); + HCOSE_COUNTERSIGN COSE_Signer_add_countersignature(HCOSE_SIGNER hSigner, HCOSE_COUNTERSIGN hCountersignature, cose_errback* perr); -bool COSE_CounterSign_validate(HCOSE_SIGNER hSigner, HCOSE_COUNTERSIGN hCountersignature, cose_errback* perr); -bool COSE_CounterSign_SetKey(HCOSE_COUNTERSIGN, const cn_cbor* pkey, cose_errback* perr); +HCOSE_COUNTERSIGN COSE_Signer_get_countersignature(HCOSE_SIGNER hSigner, + int index, + cose_errback* perr); +bool COSE_Signer_CounterSign_validate(HCOSE_SIGNER hSigner, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr); +HCOSE_COUNTERSIGN COSE_Sign_add_countersignature(HCOSE_SIGN hSignMsg, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr); +HCOSE_COUNTERSIGN COSE_Sign_get_countersignature(HCOSE_SIGN hSignMsg, + int index, + cose_errback* perr); +bool COSE_Sign_CounterSign_validate(HCOSE_SIGN hSigner, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr); + -HCOSE_COUNTERSIGN COSE_Signer_get_countersignature(HCOSE_SIGNER hSigner, int index, cose_errback* perr); /* */ diff --git a/src/CounterSign.c b/src/CounterSign.c index f9368c5c..6f9c8c9c 100644 --- a/src/CounterSign.c +++ b/src/CounterSign.c @@ -129,21 +129,6 @@ bool _COSE_CounterSign_add(COSE* pMessage, HCOSE_COUNTERSIGN hSigner, cose_errba } -HCOSE_COUNTERSIGN COSE_Signer_add_countersignature(HCOSE_SIGNER hSigner, HCOSE_COUNTERSIGN hCountersign, cose_errback* perr) -{ - CHECK_CONDITION(IsValidSignerHandle(hSigner), COSE_ERR_INVALID_HANDLE); - CHECK_CONDITION(IsValidCounterSignHandle(hCountersign), COSE_ERR_INVALID_HANDLE); - - if (!_COSE_CounterSign_add(&((COSE_SignerInfo*)hSigner)->m_message, hCountersign, perr)) { - goto errorReturn; - } - - return hCountersign; - -errorReturn: - return NULL; -} - /// Get the n-th counter signature from the attribute. /// @@ -255,45 +240,6 @@ COSE_CounterSign* _COSE_Message_get_countersignature(COSE* pMessage, int index, return NULL; } -HCOSE_COUNTERSIGN COSE_Signer_get_countersignature(HCOSE_SIGNER hSigner, int index, cose_errback* perr) -{ - COSE_CounterSign* p = NULL; - - CHECK_CONDITION(IsValidSignerHandle(hSigner), COSE_ERR_INVALID_HANDLE); - - p = _COSE_Message_get_countersignature(&((COSE_SignerInfo*)hSigner)->m_message, index, perr); - - errorReturn: - return (HCOSE_COUNTERSIGN)p; -} - -bool COSE_CounterSign_validate(HCOSE_SIGNER hSigner, HCOSE_COUNTERSIGN hCountersignature, cose_errback* perr) -{ - bool f; - COSE_SignerInfo* pSigner; - COSE_CounterSign* pCountersign; - const cn_cbor* cnContent; - const cn_cbor* cnProtected; - - CHECK_CONDITION(IsValidSignerHandle(hSigner), COSE_ERR_INVALID_HANDLE); - CHECK_CONDITION(IsValidCounterSignHandle(hCountersignature), COSE_ERR_INVALID_HANDLE); - - pSigner = (COSE_SignerInfo*)hSigner; - pCountersign = (COSE_CounterSign*)hCountersignature; - - cnContent = _COSE_arrayget_int(&pSigner->m_message, INDEX_BODY); - CHECK_CONDITION(cnContent != NULL && cnContent->type == CN_CBOR_BYTES, COSE_ERR_INVALID_PARAMETER); - - cnProtected = _COSE_arrayget_int(&pSigner->m_message, INDEX_PROTECTED); - CHECK_CONDITION(cnProtected != NULL && cnProtected->type == CN_CBOR_BYTES, COSE_ERR_INVALID_PARAMETER); - - f = _COSE_Signer_validate(&pCountersign->m_signer, cnContent, cnProtected, "CounterSignature", perr); - - return f; - -errorReturn: - return false; -} bool COSE_CounterSign_map_put_int(HCOSE_COUNTERSIGN h, int key, @@ -343,6 +289,198 @@ bool COSE_CounterSign_SetExternal(HCOSE_COUNTERSIGN hcose, pbExternalData, cbExternalData, perr); } +bool _COSE_CounterSign_Sign(COSE * baseMessage, CBOR_CONTEXT_COMMA cose_errback * perr) +{ + bool fRet = false; + cn_cbor* pcborProtectedSign = NULL; + + cn_cbor* pSignature = + _COSE_arrayget_int(baseMessage, INDEX_SIGNATURE); + int count = 0; + + COSE_CounterSign* pCountersign = baseMessage->m_counterSigners; + for (; pCountersign != NULL; + pCountersign = pCountersign->m_next, count += 1) { + pcborProtectedSign = _COSE_encode_protected(baseMessage, perr); + if (pcborProtectedSign == NULL) { + goto errorReturn; + } + if (!_COSE_Signer_sign(&pCountersign->m_signer, pSignature, + pcborProtectedSign, "CounterSignature", perr)) { + goto errorReturn; + } + } + + if (count == 1) { + cn_cbor* cn = COSE_get_cbor((HCOSE)baseMessage->m_counterSigners); + CHECK_CONDITION( + _COSE_map_put(baseMessage, + COSE_Header_CounterSign, cn, COSE_UNPROTECT_ONLY, perr), + COSE_ERR_OUT_OF_MEMORY); + } + else { + cn_cbor_errback cn_error; + cn_cbor* cn_counterSign = + cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA & cn_error); + CHECK_CONDITION_CBOR(cn_counterSign, cn_error); + + for (pCountersign = baseMessage->m_counterSigners; + pCountersign != NULL; pCountersign = pCountersign->m_next) { + cn_cbor* cn = COSE_get_cbor((HCOSE)pCountersign); + CHECK_CONDITION_CBOR( + cn_cbor_array_append(cn_counterSign, cn, &cn_error), cn_error); + } + CHECK_CONDITION(_COSE_map_put(baseMessage, + COSE_Header_CounterSign, cn_counterSign, + COSE_UNPROTECT_ONLY, perr), + COSE_ERR_OUT_OF_MEMORY); + } + + fRet = true; + errorReturn: + return fRet; +} + + +/*************************************************************************************************** + * + * SIGNER + */ +HCOSE_COUNTERSIGN COSE_Signer_add_countersignature(HCOSE_SIGNER hSigner, + HCOSE_COUNTERSIGN hCountersign, + cose_errback* perr) +{ + CHECK_CONDITION(IsValidSignerHandle(hSigner), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION( + IsValidCounterSignHandle(hCountersign), COSE_ERR_INVALID_HANDLE); + + if (!_COSE_CounterSign_add( + &((COSE_SignerInfo*)hSigner)->m_message, hCountersign, perr)) { + goto errorReturn; + } + + return hCountersign; + +errorReturn: + return NULL; +} + +HCOSE_COUNTERSIGN COSE_Signer_get_countersignature(HCOSE_SIGNER hSigner, + int index, + cose_errback* perr) +{ + COSE_CounterSign* p = NULL; + + CHECK_CONDITION(IsValidSignerHandle(hSigner), COSE_ERR_INVALID_HANDLE); + + p = _COSE_Message_get_countersignature( + &((COSE_SignerInfo*)hSigner)->m_message, index, perr); + +errorReturn: + return (HCOSE_COUNTERSIGN)p; +} + +bool COSE_Signer_CounterSign_validate(HCOSE_SIGNER hSigner, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr) +{ + CHECK_CONDITION(IsValidSignerHandle(hSigner), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION( + IsValidCounterSignHandle(hCountersignature), COSE_ERR_INVALID_HANDLE); + + COSE_SignerInfo* pSigner = (COSE_SignerInfo*)hSigner; + COSE_CounterSign* pCountersign = (COSE_CounterSign*)hCountersignature; + + const cn_cbor* cnContent = _COSE_arrayget_int( + &pSigner->m_message, INDEX_BODY); + CHECK_CONDITION(cnContent != NULL && cnContent->type == CN_CBOR_BYTES, + COSE_ERR_INVALID_PARAMETER); + + const cn_cbor* cnProtected = _COSE_arrayget_int( + &pSigner->m_message, INDEX_PROTECTED); + CHECK_CONDITION(cnProtected != NULL && cnProtected->type == CN_CBOR_BYTES, + COSE_ERR_INVALID_PARAMETER); + + bool f = _COSE_Signer_validate(&pCountersign->m_signer, cnContent, + cnProtected, + "CounterSignature", perr); + + return f; + +errorReturn: + return false; +} + + +/*************************************************************************************************** + * + * SIGN MESSAGE + */ +HCOSE_COUNTERSIGN COSE_Sign_add_countersignature(HCOSE_SIGN hSignMsg, + HCOSE_COUNTERSIGN hCountersign, + cose_errback* perr) +{ + CHECK_CONDITION(IsValidSignHandle(hSignMsg), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION( + IsValidCounterSignHandle(hCountersign), COSE_ERR_INVALID_HANDLE); + + if (!_COSE_CounterSign_add( + &((COSE_SignMessage*)hSignMsg)->m_message, hCountersign, perr)) { + goto errorReturn; + } + + return hCountersign; + +errorReturn: + return NULL; +} + +HCOSE_COUNTERSIGN COSE_Sign_get_countersignature(HCOSE_SIGN hSignMsg, + int index, + cose_errback* perr) +{ + COSE_CounterSign* p = NULL; + + CHECK_CONDITION(IsValidSignHandle(hSignMsg), COSE_ERR_INVALID_HANDLE); + + p = _COSE_Message_get_countersignature( + &((COSE_SignMessage*)hSignMsg)->m_message, index, perr); + +errorReturn: + return (HCOSE_COUNTERSIGN)p; +} + +bool COSE_Sign_CounterSign_validate(HCOSE_SIGN hSignMsg, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr) +{ + CHECK_CONDITION(IsValidSignHandle(hSignMsg), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION( + IsValidCounterSignHandle(hCountersignature), COSE_ERR_INVALID_HANDLE); + + COSE_SignMessage* pSignMsg = (COSE_SignMessage*)hSignMsg; + COSE_CounterSign* pCountersign = (COSE_CounterSign*)hCountersignature; + + const cn_cbor* cnContent = _COSE_arrayget_int( + &pSignMsg->m_message, INDEX_BODY); + CHECK_CONDITION(cnContent != NULL && cnContent->type == CN_CBOR_BYTES, + COSE_ERR_INVALID_PARAMETER); + + const cn_cbor* cnProtected = _COSE_arrayget_int( + &pSignMsg->m_message, INDEX_PROTECTED); + CHECK_CONDITION(cnProtected != NULL && cnProtected->type == CN_CBOR_BYTES, + COSE_ERR_INVALID_PARAMETER); + + bool f = _COSE_Signer_validate(&pCountersign->m_signer, cnContent, + cnProtected, + "CounterSignature", perr); + + return f; + +errorReturn: + return false; +} + #endif diff --git a/src/Sign.c b/src/Sign.c index f97dd595..c9bd0c05 100644 --- a/src/Sign.c +++ b/src/Sign.c @@ -27,7 +27,7 @@ COSE *SignRoot = NULL; * @returns result of check */ -static bool IsValidSignHandle(HCOSE_SIGN h) +bool IsValidSignHandle(HCOSE_SIGN h) { COSE_SignMessage *p = (COSE_SignMessage *)h; @@ -261,7 +261,7 @@ HCOSE_SIGNER COSE_Sign_add_signer(HCOSE_SIGN hSign, bool COSE_Sign_Sign(HCOSE_SIGN h, cose_errback *perr) { #ifdef USE_CBOR_CONTEXT - // cn_cbor_context * context = NULL; + cn_cbor_context * context = NULL; #endif COSE_SignMessage *pMessage = (COSE_SignMessage *)h; COSE_SignerInfo *pSigner; @@ -274,7 +274,7 @@ bool COSE_Sign_Sign(HCOSE_SIGN h, cose_errback *perr) return false; } #ifdef USE_CBOR_CONTEXT - // context = &pMessage->m_message.m_allocContext; + context = &pMessage->m_message.m_allocContext; #endif pcborBody = _COSE_arrayget_int(&pMessage->m_message, INDEX_BODY); @@ -292,6 +292,15 @@ bool COSE_Sign_Sign(HCOSE_SIGN h, cose_errback *perr) } } +#ifdef INCLUDE_COUNTERSIGNATURE + if (pMessage->m_message.m_counterSigners != NULL) { + if (!_COSE_CounterSign_Sign( + &pMessage->m_message, CBOR_CONTEXT_PARAM_COMMA perr)) { + goto errorReturn; + } + } +#endif + return true; } diff --git a/src/SignerInfo.c b/src/SignerInfo.c index abadbe97..20640649 100644 --- a/src/SignerInfo.c +++ b/src/SignerInfo.c @@ -294,54 +294,14 @@ bool _COSE_Signer_sign(COSE_SignerInfo *pSigner, FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM); } +#ifdef INCLUDE_COUNTERSIGNATURE if (pSigner->m_message.m_counterSigners != NULL) { - cn_cbor *pSignature = - _COSE_arrayget_int(&pSigner->m_message, INDEX_SIGNATURE); - int count = 0; - - COSE_CounterSign *pCountersign = pSigner->m_message.m_counterSigners; - for (; pCountersign != NULL; - pCountersign = pCountersign->m_next, - count += 1) { - pcborProtectedSign = - _COSE_encode_protected(&pSigner->m_message, perr); - if (pcborProtectedSign == NULL) { - goto errorReturn; - } - if (!_COSE_Signer_sign(&pCountersign->m_signer, pSignature, - pcborProtectedSign, "CounterSignature", perr)) { - goto errorReturn; - } - } - - if (count == 1) { - cn_cbor * cn = COSE_get_cbor((HCOSE)pSigner->m_message.m_counterSigners); - CHECK_CONDITION( - COSE_Signer_map_put_int((HCOSE_SIGNER)pSigner, - COSE_Header_CounterSign, cn, COSE_UNPROTECT_ONLY, perr), - COSE_ERR_OUT_OF_MEMORY); - } - else { - cn_cbor_errback cn_error; - cn_cbor *cn_counterSign = - cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA & cn_error); - CHECK_CONDITION_CBOR(cn_counterSign, cn_error); - - for (pCountersign = pSigner->m_message.m_counterSigners; - pCountersign != NULL; - pCountersign = pCountersign->m_next) { - cn_cbor *cn = COSE_get_cbor((HCOSE) pCountersign); - CHECK_CONDITION_CBOR( - cn_cbor_array_append(cn_counterSign, cn, &cn_error), - cn_error); - } - CHECK_CONDITION( - COSE_Signer_map_put_int((HCOSE_SIGNER)pSigner, - COSE_Header_CounterSign, cn_counterSign, COSE_UNPROTECT_ONLY, perr), - COSE_ERR_OUT_OF_MEMORY); + if (!_COSE_CounterSign_Sign(&pSigner->m_message, CBOR_CONTEXT_PARAM_COMMA perr)) { + goto errorReturn; } } - +#endif + fRet = true; errorReturn: diff --git a/src/cose_int.h b/src/cose_int.h index 6e965644..9dbca023 100644 --- a/src/cose_int.h +++ b/src/cose_int.h @@ -9,12 +9,12 @@ // interface, and they were quite confusing in cn-cbor.h #ifdef INCLUDE_COUNTERSIGNATURE -struct _COSE_COUNTER_SIGN; -typedef struct _COSE_COUNTER_SIGN COSE_CounterSign; +struct CounterSign; +typedef struct CounterSign COSE_CounterSign; #endif #ifdef USE_COUNTER_SIGNATURE1 -struct _COSE_COUNTER_SIGN1; -typedef struct _COSE_COUNTER_SIGN0 COSE_CounterSign1; +struct CounterSign1; +typedef struct CounterSign1 COSE_CounterSign1; #endif #define UNUSED(x) ((void)(x)) @@ -106,10 +106,10 @@ typedef struct { typedef COSE_MacMessage COSE_Mac0Message; #ifdef INCLUDE_COUNTERSIGNATURE -typedef struct _COSE_COUNTER_SIGN { +struct CounterSign { COSE_SignerInfo m_signer; COSE_CounterSign *m_next; -} COSE_CounterSign; +}; #endif #ifdef USE_CBOR_CONTEXT @@ -176,6 +176,7 @@ void _COSE_RemoveFromList(COSE **rootNode, COSE *thisMsg); bool IsValidEncryptHandle(HCOSE_ENCRYPT h); bool IsValidEnvelopedHandle(HCOSE_ENVELOPED h); bool IsValidRecipientHandle(HCOSE_RECIPIENT h); +bool IsValidSignHandle(HCOSE_SIGN h); bool IsValidSignerHandle(HCOSE_SIGNER h); bool IsValidCounterSignHandle(HCOSE_COUNTERSIGN h); @@ -330,6 +331,9 @@ bool _COSE_CountSign_create(COSE *pMessage, COSE_CounterSign * _COSE_CounterSign_Init_From_Object(cn_cbor* cbor, COSE_CounterSign *, CBOR_CONTEXT_COMMA cose_errback* perr); +bool _COSE_CounterSign_Sign(COSE *baseMessage, + CBOR_CONTEXT_COMMA cose_errback *perr); + // // Debugging Items diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f99e2045..2158d35d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -108,6 +108,12 @@ add_test( NAME sign1 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND cose_test --dir Examples/sign1-tests) +if(COSE_C_INCLUDE_COUNTERSIGN) + add_test( + NAME Countersign + WORKING_DIRECTORY $(CMAKE_SOURCE_DIR) + COMMAND cose_test --dir Examples/countersign) +endif() add_test( NAME corner-cases diff --git a/test/encrypt.c b/test/encrypt.c index 43a9b928..d845144f 100644 --- a/test/encrypt.c +++ b/test/encrypt.c @@ -541,7 +541,7 @@ int _ValidateEncrypt(const cn_cbor *pControl, } size_t cb; - byte *pb; + const byte *pb; pb = COSE_Encrypt_GetContent(hEnc, &cb, NULL); } else { diff --git a/test/sign.c b/test/sign.c index c17ebd13..07211434 100644 --- a/test/sign.c +++ b/test/sign.c @@ -107,29 +107,35 @@ int _ValidateSigned(const cn_cbor *pControl, fFail = true; } - cn_cbor* counter = cn_cbor_mapget_string(pSigners, "countersign"); - if (counter != NULL) { - counter = cn_cbor_mapget_string(counter, "signers"); - if (counter == NULL) { +#if INCLUDE_COUNTERSIGNATURE + // Validate counter signatures on signers + cn_cbor* countersignList = cn_cbor_mapget_string(pSigners, "countersign"); + if (countersignList != NULL) { + cn_cbor * countersigners = cn_cbor_mapget_string(countersignList, "signers"); + if (countersigners == NULL) { fFail = true; continue; } - int count = counter->length; - cn_cbor* counterSigners = counter->first_child; + int count = countersigners->length; + bool forward = true; + if (COSE_Signer_map_get_int(hSigner, COSE_Header_CounterSign, COSE_UNPROTECT_ONLY, 0) == NULL) { goto returnError; } - for (int counterNo = 0; counterNo < count && counterSigners != NULL; - counterNo++, counterSigners = counterSigners->next) { + for (int counterNo = 0; counterNo < count ; + counterNo++) { HCOSE_COUNTERSIGN h = COSE_Signer_get_countersignature(hSigner, counterNo, 0); if (h == NULL) { fFail = true; continue; } + cn_cbor *counterSigner = cn_cbor_index(countersigners, + forward ? counterNo : count - counterNo - 1); + cn_cbor *pkeyCountersign = BuildKey( - cn_cbor_mapget_string(counterSigners, "key"), false); + cn_cbor_mapget_string(counterSigner, "key"), false); if (pkeyCountersign == NULL) { fFail = true; continue; @@ -140,19 +146,91 @@ int _ValidateSigned(const cn_cbor *pControl, continue; } - if (COSE_CounterSign_validate(hSigner, h, 0)) { + if (COSE_Signer_CounterSign_validate(hSigner, h, 0)) { // I don't think we have any forced errors yet. } else { - fFail = true; + if (forward && counterNo == 0 && count > 1) { + forward = false; + counterNo -= 1; + } + else { + fFail = true; + } } CN_CBOR_FREE(pkeyCountersign, context); COSE_CounterSign_Free(h); } } +#endif +#if INCLUDE_COUNTERSIGNATURE + // Countersign on Signed Body + + if (iSigner == (int)pSigners->length - 1) { + // Validate counter signatures on signers + cn_cbor *countersignList = + cn_cbor_mapget_string(pSign, "countersign"); + if (countersignList != NULL) { + cn_cbor *countersigners = + cn_cbor_mapget_string(countersignList, "signers"); + if (countersigners == NULL) { + fFail = true; + continue; + } + int count = countersigners->length; + bool forward = true; + + if (COSE_Signer_map_get_int(hSigner, COSE_Header_CounterSign, + COSE_UNPROTECT_ONLY, 0) == NULL) { + goto returnError; + } + + for (int counterNo = 0; counterNo < count; counterNo++) { + HCOSE_COUNTERSIGN h = + COSE_Signer_get_countersignature(hSigner, counterNo, 0); + if (h == NULL) { + fFail = true; + continue; + } + + cn_cbor *counterSigner = cn_cbor_index(countersigners, + forward ? counterNo : count - counterNo - 1); + + cn_cbor *pkeyCountersign = BuildKey( + cn_cbor_mapget_string(counterSigner, "key"), false); + if (pkeyCountersign == NULL) { + fFail = true; + continue; + } + + if (!COSE_CounterSign_SetKey(h, pkeyCountersign, 0)) { + fFail = true; + continue; + } + + if (COSE_Signer_CounterSign_validate(hSigner, h, 0)) { + // I don't think we have any forced errors yet. + } + else { + if (forward && counterNo == 0 && count > 1) { + forward = false; + counterNo -= 1; + } + else { + fFail = true; + } + } + + CN_CBOR_FREE(pkeyCountersign, context); + COSE_CounterSign_Free(h); + } + } + } +#endif + COSE_Sign_Free(hSig); COSE_Signer_Free(hSigner); } @@ -235,7 +313,9 @@ int BuildSignedMessage(const cn_cbor *pControl) if (!COSE_Sign_AddSigner(hSignObj, hSigner, NULL)) goto returnError; - cn_cbor* countersigns = cn_cbor_mapget_string(pSigners, "countersign"); +#if INCLUDE_COUNTERSIGNATURE + // On the signer object + cn_cbor *countersigns = cn_cbor_mapget_string(pSigners, "countersign"); if (countersigns != NULL) { countersigns = cn_cbor_mapget_string(countersigns, "signers"); cn_cbor* countersign = countersigns->first_child; @@ -266,7 +346,49 @@ int BuildSignedMessage(const cn_cbor *pControl) COSE_CounterSign_Free(hCountersign); } } +#endif + +#ifdef INCLUDE_COUNTERSIGNATURE + // On the sign body + countersigns = cn_cbor_mapget_string(pSign, "countersign"); + if (countersigns != NULL) { + countersigns = cn_cbor_mapget_string(countersigns, "signers"); + cn_cbor *countersign = countersigns->first_child; + for (; countersign != NULL; countersign = countersign->next) { + cn_cbor *pkeyCountersign = + BuildKey(cn_cbor_mapget_string(countersign, "key"), false); + if (pkeyCountersign == NULL) { + goto returnError; + } + + HCOSE_COUNTERSIGN hCountersign = + COSE_CounterSign_Init(CBOR_CONTEXT_PARAM_COMMA NULL); + if (hCountersign == NULL) { + goto returnError; + } + + if (!SetSendingAttributes((HCOSE)hCountersign, countersign, + Attributes_Countersign_protected)) { + goto returnError; + } + + if (!COSE_CounterSign_SetKey( + hCountersign, pkeyCountersign, NULL)) { + goto returnError; + } + + if (!COSE_Sign_add_countersignature( + hSignObj, hCountersign, NULL)) { + goto returnError; + } + + COSE_CounterSign_Free(hCountersign); + } + } + +#endif + COSE_Signer_Free(hSigner); } diff --git a/test/test.c b/test/test.c index acf2f598..2989ea56 100644 --- a/test/test.c +++ b/test/test.c @@ -1025,7 +1025,7 @@ void RunMemoryTest(const char* szFileName) #endif } -typedef bool (__cdecl *ValidatePtr)(const cn_cbor* pControl); +typedef int (__cdecl *ValidatePtr)(const cn_cbor* pControl); bool ProcessFile(const cn_cbor* pControl, ValidatePtr validateFunction, ValidatePtr buildFunction) { @@ -1266,10 +1266,12 @@ int main(int argc, char** argv) #endif } - if (CFails > 0) + if (CFails > 0) { fprintf(stderr, "Failed %d tests\n", CFails); - else + } + else { fprintf(stderr, "SUCCESS\n"); + } exit(CFails); } From bc6a6ba97a5900e0ebbc0718bd302ceb3f07e9e9 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Sun, 26 Apr 2020 12:22:43 -0700 Subject: [PATCH 09/38] Add calls for Sign1 --- include/cose/cose.h | 10 ++++ src/Cose.c | 3 +- src/CounterSign.c | 67 ++++++++++++++++++++++++++ src/Sign1.c | 15 ++++-- src/SignerInfo.c | 2 - src/cose_int.h | 1 + src/openssl.c | 1 + test/sign.c | 114 +++++++++++++++++++++++++++++++++++++++++--- test/test.c | 2 +- 9 files changed, 202 insertions(+), 13 deletions(-) diff --git a/include/cose/cose.h b/include/cose/cose.h index 7b933be9..97cb843f 100644 --- a/include/cose/cose.h +++ b/include/cose/cose.h @@ -567,6 +567,7 @@ HCOSE_COUNTERSIGN COSE_Signer_get_countersignature(HCOSE_SIGNER hSigner, bool COSE_Signer_CounterSign_validate(HCOSE_SIGNER hSigner, HCOSE_COUNTERSIGN hCountersignature, cose_errback* perr); + HCOSE_COUNTERSIGN COSE_Sign_add_countersignature(HCOSE_SIGN hSignMsg, HCOSE_COUNTERSIGN hCountersignature, cose_errback* perr); @@ -577,6 +578,15 @@ bool COSE_Sign_CounterSign_validate(HCOSE_SIGN hSigner, HCOSE_COUNTERSIGN hCountersignature, cose_errback* perr); +HCOSE_COUNTERSIGN COSE_Sign1_add_countersignature(HCOSE_SIGN1 hSignMsg, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr); +HCOSE_COUNTERSIGN COSE_Sign1_get_countersignature(HCOSE_SIGN1 hSignMsg, + int index, + cose_errback* perr); +bool COSE_Sign1_CounterSign_validate(HCOSE_SIGN1 hSigner, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr); /* diff --git a/src/Cose.c b/src/Cose.c index 4c247337..4771d56b 100644 --- a/src/Cose.c +++ b/src/Cose.c @@ -561,10 +561,11 @@ void _COSE_RemoveFromList(COSE **root, COSE *thisMsg) extern COSE *CountersignRoot; extern COSE *SignerRoot; extern COSE *SignRoot; +extern COSE *Sign1Root; bool AreListsEmpty() { - return CountersignRoot == NULL && SignerRoot == NULL && SignRoot == NULL; + return CountersignRoot == NULL && SignerRoot == NULL && SignRoot == NULL && Sign1Root == NULL; } #endif diff --git a/src/CounterSign.c b/src/CounterSign.c index 6f9c8c9c..a655e84b 100644 --- a/src/CounterSign.c +++ b/src/CounterSign.c @@ -481,6 +481,73 @@ bool COSE_Sign_CounterSign_validate(HCOSE_SIGN hSignMsg, return false; } +/*************************************************************************************************** + * + * SIGN1 MESSAGE + */ +HCOSE_COUNTERSIGN COSE_Sign1_add_countersignature(HCOSE_SIGN1 hSignMsg, + HCOSE_COUNTERSIGN hCountersign, + cose_errback* perr) +{ + CHECK_CONDITION(IsValidSign1Handle(hSignMsg), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION( + IsValidCounterSignHandle(hCountersign), COSE_ERR_INVALID_HANDLE); + + if (!_COSE_CounterSign_add( + &((COSE_SignMessage*)hSignMsg)->m_message, hCountersign, perr)) { + goto errorReturn; + } + + return hCountersign; + +errorReturn: + return NULL; +} + +HCOSE_COUNTERSIGN COSE_Sign1_get_countersignature(HCOSE_SIGN1 hSignMsg, + int index, + cose_errback* perr) +{ + COSE_CounterSign* p = NULL; + + CHECK_CONDITION(IsValidSign1Handle(hSignMsg), COSE_ERR_INVALID_HANDLE); + + p = _COSE_Message_get_countersignature( + &((COSE_SignMessage*)hSignMsg)->m_message, index, perr); + +errorReturn: + return (HCOSE_COUNTERSIGN)p; +} + +bool COSE_Sign1_CounterSign_validate(HCOSE_SIGN1 hSignMsg, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr) +{ + CHECK_CONDITION(IsValidSign1Handle(hSignMsg), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION( + IsValidCounterSignHandle(hCountersignature), COSE_ERR_INVALID_HANDLE); + + COSE_Sign1Message* pSignMsg = (COSE_Sign1Message*)hSignMsg; + COSE_CounterSign* pCountersign = (COSE_CounterSign*)hCountersignature; + + const cn_cbor* cnContent = + _COSE_arrayget_int(&pSignMsg->m_message, INDEX_BODY); + CHECK_CONDITION(cnContent != NULL && cnContent->type == CN_CBOR_BYTES, + COSE_ERR_INVALID_PARAMETER); + + const cn_cbor* cnProtected = + _COSE_arrayget_int(&pSignMsg->m_message, INDEX_PROTECTED); + CHECK_CONDITION(cnProtected != NULL && cnProtected->type == CN_CBOR_BYTES, + COSE_ERR_INVALID_PARAMETER); + + bool f = _COSE_Signer_validate(&pCountersign->m_signer, cnContent, + cnProtected, "CounterSignature", perr); + + return f; + +errorReturn: + return false; +} #endif diff --git a/src/Sign1.c b/src/Sign1.c index cfc6c120..4d8e54a4 100644 --- a/src/Sign1.c +++ b/src/Sign1.c @@ -20,7 +20,7 @@ bool _COSE_Signer0_validate(COSE_Sign1Message *pSign, cose_errback *perr); void _COSE_Sign1_Release(COSE_Sign1Message *p); -static COSE *Sign1Root = NULL; +COSE *Sign1Root = NULL; /*! \private * @brief Test if a HCOSE_SIGN1 handle is valid @@ -209,7 +209,7 @@ bool COSE_Sign1_SetExternal(HCOSE_SIGN1 hcose, bool COSE_Sign1_Sign(HCOSE_SIGN1 h, const cn_cbor *pKey, cose_errback *perr) { #ifdef USE_CBOR_CONTEXT - // cn_cbor_context * context = NULL; + cn_cbor_context * context = NULL; #endif COSE_Sign1Message *pMessage = (COSE_Sign1Message *)h; const cn_cbor *pcborProtected; @@ -220,7 +220,7 @@ bool COSE_Sign1_Sign(HCOSE_SIGN1 h, const cn_cbor *pKey, cose_errback *perr) return false; } #ifdef USE_CBOR_CONTEXT - // context = &pMessage->m_message.m_allocContext; + context = &pMessage->m_message.m_allocContext; #endif pcborProtected = _COSE_encode_protected(&pMessage->m_message, perr); @@ -230,6 +230,15 @@ bool COSE_Sign1_Sign(HCOSE_SIGN1 h, const cn_cbor *pKey, cose_errback *perr) if (!_COSE_Signer0_sign(pMessage, pKey, perr)) goto errorReturn; +#ifdef INCLUDE_COUNTERSIGNATURE + if (pMessage->m_message.m_counterSigners != NULL) { + if (!_COSE_CounterSign_Sign( + &pMessage->m_message, CBOR_CONTEXT_PARAM_COMMA perr)) { + goto errorReturn; + } + } +#endif + return true; } diff --git a/src/SignerInfo.c b/src/SignerInfo.c index 20640649..02f08417 100644 --- a/src/SignerInfo.c +++ b/src/SignerInfo.c @@ -369,8 +369,6 @@ bool _COSE_Signer_validate(COSE_SignerInfo *pSigner, int alg = 0; #ifdef USE_CBOR_CONTEXT cn_cbor_context *context = &pSigner->m_message.m_allocContext; -#else - UNUSED(pSign); #endif size_t cbToBeSigned; bool fRet = false; diff --git a/src/cose_int.h b/src/cose_int.h index 9dbca023..a4103826 100644 --- a/src/cose_int.h +++ b/src/cose_int.h @@ -178,6 +178,7 @@ bool IsValidEnvelopedHandle(HCOSE_ENVELOPED h); bool IsValidRecipientHandle(HCOSE_RECIPIENT h); bool IsValidSignHandle(HCOSE_SIGN h); bool IsValidSignerHandle(HCOSE_SIGNER h); +bool IsValidSign1Handle(HCOSE_SIGN1 h); bool IsValidCounterSignHandle(HCOSE_COUNTERSIGN h); bool _COSE_Init(COSE_INIT_FLAGS flags, diff --git a/src/openssl.c b/src/openssl.c index e3b2ccb7..b3975937 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -796,6 +796,7 @@ bool HKDF_Extract(COSE *pcose, size_t *pcbDigest, CBOR_CONTEXT_COMMA cose_errback *perr) { + UNUSED(context); byte rgbSalt[EVP_MAX_MD_SIZE] = {0}; int cbSalt; cn_cbor *cnSalt; diff --git a/test/sign.c b/test/sign.c index 07211434..8f87333d 100644 --- a/test/sign.c +++ b/test/sign.c @@ -169,9 +169,9 @@ int _ValidateSigned(const cn_cbor *pControl, #if INCLUDE_COUNTERSIGNATURE // Countersign on Signed Body - if (iSigner == (int)pSigners->length - 1) { + if (iSigner == 0) { // Validate counter signatures on signers - cn_cbor *countersignList = + countersignList = cn_cbor_mapget_string(pSign, "countersign"); if (countersignList != NULL) { cn_cbor *countersigners = @@ -183,14 +183,14 @@ int _ValidateSigned(const cn_cbor *pControl, int count = countersigners->length; bool forward = true; - if (COSE_Signer_map_get_int(hSigner, COSE_Header_CounterSign, + if (COSE_Sign_map_get_int(hSig, COSE_Header_CounterSign, COSE_UNPROTECT_ONLY, 0) == NULL) { goto returnError; } for (int counterNo = 0; counterNo < count; counterNo++) { HCOSE_COUNTERSIGN h = - COSE_Signer_get_countersignature(hSigner, counterNo, 0); + COSE_Sign_get_countersignature(hSig, counterNo, 0); if (h == NULL) { fFail = true; continue; @@ -211,7 +211,7 @@ int _ValidateSigned(const cn_cbor *pControl, continue; } - if (COSE_Signer_CounterSign_validate(hSigner, h, 0)) { + if (COSE_Sign_CounterSign_validate(hSig, h, 0)) { // I don't think we have any forced errors yet. } else { @@ -570,6 +570,69 @@ int _ValidateSign1(const cn_cbor *pControl, fFail = true; } +#if INCLUDE_COUNTERSIGNATURE + // Countersign on Signed Body + + // Validate counter signatures on signers + cn_cbor * countersignList = cn_cbor_mapget_string(pSign, "countersign"); + if (countersignList != NULL) { + cn_cbor *countersigners = + cn_cbor_mapget_string(countersignList, "signers"); + if (countersigners == NULL) { + fFail = true; + goto exitHere; + } + int count = countersigners->length; + bool forward = true; + + if (COSE_Sign1_map_get_int(hSig, COSE_Header_CounterSign, + COSE_UNPROTECT_ONLY, 0) == NULL) { + goto returnError; + } + + for (int counterNo = 0; counterNo < count; counterNo++) { + HCOSE_COUNTERSIGN h = + COSE_Sign1_get_countersignature(hSig, counterNo, 0); + if (h == NULL) { + fFail = true; + goto exitHere; + } + + cn_cbor *counterSigner = cn_cbor_index( + countersigners, forward ? counterNo : count - counterNo - 1); + + cn_cbor *pkeyCountersign = + BuildKey(cn_cbor_mapget_string(counterSigner, "key"), false); + if (pkeyCountersign == NULL) { + fFail = true; + goto exitHere; + } + + if (!COSE_CounterSign_SetKey(h, pkeyCountersign, 0)) { + fFail = true; + goto exitHere; + } + + if (COSE_Sign1_CounterSign_validate(hSig, h, 0)) { + // I don't think we have any forced errors yet. + } + else { + if (forward && counterNo == 0 && count > 1) { + forward = false; + counterNo -= 1; + } + else { + fFail = true; + } + } + + CN_CBOR_FREE(pkeyCountersign, context); + COSE_CounterSign_Free(h); + } + } +#endif + + COSE_Sign1_Free(hSig); if (fFailBody) { @@ -630,9 +693,48 @@ int BuildSign1Message(const cn_cbor *pControl) if (pkey == NULL) goto returnError; +#ifdef INCLUDE_COUNTERSIGNATURE + // On the sign body + cn_cbor * countersigns = cn_cbor_mapget_string(pSign, "countersign"); + if (countersigns != NULL) { + countersigns = cn_cbor_mapget_string(countersigns, "signers"); + cn_cbor *countersign = countersigns->first_child; + + for (; countersign != NULL; countersign = countersign->next) { + cn_cbor *pkeyCountersign = + BuildKey(cn_cbor_mapget_string(countersign, "key"), false); + if (pkeyCountersign == NULL) { + goto returnError; + } + + HCOSE_COUNTERSIGN hCountersign = + COSE_CounterSign_Init(CBOR_CONTEXT_PARAM_COMMA NULL); + if (hCountersign == NULL) { + goto returnError; + } + + if (!SetSendingAttributes((HCOSE)hCountersign, countersign, + Attributes_Countersign_protected)) { + goto returnError; + } + + if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) { + goto returnError; + } + + if (!COSE_Sign1_add_countersignature(hSignObj, hCountersign, NULL)) { + goto returnError; + } + + COSE_CounterSign_Free(hCountersign); + } + } + +#endif + if (!COSE_Sign1_Sign(hSignObj, pkey, NULL)) goto returnError; - + size_t cb = COSE_Encode((HCOSE)hSignObj, NULL, 0, 0) + 1; byte *rgb = (byte *)malloc(cb); cb = COSE_Encode((HCOSE)hSignObj, rgb, 0, cb); diff --git a/test/test.c b/test/test.c index 2989ea56..3551b66a 100644 --- a/test/test.c +++ b/test/test.c @@ -1025,7 +1025,7 @@ void RunMemoryTest(const char* szFileName) #endif } -typedef int (__cdecl *ValidatePtr)(const cn_cbor* pControl); +typedef int (*ValidatePtr)(const cn_cbor* pControl); bool ProcessFile(const cn_cbor* pControl, ValidatePtr validateFunction, ValidatePtr buildFunction) { From 8ceeca82d5ac9a1903e3d807b55151bed5e9946f Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Sun, 26 Apr 2020 14:58:27 -0700 Subject: [PATCH 10/38] Add support for Encrypt0 messages --- include/cose/cose.h | 31 ++++++- src/Cose.c | 9 +- src/CounterSign.c | 212 +++++++++++++++++++++++++++++++++++++++++++- src/Encrypt.c | 21 +++-- src/Encrypt0.c | 2 +- src/MacMessage.c | 2 +- src/MacMessage0.c | 2 +- src/Recipient.c | 2 +- test/encrypt.c | 102 +++++++++++++++++++++ 9 files changed, 365 insertions(+), 18 deletions(-) diff --git a/include/cose/cose.h b/include/cose/cose.h index 97cb843f..b3eb7d13 100644 --- a/include/cose/cose.h +++ b/include/cose/cose.h @@ -588,8 +588,37 @@ bool COSE_Sign1_CounterSign_validate(HCOSE_SIGN1 hSigner, HCOSE_COUNTERSIGN hCountersignature, cose_errback* perr); +HCOSE_COUNTERSIGN COSE_Encrypt0_add_countersignature(HCOSE_ENCRYPT hSignMsg, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr); +HCOSE_COUNTERSIGN COSE_Encrypt0_get_countersignature(HCOSE_ENCRYPT hSignMsg, + int index, + cose_errback* perr); +bool COSE_Encrypt0_CounterSign_validate(HCOSE_ENCRYPT hSigner, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr); -/* +HCOSE_COUNTERSIGN COSE_Enveloped_add_countersignature(HCOSE_ENVELOPED hSignMsg, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr); +HCOSE_COUNTERSIGN COSE_Enveloped_get_countersignature(HCOSE_ENVELOPED hSignMsg, + int index, + cose_errback* perr); +bool COSE_Enveloped_CounterSign_validate(HCOSE_ENVELOPED hSigner, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr); + +HCOSE_COUNTERSIGN COSE_Recipient_add_countersignature(HCOSE_RECIPIENT hSignMsg, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr); +HCOSE_COUNTERSIGN COSE_Recipient_get_countersignature(HCOSE_RECIPIENT hSignMsg, + int index, + cose_errback* perr); +bool COSE_Recipient_CounterSign_validate(HCOSE_RECIPIENT hSigner, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr); + + /* */ cn_cbor* cn_cbor_clone(const cn_cbor* pIn, diff --git a/src/Cose.c b/src/Cose.c index 4771d56b..776922cd 100644 --- a/src/Cose.c +++ b/src/Cose.c @@ -562,10 +562,17 @@ extern COSE *CountersignRoot; extern COSE *SignerRoot; extern COSE *SignRoot; extern COSE *Sign1Root; +extern COSE *EncryptRoot; +extern COSE *EnvelopedRoot; +extern COSE *RecipientRoot; +extern COSE *MacRoot; +extern COSE *Mac0Root; bool AreListsEmpty() { - return CountersignRoot == NULL && SignerRoot == NULL && SignRoot == NULL && Sign1Root == NULL; + return CountersignRoot == NULL && SignerRoot == NULL && SignRoot == NULL && + Sign1Root == NULL && EncryptRoot == NULL && EnvelopedRoot == NULL && + RecipientRoot == NULL && MacRoot == NULL && Mac0Root == NULL; } #endif diff --git a/src/CounterSign.c b/src/CounterSign.c index a655e84b..59ae9e42 100644 --- a/src/CounterSign.c +++ b/src/CounterSign.c @@ -341,7 +341,7 @@ bool _COSE_CounterSign_Sign(COSE * baseMessage, CBOR_CONTEXT_COMMA cose_errback return fRet; } - +#ifdef INCLUDE_SIGN /*************************************************************************************************** * * SIGNER @@ -480,7 +480,9 @@ bool COSE_Sign_CounterSign_validate(HCOSE_SIGN hSignMsg, errorReturn: return false; } +#endif +#ifdef INCLUDE_SIGN1 /*************************************************************************************************** * * SIGN1 MESSAGE @@ -548,6 +550,214 @@ bool COSE_Sign1_CounterSign_validate(HCOSE_SIGN1 hSignMsg, errorReturn: return false; } +#endif + +#ifdef INCLUDE_ENCRYPT +/*************************************************************************************************** + * + * ENVELOPED MESSAGE + */ +HCOSE_COUNTERSIGN COSE_Enveloped_add_countersignature(HCOSE_ENVELOPED hSignMsg, + HCOSE_COUNTERSIGN hCountersign, + cose_errback* perr) +{ + CHECK_CONDITION(IsValidEnvelopedHandle(hSignMsg), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION( + IsValidCounterSignHandle(hCountersign), COSE_ERR_INVALID_HANDLE); + + if (!_COSE_CounterSign_add( + &((COSE_Enveloped*)hSignMsg)->m_message, hCountersign, perr)) { + goto errorReturn; + } + + return hCountersign; + +errorReturn: + return NULL; +} + +HCOSE_COUNTERSIGN COSE_Enveloped_get_countersignature(HCOSE_ENVELOPED hSignMsg, + int index, + cose_errback* perr) +{ + COSE_CounterSign* p = NULL; + + CHECK_CONDITION(IsValidEnvelopedHandle(hSignMsg), COSE_ERR_INVALID_HANDLE); + + p = _COSE_Message_get_countersignature( + &((COSE_Enveloped*)hSignMsg)->m_message, index, perr); + +errorReturn: + return (HCOSE_COUNTERSIGN)p; +} + +bool COSE_Enveloped_CounterSign_validate(HCOSE_ENVELOPED hSignMsg, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr) +{ + CHECK_CONDITION(IsValidEnvelopedHandle(hSignMsg), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION( + IsValidCounterSignHandle(hCountersignature), COSE_ERR_INVALID_HANDLE); + + COSE_Enveloped* pSignMsg = (COSE_Enveloped*)hSignMsg; + COSE_CounterSign* pCountersign = (COSE_CounterSign*)hCountersignature; + + const cn_cbor* cnContent = + _COSE_arrayget_int(&pSignMsg->m_message, INDEX_BODY); + CHECK_CONDITION(cnContent != NULL && cnContent->type == CN_CBOR_BYTES, + COSE_ERR_INVALID_PARAMETER); + + const cn_cbor* cnProtected = + _COSE_arrayget_int(&pSignMsg->m_message, INDEX_PROTECTED); + CHECK_CONDITION(cnProtected != NULL && cnProtected->type == CN_CBOR_BYTES, + COSE_ERR_INVALID_PARAMETER); + + bool f = _COSE_Signer_validate(&pCountersign->m_signer, cnContent, + cnProtected, "CounterSignature", perr); + + return f; + +errorReturn: + return false; +} + +/*************************************************************************************************** + * + * RECIPIENT MESSAGE + */ +HCOSE_COUNTERSIGN COSE_Recipient_add_countersignature(HCOSE_RECIPIENT hSignMsg, + HCOSE_COUNTERSIGN hCountersign, + cose_errback* perr) +{ + CHECK_CONDITION(IsValidRecipientHandle(hSignMsg), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION( + IsValidCounterSignHandle(hCountersign), COSE_ERR_INVALID_HANDLE); + + if (!_COSE_CounterSign_add( + &((COSE_RecipientInfo*)hSignMsg)->m_encrypt.m_message, hCountersign, perr)) { + goto errorReturn; + } + + return hCountersign; + +errorReturn: + return NULL; +} + +HCOSE_COUNTERSIGN COSE_Recipient_get_countersignature(HCOSE_RECIPIENT hSignMsg, + int index, + cose_errback* perr) +{ + COSE_CounterSign* p = NULL; + + CHECK_CONDITION(IsValidRecipientHandle(hSignMsg), COSE_ERR_INVALID_HANDLE); + + p = _COSE_Message_get_countersignature( + &((COSE_RecipientInfo*)hSignMsg)->m_encrypt.m_message, index, perr); + +errorReturn: + return (HCOSE_COUNTERSIGN)p; +} + +bool COSE_Recipient_CounterSign_validate(HCOSE_RECIPIENT hSignMsg, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr) +{ + CHECK_CONDITION(IsValidRecipientHandle(hSignMsg), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION( + IsValidCounterSignHandle(hCountersignature), COSE_ERR_INVALID_HANDLE); + + COSE_RecipientInfo* pSignMsg = (COSE_RecipientInfo*)hSignMsg; + COSE_CounterSign* pCountersign = (COSE_CounterSign*)hCountersignature; + + const cn_cbor* cnContent = + _COSE_arrayget_int(&pSignMsg->m_encrypt.m_message, INDEX_BODY); + CHECK_CONDITION(cnContent != NULL && cnContent->type == CN_CBOR_BYTES, + COSE_ERR_INVALID_PARAMETER); + + const cn_cbor* cnProtected = + _COSE_arrayget_int(&pSignMsg->m_encrypt.m_message, INDEX_PROTECTED); + CHECK_CONDITION(cnProtected != NULL && cnProtected->type == CN_CBOR_BYTES, + COSE_ERR_INVALID_PARAMETER); + + bool f = _COSE_Signer_validate(&pCountersign->m_signer, cnContent, + cnProtected, "CounterSignature", perr); + + return f; + +errorReturn: + return false; +} +#endif + +#ifdef INCLUDE_ENCRYPT0 +/*************************************************************************************************** + * + * ENCRYPT0 MESSAGE + */ +HCOSE_COUNTERSIGN COSE_Encrypt0_add_countersignature(HCOSE_ENCRYPT hSignMsg, + HCOSE_COUNTERSIGN hCountersign, + cose_errback* perr) +{ + CHECK_CONDITION(IsValidEncryptHandle(hSignMsg), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION( + IsValidCounterSignHandle(hCountersign), COSE_ERR_INVALID_HANDLE); + + if (!_COSE_CounterSign_add( + &((COSE_SignMessage*)hSignMsg)->m_message, hCountersign, perr)) { + goto errorReturn; + } + + return hCountersign; +errorReturn: + return NULL; +} + +HCOSE_COUNTERSIGN COSE_Encrypt0_get_countersignature(HCOSE_ENCRYPT hSignMsg, + int index, + cose_errback* perr) +{ + COSE_CounterSign* p = NULL; + + CHECK_CONDITION(IsValidEncryptHandle(hSignMsg), COSE_ERR_INVALID_HANDLE); + + p = _COSE_Message_get_countersignature( + &((COSE_SignMessage*)hSignMsg)->m_message, index, perr); + +errorReturn: + return (HCOSE_COUNTERSIGN)p; +} + +bool COSE_Encrypt0_CounterSign_validate(HCOSE_ENCRYPT hSignMsg, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr) +{ + CHECK_CONDITION(IsValidEncryptHandle(hSignMsg), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION( + IsValidCounterSignHandle(hCountersignature), COSE_ERR_INVALID_HANDLE); + + COSE_Encrypt* pSignMsg = (COSE_Encrypt*)hSignMsg; + COSE_CounterSign* pCountersign = (COSE_CounterSign*)hCountersignature; + + const cn_cbor* cnContent = + _COSE_arrayget_int(&pSignMsg->m_message, INDEX_BODY); + CHECK_CONDITION(cnContent != NULL && cnContent->type == CN_CBOR_BYTES, + COSE_ERR_INVALID_PARAMETER); + + const cn_cbor* cnProtected = + _COSE_arrayget_int(&pSignMsg->m_message, INDEX_PROTECTED); + CHECK_CONDITION(cnProtected != NULL && cnProtected->type == CN_CBOR_BYTES, + COSE_ERR_INVALID_PARAMETER); + + bool f = _COSE_Signer_validate(&pCountersign->m_signer, cnContent, + cnProtected, "CounterSignature", perr); + + return f; + +errorReturn: + return false; +} +#endif #endif diff --git a/src/Encrypt.c b/src/Encrypt.c index 5192e663..e08a82d4 100644 --- a/src/Encrypt.c +++ b/src/Encrypt.c @@ -20,7 +20,7 @@ #if INCLUDE_ENCRYPT || INCLUDE_MAC void _COSE_Enveloped_Release(COSE_Enveloped *p); -static COSE *EnvelopedRoot = NULL; +COSE *EnvelopedRoot = NULL; #endif #if INCLUDE_ENCRYPT @@ -654,16 +654,6 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, if (cbProtected == NULL) goto errorReturn; -#ifdef INCLUDE_COUNTERSIGNATURE - /* - // Setup Counter Signatures - if (!_COSE_CountSign_create( - &pcose->m_message, NULL, CBOR_CONTEXT_PARAM_COMMA perr)) { - goto errorReturn; - } - */ -#endif - // Build authenticated data size_t cbAuthData = 0; @@ -772,6 +762,15 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, } #endif // INCLUDE_ENCRYPT +#ifdef INCLUDE_COUNTERSIGNATURE + if (pcose->m_message.m_counterSigners != NULL) { + if (!_COSE_CounterSign_Sign( + &pcose->m_message, CBOR_CONTEXT_PARAM_COMMA perr)) { + goto errorReturn; + } + } +#endif + // Figure out the clean up fRet = true; diff --git a/src/Encrypt0.c b/src/Encrypt0.c index fa0885ab..178a495a 100644 --- a/src/Encrypt0.c +++ b/src/Encrypt0.c @@ -18,7 +18,7 @@ #if INCLUDE_ENCRYPT0 || INCLUDE_MAC0 void _COSE_Encrypt_Release(COSE_Encrypt *p); -static COSE *EncryptRoot = NULL; +COSE *EncryptRoot = NULL; #endif #if INCLUDE_ENCRYPT0 diff --git a/src/MacMessage.c b/src/MacMessage.c index a051be5e..75a87271 100644 --- a/src/MacMessage.c +++ b/src/MacMessage.c @@ -17,7 +17,7 @@ #if INCLUDE_MAC -static COSE *MacRoot = NULL; +COSE *MacRoot = NULL; /*! \private * @brief Test if a HCOSE_MAC handle is valid diff --git a/src/MacMessage0.c b/src/MacMessage0.c index 1cc12938..2dde20f7 100644 --- a/src/MacMessage0.c +++ b/src/MacMessage0.c @@ -18,7 +18,7 @@ #if INCLUDE_MAC0 -static COSE *Mac0Root = NULL; +COSE *Mac0Root = NULL; /*! \private * @brief Test if a HCOSE_MAC0 handle is valid diff --git a/src/Recipient.c b/src/Recipient.c index 724e7112..33eda6c0 100644 --- a/src/Recipient.c +++ b/src/Recipient.c @@ -18,7 +18,7 @@ static bool BuildContextBytes(COSE *pcose, #endif #if INCLUDE_ENCRYPT || INCLUDE_MAC -static COSE *RecipientRoot = NULL; +COSE *RecipientRoot = NULL; /*! \private * @brief Test if a HCOSE_RECIPIENT handle is valid diff --git a/test/encrypt.c b/test/encrypt.c index d845144f..7784e6e2 100644 --- a/test/encrypt.c +++ b/test/encrypt.c @@ -15,6 +15,7 @@ #include "json.h" #include "test.h" #include "context.h" +#include "cose_int.h" #ifdef _MSC_VER #pragma warning(disable : 4127) @@ -552,6 +553,68 @@ int _ValidateEncrypt(const cn_cbor *pControl, fFail = true; } +#if INCLUDE_COUNTERSIGNATURE + // Countersign on Encrypt0 Body + + // Validate counter signatures on signers + cn_cbor *countersignList = cn_cbor_mapget_string(pEncrypt, "countersign"); + if (countersignList != NULL) { + cn_cbor *countersigners = + cn_cbor_mapget_string(countersignList, "signers"); + if (countersigners == NULL) { + fFail = true; + goto exitHere; + } + int count = countersigners->length; + bool forward = true; + + if (COSE_Encrypt_map_get_int(hEnc, COSE_Header_CounterSign, + COSE_UNPROTECT_ONLY, 0) == NULL) { + goto returnError; + } + + for (int counterNo = 0; counterNo < count; counterNo++) { + HCOSE_COUNTERSIGN h = + COSE_Encrypt0_get_countersignature(hEnc, counterNo, 0); + if (h == NULL) { + fFail = true; + goto exitHere; + } + + cn_cbor *counterSigner = cn_cbor_index( + countersigners, forward ? counterNo : count - counterNo - 1); + + cn_cbor *pkeyCountersign = + BuildKey(cn_cbor_mapget_string(counterSigner, "key"), false); + if (pkeyCountersign == NULL) { + fFail = true; + goto exitHere; + } + + if (!COSE_CounterSign_SetKey(h, pkeyCountersign, 0)) { + fFail = true; + goto exitHere; + } + + if (COSE_Encrypt0_CounterSign_validate(hEnc, h, 0)) { + // I don't think we have any forced errors yet. + } + else { + if (forward && counterNo == 0 && count > 1) { + forward = false; + counterNo -= 1; + } + else { + fFail = true; + } + } + + CN_CBOR_FREE(pkeyCountersign, context); + COSE_CounterSign_Free(h); + } + } +#endif + COSE_Encrypt_Free(hEnc); exitHere: @@ -637,6 +700,45 @@ int BuildEncryptMessage(const cn_cbor *pControl) cn_cbor *k = cn_cbor_mapget_int(pkey, -1); +#ifdef INCLUDE_COUNTERSIGNATURE + // On the Encrypt0 body + cn_cbor *countersigns = cn_cbor_mapget_string(pEncrypt, "countersign"); + if (countersigns != NULL) { + countersigns = cn_cbor_mapget_string(countersigns, "signers"); + cn_cbor *countersign = countersigns->first_child; + + for (; countersign != NULL; countersign = countersign->next) { + cn_cbor *pkeyCountersign = + BuildKey(cn_cbor_mapget_string(countersign, "key"), false); + if (pkeyCountersign == NULL) { + goto returnError; + } + + HCOSE_COUNTERSIGN hCountersign = + COSE_CounterSign_Init(CBOR_CONTEXT_PARAM_COMMA NULL); + if (hCountersign == NULL) { + goto returnError; + } + + if (!SetSendingAttributes((HCOSE)hCountersign, countersign, + Attributes_Countersign_protected)) { + goto returnError; + } + + if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) { + goto returnError; + } + + if (!COSE_Encrypt0_add_countersignature(hEncObj, hCountersign, NULL)) { + goto returnError; + } + + COSE_CounterSign_Free(hCountersign); + } + } + +#endif + if (!COSE_Encrypt_encrypt(hEncObj, k->v.bytes, k->length, NULL)) goto returnError; From 88bf9f5d2b22c858cff60ec35cf37e21c7268470 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Sun, 26 Apr 2020 19:15:36 -0700 Subject: [PATCH 11/38] Add support for Enveloped THere is an error in enveloped-03 example. Needs to get fixed. --- src/Cose.c | 24 +++--- src/openssl.c | 2 + test/encrypt.c | 225 ++++++++++++++++++++++++++++++++++++++++++++++--- test/sign.c | 82 +++++++++--------- 4 files changed, 269 insertions(+), 64 deletions(-) diff --git a/src/Cose.c b/src/Cose.c index 776922cd..a3f737e6 100644 --- a/src/Cose.c +++ b/src/Cose.c @@ -508,28 +508,28 @@ cose_error _MapFromCBOR(cn_cbor_errback err) } } -void _COSE_InsertInList(COSE **root, COSE *newMsg) +void _COSE_InsertInList(COSE **rootNode, COSE *newMsg) { - if (*root == NULL) { - *root = newMsg; + if (*rootNode == NULL) { + *rootNode = newMsg; return; } - newMsg->m_handleList = *root; - *root = newMsg; + newMsg->m_handleList = *rootNode; + *rootNode = newMsg; return; } -bool _COSE_IsInList(const COSE *const root, const COSE *const thisMsg) +bool _COSE_IsInList(const COSE *const rootNode, const COSE *const thisMsg) { - if (root == NULL) { + if (rootNode == NULL) { return false; } if (thisMsg == NULL) { return false; } - for (const COSE *walk = root; walk != NULL; walk = walk->m_handleList) { + for (const COSE *walk = rootNode; walk != NULL; walk = walk->m_handleList) { if (walk == thisMsg) { return true; } @@ -537,17 +537,17 @@ bool _COSE_IsInList(const COSE *const root, const COSE *const thisMsg) return false; } -void _COSE_RemoveFromList(COSE **root, COSE *thisMsg) +void _COSE_RemoveFromList(COSE **rootNode, COSE *thisMsg) { COSE *walk; - if (*root == thisMsg) { - *root = thisMsg->m_handleList; + if (*rootNode == thisMsg) { + *rootNode = thisMsg->m_handleList; thisMsg->m_handleList = NULL; return; } - for (walk = *root; walk->m_handleList != NULL; walk = walk->m_handleList) { + for (walk = *rootNode; walk->m_handleList != NULL; walk = walk->m_handleList) { if (walk->m_handleList == thisMsg) { walk->m_handleList = thisMsg->m_handleList; thisMsg->m_handleList = NULL; diff --git a/src/openssl.c b/src/openssl.c index b3975937..c0a8fc9d 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -796,7 +796,9 @@ bool HKDF_Extract(COSE *pcose, size_t *pcbDigest, CBOR_CONTEXT_COMMA cose_errback *perr) { +#ifdef USE_CBOR_CONTEXT UNUSED(context); +#endif byte rgbSalt[EVP_MAX_MD_SIZE] = {0}; int cbSalt; cn_cbor *cnSalt; diff --git a/test/encrypt.c b/test/encrypt.c index 7784e6e2..a900f1b6 100644 --- a/test/encrypt.c +++ b/test/encrypt.c @@ -44,8 +44,9 @@ bool DecryptMessage(const byte *pbEncoded, hEnc = (HCOSE_ENVELOPED)COSE_Decode(pbEncoded, cbEncoded, &type, COSE_enveloped_object, CBOR_CONTEXT_PARAM_COMMA & cose_err); if (hEnc == NULL) { - if (fFailBody && (cose_err.err == COSE_ERR_INVALID_PARAMETER)) + if (fFailBody && (cose_err.err == COSE_ERR_INVALID_PARAMETER)) { return true; + } goto errorReturn; } @@ -141,6 +142,134 @@ bool DecryptMessage(const byte *pbEncoded, fRet = fFailBody; } +#if INCLUDE_COUNTERSIGNATURE + // Countersign on Encrypt0 Body + + // Validate counter signatures on signers + cn_cbor *countersignList = + cn_cbor_mapget_string(pRecipient1, "countersign"); + if (countersignList != NULL) { + cn_cbor *countersigners = + cn_cbor_mapget_string(countersignList, "signers"); + if (countersigners == NULL) { + fRet = false; + goto errorReturn; + } + int count = countersigners->length; + bool forward = true; + + if (COSE_Recipient_map_get_int(hRecip1, COSE_Header_CounterSign, + COSE_UNPROTECT_ONLY, 0) == NULL) { + goto errorReturn; + } + + for (int counterNo = 0; counterNo < count; counterNo++) { + HCOSE_COUNTERSIGN h = + COSE_Recipient_get_countersignature(hRecip1, counterNo, 0); + if (h == NULL) { + fRet = false; + goto errorReturn; + } + + cn_cbor *counterSigner = cn_cbor_index( + countersigners, forward ? counterNo : count - counterNo - 1); + + cn_cbor *pkeyCountersign = + BuildKey(cn_cbor_mapget_string(counterSigner, "key"), false); + if (pkeyCountersign == NULL) { + fRet = false; + goto errorReturn; + } + + if (!COSE_CounterSign_SetKey(h, pkeyCountersign, 0)) { + fRet = false; + goto errorReturn; + } + + if (COSE_Recipient_CounterSign_validate(hRecip1, h, 0)) { + // I don't think we have any forced errors yet. + } + else { + if (forward && counterNo == 0 && count > 1) { + forward = false; + counterNo -= 1; + } + else { + fRet = false; + } + } + + CN_CBOR_FREE(pkeyCountersign, context); + COSE_CounterSign_Free(h); + } + } +#endif + + + #if INCLUDE_COUNTERSIGNATURE + // Countersign on Enveloped Body + + // Validate counter signatures on signers + countersignList = cn_cbor_mapget_string(pEnveloped, "countersign"); + if (countersignList != NULL) { + cn_cbor *countersigners = + cn_cbor_mapget_string(countersignList, "signers"); + if (countersigners == NULL) { + fRet = false; + goto errorReturn; + } + int count = countersigners->length; + bool forward = true; + + if (COSE_Enveloped_map_get_int(hEnc, COSE_Header_CounterSign, + COSE_UNPROTECT_ONLY, 0) == NULL) { + fRet = false; + goto errorReturn; + } + + for (int counterNo = 0; counterNo < count; counterNo++) { + HCOSE_COUNTERSIGN h = + COSE_Enveloped_get_countersignature(hEnc, counterNo, 0); + if (h == NULL) { + fRet = false; + goto errorReturn; + } + + cn_cbor *counterSigner = cn_cbor_index( + countersigners, forward ? counterNo : count - counterNo - 1); + + cn_cbor *pkeyCountersign = + BuildKey(cn_cbor_mapget_string(counterSigner, "key"), false); + if (pkeyCountersign == NULL) { + fRet = false; + goto errorReturn; + } + + if (!COSE_CounterSign_SetKey(h, pkeyCountersign, 0)) { + fRet = false; + goto errorReturn; + } + + if (COSE_Enveloped_CounterSign_validate(hEnc, h, 0)) { + // I don't think we have any forced errors yet. + } + else { + if (forward && counterNo == 0 && count > 1) { + forward = false; + counterNo -= 1; + } + else { + fRet = false; + } + } + + CN_CBOR_FREE(pkeyCountersign, context); + COSE_CounterSign_Free(h); + } + } +#endif + + if (!fRet && !fNoSupport) CFails++; @@ -257,6 +386,46 @@ HCOSE_RECIPIENT BuildRecipient(const cn_cbor *pRecipient) goto returnError; } + #ifdef INCLUDE_COUNTERSIGNATURE + // On the Recipient + cn_cbor *countersigns1 = cn_cbor_mapget_string(pRecipient, "countersign"); + if (countersigns1 != NULL) { + countersigns1 = cn_cbor_mapget_string(countersigns1, "signers"); + cn_cbor *countersign = countersigns1->first_child; + + for (; countersign != NULL; countersign = countersign->next) { + cn_cbor *pkeyCountersign = + BuildKey(cn_cbor_mapget_string(countersign, "key"), false); + if (pkeyCountersign == NULL) { + goto returnError; + } + + HCOSE_COUNTERSIGN hCountersign = + COSE_CounterSign_Init(CBOR_CONTEXT_PARAM_COMMA NULL); + if (hCountersign == NULL) { + goto returnError; + } + + if (!SetSendingAttributes((HCOSE)hCountersign, countersign, + Attributes_Countersign_protected)) { + goto returnError; + } + + if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) { + goto returnError; + } + + if (!COSE_Recipient_add_countersignature( + hRecip, hCountersign, NULL)) { + goto returnError; + } + + COSE_CounterSign_Free(hCountersign); + } + } + +#endif + return hRecip; returnError: @@ -295,15 +464,6 @@ int BuildEnvelopedMessage(const cn_cbor *pControl) (HCOSE)hEncObj, pEnveloped, Attributes_Enveloped_protected)) goto returnError; -#if 0 - const cn_cbor * pCounterSign = cn_cbor_mapget_string(pEnveloped, "countersign"); - if (pCounterSign != NULL) { - HCOSE_COUNTERSIGN hCSign = BuildCounterSign(pCounterSign); - if (hCSign == NULL) goto returnError; - if (!COSE_Enveloped_AddCounterSigner(hEncObj, hCSign, NULL)) goto returnError; - } -#endif - const cn_cbor *pAlg = COSE_Enveloped_map_get_int(hEncObj, 1, COSE_BOTH, NULL); if (pAlg == NULL) @@ -323,12 +483,53 @@ int BuildEnvelopedMessage(const cn_cbor *pControl) if (!COSE_Enveloped_AddRecipient(hEncObj, hRecip, NULL)) goto returnError; - + COSE_Recipient_Free(hRecip); } - if (!COSE_Enveloped_encrypt(hEncObj, NULL)) +#ifdef INCLUDE_COUNTERSIGNATURE + // On the Evneloped body + cn_cbor *countersigns1 = cn_cbor_mapget_string(pEnveloped, "countersign"); + if (countersigns1 != NULL) { + countersigns1 = cn_cbor_mapget_string(countersigns1, "signers"); + cn_cbor *countersign = countersigns1->first_child; + + for (; countersign != NULL; countersign = countersign->next) { + cn_cbor *pkeyCountersign = + BuildKey(cn_cbor_mapget_string(countersign, "key"), false); + if (pkeyCountersign == NULL) { + goto returnError; + } + + HCOSE_COUNTERSIGN hCountersign = + COSE_CounterSign_Init(CBOR_CONTEXT_PARAM_COMMA NULL); + if (hCountersign == NULL) { + goto returnError; + } + + if (!SetSendingAttributes((HCOSE)hCountersign, countersign, + Attributes_Countersign_protected)) { + goto returnError; + } + + if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) { + goto returnError; + } + + if (!COSE_Enveloped_add_countersignature( + hEncObj, hCountersign, NULL)) { + goto returnError; + } + + COSE_CounterSign_Free(hCountersign); + } + } + +#endif + + if (!COSE_Enveloped_encrypt(hEncObj, NULL)) { goto returnError; + } size_t cb = COSE_Encode((HCOSE)hEncObj, NULL, 0, 0) + 1; byte *rgb = (byte *)malloc(cb); diff --git a/test/sign.c b/test/sign.c index 8f87333d..796d9e18 100644 --- a/test/sign.c +++ b/test/sign.c @@ -316,41 +316,6 @@ int BuildSignedMessage(const cn_cbor *pControl) #if INCLUDE_COUNTERSIGNATURE // On the signer object cn_cbor *countersigns = cn_cbor_mapget_string(pSigners, "countersign"); - if (countersigns != NULL) { - countersigns = cn_cbor_mapget_string(countersigns, "signers"); - cn_cbor* countersign = countersigns->first_child; - - for (; countersign != NULL; countersign = countersign->next) { - cn_cbor* pkeyCountersign = BuildKey(cn_cbor_mapget_string(countersign, "key"), false); - if (pkeyCountersign == NULL) { - goto returnError; - } - - HCOSE_COUNTERSIGN hCountersign = COSE_CounterSign_Init(CBOR_CONTEXT_PARAM_COMMA NULL); - if (hCountersign == NULL) { - goto returnError; - } - - if (!SetSendingAttributes((HCOSE)hCountersign, countersign, Attributes_Countersign_protected)) { - goto returnError; - } - - if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) { - goto returnError; - } - - if (!COSE_Signer_add_countersignature(hSigner, hCountersign, NULL)) { - goto returnError; - } - - COSE_CounterSign_Free(hCountersign); - } - } -#endif - -#ifdef INCLUDE_COUNTERSIGNATURE - // On the sign body - countersigns = cn_cbor_mapget_string(pSign, "countersign"); if (countersigns != NULL) { countersigns = cn_cbor_mapget_string(countersigns, "signers"); cn_cbor *countersign = countersigns->first_child; @@ -378,22 +343,59 @@ int BuildSignedMessage(const cn_cbor *pControl) goto returnError; } - if (!COSE_Sign_add_countersignature( - hSignObj, hCountersign, NULL)) { + if (!COSE_Signer_add_countersignature( + hSigner, hCountersign, NULL)) { goto returnError; } COSE_CounterSign_Free(hCountersign); } } - #endif - COSE_Signer_Free(hSigner); } +#ifdef INCLUDE_COUNTERSIGNATURE + // On the sign body + cn_cbor * countersigns1 = cn_cbor_mapget_string(pSign, "countersign"); + if (countersigns1 != NULL) { + countersigns1 = cn_cbor_mapget_string(countersigns1, "signers"); + cn_cbor *countersign = countersigns1->first_child; - if (!COSE_Sign_Sign(hSignObj, NULL)) + for (; countersign != NULL; countersign = countersign->next) { + cn_cbor *pkeyCountersign = + BuildKey(cn_cbor_mapget_string(countersign, "key"), false); + if (pkeyCountersign == NULL) { + goto returnError; + } + + HCOSE_COUNTERSIGN hCountersign = + COSE_CounterSign_Init(CBOR_CONTEXT_PARAM_COMMA NULL); + if (hCountersign == NULL) { + goto returnError; + } + + if (!SetSendingAttributes((HCOSE)hCountersign, countersign, + Attributes_Countersign_protected)) { + goto returnError; + } + + if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) { + goto returnError; + } + + if (!COSE_Sign_add_countersignature(hSignObj, hCountersign, NULL)) { + goto returnError; + } + + COSE_CounterSign_Free(hCountersign); + } + } + +#endif + + if (!COSE_Sign_Sign(hSignObj, NULL)) { goto returnError; + } size_t cb = COSE_Encode((HCOSE)hSignObj, NULL, 0, 0) + 1; byte *rgb = (byte *)malloc(cb); From 788518cd7e43bdee13fcaa7ac2cc7ec895dcfdff Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Sun, 26 Apr 2020 20:31:46 -0700 Subject: [PATCH 12/38] Add MAC0 Countersignature code. --- include/cose/cose.h | 20 +++ src/CMakeLists.txt | 2 +- src/Cose.c | 67 +++---- src/CounterSign.c | 141 +++++++++++++++ src/MacMessage.c | 6 +- src/cose_int.h | 10 +- test/mac_test.c | 430 ++++++++++++++++++++++++++++++++------------ test/sign.c | 20 ++- test/test.c | 14 ++ 9 files changed, 552 insertions(+), 158 deletions(-) diff --git a/include/cose/cose.h b/include/cose/cose.h index b3eb7d13..bb35c243 100644 --- a/include/cose/cose.h +++ b/include/cose/cose.h @@ -618,6 +618,26 @@ bool COSE_Recipient_CounterSign_validate(HCOSE_RECIPIENT hSigner, HCOSE_COUNTERSIGN hCountersignature, cose_errback* perr); +HCOSE_COUNTERSIGN COSE_Mac0_add_countersignature(HCOSE_MAC0 hSignMsg, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr); +HCOSE_COUNTERSIGN COSE_Mac0_get_countersignature(HCOSE_MAC0 hSignMsg, + int index, + cose_errback* perr); +bool COSE_Mac0_CounterSign_validate(HCOSE_MAC0 hSigner, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr); + +HCOSE_COUNTERSIGN COSE_Mac_add_countersignature(HCOSE_MAC hSignMsg, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr); +HCOSE_COUNTERSIGN COSE_Mac_get_countersignature(HCOSE_MAC hSignMsg, + int index, + cose_errback* perr); +bool COSE_Mac_CounterSign_validate(HCOSE_MAC hSigner, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr); + /* */ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5957f32a..1314e10d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,8 +6,8 @@ if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang") endif() if(COSE_C_OPTIMIZE) add_definitions(-Os) + add_definitions(-DNDEBUG) endif() - add_definitions(-DNDEBUG) elseif(MSVC) add_definitions(/W4) if(COSE_C_FATAL_WARNINGS) diff --git a/src/Cose.c b/src/Cose.c index a3f737e6..96ed7cda 100644 --- a/src/Cose.c +++ b/src/Cose.c @@ -17,7 +17,7 @@ bool IsValidCOSEHandle(HCOSE h) } bool _COSE_Init(COSE_INIT_FLAGS flags, - COSE *pobj, + COSE *pcose, int msgType, CBOR_CONTEXT_COMMA cose_errback *perr) { @@ -26,48 +26,48 @@ bool _COSE_Init(COSE_INIT_FLAGS flags, #ifdef USE_CBOR_CONTEXT if (context != NULL) - pobj->m_allocContext = *context; + pcose->m_allocContext = *context; #endif CHECK_CONDITION((flags & ~(COSE_INIT_FLAGS_DETACHED_CONTENT | COSE_INIT_FLAGS_NO_CBOR_TAG)) == 0, COSE_ERR_INVALID_PARAMETER); - pobj->m_flags = flags; + pcose->m_flags = flags; - pobj->m_protectedMap = + pcose->m_protectedMap = cn_cbor_map_create(CBOR_CONTEXT_PARAM_COMMA & errState); - CHECK_CONDITION_CBOR(pobj->m_protectedMap != NULL, errState); + CHECK_CONDITION_CBOR(pcose->m_protectedMap != NULL, errState); - pobj->m_dontSendMap = + pcose->m_dontSendMap = cn_cbor_map_create(CBOR_CONTEXT_PARAM_COMMA & errState); - CHECK_CONDITION_CBOR(pobj->m_dontSendMap != NULL, errState); + CHECK_CONDITION_CBOR(pcose->m_dontSendMap != NULL, errState); - pobj->m_cborRoot = pobj->m_cbor = + pcose->m_cborRoot = pcose->m_cbor = cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA & errState); - CHECK_CONDITION_CBOR(pobj->m_cbor != NULL, errState); - pobj->m_ownMsg = 1; + CHECK_CONDITION_CBOR(pcose->m_cbor != NULL, errState); + pcose->m_ownMsg = 1; - pobj->m_msgType = msgType; + pcose->m_msgType = msgType; - pobj->m_unprotectMap = + pcose->m_unprotectMap = cn_cbor_map_create(CBOR_CONTEXT_PARAM_COMMA & errState); - CHECK_CONDITION_CBOR(pobj->m_unprotectMap != NULL, errState); + CHECK_CONDITION_CBOR(pcose->m_unprotectMap != NULL, errState); CHECK_CONDITION_CBOR( - _COSE_array_replace(pobj, pobj->m_unprotectMap, INDEX_UNPROTECTED, + _COSE_array_replace(pcose, pcose->m_unprotectMap, INDEX_UNPROTECTED, CBOR_CONTEXT_PARAM_COMMA & errState), errState); - pobj->m_ownUnprotectedMap = false; + pcose->m_ownUnprotectedMap = false; if (!(flags & COSE_INIT_FLAGS_NO_CBOR_TAG)) { cn_cbor_errback cbor_error; cn_cbor *cn = cn_cbor_tag_create( - msgType, pobj->m_cborRoot, CBOR_CONTEXT_PARAM_COMMA & cbor_error); + msgType, pcose->m_cborRoot, CBOR_CONTEXT_PARAM_COMMA & cbor_error); CHECK_CONDITION_CBOR(cn != NULL, cbor_error); - pobj->m_cborRoot = cn; + pcose->m_cborRoot = cn; } - pobj->m_refCount = 1; + pcose->m_refCount = 1; return true; @@ -155,24 +155,27 @@ bool _COSE_Init_From_Object(COSE *pobj, return false; } -void _COSE_Release(COSE *pobj) +void _COSE_Release(COSE *pcose) { #ifdef USE_CBOR_CONTEXT - cn_cbor_context *context = &pobj->m_allocContext; + cn_cbor_context *context = &pcose->m_allocContext; #endif - if (pobj->m_protectedMap != NULL) - CN_CBOR_FREE(pobj->m_protectedMap, context); - if (pobj->m_ownUnprotectedMap && (pobj->m_unprotectMap != NULL)) - CN_CBOR_FREE(pobj->m_unprotectMap, context); - if (pobj->m_dontSendMap != NULL) - CN_CBOR_FREE(pobj->m_dontSendMap, context); - if (pobj->m_ownMsg && (pobj->m_cborRoot != NULL) && - (pobj->m_cborRoot->parent == NULL)) - CN_CBOR_FREE(pobj->m_cborRoot, context); - - if (pobj->m_counterSigners != NULL) { - COSE_CounterSign* p = pobj->m_counterSigners; + if (pcose->m_protectedMap != NULL) { + CN_CBOR_FREE(pcose->m_protectedMap, context); + } + if (pcose->m_ownUnprotectedMap && (pcose->m_unprotectMap != NULL)) { + CN_CBOR_FREE(pcose->m_unprotectMap, context); + } + if (pcose->m_dontSendMap != NULL) { + CN_CBOR_FREE(pcose->m_dontSendMap, context); + } + if (pcose->m_ownMsg && (pcose->m_cborRoot != NULL) && + (pcose->m_cborRoot->parent == NULL)) { + CN_CBOR_FREE(pcose->m_cborRoot, context); + } + if (pcose->m_counterSigners != NULL) { + COSE_CounterSign* p = pcose->m_counterSigners; COSE_CounterSign* p2 = NULL; while (p != NULL) { diff --git a/src/CounterSign.c b/src/CounterSign.c index 59ae9e42..f769450e 100644 --- a/src/CounterSign.c +++ b/src/CounterSign.c @@ -759,5 +759,146 @@ bool COSE_Encrypt0_CounterSign_validate(HCOSE_ENCRYPT hSignMsg, return false; } #endif + +#ifdef INCLUDE_MAC0 +/*************************************************************************************************** + * + * MAC0 MESSAGE + */ +HCOSE_COUNTERSIGN COSE_Mac0_add_countersignature(HCOSE_MAC0 hSignMsg, + HCOSE_COUNTERSIGN hCountersign, + cose_errback* perr) +{ + CHECK_CONDITION(IsValidMac0Handle(hSignMsg), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION( + IsValidCounterSignHandle(hCountersign), COSE_ERR_INVALID_HANDLE); + + if (!_COSE_CounterSign_add( + &((COSE_SignMessage*)hSignMsg)->m_message, hCountersign, perr)) { + goto errorReturn; + } + + return hCountersign; + +errorReturn: + return NULL; +} + +HCOSE_COUNTERSIGN COSE_Mac0_get_countersignature(HCOSE_MAC0 hSignMsg, + int index, + cose_errback* perr) +{ + COSE_CounterSign* p = NULL; + + CHECK_CONDITION(IsValidMac0Handle(hSignMsg), COSE_ERR_INVALID_HANDLE); + + p = _COSE_Message_get_countersignature( + &((COSE_SignMessage*)hSignMsg)->m_message, index, perr); + +errorReturn: + return (HCOSE_COUNTERSIGN)p; +} + +bool COSE_Mac0_CounterSign_validate(HCOSE_MAC0 hSignMsg, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr) +{ + CHECK_CONDITION(IsValidMac0Handle(hSignMsg), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION( + IsValidCounterSignHandle(hCountersignature), COSE_ERR_INVALID_HANDLE); + + COSE_Mac0Message* pSignMsg = (COSE_Mac0Message*)hSignMsg; + COSE_CounterSign* pCountersign = (COSE_CounterSign*)hCountersignature; + + const cn_cbor* cnContent = + _COSE_arrayget_int(&pSignMsg->m_message, INDEX_BODY); + CHECK_CONDITION(cnContent != NULL && cnContent->type == CN_CBOR_BYTES, + COSE_ERR_INVALID_PARAMETER); + + const cn_cbor* cnProtected = + _COSE_arrayget_int(&pSignMsg->m_message, INDEX_PROTECTED); + CHECK_CONDITION(cnProtected != NULL && cnProtected->type == CN_CBOR_BYTES, + COSE_ERR_INVALID_PARAMETER); + + bool f = _COSE_Signer_validate(&pCountersign->m_signer, cnContent, + cnProtected, "CounterSignature", perr); + + return f; + +errorReturn: + return false; +} +#endif + +#ifdef INCLUDE_MAC +/*************************************************************************************************** + * + * ENCRYPT0 MESSAGE + */ +HCOSE_COUNTERSIGN COSE_Mac_add_countersignature(HCOSE_MAC hSignMsg, + HCOSE_COUNTERSIGN hCountersign, + cose_errback* perr) +{ + CHECK_CONDITION(IsValidMacHandle(hSignMsg), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION( + IsValidCounterSignHandle(hCountersign), COSE_ERR_INVALID_HANDLE); + + if (!_COSE_CounterSign_add( + &((COSE_MacMessage*)hSignMsg)->m_message, hCountersign, perr)) { + goto errorReturn; + } + + return hCountersign; + +errorReturn: + return NULL; +} + +HCOSE_COUNTERSIGN COSE_Mac_get_countersignature(HCOSE_MAC hSignMsg, + int index, + cose_errback* perr) +{ + COSE_CounterSign* p = NULL; + + CHECK_CONDITION(IsValidMacHandle(hSignMsg), COSE_ERR_INVALID_HANDLE); + + p = _COSE_Message_get_countersignature( + &((COSE_MacMessage*)hSignMsg)->m_message, index, perr); + +errorReturn: + return (HCOSE_COUNTERSIGN)p; +} + +bool COSE_Mac_CounterSign_validate(HCOSE_MAC hSignMsg, + HCOSE_COUNTERSIGN hCountersignature, + cose_errback* perr) +{ + CHECK_CONDITION(IsValidMacHandle(hSignMsg), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION( + IsValidCounterSignHandle(hCountersignature), COSE_ERR_INVALID_HANDLE); + + COSE_MacMessage* pSignMsg = (COSE_MacMessage*)hSignMsg; + COSE_CounterSign* pCountersign = (COSE_CounterSign*)hCountersignature; + + const cn_cbor* cnContent = + _COSE_arrayget_int(&pSignMsg->m_message, INDEX_BODY); + CHECK_CONDITION(cnContent != NULL && cnContent->type == CN_CBOR_BYTES, + COSE_ERR_INVALID_PARAMETER); + + const cn_cbor* cnProtected = + _COSE_arrayget_int(&pSignMsg->m_message, INDEX_PROTECTED); + CHECK_CONDITION(cnProtected != NULL && cnProtected->type == CN_CBOR_BYTES, + COSE_ERR_INVALID_PARAMETER); + + bool f = _COSE_Signer_validate(&pCountersign->m_signer, cnContent, + cnProtected, "CounterSignature", perr); + + return f; + +errorReturn: + return false; +} +#endif + #endif diff --git a/src/MacMessage.c b/src/MacMessage.c index 75a87271..ae212376 100644 --- a/src/MacMessage.c +++ b/src/MacMessage.c @@ -141,6 +141,7 @@ bool COSE_Mac_Free(HCOSE_MAC h) context = &((COSE_MacMessage *)h)->m_message.m_allocContext; #endif + _COSE_Mac_Release((COSE_MacMessage *)h); COSE_FREE((COSE_MacMessage *)h, context); @@ -156,7 +157,7 @@ bool _COSE_Mac_Release(COSE_MacMessage *p) for (pRecipient = p->m_recipientFirst; pRecipient != NULL; pRecipient = pRecipient2) { pRecipient2 = pRecipient->m_recipientNext; - _COSE_Recipient_Free(pRecipient); + COSE_Recipient_Free((HCOSE_RECIPIENT) pRecipient); } _COSE_Release(&p->m_message); @@ -896,8 +897,9 @@ HCOSE_RECIPIENT COSE_Mac_GetRecipient(HCOSE_MAC cose, CHECK_CONDITION(p != NULL, COSE_ERR_NO_RECIPIENT_FOUND); p = p->m_recipientNext; } - if (p != NULL) + if (p != NULL) { p->m_encrypt.m_message.m_refCount++; + } return (HCOSE_RECIPIENT)p; errorReturn: diff --git a/src/cose_int.h b/src/cose_int.h index a4103826..5070c626 100644 --- a/src/cose_int.h +++ b/src/cose_int.h @@ -180,21 +180,23 @@ bool IsValidSignHandle(HCOSE_SIGN h); bool IsValidSignerHandle(HCOSE_SIGNER h); bool IsValidSign1Handle(HCOSE_SIGN1 h); bool IsValidCounterSignHandle(HCOSE_COUNTERSIGN h); +bool IsValidMacHandle(HCOSE_MAC h); +bool IsValidMac0Handle(HCOSE_MAC0 h); bool _COSE_Init(COSE_INIT_FLAGS flags, COSE *pcose, int msgType, - CBOR_CONTEXT_COMMA cose_errback *errp); + CBOR_CONTEXT_COMMA cose_errback *perr); bool _COSE_Init_From_Object(COSE *pobj, cn_cbor *pcbor, - CBOR_CONTEXT_COMMA cose_errback *perror); + CBOR_CONTEXT_COMMA cose_errback *perr); void _COSE_Release(COSE *pcose); cn_cbor *_COSE_map_get_string(COSE *cose, const char *key, int flags, cose_errback *errp); -cn_cbor *_COSE_map_get_int(COSE *cose, int key, int flags, cose_errback *errp); +cn_cbor *_COSE_map_get_int(COSE *cose, int key, int flags, cose_errback *perr); bool _COSE_map_put(COSE *cose, int key, cn_cbor *value, @@ -415,8 +417,6 @@ bool cn_cbor_array_replace(cn_cbor *cb_array, cn_cbor *cb_value, int index, CBOR_CONTEXT_COMMA cn_cbor_errback *errp); -cn_cbor *cn_cbor_bool_create(int boolValue, - CBOR_CONTEXT_COMMA cn_cbor_errback *errp); size_t cn_cbor_encode_size(cn_cbor *object); diff --git a/test/mac_test.c b/test/mac_test.c index dcd63aad..90e87ff4 100644 --- a/test/mac_test.c +++ b/test/mac_test.c @@ -6,6 +6,7 @@ #include #include #include +#include "cose_int.h" #if INCLUDE_MAC && !INCLUDE_ENCRYPT0 #include #endif @@ -49,18 +50,22 @@ int _ValidateMAC(const cn_cbor *pControl, goto failTest; } - if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) + if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) { goto failTest; + } pMac = cn_cbor_mapget_string(pInput, "mac"); - if ((pMac == NULL) || (pMac->type != CN_CBOR_MAP)) + if ((pMac == NULL) || (pMac->type != CN_CBOR_MAP)) { goto failTest; + } - if (!SetReceivingAttributes((HCOSE)hMAC, pMac, Attributes_MAC_protected)) + if (!SetReceivingAttributes((HCOSE)hMAC, pMac, Attributes_MAC_protected)) { goto failTest; + } pRecipients = cn_cbor_mapget_string(pMac, "recipients"); - if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) + if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) { goto failTest; + } iRecipient = (int)pRecipients->length - 1; pRecipients = pRecipients->first_child; @@ -80,8 +85,9 @@ int _ValidateMAC(const cn_cbor *pControl, } if (!SetReceivingAttributes( - (HCOSE)hRecip, pRecipients, Attributes_Recipient_protected)) + (HCOSE)hRecip, pRecipients, Attributes_Recipient_protected)) { goto failTest; + } if (!COSE_Recipient_SetKey(hRecip, pkey, NULL)) { fFail = true; @@ -120,8 +126,9 @@ int _ValidateMAC(const cn_cbor *pControl, } else { if (fAlgNoSupport) { returnCode = 0; - } else if ((pFail == NULL) || (pFail->type == CN_CBOR_FALSE)) + } else if ((pFail == NULL) || (pFail->type == CN_CBOR_FALSE)) { fFail = true; + } } COSE_Recipient_Free(hRecip); @@ -130,14 +137,17 @@ int _ValidateMAC(const cn_cbor *pControl, COSE_Mac_Free(hMAC); if (fFailBody) { - if (!fFail) + if (!fFail) { fFail = true; - else + } + else { fFail = false; + } } - if (fFail) + if (fFail) { CFails += 1; + } return returnCode; failTest: @@ -162,65 +172,78 @@ int BuildMacMessage(const cn_cbor *pControl) // const cn_cbor *pFail = cn_cbor_mapget_string(pControl, "fail"); - if ((pFail != NULL) && (pFail->type == CN_CBOR_TRUE)) + if ((pFail != NULL) && (pFail->type == CN_CBOR_TRUE)) { return 0; + } HCOSE_MAC hMacObj = COSE_Mac_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); const cn_cbor *pInputs = cn_cbor_mapget_string(pControl, "input"); - if (pInputs == NULL) + if (pInputs == NULL) { goto returnError; + } const cn_cbor *pMac = cn_cbor_mapget_string(pInputs, "mac"); - if (pMac == NULL) + if (pMac == NULL) { goto returnError; + } const cn_cbor *pContent = cn_cbor_mapget_string(pInputs, "plaintext"); if (!COSE_Mac_SetContent( - hMacObj, pContent->v.bytes, pContent->length, NULL)) + hMacObj, pContent->v.bytes, pContent->length, NULL)) { goto returnError; + } - if (!SetSendingAttributes((HCOSE)hMacObj, pMac, Attributes_MAC_protected)) + if (!SetSendingAttributes((HCOSE)hMacObj, pMac, Attributes_MAC_protected)) { goto returnError; + } const cn_cbor *pRecipients = cn_cbor_mapget_string(pMac, "recipients"); - if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) + if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) { goto returnError; + } pRecipients = pRecipients->first_child; for (iRecipient = 0; pRecipients != NULL; iRecipient++, pRecipients = pRecipients->next) { cn_cbor *pkey = BuildKey(cn_cbor_mapget_string(pRecipients, "key"), true); - if (pkey == NULL) + if (pkey == NULL) { goto returnError; + } HCOSE_RECIPIENT hRecip = COSE_Recipient_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); - if (hRecip == NULL) + if (hRecip == NULL) { goto returnError; + } if (!SetSendingAttributes( - (HCOSE)hRecip, pRecipients, Attributes_Recipient_protected)) + (HCOSE)hRecip, pRecipients, Attributes_Recipient_protected)) { goto returnError; + } - if (!COSE_Recipient_SetKey(hRecip, pkey, NULL)) + if (!COSE_Recipient_SetKey(hRecip, pkey, NULL)) { goto returnError; + } cn_cbor *pSenderKey = cn_cbor_mapget_string(pRecipients, "sender_key"); if (pSenderKey != NULL) { cn_cbor *pSendKey = BuildKey(pSenderKey, false); - if (!COSE_Recipient_SetSenderKey(hRecip, pSendKey, 2, NULL)) + if (!COSE_Recipient_SetSenderKey(hRecip, pSendKey, 2, NULL)) { goto returnError; + } } - if (!COSE_Mac_AddRecipient(hMacObj, hRecip, NULL)) + if (!COSE_Mac_AddRecipient(hMacObj, hRecip, NULL)) { goto returnError; + } COSE_Recipient_Free(hRecip); } - if (!COSE_Mac_encrypt(hMacObj, NULL)) + if (!COSE_Mac_encrypt(hMacObj, NULL)) { goto returnError; + } size_t cb = COSE_Encode((HCOSE)hMacObj, NULL, 0, 0) + 1; byte *rgb = (byte *)malloc(cb); @@ -248,37 +271,46 @@ int MacMessage() size_t cb = 0; byte *rgb = NULL; - if (hEncObj == NULL) + if (hEncObj == NULL) { goto errorReturn; + } if (!COSE_Mac_map_put_int(hEncObj, COSE_Header_Algorithm, - cn_cbor_int_create( - COSE_Algorithm_HMAC_256_256, CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) + cn_cbor_int_create( + COSE_Algorithm_HMAC_256_256, CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { goto errorReturn; - if (!COSE_Mac_SetContent(hEncObj, (byte *)sz, strlen(sz), NULL)) + } + if (!COSE_Mac_SetContent(hEncObj, (byte *)sz, strlen(sz), NULL)) { goto errorReturn; + } HCOSE_RECIPIENT hRecip = COSE_Recipient_from_shared_secret(rgbSecret, - sizeof(rgbSecret), rgbKid, cbKid, CBOR_CONTEXT_PARAM_COMMA NULL); - if (hRecip == NULL) + sizeof(rgbSecret), rgbKid, cbKid, CBOR_CONTEXT_PARAM_COMMA NULL); + if (hRecip == NULL) { goto errorReturn; - if (!COSE_Mac_AddRecipient(hEncObj, hRecip, NULL)) + } + if (!COSE_Mac_AddRecipient(hEncObj, hRecip, NULL)) { goto errorReturn; + } - if (!COSE_Mac_encrypt(hEncObj, NULL)) + if (!COSE_Mac_encrypt(hEncObj, NULL)) { goto errorReturn; + } cb = COSE_Encode((HCOSE)hEncObj, NULL, 0, 0); - if (cb == 0) + if (cb == 0) { goto errorReturn; + } rgb = (byte *)malloc(cb); - if (rgb == NULL) + if (rgb == NULL) { goto errorReturn; + } cb = COSE_Encode((HCOSE)hEncObj, rgb, 0, cb); - if (cb == 0) + if (cb == 0) { goto errorReturn; + } COSE_Mac_Free(hEncObj); @@ -302,23 +334,27 @@ int MacMessage() int typ; hEncObj = (HCOSE_MAC)COSE_Decode( rgb, (int)cb, &typ, COSE_mac_object, CBOR_CONTEXT_PARAM_COMMA NULL); - if (hEncObj == NULL) + if (hEncObj == NULL) { goto errorReturn; + } int iRecipient = 0; do { HCOSE_RECIPIENT hRecip2; hRecip2 = COSE_Mac_GetRecipient(hEncObj, iRecipient, NULL); - if (hRecip2 == NULL) + if (hRecip2 == NULL) { break; + } if (!COSE_Recipient_SetKey_secret( - hRecip2, rgbSecret, sizeof(rgbSecret), NULL, 0, NULL)) + hRecip2, rgbSecret, sizeof(rgbSecret), NULL, 0, NULL)) { goto errorReturn; + } - if (!COSE_Mac_validate(hEncObj, hRecip2, NULL)) + if (!COSE_Mac_validate(hEncObj, hRecip2, NULL)) { goto errorReturn; + } iRecipient += 1; @@ -359,24 +395,30 @@ int _ValidateMac0(const cn_cbor *pControl, hMAC = (HCOSE_MAC0)COSE_Decode(pbEncoded, cbEncoded, &type, COSE_mac0_object, CBOR_CONTEXT_PARAM_COMMA NULL); if (hMAC == NULL) { - if (fFailBody) + if (fFailBody) { return 0; - else + } + else { goto errorReturn; + } } - if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) + if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) { goto errorReturn; + } pMac = cn_cbor_mapget_string(pInput, "mac0"); - if ((pMac == NULL) || (pMac->type != CN_CBOR_MAP)) + if ((pMac == NULL) || (pMac->type != CN_CBOR_MAP)) { goto errorReturn; + } - if (!SetReceivingAttributes((HCOSE)hMAC, pMac, Attributes_MAC0_protected)) + if (!SetReceivingAttributes((HCOSE)hMAC, pMac, Attributes_MAC0_protected)) { goto errorReturn; + } pRecipients = cn_cbor_mapget_string(pMac, "recipients"); - if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) + if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) { goto errorReturn; + } pRecipients = pRecipients->first_child; @@ -390,34 +432,104 @@ int _ValidateMac0(const cn_cbor *pControl, cn_cbor *alg = COSE_Mac0_map_get_int(hMAC, COSE_Header_Algorithm, COSE_BOTH, NULL); - if (!IsAlgorithmSupported(alg)) + if (!IsAlgorithmSupported(alg)) { fUnsuportedAlg = true; + } pFail = cn_cbor_mapget_string(pRecipients, "fail"); if (COSE_Mac0_validate(hMAC, k->v.bytes, k->length, NULL)) { if (fUnsuportedAlg) { fFail = true; fUnsuportedAlg = false; - } else if ((pFail != NULL) && (pFail->type != CN_CBOR_TRUE)) + } else if ((pFail != NULL) && (pFail->type != CN_CBOR_TRUE)) { fFail = true; + } } else { - if ((pFail == NULL) || (pFail->type == CN_CBOR_FALSE)) + if ((pFail == NULL) || (pFail->type == CN_CBOR_FALSE)) { fFail = true; - if (fUnsuportedAlg) + } + if (fUnsuportedAlg) { fFail = false; + } + } + +#if INCLUDE_COUNTERSIGNATURE + // Countersign on Signed Body + + // Validate counter signatures on signers + cn_cbor *countersignList = cn_cbor_mapget_string(pMac, "countersign"); + if (countersignList != NULL) { + cn_cbor *countersigners = + cn_cbor_mapget_string(countersignList, "signers"); + if (countersigners == NULL) { + fFail = true; + goto exitHere; + } + int count = countersigners->length; + bool forward = true; + + if (COSE_Mac0_map_get_int(hMAC, COSE_Header_CounterSign, + COSE_UNPROTECT_ONLY, 0) == NULL) { + fFail = true; + goto exitHere; + } + + for (int counterNo = 0; counterNo < count; counterNo++) { + HCOSE_COUNTERSIGN h = + COSE_Mac0_get_countersignature(hMAC, counterNo, 0); + if (h == NULL) { + fFail = true; + goto exitHere; + } + + cn_cbor *counterSigner = cn_cbor_index( + countersigners, forward ? counterNo : count - counterNo - 1); + + cn_cbor *pkeyCountersign = + BuildKey(cn_cbor_mapget_string(counterSigner, "key"), false); + if (pkeyCountersign == NULL) { + fFail = true; + goto exitHere; + } + + if (!COSE_CounterSign_SetKey(h, pkeyCountersign, 0)) { + fFail = true; + goto exitHere; + } + + if (COSE_Mac0_CounterSign_validate(hMAC, h, 0)) { + // I don't think we have any forced errors yet. + } + else { + if (forward && counterNo == 0 && count > 1) { + forward = false; + counterNo -= 1; + } + else { + fFail = true; + } + } + + CN_CBOR_FREE(pkeyCountersign, context); + COSE_CounterSign_Free(h); + } } +#endif COSE_Mac0_Free(hMAC); if (fFailBody) { - if (!fFail) + if (!fFail) { fFail = true; - else + } + else { fFail = false; + } } exitHere: - if (fFail) + if (fFail) { CFails += 1; + } return 0; errorReturn: @@ -440,41 +552,89 @@ int BuildMac0Message(const cn_cbor *pControl) // const cn_cbor *pFail = cn_cbor_mapget_string(pControl, "fail"); - if ((pFail != NULL) && (pFail->type == CN_CBOR_TRUE)) + if ((pFail != NULL) && (pFail->type == CN_CBOR_TRUE)) { return 0; + } HCOSE_MAC0 hMacObj = COSE_Mac0_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); const cn_cbor *pInputs = cn_cbor_mapget_string(pControl, "input"); - if (pInputs == NULL) + if (pInputs == NULL) { goto returnError; + } const cn_cbor *pMac = cn_cbor_mapget_string(pInputs, "mac0"); - if (pMac == NULL) + if (pMac == NULL) { goto returnError; + } const cn_cbor *pContent = cn_cbor_mapget_string(pInputs, "plaintext"); if (!COSE_Mac0_SetContent( - hMacObj, pContent->v.bytes, pContent->length, NULL)) + hMacObj, pContent->v.bytes, pContent->length, NULL)) { goto returnError; + } - if (!SetSendingAttributes((HCOSE)hMacObj, pMac, Attributes_MAC0_protected)) + if (!SetSendingAttributes((HCOSE)hMacObj, pMac, Attributes_MAC0_protected)) { goto returnError; + } const cn_cbor *pRecipients = cn_cbor_mapget_string(pMac, "recipients"); - if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) + if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) { goto returnError; + } pRecipients = pRecipients->first_child; cn_cbor *pkey = BuildKey(cn_cbor_mapget_string(pRecipients, "key"), false); - if (pkey == NULL) + if (pkey == NULL) { goto returnError; + } cn_cbor *k = cn_cbor_mapget_int(pkey, -1); - if (!COSE_Mac0_encrypt(hMacObj, k->v.bytes, k->length, NULL)) + if (!COSE_Mac0_encrypt(hMacObj, k->v.bytes, k->length, NULL)) { goto returnError; + } + +#ifdef INCLUDE_COUNTERSIGNATURE + // On the sign body + cn_cbor *countersigns = cn_cbor_mapget_string(pMac, "countersign"); + if (countersigns != NULL) { + countersigns = cn_cbor_mapget_string(countersigns, "signers"); + cn_cbor *countersign = countersigns->first_child; + + for (; countersign != NULL; countersign = countersign->next) { + cn_cbor *pkeyCountersign = + BuildKey(cn_cbor_mapget_string(countersign, "key"), false); + if (pkeyCountersign == NULL) { + goto returnError; + } + + HCOSE_COUNTERSIGN hCountersign = + COSE_CounterSign_Init(CBOR_CONTEXT_PARAM_COMMA NULL); + if (hCountersign == NULL) { + goto returnError; + } + + if (!SetSendingAttributes((HCOSE)hCountersign, countersign, + Attributes_Countersign_protected)) { + goto returnError; + } + + if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) { + goto returnError; + } + if (!COSE_Mac0_add_countersignature(hMacObj, hCountersign, NULL)) { + goto returnError; + } + + COSE_CounterSign_Free(hCountersign); + } + } + +#endif + + size_t cb = COSE_Encode((HCOSE)hMacObj, NULL, 0, 0) + 1; byte *rgb = (byte *)malloc(cb); cb = COSE_Encode((HCOSE)hMacObj, rgb, 0, cb); @@ -510,25 +670,34 @@ void MAC_Corners() // Invalid Handle checks - if (COSE_Mac_SetContent((HCOSE_MAC)hEncrypt, rgb, 10, NULL)) + if (COSE_Mac_SetContent((HCOSE_MAC)hEncrypt, rgb, 10, NULL)) { CFails++; - if (COSE_Mac_map_get_int((HCOSE_MAC)hEncrypt, 1, COSE_BOTH, NULL)) + } + if (COSE_Mac_map_get_int((HCOSE_MAC)hEncrypt, 1, COSE_BOTH, NULL)) { CFails++; + } if (COSE_Mac_map_put_int( - (HCOSE_MAC)hEncrypt, 1, cn, COSE_PROTECT_ONLY, NULL)) + (HCOSE_MAC)hEncrypt, 1, cn, COSE_PROTECT_ONLY, NULL)) { CFails++; - if (COSE_Mac_encrypt((HCOSE_MAC)hEncrypt, NULL)) + } + if (COSE_Mac_encrypt((HCOSE_MAC)hEncrypt, NULL)) { CFails++; - if (COSE_Mac_validate((HCOSE_MAC)hEncrypt, (HCOSE_RECIPIENT)hMAC, NULL)) + } + if (COSE_Mac_validate((HCOSE_MAC)hEncrypt, (HCOSE_RECIPIENT)hMAC, NULL)) { CFails++; - if (COSE_Mac_AddRecipient((HCOSE_MAC)hEncrypt, (HCOSE_RECIPIENT)hMAC, NULL)) + } + if (COSE_Mac_AddRecipient((HCOSE_MAC)hEncrypt, (HCOSE_RECIPIENT)hMAC, NULL)) { CFails++; - if (COSE_Mac_GetRecipient((HCOSE_MAC)hEncrypt, 0, NULL)) + } + if (COSE_Mac_GetRecipient((HCOSE_MAC)hEncrypt, 0, NULL)) { CFails++; - if (COSE_Mac_SetExternal((HCOSE_MAC)hEncrypt, rgb, 0, NULL)) + } + if (COSE_Mac_SetExternal((HCOSE_MAC)hEncrypt, rgb, 0, NULL)) { CFails++; - if (COSE_Mac_Free((HCOSE_MAC)hEncrypt)) + } + if (COSE_Mac_Free((HCOSE_MAC)hEncrypt)) { CFails++; + } #if INCLUDE_ENCRYPT0 hEncrypt = COSE_Encrypt_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); @@ -536,64 +705,83 @@ void MAC_Corners() hEncrypt = (HCOSE_ENCRYPT)COSE_CALLOC(1, sizeof(COSE), context); #endif - if (COSE_Mac_SetContent((HCOSE_MAC)hEncrypt, rgb, 10, NULL)) + if (COSE_Mac_SetContent((HCOSE_MAC)hEncrypt, rgb, 10, NULL)) { CFails++; - if (COSE_Mac_map_get_int((HCOSE_MAC)hEncrypt, 1, COSE_BOTH, NULL)) + } + if (COSE_Mac_map_get_int((HCOSE_MAC)hEncrypt, 1, COSE_BOTH, NULL)) { CFails++; + } if (COSE_Mac_map_put_int( - (HCOSE_MAC)hEncrypt, 1, cn, COSE_PROTECT_ONLY, NULL)) + (HCOSE_MAC)hEncrypt, 1, cn, COSE_PROTECT_ONLY, NULL)) { CFails++; - if (COSE_Mac_encrypt((HCOSE_MAC)hEncrypt, NULL)) + } + if (COSE_Mac_encrypt((HCOSE_MAC)hEncrypt, NULL)) { CFails++; - if (COSE_Mac_validate((HCOSE_MAC)hEncrypt, (HCOSE_RECIPIENT)hMAC, NULL)) + } + if (COSE_Mac_validate((HCOSE_MAC)hEncrypt, (HCOSE_RECIPIENT)hMAC, NULL)) { CFails++; - if (COSE_Mac_AddRecipient((HCOSE_MAC)hEncrypt, (HCOSE_RECIPIENT)hMAC, NULL)) + } + if (COSE_Mac_AddRecipient((HCOSE_MAC)hEncrypt, (HCOSE_RECIPIENT)hMAC, NULL)) { CFails++; - if (COSE_Mac_GetRecipient((HCOSE_MAC)hEncrypt, 0, NULL)) + } + if (COSE_Mac_GetRecipient((HCOSE_MAC)hEncrypt, 0, NULL)) { CFails++; - if (COSE_Mac_SetExternal((HCOSE_MAC)hEncrypt, rgb, 0, NULL)) + } + if (COSE_Mac_SetExternal((HCOSE_MAC)hEncrypt, rgb, 0, NULL)) { CFails++; - if (COSE_Mac_Free((HCOSE_MAC)hEncrypt)) + } + if (COSE_Mac_Free((HCOSE_MAC)hEncrypt)) { CFails++; + } // // Unsupported algorithm hMAC = COSE_Mac_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); - if (hMAC == NULL) + if (hMAC == NULL) { CFails++; - if (!COSE_Mac_SetContent(hMAC, (byte *)"Message", 7, NULL)) + } + if (!COSE_Mac_SetContent(hMAC, (byte *)"Message", 7, NULL)) { CFails++; + } if (!COSE_Mac_map_put_int(hMAC, COSE_Header_Algorithm, - cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) + cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; + } hRecipient = COSE_Recipient_from_shared_secret( rgb, sizeof(rgb), rgb, sizeof(rgb), CBOR_CONTEXT_PARAM_COMMA NULL); - if (hRecipient == NULL) + if (hRecipient == NULL) { CFails++; - if (!COSE_Mac_AddRecipient(hMAC, hRecipient, NULL)) + } + if (!COSE_Mac_AddRecipient(hMAC, hRecipient, NULL)) { CFails++; + } CHECK_FAILURE(COSE_Mac_encrypt(hMAC, &cose_error), COSE_ERR_UNKNOWN_ALGORITHM, CFails++); COSE_Mac_Free(hMAC); COSE_Recipient_Free(hRecipient); hMAC = COSE_Mac_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); - if (hMAC == NULL) + if (hMAC == NULL) { CFails++; - if (!COSE_Mac_SetContent(hMAC, (byte *)"Message", 7, NULL)) + } + if (!COSE_Mac_SetContent(hMAC, (byte *)"Message", 7, NULL)) { CFails++; + } if (!COSE_Mac_map_put_int(hMAC, COSE_Header_Algorithm, - cn_cbor_string_create("hmac", CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) + cn_cbor_string_create("hmac", CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; + } hRecipient = COSE_Recipient_from_shared_secret( rgb, sizeof(rgb), rgb, sizeof(rgb), CBOR_CONTEXT_PARAM_COMMA NULL); - if (hRecipient == NULL) + if (hRecipient == NULL) { CFails++; - if (!COSE_Mac_AddRecipient(hMAC, hRecipient, NULL)) + } + if (!COSE_Mac_AddRecipient(hMAC, hRecipient, NULL)) { CFails++; + } CHECK_FAILURE(COSE_Mac_encrypt(hMAC, &cose_error), COSE_ERR_UNKNOWN_ALGORITHM, CFails++); COSE_Recipient_Free(hRecipient); @@ -620,66 +808,86 @@ void MAC0_Corners() // Invalid Handle checks - if (COSE_Mac0_SetContent((HCOSE_MAC0)hEncrypt, rgb, 10, NULL)) + if (COSE_Mac0_SetContent((HCOSE_MAC0)hEncrypt, rgb, 10, NULL)) { CFails++; - if (COSE_Mac0_map_get_int((HCOSE_MAC0)hEncrypt, 1, COSE_BOTH, NULL)) + } + if (COSE_Mac0_map_get_int((HCOSE_MAC0)hEncrypt, 1, COSE_BOTH, NULL)) { CFails++; + } if (COSE_Mac0_map_put_int( - (HCOSE_MAC0)hEncrypt, 1, cn, COSE_PROTECT_ONLY, NULL)) + (HCOSE_MAC0)hEncrypt, 1, cn, COSE_PROTECT_ONLY, NULL)) { CFails++; - if (COSE_Mac0_encrypt((HCOSE_MAC0)hEncrypt, rgb, 10, NULL)) + } + if (COSE_Mac0_encrypt((HCOSE_MAC0)hEncrypt, rgb, 10, NULL)) { CFails++; - if (COSE_Mac0_validate((HCOSE_MAC0)hEncrypt, rgb, 10, NULL)) + } + if (COSE_Mac0_validate((HCOSE_MAC0)hEncrypt, rgb, 10, NULL)) { CFails++; - if (COSE_Mac0_SetExternal((HCOSE_MAC0)hEncrypt, rgb, 0, NULL)) + } + if (COSE_Mac0_SetExternal((HCOSE_MAC0)hEncrypt, rgb, 0, NULL)) { CFails++; - if (COSE_Mac0_Free((HCOSE_MAC0)hEncrypt)) + } + if (COSE_Mac0_Free((HCOSE_MAC0)hEncrypt)) { CFails++; + } hEncrypt = COSE_Encrypt_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); - if (COSE_Mac0_SetContent((HCOSE_MAC0)hEncrypt, rgb, 10, NULL)) + if (COSE_Mac0_SetContent((HCOSE_MAC0)hEncrypt, rgb, 10, NULL)) { CFails++; - if (COSE_Mac0_map_get_int((HCOSE_MAC0)hEncrypt, 1, COSE_BOTH, NULL)) + } + if (COSE_Mac0_map_get_int((HCOSE_MAC0)hEncrypt, 1, COSE_BOTH, NULL)) { CFails++; + } if (COSE_Mac0_map_put_int( - (HCOSE_MAC0)hEncrypt, 1, cn, COSE_PROTECT_ONLY, NULL)) + (HCOSE_MAC0)hEncrypt, 1, cn, COSE_PROTECT_ONLY, NULL)) { CFails++; - if (COSE_Mac0_encrypt((HCOSE_MAC0)hEncrypt, rgb, 10, NULL)) + } + if (COSE_Mac0_encrypt((HCOSE_MAC0)hEncrypt, rgb, 10, NULL)) { CFails++; - if (COSE_Mac0_validate((HCOSE_MAC0)hEncrypt, rgb, 10, NULL)) + } + if (COSE_Mac0_validate((HCOSE_MAC0)hEncrypt, rgb, 10, NULL)) { CFails++; - if (COSE_Mac0_SetExternal((HCOSE_MAC0)hEncrypt, rgb, 0, NULL)) + } + if (COSE_Mac0_SetExternal((HCOSE_MAC0)hEncrypt, rgb, 0, NULL)) { CFails++; + } - if (COSE_Mac0_Free((HCOSE_MAC0)hEncrypt)) + if (COSE_Mac0_Free((HCOSE_MAC0)hEncrypt)) { CFails++; + } // // Unsupported algorithm hMAC = COSE_Mac0_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); - if (hMAC == NULL) + if (hMAC == NULL) { CFails++; - if (!COSE_Mac0_SetContent(hMAC, (byte *)"Message", 7, NULL)) + } + if (!COSE_Mac0_SetContent(hMAC, (byte *)"Message", 7, NULL)) { CFails++; + } if (!COSE_Mac0_map_put_int(hMAC, COSE_Header_Algorithm, - cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) + cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; + } CHECK_FAILURE(COSE_Mac0_encrypt(hMAC, rgb, sizeof(rgb), &cose_error), COSE_ERR_UNKNOWN_ALGORITHM, CFails++); COSE_Mac0_Free(hMAC); hMAC = COSE_Mac0_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); - if (hMAC == NULL) + if (hMAC == NULL) { CFails++; - if (!COSE_Mac0_SetContent(hMAC, (byte *)"Message", 7, NULL)) + } + if (!COSE_Mac0_SetContent(hMAC, (byte *)"Message", 7, NULL)) { CFails++; + } if (!COSE_Mac0_map_put_int(hMAC, COSE_Header_Algorithm, - cn_cbor_string_create("hmac", CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) + cn_cbor_string_create("hmac", CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; + } CHECK_FAILURE(COSE_Mac0_encrypt(hMAC, rgb, sizeof(rgb), &cose_error), COSE_ERR_UNKNOWN_ALGORITHM, CFails++); COSE_Mac0_Free(hMAC); diff --git a/test/sign.c b/test/sign.c index 796d9e18..369598c5 100644 --- a/test/sign.c +++ b/test/sign.c @@ -670,31 +670,37 @@ int BuildSign1Message(const cn_cbor *pControl) // const cn_cbor *pFail = cn_cbor_mapget_string(pControl, "fail"); - if ((pFail != NULL) && (pFail->type == CN_CBOR_TRUE)) + if ((pFail != NULL) && (pFail->type == CN_CBOR_TRUE)) { return 0; + } HCOSE_SIGN1 hSignObj = COSE_Sign1_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); const cn_cbor *pInputs = cn_cbor_mapget_string(pControl, "input"); - if (pInputs == NULL) + if (pInputs == NULL) { goto returnError; + } const cn_cbor *pSign = cn_cbor_mapget_string(pInputs, "sign0"); - if (pSign == NULL) + if (pSign == NULL) { goto returnError; + } const cn_cbor *pContent = cn_cbor_mapget_string(pInputs, "plaintext"); if (!COSE_Sign1_SetContent( - hSignObj, pContent->v.bytes, pContent->length, NULL)) + hSignObj, pContent->v.bytes, pContent->length, NULL)) { goto returnError; + } if (!SetSendingAttributes( - (HCOSE)hSignObj, pSign, Attributes_Sign1_protected)) + (HCOSE)hSignObj, pSign, Attributes_Sign1_protected)) { goto returnError; + } cn_cbor *pkey = BuildKey(cn_cbor_mapget_string(pSign, "key"), false); - if (pkey == NULL) + if (pkey == NULL) { goto returnError; - + } + #ifdef INCLUDE_COUNTERSIGNATURE // On the sign body cn_cbor * countersigns = cn_cbor_mapget_string(pSign, "countersign"); diff --git a/test/test.c b/test/test.c index 3551b66a..a29bb462 100644 --- a/test/test.c +++ b/test/test.c @@ -1029,30 +1029,44 @@ typedef int (*ValidatePtr)(const cn_cbor* pControl); bool ProcessFile(const cn_cbor* pControl, ValidatePtr validateFunction, ValidatePtr buildFunction) { +#ifdef USE_CBOR_CONTEXT context = CreateContext(-1); +#endif if (validateFunction(pControl)) { +#ifdef USE_CBOR_CONTEXT if (IsContextEmpty(context) != 0) { printf("Memory Cleanup Failure - Validate\n"); // CFails += 1; } +#endif +#ifndef NDEBUG if (!AreListsEmpty()) { printf("Left over handle - P1\n"); CFails += 1; } +#endif +#ifdef USE_CBOR_CONTEXT FreeContext(context); context = CreateContext(-1); +#endif buildFunction(pControl); +#ifdef USE_CBOR_CONTEXT if (IsContextEmpty(context) != 0) { printf("Memory Cleanup Failure - Build\n"); // CFails += 1; } +#endif } +#ifndef NDEBUG if (!AreListsEmpty()) { printf("Left over handle - P2\n"); CFails += 1; } +#endif +#ifdef USE_CBOR_CONTEXT FreeContext(context); context = NULL; +#endif return true; } From 5eb76f9573c9096e31392efcce887a1fe0aa5e6c Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Sun, 26 Apr 2020 21:12:38 -0700 Subject: [PATCH 13/38] Add MAC code for countersignatures --- src/Cose.c | 61 ++++++++------ src/MacMessage.c | 9 +++ test/mac_test.c | 203 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 248 insertions(+), 25 deletions(-) diff --git a/src/Cose.c b/src/Cose.c index 96ed7cda..3d20b2c6 100644 --- a/src/Cose.c +++ b/src/Cose.c @@ -11,8 +11,9 @@ bool IsValidCOSEHandle(HCOSE h) { COSE_Encrypt *p = (COSE_Encrypt *)h; - if (p == NULL) + if (p == NULL) { return false; + } return true; } @@ -25,8 +26,9 @@ bool _COSE_Init(COSE_INIT_FLAGS flags, ; #ifdef USE_CBOR_CONTEXT - if (context != NULL) + if (context != NULL) { pcose->m_allocContext = *context; + } #endif CHECK_CONDITION((flags & ~(COSE_INIT_FLAGS_DETACHED_CONTENT | @@ -84,8 +86,9 @@ bool _COSE_Init_From_Object(COSE *pobj, cn_cbor_errback cbor_error; #ifdef USE_CBOR_CONTEXT - if (context != NULL) + if (context != NULL) { pobj->m_allocContext = *context; + } #endif pobj->m_cborRoot = pcbor; pobj->m_cbor = pcbor; @@ -208,8 +211,9 @@ HCOSE COSE_Decode(const byte *rgbData, if (struct_type != 0) { CHECK_CONDITION(struct_type == (COSE_object_type)cbor->v.sint, COSE_ERR_INVALID_PARAMETER); - } else + } else { struct_type = cbor->v.uint; + } *ptype = struct_type; @@ -306,8 +310,9 @@ HCOSE COSE_Decode(const byte *rgbData, size_t COSE_Encode(HCOSE msg, byte *rgb, size_t ib, size_t cb) { - if (rgb == NULL) + if (rgb == NULL) { return cn_cbor_encode_size(((COSE *)msg)->m_cbor) + ib; + } ssize_t size = cn_cbor_encoder_write(rgb, ib, cb, ((COSE *)msg)->m_cbor); return size >= 0 ? size : 0; } @@ -315,8 +320,9 @@ size_t COSE_Encode(HCOSE msg, byte *rgb, size_t ib, size_t cb) cn_cbor *COSE_get_cbor(HCOSE h) { COSE *msg = (COSE *)h; - if (!IsValidCOSEHandle(h)) + if (!IsValidCOSEHandle(h)) { return NULL; + } return msg->m_cbor; } @@ -336,32 +342,36 @@ bool _COSE_SetExternal(COSE *pcose, cn_cbor *_COSE_map_get_int(COSE *pcose, int key, int flags, - cose_errback *perror) + cose_errback *perr) { cn_cbor *p = NULL; - if (perror != NULL) - perror->err = COSE_ERR_NONE; + if (perr != NULL) { + perr->err = COSE_ERR_NONE; + } if ((pcose->m_protectedMap != NULL) && ((flags & COSE_PROTECT_ONLY) != 0)) { p = cn_cbor_mapget_int(pcose->m_protectedMap, key); - if (p != NULL) + if (p != NULL) { return p; + } } if ((pcose->m_unprotectMap != NULL) && ((flags & COSE_UNPROTECT_ONLY) != 0)) { p = cn_cbor_mapget_int(pcose->m_unprotectMap, key); - if (p != NULL) + if (p != NULL) { return p; + } } if ((pcose->m_dontSendMap != NULL) && ((flags & COSE_DONT_SEND) != 0)) { p = cn_cbor_mapget_int(pcose->m_dontSendMap, key); } - if ((p == NULL) && (perror != NULL)) - perror->err = COSE_ERR_INVALID_PARAMETER; + if ((p == NULL) && (perr != NULL)) { + perr->err = COSE_ERR_INVALID_PARAMETER; + } return p; } @@ -373,13 +383,15 @@ cn_cbor *_COSE_map_get_str(COSE *pcose, { cn_cbor *p = NULL; - if (perror != NULL) + if (perror != NULL) { perror->err = COSE_ERR_NONE; + } if ((pcose->m_protectedMap != NULL) && ((flags & COSE_PROTECT_ONLY) != 0)) { p = cn_cbor_mapget_string(pcose->m_protectedMap, key); - if (p != NULL) + if (p != NULL) { return p; + } } if ((pcose->m_unprotectMap != NULL) && @@ -394,39 +406,39 @@ cn_cbor *_COSE_map_get_str(COSE *pcose, return p; } -bool _COSE_map_put(COSE *pCose, +bool _COSE_map_put(COSE *cose, int key, cn_cbor *value, int flags, cose_errback *perr) { #ifdef USE_CBOR_CONTEXT - cn_cbor_context *context = &pCose->m_allocContext; + cn_cbor_context *context = &cose->m_allocContext; #endif cn_cbor_errback error; bool f = false; CHECK_CONDITION(value != NULL, COSE_ERR_INVALID_PARAMETER); - CHECK_CONDITION(cn_cbor_mapget_int(pCose->m_protectedMap, key) == NULL, + CHECK_CONDITION(cn_cbor_mapget_int(cose->m_protectedMap, key) == NULL, COSE_ERR_INVALID_PARAMETER); - CHECK_CONDITION(cn_cbor_mapget_int(pCose->m_unprotectMap, key) == NULL, + CHECK_CONDITION(cn_cbor_mapget_int(cose->m_unprotectMap, key) == NULL, COSE_ERR_INVALID_PARAMETER); - CHECK_CONDITION(cn_cbor_mapget_int(pCose->m_dontSendMap, key) == NULL, + CHECK_CONDITION(cn_cbor_mapget_int(cose->m_dontSendMap, key) == NULL, COSE_ERR_INVALID_PARAMETER); switch (flags) { case COSE_PROTECT_ONLY: - f = cn_cbor_mapput_int(pCose->m_protectedMap, key, value, + f = cn_cbor_mapput_int(cose->m_protectedMap, key, value, CBOR_CONTEXT_PARAM_COMMA & error); break; case COSE_UNPROTECT_ONLY: - f = cn_cbor_mapput_int(pCose->m_unprotectMap, key, value, + f = cn_cbor_mapput_int(cose->m_unprotectMap, key, value, CBOR_CONTEXT_PARAM_COMMA & error); break; case COSE_DONT_SEND: - f = cn_cbor_mapput_int(pCose->m_dontSendMap, key, value, + f = cn_cbor_mapput_int(cose->m_dontSendMap, key, value, CBOR_CONTEXT_PARAM_COMMA & error); break; @@ -453,8 +465,9 @@ cn_cbor *_COSE_encode_protected(COSE *pMessage, cose_errback *perr) pProtected = cn_cbor_index(pMessage->m_cbor, INDEX_PROTECTED); if ((pProtected != NULL) && (pProtected->type != CN_CBOR_INVALID)) { errorReturn: - if (pbProtected != NULL) + if (pbProtected != NULL) { COSE_FREE(pbProtected, context); + } return pProtected; } diff --git a/src/MacMessage.c b/src/MacMessage.c index ae212376..22c7ad80 100644 --- a/src/MacMessage.c +++ b/src/MacMessage.c @@ -577,6 +577,15 @@ bool _COSE_Mac_compute(COSE_MacMessage *pcose, goto errorReturn; } +#ifdef INCLUDE_COUNTERSIGNATURE + if (pcose->m_message.m_counterSigners != NULL) { + if (!_COSE_CounterSign_Sign( + &pcose->m_message, CBOR_CONTEXT_PARAM_COMMA perr)) { + goto errorReturn; + } + } +#endif + // Figure out the clean up fRet = true; diff --git a/test/mac_test.c b/test/mac_test.c index 90e87ff4..b508698a 100644 --- a/test/mac_test.c +++ b/test/mac_test.c @@ -131,9 +131,126 @@ int _ValidateMAC(const cn_cbor *pControl, } } +#if INCLUDE_COUNTERSIGNATURE + // Countersign on Encrypt0 Body + + // Validate counter signatures on signers + cn_cbor *countersignList = + cn_cbor_mapget_string(pRecipients, "countersign"); + if (countersignList != NULL) { + cn_cbor *countersigners = + cn_cbor_mapget_string(countersignList, "signers"); + if (countersigners == NULL) { + goto failTest; + } + int count = countersigners->length; + bool forward = true; + + if (COSE_Recipient_map_get_int(hRecip, COSE_Header_CounterSign, + COSE_UNPROTECT_ONLY, 0) == NULL) { + goto failTest; + } + + for (int counterNo = 0; counterNo < count; counterNo++) { + HCOSE_COUNTERSIGN h = + COSE_Recipient_get_countersignature(hRecip, counterNo, 0); + if (h == NULL) { + goto failTest; + } + + cn_cbor *counterSigner = cn_cbor_index(countersigners, + forward ? counterNo : count - counterNo - 1); + + cn_cbor *pkeyCountersign = BuildKey( + cn_cbor_mapget_string(counterSigner, "key"), false); + if (pkeyCountersign == NULL) { + goto failTest; + } + + if (!COSE_CounterSign_SetKey(h, pkeyCountersign, 0)) { + goto failTest; + } + + if (COSE_Recipient_CounterSign_validate(hRecip, h, 0)) { + // I don't think we have any forced errors yet. + } + else { + if (forward && counterNo == 0 && count > 1) { + forward = false; + counterNo -= 1; + } + else { + goto failTest; + } + } + + CN_CBOR_FREE(pkeyCountersign, context); + COSE_CounterSign_Free(h); + } + } +#endif + COSE_Recipient_Free(hRecip); } +#if INCLUDE_COUNTERSIGNATURE + // Countersign on Signed Body + + // Validate counter signatures on signers + cn_cbor *countersignList = cn_cbor_mapget_string(pMac, "countersign"); + if (countersignList != NULL) { + cn_cbor *countersigners = + cn_cbor_mapget_string(countersignList, "signers"); + if (countersigners == NULL) { + goto failTest; + } + int count = countersigners->length; + bool forward = true; + + if (COSE_Mac_map_get_int(hMAC, COSE_Header_CounterSign, + COSE_UNPROTECT_ONLY, 0) == NULL) { + goto failTest; + } + + for (int counterNo = 0; counterNo < count; counterNo++) { + HCOSE_COUNTERSIGN h = + COSE_Mac_get_countersignature(hMAC, counterNo, 0); + if (h == NULL) { + goto failTest; + } + + cn_cbor *counterSigner = cn_cbor_index( + countersigners, forward ? counterNo : count - counterNo - 1); + + cn_cbor *pkeyCountersign = + BuildKey(cn_cbor_mapget_string(counterSigner, "key"), false); + if (pkeyCountersign == NULL) { + goto failTest; + } + + if (!COSE_CounterSign_SetKey(h, pkeyCountersign, 0)) { + goto failTest; + } + + if (COSE_Mac_CounterSign_validate(hMAC, h, 0)) { + // I don't think we have any forced errors yet. + } + else { + if (forward && counterNo == 0 && count > 1) { + forward = false; + counterNo -= 1; + } + else { + fFail = true; + } + } + + CN_CBOR_FREE(pkeyCountersign, context); + COSE_CounterSign_Free(h); + } + } +#endif + COSE_Mac_Free(hMAC); if (fFailBody) { @@ -238,9 +355,93 @@ int BuildMacMessage(const cn_cbor *pControl) goto returnError; } + #ifdef INCLUDE_COUNTERSIGNATURE + // On the Recipient + cn_cbor *countersigns1 = + cn_cbor_mapget_string(pRecipients, "countersign"); + if (countersigns1 != NULL) { + countersigns1 = cn_cbor_mapget_string(countersigns1, "signers"); + cn_cbor *countersign = countersigns1->first_child; + + for (; countersign != NULL; countersign = countersign->next) { + cn_cbor *pkeyCountersign = + BuildKey(cn_cbor_mapget_string(countersign, "key"), false); + if (pkeyCountersign == NULL) { + goto returnError; + } + + HCOSE_COUNTERSIGN hCountersign = + COSE_CounterSign_Init(CBOR_CONTEXT_PARAM_COMMA NULL); + if (hCountersign == NULL) { + goto returnError; + } + + if (!SetSendingAttributes((HCOSE)hCountersign, countersign, + Attributes_Countersign_protected)) { + goto returnError; + } + + if (!COSE_CounterSign_SetKey( + hCountersign, pkeyCountersign, NULL)) { + goto returnError; + } + + if (!COSE_Recipient_add_countersignature( + hRecip, hCountersign, NULL)) { + goto returnError; + } + + COSE_CounterSign_Free(hCountersign); + } + } + +#endif + + COSE_Recipient_Free(hRecip); } +#ifdef INCLUDE_COUNTERSIGNATURE + // On the Evneloped body + cn_cbor *countersigns1 = cn_cbor_mapget_string(pMac, "countersign"); + if (countersigns1 != NULL) { + countersigns1 = cn_cbor_mapget_string(countersigns1, "signers"); + cn_cbor *countersign = countersigns1->first_child; + + for (; countersign != NULL; countersign = countersign->next) { + cn_cbor *pkeyCountersign = + BuildKey(cn_cbor_mapget_string(countersign, "key"), false); + if (pkeyCountersign == NULL) { + goto returnError; + } + + HCOSE_COUNTERSIGN hCountersign = + COSE_CounterSign_Init(CBOR_CONTEXT_PARAM_COMMA NULL); + if (hCountersign == NULL) { + goto returnError; + } + + if (!SetSendingAttributes((HCOSE)hCountersign, countersign, + Attributes_Countersign_protected)) { + goto returnError; + } + + if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) { + goto returnError; + } + + if (!COSE_Mac_add_countersignature( + hMacObj, hCountersign, NULL)) { + goto returnError; + } + + COSE_CounterSign_Free(hCountersign); + } + } + +#endif + + if (!COSE_Mac_encrypt(hMacObj, NULL)) { goto returnError; } @@ -454,7 +655,7 @@ int _ValidateMac0(const cn_cbor *pControl, } #if INCLUDE_COUNTERSIGNATURE - // Countersign on Signed Body + // Countersign on Mac0 Body // Validate counter signatures on signers cn_cbor *countersignList = cn_cbor_mapget_string(pMac, "countersign"); From 2a2daff05eca0cba566bd95acb05c4afcb3839ff Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 02:31:27 -0700 Subject: [PATCH 14/38] Fix buid issues --- src/Cose.c | 60 +++++++++++++++++++++++++++++++++++++----------- src/SignerInfo.c | 11 +++++++-- 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/src/Cose.c b/src/Cose.c index 3d20b2c6..b6efbf4a 100644 --- a/src/Cose.c +++ b/src/Cose.c @@ -95,7 +95,7 @@ bool _COSE_Init_From_Object(COSE *pobj, // Check if we have a tag if (pcbor->type == CN_CBOR_TAG) { - pcbor = pobj->m_cbor = pcbor->first_child; + pobj->m_cbor = pcbor->first_child; } pmap = _COSE_arrayget_int(pobj, INDEX_PROTECTED); @@ -339,7 +339,7 @@ bool _COSE_SetExternal(COSE *pcose, return true; } -cn_cbor *_COSE_map_get_int(COSE *pcose, +cn_cbor *_COSE_map_get_int(COSE *cose, int key, int flags, cose_errback *perr) @@ -350,23 +350,23 @@ cn_cbor *_COSE_map_get_int(COSE *pcose, perr->err = COSE_ERR_NONE; } - if ((pcose->m_protectedMap != NULL) && ((flags & COSE_PROTECT_ONLY) != 0)) { - p = cn_cbor_mapget_int(pcose->m_protectedMap, key); + if ((cose->m_protectedMap != NULL) && ((flags & COSE_PROTECT_ONLY) != 0)) { + p = cn_cbor_mapget_int(cose->m_protectedMap, key); if (p != NULL) { return p; } } - if ((pcose->m_unprotectMap != NULL) && + if ((cose->m_unprotectMap != NULL) && ((flags & COSE_UNPROTECT_ONLY) != 0)) { - p = cn_cbor_mapget_int(pcose->m_unprotectMap, key); + p = cn_cbor_mapget_int(cose->m_unprotectMap, key); if (p != NULL) { return p; } } - if ((pcose->m_dontSendMap != NULL) && ((flags & COSE_DONT_SEND) != 0)) { - p = cn_cbor_mapget_int(pcose->m_dontSendMap, key); + if ((cose->m_dontSendMap != NULL) && ((flags & COSE_DONT_SEND) != 0)) { + p = cn_cbor_mapget_int(cose->m_dontSendMap, key); } if ((p == NULL) && (perr != NULL)) { @@ -555,15 +555,13 @@ bool _COSE_IsInList(const COSE *const rootNode, const COSE *const thisMsg) void _COSE_RemoveFromList(COSE **rootNode, COSE *thisMsg) { - COSE *walk; - if (*rootNode == thisMsg) { *rootNode = thisMsg->m_handleList; thisMsg->m_handleList = NULL; return; } - for (walk = *rootNode; walk->m_handleList != NULL; walk = walk->m_handleList) { + for (COSE *walk = *rootNode; walk->m_handleList != NULL; walk = walk->m_handleList) { if (walk->m_handleList == thisMsg) { walk->m_handleList = thisMsg->m_handleList; thisMsg->m_handleList = NULL; @@ -574,21 +572,55 @@ void _COSE_RemoveFromList(COSE **rootNode, COSE *thisMsg) } #ifndef NDEBUG +#ifdef INCLUDE_COUNTERSIGNATURE extern COSE *CountersignRoot; +#endif +#if INCLUDE_SIGN extern COSE *SignerRoot; extern COSE *SignRoot; +#endif +#if INCLUDE_SIGN1 extern COSE *Sign1Root; +#endif +#if INCLUDE_ENCRYPT0 extern COSE *EncryptRoot; +#endif +#if INCLUDE_ENCRYPT extern COSE *EnvelopedRoot; extern COSE *RecipientRoot; +#endif +#if INCLUDE_MAC extern COSE *MacRoot; +#endif +#if INCLUDE_MAC0 extern COSE *Mac0Root; +#endif bool AreListsEmpty() { - return CountersignRoot == NULL && SignerRoot == NULL && SignRoot == NULL && - Sign1Root == NULL && EncryptRoot == NULL && EnvelopedRoot == NULL && - RecipientRoot == NULL && MacRoot == NULL && Mac0Root == NULL; + bool fRet = true; +#if INCLUDE_COUNTERSIGNATURE + fRet &= CountersignRoot == NULL; +#endif +#if INCLUDE_SIGN + fRet &= SignerRoot == NULL && SignRoot == NULL; +#endif +#if INCLUDE_SIGN1 + fRet &= Sign1Root == NULL; +#endif +#if INCLUDE_ENCRYPT + fRet &= EncryptRoot == NULL && EnvelopedRoot == NULL; +#endif +#if INCLUDE_ENCRYPT0 + fRet &= RecipientRoot == NULL; +#endif +#if INCLUDE_MAC + fRet &= MacRoot == NULL; +#endif +#if INCLUDE_MAC0 + fRet &= Mac0Root == NULL; +#endif + return fRet; } #endif diff --git a/src/SignerInfo.c b/src/SignerInfo.c index 02f08417..c01aae05 100644 --- a/src/SignerInfo.c +++ b/src/SignerInfo.c @@ -13,8 +13,9 @@ #include "cose/cose_configure.h" #include "crypto.h" -#if INCLUDE_SIGN +#if INCLUDE_SIGN || INCLUDE_COUNTERSIGNATURE +#if INCLUDE_SIGN COSE *SignerRoot = NULL; bool IsValidSignerHandle(HCOSE_SIGNER h) @@ -22,6 +23,7 @@ bool IsValidSignerHandle(HCOSE_SIGNER h) COSE_SignerInfo *p = (COSE_SignerInfo *)h; return _COSE_IsInList(SignerRoot, (COSE *)p); } +#endif bool _COSE_SignerInfo_Release(COSE_SignerInfo *pSigner) { @@ -36,6 +38,7 @@ bool _COSE_SignerInfo_Release(COSE_SignerInfo *pSigner) return true; } +#if INCLUDE_SIGN bool COSE_Signer_Free(HCOSE_SIGNER hSigner) { COSE_SignerInfo *pSigner = (COSE_SignerInfo *)hSigner; @@ -80,6 +83,7 @@ HCOSE_SIGNER COSE_Signer_Init(CBOR_CONTEXT_COMMA cose_errback *perror) _COSE_InsertInList(&SignerRoot, &pobj->m_message); return (HCOSE_SIGNER)pobj; } +#endif bool _COSE_SignerInfo_Init(COSE_INIT_FLAGS flags, COSE_SignerInfo *pobj, @@ -312,6 +316,7 @@ bool _COSE_Signer_sign(COSE_SignerInfo *pSigner, return fRet; } +#if INCLUDE_SIGN bool COSE_Signer_SetKey(HCOSE_SIGNER h, const cn_cbor *pKey, cose_errback *perr) { COSE_SignerInfo *p; @@ -358,6 +363,7 @@ bool COSE_Signer_SetExternal(HCOSE_SIGNER hcose, return _COSE_SetExternal(&((COSE_SignerInfo *)hcose)->m_message, pbExternalData, cbExternalData, perr); } +#endif bool _COSE_Signer_validate(COSE_SignerInfo *pSigner, const cn_cbor *pcborBody, @@ -454,6 +460,7 @@ bool _COSE_Signer_validate(COSE_SignerInfo *pSigner, return fRet; } +#if INCLUDE_SIGN cn_cbor *COSE_Signer_map_get_int(HCOSE_SIGNER h, int key, int flags, @@ -483,5 +490,5 @@ bool COSE_Signer_map_put_int(HCOSE_SIGNER h, errorReturn: return false; } - +#endif #endif From fe3a90f86a35ff5ca6cad97fb4eca1e179e678e0 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 02:43:56 -0700 Subject: [PATCH 15/38] More build fixes --- src/Cose.c | 9 ++++++--- src/CounterSign.c | 14 ++++++++------ src/cose_int.h | 4 ++-- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/Cose.c b/src/Cose.c index b6efbf4a..ba382e49 100644 --- a/src/Cose.c +++ b/src/Cose.c @@ -4,8 +4,8 @@ #endif #include "cose/cose.h" -#include "cose_int.h" #include "cose/cose_configure.h" +#include "cose_int.h" #include "crypto.h" bool IsValidCOSEHandle(HCOSE h) @@ -572,7 +572,7 @@ void _COSE_RemoveFromList(COSE **rootNode, COSE *thisMsg) } #ifndef NDEBUG -#ifdef INCLUDE_COUNTERSIGNATURE +#if INCLUDE_COUNTERSIGNATURE extern COSE *CountersignRoot; #endif #if INCLUDE_SIGN @@ -609,9 +609,12 @@ bool AreListsEmpty() fRet &= Sign1Root == NULL; #endif #if INCLUDE_ENCRYPT - fRet &= EncryptRoot == NULL && EnvelopedRoot == NULL; + fRet &= EncryptRoot == NULL; #endif #if INCLUDE_ENCRYPT0 + fRet &= EnvelopedRoot == NULL; +#endif +#if INCLUDE_ENCRYPT || INCLUDE_MAC fRet &= RecipientRoot == NULL; #endif #if INCLUDE_MAC diff --git a/src/CounterSign.c b/src/CounterSign.c index f769450e..89d87dfd 100644 --- a/src/CounterSign.c +++ b/src/CounterSign.c @@ -341,7 +341,7 @@ bool _COSE_CounterSign_Sign(COSE * baseMessage, CBOR_CONTEXT_COMMA cose_errback return fRet; } -#ifdef INCLUDE_SIGN +#if INCLUDE_SIGN /*************************************************************************************************** * * SIGNER @@ -482,7 +482,7 @@ bool COSE_Sign_CounterSign_validate(HCOSE_SIGN hSignMsg, } #endif -#ifdef INCLUDE_SIGN1 +#if INCLUDE_SIGN1 /*************************************************************************************************** * * SIGN1 MESSAGE @@ -552,7 +552,7 @@ bool COSE_Sign1_CounterSign_validate(HCOSE_SIGN1 hSignMsg, } #endif -#ifdef INCLUDE_ENCRYPT +#if INCLUDE_ENCRYPT /*************************************************************************************************** * * ENVELOPED MESSAGE @@ -620,7 +620,9 @@ bool COSE_Enveloped_CounterSign_validate(HCOSE_ENVELOPED hSignMsg, errorReturn: return false; } +#endif +#if INCLUDE_ENCRYPT || INCLUDE_MAC /*************************************************************************************************** * * RECIPIENT MESSAGE @@ -690,7 +692,7 @@ bool COSE_Recipient_CounterSign_validate(HCOSE_RECIPIENT hSignMsg, } #endif -#ifdef INCLUDE_ENCRYPT0 +#if INCLUDE_ENCRYPT0 /*************************************************************************************************** * * ENCRYPT0 MESSAGE @@ -760,7 +762,7 @@ bool COSE_Encrypt0_CounterSign_validate(HCOSE_ENCRYPT hSignMsg, } #endif -#ifdef INCLUDE_MAC0 +#if INCLUDE_MAC0 /*************************************************************************************************** * * MAC0 MESSAGE @@ -830,7 +832,7 @@ bool COSE_Mac0_CounterSign_validate(HCOSE_MAC0 hSignMsg, } #endif -#ifdef INCLUDE_MAC +#if INCLUDE_MAC /*************************************************************************************************** * * ENCRYPT0 MESSAGE diff --git a/src/cose_int.h b/src/cose_int.h index 5070c626..3133391e 100644 --- a/src/cose_int.h +++ b/src/cose_int.h @@ -201,9 +201,9 @@ bool _COSE_map_put(COSE *cose, int key, cn_cbor *value, int flags, - cose_errback *errp); + cose_errback *perr); -bool _COSE_SetExternal(COSE *hcose, +bool _COSE_SetExternal(COSE *pcose, const byte *pbExternalData, size_t cbExternalData, cose_errback *perr); From 0eeaa4a4bfc4ead077f69e6416375f2250bc405a Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 02:50:10 -0700 Subject: [PATCH 16/38] Next set --- src/Cose.c | 2 ++ src/SignerInfo.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/Cose.c b/src/Cose.c index ba382e49..439e26d4 100644 --- a/src/Cose.c +++ b/src/Cose.c @@ -587,6 +587,8 @@ extern COSE *EncryptRoot; #endif #if INCLUDE_ENCRYPT extern COSE *EnvelopedRoot; +#endif +#if INCLUDE_ENCRYPT || INCLUDE_MAC extern COSE *RecipientRoot; #endif #if INCLUDE_MAC diff --git a/src/SignerInfo.c b/src/SignerInfo.c index c01aae05..06490bb1 100644 --- a/src/SignerInfo.c +++ b/src/SignerInfo.c @@ -112,9 +112,11 @@ COSE_SignerInfo *_COSE_SignerInfo_Init_From_Object(cn_cbor *cbor, &pSigner->m_message, cbor, CBOR_CONTEXT_PARAM_COMMA perr)) goto errorReturn; +#if INCLUDE_SIGN if (pIn == NULL) { _COSE_InsertInList(&SignerRoot, &pSigner->m_message); } +#endif return pSigner; errorReturn: From 150020b8f25f87d25953728e1ec6d66901b4869c Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 02:52:20 -0700 Subject: [PATCH 17/38] Correct #ifs --- src/Cose.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Cose.c b/src/Cose.c index 439e26d4..0ad54d3a 100644 --- a/src/Cose.c +++ b/src/Cose.c @@ -610,10 +610,10 @@ bool AreListsEmpty() #if INCLUDE_SIGN1 fRet &= Sign1Root == NULL; #endif -#if INCLUDE_ENCRYPT +#if INCLUDE_ENCRYPT0 fRet &= EncryptRoot == NULL; #endif -#if INCLUDE_ENCRYPT0 +#if INCLUDE_ENCRYPT fRet &= EnvelopedRoot == NULL; #endif #if INCLUDE_ENCRYPT || INCLUDE_MAC From 30e3ccac13ce9015ebc11c9f597e007d6b743b2a Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 02:59:07 -0700 Subject: [PATCH 18/38] Wrong release function --- src/Recipient.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Recipient.c b/src/Recipient.c index 33eda6c0..a3a59447 100644 --- a/src/Recipient.c +++ b/src/Recipient.c @@ -140,7 +140,7 @@ void _COSE_Recipient_Free(COSE_RecipientInfo *pRecipient) return; } - _COSE_Encrypt_Release(&pRecipient->m_encrypt); + _COSE_Enveloped_Release(&pRecipient->m_encrypt); COSE_FREE(pRecipient, &pRecipient->m_encrypt.m_message.m_allocContext); return; From 38c9899f7d221230f5ade6edddf6bdf36f40a29f Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 08:15:11 -0700 Subject: [PATCH 19/38] Fix build errors * Add more workflow cases * Correct path to countersign tests * Add OpenSSL to AppVeyor --- .appveyor.yml | 2 ++ .github/workflows/main.yml | 39 +++++++++++++++++++++++------------ include/cose/cose_configure.h | 2 +- test/CMakeLists.txt | 2 +- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index 19e4816e..be0413a3 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -11,9 +11,11 @@ environment: matrix: - USE_CONTEXT: ON USE_EMBEDTLS: OFF + OPENSSL_ROOT_DIR: c:\OpenSSL-v11-Win64\bin - USE_CONTEXT: OFF USE_EMBEDTLS: OFF + OPENSSL_ROOT_DIR: c:\OpenSSL-v11-Win64\bin - USE_CONTEXT: OFF USE_EMBEDTLS: ON diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ceaa9df2..c8263edd 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -14,13 +14,19 @@ jobs: USE_CONTEXT: ["ON", "OFF"] CMAKE_OPTIONS: [ - "-DCOSE_C_INCLUDE_ENCRYPT=ON -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF", - "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=ON -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF", - "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=ON -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF", - "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=ON -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF", - "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=ON -DCOSE_C_INCLUDE_SIGN1=OFF", - "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=ON", - "-DCOSE_C_INCLUDE_COUNTERSIGN=ON", + "-DCOSE_C_INCLUDE_ENCRYPT=ON -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", +", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=ON -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=ON -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=ON -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=ON -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=ON -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", + "-DCOSE_C_INCLUDE_ENCRYPT=ON -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=ON", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=ON -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=ON", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=ON -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=ON", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=ON -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=ON", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=ON -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=ON", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=ON -DCOSE_C_INCLUDE_COUNTERSIGN=ON", ] steps: @@ -72,12 +78,19 @@ jobs: USE_CONTEXT: ["ON", "OFF"] CMAKE_OPTIONS: [ - "-DCOSE_C_INCLUDE_ENCRYPT=ON -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF", - "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=ON -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF", - "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=ON -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF", - "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=ON -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF", - "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=ON -DCOSE_C_INCLUDE_SIGN1=OFF", - "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=ON", + "-DCOSE_C_INCLUDE_ENCRYPT=ON -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", +", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=ON -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=ON -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=ON -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=ON -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=ON -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", + "-DCOSE_C_INCLUDE_ENCRYPT=ON -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=ON", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=ON -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=ON", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=ON -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=ON", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=ON -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=ON", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=ON -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=ON", + "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=ON -DCOSE_C_INCLUDE_COUNTERSIGN=ON", ] steps: diff --git a/include/cose/cose_configure.h b/include/cose/cose_configure.h index 8515346a..ee57edfc 100644 --- a/include/cose/cose_configure.h +++ b/include/cose/cose_configure.h @@ -172,5 +172,5 @@ #define INCLUDE_SIGN1 1 #endif #ifndef INCLUDE_COUNTERSIGNATURE -#define INCLUDE_COUNTERSIGNATURE 1 +#define INCLUDE_COUNTERSIGNATURE 0 #endif diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2158d35d..cd41f0c1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -111,7 +111,7 @@ add_test( if(COSE_C_INCLUDE_COUNTERSIGN) add_test( NAME Countersign - WORKING_DIRECTORY $(CMAKE_SOURCE_DIR) + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND cose_test --dir Examples/countersign) endif() From 4bdfa8b5fd56608e77051f523e50fe41906dade7 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 08:34:46 -0700 Subject: [PATCH 20/38] Fix build errors * Try a new path for search on AppVeyor * Fix error in workflow. --- .appveyor.yml | 4 ++-- .github/workflows/main.yml | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index be0413a3..27d8b402 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -11,11 +11,11 @@ environment: matrix: - USE_CONTEXT: ON USE_EMBEDTLS: OFF - OPENSSL_ROOT_DIR: c:\OpenSSL-v11-Win64\bin + CMAKE_FRAMEWORK_PATH: c:\OpenSSL-v11-Win64\bin - USE_CONTEXT: OFF USE_EMBEDTLS: OFF - OPENSSL_ROOT_DIR: c:\OpenSSL-v11-Win64\bin + CMAKE_FRAMEWORK_PATH: c:\OpenSSL-v11-Win64\bin - USE_CONTEXT: OFF USE_EMBEDTLS: ON diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c8263edd..891ab4a0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,7 +15,6 @@ jobs: CMAKE_OPTIONS: [ "-DCOSE_C_INCLUDE_ENCRYPT=ON -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", -", "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=ON -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=ON -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=ON -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", @@ -79,7 +78,6 @@ jobs: CMAKE_OPTIONS: [ "-DCOSE_C_INCLUDE_ENCRYPT=ON -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", -", "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=ON -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=ON -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=ON -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=OFF", From 5ac62320b749bb070c8ad042ee76d27a71b667b6 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 09:10:36 -0700 Subject: [PATCH 21/38] Fix bad #ifdef behavior I did not remember that these are always defined. --- src/Cose.c | 5 ++++- src/CounterSign.c | 2 +- src/Encrypt.c | 4 ++-- test/encrypt.c | 8 ++++---- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Cose.c b/src/Cose.c index 0ad54d3a..78d150d8 100644 --- a/src/Cose.c +++ b/src/Cose.c @@ -127,7 +127,7 @@ bool _COSE_Init_From_Object(COSE *pobj, cn_cbor_map_create(CBOR_CONTEXT_PARAM_COMMA & cbor_error); CHECK_CONDITION_CBOR(pobj->m_dontSendMap != NULL, cbor_error); -#ifdef INCLUDE_COUNTERSIGNATURE +#if INCLUDE_COUNTERSIGNATURE cn_cbor* pCounter = cn_cbor_mapget_int(pobj->m_unprotectMap, COSE_Header_CounterSign); if (pCounter != NULL) { int i; @@ -177,6 +177,8 @@ void _COSE_Release(COSE *pcose) (pcose->m_cborRoot->parent == NULL)) { CN_CBOR_FREE(pcose->m_cborRoot, context); } + +#if INCLUDE_COUNTERSIGNATURE if (pcose->m_counterSigners != NULL) { COSE_CounterSign* p = pcose->m_counterSigners; COSE_CounterSign* p2 = NULL; @@ -187,6 +189,7 @@ void _COSE_Release(COSE *pcose) p = p2; } } +#endif } HCOSE COSE_Decode(const byte *rgbData, diff --git a/src/CounterSign.c b/src/CounterSign.c index 89d87dfd..8f8f1320 100644 --- a/src/CounterSign.c +++ b/src/CounterSign.c @@ -8,7 +8,7 @@ #include "cose/cose_configure.h" #include "crypto.h" -#ifdef INCLUDE_COUNTERSIGNATURE +#if INCLUDE_COUNTERSIGNATURE COSE* CountersignRoot = NULL; diff --git a/src/Encrypt.c b/src/Encrypt.c index e08a82d4..89791de9 100644 --- a/src/Encrypt.c +++ b/src/Encrypt.c @@ -762,7 +762,7 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, } #endif // INCLUDE_ENCRYPT -#ifdef INCLUDE_COUNTERSIGNATURE +#if INCLUDE_COUNTERSIGNATURE if (pcose->m_message.m_counterSigners != NULL) { if (!_COSE_CounterSign_Sign( &pcose->m_message, CBOR_CONTEXT_PARAM_COMMA perr)) { @@ -1054,7 +1054,7 @@ HCOSE_RECIPIENT COSE_Enveloped_GetRecipient(HCOSE_ENVELOPED cose, return (HCOSE_RECIPIENT)p; } -#ifdef USE_COUNTER_SIGNATURES +#if USE_COUNTER_SIGNATURES bool COSE_Enveloped_AddCounterSigner(HCOSE_ENCRYPT hEnv, HCOSE_COUNTERSIGN hSign, cose_errback *perr) diff --git a/test/encrypt.c b/test/encrypt.c index a900f1b6..64ff5fb7 100644 --- a/test/encrypt.c +++ b/test/encrypt.c @@ -206,7 +206,7 @@ bool DecryptMessage(const byte *pbEncoded, #endif - #if INCLUDE_COUNTERSIGNATURE +#if INCLUDE_COUNTERSIGNATURE // Countersign on Enveloped Body // Validate counter signatures on signers @@ -386,7 +386,7 @@ HCOSE_RECIPIENT BuildRecipient(const cn_cbor *pRecipient) goto returnError; } - #ifdef INCLUDE_COUNTERSIGNATURE +#if INCLUDE_COUNTERSIGNATURE // On the Recipient cn_cbor *countersigns1 = cn_cbor_mapget_string(pRecipient, "countersign"); if (countersigns1 != NULL) { @@ -487,7 +487,7 @@ int BuildEnvelopedMessage(const cn_cbor *pControl) COSE_Recipient_Free(hRecip); } -#ifdef INCLUDE_COUNTERSIGNATURE +#if INCLUDE_COUNTERSIGNATURE // On the Evneloped body cn_cbor *countersigns1 = cn_cbor_mapget_string(pEnveloped, "countersign"); if (countersigns1 != NULL) { @@ -901,7 +901,7 @@ int BuildEncryptMessage(const cn_cbor *pControl) cn_cbor *k = cn_cbor_mapget_int(pkey, -1); -#ifdef INCLUDE_COUNTERSIGNATURE +#if INCLUDE_COUNTERSIGNATURE // On the Encrypt0 body cn_cbor *countersigns = cn_cbor_mapget_string(pEncrypt, "countersign"); if (countersigns != NULL) { From 446b2ae1380624b5e2a60a78cafc239479a1b6bf Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 09:38:42 -0700 Subject: [PATCH 22/38] Catch the rest of the #ifdefs Use findstr to get all of them Force a package sort order to get the lastest version of openssl for AppVeyor --- CMakeLists.txt | 4 ++++ include/cose/cose_configure.h | 5 +++-- src/CounterSign0.c | 2 +- src/Encrypt.c | 4 ++-- src/MacMessage.c | 2 +- src/Sign.c | 2 +- src/Sign1.c | 2 +- src/SignerInfo.c | 2 +- src/cose_int.h | 10 ++-------- 9 files changed, 16 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d21aa4d9..b04f860a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -90,6 +90,10 @@ if(versbose) set(CMAKE_VERBOSE_MAKEFILE ON) endif() +# Control the order of packages - get the latest not the first +SET(CMAKE_FIND_PACKAGE_SORT_ORDER NATURAL) +SET(CMAKE_FIND_PACKAGE_SORT_DIRECTION DEC) + ############################################################################### # DOCS ############################################################################### diff --git a/include/cose/cose_configure.h b/include/cose/cose_configure.h index ee57edfc..873bdd81 100644 --- a/include/cose/cose_configure.h +++ b/include/cose/cose_configure.h @@ -147,8 +147,6 @@ #endif #endif // !defined (USE_MBED_TLS) -// #define USE_COUNTER_SIGNATURE1 - // // Define which COSE objects are included // @@ -174,3 +172,6 @@ #ifndef INCLUDE_COUNTERSIGNATURE #define INCLUDE_COUNTERSIGNATURE 0 #endif +#ifndef INCLUDE_COUNTERSIGNATURE1 +#define INCLUDE_COUNTERSIGNATURE1 0 +#endif diff --git a/src/CounterSign0.c b/src/CounterSign0.c index 649370f2..41fd5a5f 100644 --- a/src/CounterSign0.c +++ b/src/CounterSign0.c @@ -8,7 +8,7 @@ #include "cose/cose_configure.h" #include "crypto.h" -#ifdef USE_COUNTER_SIGNATURE1 +#if USE_COUNTER_SIGNATURE1 extern bool IsValidCounterSign1Handle(HCOSE_COUNTERSIGN1 h); diff --git a/src/Encrypt.c b/src/Encrypt.c index 89791de9..758d5220 100644 --- a/src/Encrypt.c +++ b/src/Encrypt.c @@ -698,7 +698,7 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, case COSE_Algorithm_AES_CCM_64_64_128: if (!AES_CCM_Encrypt( pcose, 64, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) - goto errorReturn; + goto errorReturn; break; #endif @@ -754,7 +754,7 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM); } -#ifdef INCLUDE_ENCRYPT +#if INCLUDE_ENCRYPT for (pri = pcose->m_recipientFirst; pri != NULL; pri = pri->m_recipientNext) { if (!_COSE_Recipient_encrypt(pri, pbKey, cbKey, perr)) diff --git a/src/MacMessage.c b/src/MacMessage.c index 22c7ad80..8b954502 100644 --- a/src/MacMessage.c +++ b/src/MacMessage.c @@ -577,7 +577,7 @@ bool _COSE_Mac_compute(COSE_MacMessage *pcose, goto errorReturn; } -#ifdef INCLUDE_COUNTERSIGNATURE +#if INCLUDE_COUNTERSIGNATURE if (pcose->m_message.m_counterSigners != NULL) { if (!_COSE_CounterSign_Sign( &pcose->m_message, CBOR_CONTEXT_PARAM_COMMA perr)) { diff --git a/src/Sign.c b/src/Sign.c index c9bd0c05..ec60bec4 100644 --- a/src/Sign.c +++ b/src/Sign.c @@ -292,7 +292,7 @@ bool COSE_Sign_Sign(HCOSE_SIGN h, cose_errback *perr) } } -#ifdef INCLUDE_COUNTERSIGNATURE +#if INCLUDE_COUNTERSIGNATURE if (pMessage->m_message.m_counterSigners != NULL) { if (!_COSE_CounterSign_Sign( &pMessage->m_message, CBOR_CONTEXT_PARAM_COMMA perr)) { diff --git a/src/Sign1.c b/src/Sign1.c index 4d8e54a4..34ba5596 100644 --- a/src/Sign1.c +++ b/src/Sign1.c @@ -230,7 +230,7 @@ bool COSE_Sign1_Sign(HCOSE_SIGN1 h, const cn_cbor *pKey, cose_errback *perr) if (!_COSE_Signer0_sign(pMessage, pKey, perr)) goto errorReturn; -#ifdef INCLUDE_COUNTERSIGNATURE +#if INCLUDE_COUNTERSIGNATURE if (pMessage->m_message.m_counterSigners != NULL) { if (!_COSE_CounterSign_Sign( &pMessage->m_message, CBOR_CONTEXT_PARAM_COMMA perr)) { diff --git a/src/SignerInfo.c b/src/SignerInfo.c index 06490bb1..7c901df9 100644 --- a/src/SignerInfo.c +++ b/src/SignerInfo.c @@ -300,7 +300,7 @@ bool _COSE_Signer_sign(COSE_SignerInfo *pSigner, FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM); } -#ifdef INCLUDE_COUNTERSIGNATURE +#if INCLUDE_COUNTERSIGNATURE if (pSigner->m_message.m_counterSigners != NULL) { if (!_COSE_CounterSign_Sign(&pSigner->m_message, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; diff --git a/src/cose_int.h b/src/cose_int.h index 3133391e..59fa0961 100644 --- a/src/cose_int.h +++ b/src/cose_int.h @@ -8,14 +8,10 @@ // These definitions are here because they aren't required for the public // interface, and they were quite confusing in cn-cbor.h -#ifdef INCLUDE_COUNTERSIGNATURE struct CounterSign; typedef struct CounterSign COSE_CounterSign; -#endif -#ifdef USE_COUNTER_SIGNATURE1 struct CounterSign1; typedef struct CounterSign1 COSE_CounterSign1; -#endif #define UNUSED(x) ((void)(x)) @@ -40,10 +36,10 @@ typedef struct _COSE { cn_cbor_context m_allocContext; #endif struct _COSE *m_handleList; -#ifdef INCLUDE_COUNTERSIGNATURE +#if INCLUDE_COUNTERSIGNATURE COSE_CounterSign *m_counterSigners; // Linked list of all counter signatures #endif -#ifdef INCLUDE_COUNTERSIGNATURE1 +#if INCLUDE_COUNTERSIGNATURE1 COSE_CounterSign1* m_counterSign1; #endif } COSE; @@ -105,12 +101,10 @@ typedef struct { #endif typedef COSE_MacMessage COSE_Mac0Message; -#ifdef INCLUDE_COUNTERSIGNATURE struct CounterSign { COSE_SignerInfo m_signer; COSE_CounterSign *m_next; }; -#endif #ifdef USE_CBOR_CONTEXT /** From ccad538507bc8e176ead2e4ed63942ba78ec53ab Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 09:45:26 -0700 Subject: [PATCH 23/38] Missed a directory --- test/mac_test.c | 6 +++--- test/sign.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/test/mac_test.c b/test/mac_test.c index b508698a..b44ddf70 100644 --- a/test/mac_test.c +++ b/test/mac_test.c @@ -355,7 +355,7 @@ int BuildMacMessage(const cn_cbor *pControl) goto returnError; } - #ifdef INCLUDE_COUNTERSIGNATURE +#if INCLUDE_COUNTERSIGNATURE // On the Recipient cn_cbor *countersigns1 = cn_cbor_mapget_string(pRecipients, "countersign"); @@ -401,7 +401,7 @@ int BuildMacMessage(const cn_cbor *pControl) COSE_Recipient_Free(hRecip); } -#ifdef INCLUDE_COUNTERSIGNATURE +#if INCLUDE_COUNTERSIGNATURE // On the Evneloped body cn_cbor *countersigns1 = cn_cbor_mapget_string(pMac, "countersign"); if (countersigns1 != NULL) { @@ -796,7 +796,7 @@ int BuildMac0Message(const cn_cbor *pControl) goto returnError; } -#ifdef INCLUDE_COUNTERSIGNATURE +#if INCLUDE_COUNTERSIGNATURE // On the sign body cn_cbor *countersigns = cn_cbor_mapget_string(pMac, "countersign"); if (countersigns != NULL) { diff --git a/test/sign.c b/test/sign.c index 369598c5..e5fd4a45 100644 --- a/test/sign.c +++ b/test/sign.c @@ -354,7 +354,7 @@ int BuildSignedMessage(const cn_cbor *pControl) #endif COSE_Signer_Free(hSigner); } -#ifdef INCLUDE_COUNTERSIGNATURE +#if INCLUDE_COUNTERSIGNATURE // On the sign body cn_cbor * countersigns1 = cn_cbor_mapget_string(pSign, "countersign"); if (countersigns1 != NULL) { @@ -701,7 +701,7 @@ int BuildSign1Message(const cn_cbor *pControl) goto returnError; } -#ifdef INCLUDE_COUNTERSIGNATURE +#if INCLUDE_COUNTERSIGNATURE // On the sign body cn_cbor * countersigns = cn_cbor_mapget_string(pSign, "countersign"); if (countersigns != NULL) { From 751c1df17f6d35a14b0d24dd21e35c93ae3778cb Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 11:18:10 -0700 Subject: [PATCH 24/38] Fix crash error Don't deallocate twice --- src/Encrypt.c | 153 +++++++++++++++-------- src/Recipient.c | 324 ++++++++++++++++++++++++++++++++---------------- 2 files changed, 315 insertions(+), 162 deletions(-) diff --git a/src/Encrypt.c b/src/Encrypt.c index 758d5220..8bfad20a 100644 --- a/src/Encrypt.c +++ b/src/Encrypt.c @@ -18,8 +18,6 @@ #include "crypto.h" #if INCLUDE_ENCRYPT || INCLUDE_MAC -void _COSE_Enveloped_Release(COSE_Enveloped *p); - COSE *EnvelopedRoot = NULL; #endif @@ -93,19 +91,20 @@ HCOSE_ENVELOPED _COSE_Enveloped_Init_From_Object(cn_cbor *cbor, COSE_Enveloped *pobj = pIn; cn_cbor *pRecipients = NULL; cose_errback error = {0}; - if (perr == NULL) + if (perr == NULL) { perr = &error; + } - if (pobj == NULL) + if (pobj == NULL) { pobj = (COSE_Enveloped *)COSE_CALLOC(1, sizeof(COSE_Enveloped), context); + } if (pobj == NULL) { perr->err = COSE_ERR_OUT_OF_MEMORY; errorReturn: - if (pobj != NULL) { + if (pIn == NULL && pobj != NULL) { _COSE_Enveloped_Release(pobj); - if (pIn == NULL) - COSE_FREE(pobj, context); + COSE_FREE(pobj, context); } return NULL; } @@ -132,8 +131,9 @@ HCOSE_ENVELOPED _COSE_Enveloped_Init_From_Object(cn_cbor *cbor, } } - if (pIn == NULL) + if (pIn == NULL) { _COSE_InsertInList(&EnvelopedRoot, &pobj->m_message); + } return (HCOSE_ENVELOPED)pobj; } @@ -147,8 +147,9 @@ bool COSE_Enveloped_Free(HCOSE_ENVELOPED h) #endif COSE_Enveloped *p = (COSE_Enveloped *)h; - if (!IsValidEnvelopedHandle(h)) + if (!IsValidEnvelopedHandle(h)) { return false; + } if (p->m_message.m_refCount > 1) { p->m_message.m_refCount--; @@ -175,8 +176,9 @@ void _COSE_Enveloped_Release(COSE_Enveloped *p) COSE_RecipientInfo *pRecipient1; COSE_RecipientInfo *pRecipient2; - if (p->pbContent != NULL) + if (p->pbContent != NULL) { COSE_FREE((void *)p->pbContent, &p->m_message.m_allocContext); + } // if (p->pbIV != NULL) COSE_FREE(p->pbIV, &p->m_message.m_allocContext); for (pRecipient1 = p->m_recipientFirst; pRecipient1 != NULL; @@ -241,8 +243,9 @@ bool _COSE_Enveloped_decrypt(COSE_Enveloped *pcose, if (cn == NULL) { error: errorReturn: - if (pbAuthData != NULL) + if (pbAuthData != NULL) { COSE_FREE(pbAuthData, context); + } if (pbKeyNew != NULL) { memset(pbKeyNew, 0xff, cbitKey / 8); COSE_FREE(pbKeyNew, context); @@ -352,13 +355,15 @@ bool _COSE_Enveloped_decrypt(COSE_Enveloped *pcose, pRecipX = pRecipX->m_recipientNext) { if (pRecipX == pRecip) { if (!_COSE_Recipient_decrypt( - pRecipX, pRecip, alg, cbitKey, pbKeyNew, perr)) + pRecipX, pRecip, alg, cbitKey, pbKeyNew, perr)) { goto errorReturn; + } break; } else if (pRecipX->m_encrypt.m_recipientFirst != NULL) { if (_COSE_Recipient_decrypt( - pRecipX, pRecip, alg, cbitKey, pbKeyNew, perr)) + pRecipX, pRecip, alg, cbitKey, pbKeyNew, perr)) { break; + } } } CHECK_CONDITION(pRecipX != NULL, COSE_ERR_NO_RECIPIENT_FOUND); @@ -366,8 +371,9 @@ bool _COSE_Enveloped_decrypt(COSE_Enveloped *pcose, for (pRecip = pcose->m_recipientFirst; pRecip != NULL; pRecip = pRecip->m_recipientNext) { if (_COSE_Recipient_decrypt( - pRecip, NULL, alg, cbitKey, pbKeyNew, perr)) + pRecip, NULL, alg, cbitKey, pbKeyNew, perr)) { break; + } } CHECK_CONDITION(pRecip != NULL, COSE_ERR_NO_RECIPIENT_FOUND); } @@ -377,8 +383,9 @@ bool _COSE_Enveloped_decrypt(COSE_Enveloped *pcose, // Build authenticated data if (!_COSE_Encrypt_Build_AAD( - &pcose->m_message, &pbAuthData, &cbAuthData, szContext, perr)) + &pcose->m_message, &pbAuthData, &cbAuthData, szContext, perr)) { goto errorReturn; + } cn = _COSE_arrayget_int(&pcose->m_message, INDEX_BODY); CHECK_CONDITION(cn != NULL, COSE_ERR_INVALID_PARAMETER); @@ -387,88 +394,99 @@ bool _COSE_Enveloped_decrypt(COSE_Enveloped *pcose, #ifdef USE_AES_CCM_16_64_128 case COSE_Algorithm_AES_CCM_16_64_128: if (!AES_CCM_Decrypt(pcose, 64, 16, pbKey, cbitKey / 8, cn->v.bytes, - cn->length, pbAuthData, cbAuthData, perr)) + cn->length, pbAuthData, cbAuthData, perr)) { goto error; + } break; #endif #ifdef USE_AES_CCM_16_64_256 case COSE_Algorithm_AES_CCM_16_64_256: if (!AES_CCM_Decrypt(pcose, 64, 16, pbKey, cbitKey / 8, cn->v.bytes, - cn->length, pbAuthData, cbAuthData, perr)) + cn->length, pbAuthData, cbAuthData, perr)) { goto error; + } break; #endif #ifdef USE_AES_CCM_16_128_128 case COSE_Algorithm_AES_CCM_16_128_128: if (!AES_CCM_Decrypt(pcose, 128, 16, pbKey, cbitKey / 8, - cn->v.bytes, cn->length, pbAuthData, cbAuthData, perr)) + cn->v.bytes, cn->length, pbAuthData, cbAuthData, perr)) { goto error; + } break; #endif #ifdef USE_AES_CCM_16_128_256 case COSE_Algorithm_AES_CCM_16_128_256: if (!AES_CCM_Decrypt(pcose, 128, 16, pbKey, cbitKey / 8, - cn->v.bytes, cn->length, pbAuthData, cbAuthData, perr)) + cn->v.bytes, cn->length, pbAuthData, cbAuthData, perr)) { goto error; + } break; #endif #ifdef USE_AES_CCM_64_64_128 case COSE_Algorithm_AES_CCM_64_64_128: if (!AES_CCM_Decrypt(pcose, 64, 64, pbKey, cbitKey / 8, cn->v.bytes, - cn->length, pbAuthData, cbAuthData, perr)) + cn->length, pbAuthData, cbAuthData, perr)) { goto error; + } break; #endif #ifdef USE_AES_CCM_64_64_256 case COSE_Algorithm_AES_CCM_64_64_256: if (!AES_CCM_Decrypt(pcose, 64, 64, pbKey, cbitKey / 8, cn->v.bytes, - cn->length, pbAuthData, cbAuthData, perr)) + cn->length, pbAuthData, cbAuthData, perr)) { goto error; + } break; #endif #ifdef USE_AES_CCM_64_128_128 case COSE_Algorithm_AES_CCM_64_128_128: if (!AES_CCM_Decrypt(pcose, 128, 64, pbKey, cbitKey / 8, - cn->v.bytes, cn->length, pbAuthData, cbAuthData, perr)) + cn->v.bytes, cn->length, pbAuthData, cbAuthData, perr)) { goto error; + } break; #endif #ifdef USE_AES_CCM_64_128_256 case COSE_Algorithm_AES_CCM_64_128_256: if (!AES_CCM_Decrypt(pcose, 128, 64, pbKey, cbitKey / 8, - cn->v.bytes, cn->length, pbAuthData, cbAuthData, perr)) + cn->v.bytes, cn->length, pbAuthData, cbAuthData, perr)) { goto error; + } break; #endif #ifdef USE_AES_GCM_128 case COSE_Algorithm_AES_GCM_128: if (!AES_GCM_Decrypt(pcose, pbKey, cbitKey / 8, cn->v.bytes, - cn->length, pbAuthData, cbAuthData, perr)) + cn->length, pbAuthData, cbAuthData, perr)) { goto error; + } break; #endif #ifdef USE_AES_GCM_192 case COSE_Algorithm_AES_GCM_192: if (!AES_GCM_Decrypt(pcose, pbKey, cbitKey / 8, cn->v.bytes, - cn->length, pbAuthData, cbAuthData, perr)) + cn->length, pbAuthData, cbAuthData, perr)) { goto error; + } break; #endif #ifdef USE_AES_GCM_256 case COSE_Algorithm_AES_GCM_256: if (!AES_GCM_Decrypt(pcose, pbKey, cbitKey / 8, cn->v.bytes, - cn->length, pbAuthData, cbAuthData, perr)) + cn->length, pbAuthData, cbAuthData, perr)) { goto error; + } break; #endif @@ -477,12 +495,15 @@ bool _COSE_Enveloped_decrypt(COSE_Enveloped *pcose, break; } - if (pbAuthData != NULL) + if (pbAuthData != NULL) { COSE_FREE(pbAuthData, context); - if (pbKeyNew != NULL) + } + if (pbKeyNew != NULL) { COSE_FREE(pbKeyNew, context); - if (perr != NULL) + } + if (perr != NULL) { perr->err = COSE_ERR_NONE; + } return true; } @@ -526,8 +547,9 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, cn_Alg = _COSE_map_get_int( &pcose->m_message, COSE_Header_Algorithm, COSE_BOTH, perr); - if (cn_Alg == NULL) + if (cn_Alg == NULL) { goto errorReturn; + } CHECK_CONDITION((cn_Alg->type != CN_CBOR_TEXT), COSE_ERR_UNKNOWN_ALGORITHM); CHECK_CONDITION( @@ -629,8 +651,9 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, pbKeyNew = _COSE_RecipientInfo_generateKey(pri, alg, cbitKey, perr); cbKey = cbitKey / 8; - if (pbKeyNew == NULL) + if (pbKeyNew == NULL) { goto errorReturn; + } } else { t |= 2; } @@ -651,102 +674,115 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, const cn_cbor *cbProtected = _COSE_encode_protected(&pcose->m_message, perr); - if (cbProtected == NULL) + if (cbProtected == NULL) { goto errorReturn; + } // Build authenticated data size_t cbAuthData = 0; if (!_COSE_Encrypt_Build_AAD( - &pcose->m_message, &pbAuthData, &cbAuthData, szContext, perr)) + &pcose->m_message, &pbAuthData, &cbAuthData, szContext, perr)) { goto errorReturn; + } switch (alg) { #ifdef USE_AES_CCM_16_64_128 case COSE_Algorithm_AES_CCM_16_64_128: if (!AES_CCM_Encrypt( - pcose, 64, 16, pbKey, cbKey, pbAuthData, cbAuthData, perr)) + pcose, 64, 16, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_AES_CCM_16_64_256 case COSE_Algorithm_AES_CCM_16_64_256: if (!AES_CCM_Encrypt( - pcose, 64, 16, pbKey, cbKey, pbAuthData, cbAuthData, perr)) + pcose, 64, 16, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_AES_CCM_16_128_128 case COSE_Algorithm_AES_CCM_16_128_128: if (!AES_CCM_Encrypt( - pcose, 128, 16, pbKey, cbKey, pbAuthData, cbAuthData, perr)) + pcose, 128, 16, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_AES_CCM_16_128_256 case COSE_Algorithm_AES_CCM_16_128_256: if (!AES_CCM_Encrypt( - pcose, 128, 16, pbKey, cbKey, pbAuthData, cbAuthData, perr)) + pcose, 128, 16, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_AES_CCM_64_64_128 case COSE_Algorithm_AES_CCM_64_64_128: if (!AES_CCM_Encrypt( - pcose, 64, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) - goto errorReturn; + pcose, 64, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { + goto errorReturn; + } break; #endif #ifdef USE_AES_CCM_64_64_256 case COSE_Algorithm_AES_CCM_64_64_256: if (!AES_CCM_Encrypt( - pcose, 64, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) + pcose, 64, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_AES_CCM_64_128_128 case COSE_Algorithm_AES_CCM_64_128_128: if (!AES_CCM_Encrypt( - pcose, 128, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) + pcose, 128, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_AES_CCM_64_128_256 case COSE_Algorithm_AES_CCM_64_128_256: if (!AES_CCM_Encrypt( - pcose, 128, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) + pcose, 128, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_AES_GCM_128 case COSE_Algorithm_AES_GCM_128: if (!AES_GCM_Encrypt( - pcose, pbKey, cbKey, pbAuthData, cbAuthData, perr)) + pcose, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_AES_GCM_192 case COSE_Algorithm_AES_GCM_192: if (!AES_GCM_Encrypt( - pcose, pbKey, cbKey, pbAuthData, cbAuthData, perr)) + pcose, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_AES_GCM_256 case COSE_Algorithm_AES_GCM_256: if (!AES_GCM_Encrypt( - pcose, pbKey, cbKey, pbAuthData, cbAuthData, perr)) + pcose, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { goto errorReturn; + } break; #endif @@ -757,8 +793,9 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, #if INCLUDE_ENCRYPT for (pri = pcose->m_recipientFirst; pri != NULL; pri = pri->m_recipientNext) { - if (!_COSE_Recipient_encrypt(pri, pbKey, cbKey, perr)) + if (!_COSE_Recipient_encrypt(pri, pbKey, cbKey, perr)) { goto errorReturn; + } } #endif // INCLUDE_ENCRYPT @@ -776,8 +813,9 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, fRet = true; errorReturn: - if (pbAuthData != NULL) + if (pbAuthData != NULL) { COSE_FREE(pbAuthData, context); + } if (pbKeyNew != NULL) { memset(pbKeyNew, 0, cbKey); COSE_FREE(pbKeyNew, context); @@ -842,8 +880,9 @@ bool _COSE_Enveloped_SetContent(COSE_Enveloped *cose, cose->pbContent = pb = (byte *)COSE_CALLOC(cb, 1, &cose->m_message.m_allocContext); if (cose->pbContent == NULL) { - if (perror != NULL) + if (perror != NULL) { perror->err = COSE_ERR_INVALID_PARAMETER; + } return false; } memcpy(pb, rgb, cb); @@ -870,8 +909,9 @@ cn_cbor *COSE_Enveloped_map_get_int(HCOSE_ENVELOPED h, cose_errback *perror) { if (!IsValidEnvelopedHandle(h)) { - if (perror != NULL) + if (perror != NULL) { perror->err = COSE_ERR_INVALID_HANDLE; + } return NULL; } @@ -929,8 +969,9 @@ bool COSE_Enveloped_AddRecipient(HCOSE_ENVELOPED hEnc, if (!_COSE_array_replace(&pEncrypt->m_message, pRecipients, INDEX_RECIPIENTS, CBOR_CONTEXT_PARAM_COMMA & cbor_error)) { CN_CBOR_FREE(pRecipients, context); - if (perr != NULL) + if (perr != NULL) { perr->err = _MapFromCBOR(cbor_error); + } goto errorReturn; } } @@ -1021,12 +1062,15 @@ bool _COSE_Encrypt_Build_AAD(COSE *pMessage, return true; errorReturn: - if (pbAuthData != NULL) + if (pbAuthData != NULL) { COSE_FREE(pbAuthData, context); - if (ptmp != NULL) + } + if (ptmp != NULL) { CN_CBOR_FREE(ptmp, NULL); - if (pAuthData != NULL) + } + if (pAuthData != NULL) { CN_CBOR_FREE(pAuthData, context); + } return false; } #endif @@ -1047,8 +1091,9 @@ HCOSE_RECIPIENT COSE_Enveloped_GetRecipient(HCOSE_ENVELOPED cose, CHECK_CONDITION(p != NULL, COSE_ERR_INVALID_PARAMETER); p = p->m_recipientNext; } - if (p != NULL) + if (p != NULL) { p->m_encrypt.m_message.m_refCount++; + } errorReturn: return (HCOSE_RECIPIENT)p; diff --git a/src/Recipient.c b/src/Recipient.c index a3a59447..3b7f39f0 100644 --- a/src/Recipient.c +++ b/src/Recipient.c @@ -37,8 +37,9 @@ bool IsValidRecipientHandle(HCOSE_RECIPIENT h) { COSE_RecipientInfo *p = (COSE_RecipientInfo *)h; - if (p == NULL) + if (p == NULL) { return false; + } return _COSE_IsInList(RecipientRoot, &p->m_encrypt.m_message); } @@ -92,18 +93,21 @@ HCOSE_RECIPIENT COSE_Recipient_from_shared_secret(byte *rgbKey, HCOSE_RECIPIENT hRecipient = NULL; hRecipient = COSE_Recipient_Init(0, CBOR_CONTEXT_PARAM_COMMA perr); - if (hRecipient == NULL) + if (hRecipient == NULL) { goto errorReturn; + } if (!COSE_Recipient_SetKey_secret( - hRecipient, rgbKey, cbKey, rgbKid, cbKid, perr)) + hRecipient, rgbKey, cbKey, rgbKid, cbKid, perr)) { goto errorReturn; + } return hRecipient; errorReturn: - if (hRecipient != NULL) + if (hRecipient != NULL) { COSE_Recipient_Free(hRecipient); + } return NULL; } @@ -128,8 +132,9 @@ COSE_RecipientInfo *_COSE_Recipient_Init_From_Object(cn_cbor *cbor, return pRecipient; errorReturn: - if (pRecipient != NULL) + if (pRecipient != NULL) { _COSE_Recipient_Free(pRecipient); + } return NULL; } @@ -187,8 +192,9 @@ static bool HKDF_X(COSE *pCose, size_t cbSecret = 0; if (!BuildContextBytes(pCose, algResult, cbitKey, &pbContext, &cbContext, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } if (fECDH) { #ifdef USE_ECDH @@ -215,12 +221,14 @@ static bool HKDF_X(COSE *pCose, pkeyMessage = (cn_cbor *)pKeyPrivate; if (!ECDH_ComputeSecret(pCose, &pkeyMessage, pKeyPublic, &pbSecret, - &cbSecret, CBOR_CONTEXT_PARAM_COMMA perr)) + &cbSecret, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } if (!fStatic && pkeyMessage->parent == NULL) { if (!_COSE_map_put(pCose, COSE_Header_ECDH_EPHEMERAL, - pkeyMessage, COSE_UNPROTECT_ONLY, perr)) + pkeyMessage, COSE_UNPROTECT_ONLY, perr)) { goto errorReturn; + } } } else { pkeyMessage = _COSE_map_get_int(pCose, @@ -232,8 +240,9 @@ static bool HKDF_X(COSE *pCose, if (!ECDH_ComputeSecret(pCose, (cn_cbor **)&pKeyPrivate, pkeyMessage, &pbSecret, &cbSecret, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } } #else goto errorReturn; @@ -258,20 +267,23 @@ static bool HKDF_X(COSE *pCose, if (fHMAC) { #ifdef USE_HKDF_SHA2 if (!HKDF_Extract(pCose, pbSecret, cbSecret, cbitHash, rgbDigest, - &cbDigest, CBOR_CONTEXT_PARAM_COMMA perr)) + &cbDigest, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } if (!HKDF_Expand(pCose, cbitHash, rgbDigest, cbDigest, pbContext, - cbContext, pbKey, cbitKey / 8, perr)) + cbContext, pbKey, cbitKey / 8, perr)) { goto errorReturn; + } #else goto errorReturn; #endif } else { #ifdef USE_HKDF_AES if (!HKDF_AES_Expand(pCose, cbitHash, pbSecret, cbSecret, pbContext, - cbContext, pbKey, cbitKey / 8, perr)) + cbContext, pbKey, cbitKey / 8, perr)) { goto errorReturn; + } #else goto errorReturn; #endif @@ -284,8 +296,9 @@ static bool HKDF_X(COSE *pCose, COSE_FREE(pbSecret, context); } memset(rgbDigest, 0, sizeof(rgbDigest)); - if (pbContext != NULL) + if (pbContext != NULL) { COSE_FREE(pbContext, context); + } return fRet; } #endif // defined(USE_HKDF_SHA2) || defined(USE_HKDF_AES) @@ -325,14 +338,18 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, &pRecip->m_encrypt.m_message, COSE_Header_Algorithm, COSE_BOTH, perr); if (cn == NULL) { errorReturn: - if (pbContext != NULL) + if (pbContext != NULL) { COSE_FREE(pbContext, context); - if (pbProtected != NULL) + } + if (pbProtected != NULL) { COSE_FREE(pbProtected, context); - if (pbAuthData != NULL) + } + if (pbAuthData != NULL) { COSE_FREE(pbAuthData, context); - if (pbSecret != NULL) + } + if (pbSecret != NULL) { COSE_FREE(pbSecret, context); + } return false; } CHECK_CONDITION(cn->type != CN_CBOR_TEXT, COSE_ERR_UNKNOWN_ALGORITHM); @@ -456,8 +473,9 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, for (pRecip2 = pcose->m_recipientFirst; pRecip2 != NULL; pRecip2 = pRecip->m_recipientNext) { if (_COSE_Recipient_decrypt( - pRecip2, NULL, alg, cbitKeyX, pbKeyX, perr)) + pRecip2, NULL, alg, cbitKeyX, pbKeyX, perr)) { break; + } } CHECK_CONDITION(pRecip2 != NULL, COSE_ERR_NO_RECIPIENT_FOUND); } @@ -471,8 +489,9 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, if (pbKeyX != NULL) { int x = cbitKeyOut / 8; if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, pbKeyX, cbitKeyX, - cnBody->v.bytes, cnBody->length, pbKeyOut, &x, perr)) + cnBody->v.bytes, cnBody->length, pbKeyOut, &x, perr)) { goto errorReturn; + } } else { CHECK_CONDITION( pRecip->m_pkey != NULL, COSE_ERR_INVALID_PARAMETER); @@ -483,8 +502,9 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, cn->v.bytes, cn->length * 8, cnBody->v.bytes, cnBody->length, - pbKeyOut, &x, perr)) + pbKeyOut, &x, perr)) { goto errorReturn; + } } break; #endif @@ -494,8 +514,9 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, if (pbKeyX != NULL) { int x = cbitKeyOut / 8; if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, pbKeyX, cbitKeyX, - cnBody->v.bytes, cnBody->length, pbKeyOut, &x, perr)) + cnBody->v.bytes, cnBody->length, pbKeyOut, &x, perr)) { goto errorReturn; + } } else { CHECK_CONDITION( pRecip->m_pkey != NULL, COSE_ERR_INVALID_PARAMETER); @@ -506,8 +527,9 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, cn->v.bytes, cn->length * 8, cnBody->v.bytes, cnBody->length, - pbKeyOut, &x, perr)) + pbKeyOut, &x, perr)) { goto errorReturn; + } } break; #endif @@ -517,8 +539,9 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, if (pbKeyX != NULL) { int x = cbitKeyOut / 8; if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, pbKeyX, cbitKeyX, - cnBody->v.bytes, cnBody->length, pbKeyOut, &x, perr)) + cnBody->v.bytes, cnBody->length, pbKeyOut, &x, perr)) { goto errorReturn; + } } else { CHECK_CONDITION( pRecip->m_pkey != NULL, COSE_ERR_INVALID_PARAMETER); @@ -529,8 +552,9 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, cn->v.bytes, cn->length * 8, cnBody->v.bytes, cnBody->length, - pbKeyOut, &x, perr)) + pbKeyOut, &x, perr)) { goto errorReturn; + } } break; #endif @@ -539,8 +563,9 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, case COSE_Algorithm_Direct_HKDF_HMAC_SHA_256: if (!HKDF_X(&pcose->m_message, true, false, false, false, algIn, pRecip->m_pkey, NULL, pbKeyOut, cbitKeyOut, 256, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } break; #endif @@ -548,8 +573,9 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, case COSE_Algorithm_Direct_HKDF_HMAC_SHA_512: if (!HKDF_X(&pcose->m_message, true, false, false, false, algIn, pRecip->m_pkey, NULL, pbKeyOut, cbitKeyOut, 512, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } break; #endif @@ -557,8 +583,9 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, case COSE_Algorithm_Direct_HKDF_AES_128: if (!HKDF_X(&pcose->m_message, false, false, false, false, algIn, pRecip->m_pkey, NULL, pbKeyOut, cbitKeyOut, 128, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } break; #endif @@ -566,8 +593,9 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, case COSE_Algorithm_Direct_HKDF_AES_256: if (!HKDF_X(&pcose->m_message, false, false, false, false, algIn, pRecip->m_pkey, NULL, pbKeyOut, cbitKeyOut, 256, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } break; #endif @@ -575,8 +603,9 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, case COSE_Algorithm_ECDH_ES_HKDF_256: if (!HKDF_X(&pcose->m_message, true, true, false, false, algIn, pRecip->m_pkey, NULL, pbKeyOut, cbitKeyOut, 256, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } break; #endif @@ -584,8 +613,9 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, case COSE_Algorithm_ECDH_ES_HKDF_512: if (!HKDF_X(&pcose->m_message, true, true, false, false, algIn, pRecip->m_pkey, NULL, pbKeyOut, cbitKeyOut, 512, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } break; #endif @@ -593,8 +623,9 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, case COSE_Algorithm_ECDH_SS_HKDF_256: if (!HKDF_X(&pcose->m_message, true, true, true, false, algIn, pRecip->m_pkey, NULL, pbKeyOut, cbitKeyOut, 256, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } break; #endif @@ -602,8 +633,9 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, case COSE_Algorithm_ECDH_SS_HKDF_512: if (!HKDF_X(&pcose->m_message, true, true, true, false, algIn, pRecip->m_pkey, NULL, pbKeyOut, cbitKeyOut, 512, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } break; #endif @@ -611,12 +643,14 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, case COSE_Algorithm_ECDH_ES_A128KW: if (!HKDF_X(&pcose->m_message, true, true, false, false, COSE_Algorithm_AES_KW_128, pRecip->m_pkey, NULL, rgbKey, - 128, 256, CBOR_CONTEXT_PARAM_COMMA perr)) + 128, 256, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, rgbKey, 128, - cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) + cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) { goto errorReturn; + } break; #endif @@ -625,12 +659,14 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, case COSE_Algorithm_ECDH_ES_A192KW: if (!HKDF_X(&pcose->m_message, true, true, false, false, COSE_Algorithm_AES_KW_192, pRecip->m_pkey, NULL, rgbKey, - 192, 256, CBOR_CONTEXT_PARAM_COMMA perr)) + 192, 256, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, rgbKey, 192, - cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) + cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) { goto errorReturn; + } break; #endif @@ -639,12 +675,14 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, case COSE_Algorithm_ECDH_ES_A256KW: if (!HKDF_X(&pcose->m_message, true, true, false, false, COSE_Algorithm_AES_KW_256, pRecip->m_pkey, NULL, rgbKey, - 256, 256, CBOR_CONTEXT_PARAM_COMMA perr)) + 256, 256, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, rgbKey, 256, - cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) + cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) { goto errorReturn; + } break; #endif @@ -653,12 +691,14 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, case COSE_Algorithm_ECDH_SS_A128KW: if (!HKDF_X(&pcose->m_message, true, true, true, false, COSE_Algorithm_AES_KW_128, pRecip->m_pkey, NULL, rgbKey, - 128, 256, CBOR_CONTEXT_PARAM_COMMA perr)) + 128, 256, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, rgbKey, 128, - cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) + cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) { goto errorReturn; + } break; #endif @@ -667,12 +707,14 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, case COSE_Algorithm_ECDH_SS_A192KW: if (!HKDF_X(&pcose->m_message, true, true, true, false, COSE_Algorithm_AES_KW_192, pRecip->m_pkey, NULL, rgbKey, - 192, 256, CBOR_CONTEXT_PARAM_COMMA perr)) + 192, 256, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, rgbKey, 192, - cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) + cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) { goto errorReturn; + } break; #endif @@ -681,12 +723,14 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, case COSE_Algorithm_ECDH_SS_A256KW: if (!HKDF_X(&pcose->m_message, true, true, true, false, COSE_Algorithm_AES_KW_256, pRecip->m_pkey, NULL, rgbKey, - 256, 256, CBOR_CONTEXT_PARAM_COMMA perr)) + 256, 256, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, rgbKey, 256, - cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) + cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) { goto errorReturn; + } break; #endif @@ -728,8 +772,9 @@ bool _COSE_Recipient_encrypt(COSE_RecipientInfo *pRecipient, cn_Alg = _COSE_map_get_int(&pRecipient->m_encrypt.m_message, COSE_Header_Algorithm, COSE_BOTH, perr); - if (cn_Alg == NULL) + if (cn_Alg == NULL) { goto errorReturn; + } CHECK_CONDITION(cn_Alg->type != CN_CBOR_TEXT, COSE_ERR_UNKNOWN_ALGORITHM); CHECK_CONDITION( @@ -840,8 +885,9 @@ bool _COSE_Recipient_encrypt(COSE_RecipientInfo *pRecipient, t |= 1; pbKey = _COSE_RecipientInfo_generateKey(pri, alg, cbitKey, perr); - if (pbKey == NULL) + if (pbKey == NULL) { goto errorReturn; + } cbKey = cbitKey / 8; } else { t |= 2; @@ -865,14 +911,16 @@ bool _COSE_Recipient_encrypt(COSE_RecipientInfo *pRecipient, const cn_cbor *cbProtected = _COSE_encode_protected(&pRecipient->m_encrypt.m_message, perr); - if (cbProtected == NULL) + if (cbProtected == NULL) { goto errorReturn; + } // Build authenticated data size_t cbAuthData = 0; if (!_COSE_Encrypt_Build_AAD(&pRecipient->m_encrypt.m_message, &pbAuthData, - &cbAuthData, "Recipient", perr)) + &cbAuthData, "Recipient", perr)) { goto errorReturn; + } switch (alg) { case COSE_Algorithm_Direct: @@ -917,12 +965,14 @@ bool _COSE_Recipient_encrypt(COSE_RecipientInfo *pRecipient, cn_cbor *pK = cn_cbor_mapget_int(pRecipient->m_pkey, -1); CHECK_CONDITION(pK != NULL, COSE_ERR_INVALID_PARAMETER); if (!AES_KW_Encrypt(pRecipient, pK->v.bytes, - (int)pK->length * 8, pbContent, (int)cbContent, perr)) + (int)pK->length * 8, pbContent, (int)cbContent, perr)) { goto errorReturn; + } } else { if (!AES_KW_Encrypt(pRecipient, pbKey, (int)cbKey * 8, - pbContent, (int)cbContent, perr)) + pbContent, (int)cbContent, perr)) { goto errorReturn; + } } break; #endif @@ -933,12 +983,14 @@ bool _COSE_Recipient_encrypt(COSE_RecipientInfo *pRecipient, cn_cbor *pK = cn_cbor_mapget_int(pRecipient->m_pkey, -1); CHECK_CONDITION(pK != NULL, COSE_ERR_INVALID_PARAMETER); if (!AES_KW_Encrypt(pRecipient, pK->v.bytes, - (int)pK->length * 8, pbContent, (int)cbContent, perr)) + (int)pK->length * 8, pbContent, (int)cbContent, perr)) { goto errorReturn; + } } else { if (!AES_KW_Encrypt(pRecipient, pbKey, (int)cbKey * 8, - pbContent, (int)cbContent, perr)) + pbContent, (int)cbContent, perr)) { goto errorReturn; + } } break; #endif @@ -949,12 +1001,14 @@ bool _COSE_Recipient_encrypt(COSE_RecipientInfo *pRecipient, cn_cbor *pK = cn_cbor_mapget_int(pRecipient->m_pkey, -1); CHECK_CONDITION(pK != NULL, COSE_ERR_INVALID_PARAMETER); if (!AES_KW_Encrypt(pRecipient, pK->v.bytes, - (int)pK->length * 8, pbContent, (int)cbContent, perr)) + (int)pK->length * 8, pbContent, (int)cbContent, perr)) { goto errorReturn; + } } else { if (!AES_KW_Encrypt(pRecipient, pbKey, (int)cbKey * 8, - pbContent, (int)cbContent, perr)) + pbContent, (int)cbContent, perr)) { goto errorReturn; + } } break; #endif @@ -963,11 +1017,13 @@ bool _COSE_Recipient_encrypt(COSE_RecipientInfo *pRecipient, case COSE_Algorithm_ECDH_ES_A128KW: if (!HKDF_X(&pRecipient->m_encrypt.m_message, true, true, false, true, COSE_Algorithm_AES_KW_128, NULL, pRecipient->m_pkey, - rgbKey, 128, 256, CBOR_CONTEXT_PARAM_COMMA perr)) + rgbKey, 128, 256, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } if (!AES_KW_Encrypt( - pRecipient, rgbKey, 128, pbContent, (int)cbContent, perr)) + pRecipient, rgbKey, 128, pbContent, (int)cbContent, perr)) { goto errorReturn; + } break; #endif @@ -975,11 +1031,13 @@ bool _COSE_Recipient_encrypt(COSE_RecipientInfo *pRecipient, case COSE_Algorithm_ECDH_ES_A192KW: if (!HKDF_X(&pRecipient->m_encrypt.m_message, true, true, false, true, COSE_Algorithm_AES_KW_192, NULL, pRecipient->m_pkey, - rgbKey, 192, 256, CBOR_CONTEXT_PARAM_COMMA perr)) + rgbKey, 192, 256, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } if (!AES_KW_Encrypt( - pRecipient, rgbKey, 192, pbContent, (int)cbContent, perr)) + pRecipient, rgbKey, 192, pbContent, (int)cbContent, perr)) { goto errorReturn; + } break; #endif @@ -987,11 +1045,13 @@ bool _COSE_Recipient_encrypt(COSE_RecipientInfo *pRecipient, case COSE_Algorithm_ECDH_ES_A256KW: if (!HKDF_X(&pRecipient->m_encrypt.m_message, true, true, false, true, COSE_Algorithm_AES_KW_256, NULL, pRecipient->m_pkey, - rgbKey, 256, 256, CBOR_CONTEXT_PARAM_COMMA perr)) + rgbKey, 256, 256, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } if (!AES_KW_Encrypt( - pRecipient, rgbKey, 256, pbContent, (int)cbContent, perr)) + pRecipient, rgbKey, 256, pbContent, (int)cbContent, perr)) { goto errorReturn; + } break; #endif @@ -1000,11 +1060,13 @@ bool _COSE_Recipient_encrypt(COSE_RecipientInfo *pRecipient, if (!HKDF_X(&pRecipient->m_encrypt.m_message, true, true, true, true, COSE_Algorithm_AES_KW_128, pRecipient->m_pkeyStatic, pRecipient->m_pkey, rgbKey, 128, 256, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } if (!AES_KW_Encrypt( - pRecipient, rgbKey, 128, pbContent, (int)cbContent, perr)) + pRecipient, rgbKey, 128, pbContent, (int)cbContent, perr)) { goto errorReturn; + } break; #endif @@ -1013,11 +1075,13 @@ bool _COSE_Recipient_encrypt(COSE_RecipientInfo *pRecipient, if (!HKDF_X(&pRecipient->m_encrypt.m_message, true, true, true, true, COSE_Algorithm_AES_KW_192, pRecipient->m_pkeyStatic, pRecipient->m_pkey, rgbKey, 192, 256, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } if (!AES_KW_Encrypt( - pRecipient, rgbKey, 192, pbContent, (int)cbContent, perr)) + pRecipient, rgbKey, 192, pbContent, (int)cbContent, perr)) { goto errorReturn; + } break; #endif @@ -1026,11 +1090,13 @@ bool _COSE_Recipient_encrypt(COSE_RecipientInfo *pRecipient, if (!HKDF_X(&pRecipient->m_encrypt.m_message, true, true, true, true, COSE_Algorithm_AES_KW_256, pRecipient->m_pkeyStatic, pRecipient->m_pkey, rgbKey, 256, 256, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } if (!AES_KW_Encrypt( - pRecipient, rgbKey, 256, pbContent, (int)cbContent, perr)) + pRecipient, rgbKey, 256, pbContent, (int)cbContent, perr)) { goto errorReturn; + } break; #endif @@ -1040,8 +1106,9 @@ bool _COSE_Recipient_encrypt(COSE_RecipientInfo *pRecipient, for (pri = pRecipient->m_encrypt.m_recipientFirst; pri != NULL; pri = pri->m_recipientNext) { - if (!_COSE_Recipient_encrypt(pri, pbKey, cbKey, perr)) + if (!_COSE_Recipient_encrypt(pri, pbKey, cbKey, perr)) { goto errorReturn; + } } // Figure out the clean up @@ -1054,14 +1121,18 @@ bool _COSE_Recipient_encrypt(COSE_RecipientInfo *pRecipient, memset(pbKey, 0, cbKey); COSE_FREE(pbKey, context); } - if (pbSecret != NULL) + if (pbSecret != NULL) { COSE_FREE(pbSecret, context); - if (pbContext != NULL) + } + if (pbContext != NULL) { COSE_FREE(pbContext, context); - if (pbAuthData != NULL) + } + if (pbAuthData != NULL) { COSE_FREE(pbAuthData, context); - if (ptmp != NULL) + } + if (ptmp != NULL) { cn_cbor_free(ptmp CBOR_CONTEXT_PARAM); + } return fRet; } @@ -1108,8 +1179,9 @@ byte *_COSE_RecipientInfo_generateKey(COSE_RecipientInfo *pRecipient, case COSE_Algorithm_Direct_HKDF_HMAC_SHA_256: if (!HKDF_X(&pRecipient->m_encrypt.m_message, true, false, false, true, algIn, pRecipient->m_pkey, NULL, pb, cbitKeySize, 256, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } break; #endif @@ -1117,8 +1189,9 @@ byte *_COSE_RecipientInfo_generateKey(COSE_RecipientInfo *pRecipient, case COSE_Algorithm_Direct_HKDF_HMAC_SHA_512: if (!HKDF_X(&pRecipient->m_encrypt.m_message, true, false, false, true, algIn, pRecipient->m_pkey, NULL, pb, cbitKeySize, 512, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } break; #endif @@ -1126,8 +1199,9 @@ byte *_COSE_RecipientInfo_generateKey(COSE_RecipientInfo *pRecipient, case COSE_Algorithm_Direct_HKDF_AES_128: if (!HKDF_X(&pRecipient->m_encrypt.m_message, false, false, false, true, algIn, pRecipient->m_pkey, NULL, pb, cbitKeySize, 128, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } break; #endif @@ -1135,8 +1209,9 @@ byte *_COSE_RecipientInfo_generateKey(COSE_RecipientInfo *pRecipient, case COSE_Algorithm_Direct_HKDF_AES_256: if (!HKDF_X(&pRecipient->m_encrypt.m_message, false, false, false, true, algIn, pRecipient->m_pkey, NULL, pb, cbitKeySize, 256, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } break; #endif @@ -1144,8 +1219,9 @@ byte *_COSE_RecipientInfo_generateKey(COSE_RecipientInfo *pRecipient, case COSE_Algorithm_ECDH_ES_HKDF_256: if (!HKDF_X(&pRecipient->m_encrypt.m_message, true, true, false, true, algIn, NULL, pRecipient->m_pkey, pb, cbitKeySize, 256, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } break; #endif @@ -1153,8 +1229,9 @@ byte *_COSE_RecipientInfo_generateKey(COSE_RecipientInfo *pRecipient, case COSE_Algorithm_ECDH_ES_HKDF_512: if (!HKDF_X(&pRecipient->m_encrypt.m_message, true, true, false, true, algIn, NULL, pRecipient->m_pkey, pb, cbitKeySize, 512, - CBOR_CONTEXT_PARAM_COMMA perr)) + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } break; #endif @@ -1162,8 +1239,9 @@ byte *_COSE_RecipientInfo_generateKey(COSE_RecipientInfo *pRecipient, case COSE_Algorithm_ECDH_SS_HKDF_256: if (!HKDF_X(&pRecipient->m_encrypt.m_message, true, true, true, true, algIn, pRecipient->m_pkeyStatic, pRecipient->m_pkey, - pb, cbitKeySize, 256, CBOR_CONTEXT_PARAM_COMMA perr)) + pb, cbitKeySize, 256, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } break; #endif @@ -1171,8 +1249,9 @@ byte *_COSE_RecipientInfo_generateKey(COSE_RecipientInfo *pRecipient, case COSE_Algorithm_ECDH_SS_HKDF_512: if (!HKDF_X(&pRecipient->m_encrypt.m_message, true, true, true, true, algIn, pRecipient->m_pkeyStatic, pRecipient->m_pkey, - pb, cbitKeySize, 512, CBOR_CONTEXT_PARAM_COMMA perr)) + pb, cbitKeySize, 512, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } break; #endif @@ -1180,20 +1259,25 @@ byte *_COSE_RecipientInfo_generateKey(COSE_RecipientInfo *pRecipient, FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER); } - if (pbSecret != NULL) + if (pbSecret != NULL) { COSE_FREE(pbSecret, context); - if (pbContext != NULL) + } + if (pbContext != NULL) { COSE_FREE(pbContext, context); + } return pb; errorReturn: - if (pbSecret != NULL) + if (pbSecret != NULL) { COSE_FREE(pbSecret, context); - if (pbContext != NULL) + } + if (pbContext != NULL) { COSE_FREE(pbContext, context); - if (pb != NULL) + } + if (pb != NULL) { COSE_FREE(pb, context); + } return NULL; } #endif @@ -1237,8 +1321,9 @@ bool COSE_Recipient_SetKey_secret(HCOSE_RECIPIENT hRecipient, COSE_Algorithm_Direct, CBOR_CONTEXT_PARAM_COMMA & cbor_error); CHECK_CONDITION_CBOR(cn_Temp != NULL, cbor_error); if (!COSE_Recipient_map_put_int(hRecipient, COSE_Header_Algorithm, - cn_Temp, COSE_UNPROTECT_ONLY, perr)) + cn_Temp, COSE_UNPROTECT_ONLY, perr)) { goto errorReturn; + } cn_Temp = NULL; } @@ -1253,8 +1338,9 @@ bool COSE_Recipient_SetKey_secret(HCOSE_RECIPIENT hRecipient, pbTemp = NULL; if (!COSE_Recipient_map_put_int( - hRecipient, COSE_Header_KID, cnTemp, COSE_UNPROTECT_ONLY, perr)) + hRecipient, COSE_Header_KID, cnTemp, COSE_UNPROTECT_ONLY, perr)) { goto errorReturn; + } } pbKey = (byte *)COSE_CALLOC(cbKey, 1, context); @@ -1281,21 +1367,26 @@ bool COSE_Recipient_SetKey_secret(HCOSE_RECIPIENT hRecipient, cbor_error); cnTemp = NULL; - if (!COSE_Recipient_SetKey(hRecipient, cn_Temp, perr)) + if (!COSE_Recipient_SetKey(hRecipient, cn_Temp, perr)) { goto errorReturn; + } cn_Temp = NULL; return true; errorReturn: - if (cn_Temp != NULL) + if (cn_Temp != NULL) { CN_CBOR_FREE(cn_Temp, context); - if (cnTemp != NULL) + } + if (cnTemp != NULL) { CN_CBOR_FREE(cnTemp, context); - if (pbTemp != NULL) + } + if (pbTemp != NULL) { COSE_FREE(pbTemp, context); - if (pbKey != NULL) + } + if (pbKey != NULL) { COSE_FREE(pbKey, context); + } return false; } @@ -1420,10 +1511,12 @@ bool COSE_Recipient_SetSenderKey(HCOSE_RECIPIENT h, f = true; errorReturn: - if (cn2 != NULL) + if (cn2 != NULL) { CN_CBOR_FREE(cn2, context); - if (cn3 != NULL) + } + if (cn3 != NULL) { CN_CBOR_FREE(cn3, context); + } return f; } @@ -1449,8 +1542,9 @@ bool COSE_Recipient_SetExternal(HCOSE_RECIPIENT hcose, cose_errback *perr) { if (!IsValidRecipientHandle(hcose)) { - if (perr != NULL) + if (perr != NULL) { perr->err = COSE_ERR_INVALID_HANDLE; + } return false; } @@ -1469,8 +1563,9 @@ bool COSE_Recipient_map_put_int(HCOSE_RECIPIENT h, CHECK_CONDITION(value != NULL, COSE_ERR_INVALID_PARAMETER); if (!_COSE_map_put(&((COSE_RecipientInfo *)h)->m_encrypt.m_message, key, - value, flags, perr)) + value, flags, perr)) { return false; + } if (key == COSE_Header_Algorithm) { if (value->type == CN_CBOR_INT) { @@ -1551,8 +1646,9 @@ static bool BuildContextBytes(COSE *pcose, cnParam = _COSE_map_get_int(pcose, COSE_Header_KDF_U_name, COSE_BOTH, perr); if (cnParam != NULL) { cnT = cn_cbor_clone(cnParam, CBOR_CONTEXT_PARAM_COMMA & cbor_error); - } else + } else { cnT = cn_cbor_null_create(CBOR_CONTEXT_PARAM_COMMA & cbor_error); + } CHECK_CONDITION_CBOR(cnT != NULL, cbor_error); CHECK_CONDITION_CBOR( cn_cbor_array_append(cnArrayT, cnT, &cbor_error), cbor_error); @@ -1563,8 +1659,9 @@ static bool BuildContextBytes(COSE *pcose, _COSE_map_get_int(pcose, COSE_Header_KDF_U_nonce, COSE_BOTH, perr); if (cnParam != NULL) { cnT = cn_cbor_clone(cnParam, CBOR_CONTEXT_PARAM_COMMA & cbor_error); - } else + } else { cnT = cn_cbor_null_create(CBOR_CONTEXT_PARAM_COMMA & cbor_error); + } CHECK_CONDITION_CBOR(cnT != NULL, cbor_error); CHECK_CONDITION_CBOR( cn_cbor_array_append(cnArrayT, cnT, &cbor_error), cbor_error); @@ -1575,8 +1672,9 @@ static bool BuildContextBytes(COSE *pcose, _COSE_map_get_int(pcose, COSE_Header_KDF_U_other, COSE_BOTH, perr); if (cnParam != NULL) { cnT = cn_cbor_clone(cnParam, CBOR_CONTEXT_PARAM_COMMA & cbor_error); - } else + } else { cnT = cn_cbor_null_create(CBOR_CONTEXT_PARAM_COMMA & cbor_error); + } CHECK_CONDITION_CBOR(cnT != NULL, cbor_error); CHECK_CONDITION_CBOR( cn_cbor_array_append(cnArrayT, cnT, &cbor_error), cbor_error); @@ -1593,8 +1691,9 @@ static bool BuildContextBytes(COSE *pcose, cnParam = _COSE_map_get_int(pcose, COSE_Header_KDF_V_name, COSE_BOTH, perr); if (cnParam != NULL) { cnT = cn_cbor_clone(cnParam, CBOR_CONTEXT_PARAM_COMMA & cbor_error); - } else + } else { cnT = cn_cbor_null_create(CBOR_CONTEXT_PARAM_COMMA & cbor_error); + } CHECK_CONDITION_CBOR(cnT != NULL, cbor_error); CHECK_CONDITION_CBOR( cn_cbor_array_append(cnArrayT, cnT, &cbor_error), cbor_error); @@ -1605,8 +1704,9 @@ static bool BuildContextBytes(COSE *pcose, _COSE_map_get_int(pcose, COSE_Header_KDF_V_nonce, COSE_BOTH, perr); if (cnParam != NULL) { cnT = cn_cbor_clone(cnParam, CBOR_CONTEXT_PARAM_COMMA & cbor_error); - } else + } else { cnT = cn_cbor_null_create(CBOR_CONTEXT_PARAM_COMMA & cbor_error); + } CHECK_CONDITION_CBOR(cnT != NULL, cbor_error); CHECK_CONDITION_CBOR( cn_cbor_array_append(cnArrayT, cnT, &cbor_error), cbor_error); @@ -1617,8 +1717,9 @@ static bool BuildContextBytes(COSE *pcose, _COSE_map_get_int(pcose, COSE_Header_KDF_V_other, COSE_BOTH, perr); if (cnParam != NULL) { cnT = cn_cbor_clone(cnParam, CBOR_CONTEXT_PARAM_COMMA & cbor_error); - } else + } else { cnT = cn_cbor_null_create(CBOR_CONTEXT_PARAM_COMMA & cbor_error); + } CHECK_CONDITION_CBOR(cnT != NULL, cbor_error); CHECK_CONDITION_CBOR( cn_cbor_array_append(cnArrayT, cnT, &cbor_error), cbor_error); @@ -1687,14 +1788,18 @@ static bool BuildContextBytes(COSE *pcose, fReturn = true; returnHere: - if (pbContext != NULL) + if (pbContext != NULL) { COSE_FREE(pbContext, context); - if (pArray != NULL) + } + if (pArray != NULL) { CN_CBOR_FREE(pArray, context); - if (cnArrayT != NULL) + } + if (cnArrayT != NULL) { CN_CBOR_FREE(cnArrayT, context); - if (cnT != NULL) + } + if (cnT != NULL) { CN_CBOR_FREE(cnT, context); + } return fReturn; errorReturn: @@ -1722,8 +1827,9 @@ cn_cbor *COSE_Recipient_map_get_int(HCOSE_RECIPIENT h, cose_errback *perror) { if (!IsValidRecipientHandle(h)) { - if (perror != NULL) + if (perror != NULL) { perror->err = COSE_ERR_INVALID_HANDLE; + } return NULL; } @@ -1746,8 +1852,9 @@ HCOSE_RECIPIENT COSE_Recipient_GetRecipient(HCOSE_RECIPIENT cose, CHECK_CONDITION(p != NULL, COSE_ERR_INVALID_PARAMETER); p = p->m_recipientNext; } - if (p != NULL) + if (p != NULL) { p->m_encrypt.m_message.m_refCount++; + } errorReturn: return (HCOSE_RECIPIENT)p; @@ -1787,8 +1894,9 @@ bool COSE_Recipient_AddRecipient(HCOSE_RECIPIENT hEnc, if (!_COSE_array_replace(&pEncrypt->m_message, pRecipients, INDEX_RECIPIENTS, CBOR_CONTEXT_PARAM_COMMA & cbor_error)) { CN_CBOR_FREE(pRecipients, context); - if (perr != NULL) + if (perr != NULL) { perr->err = _MapFromCBOR(cbor_error); + } goto errorReturn; } } From 4eabe4c75593c015e43842d52a8b3629b0f4be54 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 11:25:58 -0700 Subject: [PATCH 25/38] Remove the assert as well --- dumper/dumper.c | 2 +- test/test.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dumper/dumper.c b/dumper/dumper.c index 4e800792..23feca17 100644 --- a/dumper/dumper.c +++ b/dumper/dumper.c @@ -31,7 +31,7 @@ FOO Recipients; extern FOO Signer[]; #ifdef USE_CBOR_CONTEXT -#define context NULL +void* context = NULL; #endif FOO AlgorithmMap[38] = { diff --git a/test/test.c b/test/test.c index a29bb462..4153a45a 100644 --- a/test/test.c +++ b/test/test.c @@ -461,7 +461,7 @@ bool SetAttributes(HCOSE hHandle, assert(false); break; } - assert(fRet); + // assert(fRet); } return fRet; From 00c30491d84d3fb64e2e93f9c6ef8035d25c307c Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 15:55:26 -0700 Subject: [PATCH 26/38] Try a different way to change the path --- .appveyor.yml | 3 +- src/openssl.c | 150 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 102 insertions(+), 51 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index 27d8b402..91648f19 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -29,8 +29,9 @@ install: - git clone --depth 1 git://github.com/cose-wg/Examples Examples before_build: + - set PATH=c:\OpenSSL-v11-Win64;%PATH% - cmake --version - - cmake -DCOSE_C_USE_CONTEXT=%USE_CONTEXT% -DCOSE_C_USE_MBEDTLS=%USE_EMBEDTLS% -DBUILD_SHARED_LIBS=OFF -G "Visual Studio 15 2017 Win64" . + - cmake -DCOSE_C_USE_CONTEXT=%USE_CONTEXT% -DCOSE_C_USE_MBEDTLS=%USE_EMBEDTLS% -DBUILD_SHARED_LIBS=OFF -G "Visual Studio 15 2017 Win64" . build_script: - msbuild cose-c.sln diff --git a/src/openssl.c b/src/openssl.c index c0a8fc9d..8691bf42 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -88,12 +88,14 @@ bool AES_CCM_Decrypt(COSE_Enveloped *pcose, pIV = _COSE_map_get_int(&pcose->m_message, COSE_Header_IV, COSE_BOTH, NULL); if ((pIV == NULL) || (pIV->type != CN_CBOR_BYTES)) { - if (perr != NULL) + if (perr != NULL) { perr->err = COSE_ERR_INVALID_PARAMETER; + } errorReturn: - if (rgbOut != NULL) + if (rgbOut != NULL) { COSE_FREE(rgbOut, context); + } EVP_CIPHER_CTX_free(ctx); return false; } @@ -220,8 +222,9 @@ bool AES_CCM_Encrypt(COSE_Enveloped *pcose, pbIV = NULL; if (!_COSE_map_put(&pcose->m_message, COSE_Header_IV, cbor_iv_t, - COSE_UNPROTECT_ONLY, perr)) + COSE_UNPROTECT_ONLY, perr)) { goto errorReturn; + } cbor_iv_t = NULL; } else { CHECK_CONDITION( @@ -281,14 +284,18 @@ bool AES_CCM_Encrypt(COSE_Enveloped *pcose, return true; errorReturn: - if (pbIV != NULL) + if (pbIV != NULL) { COSE_FREE(pbIV, context); - if (cbor_iv_t != NULL) + } + if (cbor_iv_t != NULL) { COSE_FREE(cbor_iv_t, context); - if (rgbOut != NULL) + } + if (rgbOut != NULL) { COSE_FREE(rgbOut, context); - if (cnTmp != NULL) + } + if (cnTmp != NULL) { COSE_FREE(cnTmp, context); + } EVP_CIPHER_CTX_free(ctx); return false; } @@ -321,12 +328,14 @@ bool AES_GCM_Decrypt(COSE_Enveloped *pcose, pIV = _COSE_map_get_int(&pcose->m_message, COSE_Header_IV, COSE_BOTH, NULL); if ((pIV == NULL) || (pIV->type != CN_CBOR_BYTES)) { - if (perr != NULL) + if (perr != NULL) { perr->err = COSE_ERR_INVALID_PARAMETER; + } errorReturn: - if (rgbOut != NULL) + if (rgbOut != NULL) { COSE_FREE(rgbOut, context); + } EVP_CIPHER_CTX_free(ctx); return false; } @@ -443,8 +452,9 @@ bool AES_GCM_Encrypt(COSE_Enveloped *pcose, pbIV = NULL; if (!_COSE_map_put(&pcose->m_message, COSE_Header_IV, cbor_iv_t, - COSE_UNPROTECT_ONLY, perr)) + COSE_UNPROTECT_ONLY, perr)) { goto errorReturn; + } cbor_iv_t = NULL; } else { CHECK_CONDITION( @@ -509,12 +519,15 @@ bool AES_GCM_Encrypt(COSE_Enveloped *pcose, return true; errorReturn: - if (pbIV != NULL) + if (pbIV != NULL) { COSE_FREE(pbIV, context); - if (cbor_iv_t != NULL) + } + if (cbor_iv_t != NULL) { COSE_FREE(cbor_iv_t, context); - if (rgbOut != NULL) + } + if (rgbOut != NULL) { COSE_FREE(rgbOut, context); + } EVP_CIPHER_CTX_free(ctx); return false; } @@ -590,10 +603,12 @@ bool AES_CBC_MAC_Create(COSE_MacMessage *pcose, return !f; errorReturn: - if (rgbOut != NULL) + if (rgbOut != NULL) { COSE_FREE(rgbOut, context); - if (cn != NULL) + } + if (cn != NULL) { CN_CBOR_FREE(cn, context); + } EVP_CIPHER_CTX_free(ctx); return false; } @@ -653,8 +668,9 @@ bool AES_CBC_MAC_Validate(COSE_MacMessage *pcose, cn_cbor *cn = _COSE_arrayget_int(&pcose->m_message, INDEX_MAC_TAG); CHECK_CONDITION(cn != NULL, COSE_ERR_CBOR); - for (i = 0; i < (unsigned int)TSize; i++) + for (i = 0; i < (unsigned int)TSize; i++) { f |= (cn->v.bytes[i] != rgbTag[i]); + } EVP_CIPHER_CTX_free(ctx); return !f; @@ -1021,10 +1037,12 @@ bool HMAC_Validate(COSE_MacMessage *pcose, cn_cbor *cn = _COSE_arrayget_int(&pcose->m_message, INDEX_MAC_TAG); CHECK_CONDITION(cn != NULL, COSE_ERR_CBOR); - if (cn->length > (int)cbOut) + if (cn->length > (int)cbOut) { return false; - for (i = 0; i < (unsigned int)TSize / 8; i++) + } + for (i = 0; i < (unsigned int)TSize / 8; i++) { f |= (cn->v.bytes[i] != rgbOut[i]); + } HMAC_CTX_free(ctx); return !f; @@ -1120,8 +1138,9 @@ EC_KEY *ECKey_From(const cn_cbor *pKey, int *cbGroup, cose_errback *perr) return pNewKey; errorReturn: - if (pNewKey != NULL) + if (pNewKey != NULL) { EC_KEY_free(pNewKey); + } return NULL; } @@ -1224,10 +1243,12 @@ cn_cbor *EC_FromKey(const EC_KEY *pKey, CBOR_CONTEXT_COMMA cose_errback *perr) p = NULL; returnHere: - if (pbOut != NULL) + if (pbOut != NULL) { COSE_FREE(pbOut, context); - if (p != NULL) + } + if (p != NULL) { CN_CBOR_FREE(p, context); + } return pkey; errorReturn: @@ -1277,12 +1298,15 @@ bool ECDSA_Sign(COSE *pSigner, eckey = ECKey_From(pKey, &cbR, perr); if (eckey == NULL) { errorReturn: - if (pbSig != NULL) + if (pbSig != NULL) { COSE_FREE(pbSig, context); - if (p != NULL) + } + if (p != NULL) { CN_CBOR_FREE(p, context); - if (eckey != NULL) + } + if (eckey != NULL) { EC_KEY_free(eckey); + } return false; } @@ -1329,8 +1353,9 @@ bool ECDSA_Sign(COSE *pSigner, pbSig = NULL; - if (eckey != NULL) + if (eckey != NULL) { EC_KEY_free(eckey); + } return true; } @@ -1361,12 +1386,15 @@ bool ECDSA_Verify(COSE *pSigner, eckey = ECKey_From(pKey, &cbR, perr); if (eckey == NULL) { errorReturn: - if (p != NULL) + if (p != NULL) { CN_CBOR_FREE(p, context); - if (eckey != NULL) + } + if (eckey != NULL) { EC_KEY_free(eckey); - if (sig != NULL) + } + if (sig != NULL) { ECDSA_SIG_free(sig); + } return false; } @@ -1403,10 +1431,12 @@ bool ECDSA_Verify(COSE *pSigner, CHECK_CONDITION(ECDSA_do_verify(rgbDigest, cbDigest, sig, eckey) == 1, COSE_ERR_CRYPTO_FAIL); - if (eckey != NULL) + if (eckey != NULL) { EC_KEY_free(eckey); - if (sig != NULL) + } + if (sig != NULL) { ECDSA_SIG_free(sig); + } return true; } @@ -1433,14 +1463,18 @@ bool EdDSA_Sign(COSE *pSigner, p = cn_cbor_mapget_int(pKeyIn, COSE_Key_OPK_Curve); if (p == NULL) { errorReturn: - if (mdCtx != NULL) + if (mdCtx != NULL) { EVP_MD_CTX_free(mdCtx); - if (keyCtx != NULL) + } + if (keyCtx != NULL) { EVP_PKEY_CTX_free(keyCtx); - if (pkey != NULL) + } + if (pkey != NULL) { EVP_PKEY_free(pkey); - if (pbSig != NULL) + } + if (pbSig != NULL) { COSE_FREE(pbSig, context); + } return false; } @@ -1494,14 +1528,18 @@ bool EdDSA_Sign(COSE *pSigner, _COSE_array_replace(pSigner, p, index, CBOR_CONTEXT_PARAM_COMMA NULL), COSE_ERR_CBOR); - if (mdCtx != NULL) + if (mdCtx != NULL) { EVP_MD_CTX_free(mdCtx); - if (keyCtx != NULL) + } + if (keyCtx != NULL) { EVP_PKEY_CTX_free(keyCtx); - if (pkey != NULL) + } + if (pkey != NULL) { EVP_PKEY_free(pkey); - if (pbSig != NULL) + } + if (pbSig != NULL) { COSE_FREE(pbSig, context); + } return true; } @@ -1519,8 +1557,9 @@ bool EdDSA_Verify(COSE *pSigner, cn_cbor *p = cn_cbor_mapget_int(pKey, COSE_Key_OPK_Curve); if (p == NULL) { errorReturn: - if (pkey != NULL) + if (pkey != NULL) { EVP_PKEY_free(pkey); + } return false; } @@ -1559,10 +1598,12 @@ bool EdDSA_Verify(COSE *pSigner, rgbToSign, cbToSign) == 1, COSE_ERR_CRYPTO_FAIL); - if (pmdCtx != NULL) + if (pmdCtx != NULL) { EVP_MD_CTX_free(pmdCtx); - if (pkey != NULL) + } + if (pkey != NULL) { EVP_PKEY_free(pkey); + } return true; } @@ -1633,8 +1674,9 @@ bool AES_KW_Encrypt(COSE_RecipientInfo *pcose, errorReturn: COSE_FREE(cnTmp, context); - if (pbOut != NULL) + if (pbOut != NULL) { COSE_FREE(pbOut, context); + } return false; } @@ -1670,17 +1712,20 @@ bool ECDH_ComputeSecret(COSE *pRecipient, bool fRet = false; peckeyPublic = ECKey_From(pKeyPublic, &cbGroup, perr); - if (peckeyPublic == NULL) + if (peckeyPublic == NULL) { goto errorReturn; + } if (*ppKeyPrivate == NULL) { { cn_cbor *pCompress = _COSE_map_get_int( pRecipient, COSE_Header_UseCompressedECDH, COSE_BOTH, perr); - if (pCompress == NULL) + if (pCompress == NULL) { FUseCompressed = false; - else + } + else { FUseCompressed = (pCompress->type == CN_CBOR_TRUE); + } } peckeyPrivate = EC_KEY_new(); EC_KEY_set_group(peckeyPrivate, EC_KEY_get0_group(peckeyPublic)); @@ -1688,12 +1733,14 @@ bool ECDH_ComputeSecret(COSE *pRecipient, EC_KEY_generate_key(peckeyPrivate) == 1, COSE_ERR_CRYPTO_FAIL); *ppKeyPrivate = EC_FromKey(peckeyPrivate, CBOR_CONTEXT_PARAM_COMMA perr); - if (*ppKeyPrivate == NULL) + if (*ppKeyPrivate == NULL) { goto errorReturn; + } } else { peckeyPrivate = ECKey_From(*ppKeyPrivate, &cbGroup, perr); - if (peckeyPrivate == NULL) + if (peckeyPrivate == NULL) { goto errorReturn; + } } pbsecret = COSE_CALLOC(cbGroup, 1, context); @@ -1710,12 +1757,15 @@ bool ECDH_ComputeSecret(COSE *pRecipient, fRet = true; errorReturn: - if (pbsecret != NULL) + if (pbsecret != NULL) { COSE_FREE(pbsecret, context); - if (peckeyPublic != NULL) + } + if (peckeyPublic != NULL) { EC_KEY_free(peckeyPublic); - if (peckeyPrivate != NULL) + } + if (peckeyPrivate != NULL) { EC_KEY_free(peckeyPrivate); + } return fRet; } From 5aaa1997b0b45212fbfc6c3164f94cff5540ba66 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 16:03:52 -0700 Subject: [PATCH 27/38] Another shot at it --- .appveyor.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.appveyor.yml b/.appveyor.yml index 91648f19..686ebc63 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -29,8 +29,11 @@ install: - git clone --depth 1 git://github.com/cose-wg/Examples Examples before_build: - - set PATH=c:\OpenSSL-v11-Win64;%PATH% + - set PATH=c:\OpenSSL-v111-Win64\bin;%PATH% + - dir c:\OpenSSL-v11-Win64\bin - cmake --version + - mkdir build + - cd build - cmake -DCOSE_C_USE_CONTEXT=%USE_CONTEXT% -DCOSE_C_USE_MBEDTLS=%USE_EMBEDTLS% -DBUILD_SHARED_LIBS=OFF -G "Visual Studio 15 2017 Win64" . build_script: From ea0e0fc1bc550814c75027a42c867c284a9484b5 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 16:09:23 -0700 Subject: [PATCH 28/38] Correct path to cmake files --- .appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.appveyor.yml b/.appveyor.yml index 686ebc63..ebf5e55e 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -34,7 +34,7 @@ before_build: - cmake --version - mkdir build - cd build - - cmake -DCOSE_C_USE_CONTEXT=%USE_CONTEXT% -DCOSE_C_USE_MBEDTLS=%USE_EMBEDTLS% -DBUILD_SHARED_LIBS=OFF -G "Visual Studio 15 2017 Win64" . + - cmake -DCOSE_C_USE_CONTEXT=%USE_CONTEXT% -DCOSE_C_USE_MBEDTLS=%USE_EMBEDTLS% -DBUILD_SHARED_LIBS=OFF -G "Visual Studio 15 2017 Win64" .. build_script: - msbuild cose-c.sln From 17409f572dbec550257501f99f0fb5dac5892ab0 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 16:14:54 -0700 Subject: [PATCH 29/38] Updte to newer image --- .appveyor.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index ebf5e55e..cf06fa53 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -1,7 +1,7 @@ platform: - x64 -image: Visual Studio 2017 +image: Visual Studio 2019 configuration: - Release @@ -30,7 +30,7 @@ install: before_build: - set PATH=c:\OpenSSL-v111-Win64\bin;%PATH% - - dir c:\OpenSSL-v11-Win64\bin + - dir c:\OpenSSL-v11-Win64 - cmake --version - mkdir build - cd build From ac5599716eb1d0fedb04b8153afa26e25e1d4a28 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 16:16:50 -0700 Subject: [PATCH 30/38] Correct cmake to new image --- .appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.appveyor.yml b/.appveyor.yml index cf06fa53..d5654981 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -34,7 +34,7 @@ before_build: - cmake --version - mkdir build - cd build - - cmake -DCOSE_C_USE_CONTEXT=%USE_CONTEXT% -DCOSE_C_USE_MBEDTLS=%USE_EMBEDTLS% -DBUILD_SHARED_LIBS=OFF -G "Visual Studio 15 2017 Win64" .. + - cmake -DCOSE_C_USE_CONTEXT=%USE_CONTEXT% -DCOSE_C_USE_MBEDTLS=%USE_EMBEDTLS% -DBUILD_SHARED_LIBS=OFF -G "Visual Studio 15 2019" .. build_script: - msbuild cose-c.sln From a12fd29bf53dda9ec814f8e9fa54104131993e84 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 16:20:08 -0700 Subject: [PATCH 31/38] aaaarg --- .appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.appveyor.yml b/.appveyor.yml index d5654981..b85404a9 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -34,7 +34,7 @@ before_build: - cmake --version - mkdir build - cd build - - cmake -DCOSE_C_USE_CONTEXT=%USE_CONTEXT% -DCOSE_C_USE_MBEDTLS=%USE_EMBEDTLS% -DBUILD_SHARED_LIBS=OFF -G "Visual Studio 15 2019" .. + - cmake -DCOSE_C_USE_CONTEXT=%USE_CONTEXT% -DCOSE_C_USE_MBEDTLS=%USE_EMBEDTLS% -DBUILD_SHARED_LIBS=OFF -G "Visual Studio 16 2019" .. build_script: - msbuild cose-c.sln From f91b7a63ffa3c4428d75999238e8763a6a2e82b0 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Mon, 27 Apr 2020 17:19:39 -0700 Subject: [PATCH 32/38] Add the rest of the missing braces. --- .appveyor.yml | 4 +- src/Cose.c | 40 ++--- src/CounterSign.c | 153 ++++++++++--------- src/CounterSign0.c | 45 ++++-- src/Encrypt.c | 46 +++--- src/Encrypt0.c | 40 +++-- src/MacMessage.c | 140 ++++++++++++------ src/MacMessage0.c | 21 ++- src/Recipient.c | 74 ++++++---- src/Sign.c | 77 ++++++---- src/Sign1.c | 111 +++++++++----- src/SignerInfo.c | 112 +++++++++----- src/cbor.c | 18 ++- src/cose_int.h | 16 +- src/mbedtls.c | 21 ++- src/openssl.c | 27 ++-- test/context.c | 20 +-- test/encrypt.c | 360 +++++++++++++++++++++++++++++---------------- test/json.c | 69 ++++++--- test/mac_test.c | 56 +++---- test/sign.c | 230 ++++++++++++++++++----------- test/test.c | 284 +++++++++++++++++++++++------------ test/test.h | 6 +- 23 files changed, 1249 insertions(+), 721 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index b85404a9..90e86656 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -11,11 +11,9 @@ environment: matrix: - USE_CONTEXT: ON USE_EMBEDTLS: OFF - CMAKE_FRAMEWORK_PATH: c:\OpenSSL-v11-Win64\bin - USE_CONTEXT: OFF USE_EMBEDTLS: OFF - CMAKE_FRAMEWORK_PATH: c:\OpenSSL-v11-Win64\bin - USE_CONTEXT: OFF USE_EMBEDTLS: ON @@ -30,7 +28,7 @@ install: before_build: - set PATH=c:\OpenSSL-v111-Win64\bin;%PATH% - - dir c:\OpenSSL-v11-Win64 + - set OPENSSL_ROOT_DIR=c:\OpenSSL-v111-Win64 - cmake --version - mkdir build - cd build diff --git a/src/Cose.c b/src/Cose.c index 78d150d8..e5214eb6 100644 --- a/src/Cose.c +++ b/src/Cose.c @@ -109,7 +109,8 @@ bool _COSE_Init_From_Object(COSE *pobj, pobj->m_protectedMap = cn_cbor_map_create(CBOR_CONTEXT_PARAM_COMMA NULL); CHECK_CONDITION(pobj->m_protectedMap, COSE_ERR_OUT_OF_MEMORY); - } else { + } + else { pobj->m_protectedMap = cn_cbor_decode((const byte *)pmap->v.str, pmap->length, CBOR_CONTEXT_PARAM_COMMA & errState); CHECK_CONDITION( @@ -128,22 +129,25 @@ bool _COSE_Init_From_Object(COSE *pobj, CHECK_CONDITION_CBOR(pobj->m_dontSendMap != NULL, cbor_error); #if INCLUDE_COUNTERSIGNATURE - cn_cbor* pCounter = cn_cbor_mapget_int(pobj->m_unprotectMap, COSE_Header_CounterSign); + cn_cbor *pCounter = + cn_cbor_mapget_int(pobj->m_unprotectMap, COSE_Header_CounterSign); if (pCounter != NULL) { int i; - CHECK_CONDITION(pCounter->type == CN_CBOR_ARRAY, COSE_ERR_INVALID_PARAMETER); + CHECK_CONDITION( + pCounter->type == CN_CBOR_ARRAY, COSE_ERR_INVALID_PARAMETER); CHECK_CONDITION(pCounter->length > 0, COSE_ERR_INVALID_PARAMETER); if (pCounter->first_child->type == CN_CBOR_ARRAY) { - - cn_cbor* pSig = pCounter->first_child; + cn_cbor *pSig = pCounter->first_child; for (i = 0; i < pCounter->length; i++, pSig = pSig->next) { - COSE_CounterSign* cs = _COSE_CounterSign_Init_From_Object(pSig, NULL, CBOR_CONTEXT_PARAM_COMMA perr); + COSE_CounterSign *cs = _COSE_CounterSign_Init_From_Object( + pSig, NULL, CBOR_CONTEXT_PARAM_COMMA perr); cs->m_next = pobj->m_counterSigners; pobj->m_counterSigners = cs; } } else { - COSE_CounterSign* cs = _COSE_CounterSign_Init_From_Object(pCounter, NULL, CBOR_CONTEXT_PARAM_COMMA perr); + COSE_CounterSign *cs = _COSE_CounterSign_Init_From_Object( + pCounter, NULL, CBOR_CONTEXT_PARAM_COMMA perr); pobj->m_counterSigners = cs; } } @@ -180,8 +184,8 @@ void _COSE_Release(COSE *pcose) #if INCLUDE_COUNTERSIGNATURE if (pcose->m_counterSigners != NULL) { - COSE_CounterSign* p = pcose->m_counterSigners; - COSE_CounterSign* p2 = NULL; + COSE_CounterSign *p = pcose->m_counterSigners; + COSE_CounterSign *p2 = NULL; while (p != NULL) { p2 = p->m_next; @@ -214,14 +218,16 @@ HCOSE COSE_Decode(const byte *rgbData, if (struct_type != 0) { CHECK_CONDITION(struct_type == (COSE_object_type)cbor->v.sint, COSE_ERR_INVALID_PARAMETER); - } else { + } + else { struct_type = cbor->v.uint; } *ptype = struct_type; cbor = cbor->first_child; - } else { + } + else { *ptype = struct_type; } @@ -342,10 +348,7 @@ bool _COSE_SetExternal(COSE *pcose, return true; } -cn_cbor *_COSE_map_get_int(COSE *cose, - int key, - int flags, - cose_errback *perr) +cn_cbor *_COSE_map_get_int(COSE *cose, int key, int flags, cose_errback *perr) { cn_cbor *p = NULL; @@ -482,7 +485,8 @@ cn_cbor *_COSE_encode_protected(COSE *pMessage, cose_errback *perr) CHECK_CONDITION(cn_cbor_encoder_write(pbProtected, 0, cbProtected, pMessage->m_protectedMap) == cbProtected, COSE_ERR_CBOR); - } else { + } + else { cbProtected = 0; } @@ -498,7 +502,6 @@ cn_cbor *_COSE_encode_protected(COSE *pMessage, cose_errback *perr) return pProtected; } - bool _COSE_array_replace(COSE *pMessage, cn_cbor *cb_value, int index, @@ -564,7 +567,8 @@ void _COSE_RemoveFromList(COSE **rootNode, COSE *thisMsg) return; } - for (COSE *walk = *rootNode; walk->m_handleList != NULL; walk = walk->m_handleList) { + for (COSE *walk = *rootNode; walk->m_handleList != NULL; + walk = walk->m_handleList) { if (walk->m_handleList == thisMsg) { walk->m_handleList = thisMsg->m_handleList; thisMsg->m_handleList = NULL; diff --git a/src/CounterSign.c b/src/CounterSign.c index 8f8f1320..ad9b8d7e 100644 --- a/src/CounterSign.c +++ b/src/CounterSign.c @@ -34,22 +34,27 @@ bool _COSE_CounterSign_Free(COSE_CounterSign* pSigner) return true; } -COSE_CounterSign * _COSE_CounterSign_Init_From_Object(cn_cbor* cbor, COSE_CounterSign * pIn, CBOR_CONTEXT_COMMA cose_errback* perr) +COSE_CounterSign* _COSE_CounterSign_Init_From_Object(cn_cbor* cbor, + COSE_CounterSign* pIn, + CBOR_CONTEXT_COMMA cose_errback* perr) { COSE_CounterSign* pobj = pIn; - cose_errback error = { 0 }; - if (perr == NULL) perr = &error; + cose_errback error = {0}; + if (perr == NULL) + perr = &error; if (pobj == NULL) { - pobj = (COSE_CounterSign*)COSE_CALLOC(1, sizeof(COSE_CounterSign), context); + pobj = (COSE_CounterSign*)COSE_CALLOC( + 1, sizeof(COSE_CounterSign), context); CHECK_CONDITION(pobj != NULL, COSE_ERR_OUT_OF_MEMORY); } CHECK_CONDITION(cbor->type == CN_CBOR_ARRAY, COSE_ERR_INVALID_PARAMETER); - if (!_COSE_SignerInfo_Init_From_Object(cbor, &pobj->m_signer, CBOR_CONTEXT_PARAM_COMMA perr)) { + if (!_COSE_SignerInfo_Init_From_Object( + cbor, &pobj->m_signer, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; - } + } if (pIn == NULL) { _COSE_InsertInList(&CountersignRoot, &pobj->m_signer.m_message); @@ -64,18 +69,22 @@ COSE_CounterSign * _COSE_CounterSign_Init_From_Object(cn_cbor* cbor, COSE_Counte return NULL; } -bool _COSE_CounterSign_Init(COSE_CounterSign* pobject, CBOR_CONTEXT_COMMA cose_errback* perror) +bool _COSE_CounterSign_Init(COSE_CounterSign* pobject, + CBOR_CONTEXT_COMMA cose_errback* perror) { - return _COSE_SignerInfo_Init(COSE_INIT_FLAGS_NO_CBOR_TAG, &pobject->m_signer, COSE_countersign_object, CBOR_CONTEXT_PARAM_COMMA perror); + return _COSE_SignerInfo_Init(COSE_INIT_FLAGS_NO_CBOR_TAG, + &pobject->m_signer, COSE_countersign_object, + CBOR_CONTEXT_PARAM_COMMA perror); } HCOSE_COUNTERSIGN COSE_CounterSign_Init(CBOR_CONTEXT_COMMA cose_errback* perror) { - COSE_CounterSign* pobject = (COSE_CounterSign*)COSE_CALLOC(1, sizeof(COSE_CounterSign), context); + COSE_CounterSign* pobject = + (COSE_CounterSign*)COSE_CALLOC(1, sizeof(COSE_CounterSign), context); if (pobject == NULL) { - if (perror != NULL) { + if (perror != NULL) { perror->err = COSE_ERR_OUT_OF_MEMORY; - } + } return NULL; } @@ -85,10 +94,9 @@ HCOSE_COUNTERSIGN COSE_CounterSign_Init(CBOR_CONTEXT_COMMA cose_errback* perror) } _COSE_InsertInList(&CountersignRoot, &pobject->m_signer.m_message); - return (HCOSE_COUNTERSIGN) pobject; + return (HCOSE_COUNTERSIGN)pobject; } - bool COSE_CounterSign_Free(HCOSE_COUNTERSIGN h) { COSE_CounterSign* p = (COSE_CounterSign*)h; @@ -109,15 +117,17 @@ bool COSE_CounterSign_Free(HCOSE_COUNTERSIGN h) return fRet; } - /// Add a countersignature to the list used to create the attribute /// -bool _COSE_CounterSign_add(COSE* pMessage, HCOSE_COUNTERSIGN hSigner, cose_errback* perr) +bool _COSE_CounterSign_add(COSE* pMessage, + HCOSE_COUNTERSIGN hSigner, + cose_errback* perr) { COSE_CounterSign* pSigner = (COSE_CounterSign*)hSigner; CHECK_CONDITION(IsValidCounterSignHandle(hSigner), COSE_ERR_INVALID_HANDLE); - CHECK_CONDITION(pSigner->m_signer.m_message.m_counterSigners == NULL, COSE_ERR_INVALID_PARAMETER); + CHECK_CONDITION(pSigner->m_signer.m_message.m_counterSigners == NULL, + COSE_ERR_INVALID_PARAMETER); pSigner->m_next = pMessage->m_counterSigners; pMessage->m_counterSigners = pSigner; @@ -128,11 +138,11 @@ bool _COSE_CounterSign_add(COSE* pMessage, HCOSE_COUNTERSIGN hSigner, cose_errba return false; } - - /// Get the n-th counter signature from the attribute. /// -HCOSE_COUNTERSIGN _COSE_CounterSign_get(COSE* pMessage, int iSigner, cose_errback* perr) +HCOSE_COUNTERSIGN _COSE_CounterSign_get(COSE* pMessage, + int iSigner, + cose_errback* perr) { COSE_CounterSign* pSigner = pMessage->m_counterSigners; int i; @@ -149,10 +159,13 @@ HCOSE_COUNTERSIGN _COSE_CounterSign_get(COSE* pMessage, int iSigner, cose_errbac /// _COSE_CounterSign_create /// -/// Create the CounterSign attribute based on the set of countersignatures added to the message. +/// Create the CounterSign attribute based on the set of countersignatures added +/// to the message. /// -bool _COSE_CounterSign_create(COSE* pMessage, cn_cbor* pcnBody, CBOR_CONTEXT_COMMA cose_errback* perr) +bool _COSE_CounterSign_create(COSE* pMessage, + cn_cbor* pcnBody, + CBOR_CONTEXT_COMMA cose_errback* perr) { cn_cbor* pArray = NULL; cn_cbor_errback cbor_err; @@ -161,7 +174,8 @@ bool _COSE_CounterSign_create(COSE* pMessage, cn_cbor* pcnBody, CBOR_CONTEXT_COM cn_cbor* pcn = NULL; cn_cbor* pcn2 = NULL; - if (pMessage->m_counterSigners == NULL) return true; + if (pMessage->m_counterSigners == NULL) + return true; // One or more than one? if (pMessage->m_counterSigners->m_signer.m_signerNext != NULL) { @@ -172,21 +186,27 @@ bool _COSE_CounterSign_create(COSE* pMessage, cn_cbor* pcnBody, CBOR_CONTEXT_COM pcnProtected = _COSE_arrayget_int(pMessage, INDEX_PROTECTED); CHECK_CONDITION(pcnProtected != NULL, COSE_ERR_INTERNAL); - for (pSigner = pMessage->m_counterSigners; pSigner != NULL; pSigner = pSigner->m_next) { - CHECK_CONDITION(pSigner->m_signer.m_signerNext == NULL, COSE_ERR_INTERNAL); + for (pSigner = pMessage->m_counterSigners; pSigner != NULL; + pSigner = pSigner->m_next) { + CHECK_CONDITION( + pSigner->m_signer.m_signerNext == NULL, COSE_ERR_INTERNAL); - pcn = cn_cbor_data_create(pcnProtected->v.bytes, pcnProtected->length, CBOR_CONTEXT_PARAM_COMMA & cbor_err); + pcn = cn_cbor_data_create(pcnProtected->v.bytes, pcnProtected->length, + CBOR_CONTEXT_PARAM_COMMA & cbor_err); CHECK_CONDITION_CBOR(pcnProtected != NULL, cbor_err); pcn2 = cn_cbor_clone(pcnBody, CBOR_CONTEXT_PARAM_COMMA & cbor_err); CHECK_CONDITION_CBOR(pcnBody != NULL, cbor_err); - if (!_COSE_Signer_sign(&pSigner->m_signer, pcnBody, pcn2, "CounterSignature", perr)) goto errorReturn; + if (!_COSE_Signer_sign( + &pSigner->m_signer, pcnBody, pcn2, "CounterSignature", perr)) + goto errorReturn; pcn = NULL; pcn2 = NULL; if (pArray != NULL) { - bool f = cn_cbor_array_append(pArray, pSigner->m_signer.m_message.m_cborRoot, &cbor_err); + bool f = cn_cbor_array_append( + pArray, pSigner->m_signer.m_message.m_cborRoot, &cbor_err); CHECK_CONDITION_CBOR(f, cbor_err); } else { @@ -194,18 +214,25 @@ bool _COSE_CounterSign_create(COSE* pMessage, cn_cbor* pcnBody, CBOR_CONTEXT_COM } } - if (!_COSE_map_put(pMessage, COSE_Header_CounterSign, pArray, COSE_UNPROTECT_ONLY, perr)) goto errorReturn; + if (!_COSE_map_put(pMessage, COSE_Header_CounterSign, pArray, + COSE_UNPROTECT_ONLY, perr)) + goto errorReturn; return true; errorReturn: - if (pArray != NULL) CN_CBOR_FREE(pArray, context); - if ((pcn != NULL) && (pcn->parent != NULL)) CN_CBOR_FREE(pcn, context); - if ((pcn2 != NULL) && (pcn2->parent != NULL)) CN_CBOR_FREE(pcn2, context); + if (pArray != NULL) + CN_CBOR_FREE(pArray, context); + if ((pcn != NULL) && (pcn->parent != NULL)) + CN_CBOR_FREE(pcn, context); + if ((pcn2 != NULL) && (pcn2->parent != NULL)) + CN_CBOR_FREE(pcn2, context); return false; } -bool COSE_CounterSign_SetKey(HCOSE_COUNTERSIGN h, const cn_cbor* pkey, cose_errback* perr) +bool COSE_CounterSign_SetKey(HCOSE_COUNTERSIGN h, + const cn_cbor* pkey, + cose_errback* perr) { bool fRet = false; CHECK_CONDITION(IsValidCounterSignHandle(h), COSE_ERR_INVALID_HANDLE); @@ -219,17 +246,18 @@ bool COSE_CounterSign_SetKey(HCOSE_COUNTERSIGN h, const cn_cbor* pkey, cose_errb return fRet; } - -COSE_CounterSign* _COSE_Message_get_countersignature(COSE* pMessage, int index, cose_errback* perr) +COSE_CounterSign* _COSE_Message_get_countersignature(COSE* pMessage, + int index, + cose_errback* perr) { - CHECK_CONDITION(pMessage->m_counterSigners != NULL, COSE_ERR_INVALID_PARAMETER); + CHECK_CONDITION( + pMessage->m_counterSigners != NULL, COSE_ERR_INVALID_PARAMETER); COSE_CounterSign* pCounterSign = pMessage->m_counterSigners; for (int i = 0; i < index; i++) { pCounterSign = pCounterSign->m_next; CHECK_CONDITION(pCounterSign != NULL, COSE_ERR_INVALID_PARAMETER); - } pCounterSign->m_signer.m_message.m_refCount += 1; @@ -240,7 +268,6 @@ COSE_CounterSign* _COSE_Message_get_countersignature(COSE* pMessage, int index, return NULL; } - bool COSE_CounterSign_map_put_int(HCOSE_COUNTERSIGN h, int key, cn_cbor* value, @@ -257,7 +284,6 @@ bool COSE_CounterSign_map_put_int(HCOSE_COUNTERSIGN h, return false; } - /*! * @brief Set the application external data for authentication * @@ -289,13 +315,13 @@ bool COSE_CounterSign_SetExternal(HCOSE_COUNTERSIGN hcose, pbExternalData, cbExternalData, perr); } -bool _COSE_CounterSign_Sign(COSE * baseMessage, CBOR_CONTEXT_COMMA cose_errback * perr) +bool _COSE_CounterSign_Sign(COSE* baseMessage, + CBOR_CONTEXT_COMMA cose_errback* perr) { bool fRet = false; cn_cbor* pcborProtectedSign = NULL; - - cn_cbor* pSignature = - _COSE_arrayget_int(baseMessage, INDEX_SIGNATURE); + + cn_cbor* pSignature = _COSE_arrayget_int(baseMessage, INDEX_SIGNATURE); int count = 0; COSE_CounterSign* pCountersign = baseMessage->m_counterSigners; @@ -313,9 +339,8 @@ bool _COSE_CounterSign_Sign(COSE * baseMessage, CBOR_CONTEXT_COMMA cose_errback if (count == 1) { cn_cbor* cn = COSE_get_cbor((HCOSE)baseMessage->m_counterSigners); - CHECK_CONDITION( - _COSE_map_put(baseMessage, - COSE_Header_CounterSign, cn, COSE_UNPROTECT_ONLY, perr), + CHECK_CONDITION(_COSE_map_put(baseMessage, COSE_Header_CounterSign, cn, + COSE_UNPROTECT_ONLY, perr), COSE_ERR_OUT_OF_MEMORY); } else { @@ -324,20 +349,19 @@ bool _COSE_CounterSign_Sign(COSE * baseMessage, CBOR_CONTEXT_COMMA cose_errback cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA & cn_error); CHECK_CONDITION_CBOR(cn_counterSign, cn_error); - for (pCountersign = baseMessage->m_counterSigners; - pCountersign != NULL; pCountersign = pCountersign->m_next) { + for (pCountersign = baseMessage->m_counterSigners; pCountersign != NULL; + pCountersign = pCountersign->m_next) { cn_cbor* cn = COSE_get_cbor((HCOSE)pCountersign); CHECK_CONDITION_CBOR( cn_cbor_array_append(cn_counterSign, cn, &cn_error), cn_error); } - CHECK_CONDITION(_COSE_map_put(baseMessage, - COSE_Header_CounterSign, cn_counterSign, - COSE_UNPROTECT_ONLY, perr), + CHECK_CONDITION(_COSE_map_put(baseMessage, COSE_Header_CounterSign, + cn_counterSign, COSE_UNPROTECT_ONLY, perr), COSE_ERR_OUT_OF_MEMORY); } fRet = true; - errorReturn: +errorReturn: return fRet; } @@ -391,19 +415,18 @@ bool COSE_Signer_CounterSign_validate(HCOSE_SIGNER hSigner, COSE_SignerInfo* pSigner = (COSE_SignerInfo*)hSigner; COSE_CounterSign* pCountersign = (COSE_CounterSign*)hCountersignature; - const cn_cbor* cnContent = _COSE_arrayget_int( - &pSigner->m_message, INDEX_BODY); + const cn_cbor* cnContent = + _COSE_arrayget_int(&pSigner->m_message, INDEX_BODY); CHECK_CONDITION(cnContent != NULL && cnContent->type == CN_CBOR_BYTES, COSE_ERR_INVALID_PARAMETER); - const cn_cbor* cnProtected = _COSE_arrayget_int( - &pSigner->m_message, INDEX_PROTECTED); + const cn_cbor* cnProtected = + _COSE_arrayget_int(&pSigner->m_message, INDEX_PROTECTED); CHECK_CONDITION(cnProtected != NULL && cnProtected->type == CN_CBOR_BYTES, COSE_ERR_INVALID_PARAMETER); bool f = _COSE_Signer_validate(&pCountersign->m_signer, cnContent, - cnProtected, - "CounterSignature", perr); + cnProtected, "CounterSignature", perr); return f; @@ -411,7 +434,6 @@ bool COSE_Signer_CounterSign_validate(HCOSE_SIGNER hSigner, return false; } - /*************************************************************************************************** * * SIGN MESSAGE @@ -461,19 +483,18 @@ bool COSE_Sign_CounterSign_validate(HCOSE_SIGN hSignMsg, COSE_SignMessage* pSignMsg = (COSE_SignMessage*)hSignMsg; COSE_CounterSign* pCountersign = (COSE_CounterSign*)hCountersignature; - const cn_cbor* cnContent = _COSE_arrayget_int( - &pSignMsg->m_message, INDEX_BODY); + const cn_cbor* cnContent = + _COSE_arrayget_int(&pSignMsg->m_message, INDEX_BODY); CHECK_CONDITION(cnContent != NULL && cnContent->type == CN_CBOR_BYTES, COSE_ERR_INVALID_PARAMETER); - const cn_cbor* cnProtected = _COSE_arrayget_int( - &pSignMsg->m_message, INDEX_PROTECTED); + const cn_cbor* cnProtected = + _COSE_arrayget_int(&pSignMsg->m_message, INDEX_PROTECTED); CHECK_CONDITION(cnProtected != NULL && cnProtected->type == CN_CBOR_BYTES, COSE_ERR_INVALID_PARAMETER); bool f = _COSE_Signer_validate(&pCountersign->m_signer, cnContent, - cnProtected, - "CounterSignature", perr); + cnProtected, "CounterSignature", perr); return f; @@ -636,7 +657,8 @@ HCOSE_COUNTERSIGN COSE_Recipient_add_countersignature(HCOSE_RECIPIENT hSignMsg, IsValidCounterSignHandle(hCountersign), COSE_ERR_INVALID_HANDLE); if (!_COSE_CounterSign_add( - &((COSE_RecipientInfo*)hSignMsg)->m_encrypt.m_message, hCountersign, perr)) { + &((COSE_RecipientInfo*)hSignMsg)->m_encrypt.m_message, hCountersign, + perr)) { goto errorReturn; } @@ -903,4 +925,3 @@ bool COSE_Mac_CounterSign_validate(HCOSE_MAC hSignMsg, #endif #endif - diff --git a/src/CounterSign0.c b/src/CounterSign0.c index 41fd5a5f..dc9e4a22 100644 --- a/src/CounterSign0.c +++ b/src/CounterSign0.c @@ -12,12 +12,14 @@ extern bool IsValidCounterSign1Handle(HCOSE_COUNTERSIGN1 h); - -bool _COSE_CounterSign1_add(COSE* pMessage, HCOSE_COUNTERSIGN1 hSigner, cose_errback* perr) +bool _COSE_CounterSign1_add(COSE* pMessage, + HCOSE_COUNTERSIGN1 hSigner, + cose_errback* perr) { COSE_CounterSign1* pSigner = (COSE_CounterSign1*)hSigner; - CHECK_CONDITION(IsValidCounterSign1Handle(hSigner), COSE_ERR_INVALID_HANDLE); + CHECK_CONDITION( + IsValidCounterSign1Handle(hSigner), COSE_ERR_INVALID_HANDLE); pMessage->m_counterSign1 = pSigner; return true; @@ -35,7 +37,9 @@ HCOSE_COUNTERSIGN _COSE_CounterSign1_get(COSE* pMessage, cose_errback* perr) return (HCOSE_COUNTERSIGN)pSigner; } -bool _COSE_CountSign_create(COSE* pMessage, cn_cbor* pcnBody, CBOR_CONTEXT_COMMA cose_errback* perr) +bool _COSE_CountSign_create(COSE* pMessage, + cn_cbor* pcnBody, + CBOR_CONTEXT_COMMA cose_errback* perr) { cn_cbor* pArray = NULL; cn_cbor_errback cbor_err; @@ -44,7 +48,8 @@ bool _COSE_CountSign_create(COSE* pMessage, cn_cbor* pcnBody, CBOR_CONTEXT_COMMA cn_cbor* pcn = NULL; cn_cbor* pcn2 = NULL; - if (pMessage->m_counterSigners == NULL) return true; + if (pMessage->m_counterSigners == NULL) + return true; // One or more than one? if (pMessage->m_counterSigners->m_signer.m_signerNext != NULL) { @@ -55,21 +60,26 @@ bool _COSE_CountSign_create(COSE* pMessage, cn_cbor* pcnBody, CBOR_CONTEXT_COMMA pcnProtected = _COSE_arrayget_int(pMessage, INDEX_PROTECTED); CHECK_CONDITION(pcnProtected != NULL, COSE_ERR_INTERNAL); - for (pSigner = pMessage->m_counterSigners; pSigner != NULL; pSigner = pSigner->m_next) { - CHECK_CONDITION(pSigner->m_signer.m_signerNext == NULL, COSE_ERR_INTERNAL); + for (pSigner = pMessage->m_counterSigners; pSigner != NULL; + pSigner = pSigner->m_next) { + CHECK_CONDITION( + pSigner->m_signer.m_signerNext == NULL, COSE_ERR_INTERNAL); - pcn = cn_cbor_data_create(pcnProtected->v.bytes, pcnProtected->v.count, CBOR_CONTEXT_PARAM_COMMA & cbor_err); + pcn = cn_cbor_data_create(pcnProtected->v.bytes, pcnProtected->v.count, + CBOR_CONTEXT_PARAM_COMMA & cbor_err); CHECK_CONDITION_CBOR(pcnProtected != NULL, cbor_err); pcn2 = cn_cbor_clone(pcnBody, CBOR_CONTEXT_PARAM_COMMA & cbor_err); CHECK_CONDITION_CBOR(pcnBody != NULL, cbor_err); - if (!_COSE_Signer_sign(&pSigner->m_signer, pcnBody, pcn2, perr)) goto errorReturn; + if (!_COSE_Signer_sign(&pSigner->m_signer, pcnBody, pcn2, perr)) + goto errorReturn; pcn = NULL; pcn2 = NULL; if (pArray != NULL) { - bool f = cn_cbor_array_append(pArray, pSigner->m_signer.m_message.m_cborRoot, &cbor_err); + bool f = cn_cbor_array_append( + pArray, pSigner->m_signer.m_message.m_cborRoot, &cbor_err); CHECK_CONDITION_CBOR(f, cbor_err); } else { @@ -77,17 +87,20 @@ bool _COSE_CountSign_create(COSE* pMessage, cn_cbor* pcnBody, CBOR_CONTEXT_COMMA } } - if (!_COSE_map_put(pMessage, COSE_Header_CounterSign, pArray, COSE_UNPROTECT_ONLY, perr)) goto errorReturn; + if (!_COSE_map_put(pMessage, COSE_Header_CounterSign, pArray, + COSE_UNPROTECT_ONLY, perr)) + goto errorReturn; return true; errorReturn: - if (pArray != NULL) CN_CBOR_FREE(pArray, context); - if ((pcn != NULL) && (pcn->parent != NULL)) CN_CBOR_FREE(pcn, context); - if ((pcn2 != NULL) && (pcn2->parent != NULL)) CN_CBOR_FREE(pcn2, context); + if (pArray != NULL) + CN_CBOR_FREE(pArray, context); + if ((pcn != NULL) && (pcn->parent != NULL)) + CN_CBOR_FREE(pcn, context); + if ((pcn2 != NULL) && (pcn2->parent != NULL)) + CN_CBOR_FREE(pcn2, context); return false; } #endif - - diff --git a/src/Encrypt.c b/src/Encrypt.c index 8bfad20a..0eb43abb 100644 --- a/src/Encrypt.c +++ b/src/Encrypt.c @@ -359,7 +359,8 @@ bool _COSE_Enveloped_decrypt(COSE_Enveloped *pcose, goto errorReturn; } break; - } else if (pRecipX->m_encrypt.m_recipientFirst != NULL) { + } + else if (pRecipX->m_encrypt.m_recipientFirst != NULL) { if (_COSE_Recipient_decrypt( pRecipX, pRecip, alg, cbitKey, pbKeyNew, perr)) { break; @@ -367,7 +368,8 @@ bool _COSE_Enveloped_decrypt(COSE_Enveloped *pcose, } } CHECK_CONDITION(pRecipX != NULL, COSE_ERR_NO_RECIPIENT_FOUND); - } else { + } + else { for (pRecip = pcose->m_recipientFirst; pRecip != NULL; pRecip = pRecip->m_recipientNext) { if (_COSE_Recipient_decrypt( @@ -654,7 +656,8 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, if (pbKeyNew == NULL) { goto errorReturn; } - } else { + } + else { t |= 2; } } @@ -689,8 +692,8 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, switch (alg) { #ifdef USE_AES_CCM_16_64_128 case COSE_Algorithm_AES_CCM_16_64_128: - if (!AES_CCM_Encrypt( - pcose, 64, 16, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { + if (!AES_CCM_Encrypt(pcose, 64, 16, pbKey, cbKey, pbAuthData, + cbAuthData, perr)) { goto errorReturn; } break; @@ -698,8 +701,8 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, #ifdef USE_AES_CCM_16_64_256 case COSE_Algorithm_AES_CCM_16_64_256: - if (!AES_CCM_Encrypt( - pcose, 64, 16, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { + if (!AES_CCM_Encrypt(pcose, 64, 16, pbKey, cbKey, pbAuthData, + cbAuthData, perr)) { goto errorReturn; } break; @@ -707,8 +710,8 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, #ifdef USE_AES_CCM_16_128_128 case COSE_Algorithm_AES_CCM_16_128_128: - if (!AES_CCM_Encrypt( - pcose, 128, 16, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { + if (!AES_CCM_Encrypt(pcose, 128, 16, pbKey, cbKey, pbAuthData, + cbAuthData, perr)) { goto errorReturn; } break; @@ -716,8 +719,8 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, #ifdef USE_AES_CCM_16_128_256 case COSE_Algorithm_AES_CCM_16_128_256: - if (!AES_CCM_Encrypt( - pcose, 128, 16, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { + if (!AES_CCM_Encrypt(pcose, 128, 16, pbKey, cbKey, pbAuthData, + cbAuthData, perr)) { goto errorReturn; } break; @@ -725,8 +728,8 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, #ifdef USE_AES_CCM_64_64_128 case COSE_Algorithm_AES_CCM_64_64_128: - if (!AES_CCM_Encrypt( - pcose, 64, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { + if (!AES_CCM_Encrypt(pcose, 64, 64, pbKey, cbKey, pbAuthData, + cbAuthData, perr)) { goto errorReturn; } break; @@ -734,8 +737,8 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, #ifdef USE_AES_CCM_64_64_256 case COSE_Algorithm_AES_CCM_64_64_256: - if (!AES_CCM_Encrypt( - pcose, 64, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { + if (!AES_CCM_Encrypt(pcose, 64, 64, pbKey, cbKey, pbAuthData, + cbAuthData, perr)) { goto errorReturn; } break; @@ -743,8 +746,8 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, #ifdef USE_AES_CCM_64_128_128 case COSE_Algorithm_AES_CCM_64_128_128: - if (!AES_CCM_Encrypt( - pcose, 128, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { + if (!AES_CCM_Encrypt(pcose, 128, 64, pbKey, cbKey, pbAuthData, + cbAuthData, perr)) { goto errorReturn; } break; @@ -752,8 +755,8 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, #ifdef USE_AES_CCM_64_128_256 case COSE_Algorithm_AES_CCM_64_128_256: - if (!AES_CCM_Encrypt( - pcose, 128, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { + if (!AES_CCM_Encrypt(pcose, 128, 64, pbKey, cbKey, pbAuthData, + cbAuthData, perr)) { goto errorReturn; } break; @@ -807,7 +810,7 @@ bool _COSE_Enveloped_encrypt(COSE_Enveloped *pcose, } } #endif - + // Figure out the clean up fRet = true; @@ -1022,7 +1025,8 @@ bool _COSE_Encrypt_Build_AAD(COSE *pMessage, if ((pItem->length == 1) && (pItem->v.bytes[0] == 0xa0)) { ptmp = cn_cbor_data_create(NULL, 0, CBOR_CONTEXT_PARAM_COMMA & cbor_error); - } else { + } + else { ptmp = cn_cbor_data_create(pItem->v.bytes, (int)pItem->length, CBOR_CONTEXT_PARAM_COMMA & cbor_error); } diff --git a/src/Encrypt0.c b/src/Encrypt0.c index 178a495a..3d7227b4 100644 --- a/src/Encrypt0.c +++ b/src/Encrypt0.c @@ -16,7 +16,6 @@ #include "crypto.h" #if INCLUDE_ENCRYPT0 || INCLUDE_MAC0 -void _COSE_Encrypt_Release(COSE_Encrypt *p); COSE *EncryptRoot = NULL; #endif @@ -75,8 +74,9 @@ HCOSE_ENCRYPT COSE_Encrypt_Init_From_Object(cn_cbor *cbor, COSE_Encrypt *pobj; cose_errback error = {0}; - if (perr == NULL) + if (perr == NULL) { perr = &error; + } pobj = (COSE_Encrypt *)COSE_CALLOC(1, sizeof(COSE_Encrypt), context); if (pobj == NULL) { @@ -108,18 +108,21 @@ HCOSE_ENCRYPT _COSE_Encrypt_Init_From_Object(cn_cbor *cbor, COSE_Encrypt *pobj = pIn; cn_cbor *pRecipients = NULL; cose_errback error = {0}; - if (perr == NULL) + if (perr == NULL) { perr = &error; + } - if (pobj == NULL) + if (pobj == NULL) { pobj = (COSE_Encrypt *)COSE_CALLOC(1, sizeof(COSE_Encrypt), context); + } if (pobj == NULL) { perr->err = COSE_ERR_OUT_OF_MEMORY; errorReturn: if (pobj != NULL) { _COSE_Encrypt_Release(pobj); - if (pIn == NULL) + if (pIn == NULL) { COSE_FREE(pobj, context); + } } return NULL; } @@ -144,8 +147,9 @@ bool COSE_Encrypt_Free(HCOSE_ENCRYPT h) #endif COSE_Encrypt *pEncrypt = (COSE_Encrypt *)h; - if (!IsValidEncryptHandle(h)) + if (!IsValidEncryptHandle(h)) { return false; + } #ifdef USE_CBOR_CONTEXT context = &((COSE_Encrypt *)h)->m_message.m_allocContext; @@ -164,8 +168,9 @@ bool COSE_Encrypt_Free(HCOSE_ENCRYPT h) #if INCLUDE_ENCRYPT0 || INCLUDE_MAC0 void _COSE_Encrypt_Release(COSE_Encrypt *p) { - if (p->pbContent != NULL) + if (p->pbContent != NULL) { COSE_FREE((void *)p->pbContent, &p->m_message.m_allocContext); + } _COSE_Release(&p->m_message); } @@ -181,8 +186,9 @@ bool COSE_Encrypt_decrypt(HCOSE_ENCRYPT h, bool f; if (!IsValidEncryptHandle(h)) { - if (perr != NULL) + if (perr != NULL) { perr->err = COSE_ERR_INVALID_PARAMETER; + } return false; } @@ -211,8 +217,9 @@ const byte *COSE_Encrypt_GetContent(HCOSE_ENCRYPT h, { COSE_Encrypt *cose = (COSE_Encrypt *)h; if (!IsValidEncryptHandle(h) || (pcbContent == NULL)) { - if (perror != NULL) + if (perror != NULL) { perror->err = COSE_ERR_INVALID_PARAMETER; + } return false; } @@ -226,8 +233,9 @@ bool COSE_Encrypt_SetContent(HCOSE_ENCRYPT h, cose_errback *perror) { if (!IsValidEncryptHandle(h) || (rgb == NULL)) { - if (perror != NULL) + if (perror != NULL) { perror->err = COSE_ERR_INVALID_PARAMETER; + } return false; } @@ -243,8 +251,9 @@ bool _COSE_Encrypt_SetContent(COSE_Encrypt *cose, cose->pbContent = pb = (byte *)COSE_CALLOC(cb, 1, &cose->m_message.m_allocContext); if (cose->pbContent == NULL) { - if (perror != NULL) + if (perror != NULL) { perror->err = COSE_ERR_INVALID_PARAMETER; + } return false; } memcpy(pb, rgb, cb); @@ -275,8 +284,9 @@ bool COSE_Encrypt_SetExternal(HCOSE_ENCRYPT hcose, cose_errback *perr) { if (!IsValidEncryptHandle(hcose)) { - if (perr != NULL) + if (perr != NULL) { perr->err = COSE_ERR_INVALID_PARAMETER; + } return false; } @@ -290,8 +300,9 @@ cn_cbor *COSE_Encrypt_map_get_int(HCOSE_ENCRYPT h, cose_errback *perror) { if (!IsValidEncryptHandle(h)) { - if (perror != NULL) + if (perror != NULL) { perror->err = COSE_ERR_INVALID_PARAMETER; + } return NULL; } @@ -306,8 +317,9 @@ bool COSE_Encrypt_map_put_int(HCOSE_ENCRYPT h, cose_errback *perror) { if (!IsValidEncryptHandle(h) || (value == NULL)) { - if (perror != NULL) + if (perror != NULL) { perror->err = COSE_ERR_INVALID_PARAMETER; + } return false; } diff --git a/src/MacMessage.c b/src/MacMessage.c index 8b954502..c5d29e13 100644 --- a/src/MacMessage.c +++ b/src/MacMessage.c @@ -74,12 +74,14 @@ HCOSE_MAC _COSE_Mac_Init_From_Object(cn_cbor *cbor, cn_cbor *pRecipients = NULL; // cn_cbor * tmp; cose_errback error = {COSE_ERR_NONE}; - if (perr == NULL) + if (perr == NULL) { perr = &error; + } - if (pobj == NULL) + if (pobj == NULL) { pobj = (COSE_MacMessage *)COSE_CALLOC(1, sizeof(COSE_MacMessage), context); + } if (pobj == NULL) { perr->err = COSE_ERR_OUT_OF_MEMORY; errorReturn: @@ -106,8 +108,9 @@ HCOSE_MAC _COSE_Mac_Init_From_Object(cn_cbor *cbor, while (pRecipients != NULL) { COSE_RecipientInfo *pInfo = _COSE_Recipient_Init_From_Object( pRecipients, CBOR_CONTEXT_PARAM_COMMA perr); - if (pInfo == NULL) + if (pInfo == NULL) { goto errorReturn; + } pInfo->m_recipientNext = pobj->m_recipientFirst; pobj->m_recipientFirst = pInfo; @@ -127,8 +130,9 @@ bool COSE_Mac_Free(HCOSE_MAC h) #endif COSE_MacMessage *p = (COSE_MacMessage *)h; - if (!IsValidMacHandle(h)) + if (!IsValidMacHandle(h)) { return false; + } if (p->m_message.m_refCount > 1) { p->m_message.m_refCount--; @@ -141,7 +145,6 @@ bool COSE_Mac_Free(HCOSE_MAC h) context = &((COSE_MacMessage *)h)->m_message.m_allocContext; #endif - _COSE_Mac_Release((COSE_MacMessage *)h); COSE_FREE((COSE_MacMessage *)h, context); @@ -157,7 +160,7 @@ bool _COSE_Mac_Release(COSE_MacMessage *p) for (pRecipient = p->m_recipientFirst; pRecipient != NULL; pRecipient = pRecipient2) { pRecipient2 = pRecipient->m_recipientNext; - COSE_Recipient_Free((HCOSE_RECIPIENT) pRecipient); + COSE_Recipient_Free((HCOSE_RECIPIENT)pRecipient); } _COSE_Release(&p->m_message); @@ -191,8 +194,9 @@ bool COSE_Mac_SetContent(HCOSE_MAC cose, return true; errorReturn: - if (ptmp != NULL) + if (ptmp != NULL) { CN_CBOR_FREE(ptmp, context); + } return false; } @@ -218,8 +222,9 @@ bool COSE_Mac_SetExternal(HCOSE_MAC hcose, cose_errback *perr) { if (!IsValidMacHandle(hcose)) { - if (perr != NULL) + if (perr != NULL) { perr->err = COSE_ERR_INVALID_PARAMETER; + } return false; } @@ -233,8 +238,9 @@ cn_cbor *COSE_Mac_map_get_int(HCOSE_MAC h, cose_errback *perror) { if (!IsValidMacHandle(h)) { - if (perror != NULL) + if (perror != NULL) { perror->err = COSE_ERR_INVALID_PARAMETER; + } return NULL; } @@ -249,8 +255,9 @@ bool COSE_Mac_map_put_int(HCOSE_MAC h, cose_errback *perror) { if (!IsValidMacHandle(h) || (value == NULL)) { - if (perror != NULL) + if (perror != NULL) { perror->err = COSE_ERR_INVALID_PARAMETER; + } return false; } @@ -299,7 +306,8 @@ bool _COSE_Mac_Build_AAD(COSE *pCose, if ((pcn->length == 1) && (pcn->v.bytes[0] == 0xa0)) { ptmp = cn_cbor_data_create(NULL, 0, CBOR_CONTEXT_PARAM_COMMA NULL); - } else { + } + else { ptmp = cn_cbor_data_create( pcn->v.bytes, (int)pcn->length, CBOR_CONTEXT_PARAM_COMMA NULL); } @@ -341,12 +349,15 @@ bool _COSE_Mac_Build_AAD(COSE *pCose, fRet = true; errorReturn: - if (pbAuthData != NULL) + if (pbAuthData != NULL) { COSE_FREE(pbAuthData, context); - if (pAuthData != NULL) + } + if (pAuthData != NULL) { CN_CBOR_FREE(pAuthData, context); - if (ptmp != NULL) + } + if (ptmp != NULL) { CN_CBOR_FREE(ptmp, context); + } return fRet; } #endif @@ -391,8 +402,9 @@ bool _COSE_Mac_compute(COSE_MacMessage *pcose, cn_Alg = _COSE_map_get_int( &pcose->m_message, COSE_Header_Algorithm, COSE_BOTH, perr); - if (cn_Alg == NULL) + if (cn_Alg == NULL) { goto errorReturn; + } CHECK_CONDITION(cn_Alg->type != CN_CBOR_TEXT, COSE_ERR_UNKNOWN_ALGORITHM); CHECK_CONDITION( ((cn_Alg->type == CN_CBOR_UINT || cn_Alg->type == CN_CBOR_INT)), @@ -461,7 +473,8 @@ bool _COSE_Mac_compute(COSE_MacMessage *pcose, CHECK_CONDITION(cbKeyIn == cbitKey / 8, COSE_ERR_INVALID_PARAMETER); pbKey = pbKeyIn; cbKey = cbKeyIn; - } else { + } + else { t = 0; for (pri = pcose->m_recipientFirst; pri != NULL; pri = pri->m_recipientNext) { @@ -474,7 +487,8 @@ bool _COSE_Mac_compute(COSE_MacMessage *pcose, cbKey = cbitKey / 8; CHECK_CONDITION(pbKeyNew != NULL, COSE_ERR_OUT_OF_MEMORY); pbKey = pbKeyNew; - } else { + } + else { t |= 2; } } @@ -493,77 +507,87 @@ bool _COSE_Mac_compute(COSE_MacMessage *pcose, const cn_cbor *cbProtected = _COSE_encode_protected(&pcose->m_message, perr); - if (cbProtected == NULL) + if (cbProtected == NULL) { goto errorReturn; + } // Build authenticated data if (!_COSE_Mac_Build_AAD(&pcose->m_message, szContext, &pbAuthData, - &cbAuthData, CBOR_CONTEXT_PARAM_COMMA perr)) + &cbAuthData, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } switch (alg) { #ifdef USE_AES_CBC_MAC_128_64 case COSE_Algorithm_CBC_MAC_128_64: if (!AES_CBC_MAC_Create( - pcose, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) + pcose, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_AES_CBC_MAC_256_64 case COSE_Algorithm_CBC_MAC_256_64: if (!AES_CBC_MAC_Create( - pcose, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) + pcose, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_AES_CBC_MAC_128_128 case COSE_Algorithm_CBC_MAC_128_128: if (!AES_CBC_MAC_Create( - pcose, 128, pbKey, cbKey, pbAuthData, cbAuthData, perr)) + pcose, 128, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_AES_CBC_MAC_256_128 case COSE_Algorithm_CBC_MAC_256_128: if (!AES_CBC_MAC_Create( - pcose, 128, pbKey, cbKey, pbAuthData, cbAuthData, perr)) + pcose, 128, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_HMAC_256_64 case COSE_Algorithm_HMAC_256_64: if (!HMAC_Create( - pcose, 256, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) + pcose, 256, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_HMAC_256_256 case COSE_Algorithm_HMAC_256_256: if (!HMAC_Create(pcose, 256, 256, pbKey, cbKey, pbAuthData, - cbAuthData, perr)) + cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_HMAC_384_384 case COSE_Algorithm_HMAC_384_384: if (!HMAC_Create(pcose, 384, 384, pbKey, cbKey, pbAuthData, - cbAuthData, perr)) + cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_HMAC_512_512 case COSE_Algorithm_HMAC_512_512: if (!HMAC_Create(pcose, 512, 512, pbKey, cbKey, pbAuthData, - cbAuthData, perr)) + cbAuthData, perr)) { goto errorReturn; + } break; #endif @@ -573,8 +597,9 @@ bool _COSE_Mac_compute(COSE_MacMessage *pcose, for (pri = pcose->m_recipientFirst; pri != NULL; pri = pri->m_recipientNext) { - if (!_COSE_Recipient_encrypt(pri, pbKey, cbKey, perr)) + if (!_COSE_Recipient_encrypt(pri, pbKey, cbKey, perr)) { goto errorReturn; + } } #if INCLUDE_COUNTERSIGNATURE @@ -585,7 +610,7 @@ bool _COSE_Mac_compute(COSE_MacMessage *pcose, } } #endif - + // Figure out the clean up fRet = true; @@ -595,8 +620,9 @@ bool _COSE_Mac_compute(COSE_MacMessage *pcose, memset(pbKeyNew, 0, cbKey); COSE_FREE(pbKeyNew, context); } - if (pbAuthData != NULL) + if (pbAuthData != NULL) { COSE_FREE(pbAuthData, context); + } return fRet; } #endif @@ -643,12 +669,14 @@ bool _COSE_Mac_validate(COSE_MacMessage *pcose, cn = _COSE_map_get_int( &pcose->m_message, COSE_Header_Algorithm, COSE_BOTH, perr); - if (cn == NULL) + if (cn == NULL) { goto errorReturn; + } if (cn->type == CN_CBOR_TEXT) { FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM); - } else { + } + else { CHECK_CONDITION((cn->type == CN_CBOR_UINT || cn->type == CN_CBOR_INT), COSE_ERR_INVALID_PARAMETER); @@ -713,7 +741,8 @@ bool _COSE_Mac_validate(COSE_MacMessage *pcose, if (pbKeyIn != NULL) { CHECK_CONDITION(cbitKey / 8 == cbKeyIn, COSE_ERR_INVALID_PARAMETER); pbKey = pbKeyIn; - } else { + } + else { if (pbKeyNew == NULL) { pbKeyNew = COSE_CALLOC(cbitKey / 8, 1, context); CHECK_CONDITION(pbKeyNew != NULL, COSE_ERR_OUT_OF_MEMORY); @@ -729,22 +758,27 @@ bool _COSE_Mac_validate(COSE_MacMessage *pcose, pRecipX = pRecipX->m_recipientNext) { if (pRecip == pRecipX) { if (!_COSE_Recipient_decrypt( - pRecipX, pRecip, alg, cbitKey, pbKeyNew, perr)) + pRecipX, pRecip, alg, cbitKey, pbKeyNew, perr)) { goto errorReturn; + } break; - } else if (pRecipX->m_encrypt.m_recipientFirst != NULL) { + } + else if (pRecipX->m_encrypt.m_recipientFirst != NULL) { if (_COSE_Recipient_decrypt( - pRecipX, pRecip, alg, cbitKey, pbKeyNew, perr)) + pRecipX, pRecip, alg, cbitKey, pbKeyNew, perr)) { break; + } } } CHECK_CONDITION(pRecipX != NULL, COSE_ERR_NO_RECIPIENT_FOUND); - } else { + } + else { for (pRecip = pcose->m_recipientFirst; pRecip != NULL; pRecip = pRecip->m_recipientNext) { if (_COSE_Recipient_decrypt( - pRecip, NULL, alg, cbitKey, pbKeyNew, perr)) + pRecip, NULL, alg, cbitKey, pbKeyNew, perr)) { break; + } } CHECK_CONDITION(pRecip != NULL, COSE_ERR_NO_RECIPIENT_FOUND); } @@ -753,71 +787,80 @@ bool _COSE_Mac_validate(COSE_MacMessage *pcose, // Build authenticated data if (!_COSE_Mac_Build_AAD(&pcose->m_message, szContext, &pbAuthData, - &cbAuthData, CBOR_CONTEXT_PARAM_COMMA perr)) + &cbAuthData, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } switch (alg) { #ifdef USE_HMAC_256_256 case COSE_Algorithm_HMAC_256_256: if (!HMAC_Validate(pcose, 256, 256, pbKey, cbitKey / 8, pbAuthData, - cbAuthData, perr)) + cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_HMAC_256_64 case COSE_Algorithm_HMAC_256_64: if (!HMAC_Validate(pcose, 256, 64, pbKey, cbitKey / 8, pbAuthData, - cbAuthData, perr)) + cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_HMAC_384_384 case COSE_Algorithm_HMAC_384_384: if (!HMAC_Validate(pcose, 384, 384, pbKey, cbitKey / 8, pbAuthData, - cbAuthData, perr)) + cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_HMAC_512_512 case COSE_Algorithm_HMAC_512_512: if (!HMAC_Validate(pcose, 512, 512, pbKey, cbitKey / 8, pbAuthData, - cbAuthData, perr)) + cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_AES_CBC_MAC_128_64 case COSE_Algorithm_CBC_MAC_128_64: if (!AES_CBC_MAC_Validate(pcose, 64, pbKey, cbitKey / 8, pbAuthData, - cbAuthData, perr)) + cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_AES_CBC_MAC_256_64 case COSE_Algorithm_CBC_MAC_256_64: if (!AES_CBC_MAC_Validate(pcose, 64, pbKey, cbitKey / 8, pbAuthData, - cbAuthData, perr)) + cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_AES_CBC_MAC_128_128 case COSE_Algorithm_CBC_MAC_128_128: if (!AES_CBC_MAC_Validate(pcose, 128, pbKey, cbitKey / 8, - pbAuthData, cbAuthData, perr)) + pbAuthData, cbAuthData, perr)) { goto errorReturn; + } break; #endif #ifdef USE_AES_CBC_MAC_256_128 case COSE_Algorithm_CBC_MAC_256_128: if (!AES_CBC_MAC_Validate(pcose, 128, pbKey, cbitKey / 8, - pbAuthData, cbAuthData, perr)) + pbAuthData, cbAuthData, perr)) { goto errorReturn; + } break; #endif @@ -887,8 +930,9 @@ bool COSE_Mac_AddRecipient(HCOSE_MAC hMac, return true; errorReturn: - if (pRecipientsT == NULL) + if (pRecipientsT == NULL) { CN_CBOR_FREE(pRecipientsT, context); + } return false; } diff --git a/src/MacMessage0.c b/src/MacMessage0.c index 2dde20f7..ce6df54d 100644 --- a/src/MacMessage0.c +++ b/src/MacMessage0.c @@ -76,12 +76,14 @@ HCOSE_MAC0 _COSE_Mac0_Init_From_Object(cn_cbor *cbor, cn_cbor *pRecipients = NULL; // cn_cbor * tmp; cose_errback error = {COSE_ERR_NONE}; - if (perr == NULL) + if (perr == NULL) { perr = &error; + } - if (pobj == NULL) + if (pobj == NULL) { pobj = (COSE_Mac0Message *)COSE_CALLOC( 1, sizeof(COSE_Mac0Message), context); + } if (pobj == NULL) { perr->err = COSE_ERR_OUT_OF_MEMORY; errorReturn: @@ -114,8 +116,9 @@ bool COSE_Mac0_Free(HCOSE_MAC0 h) #endif COSE_Mac0Message *p = (COSE_Mac0Message *)h; - if (!IsValidMac0Handle(h)) + if (!IsValidMac0Handle(h)) { return false; + } if (p->m_message.m_refCount > 1) { p->m_message.m_refCount--; @@ -168,8 +171,9 @@ bool COSE_Mac0_SetContent(HCOSE_MAC0 cose, return true; errorReturn: - if (ptmp != NULL) + if (ptmp != NULL) { CN_CBOR_FREE(ptmp, context); + } return false; } @@ -195,8 +199,9 @@ bool COSE_Mac0_SetExternal(HCOSE_MAC0 hcose, cose_errback *perr) { if (!IsValidMac0Handle(hcose)) { - if (perr != NULL) + if (perr != NULL) { perr->err = COSE_ERR_INVALID_PARAMETER; + } return false; } @@ -210,8 +215,9 @@ cn_cbor *COSE_Mac0_map_get_int(HCOSE_MAC0 h, cose_errback *perror) { if (!IsValidMac0Handle(h)) { - if (perror != NULL) + if (perror != NULL) { perror->err = COSE_ERR_INVALID_PARAMETER; + } return NULL; } @@ -226,8 +232,9 @@ bool COSE_Mac0_map_put_int(HCOSE_MAC0 h, cose_errback *perror) { if (!IsValidMac0Handle(h) || (value == NULL)) { - if (perror != NULL) + if (perror != NULL) { perror->err = COSE_ERR_INVALID_PARAMETER; + } return false; } diff --git a/src/Recipient.c b/src/Recipient.c index 3b7f39f0..ef48eaa3 100644 --- a/src/Recipient.c +++ b/src/Recipient.c @@ -74,7 +74,7 @@ bool COSE_Recipient_Free(HCOSE_RECIPIENT hRecipient) p->m_encrypt.m_message.m_refCount--; return true; } - + _COSE_RemoveFromList(&RecipientRoot, &p->m_encrypt.m_message); _COSE_Recipient_Free(p); @@ -230,7 +230,8 @@ static bool HKDF_X(COSE *pCose, goto errorReturn; } } - } else { + } + else { pkeyMessage = _COSE_map_get_int(pCose, fStatic ? COSE_Header_ECDH_STATIC : COSE_Header_ECDH_EPHEMERAL, COSE_BOTH, perr); @@ -247,7 +248,8 @@ static bool HKDF_X(COSE *pCose, #else goto errorReturn; #endif - } else { + } + else { CHECK_CONDITION(pKeyPrivate != NULL, COSE_ERR_INVALID_PARAMETER); cn = cn_cbor_mapget_int(pKeyPrivate, COSE_Key_Type); CHECK_CONDITION((cn != NULL) && (cn->type == CN_CBOR_UINT), @@ -272,13 +274,14 @@ static bool HKDF_X(COSE *pCose, } if (!HKDF_Expand(pCose, cbitHash, rgbDigest, cbDigest, pbContext, - cbContext, pbKey, cbitKey / 8, perr)) { + cbContext, pbKey, cbitKey / 8, perr)) { goto errorReturn; } #else goto errorReturn; #endif - } else { + } + else { #ifdef USE_HKDF_AES if (!HKDF_AES_Expand(pCose, cbitHash, pbSecret, cbSecret, pbContext, cbContext, pbKey, cbitKey / 8, perr)) { @@ -492,7 +495,8 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, cnBody->v.bytes, cnBody->length, pbKeyOut, &x, perr)) { goto errorReturn; } - } else { + } + else { CHECK_CONDITION( pRecip->m_pkey != NULL, COSE_ERR_INVALID_PARAMETER); int x = cbitKeyOut / 8; @@ -517,7 +521,8 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, cnBody->v.bytes, cnBody->length, pbKeyOut, &x, perr)) { goto errorReturn; } - } else { + } + else { CHECK_CONDITION( pRecip->m_pkey != NULL, COSE_ERR_INVALID_PARAMETER); int x = cbitKeyOut / 8; @@ -542,7 +547,8 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, cnBody->v.bytes, cnBody->length, pbKeyOut, &x, perr)) { goto errorReturn; } - } else { + } + else { CHECK_CONDITION( pRecip->m_pkey != NULL, COSE_ERR_INVALID_PARAMETER); int x = cbitKeyOut / 8; @@ -648,7 +654,7 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, } if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, rgbKey, 128, - cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) { + cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) { goto errorReturn; } @@ -664,7 +670,7 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, } if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, rgbKey, 192, - cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) { + cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) { goto errorReturn; } @@ -680,7 +686,7 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, } if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, rgbKey, 256, - cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) { + cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) { goto errorReturn; } @@ -696,7 +702,7 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, } if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, rgbKey, 128, - cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) { + cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) { goto errorReturn; } @@ -712,7 +718,7 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, } if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, rgbKey, 192, - cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) { + cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) { goto errorReturn; } @@ -728,7 +734,7 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo *pRecip, } if (!AES_KW_Decrypt((COSE_Enveloped *)pcose, rgbKey, 256, - cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) { + cnBody->v.bytes, cnBody->length, pbKeyOut, &cbKey2, perr)) { goto errorReturn; } @@ -889,7 +895,8 @@ bool _COSE_Recipient_encrypt(COSE_RecipientInfo *pRecipient, goto errorReturn; } cbKey = cbitKey / 8; - } else { + } + else { t |= 2; } } @@ -968,7 +975,8 @@ bool _COSE_Recipient_encrypt(COSE_RecipientInfo *pRecipient, (int)pK->length * 8, pbContent, (int)cbContent, perr)) { goto errorReturn; } - } else { + } + else { if (!AES_KW_Encrypt(pRecipient, pbKey, (int)cbKey * 8, pbContent, (int)cbContent, perr)) { goto errorReturn; @@ -986,7 +994,8 @@ bool _COSE_Recipient_encrypt(COSE_RecipientInfo *pRecipient, (int)pK->length * 8, pbContent, (int)cbContent, perr)) { goto errorReturn; } - } else { + } + else { if (!AES_KW_Encrypt(pRecipient, pbKey, (int)cbKey * 8, pbContent, (int)cbContent, perr)) { goto errorReturn; @@ -1004,7 +1013,8 @@ bool _COSE_Recipient_encrypt(COSE_RecipientInfo *pRecipient, (int)pK->length * 8, pbContent, (int)cbContent, perr)) { goto errorReturn; } - } else { + } + else { if (!AES_KW_Encrypt(pRecipient, pbKey, (int)cbKey * 8, pbContent, (int)cbContent, perr)) { goto errorReturn; @@ -1316,7 +1326,8 @@ bool COSE_Recipient_SetKey_secret(HCOSE_RECIPIENT hRecipient, CHECK_CONDITION(cnAlg->type == CN_CBOR_INT && cnAlg->v.sint == COSE_Algorithm_Direct, COSE_ERR_INVALID_PARAMETER); - } else { + } + else { cn_Temp = cn_cbor_int_create( COSE_Algorithm_Direct, CBOR_CONTEXT_PARAM_COMMA & cbor_error); CHECK_CONDITION_CBOR(cn_Temp != NULL, cbor_error); @@ -1337,8 +1348,8 @@ bool COSE_Recipient_SetKey_secret(HCOSE_RECIPIENT hRecipient, CHECK_CONDITION_CBOR(cnTemp != NULL, cbor_error); pbTemp = NULL; - if (!COSE_Recipient_map_put_int( - hRecipient, COSE_Header_KID, cnTemp, COSE_UNPROTECT_ONLY, perr)) { + if (!COSE_Recipient_map_put_int(hRecipient, COSE_Header_KID, cnTemp, + COSE_UNPROTECT_ONLY, perr)) { goto errorReturn; } } @@ -1603,7 +1614,8 @@ bool COSE_Recipient_map_put_int(HCOSE_RECIPIENT h, ~1; break; } - } else { + } + else { ((COSE_RecipientInfo *)h)->m_encrypt.m_message.m_flags &= ~1; } } @@ -1646,7 +1658,8 @@ static bool BuildContextBytes(COSE *pcose, cnParam = _COSE_map_get_int(pcose, COSE_Header_KDF_U_name, COSE_BOTH, perr); if (cnParam != NULL) { cnT = cn_cbor_clone(cnParam, CBOR_CONTEXT_PARAM_COMMA & cbor_error); - } else { + } + else { cnT = cn_cbor_null_create(CBOR_CONTEXT_PARAM_COMMA & cbor_error); } CHECK_CONDITION_CBOR(cnT != NULL, cbor_error); @@ -1659,7 +1672,8 @@ static bool BuildContextBytes(COSE *pcose, _COSE_map_get_int(pcose, COSE_Header_KDF_U_nonce, COSE_BOTH, perr); if (cnParam != NULL) { cnT = cn_cbor_clone(cnParam, CBOR_CONTEXT_PARAM_COMMA & cbor_error); - } else { + } + else { cnT = cn_cbor_null_create(CBOR_CONTEXT_PARAM_COMMA & cbor_error); } CHECK_CONDITION_CBOR(cnT != NULL, cbor_error); @@ -1672,7 +1686,8 @@ static bool BuildContextBytes(COSE *pcose, _COSE_map_get_int(pcose, COSE_Header_KDF_U_other, COSE_BOTH, perr); if (cnParam != NULL) { cnT = cn_cbor_clone(cnParam, CBOR_CONTEXT_PARAM_COMMA & cbor_error); - } else { + } + else { cnT = cn_cbor_null_create(CBOR_CONTEXT_PARAM_COMMA & cbor_error); } CHECK_CONDITION_CBOR(cnT != NULL, cbor_error); @@ -1691,7 +1706,8 @@ static bool BuildContextBytes(COSE *pcose, cnParam = _COSE_map_get_int(pcose, COSE_Header_KDF_V_name, COSE_BOTH, perr); if (cnParam != NULL) { cnT = cn_cbor_clone(cnParam, CBOR_CONTEXT_PARAM_COMMA & cbor_error); - } else { + } + else { cnT = cn_cbor_null_create(CBOR_CONTEXT_PARAM_COMMA & cbor_error); } CHECK_CONDITION_CBOR(cnT != NULL, cbor_error); @@ -1704,7 +1720,8 @@ static bool BuildContextBytes(COSE *pcose, _COSE_map_get_int(pcose, COSE_Header_KDF_V_nonce, COSE_BOTH, perr); if (cnParam != NULL) { cnT = cn_cbor_clone(cnParam, CBOR_CONTEXT_PARAM_COMMA & cbor_error); - } else { + } + else { cnT = cn_cbor_null_create(CBOR_CONTEXT_PARAM_COMMA & cbor_error); } CHECK_CONDITION_CBOR(cnT != NULL, cbor_error); @@ -1717,7 +1734,8 @@ static bool BuildContextBytes(COSE *pcose, _COSE_map_get_int(pcose, COSE_Header_KDF_V_other, COSE_BOTH, perr); if (cnParam != NULL) { cnT = cn_cbor_clone(cnParam, CBOR_CONTEXT_PARAM_COMMA & cbor_error); - } else { + } + else { cnT = cn_cbor_null_create(CBOR_CONTEXT_PARAM_COMMA & cbor_error); } CHECK_CONDITION_CBOR(cnT != NULL, cbor_error); diff --git a/src/Sign.c b/src/Sign.c index ec60bec4..9fed2cd8 100644 --- a/src/Sign.c +++ b/src/Sign.c @@ -31,8 +31,9 @@ bool IsValidSignHandle(HCOSE_SIGN h) { COSE_SignMessage *p = (COSE_SignMessage *)h; - if (p == NULL) + if (p == NULL) { return false; + } return _COSE_IsInList(SignRoot, (COSE *)p); } @@ -74,12 +75,14 @@ HCOSE_SIGN _COSE_Sign_Init_From_Object(cn_cbor *cbor, cn_cbor *pSigners = NULL; // cn_cbor * tmp; cose_errback error = {0}; - if (perr == NULL) + if (perr == NULL) { perr = &error; + } - if (pobj == NULL) + if (pobj == NULL) { pobj = (COSE_SignMessage *)COSE_CALLOC( 1, sizeof(COSE_SignMessage), context); + } CHECK_CONDITION(pobj != NULL, COSE_ERR_OUT_OF_MEMORY); if (!_COSE_Init_From_Object( @@ -98,24 +101,27 @@ HCOSE_SIGN _COSE_Sign_Init_From_Object(cn_cbor *cbor, do { COSE_SignerInfo *pInfo = _COSE_SignerInfo_Init_From_Object( pSigners, NULL, CBOR_CONTEXT_PARAM_COMMA perr); - if (pInfo == NULL) + if (pInfo == NULL) { goto errorReturn; + } pInfo->m_signerNext = pobj->m_signerFirst; pobj->m_signerFirst = pInfo; pSigners = pSigners->next; } while (pSigners != NULL); - if (pIn == NULL) + if (pIn == NULL) { _COSE_InsertInList(&SignRoot, &pobj->m_message); + } return (HCOSE_SIGN)pobj; errorReturn: if (pobj != NULL) { _COSE_Sign_Release(pobj); - if (pIn == NULL) + if (pIn == NULL) { COSE_FREE(pobj, context); + } } return NULL; } @@ -127,8 +133,9 @@ bool COSE_Sign_Free(HCOSE_SIGN h) #endif COSE_SignMessage *pMessage = (COSE_SignMessage *)h; - if (!IsValidSignHandle(h)) + if (!IsValidSignHandle(h)) { return false; + } // Check reference counting if (pMessage->m_message.m_refCount > 1) { @@ -156,7 +163,7 @@ void _COSE_Sign_Release(COSE_SignMessage *p) for (pSigner = p->m_signerFirst; pSigner != NULL; pSigner = pSigner2) { pSigner2 = pSigner->m_signerNext; - COSE_Signer_Free((HCOSE_SIGNER) pSigner); + COSE_Signer_Free((HCOSE_SIGNER)pSigner); } _COSE_Release(&p->m_message); @@ -191,8 +198,9 @@ bool COSE_Sign_SetContent(HCOSE_SIGN h, f = true; errorReturn: - if (p != NULL) + if (p != NULL) { CN_CBOR_FREE(p, context); + } return f; } @@ -219,14 +227,16 @@ HCOSE_SIGNER COSE_Sign_add_signer(HCOSE_SIGN hSign, #endif hSigner = COSE_Signer_Init(CBOR_CONTEXT_PARAM_COMMA perr); - if (hSigner == NULL) + if (hSigner == NULL) { goto errorReturn; + } cbor2 = cn_cbor_int_create(algId, CBOR_CONTEXT_PARAM_COMMA & cbor_error); CHECK_CONDITION_CBOR(cbor2 != NULL, cbor_error); if (!COSE_Signer_map_put_int( - hSigner, COSE_Header_Algorithm, cbor2, COSE_PROTECT_ONLY, perr)) + hSigner, COSE_Header_Algorithm, cbor2, COSE_PROTECT_ONLY, perr)) { goto errorReturn; + } cbor2 = NULL; cbor = cn_cbor_mapget_int(pkey, COSE_Key_ID); @@ -237,31 +247,36 @@ HCOSE_SIGNER COSE_Sign_add_signer(HCOSE_SIGN hSign, CBOR_CONTEXT_PARAM_COMMA & cbor_error); CHECK_CONDITION_CBOR(cbor2 != NULL, cbor_error); if (!COSE_Signer_map_put_int( - hSigner, COSE_Header_KID, cbor2, COSE_UNPROTECT_ONLY, perr)) + hSigner, COSE_Header_KID, cbor2, COSE_UNPROTECT_ONLY, perr)) { goto errorReturn; + } cbor2 = NULL; } - if (!COSE_Signer_SetKey(hSigner, pkey, perr)) + if (!COSE_Signer_SetKey(hSigner, pkey, perr)) { goto errorReturn; + } - if (!COSE_Sign_AddSigner(hSign, hSigner, perr)) + if (!COSE_Sign_AddSigner(hSign, hSigner, perr)) { goto errorReturn; + } return hSigner; errorReturn: - if (cbor2 != NULL) + if (cbor2 != NULL) { CN_CBOR_FREE((void *)cbor2, context); - if (hSigner != NULL) + } + if (hSigner != NULL) { COSE_Signer_Free(hSigner); + } return NULL; } bool COSE_Sign_Sign(HCOSE_SIGN h, cose_errback *perr) { #ifdef USE_CBOR_CONTEXT - cn_cbor_context * context = NULL; + cn_cbor_context *context = NULL; #endif COSE_SignMessage *pMessage = (COSE_SignMessage *)h; COSE_SignerInfo *pSigner; @@ -282,12 +297,14 @@ bool COSE_Sign_Sign(HCOSE_SIGN h, cose_errback *perr) COSE_ERR_INVALID_PARAMETER); pcborProtected = _COSE_encode_protected(&pMessage->m_message, perr); - if (pcborProtected == NULL) + if (pcborProtected == NULL) { goto errorReturn; + } for (pSigner = pMessage->m_signerFirst; pSigner != NULL; - pSigner = pSigner->m_signerNext) { - if (!_COSE_Signer_sign(pSigner, pcborBody, pcborProtected, "Signature", perr)) { + pSigner = pSigner->m_signerNext) { + if (!_COSE_Signer_sign( + pSigner, pcborBody, pcborProtected, "Signature", perr)) { goto errorReturn; } } @@ -300,7 +317,7 @@ bool COSE_Sign_Sign(HCOSE_SIGN h, cose_errback *perr) } } #endif - + return true; } @@ -328,7 +345,8 @@ bool COSE_Sign_validate(HCOSE_SIGN hSign, CHECK_CONDITION(cnProtected != NULL && cnProtected->type == CN_CBOR_BYTES, COSE_ERR_INVALID_PARAMETER); - f = _COSE_Signer_validate(pSigner, cnContent, cnProtected, "Signature", perr); + f = _COSE_Signer_validate( + pSigner, cnContent, cnProtected, "Signature", perr); return f; @@ -383,8 +401,9 @@ bool COSE_Sign_AddSigner(HCOSE_SIGN hSign, return true; errorReturn: - if (pSignersT == NULL) + if (pSignersT == NULL) { CN_CBOR_FREE(pSignersT, context); + } return false; } @@ -394,8 +413,9 @@ cn_cbor *COSE_Sign_map_get_int(HCOSE_SIGN h, cose_errback *perror) { if (!IsValidSignHandle(h)) { - if (perror != NULL) + if (perror != NULL) { perror->err = COSE_ERR_INVALID_HANDLE; + } return NULL; } @@ -410,8 +430,9 @@ bool COSE_Sign_map_put_int(HCOSE_SIGN h, cose_errback *perror) { if (!IsValidSignHandle(h)) { - if (perror != NULL) + if (perror != NULL) { perror->err = COSE_ERR_INVALID_HANDLE; + } return false; } @@ -427,16 +448,18 @@ HCOSE_SIGNER COSE_Sign_GetSigner(HCOSE_SIGN cose, COSE_SignerInfo *p; if (!IsValidSignHandle(cose)) { - if (perr != NULL) + if (perr != NULL) { perr->err = COSE_ERR_INVALID_HANDLE; + } return NULL; } p = ((COSE_SignMessage *)cose)->m_signerFirst; for (i = 0; i < iSigner; i++) { if (p == NULL) { - if (perr != NULL) + if (perr != NULL) { perr->err = COSE_ERR_INVALID_PARAMETER; + } return NULL; } p = p->m_signerNext; diff --git a/src/Sign1.c b/src/Sign1.c index 34ba5596..558dc6d9 100644 --- a/src/Sign1.c +++ b/src/Sign1.c @@ -40,8 +40,9 @@ bool IsValidSign1Handle(HCOSE_SIGN1 h) { COSE_Sign1Message *p = (COSE_Sign1Message *)h; - if (p == NULL) + if (p == NULL) { return false; + } return _COSE_IsInList(Sign1Root, (COSE *)p); } @@ -52,8 +53,9 @@ HCOSE_SIGN1 COSE_Sign1_Init(COSE_INIT_FLAGS flags, COSE_Sign1Message *pobj = (COSE_Sign1Message *)COSE_CALLOC(1, sizeof(COSE_Sign1Message), context); if (pobj == NULL) { - if (perr != NULL) + if (perr != NULL) { perr->err = COSE_ERR_OUT_OF_MEMORY; + } return NULL; } @@ -79,12 +81,14 @@ HCOSE_SIGN1 _COSE_Sign1_Init_From_Object(cn_cbor *cbor, COSE_Sign1Message *pobj = pIn; cose_errback error = {0}; - if (perr == NULL) + if (perr == NULL) { perr = &error; + } - if (pobj == NULL) + if (pobj == NULL) { pobj = (COSE_Sign1Message *)COSE_CALLOC( 1, sizeof(COSE_Sign1Message), context); + } CHECK_CONDITION(pobj != NULL, COSE_ERR_OUT_OF_MEMORY); if (!_COSE_Init_From_Object( @@ -92,16 +96,18 @@ HCOSE_SIGN1 _COSE_Sign1_Init_From_Object(cn_cbor *cbor, goto errorReturn; } - if (pIn == NULL) + if (pIn == NULL) { _COSE_InsertInList(&Sign1Root, &pobj->m_message); + } return (HCOSE_SIGN1)pobj; errorReturn: if (pobj != NULL) { _COSE_Sign1_Release(pobj); - if (pIn == NULL) + if (pIn == NULL) { COSE_FREE(pobj, context); + } } return NULL; } @@ -113,8 +119,9 @@ bool COSE_Sign1_Free(HCOSE_SIGN1 h) #endif COSE_Sign1Message *pMessage = (COSE_Sign1Message *)h; - if (!IsValidSign1Handle(h)) + if (!IsValidSign1Handle(h)) { return false; + } // Check reference counting if (pMessage->m_message.m_refCount > 1) { @@ -170,8 +177,9 @@ bool COSE_Sign1_SetContent(HCOSE_SIGN1 h, fRet = true; errorReturn: - if (p != NULL) + if (p != NULL) { CN_CBOR_FREE(p, context); + } return fRet; } @@ -197,8 +205,9 @@ bool COSE_Sign1_SetExternal(HCOSE_SIGN1 hcose, cose_errback *perr) { if (!IsValidSign1Handle(hcose)) { - if (perr != NULL) + if (perr != NULL) { perr->err = COSE_ERR_INVALID_HANDLE; + } return false; } @@ -209,7 +218,7 @@ bool COSE_Sign1_SetExternal(HCOSE_SIGN1 hcose, bool COSE_Sign1_Sign(HCOSE_SIGN1 h, const cn_cbor *pKey, cose_errback *perr) { #ifdef USE_CBOR_CONTEXT - cn_cbor_context * context = NULL; + cn_cbor_context *context = NULL; #endif COSE_Sign1Message *pMessage = (COSE_Sign1Message *)h; const cn_cbor *pcborProtected; @@ -224,11 +233,13 @@ bool COSE_Sign1_Sign(HCOSE_SIGN1 h, const cn_cbor *pKey, cose_errback *perr) #endif pcborProtected = _COSE_encode_protected(&pMessage->m_message, perr); - if (pcborProtected == NULL) + if (pcborProtected == NULL) { goto errorReturn; + } - if (!_COSE_Signer0_sign(pMessage, pKey, perr)) + if (!_COSE_Signer0_sign(pMessage, pKey, perr)) { goto errorReturn; + } #if INCLUDE_COUNTERSIGNATURE if (pMessage->m_message.m_counterSigners != NULL) { @@ -238,7 +249,7 @@ bool COSE_Sign1_Sign(HCOSE_SIGN1 h, const cn_cbor *pKey, cose_errback *perr) } } #endif - + return true; } @@ -277,8 +288,9 @@ cn_cbor *COSE_Sign1_map_get_int(HCOSE_SIGN1 h, cose_errback *perror) { if (!IsValidSign1Handle(h)) { - if (perror != NULL) + if (perror != NULL) { perror->err = COSE_ERR_INVALID_HANDLE; + } return NULL; } @@ -331,12 +343,14 @@ static bool CreateSign1AAD(COSE_Sign1Message *pMessage, cn2 = _COSE_arrayget_int(&pMessage->m_message, INDEX_PROTECTED); CHECK_CONDITION(cn2 != NULL, COSE_ERR_INVALID_PARAMETER); - if ((cn2->length == 1) && (cn2->v.bytes[0] == 0xa0)) + if ((cn2->length == 1) && (cn2->v.bytes[0] == 0xa0)) { cn = cn_cbor_data_create(NULL, 0, CBOR_CONTEXT_PARAM_COMMA & cbor_error); - else + } + else { cn = cn_cbor_data_create(cn2->v.bytes, (int)cn2->length, - CBOR_CONTEXT_PARAM_COMMA & cbor_error); + CBOR_CONTEXT_PARAM_COMMA & cbor_error); + } CHECK_CONDITION_CBOR(cn != NULL, cbor_error); CHECK_CONDITION_CBOR( cn_cbor_array_append(pArray, cn, &cbor_error), cbor_error); @@ -370,19 +384,24 @@ static bool CreateSign1AAD(COSE_Sign1Message *pMessage, *pcbToSign = cbToSign; pbToSign = NULL; - if (cn != NULL) + if (cn != NULL) { CN_CBOR_FREE(cn, context); - if (pArray != NULL) + } + if (pArray != NULL) { COSE_FREE(pArray, context); + } return true; errorReturn: - if (pbToSign != NULL) + if (pbToSign != NULL) { COSE_FREE(pbToSign, context); - if (cn != NULL) + } + if (cn != NULL) { CN_CBOR_FREE(cn, context); - if (pArray != NULL) + } + if (pArray != NULL) { COSE_FREE(pArray, context); + } return false; } @@ -404,36 +423,44 @@ bool _COSE_Signer0_sign(COSE_Sign1Message *pSigner, pArray = cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA NULL); if (pArray == NULL) { - if (perr != NULL) + if (perr != NULL) { perr->err = COSE_ERR_OUT_OF_MEMORY; + } errorReturn: - if (pcborBody2 != NULL) + if (pcborBody2 != NULL) { CN_CBOR_FREE(pcborBody2, context); - if (pcborProtected2 != NULL) + } + if (pcborProtected2 != NULL) { CN_CBOR_FREE(pcborProtected2, context); - if (pArray != NULL) + } + if (pArray != NULL) { COSE_FREE(pArray, context); - if (pbToSign != NULL) + } + if (pbToSign != NULL) { COSE_FREE(pbToSign, context); + } return false; } cn = _COSE_map_get_int( &pSigner->m_message, COSE_Header_Algorithm, COSE_BOTH, perr); - if (cn == NULL) + if (cn == NULL) { goto errorReturn; + } if (cn->type == CN_CBOR_TEXT) { FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM); - } else { + } + else { CHECK_CONDITION((cn->type == CN_CBOR_UINT || cn->type == CN_CBOR_INT), COSE_ERR_INVALID_PARAMETER); alg = (int)cn->v.uint; } - if (!CreateSign1AAD(pSigner, &pbToSign, &cbToSign, "Signature1", perr)) + if (!CreateSign1AAD(pSigner, &pbToSign, &cbToSign, "Signature1", perr)) { goto errorReturn; + } switch (alg) { #ifdef USE_ECDSA_SHA_256 @@ -493,12 +520,14 @@ bool _COSE_Signer0_validate(COSE_Sign1Message *pSign, cn = _COSE_map_get_int( &pSign->m_message, COSE_Header_Algorithm, COSE_BOTH, perr); - if (cn == NULL) + if (cn == NULL) { goto errorReturn; + } if (cn->type == CN_CBOR_TEXT) { FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM); - } else { + } + else { CHECK_CONDITION((cn->type == CN_CBOR_UINT || cn->type == CN_CBOR_INT), COSE_ERR_INVALID_PARAMETER); @@ -507,39 +536,44 @@ bool _COSE_Signer0_validate(COSE_Sign1Message *pSign, // Build protected headers - if (!CreateSign1AAD(pSign, &pbToSign, &cbToSign, "Signature1", perr)) + if (!CreateSign1AAD(pSign, &pbToSign, &cbToSign, "Signature1", perr)) { goto errorReturn; + } switch (alg) { #ifdef USE_ECDSA_SHA_256 case COSE_Algorithm_ECDSA_SHA_256: if (!ECDSA_Verify(&pSign->m_message, INDEX_SIGNATURE + 1, pKey, 256, - pbToSign, cbToSign, perr)) + pbToSign, cbToSign, perr)) { goto errorReturn; + } break; #endif #ifdef USE_ECDSA_SHA_384 case COSE_Algorithm_ECDSA_SHA_384: if (!ECDSA_Verify(&pSign->m_message, INDEX_SIGNATURE + 1, pKey, 384, - pbToSign, cbToSign, perr)) + pbToSign, cbToSign, perr)) { goto errorReturn; + } break; #endif #ifdef USE_ECDSA_SHA_512 case COSE_Algorithm_ECDSA_SHA_512: if (!ECDSA_Verify(&pSign->m_message, INDEX_SIGNATURE + 1, pKey, 512, - pbToSign, cbToSign, perr)) + pbToSign, cbToSign, perr)) { goto errorReturn; + } break; #endif #ifdef USE_EDDSA case COSE_Algorithm_EdDSA: if (!EdDSA_Verify(&pSign->m_message, INDEX_SIGNATURE + 1, pKey, - pbToSign, cbToSign, perr)) + pbToSign, cbToSign, perr)) { goto errorReturn; + } break; #endif @@ -551,8 +585,9 @@ bool _COSE_Signer0_validate(COSE_Sign1Message *pSign, fRet = true; errorReturn: - if (pbToSign != NULL) + if (pbToSign != NULL) { COSE_FREE(pbToSign, context); + } return fRet; } diff --git a/src/SignerInfo.c b/src/SignerInfo.c index 7c901df9..fac8d911 100644 --- a/src/SignerInfo.c +++ b/src/SignerInfo.c @@ -44,8 +44,9 @@ bool COSE_Signer_Free(HCOSE_SIGNER hSigner) COSE_SignerInfo *pSigner = (COSE_SignerInfo *)hSigner; bool fRet = false; - if (!IsValidSignerHandle(hSigner)) + if (!IsValidSignerHandle(hSigner)) { goto errorReturn; + } if (pSigner->m_message.m_refCount > 1) { pSigner->m_message.m_refCount--; @@ -68,8 +69,9 @@ HCOSE_SIGNER COSE_Signer_Init(CBOR_CONTEXT_COMMA cose_errback *perror) COSE_SignerInfo *pobj = (COSE_SignerInfo *)COSE_CALLOC(1, sizeof(COSE_SignerInfo), context); if (pobj == NULL) { - if (perror != NULL) + if (perror != NULL) { perror->err = COSE_ERR_OUT_OF_MEMORY; + } return NULL; } @@ -109,8 +111,9 @@ COSE_SignerInfo *_COSE_SignerInfo_Init_From_Object(cn_cbor *cbor, CHECK_CONDITION(cbor->type == CN_CBOR_ARRAY, COSE_ERR_INVALID_PARAMETER); if (!_COSE_Init_From_Object( - &pSigner->m_message, cbor, CBOR_CONTEXT_PARAM_COMMA perr)) + &pSigner->m_message, cbor, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } #if INCLUDE_SIGN if (pIn == NULL) { @@ -136,7 +139,7 @@ static bool BuildToBeSigned(byte **ppbToSign, const cn_cbor *pcborProtectedSign, const byte *pbExternal, size_t cbExternal, - const char * const contextString, + const char *const contextString, CBOR_CONTEXT_COMMA cose_errback *perr) { cn_cbor *pArray = NULL; @@ -156,25 +159,29 @@ static bool BuildToBeSigned(byte **ppbToSign, cn_cbor_array_append(pArray, cn, &cbor_error), cbor_error); cn = NULL; - if (pcborProtected->length == 1 && (pcborProtected->v.bytes[0] == 0xa0)) + if (pcborProtected->length == 1 && (pcborProtected->v.bytes[0] == 0xa0)) { cn = cn_cbor_data_create(NULL, 0, CBOR_CONTEXT_PARAM_COMMA & cbor_error); - else + } + else { cn = cn_cbor_data_create(pcborProtected->v.bytes, - (int)pcborProtected->length, CBOR_CONTEXT_PARAM_COMMA & cbor_error); + (int)pcborProtected->length, CBOR_CONTEXT_PARAM_COMMA & cbor_error); + } CHECK_CONDITION_CBOR(cn != NULL, cbor_error); CHECK_CONDITION_CBOR( cn_cbor_array_append(pArray, cn, &cbor_error), cbor_error); cn = NULL; if ((pcborProtectedSign->length == 1) && - (pcborProtectedSign->v.bytes[0] == 0xa0)) + (pcborProtectedSign->v.bytes[0] == 0xa0)) { cn = cn_cbor_data_create(NULL, 0, CBOR_CONTEXT_PARAM_COMMA & cbor_error); - else + } + else { cn = cn_cbor_data_create(pcborProtectedSign->v.bytes, - (int)pcborProtectedSign->length, - CBOR_CONTEXT_PARAM_COMMA & cbor_error); + (int)pcborProtectedSign->length, + CBOR_CONTEXT_PARAM_COMMA & cbor_error); + } CHECK_CONDITION_CBOR(cn != NULL, cbor_error); CHECK_CONDITION_CBOR( cn_cbor_array_append(pArray, cn, &cbor_error), cbor_error); @@ -209,19 +216,22 @@ static bool BuildToBeSigned(byte **ppbToSign, f = true; errorReturn: - if (cn != NULL) + if (cn != NULL) { CN_CBOR_FREE(cn, context); - if (pArray != NULL) + } + if (pArray != NULL) { CN_CBOR_FREE(pArray, context); - if (pbToSign != NULL) + } + if (pbToSign != NULL) { COSE_FREE(pbToSign, context); + } return f; } bool _COSE_Signer_sign(COSE_SignerInfo *pSigner, const cn_cbor *pcborBody, const cn_cbor *pcborProtected, - const char * const contextString, + const char *const contextString, cose_errback *perr) { #ifdef USE_CBOR_CONTEXT @@ -240,12 +250,14 @@ bool _COSE_Signer_sign(COSE_SignerInfo *pSigner, cnAlgorithm = _COSE_map_get_int( &pSigner->m_message, COSE_Header_Algorithm, COSE_BOTH, perr); - if (cnAlgorithm == NULL) + if (cnAlgorithm == NULL) { goto errorReturn; + } if (cnAlgorithm->type == CN_CBOR_TEXT) { FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM); - } else { + } + else { CHECK_CONDITION((cnAlgorithm->type == CN_CBOR_UINT || cnAlgorithm->type == CN_CBOR_INT), COSE_ERR_INVALID_PARAMETER); @@ -254,45 +266,51 @@ bool _COSE_Signer_sign(COSE_SignerInfo *pSigner, } pcborProtectedSign = _COSE_encode_protected(&pSigner->m_message, perr); - if (pcborProtectedSign == NULL) + if (pcborProtectedSign == NULL) { goto errorReturn; + } if (!BuildToBeSigned(&pbToSign, &cbToSign, pcborBody, pcborProtected, - pcborProtectedSign, pSigner->m_message.m_pbExternal, - pSigner->m_message.m_cbExternal, - contextString, CBOR_CONTEXT_PARAM_COMMA perr)) + pcborProtectedSign, pSigner->m_message.m_pbExternal, + pSigner->m_message.m_cbExternal, contextString, + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } switch (alg) { #ifdef USE_ECDSA_SHA_256 case COSE_Algorithm_ECDSA_SHA_256: if (!ECDSA_Sign(&pSigner->m_message, INDEX_SIGNATURE, - pSigner->m_pkey, 256, pbToSign, cbToSign, perr)) + pSigner->m_pkey, 256, pbToSign, cbToSign, perr)) { goto errorReturn; + } break; #endif #ifdef USE_ECDSA_SHA_384 case COSE_Algorithm_ECDSA_SHA_384: if (!ECDSA_Sign(&pSigner->m_message, INDEX_SIGNATURE, - pSigner->m_pkey, 384, pbToSign, cbToSign, perr)) + pSigner->m_pkey, 384, pbToSign, cbToSign, perr)) { goto errorReturn; + } break; #endif #ifdef USE_ECDSA_SHA_512 case COSE_Algorithm_ECDSA_SHA_512: if (!ECDSA_Sign(&pSigner->m_message, INDEX_SIGNATURE, - pSigner->m_pkey, 512, pbToSign, cbToSign, perr)) + pSigner->m_pkey, 512, pbToSign, cbToSign, perr)) { goto errorReturn; + } break; #endif #ifdef USE_EDDSA case COSE_Algorithm_EdDSA: if (!EdDSA_Sign(&pSigner->m_message, INDEX_SIGNATURE, - pSigner->m_pkey, pbToSign, cbToSign, perr)) + pSigner->m_pkey, pbToSign, cbToSign, perr)) { goto errorReturn; + } break; #endif @@ -302,19 +320,22 @@ bool _COSE_Signer_sign(COSE_SignerInfo *pSigner, #if INCLUDE_COUNTERSIGNATURE if (pSigner->m_message.m_counterSigners != NULL) { - if (!_COSE_CounterSign_Sign(&pSigner->m_message, CBOR_CONTEXT_PARAM_COMMA perr)) { + if (!_COSE_CounterSign_Sign( + &pSigner->m_message, CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; } } #endif - + fRet = true; errorReturn: - if (pArray != NULL) + if (pArray != NULL) { COSE_FREE(pArray, context); - if (pbToSign != NULL) + } + if (pbToSign != NULL) { COSE_FREE(pbToSign, context); + } return fRet; } @@ -357,8 +378,9 @@ bool COSE_Signer_SetExternal(HCOSE_SIGNER hcose, cose_errback *perr) { if (!IsValidSignerHandle(hcose)) { - if (perr != NULL) + if (perr != NULL) { perr->err = COSE_ERR_INVALID_HANDLE; + } return false; } @@ -370,7 +392,7 @@ bool COSE_Signer_SetExternal(HCOSE_SIGNER hcose, bool _COSE_Signer_validate(COSE_SignerInfo *pSigner, const cn_cbor *pcborBody, const cn_cbor *pcborProtected, - const char * const contextString, + const char *const contextString, cose_errback *perr) { byte *pbToBeSigned = NULL; @@ -383,12 +405,14 @@ bool _COSE_Signer_validate(COSE_SignerInfo *pSigner, const cn_cbor *cn = _COSE_map_get_int( &pSigner->m_message, COSE_Header_Algorithm, COSE_BOTH, perr); - if (cn == NULL) + if (cn == NULL) { goto errorReturn; + } if (cn->type == CN_CBOR_TEXT) { FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM); - } else { + } + else { CHECK_CONDITION((cn->type == CN_CBOR_UINT || cn->type == CN_CBOR_INT), COSE_ERR_INVALID_PARAMETER); @@ -406,8 +430,10 @@ bool _COSE_Signer_validate(COSE_SignerInfo *pSigner, // Build authenticated data if (!BuildToBeSigned(&pbToBeSigned, &cbToBeSigned, pcborBody, pcborProtected, cnProtected, pSigner->m_message.m_pbExternal, - pSigner->m_message.m_cbExternal, contextString, CBOR_CONTEXT_PARAM_COMMA perr)) + pSigner->m_message.m_cbExternal, contextString, + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; + } cn_cbor *cnSignature = _COSE_arrayget_int(&pSigner->m_message, INDEX_SIGNATURE); @@ -419,32 +445,36 @@ bool _COSE_Signer_validate(COSE_SignerInfo *pSigner, #ifdef USE_ECDSA_SHA_256 case COSE_Algorithm_ECDSA_SHA_256: if (!ECDSA_Verify(&pSigner->m_message, INDEX_SIGNATURE, - pSigner->m_pkey, 256, pbToBeSigned, cbToBeSigned, perr)) + pSigner->m_pkey, 256, pbToBeSigned, cbToBeSigned, perr)) { goto errorReturn; + } break; #endif #ifdef USE_ECDSA_SHA_384 case COSE_Algorithm_ECDSA_SHA_384: if (!ECDSA_Verify(&pSigner->m_message, INDEX_SIGNATURE, - pSigner->m_pkey, 384, pbToBeSigned, cbToBeSigned, perr)) + pSigner->m_pkey, 384, pbToBeSigned, cbToBeSigned, perr)) { goto errorReturn; + } break; #endif #ifdef USE_ECDSA_SHA_512 case COSE_Algorithm_ECDSA_SHA_512: if (!ECDSA_Verify(&pSigner->m_message, INDEX_SIGNATURE, - pSigner->m_pkey, 512, pbToBeSigned, cbToBeSigned, perr)) + pSigner->m_pkey, 512, pbToBeSigned, cbToBeSigned, perr)) { goto errorReturn; + } break; #endif #ifdef USE_EDDSA case COSE_Algorithm_EdDSA: if (!EdDSA_Verify(&pSigner->m_message, INDEX_SIGNATURE, - pSigner->m_pkey, pbToBeSigned, cbToBeSigned, perr)) + pSigner->m_pkey, pbToBeSigned, cbToBeSigned, perr)) { goto errorReturn; + } break; #endif @@ -456,8 +486,9 @@ bool _COSE_Signer_validate(COSE_SignerInfo *pSigner, fRet = true; errorReturn: - if (pbToBeSigned != NULL) + if (pbToBeSigned != NULL) { COSE_FREE(pbToBeSigned, context); + } return fRet; } @@ -469,8 +500,9 @@ cn_cbor *COSE_Signer_map_get_int(HCOSE_SIGNER h, cose_errback *perr) { if (!IsValidSignerHandle(h)) { - if (perr != NULL) + if (perr != NULL) { perr->err = COSE_ERR_INVALID_HANDLE; + } return NULL; } diff --git a/src/cbor.c b/src/cbor.c index 54511bbc..2e34d4c8 100644 --- a/src/cbor.c +++ b/src/cbor.c @@ -57,8 +57,9 @@ bool cn_cbor_array_replace(cn_cbor *cb_array, cn_cbor *cb_temp2; if (!cb_array || !cb_value || cb_array->type != CN_CBOR_ARRAY) { - if (errp != NULL) + if (errp != NULL) { errp->err = CN_CBOR_ERR_INVALID_PARAMETER; + } return false; } @@ -135,8 +136,9 @@ cn_cbor *cn_cbor_clone(const cn_cbor *pIn, switch (pIn->type) { case CN_CBOR_TEXT: sz = CN_CBOR_CALLOC(pIn->length + 1, 1, context); - if (sz == NULL) + if (sz == NULL) { return NULL; + } memcpy(sz, pIn->v.str, pIn->length); sz[pIn->length] = 0; pOut = cn_cbor_string_create(sz CBOR_CONTEXT_PARAM, pcn_cbor_error); @@ -149,8 +151,9 @@ cn_cbor *cn_cbor_clone(const cn_cbor *pIn, case CN_CBOR_BYTES: pb = CN_CBOR_CALLOC((int)pIn->length, 1, context); - if (pb == NULL) + if (pb == NULL) { return NULL; + } memcpy(pb, pIn->v.bytes, pIn->length); pOut = cn_cbor_data_create( pb, (int)pIn->length CBOR_CONTEXT_PARAM, pcn_cbor_error); @@ -169,8 +172,9 @@ cn_cbor *cn_cbor_tag_create(int tag, { cn_cbor *pcnTag = CN_CALLOC(context); if (pcnTag == NULL) { - if (perr != NULL) + if (perr != NULL) { perr->err = CN_CBOR_ERR_OUT_OF_MEMORY; + } return NULL; } @@ -187,8 +191,9 @@ cn_cbor *cn_cbor_bool_create(int boolValue, { cn_cbor *pcn = CN_CALLOC(context); if (pcn == NULL) { - if (errp != NULL) + if (errp != NULL) { errp->err = CN_CBOR_ERR_OUT_OF_MEMORY; + } return NULL; } @@ -200,8 +205,9 @@ cn_cbor *cn_cbor_null_create(CBOR_CONTEXT_COMMA cn_cbor_errback *errp) { cn_cbor *pcn = CN_CALLOC(context); if (pcn == NULL) { - if (errp != NULL) + if (errp != NULL) { errp->err = CN_CBOR_ERR_OUT_OF_MEMORY; + } return NULL; } pcn->type = CN_CBOR_NULL; diff --git a/src/cose_int.h b/src/cose_int.h index 59fa0961..23f73083 100644 --- a/src/cose_int.h +++ b/src/cose_int.h @@ -37,10 +37,11 @@ typedef struct _COSE { #endif struct _COSE *m_handleList; #if INCLUDE_COUNTERSIGNATURE - COSE_CounterSign *m_counterSigners; // Linked list of all counter signatures + COSE_CounterSign + *m_counterSigners; // Linked list of all counter signatures #endif #if INCLUDE_COUNTERSIGNATURE1 - COSE_CounterSign1* m_counterSign1; + COSE_CounterSign1 *m_counterSign1; #endif } COSE; @@ -269,8 +270,8 @@ bool _COSE_SignerInfo_Init(COSE_INIT_FLAGS flags, bool _COSE_Signer_sign(COSE_SignerInfo *pSigner, const cn_cbor *pcborBody, const cn_cbor *pcborProtected, - const char * const contextString, - cose_errback *perr); + const char *const contextString, + cose_errback *perr); COSE_SignerInfo *_COSE_SignerInfo_Init_From_Object(cn_cbor *cbor, COSE_SignerInfo *pIn, CBOR_CONTEXT_COMMA cose_errback *perr); @@ -278,7 +279,7 @@ bool _COSE_SignerInfo_Release(COSE_SignerInfo *pSigner); bool _COSE_Signer_validate(COSE_SignerInfo *pSigner, const cn_cbor *pbContent, const cn_cbor *pbProtected, - const char * const szContext, + const char *const szContext, cose_errback *perr); // Sign1 items @@ -325,13 +326,12 @@ bool _COSE_CounterSign_add(COSE *pMessage, bool _COSE_CountSign_create(COSE *pMessage, cn_cbor *pcnBody, CBOR_CONTEXT_COMMA cose_errback *perr); -COSE_CounterSign * _COSE_CounterSign_Init_From_Object(cn_cbor* cbor, +COSE_CounterSign *_COSE_CounterSign_Init_From_Object(cn_cbor *cbor, COSE_CounterSign *, - CBOR_CONTEXT_COMMA cose_errback* perr); + CBOR_CONTEXT_COMMA cose_errback *perr); bool _COSE_CounterSign_Sign(COSE *baseMessage, CBOR_CONTEXT_COMMA cose_errback *perr); - // // Debugging Items diff --git a/src/mbedtls.c b/src/mbedtls.c index a1f2a6f3..adf2909f 100644 --- a/src/mbedtls.c +++ b/src/mbedtls.c @@ -132,7 +132,8 @@ bool AES_CCM_Encrypt(COSE_Enveloped *pcose, COSE_UNPROTECT_ONLY, perr)) goto errorReturn; cbor_iv_t = NULL; - } else { + } + else { CHECK_CONDITION( cbor_iv->type == CN_CBOR_BYTES, COSE_ERR_INVALID_PARAMETER); CHECK_CONDITION(cbor_iv->length == NSize, COSE_ERR_INVALID_PARAMETER); @@ -309,7 +310,8 @@ bool AES_GCM_Encrypt(COSE_Enveloped *pcose, COSE_UNPROTECT_ONLY, perr)) goto errorReturn; cbor_iv_t = NULL; - } else { + } + else { CHECK_CONDITION( cbor_iv->type == CN_CBOR_BYTES, COSE_ERR_INVALID_PARAMETER); CHECK_CONDITION(cbor_iv->length == 96 / 8, COSE_ERR_INVALID_PARAMETER); @@ -919,7 +921,8 @@ bool ECKey_From(const cn_cbor *pKey, if (p->type == CN_CBOR_UINT) { CHECK_CONDITION( p->v.uint == COSE_Key_Type_EC2, COSE_ERR_INVALID_PARAMETER); - } else { + } + else { FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER); } @@ -960,13 +963,16 @@ bool ECKey_From(const cn_cbor *pKey, cbKey = cbGroup * 2 + 1; CHECK_CONDITION(p->length == cbGroup, COSE_ERR_INVALID_PARAMETER); memcpy(rgbKey + p->length + 1, p->v.str, p->length); - } else if (p->type == CN_CBOR_TRUE) { + } + else if (p->type == CN_CBOR_TRUE) { cbKey = cbGroup + 1; rgbKey[0] = 0x03; - } else if (p->type == CN_CBOR_FALSE) { + } + else if (p->type == CN_CBOR_FALSE) { cbKey = cbGroup + 1; rgbKey[0] = 0x02; - } else + } + else FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER); CHECK_CONDITION(mbedtls_ecp_point_read_binary( @@ -1551,7 +1557,8 @@ bool ECDH_ComputeSecret(COSE *pRecipient, *ppKeyPrivate = pkey; pkey = NULL; - } else { + } + else { p = cn_cbor_mapget_int(*ppKeyPrivate, COSE_Key_EC_d); CHECK_CONDITION(p != NULL, COSE_ERR_INVALID_PARAMETER); diff --git a/src/openssl.c b/src/openssl.c index 8691bf42..5f1c87d4 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -226,7 +226,8 @@ bool AES_CCM_Encrypt(COSE_Enveloped *pcose, goto errorReturn; } cbor_iv_t = NULL; - } else { + } + else { CHECK_CONDITION( cbor_iv->type == CN_CBOR_BYTES, COSE_ERR_INVALID_PARAMETER); CHECK_CONDITION(cbor_iv->length == NSize, COSE_ERR_INVALID_PARAMETER); @@ -456,7 +457,8 @@ bool AES_GCM_Encrypt(COSE_Enveloped *pcose, goto errorReturn; } cbor_iv_t = NULL; - } else { + } + else { CHECK_CONDITION( cbor_iv->type == CN_CBOR_BYTES, COSE_ERR_INVALID_PARAMETER); CHECK_CONDITION(cbor_iv->length == 96 / 8, COSE_ERR_INVALID_PARAMETER); @@ -855,7 +857,8 @@ bool HKDF_Extract(COSE *pcose, CHECK_CONDITION( HMAC_Init_ex(ctx, cnSalt->v.bytes, (int)cnSalt->length, pmd, NULL), COSE_ERR_CRYPTO_FAIL); - } else { + } + else { CHECK_CONDITION(HMAC_Init_ex(ctx, rgbSalt, cbSalt, pmd, NULL), COSE_ERR_CRYPTO_FAIL); } @@ -1108,13 +1111,16 @@ EC_KEY *ECKey_From(const cn_cbor *pKey, int *cbGroup, cose_errback *perr) cbKey = (*cbGroup * 2) + 1; CHECK_CONDITION(p->length == *cbGroup, COSE_ERR_INVALID_PARAMETER); memcpy(rgbKey + p->length + 1, p->v.str, p->length); - } else if (p->type == CN_CBOR_TRUE) { + } + else if (p->type == CN_CBOR_TRUE) { cbKey = (*cbGroup) + 1; rgbKey[0] = POINT_CONVERSION_COMPRESSED + 1; - } else if (p->type == CN_CBOR_FALSE) { + } + else if (p->type == CN_CBOR_FALSE) { cbKey = (*cbGroup) + 1; rgbKey[0] = POINT_CONVERSION_COMPRESSED; - } else + } + else FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER); pPoint = EC_POINT_new(ecgroup); @@ -1196,7 +1202,8 @@ cn_cbor *EC_FromKey(const EC_KEY *pKey, CBOR_CONTEXT_COMMA cose_errback *perr) EC_POINT_point2oct(pgroup, pPoint, POINT_CONVERSION_COMPRESSED, pbOut, cbSize, NULL) == cbSize, COSE_ERR_CRYPTO_FAIL); - } else { + } + else { cbSize = EC_POINT_point2oct( pgroup, pPoint, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL); CHECK_CONDITION(cbSize > 0, COSE_ERR_CRYPTO_FAIL); @@ -1223,7 +1230,8 @@ cn_cbor *EC_FromKey(const EC_KEY *pKey, CBOR_CONTEXT_COMMA cose_errback *perr) CBOR_CONTEXT_PARAM_COMMA & cbor_error), cbor_error); p = NULL; - } else { + } + else { p = cn_cbor_data_create(pbOut + cbSize / 2 + 1, (int)(cbSize / 2), CBOR_CONTEXT_PARAM_COMMA & cbor_error); pbOut = NULL; // It is already part of the other one. @@ -1736,7 +1744,8 @@ bool ECDH_ComputeSecret(COSE *pRecipient, if (*ppKeyPrivate == NULL) { goto errorReturn; } - } else { + } + else { peckeyPrivate = ECKey_From(*ppKeyPrivate, &cbGroup, perr); if (peckeyPrivate == NULL) { goto errorReturn; diff --git a/test/context.c b/test/context.c index 99f36bdc..881517ed 100644 --- a/test/context.c +++ b/test/context.c @@ -41,7 +41,8 @@ bool CheckMemory(MyContext *pContext) assert(false); } } - } else if (p->pad[0] == (byte)0xef) { + } + else if (p->pad[0] == (byte)0xef) { for (unsigned i = 0; i < 4; i++) { if ((p->pad[i] != (byte)0xef) || (p->pad[i + 4 + p->size] != (byte)0xef)) { @@ -49,7 +50,8 @@ bool CheckMemory(MyContext *pContext) assert(false); } } - } else { + } + else { fprintf(stderr, "Incorrect pad value"); assert(false); } @@ -91,8 +93,9 @@ void MyFree(void *ptr, void *context) MyContext *myContext = (MyContext *)context; CheckMemory(myContext); - if (ptr == NULL) + if (ptr == NULL) { return; + } memset(&pb->pad, 0xab, pb->size + 8); } @@ -129,16 +132,15 @@ void FreeContext(cn_cbor_context *pContext) return; } -int IsContextEmpty(cn_cbor_context * pContext) +int IsContextEmpty(cn_cbor_context *pContext) { - MyContext* myContext = (MyContext*)pContext; - MyItem* p; + MyContext *myContext = (MyContext *)pContext; + MyItem *p; int i = 0; - // Walk memory and check every block - for (p = (MyItem*)myContext->pFirst; p != NULL; p = p->pNext) { + for (p = (MyItem *)myContext->pFirst; p != NULL; p = p->pNext) { if (p->pad[0] == (byte)0xab) { // Block has been freed } @@ -151,4 +153,4 @@ int IsContextEmpty(cn_cbor_context * pContext) return i; } -#endif // USE_CBOR_CONTEXT +#endif // USE_CBOR_CONTEXT diff --git a/test/encrypt.c b/test/encrypt.c index 64ff5fb7..5ac5aac8 100644 --- a/test/encrypt.c +++ b/test/encrypt.c @@ -51,8 +51,9 @@ bool DecryptMessage(const byte *pbEncoded, } if (!SetReceivingAttributes( - (HCOSE)hEnc, pEnveloped, Attributes_Enveloped_protected)) + (HCOSE)hEnc, pEnveloped, Attributes_Enveloped_protected)) { goto errorReturn; + } cn_cbor *alg = COSE_Enveloped_map_get_int( hEnc, COSE_Header_Algorithm, COSE_BOTH, NULL); @@ -61,26 +62,32 @@ bool DecryptMessage(const byte *pbEncoded, } hRecip1 = COSE_Enveloped_GetRecipient(hEnc, iRecipient1, NULL); - if (hRecip1 == NULL) + if (hRecip1 == NULL) { goto errorReturn; + } if (!SetReceivingAttributes( - (HCOSE)hRecip1, pRecipient1, Attributes_Recipient_protected)) + (HCOSE)hRecip1, pRecipient1, Attributes_Recipient_protected)) { goto errorReturn; + } if (pRecipient2 != NULL) { pkey = BuildKey(cn_cbor_mapget_string(pRecipient2, "key"), false); - if (pkey == NULL) + if (pkey == NULL) { goto errorReturn; + } hRecip2 = COSE_Recipient_GetRecipient(hRecip1, iRecipient2, NULL); - if (hRecip2 == NULL) + if (hRecip2 == NULL) { goto errorReturn; + } if (!SetReceivingAttributes( - (HCOSE)hRecip2, pRecipient2, Attributes_Recipient_protected)) + (HCOSE)hRecip2, pRecipient2, Attributes_Recipient_protected)) { goto errorReturn; - if (!COSE_Recipient_SetKey(hRecip2, pkey, NULL)) + } + if (!COSE_Recipient_SetKey(hRecip2, pkey, NULL)) { goto errorReturn; + } cn_cbor *cnStatic = cn_cbor_mapget_string(pRecipient2, "sender_key"); if (cnStatic != NULL) { @@ -92,12 +99,15 @@ bool DecryptMessage(const byte *pbEncoded, } hRecip = hRecip2; - } else { + } + else { pkey = BuildKey(cn_cbor_mapget_string(pRecipient1, "key"), false); - if (pkey == NULL) + if (pkey == NULL) { goto errorReturn; - if (!COSE_Recipient_SetKey(hRecip1, pkey, NULL)) + } + if (!COSE_Recipient_SetKey(hRecip1, pkey, NULL)) { goto errorReturn; + } cn_cbor *cnStatic = cn_cbor_mapget_string(pRecipient1, "sender_key"); if (cnStatic != NULL) { @@ -113,33 +123,40 @@ bool DecryptMessage(const byte *pbEncoded, if (!fFailBody) { cn_cbor *cn = cn_cbor_mapget_string(pRecipient1, "fail"); - if (cn != NULL && (cn->type == CN_CBOR_TRUE)) + if (cn != NULL && (cn->type == CN_CBOR_TRUE)) { fFailBody = true; + } if (fFailBody && (pRecipient2 != NULL)) { cn = cn_cbor_mapget_string(pRecipient2, "fail"); - if (cn != NULL && (cn->type == CN_CBOR_TRUE)) + if (cn != NULL && (cn->type == CN_CBOR_TRUE)) { fFailBody = true; + } } if (hRecip2 != NULL) { alg = COSE_Recipient_map_get_int( hRecip2, COSE_Header_Algorithm, COSE_BOTH, NULL); - if (!IsAlgorithmSupported(alg)) + if (!IsAlgorithmSupported(alg)) { fNoSupport = true; + } } alg = COSE_Recipient_map_get_int( hRecip1, COSE_Header_Algorithm, COSE_BOTH, NULL); - if (!IsAlgorithmSupported(alg)) + if (!IsAlgorithmSupported(alg)) { fNoSupport = true; + } } if (COSE_Enveloped_decrypt(hEnc, hRecip, NULL)) { fRet = !fFailBody; - } else { - if (fNoSupport) + } + else { + if (fNoSupport) { fRet = false; - else + } + else { fRet = fFailBody; + } } #if INCLUDE_COUNTERSIGNATURE @@ -205,7 +222,6 @@ bool DecryptMessage(const byte *pbEncoded, } #endif - #if INCLUDE_COUNTERSIGNATURE // Countersign on Enveloped Body @@ -269,17 +285,20 @@ bool DecryptMessage(const byte *pbEncoded, } #endif - - if (!fRet && !fNoSupport) + if (!fRet && !fNoSupport) { CFails++; + } errorReturn: - if (hEnc != NULL) + if (hEnc != NULL) { COSE_Enveloped_Free(hEnc); - if (hRecip1 != NULL) + } + if (hRecip1 != NULL) { COSE_Recipient_Free(hRecip1); - if (hRecip2 != NULL) + } + if (hRecip2 != NULL) { COSE_Recipient_Free(hRecip2); + } return fRet; } @@ -301,15 +320,18 @@ int _ValidateEnveloped(const cn_cbor *pControl, fFailBody = true; } - if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) + if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) { goto errorReturn; + } pEnveloped = cn_cbor_mapget_string(pInput, "enveloped"); - if ((pEnveloped == NULL) || (pEnveloped->type != CN_CBOR_MAP)) + if ((pEnveloped == NULL) || (pEnveloped->type != CN_CBOR_MAP)) { goto errorReturn; + } pRecipients = cn_cbor_mapget_string(pEnveloped, "recipients"); - if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) + if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) { goto errorReturn; + } iRecipient = (int)pRecipients->length - 1; pRecipients = pRecipients->first_child; @@ -317,15 +339,18 @@ int _ValidateEnveloped(const cn_cbor *pControl, cn_cbor *pRecip2 = cn_cbor_mapget_string(pRecipients, "recipients"); if (pRecip2 == NULL) { if (DecryptMessage(pbEncoded, cbEncoded, fFailBody, pEnveloped, - pRecipients, iRecipient, NULL, 0)) + pRecipients, iRecipient, NULL, 0)) { passCount++; - } else { + } + } + else { int iRecipient2 = (int)(pRecip2->length - 1); pRecip2 = pRecip2->first_child; for (; pRecip2 != NULL; pRecip2 = pRecip2->next, iRecipient2--) { if (DecryptMessage(pbEncoded, cbEncoded, fFailBody, pEnveloped, - pRecipients, iRecipient, pRecip2, iRecipient2)) + pRecipients, iRecipient, pRecip2, iRecipient2)) { passCount++; + } } } } @@ -348,31 +373,37 @@ HCOSE_RECIPIENT BuildRecipient(const cn_cbor *pRecipient) { HCOSE_RECIPIENT hRecip = COSE_Recipient_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); - if (hRecip == NULL) + if (hRecip == NULL) { goto returnError; + } if (!SetSendingAttributes( - (HCOSE)hRecip, pRecipient, Attributes_Recipient_protected)) + (HCOSE)hRecip, pRecipient, Attributes_Recipient_protected)) { goto returnError; + } cn_cbor *cnKey = cn_cbor_mapget_string(pRecipient, "key"); if (cnKey != NULL) { cn_cbor *pkey = BuildKey(cnKey, true); - if (pkey == NULL) + if (pkey == NULL) { goto returnError; + } - if (!COSE_Recipient_SetKey(hRecip, pkey, NULL)) + if (!COSE_Recipient_SetKey(hRecip, pkey, NULL)) { goto returnError; + } } cnKey = cn_cbor_mapget_string(pRecipient, "recipients"); if (cnKey != NULL) { for (cnKey = cnKey->first_child; cnKey != NULL; cnKey = cnKey->next) { HCOSE_RECIPIENT hRecip2 = BuildRecipient(cnKey); - if (hRecip2 == NULL) + if (hRecip2 == NULL) { goto returnError; - if (!COSE_Recipient_AddRecipient(hRecip, hRecip2, NULL)) + } + if (!COSE_Recipient_AddRecipient(hRecip, hRecip2, NULL)) { goto returnError; + } COSE_Recipient_Free(hRecip2); } } @@ -382,12 +413,13 @@ HCOSE_RECIPIENT BuildRecipient(const cn_cbor *pRecipient) cn_cbor *pSendKey = BuildKey(pSenderKey, false); cn_cbor *pKid = cn_cbor_mapget_string(pSenderKey, "kid"); if (!COSE_Recipient_SetSenderKey( - hRecip, pSendKey, (pKid == NULL) ? 2 : 1, NULL)) + hRecip, pSendKey, (pKid == NULL) ? 2 : 1, NULL)) { goto returnError; + } } #if INCLUDE_COUNTERSIGNATURE - // On the Recipient + // On the Recipient cn_cbor *countersigns1 = cn_cbor_mapget_string(pRecipient, "countersign"); if (countersigns1 != NULL) { countersigns1 = cn_cbor_mapget_string(countersigns1, "signers"); @@ -442,48 +474,57 @@ int BuildEnvelopedMessage(const cn_cbor *pControl) // const cn_cbor *pFail = cn_cbor_mapget_string(pControl, "fail"); - if ((pFail != NULL) && (pFail->type == CN_CBOR_TRUE)) + if ((pFail != NULL) && (pFail->type == CN_CBOR_TRUE)) { return 0; + } HCOSE_ENVELOPED hEncObj = COSE_Enveloped_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); const cn_cbor *pInputs = cn_cbor_mapget_string(pControl, "input"); - if (pInputs == NULL) + if (pInputs == NULL) { goto returnError; + } const cn_cbor *pEnveloped = cn_cbor_mapget_string(pInputs, "enveloped"); - if (pEnveloped == NULL) + if (pEnveloped == NULL) { goto returnError; + } const cn_cbor *pContent = cn_cbor_mapget_string(pInputs, "plaintext"); if (!COSE_Enveloped_SetContent( - hEncObj, pContent->v.bytes, pContent->length, NULL)) + hEncObj, pContent->v.bytes, pContent->length, NULL)) { goto returnError; + } if (!SetSendingAttributes( - (HCOSE)hEncObj, pEnveloped, Attributes_Enveloped_protected)) + (HCOSE)hEncObj, pEnveloped, Attributes_Enveloped_protected)) { goto returnError; + } const cn_cbor *pAlg = COSE_Enveloped_map_get_int(hEncObj, 1, COSE_BOTH, NULL); - if (pAlg == NULL) + if (pAlg == NULL) { goto returnError; + } const cn_cbor *pRecipients = cn_cbor_mapget_string(pEnveloped, "recipients"); - if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) + if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) { goto returnError; + } pRecipients = pRecipients->first_child; for (iRecipient = 0; pRecipients != NULL; iRecipient++, pRecipients = pRecipients->next) { HCOSE_RECIPIENT hRecip = BuildRecipient(pRecipients); - if (hRecip == NULL) + if (hRecip == NULL) { goto returnError; + } - if (!COSE_Enveloped_AddRecipient(hEncObj, hRecip, NULL)) + if (!COSE_Enveloped_AddRecipient(hEncObj, hRecip, NULL)) { goto returnError; - + } + COSE_Recipient_Free(hRecip); } @@ -560,39 +601,49 @@ int EncryptMessage() char *sz = "This is the content to be used"; HCOSE_RECIPIENT hRecip = NULL; - if (hEncObj == NULL) + if (hEncObj == NULL) { goto errorReturn; + } if (!COSE_Enveloped_map_put_int(hEncObj, COSE_Header_Algorithm, - cn_cbor_int_create(COSE_Algorithm_AES_CCM_16_64_128, - CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) + cn_cbor_int_create(COSE_Algorithm_AES_CCM_16_64_128, + CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { goto errorReturn; - if (!COSE_Enveloped_SetContent(hEncObj, (byte *)sz, strlen(sz), NULL)) + } + if (!COSE_Enveloped_SetContent(hEncObj, (byte *)sz, strlen(sz), NULL)) { goto errorReturn; + } if (!COSE_Enveloped_map_put_int(hEncObj, COSE_Header_IV, - cn_cbor_data_create(rgbKid, 13, CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_UNPROTECT_ONLY, NULL)) + cn_cbor_data_create(rgbKid, 13, CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_UNPROTECT_ONLY, NULL)) { goto errorReturn; + } hRecip = COSE_Recipient_from_shared_secret( rgbSecret, cbSecret, rgbKid, cbKid, CBOR_CONTEXT_PARAM_COMMA NULL); - if (hRecip == NULL) + if (hRecip == NULL) { goto errorReturn; - if (!COSE_Enveloped_AddRecipient(hEncObj, hRecip, NULL)) + } + if (!COSE_Enveloped_AddRecipient(hEncObj, hRecip, NULL)) { goto errorReturn; + } - if (!COSE_Enveloped_encrypt(hEncObj, NULL)) + if (!COSE_Enveloped_encrypt(hEncObj, NULL)) { goto errorReturn; + } cb = COSE_Encode((HCOSE)hEncObj, NULL, 0, 0); - if (cb < 1) + if (cb < 1) { goto errorReturn; + } rgb = (byte *)malloc(cb); - if (rgb == NULL) + if (rgb == NULL) { goto errorReturn; + } cb = COSE_Encode((HCOSE)hEncObj, rgb, 0, cb); - if (cb < 1) + if (cb < 1) { goto errorReturn; + } COSE_Recipient_Free(hRecip); hRecip = NULL; @@ -620,21 +671,25 @@ int EncryptMessage() int typ; hEncObj = (HCOSE_ENVELOPED)COSE_Decode(rgb, (int)cb, &typ, COSE_enveloped_object, CBOR_CONTEXT_PARAM_COMMA NULL); - if (hEncObj == NULL) + if (hEncObj == NULL) { goto errorReturn; + } int iRecipient = 0; do { hRecip = COSE_Enveloped_GetRecipient(hEncObj, iRecipient, NULL); - if (hRecip == NULL) + if (hRecip == NULL) { break; + } if (!COSE_Recipient_SetKey_secret( - hRecip, rgbSecret, cbSecret, NULL, 0, NULL)) + hRecip, rgbSecret, cbSecret, NULL, 0, NULL)) { goto errorReturn; + } - if (!COSE_Enveloped_decrypt(hEncObj, hRecip, NULL)) + if (!COSE_Enveloped_decrypt(hEncObj, hRecip, NULL)) { goto errorReturn; + } COSE_Recipient_Free(hRecip); hRecip = NULL; @@ -647,10 +702,12 @@ int EncryptMessage() return 1; errorReturn: - if (hEncObj != NULL) + if (hEncObj != NULL) { COSE_Enveloped_Free(hEncObj); - if (hRecip != NULL) + } + if (hRecip != NULL) { COSE_Recipient_Free(hRecip); + } CFails++; return 0; } @@ -679,15 +736,18 @@ int _ValidateEncrypt(const cn_cbor *pControl, fFailBody = true; } - if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) + if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) { goto returnError; + } pEncrypt = cn_cbor_mapget_string(pInput, "encrypted"); - if ((pEncrypt == NULL) || (pEncrypt->type != CN_CBOR_MAP)) + if ((pEncrypt == NULL) || (pEncrypt->type != CN_CBOR_MAP)) { goto returnError; + } pRecipients = cn_cbor_mapget_string(pEncrypt, "recipients"); - if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) + if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) { goto returnError; + } pRecipients = pRecipients->first_child; @@ -695,29 +755,36 @@ int _ValidateEncrypt(const cn_cbor *pControl, hEnc = (HCOSE_ENCRYPT)COSE_Decode(pbEncoded, cbEncoded, &type, COSE_encrypt_object, CBOR_CONTEXT_PARAM_COMMA NULL); if (hEnc == NULL) { - if (fFailBody) + if (fFailBody) { return 0; - else + } + else { goto returnError; + } } - } else { + } + else { hEnc = COSE_Encrypt_Init_From_Object( pcnEncoded, CBOR_CONTEXT_PARAM_COMMA NULL); if (hEnc == NULL) { - if (fFailBody) + if (fFailBody) { return 0; - else + } + else { goto returnError; + } } } if (!SetReceivingAttributes( - (HCOSE)hEnc, pEncrypt, Attributes_Encrypt_protected)) + (HCOSE)hEnc, pEncrypt, Attributes_Encrypt_protected)) { goto returnError; + } cn_cbor *pkey = BuildKey(cn_cbor_mapget_string(pRecipients, "key"), true); - if (pkey == NULL) + if (pkey == NULL) { goto returnError; + } cn_cbor *k = cn_cbor_mapget_int(pkey, -1); if (k == NULL) { @@ -738,7 +805,8 @@ int _ValidateEncrypt(const cn_cbor *pControl, if (!fAlgSupport) { fFail = true; fAlgSupport = false; - } else if ((pFail != NULL) && (pFail->type != CN_CBOR_TRUE)) { + } + else if ((pFail != NULL) && (pFail->type != CN_CBOR_TRUE)) { fFail = true; } @@ -746,12 +814,14 @@ int _ValidateEncrypt(const cn_cbor *pControl, const byte *pb; pb = COSE_Encrypt_GetContent(hEnc, &cb, NULL); } - else { + else { if (fAlgSupport) { fFail = true; fAlgSupport = false; - } else if ((pFail == NULL) || (pFail->type == CN_CBOR_FALSE)) + } + else if ((pFail == NULL) || (pFail->type == CN_CBOR_FALSE)) { fFail = true; + } } #if INCLUDE_COUNTERSIGNATURE @@ -822,17 +892,21 @@ int _ValidateEncrypt(const cn_cbor *pControl, if (fAlgSupport) { if (fFailBody) { - if (!fFail) + if (!fFail) { fFail = true; - else + } + else { fFail = false; + } } - } else { + } + else { fFail = false; } - if (fFail) + if (fFail) { CFails += 1; + } return fAlgSupport ? 1 : 0; returnError: @@ -847,13 +921,15 @@ int ValidateEncrypt(const cn_cbor *pControl) int fRet; fRet = _ValidateEncrypt(pControl, pbEncoded, cbEncoded, NULL); - if (!fRet) + if (!fRet) { return fRet; + } cn_cbor *cbor = cn_cbor_decode(pbEncoded, cbEncoded, CBOR_CONTEXT_PARAM_COMMA NULL); - if (cbor == NULL) + if (cbor == NULL) { return false; + } return _ValidateEncrypt(pControl, NULL, 0, cbor); } @@ -865,39 +941,47 @@ int BuildEncryptMessage(const cn_cbor *pControl) // const cn_cbor *pFail = cn_cbor_mapget_string(pControl, "fail"); - if ((pFail != NULL) && (pFail->type == CN_CBOR_TRUE)) + if ((pFail != NULL) && (pFail->type == CN_CBOR_TRUE)) { return 0; + } HCOSE_ENCRYPT hEncObj = COSE_Encrypt_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); const cn_cbor *pInputs = cn_cbor_mapget_string(pControl, "input"); - if (pInputs == NULL) + if (pInputs == NULL) { goto returnError; + } const cn_cbor *pEncrypt = cn_cbor_mapget_string(pInputs, "encrypted"); - if (pEncrypt == NULL) + if (pEncrypt == NULL) { goto returnError; + } const cn_cbor *pContent = cn_cbor_mapget_string(pInputs, "plaintext"); if (!COSE_Encrypt_SetContent( - hEncObj, pContent->v.bytes, pContent->length, NULL)) + hEncObj, pContent->v.bytes, pContent->length, NULL)) { goto returnError; + } if (!SetSendingAttributes( - (HCOSE)hEncObj, pEncrypt, Attributes_Encrypt_protected)) + (HCOSE)hEncObj, pEncrypt, Attributes_Encrypt_protected)) { goto returnError; + } const cn_cbor *pAlg = COSE_Encrypt_map_get_int(hEncObj, 1, COSE_BOTH, NULL); - if (pAlg == NULL) + if (pAlg == NULL) { goto returnError; + } const cn_cbor *pRecipients = cn_cbor_mapget_string(pEncrypt, "recipients"); - if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) + if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) { goto returnError; + } pRecipients = pRecipients->first_child; cn_cbor *pkey = BuildKey(cn_cbor_mapget_string(pRecipients, "key"), false); - if (pkey == NULL) + if (pkey == NULL) { goto returnError; + } cn_cbor *k = cn_cbor_mapget_int(pkey, -1); @@ -930,7 +1014,8 @@ int BuildEncryptMessage(const cn_cbor *pControl) goto returnError; } - if (!COSE_Encrypt0_add_countersignature(hEncObj, hCountersign, NULL)) { + if (!COSE_Encrypt0_add_countersignature( + hEncObj, hCountersign, NULL)) { goto returnError; } @@ -940,8 +1025,9 @@ int BuildEncryptMessage(const cn_cbor *pControl) #endif - if (!COSE_Encrypt_encrypt(hEncObj, k->v.bytes, k->length, NULL)) + if (!COSE_Encrypt_encrypt(hEncObj, k->v.bytes, k->length, NULL)) { goto returnError; + } size_t cb = COSE_Encode((HCOSE)hEncObj, NULL, 0, 0) + 1; byte *rgb = (byte *)malloc(cb); @@ -1063,10 +1149,12 @@ void Enveloped_Corners() CHECK_FAILURE(COSE_Enveloped_SetExternal(hEncrypt, NULL, 10, &cose_error), COSE_ERR_INVALID_PARAMETER, CFails++); - if (!COSE_Enveloped_Free(hEncrypt)) + if (!COSE_Enveloped_Free(hEncrypt)) { CFails++; - if (!COSE_Recipient_Free(hRecipient)) + } + if (!COSE_Recipient_Free(hRecipient)) { CFails++; + } // // Unsupported algorithm @@ -1074,8 +1162,9 @@ void Enveloped_Corners() // Bad Int algorithm hEncrypt = COSE_Enveloped_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); - if (hEncrypt == NULL) + if (hEncrypt == NULL) { CFails++; + } CHECK_RETURN( COSE_Enveloped_SetContent(hEncrypt, (byte *)"Message", 7, &cose_error), COSE_ERR_NONE, CFails++); @@ -1085,8 +1174,9 @@ void Enveloped_Corners() COSE_ERR_NONE, CFails++); hRecipient = COSE_Recipient_from_shared_secret( rgb, sizeof(rgb), rgb, sizeof(rgb), CBOR_CONTEXT_PARAM_COMMA NULL); - if (hRecipient == NULL) + if (hRecipient == NULL) { CFails++; + } CHECK_RETURN(COSE_Enveloped_AddRecipient(hEncrypt, hRecipient, &cose_error), COSE_ERR_NONE, CFails++); CHECK_FAILURE(COSE_Enveloped_encrypt(hEncrypt, &cose_error), @@ -1095,8 +1185,9 @@ void Enveloped_Corners() COSE_Enveloped_Free(hEncrypt); hEncrypt = COSE_Enveloped_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); - if (hEncrypt == NULL) + if (hEncrypt == NULL) { CFails++; + } CHECK_RETURN( COSE_Enveloped_SetContent(hEncrypt, (byte *)"Message", 7, &cose_error), COSE_ERR_NONE, CFails++); @@ -1107,8 +1198,9 @@ void Enveloped_Corners() COE_ERR_NONE, CFails++); hRecipient = COSE_Recipient_from_shared_secret( rgb, sizeof(rgb), rgb, sizeof(rgb), CBOR_CONTEXT_PARAM_COMMA NULL); - if (hRecipient == NULL) + if (hRecipient == NULL) { CFails++; + } CHECK_RETURN(COSE_Enveloped_AddRecipient(hEncrypt, hRecipient, &cose_error), COSE_ERR_NONE, CFails++); CHECK_FAILURE(COSE_Enveloped_encrypt(hEncrypt, &cose_error), @@ -1141,22 +1233,29 @@ void Encrypt_Corners() // NULL Handle checks - if (COSE_Encrypt_SetContent(hEncrypt, rgb, 10, NULL)) + if (COSE_Encrypt_SetContent(hEncrypt, rgb, 10, NULL)) { CFails++; - if (COSE_Encrypt_map_get_int(hEncrypt, 1, COSE_BOTH, NULL)) + } + if (COSE_Encrypt_map_get_int(hEncrypt, 1, COSE_BOTH, NULL)) { CFails++; - if (COSE_Encrypt_map_put_int(hEncrypt, 1, cn, COSE_PROTECT_ONLY, NULL)) + } + if (COSE_Encrypt_map_put_int(hEncrypt, 1, cn, COSE_PROTECT_ONLY, NULL)) { CFails++; - if (COSE_Encrypt_SetExternal(hEncrypt, rgb, 10, NULL)) + } + if (COSE_Encrypt_SetExternal(hEncrypt, rgb, 10, NULL)) { CFails++; - if (COSE_Encrypt_encrypt(hEncrypt, rgb, sizeof(rgb), NULL)) + } + if (COSE_Encrypt_encrypt(hEncrypt, rgb, sizeof(rgb), NULL)) { CFails++; - if (COSE_Encrypt_decrypt(hEncrypt, rgb, sizeof(rgb), NULL)) + } + if (COSE_Encrypt_decrypt(hEncrypt, rgb, sizeof(rgb), NULL)) { CFails++; - if (COSE_Encrypt_Free((HCOSE_ENCRYPT)hEncrypt)) + } + if (COSE_Encrypt_Free((HCOSE_ENCRYPT)hEncrypt)) { CFails++; + } - // Wrong type of handle checks + // Wrong type of handle checks #if INCLUDE_MAC hEncrypt = (HCOSE_ENCRYPT)COSE_Mac_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); @@ -1164,46 +1263,59 @@ void Encrypt_Corners() hEncrypt = (HCOSE_ENCRYPT)COSE_CALLOC(1, sizeof(COSE), context); #endif - if (COSE_Encrypt_SetContent(hEncrypt, rgb, 10, NULL)) + if (COSE_Encrypt_SetContent(hEncrypt, rgb, 10, NULL)) { CFails++; - if (COSE_Encrypt_map_get_int(hEncrypt, 1, COSE_BOTH, NULL)) + } + if (COSE_Encrypt_map_get_int(hEncrypt, 1, COSE_BOTH, NULL)) { CFails++; - if (COSE_Encrypt_map_put_int(hEncrypt, 1, cn, COSE_PROTECT_ONLY, NULL)) + } + if (COSE_Encrypt_map_put_int(hEncrypt, 1, cn, COSE_PROTECT_ONLY, NULL)) { CFails++; - if (COSE_Encrypt_encrypt(hEncrypt, rgb, sizeof(rgb), NULL)) + } + if (COSE_Encrypt_encrypt(hEncrypt, rgb, sizeof(rgb), NULL)) { CFails++; - if (COSE_Encrypt_SetExternal(hEncrypt, rgb, 10, NULL)) + } + if (COSE_Encrypt_SetExternal(hEncrypt, rgb, 10, NULL)) { CFails++; - if (COSE_Encrypt_decrypt(hEncrypt, rgb, sizeof(rgb), NULL)) + } + if (COSE_Encrypt_decrypt(hEncrypt, rgb, sizeof(rgb), NULL)) { CFails++; - if (COSE_Encrypt_Free(hEncrypt)) + } + if (COSE_Encrypt_Free(hEncrypt)) { CFails++; + } // // Unsupported algorithm hEncrypt = COSE_Encrypt_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); - if (hEncrypt == NULL) + if (hEncrypt == NULL) { CFails++; - if (!COSE_Encrypt_SetContent(hEncrypt, (byte *)"Message", 7, NULL)) + } + if (!COSE_Encrypt_SetContent(hEncrypt, (byte *)"Message", 7, NULL)) { CFails++; + } if (!COSE_Encrypt_map_put_int(hEncrypt, COSE_Header_Algorithm, - cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) + cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; + } CHECK_FAILURE(COSE_Encrypt_encrypt(hEncrypt, rgb, sizeof(rgb), &cose_error), COSE_ERR_UNKNOWN_ALGORITHM, CFails++); COSE_Encrypt_Free(hEncrypt); hEncrypt = COSE_Encrypt_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); - if (hEncrypt == NULL) + if (hEncrypt == NULL) { CFails++; - if (!COSE_Encrypt_SetContent(hEncrypt, (byte *)"Message", 7, NULL)) + } + if (!COSE_Encrypt_SetContent(hEncrypt, (byte *)"Message", 7, NULL)) { CFails++; + } if (!COSE_Encrypt_map_put_int(hEncrypt, COSE_Header_Algorithm, - cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) + cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; + } CHECK_FAILURE(COSE_Encrypt_encrypt(hEncrypt, rgb, sizeof(rgb), &cose_error), COSE_ERR_UNKNOWN_ALGORITHM, CFails++); COSE_Encrypt_Free(hEncrypt); diff --git a/test/json.c b/test/json.c index ce92fd01..0eb426da 100644 --- a/test/json.c +++ b/test/json.c @@ -54,9 +54,11 @@ const cn_cbor *ParseString(char *rgch, int ib, int cch) break; case '"': - for (ib2 = ib + 1; ib2 < cch; ib2++) - if (rgch[ib2] == '"') + for (ib2 = ib + 1; ib2 < cch; ib2++) { + if (rgch[ib2] == '"') { break; + } + } rgch[ib2] = 0; node = cn_cbor_string_create( &rgch[ib + 1], CBOR_CONTEXT_PARAM_COMMA NULL); @@ -65,8 +67,9 @@ const cn_cbor *ParseString(char *rgch, int ib, int cch) break; case 't': - if (strncmp(&rgch[ib], "true", 4) != 0) + if (strncmp(&rgch[ib], "true", 4) != 0) { goto error; + } node = cn_cbor_data_create(NULL, 0, CBOR_CONTEXT_PARAM_COMMA NULL); node->type = CN_CBOR_TRUE; @@ -74,8 +77,9 @@ const cn_cbor *ParseString(char *rgch, int ib, int cch) break; case 'f': - if (strncmp(&rgch[ib], "false", 5) != 0) + if (strncmp(&rgch[ib], "false", 5) != 0) { goto error; + } node = cn_cbor_data_create(NULL, 0, CBOR_CONTEXT_PARAM_COMMA NULL); node->type = CN_CBOR_FALSE; @@ -95,10 +99,12 @@ const cn_cbor *ParseString(char *rgch, int ib, int cch) case '-': node = cn_cbor_int_create( atol(&rgch[ib]), CBOR_CONTEXT_PARAM_COMMA NULL); - if (rgch[ib] == '-') + if (rgch[ib] == '-') { ib++; - while (isdigit(rgch[ib])) + } + while (isdigit(rgch[ib])) { ib++; + } ib--; break; @@ -113,7 +119,8 @@ const cn_cbor *ParseString(char *rgch, int ib, int cch) if (parent->last_child != NULL) { parent->last_child->next = node; parent->last_child = node; - } else { + } + else { parent->first_child = node; } parent->last_child = node; @@ -125,8 +132,9 @@ const cn_cbor *ParseString(char *rgch, int ib, int cch) } if (parent == NULL) { parent = node; - if (root == NULL) + if (root == NULL) { root = node; + } } } @@ -169,8 +177,9 @@ char *base64_encode(const unsigned char *data, *output_length = 4 * ((input_length + 2) / 3); char *encoded_data = malloc(*output_length); - if (encoded_data == NULL) + if (encoded_data == NULL) { return NULL; + } for (size_t i = 0, j = 0; i < input_length;) { uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0; @@ -185,8 +194,9 @@ char *base64_encode(const unsigned char *data, encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F]; } - for (int i = 0; i < mod_table[input_length % 3]; i++) + for (int i = 0; i < mod_table[input_length % 3]; i++) { encoded_data[*output_length - 1 - i] = '='; + } return encoded_data; } @@ -197,8 +207,9 @@ unsigned char *base64_decode(const char *data, { char *p = NULL; - if (decoding_table == NULL) + if (decoding_table == NULL) { build_decoding_table(); + } if (input_length % 4 != 0) { int c = 4 - (input_length % 4); @@ -210,15 +221,18 @@ unsigned char *base64_decode(const char *data, } *output_length = input_length / 4 * 3; - if (data[input_length - 1] == '=') + if (data[input_length - 1] == '=') { (*output_length)--; - if (data[input_length - 2] == '=') + } + if (data[input_length - 2] == '=') { (*output_length)--; + } unsigned char *decoded_data = malloc(*output_length); if (decoded_data == NULL) { - if (p != NULL) + if (p != NULL) { free(p); + } return NULL; } @@ -235,12 +249,15 @@ unsigned char *base64_decode(const char *data, uint32_t triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + (sextet_d << 0 * 6); - if (j < *output_length) + if (j < *output_length) { decoded_data[j++] = (triple >> 2 * 8) & 0xFF; - if (j < *output_length) + } + if (j < *output_length) { decoded_data[j++] = (triple >> 1 * 8) & 0xFF; - if (j < *output_length) + } + if (j < *output_length) { decoded_data[j++] = (triple >> 0 * 8) & 0xFF; + } } free(p); @@ -251,8 +268,9 @@ static void build_decoding_table() { decoding_table = malloc(256); - for (int i = 0; i < 64; i++) + for (int i = 0; i < 64; i++) { decoding_table[(int)encoding_table[i]] = (unsigned char)i; + } } void base64_cleanup() @@ -278,18 +296,23 @@ unsigned char *hex_decode(const char *data, for (unsigned int i = 0, j = 0; i < input_length; i++) { int c; - if ('0' <= data[i] && data[i] <= '9') + if ('0' <= data[i] && data[i] <= '9') { c = data[i] - '0'; - else if ('A' <= data[i] && data[i] <= 'F') + } + else if ('A' <= data[i] && data[i] <= 'F') { c = data[i] - 'A' + 10; - else if ('a' <= data[i] && data[i] <= 'f') + } + else if ('a' <= data[i] && data[i] <= 'f') { c = data[i] - 'a' + 10; - else + } + else { return NULL; + } if ((i & 0x1) == 0) { decoded_data[j] = ((unsigned char)c << 4); - } else { + } + else { decoded_data[j++] |= (unsigned char)c; } } diff --git a/test/mac_test.c b/test/mac_test.c index b44ddf70..ad455e74 100644 --- a/test/mac_test.c +++ b/test/mac_test.c @@ -120,13 +120,16 @@ int _ValidateMAC(const cn_cbor *pControl, if (COSE_Mac_validate(hMAC, hRecip, NULL)) { if (fAlgNoSupport) { fFail = true; - } else if ((pFail != NULL) && (pFail->type != CN_CBOR_TRUE)) { + } + else if ((pFail != NULL) && (pFail->type != CN_CBOR_TRUE)) { fFail = true; } - } else { + } + else { if (fAlgNoSupport) { returnCode = 0; - } else if ((pFail == NULL) || (pFail->type == CN_CBOR_FALSE)) { + } + else if ((pFail == NULL) || (pFail->type == CN_CBOR_FALSE)) { fFail = true; } } @@ -396,7 +399,6 @@ int BuildMacMessage(const cn_cbor *pControl) } #endif - COSE_Recipient_Free(hRecip); } @@ -430,8 +432,7 @@ int BuildMacMessage(const cn_cbor *pControl) goto returnError; } - if (!COSE_Mac_add_countersignature( - hMacObj, hCountersign, NULL)) { + if (!COSE_Mac_add_countersignature(hMacObj, hCountersign, NULL)) { goto returnError; } @@ -440,7 +441,6 @@ int BuildMacMessage(const cn_cbor *pControl) } #endif - if (!COSE_Mac_encrypt(hMacObj, NULL)) { goto returnError; @@ -477,9 +477,9 @@ int MacMessage() } if (!COSE_Mac_map_put_int(hEncObj, COSE_Header_Algorithm, - cn_cbor_int_create( - COSE_Algorithm_HMAC_256_256, CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) { + cn_cbor_int_create( + COSE_Algorithm_HMAC_256_256, CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { goto errorReturn; } if (!COSE_Mac_SetContent(hEncObj, (byte *)sz, strlen(sz), NULL)) { @@ -487,7 +487,7 @@ int MacMessage() } HCOSE_RECIPIENT hRecip = COSE_Recipient_from_shared_secret(rgbSecret, - sizeof(rgbSecret), rgbKid, cbKid, CBOR_CONTEXT_PARAM_COMMA NULL); + sizeof(rgbSecret), rgbKid, cbKid, CBOR_CONTEXT_PARAM_COMMA NULL); if (hRecip == NULL) { goto errorReturn; } @@ -642,10 +642,12 @@ int _ValidateMac0(const cn_cbor *pControl, if (fUnsuportedAlg) { fFail = true; fUnsuportedAlg = false; - } else if ((pFail != NULL) && (pFail->type != CN_CBOR_TRUE)) { + } + else if ((pFail != NULL) && (pFail->type != CN_CBOR_TRUE)) { fFail = true; } - } else { + } + else { if ((pFail == NULL) || (pFail->type == CN_CBOR_FALSE)) { fFail = true; } @@ -774,7 +776,8 @@ int BuildMac0Message(const cn_cbor *pControl) goto returnError; } - if (!SetSendingAttributes((HCOSE)hMacObj, pMac, Attributes_MAC0_protected)) { + if (!SetSendingAttributes( + (HCOSE)hMacObj, pMac, Attributes_MAC0_protected)) { goto returnError; } @@ -834,8 +837,7 @@ int BuildMac0Message(const cn_cbor *pControl) } #endif - - + size_t cb = COSE_Encode((HCOSE)hMacObj, NULL, 0, 0) + 1; byte *rgb = (byte *)malloc(cb); cb = COSE_Encode((HCOSE)hMacObj, rgb, 0, cb); @@ -887,7 +889,8 @@ void MAC_Corners() if (COSE_Mac_validate((HCOSE_MAC)hEncrypt, (HCOSE_RECIPIENT)hMAC, NULL)) { CFails++; } - if (COSE_Mac_AddRecipient((HCOSE_MAC)hEncrypt, (HCOSE_RECIPIENT)hMAC, NULL)) { + if (COSE_Mac_AddRecipient( + (HCOSE_MAC)hEncrypt, (HCOSE_RECIPIENT)hMAC, NULL)) { CFails++; } if (COSE_Mac_GetRecipient((HCOSE_MAC)hEncrypt, 0, NULL)) { @@ -922,7 +925,8 @@ void MAC_Corners() if (COSE_Mac_validate((HCOSE_MAC)hEncrypt, (HCOSE_RECIPIENT)hMAC, NULL)) { CFails++; } - if (COSE_Mac_AddRecipient((HCOSE_MAC)hEncrypt, (HCOSE_RECIPIENT)hMAC, NULL)) { + if (COSE_Mac_AddRecipient( + (HCOSE_MAC)hEncrypt, (HCOSE_RECIPIENT)hMAC, NULL)) { CFails++; } if (COSE_Mac_GetRecipient((HCOSE_MAC)hEncrypt, 0, NULL)) { @@ -946,8 +950,8 @@ void MAC_Corners() CFails++; } if (!COSE_Mac_map_put_int(hMAC, COSE_Header_Algorithm, - cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) { + cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; } hRecipient = COSE_Recipient_from_shared_secret( @@ -971,8 +975,8 @@ void MAC_Corners() CFails++; } if (!COSE_Mac_map_put_int(hMAC, COSE_Header_Algorithm, - cn_cbor_string_create("hmac", CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) { + cn_cbor_string_create("hmac", CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; } hRecipient = COSE_Recipient_from_shared_secret( @@ -1069,8 +1073,8 @@ void MAC0_Corners() CFails++; } if (!COSE_Mac0_map_put_int(hMAC, COSE_Header_Algorithm, - cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) { + cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; } CHECK_FAILURE(COSE_Mac0_encrypt(hMAC, rgb, sizeof(rgb), &cose_error), @@ -1085,8 +1089,8 @@ void MAC0_Corners() CFails++; } if (!COSE_Mac0_map_put_int(hMAC, COSE_Header_Algorithm, - cn_cbor_string_create("hmac", CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) { + cn_cbor_string_create("hmac", CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; } CHECK_FAILURE(COSE_Mac0_encrypt(hMAC, rgb, sizeof(rgb), &cose_error), diff --git a/test/sign.c b/test/sign.c index e5fd4a45..c342df2c 100644 --- a/test/sign.c +++ b/test/sign.c @@ -43,15 +43,18 @@ int _ValidateSigned(const cn_cbor *pControl, fFailBody = true; } - if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) + if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) { goto returnError; + } pSign = cn_cbor_mapget_string(pInput, "sign"); - if ((pSign == NULL) || (pSign->type != CN_CBOR_MAP)) + if ((pSign == NULL) || (pSign->type != CN_CBOR_MAP)) { goto returnError; + } pSigners = cn_cbor_mapget_string(pSign, "signers"); - if ((pSigners == NULL) || (pSigners->type != CN_CBOR_ARRAY)) + if ((pSigners == NULL) || (pSigners->type != CN_CBOR_ARRAY)) { goto returnError; + } iSigner = (int)pSigners->length - 1; pSigners = pSigners->first_child; @@ -59,14 +62,17 @@ int _ValidateSigned(const cn_cbor *pControl, hSig = (HCOSE_SIGN)COSE_Decode(pbEncoded, cbEncoded, &type, COSE_sign_object, CBOR_CONTEXT_PARAM_COMMA NULL); if (hSig == NULL) { - if (fFailBody) + if (fFailBody) { return 0; - else + } + else { goto returnError; + } } if (!SetReceivingAttributes( - (HCOSE)hSig, pSign, Attributes_Sign_protected)) + (HCOSE)hSig, pSign, Attributes_Sign_protected)) { goto returnError; + } cn_cbor *pkey = BuildKey(cn_cbor_mapget_string(pSigners, "key"), false); if (pkey == NULL) { @@ -80,8 +86,9 @@ int _ValidateSigned(const cn_cbor *pControl, continue; } if (!SetReceivingAttributes( - (HCOSE)hSigner, pSigners, Attributes_Signer_protected)) + (HCOSE)hSigner, pSigners, Attributes_Signer_protected)) { goto returnError; + } if (!COSE_Signer_SetKey(hSigner, pkey, NULL)) { fFail = true; @@ -90,28 +97,36 @@ int _ValidateSigned(const cn_cbor *pControl, cn_cbor *alg = COSE_Signer_map_get_int( hSigner, COSE_Header_Algorithm, COSE_BOTH, 0); - if (!IsAlgorithmSupported(alg)) + if (!IsAlgorithmSupported(alg)) { fNoSupportAlg = true; + } pFail = cn_cbor_mapget_string(pSigners, "fail"); if (COSE_Sign_validate(hSig, hSigner, NULL)) { if (fNoSupportAlg) { fFail = true; - } else if ((pFail != NULL) && (pFail->type != CN_CBOR_TRUE)) + } + else if ((pFail != NULL) && (pFail->type != CN_CBOR_TRUE)) { fFail = true; - } else { + } + } + else { if (fNoSupportAlg) { fFailBody = false; fFail = false; - } else if ((pFail == NULL) || (pFail->type == CN_CBOR_FALSE)) + } + else if ((pFail == NULL) || (pFail->type == CN_CBOR_FALSE)) { fFail = true; + } } #if INCLUDE_COUNTERSIGNATURE // Validate counter signatures on signers - cn_cbor* countersignList = cn_cbor_mapget_string(pSigners, "countersign"); + cn_cbor *countersignList = + cn_cbor_mapget_string(pSigners, "countersign"); if (countersignList != NULL) { - cn_cbor * countersigners = cn_cbor_mapget_string(countersignList, "signers"); + cn_cbor *countersigners = + cn_cbor_mapget_string(countersignList, "signers"); if (countersigners == NULL) { fFail = true; continue; @@ -119,13 +134,14 @@ int _ValidateSigned(const cn_cbor *pControl, int count = countersigners->length; bool forward = true; - if (COSE_Signer_map_get_int(hSigner, COSE_Header_CounterSign, COSE_UNPROTECT_ONLY, 0) == NULL) { + if (COSE_Signer_map_get_int(hSigner, COSE_Header_CounterSign, + COSE_UNPROTECT_ONLY, 0) == NULL) { goto returnError; } - for (int counterNo = 0; counterNo < count ; - counterNo++) { - HCOSE_COUNTERSIGN h = COSE_Signer_get_countersignature(hSigner, counterNo, 0); + for (int counterNo = 0; counterNo < count; counterNo++) { + HCOSE_COUNTERSIGN h = + COSE_Signer_get_countersignature(hSigner, counterNo, 0); if (h == NULL) { fFail = true; continue; @@ -133,7 +149,7 @@ int _ValidateSigned(const cn_cbor *pControl, cn_cbor *counterSigner = cn_cbor_index(countersigners, forward ? counterNo : count - counterNo - 1); - + cn_cbor *pkeyCountersign = BuildKey( cn_cbor_mapget_string(counterSigner, "key"), false); if (pkeyCountersign == NULL) { @@ -147,7 +163,7 @@ int _ValidateSigned(const cn_cbor *pControl, } if (COSE_Signer_CounterSign_validate(hSigner, h, 0)) { - // I don't think we have any forced errors yet. + // I don't think we have any forced errors yet. } else { if (forward && counterNo == 0 && count > 1) { @@ -165,14 +181,12 @@ int _ValidateSigned(const cn_cbor *pControl, } #endif - #if INCLUDE_COUNTERSIGNATURE // Countersign on Signed Body - + if (iSigner == 0) { // Validate counter signatures on signers - countersignList = - cn_cbor_mapget_string(pSign, "countersign"); + countersignList = cn_cbor_mapget_string(pSign, "countersign"); if (countersignList != NULL) { cn_cbor *countersigners = cn_cbor_mapget_string(countersignList, "signers"); @@ -230,20 +244,23 @@ int _ValidateSigned(const cn_cbor *pControl, } } #endif - + COSE_Sign_Free(hSig); COSE_Signer_Free(hSigner); } if (fFailBody) { - if (!fFail) + if (!fFail) { fFail = true; - else + } + else { fFail = false; + } } - if (fFail) + if (fFail) { CFails += 1; + } return fNoSupportAlg ? 0 : 1; returnError: @@ -268,50 +285,61 @@ int BuildSignedMessage(const cn_cbor *pControl) // const cn_cbor *pFail = cn_cbor_mapget_string(pControl, "fail"); - if ((pFail != NULL) && (pFail->type == CN_CBOR_TRUE)) + if ((pFail != NULL) && (pFail->type == CN_CBOR_TRUE)) { return 0; + } HCOSE_SIGN hSignObj = COSE_Sign_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); const cn_cbor *pInputs = cn_cbor_mapget_string(pControl, "input"); - if (pInputs == NULL) + if (pInputs == NULL) { goto returnError; + } const cn_cbor *pSign = cn_cbor_mapget_string(pInputs, "sign"); - if (pSign == NULL) + if (pSign == NULL) { goto returnError; + } const cn_cbor *pContent = cn_cbor_mapget_string(pInputs, "plaintext"); if (!COSE_Sign_SetContent( - hSignObj, pContent->v.bytes, pContent->length, NULL)) + hSignObj, pContent->v.bytes, pContent->length, NULL)) { goto returnError; + } if (!SetSendingAttributes( - (HCOSE)hSignObj, pSign, Attributes_Sign_protected)) + (HCOSE)hSignObj, pSign, Attributes_Sign_protected)) { goto returnError; + } const cn_cbor *pSigners = cn_cbor_mapget_string(pSign, "signers"); - if ((pSigners == NULL) || (pSigners->type != CN_CBOR_ARRAY)) + if ((pSigners == NULL) || (pSigners->type != CN_CBOR_ARRAY)) { goto returnError; + } pSigners = pSigners->first_child; for (iSigner = 0; pSigners != NULL; iSigner++, pSigners = pSigners->next) { cn_cbor *pkey = BuildKey(cn_cbor_mapget_string(pSigners, "key"), false); - if (pkey == NULL) + if (pkey == NULL) { goto returnError; + } HCOSE_SIGNER hSigner = COSE_Signer_Init(CBOR_CONTEXT_PARAM_COMMA NULL); - if (hSigner == NULL) + if (hSigner == NULL) { goto returnError; + } if (!SetSendingAttributes( - (HCOSE)hSigner, pSigners, Attributes_Signer_protected)) + (HCOSE)hSigner, pSigners, Attributes_Signer_protected)) { goto returnError; + } - if (!COSE_Signer_SetKey(hSigner, pkey, NULL)) + if (!COSE_Signer_SetKey(hSigner, pkey, NULL)) { goto returnError; + } - if (!COSE_Sign_AddSigner(hSignObj, hSigner, NULL)) + if (!COSE_Sign_AddSigner(hSignObj, hSigner, NULL)) { goto returnError; + } #if INCLUDE_COUNTERSIGNATURE // On the signer object @@ -356,7 +384,7 @@ int BuildSignedMessage(const cn_cbor *pControl) } #if INCLUDE_COUNTERSIGNATURE // On the sign body - cn_cbor * countersigns1 = cn_cbor_mapget_string(pSign, "countersign"); + cn_cbor *countersigns1 = cn_cbor_mapget_string(pSign, "countersign"); if (countersigns1 != NULL) { countersigns1 = cn_cbor_mapget_string(countersigns1, "signers"); cn_cbor *countersign = countersigns1->first_child; @@ -529,23 +557,28 @@ int _ValidateSign1(const cn_cbor *pControl, fFailBody = true; } - if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) + if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) { goto returnError; + } pSign = cn_cbor_mapget_string(pInput, "sign0"); - if ((pSign == NULL) || (pSign->type != CN_CBOR_MAP)) + if ((pSign == NULL) || (pSign->type != CN_CBOR_MAP)) { goto returnError; + } hSig = (HCOSE_SIGN1)COSE_Decode(pbEncoded, cbEncoded, &type, - COSE_sign1_object, CBOR_CONTEXT_PARAM_COMMA NULL); + COSE_sign1_object, CBOR_CONTEXT_PARAM_COMMA NULL); if (hSig == NULL) { - if (fFailBody) + if (fFailBody) { return 0; - else + } + else { goto returnError; + } } - if (!SetReceivingAttributes((HCOSE)hSig, pSign, Attributes_Sign1_protected)) + if (!SetReceivingAttributes((HCOSE)hSig, pSign, Attributes_Sign1_protected)) { goto returnError; + } cn_cbor *pkey = BuildKey(cn_cbor_mapget_string(pSign, "key"), false); if (pkey == NULL) { @@ -555,28 +588,34 @@ int _ValidateSign1(const cn_cbor *pControl, cn_cbor *alg = COSE_Sign1_map_get_int(hSig, COSE_Header_Algorithm, COSE_BOTH, NULL); - if (!IsAlgorithmSupported(alg)) + if (!IsAlgorithmSupported(alg)) { fNoAlgSupport = true; + } pFail = cn_cbor_mapget_string(pInput, "fail"); if (COSE_Sign1_validate(hSig, pkey, NULL)) { if (fNoAlgSupport) { fFail = true; - } else if ((pFail != NULL) && (pFail->type != CN_CBOR_TRUE)) + } + else if ((pFail != NULL) && (pFail->type != CN_CBOR_TRUE)) { fFail = true; - } else { + } + } + else { if (fNoAlgSupport) { fFailBody = false; fFail = false; - } else if ((pFail == NULL) || (pFail->type == CN_CBOR_FALSE)) + } + else if ((pFail == NULL) || (pFail->type == CN_CBOR_FALSE)) { fFail = true; + } } #if INCLUDE_COUNTERSIGNATURE // Countersign on Signed Body // Validate counter signatures on signers - cn_cbor * countersignList = cn_cbor_mapget_string(pSign, "countersign"); + cn_cbor *countersignList = cn_cbor_mapget_string(pSign, "countersign"); if (countersignList != NULL) { cn_cbor *countersigners = cn_cbor_mapget_string(countersignList, "signers"); @@ -633,21 +672,23 @@ int _ValidateSign1(const cn_cbor *pControl, } } #endif - COSE_Sign1_Free(hSig); if (fFailBody) { - if (!fFail) + if (!fFail) { fFail = true; - else + } + else { fFail = false; + } } exitHere: - if (fFail) + if (fFail) { CFails += 1; + } return fNoAlgSupport ? 0 : 1; returnError: @@ -700,10 +741,10 @@ int BuildSign1Message(const cn_cbor *pControl) if (pkey == NULL) { goto returnError; } - + #if INCLUDE_COUNTERSIGNATURE // On the sign body - cn_cbor * countersigns = cn_cbor_mapget_string(pSign, "countersign"); + cn_cbor *countersigns = cn_cbor_mapget_string(pSign, "countersign"); if (countersigns != NULL) { countersigns = cn_cbor_mapget_string(countersigns, "signers"); cn_cbor *countersign = countersigns->first_child; @@ -730,7 +771,8 @@ int BuildSign1Message(const cn_cbor *pControl) goto returnError; } - if (!COSE_Sign1_add_countersignature(hSignObj, hCountersign, NULL)) { + if (!COSE_Sign1_add_countersignature( + hSignObj, hCountersign, NULL)) { goto returnError; } @@ -740,9 +782,10 @@ int BuildSign1Message(const cn_cbor *pControl) #endif - if (!COSE_Sign1_Sign(hSignObj, pkey, NULL)) + if (!COSE_Sign1_Sign(hSignObj, pkey, NULL)) { goto returnError; - + } + size_t cb = COSE_Encode((HCOSE)hSignObj, NULL, 0, 0) + 1; byte *rgb = (byte *)malloc(cb); cb = COSE_Encode((HCOSE)hSignObj, rgb, 0, cb); @@ -905,54 +948,69 @@ void Sign_Corners() // Unsupported algorithm hSign = COSE_Sign_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); - if (hSign == NULL) + if (hSign == NULL) { CFails++; + } hSigner = COSE_Signer_Init(CBOR_CONTEXT_PARAM_COMMA NULL); - if (hSigner == NULL) + if (hSigner == NULL) { CFails++; + } - if (!COSE_Sign_SetContent(hSign, (byte *)"Message", 7, NULL)) + if (!COSE_Sign_SetContent(hSign, (byte *)"Message", 7, NULL)) { CFails++; + } if (!COSE_Signer_map_put_int(hSigner, COSE_Header_Algorithm, - cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) + cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; - if (!COSE_Sign_AddSigner(hSign, hSigner, NULL)) + } + if (!COSE_Sign_AddSigner(hSign, hSigner, NULL)) { CFails++; + } CHECK_FAILURE(COSE_Sign_Sign(hSign, &cose_error), COSE_ERR_UNKNOWN_ALGORITHM, CFails++); - if (COSE_Sign_GetSigner(hSign, 9, NULL)) + if (COSE_Sign_GetSigner(hSign, 9, NULL)) { CFails++; + } COSE_Sign_Free(hSign); COSE_Signer_Free(hSigner); hSign = COSE_Sign_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); - if (hSign == NULL) + if (hSign == NULL) { CFails++; + } hSigner = COSE_Signer_Init(CBOR_CONTEXT_PARAM_COMMA NULL); - if (hSigner == NULL) + if (hSigner == NULL) { CFails++; + } - if (!COSE_Sign_SetContent(hSign, (byte *)"Message", 7, NULL)) + if (!COSE_Sign_SetContent(hSign, (byte *)"Message", 7, NULL)) { CFails++; + } if (!COSE_Signer_map_put_int(hSigner, COSE_Header_Algorithm, - cn_cbor_string_create("hmac", CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) + cn_cbor_string_create("hmac", CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; - if (!COSE_Sign_AddSigner(hSign, hSigner, NULL)) + } + if (!COSE_Sign_AddSigner(hSign, hSigner, NULL)) { CFails++; + } CHECK_FAILURE(COSE_Sign_Sign(hSign, &cose_error), COSE_ERR_UNKNOWN_ALGORITHM, CFails++); - if (COSE_Sign_GetSigner(hSign, 9, NULL)) + if (COSE_Sign_GetSigner(hSign, 9, NULL)) { CFails++; + } cn = COSE_Signer_map_get_int( hSigner, COSE_Header_Algorithm, COSE_BOTH, &cose_error); if (cn != NULL) { - if (cn->type != CN_CBOR_TEXT) + if (cn->type != CN_CBOR_TEXT) { CFails++; - } else + } + } + else { CFails++; + } return; } @@ -1035,31 +1093,37 @@ void Sign1_Corners() // Unsupported algorithm hSign = COSE_Sign1_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); - if (hSign == NULL) + if (hSign == NULL) { CFails++; + } cn = cn_cbor_int_create(15, CBOR_CONTEXT_PARAM_COMMA NULL); - if (!COSE_Sign1_SetContent(hSign, (byte *)"Message", 7, NULL)) + if (!COSE_Sign1_SetContent(hSign, (byte *)"Message", 7, NULL)) { CFails++; + } if (!COSE_Sign1_map_put_int(hSign, COSE_Header_Algorithm, - cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) + cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; + } CHECK_FAILURE(COSE_Sign1_Sign(hSign, cn, &cose_error), COSE_ERR_UNKNOWN_ALGORITHM, CFails++); COSE_Sign1_Free(hSign); hSign = COSE_Sign1_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL); - if (hSign == NULL) + if (hSign == NULL) { CFails++; + } - if (!COSE_Sign1_SetContent(hSign, (byte *)"Message", 7, NULL)) + if (!COSE_Sign1_SetContent(hSign, (byte *)"Message", 7, NULL)) { CFails++; + } if (!COSE_Sign1_map_put_int(hSign, COSE_Header_Algorithm, - cn_cbor_string_create("hmac", CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) + cn_cbor_string_create("hmac", CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; + } CHECK_FAILURE(COSE_Sign1_Sign(hSign, cn, &cose_error), COSE_ERR_UNKNOWN_ALGORITHM, CFails++); diff --git a/test/test.c b/test/test.c index 4153a45a..bdb407cd 100644 --- a/test/test.c +++ b/test/test.c @@ -91,8 +91,9 @@ int MapName(const cn_cbor* p, NameMap* rgMap, unsigned int cMap) unsigned int i; for (i = 0; i < cMap; i++) { - if (strcmp(rgMap[i].sz, p->v.str) == 0) + if (strcmp(rgMap[i].sz, p->v.str) == 0) { return rgMap[i].i; + } } assert(false); @@ -107,12 +108,15 @@ int MapAlgorithmName(const cn_cbor* p) byte fromHex(char c) { - if (('0' <= c) && (c <= '9')) + if (('0' <= c) && (c <= '9')) { return c - '0'; - if (('A' <= c) && (c <= 'F')) + } + if (('A' <= c) && (c <= 'F')) { return c - 'A' + 10; - if (('a' <= c) && (c <= 'f')) + } + if (('a' <= c) && (c <= 'f')) { return c - 'a' + 10; + } fprintf(stderr, "Invalid hex"); exit(1); } @@ -135,8 +139,9 @@ int IsAlgorithmSupported(const cn_cbor* alg) // Pretend we support any algorithm which is not an integer - this is a // fail test case - if ((alg->type != CN_CBOR_INT) && (alg->type != CN_CBOR_UINT)) + if ((alg->type != CN_CBOR_INT) && (alg->type != CN_CBOR_UINT)) { return true; + } switch (alg->v.sint) { default: return false; @@ -348,48 +353,60 @@ bool SetAttributes(HCOSE hHandle, keyNew = COSE_Header_Algorithm; pValueNew = cn_cbor_int_create( MapAlgorithmName(pValue), CBOR_CONTEXT_PARAM_COMMA NULL); - } else if (strcmp(pKey->v.str, "ctyp") == 0) { + } + else if (strcmp(pKey->v.str, "ctyp") == 0) { keyNew = COSE_Header_Content_Type; pValueNew = cn_cbor_clone(pValue, CBOR_CONTEXT_PARAM_COMMA NULL); - if (pValueNew == NULL) + if (pValueNew == NULL) { return false; - } else if (strcmp(pKey->v.str, "IV_hex") == 0) { + } + } + else if (strcmp(pKey->v.str, "IV_hex") == 0) { keyNew = COSE_Header_IV; pValueNew = cn_cbor_data_create(FromHex(pValue->v.str, (int)pValue->length), (int)pValue->length / 2, CBOR_CONTEXT_PARAM_COMMA NULL); - } else if (strcmp(pKey->v.str, "apu_id") == 0) { + } + else if (strcmp(pKey->v.str, "apu_id") == 0) { keyNew = COSE_Header_KDF_U_name; pValueNew = cn_cbor_data_create(pValue->v.bytes, (int)pValue->length, CBOR_CONTEXT_PARAM_COMMA NULL); - if (pValueNew == NULL) + if (pValueNew == NULL) { return false; - - } else if (strcmp(pKey->v.str, "apv_id") == 0) { + } + } + else if (strcmp(pKey->v.str, "apv_id") == 0) { keyNew = COSE_Header_KDF_V_name; pValueNew = cn_cbor_data_create(pValue->v.bytes, (int)pValue->length, CBOR_CONTEXT_PARAM_COMMA NULL); - if (pValueNew == NULL) + if (pValueNew == NULL) { return false; - - } else if (strcmp(pKey->v.str, "pub_other") == 0) { + } + } + else if (strcmp(pKey->v.str, "pub_other") == 0) { keyNew = COSE_Header_KDF_PUB_other; pValueNew = cn_cbor_data_create(pValue->v.bytes, (int)pValue->length, CBOR_CONTEXT_PARAM_COMMA NULL); - if (pValueNew == NULL) + if (pValueNew == NULL) { return false; - } else if (strcmp(pKey->v.str, "priv_other") == 0) { + } + } + else if (strcmp(pKey->v.str, "priv_other") == 0) { keyNew = COSE_Header_KDF_PRIV; pValueNew = cn_cbor_data_create(pValue->v.bytes, (int)pValue->length, CBOR_CONTEXT_PARAM_COMMA NULL); - if (pValueNew == NULL) + if (pValueNew == NULL) { return false; - } else if (strcmp(pKey->v.str, "spk") == 0) { + } + } + else if (strcmp(pKey->v.str, "spk") == 0) { keyNew = COSE_Header_ECDH_STATIC; pValueNew = BuildKey(pValue, fPublicKey); - if (pValueNew == NULL) + if (pValueNew == NULL) { return false; - } else { + } + } + else { continue; } @@ -472,27 +489,32 @@ bool SetSendingAttributes(HCOSE hMsg, const cn_cbor* pIn, int base) bool f = false; if (!SetAttributes(hMsg, cn_cbor_mapget_string(pIn, "protected"), - COSE_PROTECT_ONLY, base, true)) + COSE_PROTECT_ONLY, base, true)) { goto returnError; + } if (!SetAttributes(hMsg, cn_cbor_mapget_string(pIn, "unprotected"), - COSE_UNPROTECT_ONLY, base, true)) + COSE_UNPROTECT_ONLY, base, true)) { goto returnError; + } if (!SetAttributes(hMsg, cn_cbor_mapget_string(pIn, "unsent"), - COSE_DONT_SEND, base, false)) + COSE_DONT_SEND, base, false)) { goto returnError; + } cn_cbor* pExternal = cn_cbor_mapget_string(pIn, "external"); if (pExternal != NULL) { cn_cbor* pcn = cn_cbor_clone(pExternal, CBOR_CONTEXT_PARAM_COMMA NULL); - if (pcn == NULL) + if (pcn == NULL) { goto returnError; + } switch (base) { #if INCLUDE_ENCRYPT0 case Attributes_Encrypt_protected: if (!COSE_Encrypt_SetExternal((HCOSE_ENCRYPT)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, - NULL)) + NULL)) { goto returnError; + } break; #endif @@ -500,8 +522,9 @@ bool SetSendingAttributes(HCOSE hMsg, const cn_cbor* pIn, int base) case Attributes_Enveloped_protected: if (!COSE_Enveloped_SetExternal((HCOSE_ENVELOPED)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, - NULL)) + NULL)) { goto returnError; + } break; #endif @@ -509,8 +532,9 @@ bool SetSendingAttributes(HCOSE hMsg, const cn_cbor* pIn, int base) case Attributes_MAC_protected: if (!COSE_Mac_SetExternal((HCOSE_MAC)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, - NULL)) + NULL)) { goto returnError; + } break; #endif @@ -518,8 +542,9 @@ bool SetSendingAttributes(HCOSE hMsg, const cn_cbor* pIn, int base) case Attributes_MAC0_protected: if (!COSE_Mac0_SetExternal((HCOSE_MAC0)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, - NULL)) + NULL)) { goto returnError; + } break; #endif @@ -527,8 +552,9 @@ bool SetSendingAttributes(HCOSE hMsg, const cn_cbor* pIn, int base) case Attributes_Signer_protected: if (!COSE_Signer_SetExternal((HCOSE_SIGNER)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, - NULL)) + NULL)) { goto returnError; + } break; #endif @@ -536,8 +562,9 @@ bool SetSendingAttributes(HCOSE hMsg, const cn_cbor* pIn, int base) case Attributes_Sign1_protected: if (!COSE_Sign1_SetExternal((HCOSE_SIGN1)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, - NULL)) + NULL)) { goto returnError; + } break; #endif #if INCLUDE_COUNTERSIGNATURE @@ -565,21 +592,24 @@ bool SetReceivingAttributes(HCOSE hMsg, const cn_cbor* pIn, int base) bool f = false; if (!SetAttributes(hMsg, cn_cbor_mapget_string(pIn, "unsent"), - COSE_DONT_SEND, base, true)) + COSE_DONT_SEND, base, true)) { goto returnError; + } cn_cbor* pExternal = cn_cbor_mapget_string(pIn, "external"); if (pExternal != NULL) { cn_cbor* pcn = cn_cbor_clone(pExternal, CBOR_CONTEXT_PARAM_COMMA NULL); - if (pcn == NULL) + if (pcn == NULL) { goto returnError; + } switch (base) { #if INCLUDE_ENCRYPT0 case Attributes_Encrypt_protected: if (!COSE_Encrypt_SetExternal((HCOSE_ENCRYPT)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, - NULL)) + NULL)) { goto returnError; + } break; #endif @@ -587,8 +617,9 @@ bool SetReceivingAttributes(HCOSE hMsg, const cn_cbor* pIn, int base) case Attributes_Enveloped_protected: if (!COSE_Enveloped_SetExternal((HCOSE_ENVELOPED)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, - NULL)) + NULL)) { goto returnError; + } break; #endif @@ -596,8 +627,9 @@ bool SetReceivingAttributes(HCOSE hMsg, const cn_cbor* pIn, int base) case Attributes_MAC_protected: if (!COSE_Mac_SetExternal((HCOSE_MAC)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, - NULL)) + NULL)) { goto returnError; + } break; #endif @@ -605,8 +637,9 @@ bool SetReceivingAttributes(HCOSE hMsg, const cn_cbor* pIn, int base) case Attributes_MAC0_protected: if (!COSE_Mac0_SetExternal((HCOSE_MAC0)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, - NULL)) + NULL)) { goto returnError; + } break; #endif @@ -614,8 +647,9 @@ bool SetReceivingAttributes(HCOSE hMsg, const cn_cbor* pIn, int base) case Attributes_Signer_protected: if (!COSE_Signer_SetExternal((HCOSE_SIGNER)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, - NULL)) + NULL)) { goto returnError; + } break; #endif @@ -623,8 +657,9 @@ bool SetReceivingAttributes(HCOSE hMsg, const cn_cbor* pIn, int base) case Attributes_Sign1_protected: if (!COSE_Sign1_SetExternal((HCOSE_SIGN1)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, - NULL)) + NULL)) { goto returnError; + } break; #endif #if INCLUDE_COUNTERSIGNATURE @@ -656,31 +691,43 @@ cn_cbor* BuildKey(const cn_cbor* pKeyIn, bool fPublicKey) unsigned char* pb = NULL; size_t cb; - if (pKeyOut == NULL) + if (pKeyOut == NULL) { return NULL; + } - if ((pKty == NULL) || (pKty->type != CN_CBOR_TEXT)) + if ((pKty == NULL) || (pKty->type != CN_CBOR_TEXT)) { return NULL; + } if (pKty->length == 2) { - if (strncmp(pKty->v.str, "EC", 2) == 0) + if (strncmp(pKty->v.str, "EC", 2) == 0) { kty = 2; - else + } + else { return NULL; - } else if (pKty->length == 3) { - if (strncmp(pKty->v.str, "oct", 3) == 0) + } + } + else if (pKty->length == 3) { + if (strncmp(pKty->v.str, "oct", 3) == 0) { kty = 4; - else if (strncmp(pKty->v.str, "OKP", 3) == 0) + } + else if (strncmp(pKty->v.str, "OKP", 3) == 0) { kty = COSE_Key_Type_OKP; - else + } + else { return NULL; - } else + } + } + else { return NULL; + } p = cn_cbor_int_create(kty, CBOR_CONTEXT_PARAM_COMMA NULL); - if (p == NULL) + if (p == NULL) { return NULL; - if (!cn_cbor_mapput_int(pKeyOut, 1, p, CBOR_CONTEXT_PARAM_COMMA NULL)) + } + if (!cn_cbor_mapput_int(pKeyOut, 1, p, CBOR_CONTEXT_PARAM_COMMA NULL)) { return NULL; + } for (pKey = pKeyIn->first_child; pKey != NULL; pKey = pKey->next->next) { pValue = pKey->next; @@ -697,55 +744,65 @@ cn_cbor* BuildKey(const cn_cbor* pKeyIn, bool fPublicKey) case OPERATION_NONE: p = cn_cbor_clone( pValue, CBOR_CONTEXT_PARAM_COMMA NULL); - if (p == NULL) + if (p == NULL) { return NULL; + } if (!cn_cbor_mapput_int(pKeyOut, - RgStringKeys[i].keyNew, p, - CBOR_CONTEXT_PARAM_COMMA NULL)) + RgStringKeys[i].keyNew, p, + CBOR_CONTEXT_PARAM_COMMA NULL)) { return NULL; + } break; case OPERATION_BASE64: - if ((strcmp(pKey->v.str, "d") == 0) && fPublicKey) + if ((strcmp(pKey->v.str, "d") == 0) && fPublicKey) { continue; + } pb = base64_decode( pValue->v.str, pValue->length, &cb); p = cn_cbor_data_create( pb, (int)cb, CBOR_CONTEXT_PARAM_COMMA NULL); - if (p == NULL) + if (p == NULL) { return NULL; + } if (!cn_cbor_mapput_int(pKeyOut, - RgStringKeys[i].keyNew, p, - CBOR_CONTEXT_PARAM_COMMA NULL)) + RgStringKeys[i].keyNew, p, + CBOR_CONTEXT_PARAM_COMMA NULL)) { return NULL; + } break; case OPERATION_STRING: p = cn_cbor_int_create(MapName(pValue, RgCurveNames, _countof(RgCurveNames)), CBOR_CONTEXT_PARAM_COMMA NULL); - if (p == NULL) + if (p == NULL) { return NULL; + } if (!cn_cbor_mapput_int(pKeyOut, - RgStringKeys[i].keyNew, p, - CBOR_CONTEXT_PARAM_COMMA NULL)) + RgStringKeys[i].keyNew, p, + CBOR_CONTEXT_PARAM_COMMA NULL)) { return NULL; + } break; case OPERATION_HEX: if ((strcmp(pKey->v.str, "d_hex") == 0) && - fPublicKey) + fPublicKey) { continue; + } pb = hex_decode(pValue->v.str, pValue->length, &cb); p = cn_cbor_data_create( pb, (int)cb, CBOR_CONTEXT_PARAM_COMMA NULL); - if (p == NULL) + if (p == NULL) { return NULL; + } if (!cn_cbor_mapput_int(pKeyOut, - RgStringKeys[i].keyNew, p, - CBOR_CONTEXT_PARAM_COMMA NULL)) + RgStringKeys[i].keyNew, p, + CBOR_CONTEXT_PARAM_COMMA NULL)) { return NULL; + } break; } i = 99; @@ -839,17 +896,17 @@ void RunMemoryTest(const char* szFileName) bool fValidateDone = false; bool fBuildDone = false; - - - for (iFail = 0; (!fValidateDone || !fBuildDone) && (iFail < 100000); iFail++) { + for (iFail = 0; (!fValidateDone || !fBuildDone) && (iFail < 100000); + iFail++) { if (cn_cbor_mapget_string(pInput, "mac") != NULL) { #if INCLUDE_MAC if (!fValidateDone) { context = CreateContext(iFail); CFails = 0; ValidateMAC(pControl); - if (CFails == 0) + if (CFails == 0) { fValidateDone = true; + } if (IsContextEmpty(context) != 0) { CFails += 1; } @@ -860,8 +917,9 @@ void RunMemoryTest(const char* szFileName) context = CreateContext(iFail); CFails = 0; BuildMacMessage(pControl); - if (CFails == 0) + if (CFails == 0) { fBuildDone = true; + } if (IsContextEmpty(context) != 0) { CFails += 1; } @@ -871,14 +929,16 @@ void RunMemoryTest(const char* szFileName) fValidateDone = true; fBuildDone = true; #endif - } else if (cn_cbor_mapget_string(pInput, "mac0") != NULL) { + } + else if (cn_cbor_mapget_string(pInput, "mac0") != NULL) { #if INCLUDE_MAC0 if (!fValidateDone) { context = CreateContext(iFail); CFails = 0; ValidateMac0(pControl); - if (CFails == 0) + if (CFails == 0) { fValidateDone = true; + } if (IsContextEmpty(context) != 0) { CFails += 1; } @@ -889,8 +949,9 @@ void RunMemoryTest(const char* szFileName) context = CreateContext(iFail); CFails = 0; BuildMac0Message(pControl); - if (CFails == 0) + if (CFails == 0) { fBuildDone = true; + } if (IsContextEmpty(context) != 0) { CFails += 1; } @@ -900,14 +961,16 @@ void RunMemoryTest(const char* szFileName) fValidateDone = true; fBuildDone = true; #endif - } else if (cn_cbor_mapget_string(pInput, "encrypted") != NULL) { + } + else if (cn_cbor_mapget_string(pInput, "encrypted") != NULL) { #if INCLUDE_ENCRYPT0 if (!fValidateDone) { context = CreateContext(iFail); CFails = 0; ValidateEncrypt(pControl); - if (CFails == 0) + if (CFails == 0) { fValidateDone = true; + } if (IsContextEmpty(context) != 0) { CFails += 1; } @@ -918,8 +981,9 @@ void RunMemoryTest(const char* szFileName) context = CreateContext(iFail); CFails = 0; BuildEncryptMessage(pControl); - if (CFails == 0) + if (CFails == 0) { fBuildDone = true; + } if (IsContextEmpty(context) != 0) { CFails += 1; } @@ -929,14 +993,16 @@ void RunMemoryTest(const char* szFileName) fValidateDone = true; fBuildDone = true; #endif - } else if (cn_cbor_mapget_string(pInput, "enveloped") != NULL) { + } + else if (cn_cbor_mapget_string(pInput, "enveloped") != NULL) { #if INCLUDE_ENCRYPT if (!fValidateDone) { context = CreateContext(iFail); CFails = 0; ValidateEnveloped(pControl); - if (CFails == 0) + if (CFails == 0) { fValidateDone = true; + } if (IsContextEmpty(context) != 0) { CFails += 1; } @@ -947,8 +1013,9 @@ void RunMemoryTest(const char* szFileName) context = CreateContext(iFail); CFails = 0; BuildEnvelopedMessage(pControl); - if (CFails == 0) + if (CFails == 0) { fBuildDone = true; + } if (IsContextEmpty(context) != 0) { CFails += 1; } @@ -958,14 +1025,16 @@ void RunMemoryTest(const char* szFileName) fValidateDone = true; fBuildDone = true; #endif - } else if (cn_cbor_mapget_string(pInput, "sign") != NULL) { + } + else if (cn_cbor_mapget_string(pInput, "sign") != NULL) { #if INCLUDE_SIGN if (!fValidateDone) { context = CreateContext(iFail); CFails = 0; ValidateSigned(pControl); - if (CFails == 0) + if (CFails == 0) { fValidateDone = true; + } if (IsContextEmpty(context) != 0) { CFails += 1; } @@ -976,8 +1045,9 @@ void RunMemoryTest(const char* szFileName) context = CreateContext(iFail); CFails = 0; BuildSignedMessage(pControl); - if (CFails == 0) + if (CFails == 0) { fBuildDone = true; + } if (IsContextEmpty(context) != 0) { CFails += 1; } @@ -987,14 +1057,16 @@ void RunMemoryTest(const char* szFileName) fValidateDone = true; fBuildDone = true; #endif - } else if (cn_cbor_mapget_string(pInput, "sign0") != NULL) { + } + else if (cn_cbor_mapget_string(pInput, "sign0") != NULL) { #if INCLUDE_SIGN1 if (!fValidateDone) { context = CreateContext(iFail); CFails = 0; ValidateSign1(pControl); - if (CFails == 0) + if (CFails == 0) { fValidateDone = true; + } if (IsContextEmpty(context) != 0) { CFails += 1; } @@ -1005,8 +1077,9 @@ void RunMemoryTest(const char* szFileName) context = CreateContext(iFail); CFails = 0; BuildSign1Message(pControl); - if (CFails == 0) + if (CFails == 0) { fBuildDone = true; + } if (IsContextEmpty(context) != 0) { CFails += 1; } @@ -1027,7 +1100,9 @@ void RunMemoryTest(const char* szFileName) typedef int (*ValidatePtr)(const cn_cbor* pControl); -bool ProcessFile(const cn_cbor* pControl, ValidatePtr validateFunction, ValidatePtr buildFunction) +bool ProcessFile(const cn_cbor* pControl, + ValidatePtr validateFunction, + ValidatePtr buildFunction) { #ifdef USE_CBOR_CONTEXT context = CreateContext(-1); @@ -1098,23 +1173,28 @@ void RunFileTest(const char* szFileName) #if INCLUDE_MAC ProcessFile(pControl, ValidateMAC, BuildMacMessage); #endif - } else if (cn_cbor_mapget_string(pInput, "mac0") != NULL) { + } + else if (cn_cbor_mapget_string(pInput, "mac0") != NULL) { #if INCLUDE_MAC0 ProcessFile(pControl, ValidateMac0, BuildMac0Message); #endif - } else if (cn_cbor_mapget_string(pInput, "enveloped") != NULL) { + } + else if (cn_cbor_mapget_string(pInput, "enveloped") != NULL) { #if INCLUDE_ENCRYPT ProcessFile(pControl, ValidateEnveloped, BuildEnvelopedMessage); #endif - } else if (cn_cbor_mapget_string(pInput, "sign") != NULL) { + } + else if (cn_cbor_mapget_string(pInput, "sign") != NULL) { #if INCLUDE_SIGN ProcessFile(pControl, ValidateSigned, BuildSignedMessage); #endif - } else if (cn_cbor_mapget_string(pInput, "sign0") != NULL) { + } + else if (cn_cbor_mapget_string(pInput, "sign0") != NULL) { #if INCLUDE_SIGN1 ProcessFile(pControl, ValidateSign1, BuildSign1Message); #endif - } else if (cn_cbor_mapget_string(pInput, "encrypted") != NULL) { + } + else if (cn_cbor_mapget_string(pInput, "encrypted") != NULL) { #if INCLUDE_ENCRYPT0 ProcessFile(pControl, ValidateEncrypt, BuildEncryptMessage); #endif @@ -1226,12 +1306,15 @@ int main(int argc, char** argv) if (argv[i][0] == '-') { if (strcmp(argv[i], "--dir") == 0) { fDir = true; - } else if (strcmp(argv[i], "--corners") == 0) { + } + else if (strcmp(argv[i], "--corners") == 0) { fCorners = true; - } else if (strcmp(argv[i], "--memory") == 0) { + } + else if (strcmp(argv[i], "--memory") == 0) { fMemory = true; } - } else { + } + else { szWhere = argv[i]; } } @@ -1251,18 +1334,23 @@ int main(int argc, char** argv) exit(1); } RunMemoryTest(szWhere); - } else if (szWhere != NULL) { + } + else if (szWhere != NULL) { if (szWhere == NULL) { fprintf(stderr, "Must specify a file name\n"); exit(1); } - if (fDir) + if (fDir) { RunTestsInDirectory(szWhere); - else + } + else { RunFileTest(szWhere); - } else if (fCorners) { + } + } + else if (fCorners) { RunCorners(); - } else { + } + else { #ifdef USE_CBOR_CONTEXT context = CreateContext((unsigned int)-1); #endif diff --git a/test/test.h b/test/test.h index 8a2a1935..7fefe476 100644 --- a/test/test.h +++ b/test/test.h @@ -90,7 +90,8 @@ int IsAlgorithmSupported(const cn_cbor* alg); if (!bReturn) { \ if (cose_error.err != errorReturn) \ onFailure; \ - } else if (errorReturn != COSE_ERR_NONE) \ + } \ + else if (errorReturn != COSE_ERR_NONE) \ onFailure; \ } @@ -100,6 +101,7 @@ int IsAlgorithmSupported(const cn_cbor* alg); if (bReturn == NULL) { \ if (cose_error.err != errorReturn) \ onFailure; \ - } else if (errorReturn != COSE_ERR_NONE) \ + } \ + else if (errorReturn != COSE_ERR_NONE) \ onFailure; \ } From 1af457ea4bceaf9462f440292441dc3406f15485 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Tue, 28 Apr 2020 10:45:15 -0700 Subject: [PATCH 33/38] Add no algorithm support for countersign tests Countersign tests no longer fail if an algorithm is not supported Correct how cmake determines if countersignatures are supported. --- CMakeLists.txt | 3 ++ include/cose/cose_configure.h | 2 +- src/CounterSign.c | 53 ++++++++++++++++++++++---- test/encrypt.c | 62 +++++++++++++++++++++++------- test/mac_test.c | 71 ++++++++++++++++++++++++++++------- test/sign.c | 48 ++++++++++++++++++++--- test/test.c | 11 ++---- 7 files changed, 200 insertions(+), 50 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b04f860a..26234867 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,6 +79,9 @@ endif() if(NOT COSE_C_INCLUDE_SIGN1) add_definitions(-DINCLUDE_SIGN1=0) endif() +if (NOT COSE_C_INCLUDE_COUNTERSIGN) + add_definitions(-DINCLUDE_COUNTERSIGN=0) +endif() if(COSE_C_USE_CONTEXT) add_definitions(-DUSE_CBOR_CONTEXT) endif() diff --git a/include/cose/cose_configure.h b/include/cose/cose_configure.h index 873bdd81..753b8d4b 100644 --- a/include/cose/cose_configure.h +++ b/include/cose/cose_configure.h @@ -170,7 +170,7 @@ #define INCLUDE_SIGN1 1 #endif #ifndef INCLUDE_COUNTERSIGNATURE -#define INCLUDE_COUNTERSIGNATURE 0 +#define INCLUDE_COUNTERSIGNATURE 1 #endif #ifndef INCLUDE_COUNTERSIGNATURE1 #define INCLUDE_COUNTERSIGNATURE1 0 diff --git a/src/CounterSign.c b/src/CounterSign.c index ad9b8d7e..af3d3e8a 100644 --- a/src/CounterSign.c +++ b/src/CounterSign.c @@ -41,8 +41,9 @@ COSE_CounterSign* _COSE_CounterSign_Init_From_Object(cn_cbor* cbor, COSE_CounterSign* pobj = pIn; cose_errback error = {0}; - if (perr == NULL) + if (perr == NULL) { perr = &error; + } if (pobj == NULL) { pobj = (COSE_CounterSign*)COSE_CALLOC( @@ -174,8 +175,9 @@ bool _COSE_CounterSign_create(COSE* pMessage, cn_cbor* pcn = NULL; cn_cbor* pcn2 = NULL; - if (pMessage->m_counterSigners == NULL) + if (pMessage->m_counterSigners == NULL) { return true; + } // One or more than one? if (pMessage->m_counterSigners->m_signer.m_signerNext != NULL) { @@ -199,8 +201,9 @@ bool _COSE_CounterSign_create(COSE* pMessage, CHECK_CONDITION_CBOR(pcnBody != NULL, cbor_err); if (!_COSE_Signer_sign( - &pSigner->m_signer, pcnBody, pcn2, "CounterSignature", perr)) + &pSigner->m_signer, pcnBody, pcn2, "CounterSignature", perr)) { goto errorReturn; + } pcn = NULL; pcn2 = NULL; @@ -215,18 +218,22 @@ bool _COSE_CounterSign_create(COSE* pMessage, } if (!_COSE_map_put(pMessage, COSE_Header_CounterSign, pArray, - COSE_UNPROTECT_ONLY, perr)) + COSE_UNPROTECT_ONLY, perr)) { goto errorReturn; + } return true; errorReturn: - if (pArray != NULL) + if (pArray != NULL) { CN_CBOR_FREE(pArray, context); - if ((pcn != NULL) && (pcn->parent != NULL)) + } + if ((pcn != NULL) && (pcn->parent != NULL)) { CN_CBOR_FREE(pcn, context); - if ((pcn2 != NULL) && (pcn2->parent != NULL)) + } + if ((pcn2 != NULL) && (pcn2->parent != NULL)) { CN_CBOR_FREE(pcn2, context); + } return false; } @@ -306,8 +313,9 @@ bool COSE_CounterSign_SetExternal(HCOSE_COUNTERSIGN hcose, cose_errback* perr) { if (!IsValidCounterSignHandle(hcose)) { - if (perr != NULL) + if (perr != NULL) { perr->err = COSE_ERR_INVALID_HANDLE; + } return false; } @@ -365,6 +373,35 @@ bool _COSE_CounterSign_Sign(COSE* baseMessage, return fRet; } +/*! brief Retrieve header parameter from an enveloped message structure + * + * Retrieve a header parameter from the message. + * Retrieved object is the same as the one in the message - do not delete it + * + * @param[in] h Handle of recipient object + * @param[in] key Key to look for + * @param[in] flags What buckets should we look for the message + * @param[out] perror Location to return error codes + * @return Object which is found or NULL + */ + +cn_cbor* COSE_CounterSign_map_get_int(HCOSE_COUNTERSIGN h, + int key, + int flags, + cose_errback* perror) +{ + if (!IsValidCounterSignHandle(h)) { + if (perror != NULL) { + perror->err = COSE_ERR_INVALID_HANDLE; + } + return NULL; + } + + return _COSE_map_get_int( + &((COSE_CounterSign*)h)->m_signer.m_message, key, flags, perror); +} + + #if INCLUDE_SIGN /*************************************************************************************************** * diff --git a/test/encrypt.c b/test/encrypt.c index 5ac5aac8..12d845c6 100644 --- a/test/encrypt.c +++ b/test/encrypt.c @@ -36,9 +36,9 @@ bool DecryptMessage(const byte *pbEncoded, HCOSE_RECIPIENT hRecip1 = NULL; HCOSE_RECIPIENT hRecip2 = NULL; bool fRet = false; - int type; + int type = 0; cose_errback cose_err; - cn_cbor *pkey; + cn_cbor *pkey = NULL; bool fNoSupport = false; hEnc = (HCOSE_ENVELOPED)COSE_Decode(pbEncoded, cbEncoded, &type, @@ -181,11 +181,13 @@ bool DecryptMessage(const byte *pbEncoded, } for (int counterNo = 0; counterNo < count; counterNo++) { + bool noSupportSign = false; + HCOSE_COUNTERSIGN h = COSE_Recipient_get_countersignature(hRecip1, counterNo, 0); if (h == NULL) { fRet = false; - goto errorReturn; + continue; } cn_cbor *counterSigner = cn_cbor_index( @@ -195,13 +197,23 @@ bool DecryptMessage(const byte *pbEncoded, BuildKey(cn_cbor_mapget_string(counterSigner, "key"), false); if (pkeyCountersign == NULL) { fRet = false; - goto errorReturn; + COSE_CounterSign_Free(h); + continue; } if (!COSE_CounterSign_SetKey(h, pkeyCountersign, 0)) { fRet = false; - goto errorReturn; + COSE_CounterSign_Free(h); + CN_CBOR_FREE(pkeyCountersign, context); + continue; + } + + alg = COSE_CounterSign_map_get_int(h, COSE_Header_Algorithm, COSE_BOTH, NULL); + if (!IsAlgorithmSupported(alg)) { + noSupportSign = true; + fNoSupport = true; } + if (COSE_Recipient_CounterSign_validate(hRecip1, h, 0)) { // I don't think we have any forced errors yet. @@ -212,7 +224,7 @@ bool DecryptMessage(const byte *pbEncoded, counterNo -= 1; } else { - fRet = false; + fRet = !noSupportSign; } } @@ -244,11 +256,12 @@ bool DecryptMessage(const byte *pbEncoded, } for (int counterNo = 0; counterNo < count; counterNo++) { + bool noSupportSign = false; HCOSE_COUNTERSIGN h = COSE_Enveloped_get_countersignature(hEnc, counterNo, 0); if (h == NULL) { fRet = false; - goto errorReturn; + continue; } cn_cbor *counterSigner = cn_cbor_index( @@ -258,14 +271,24 @@ bool DecryptMessage(const byte *pbEncoded, BuildKey(cn_cbor_mapget_string(counterSigner, "key"), false); if (pkeyCountersign == NULL) { fRet = false; - goto errorReturn; + COSE_CounterSign_Free(h); + continue; } if (!COSE_CounterSign_SetKey(h, pkeyCountersign, 0)) { fRet = false; - goto errorReturn; + COSE_CounterSign_Free(h); + CN_CBOR_FREE(pkeyCountersign, context); + continue; } + alg = COSE_CounterSign_map_get_int( + h, COSE_Header_Algorithm, COSE_BOTH, NULL); + if (!IsAlgorithmSupported(alg)) { + noSupportSign = true; + fNoSupport = true; + } + if (COSE_Enveloped_CounterSign_validate(hEnc, h, 0)) { // I don't think we have any forced errors yet. } @@ -275,7 +298,7 @@ bool DecryptMessage(const byte *pbEncoded, counterNo -= 1; } else { - fRet = false; + fRet = !noSupportSign; } } @@ -845,11 +868,14 @@ int _ValidateEncrypt(const cn_cbor *pControl, } for (int counterNo = 0; counterNo < count; counterNo++) { + bool noSupportSign = false; + bool failThis = false; + HCOSE_COUNTERSIGN h = COSE_Encrypt0_get_countersignature(hEnc, counterNo, 0); if (h == NULL) { fFail = true; - goto exitHere; + continue; } cn_cbor *counterSigner = cn_cbor_index( @@ -859,14 +885,24 @@ int _ValidateEncrypt(const cn_cbor *pControl, BuildKey(cn_cbor_mapget_string(counterSigner, "key"), false); if (pkeyCountersign == NULL) { fFail = true; - goto exitHere; + COSE_CounterSign_Free(h); + continue; } if (!COSE_CounterSign_SetKey(h, pkeyCountersign, 0)) { fFail = true; - goto exitHere; + COSE_CounterSign_Free(h); + CN_CBOR_FREE(pkeyCountersign, context); + continue; } + alg = COSE_CounterSign_map_get_int( + h, COSE_Header_Algorithm, COSE_BOTH, NULL); + if (!IsAlgorithmSupported(alg)) { + noSupportSign = true; + fAlgSupport = false; + } + if (COSE_Encrypt0_CounterSign_validate(hEnc, h, 0)) { // I don't think we have any forced errors yet. } diff --git a/test/mac_test.c b/test/mac_test.c index ad455e74..11139960 100644 --- a/test/mac_test.c +++ b/test/mac_test.c @@ -155,10 +155,19 @@ int _ValidateMAC(const cn_cbor *pControl, } for (int counterNo = 0; counterNo < count; counterNo++) { + bool noSignSupport = false; HCOSE_COUNTERSIGN h = COSE_Recipient_get_countersignature(hRecip, counterNo, 0); if (h == NULL) { - goto failTest; + continue; + } + + alg = COSE_CounterSign_map_get_int( + h, COSE_Header_Algorithm, COSE_BOTH, NULL); + if (!IsAlgorithmSupported(alg)) { + fAlgNoSupport = true; + noSignSupport = true; + returnCode = 0; } cn_cbor *counterSigner = cn_cbor_index(countersigners, @@ -167,11 +176,16 @@ int _ValidateMAC(const cn_cbor *pControl, cn_cbor *pkeyCountersign = BuildKey( cn_cbor_mapget_string(counterSigner, "key"), false); if (pkeyCountersign == NULL) { - goto failTest; + fFail = true; + COSE_CounterSign_Free(h); + continue; } if (!COSE_CounterSign_SetKey(h, pkeyCountersign, 0)) { - goto failTest; + fFail = true; + CN_CBOR_FREE(pkeyCountersign, context); + COSE_CounterSign_Free(h); + continue; } if (COSE_Recipient_CounterSign_validate(hRecip, h, 0)) { @@ -183,7 +197,7 @@ int _ValidateMAC(const cn_cbor *pControl, counterNo -= 1; } else { - goto failTest; + fFail |= !noSignSupport; } } @@ -216,10 +230,13 @@ int _ValidateMAC(const cn_cbor *pControl, } for (int counterNo = 0; counterNo < count; counterNo++) { + bool noSignSupport = false; + HCOSE_COUNTERSIGN h = COSE_Mac_get_countersignature(hMAC, counterNo, 0); if (h == NULL) { - goto failTest; + fFail = true; + continue; } cn_cbor *counterSigner = cn_cbor_index( @@ -228,11 +245,24 @@ int _ValidateMAC(const cn_cbor *pControl, cn_cbor *pkeyCountersign = BuildKey(cn_cbor_mapget_string(counterSigner, "key"), false); if (pkeyCountersign == NULL) { - goto failTest; + fFail = true; + COSE_CounterSign_Free(h); + continue; } if (!COSE_CounterSign_SetKey(h, pkeyCountersign, 0)) { - goto failTest; + fFail = true; + COSE_CounterSign_Free(h); + CN_CBOR_FREE(pkeyCountersign, context); + continue; + } + + cn_cbor *alg = COSE_CounterSign_map_get_int( + h, COSE_Header_Algorithm, COSE_BOTH, NULL); + if (!IsAlgorithmSupported(alg)) { + fAlgNoSupport = true; + noSignSupport = true; + returnCode = 0; } if (COSE_Mac_CounterSign_validate(hMAC, h, 0)) { @@ -244,7 +274,7 @@ int _ValidateMAC(const cn_cbor *pControl, counterNo -= 1; } else { - fFail = true; + fFail |= !noSignSupport; } } @@ -265,7 +295,7 @@ int _ValidateMAC(const cn_cbor *pControl, } } - if (fFail) { + if (fFail && !fAlgNoSupport) { CFails += 1; } return returnCode; @@ -678,11 +708,13 @@ int _ValidateMac0(const cn_cbor *pControl, } for (int counterNo = 0; counterNo < count; counterNo++) { + bool noSignAlg = false; + HCOSE_COUNTERSIGN h = COSE_Mac0_get_countersignature(hMAC, counterNo, 0); if (h == NULL) { fFail = true; - goto exitHere; + continue; } cn_cbor *counterSigner = cn_cbor_index( @@ -692,13 +724,24 @@ int _ValidateMac0(const cn_cbor *pControl, BuildKey(cn_cbor_mapget_string(counterSigner, "key"), false); if (pkeyCountersign == NULL) { fFail = true; - goto exitHere; + COSE_CounterSign_Free(h); + continue; } if (!COSE_CounterSign_SetKey(h, pkeyCountersign, 0)) { fFail = true; - goto exitHere; + COSE_CounterSign_Free(h); + CN_CBOR_FREE(pkeyCountersign, context); + continue; + } + + alg = COSE_CounterSign_map_get_int( + h, COSE_Header_Algorithm, COSE_BOTH, NULL); + if (!IsAlgorithmSupported(alg)) { + fUnsuportedAlg = true; + noSignAlg = true; } + if (COSE_Mac0_CounterSign_validate(hMAC, h, 0)) { // I don't think we have any forced errors yet. @@ -709,7 +752,7 @@ int _ValidateMac0(const cn_cbor *pControl, counterNo -= 1; } else { - fFail = true; + fFail |= !noSignAlg; } } @@ -733,7 +776,7 @@ int _ValidateMac0(const cn_cbor *pControl, if (fFail) { CFails += 1; } - return 0; + return fUnsuportedAlg ? 0 : 1; errorReturn: CFails += 1; diff --git a/test/sign.c b/test/sign.c index c342df2c..bef7b44a 100644 --- a/test/sign.c +++ b/test/sign.c @@ -140,6 +140,8 @@ int _ValidateSigned(const cn_cbor *pControl, } for (int counterNo = 0; counterNo < count; counterNo++) { + bool noSignAlg = false; + HCOSE_COUNTERSIGN h = COSE_Signer_get_countersignature(hSigner, counterNo, 0); if (h == NULL) { @@ -154,14 +156,24 @@ int _ValidateSigned(const cn_cbor *pControl, cn_cbor_mapget_string(counterSigner, "key"), false); if (pkeyCountersign == NULL) { fFail = true; + COSE_CounterSign_Free(h); continue; } if (!COSE_CounterSign_SetKey(h, pkeyCountersign, 0)) { fFail = true; + COSE_CounterSign_Free(h); + CN_CBOR_FREE(pkeyCountersign, context); continue; } + alg = COSE_CounterSign_map_get_int( + h, COSE_Header_Algorithm, COSE_BOTH, 0); + if (!IsAlgorithmSupported(alg)) { + fNoSupportAlg = true; + noSignAlg = true; + } + if (COSE_Signer_CounterSign_validate(hSigner, h, 0)) { // I don't think we have any forced errors yet. } @@ -171,7 +183,7 @@ int _ValidateSigned(const cn_cbor *pControl, counterNo -= 1; } else { - fFail = true; + fFail |= !noSignAlg; } } @@ -203,6 +215,8 @@ int _ValidateSigned(const cn_cbor *pControl, } for (int counterNo = 0; counterNo < count; counterNo++) { + bool noSignAlg = false; + HCOSE_COUNTERSIGN h = COSE_Sign_get_countersignature(hSig, counterNo, 0); if (h == NULL) { @@ -217,14 +231,24 @@ int _ValidateSigned(const cn_cbor *pControl, cn_cbor_mapget_string(counterSigner, "key"), false); if (pkeyCountersign == NULL) { fFail = true; + COSE_CounterSign_Free(h); continue; } if (!COSE_CounterSign_SetKey(h, pkeyCountersign, 0)) { fFail = true; + COSE_CounterSign_Free(h); + CN_CBOR_FREE(pkeyCountersign, context); continue; } + alg = COSE_CounterSign_map_get_int( + h, COSE_Header_Algorithm, COSE_BOTH, 0); + if (!IsAlgorithmSupported(alg)) { + fNoSupportAlg = true; + noSignAlg = true; + } + if (COSE_Sign_CounterSign_validate(hSig, h, 0)) { // I don't think we have any forced errors yet. } @@ -234,7 +258,7 @@ int _ValidateSigned(const cn_cbor *pControl, counterNo -= 1; } else { - fFail = true; + fFail |= !noSignAlg; } } @@ -632,11 +656,13 @@ int _ValidateSign1(const cn_cbor *pControl, } for (int counterNo = 0; counterNo < count; counterNo++) { + bool noSignAlg = false; + HCOSE_COUNTERSIGN h = COSE_Sign1_get_countersignature(hSig, counterNo, 0); if (h == NULL) { fFail = true; - goto exitHere; + continue; } cn_cbor *counterSigner = cn_cbor_index( @@ -646,12 +672,22 @@ int _ValidateSign1(const cn_cbor *pControl, BuildKey(cn_cbor_mapget_string(counterSigner, "key"), false); if (pkeyCountersign == NULL) { fFail = true; - goto exitHere; + COSE_CounterSign_Free(h); + continue; } if (!COSE_CounterSign_SetKey(h, pkeyCountersign, 0)) { fFail = true; - goto exitHere; + COSE_CounterSign_Free(h); + CN_CBOR_FREE(pkeyCountersign, context); + continue; + } + + alg = COSE_Sign1_map_get_int( + hSig, COSE_Header_Algorithm, COSE_BOTH, NULL); + if (!IsAlgorithmSupported(alg)) { + fNoAlgSupport = true; + noSignAlg = true; } if (COSE_Sign1_CounterSign_validate(hSig, h, 0)) { @@ -663,7 +699,7 @@ int _ValidateSign1(const cn_cbor *pControl, counterNo -= 1; } else { - fFail = true; + fFail |= !noSignAlg; } } diff --git a/test/test.c b/test/test.c index bdb407cd..921c6895 100644 --- a/test/test.c +++ b/test/test.c @@ -88,9 +88,7 @@ NameMap RgCurveNames[7] = {{"P-256", 1}, {"P-384", 2}, {"P-521", 3}, int MapName(const cn_cbor* p, NameMap* rgMap, unsigned int cMap) { - unsigned int i; - - for (i = 0; i < cMap; i++) { + for (unsigned int i = 0; i < cMap; i++) { if (strcmp(rgMap[i].sz, p->v.str) == 0) { return rgMap[i].i; } @@ -125,9 +123,8 @@ byte* FromHex(const char* rgch, int cch) { byte* pb = malloc(cch / 2); const char* pb2 = rgch; - int i; - for (i = 0; i < cch; i += 2) { + for (int i = 0; i < cch; i += 2) { pb[i / 2] = fromHex(pb2[i]) * 16 + fromHex(pb2[i + 1]); } @@ -270,13 +267,11 @@ int IsAlgorithmSupported(const cn_cbor* alg) case -999: // Unsupported algorithm for testing. return true; } - return true; } byte* GetCBOREncoding(const cn_cbor* pControl, int* pcbEncoded) { const cn_cbor* pOutputs = cn_cbor_mapget_string(pControl, "output"); - const cn_cbor* pCBOR; byte* pb = NULL; const byte* pb2; int i; @@ -286,7 +281,7 @@ byte* GetCBOREncoding(const cn_cbor* pControl, int* pcbEncoded) exit(1); } - pCBOR = cn_cbor_mapget_string(pOutputs, "cbor"); + const cn_cbor* pCBOR = cn_cbor_mapget_string(pOutputs, "cbor"); if ((pCBOR == NULL) || (pCBOR->type != CN_CBOR_TEXT)) { fprintf(stderr, "Invalid cbor object"); exit(1); From ae2ef6ae571086fb8e339063c505dce9c5b246de Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Tue, 28 Apr 2020 11:21:29 -0700 Subject: [PATCH 34/38] Correct Mac0 build function --- test/mac_test.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/mac_test.c b/test/mac_test.c index 11139960..7503e1f9 100644 --- a/test/mac_test.c +++ b/test/mac_test.c @@ -838,9 +838,6 @@ int BuildMac0Message(const cn_cbor *pControl) cn_cbor *k = cn_cbor_mapget_int(pkey, -1); - if (!COSE_Mac0_encrypt(hMacObj, k->v.bytes, k->length, NULL)) { - goto returnError; - } #if INCLUDE_COUNTERSIGNATURE // On the sign body @@ -881,6 +878,10 @@ int BuildMac0Message(const cn_cbor *pControl) #endif + if (!COSE_Mac0_encrypt(hMacObj, k->v.bytes, k->length, NULL)) { + goto returnError; + } + size_t cb = COSE_Encode((HCOSE)hMacObj, NULL, 0, 0) + 1; byte *rgb = (byte *)malloc(cb); cb = COSE_Encode((HCOSE)hMacObj, rgb, 0, cb); From 3c30726e40fc3b0c7c4ebc9f108576001110c6f3 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Tue, 28 Apr 2020 11:47:09 -0700 Subject: [PATCH 35/38] Should fix my appveyor build problem --- include/cose/cose_configure.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/cose/cose_configure.h b/include/cose/cose_configure.h index 753b8d4b..5250faa6 100644 --- a/include/cose/cose_configure.h +++ b/include/cose/cose_configure.h @@ -143,7 +143,9 @@ // Requires OPEN SSL 1.1.1 to build #define USE_EDDSA #else -#pragma message("OPENSSL VERSION IS " #OPENSSL_VERISON_NUMBER) +#define STRINGIFY(x) #x +#define TOSTRING(x) STRINGIFY(x) +#pragma message("OPENSSL VERSION IS " OPENSSL_VERSION_TEXT) #endif #endif // !defined (USE_MBED_TLS) From 80ed5bd6dd016095c8fe8564f9f16ce51d58ebef Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Tue, 28 Apr 2020 11:54:59 -0700 Subject: [PATCH 36/38] Don't push my personal files --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index bec05a8f..e5b428ba 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ *.x86_64 *.hex a.bat +clang-format.bat # Debug files *.dSYM/ @@ -59,6 +60,7 @@ build *.VC.opendb .vs .vscode +CMakeSettings.json # CMake and CTest directories project_cn-cbor-prefix From 6d4f5ae9108b5b67fcf956db54a079e6cbee8e2c Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Tue, 28 Apr 2020 12:08:25 -0700 Subject: [PATCH 37/38] CLang-format --- src/CounterSign.c | 1 - src/MacMessage.c | 4 ++-- src/Sign.c | 2 +- src/Sign1.c | 2 +- src/SignerInfo.c | 12 ++++++------ test/encrypt.c | 30 +++++++++++++++--------------- test/mac_test.c | 8 +++----- test/sign.c | 33 +++++++++++++++++---------------- test/test.c | 20 ++++++++++---------- 9 files changed, 55 insertions(+), 57 deletions(-) diff --git a/src/CounterSign.c b/src/CounterSign.c index af3d3e8a..4c52e701 100644 --- a/src/CounterSign.c +++ b/src/CounterSign.c @@ -401,7 +401,6 @@ cn_cbor* COSE_CounterSign_map_get_int(HCOSE_COUNTERSIGN h, &((COSE_CounterSign*)h)->m_signer.m_message, key, flags, perror); } - #if INCLUDE_SIGN /*************************************************************************************************** * diff --git a/src/MacMessage.c b/src/MacMessage.c index c5d29e13..96ef06ad 100644 --- a/src/MacMessage.c +++ b/src/MacMessage.c @@ -557,8 +557,8 @@ bool _COSE_Mac_compute(COSE_MacMessage *pcose, #ifdef USE_HMAC_256_64 case COSE_Algorithm_HMAC_256_64: - if (!HMAC_Create( - pcose, 256, 64, pbKey, cbKey, pbAuthData, cbAuthData, perr)) { + if (!HMAC_Create(pcose, 256, 64, pbKey, cbKey, pbAuthData, + cbAuthData, perr)) { goto errorReturn; } break; diff --git a/src/Sign.c b/src/Sign.c index 9fed2cd8..0bfe1856 100644 --- a/src/Sign.c +++ b/src/Sign.c @@ -302,7 +302,7 @@ bool COSE_Sign_Sign(HCOSE_SIGN h, cose_errback *perr) } for (pSigner = pMessage->m_signerFirst; pSigner != NULL; - pSigner = pSigner->m_signerNext) { + pSigner = pSigner->m_signerNext) { if (!_COSE_Signer_sign( pSigner, pcborBody, pcborProtected, "Signature", perr)) { goto errorReturn; diff --git a/src/Sign1.c b/src/Sign1.c index 558dc6d9..d9e05682 100644 --- a/src/Sign1.c +++ b/src/Sign1.c @@ -349,7 +349,7 @@ static bool CreateSign1AAD(COSE_Sign1Message *pMessage, } else { cn = cn_cbor_data_create(cn2->v.bytes, (int)cn2->length, - CBOR_CONTEXT_PARAM_COMMA & cbor_error); + CBOR_CONTEXT_PARAM_COMMA & cbor_error); } CHECK_CONDITION_CBOR(cn != NULL, cbor_error); CHECK_CONDITION_CBOR( diff --git a/src/SignerInfo.c b/src/SignerInfo.c index fac8d911..8d117c57 100644 --- a/src/SignerInfo.c +++ b/src/SignerInfo.c @@ -165,7 +165,7 @@ static bool BuildToBeSigned(byte **ppbToSign, } else { cn = cn_cbor_data_create(pcborProtected->v.bytes, - (int)pcborProtected->length, CBOR_CONTEXT_PARAM_COMMA & cbor_error); + (int)pcborProtected->length, CBOR_CONTEXT_PARAM_COMMA & cbor_error); } CHECK_CONDITION_CBOR(cn != NULL, cbor_error); CHECK_CONDITION_CBOR( @@ -179,8 +179,8 @@ static bool BuildToBeSigned(byte **ppbToSign, } else { cn = cn_cbor_data_create(pcborProtectedSign->v.bytes, - (int)pcborProtectedSign->length, - CBOR_CONTEXT_PARAM_COMMA & cbor_error); + (int)pcborProtectedSign->length, + CBOR_CONTEXT_PARAM_COMMA & cbor_error); } CHECK_CONDITION_CBOR(cn != NULL, cbor_error); CHECK_CONDITION_CBOR( @@ -271,9 +271,9 @@ bool _COSE_Signer_sign(COSE_SignerInfo *pSigner, } if (!BuildToBeSigned(&pbToSign, &cbToSign, pcborBody, pcborProtected, - pcborProtectedSign, pSigner->m_message.m_pbExternal, - pSigner->m_message.m_cbExternal, contextString, - CBOR_CONTEXT_PARAM_COMMA perr)) { + pcborProtectedSign, pSigner->m_message.m_pbExternal, + pSigner->m_message.m_cbExternal, contextString, + CBOR_CONTEXT_PARAM_COMMA perr)) { goto errorReturn; } diff --git a/test/encrypt.c b/test/encrypt.c index 12d845c6..a776183a 100644 --- a/test/encrypt.c +++ b/test/encrypt.c @@ -182,7 +182,7 @@ bool DecryptMessage(const byte *pbEncoded, for (int counterNo = 0; counterNo < count; counterNo++) { bool noSupportSign = false; - + HCOSE_COUNTERSIGN h = COSE_Recipient_get_countersignature(hRecip1, counterNo, 0); if (h == NULL) { @@ -208,12 +208,12 @@ bool DecryptMessage(const byte *pbEncoded, continue; } - alg = COSE_CounterSign_map_get_int(h, COSE_Header_Algorithm, COSE_BOTH, NULL); + alg = COSE_CounterSign_map_get_int( + h, COSE_Header_Algorithm, COSE_BOTH, NULL); if (!IsAlgorithmSupported(alg)) { noSupportSign = true; fNoSupport = true; } - if (COSE_Recipient_CounterSign_validate(hRecip1, h, 0)) { // I don't think we have any forced errors yet. @@ -288,7 +288,7 @@ bool DecryptMessage(const byte *pbEncoded, noSupportSign = true; fNoSupport = true; } - + if (COSE_Enveloped_CounterSign_validate(hEnc, h, 0)) { // I don't think we have any forced errors yet. } @@ -628,17 +628,17 @@ int EncryptMessage() goto errorReturn; } if (!COSE_Enveloped_map_put_int(hEncObj, COSE_Header_Algorithm, - cn_cbor_int_create(COSE_Algorithm_AES_CCM_16_64_128, - CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) { + cn_cbor_int_create(COSE_Algorithm_AES_CCM_16_64_128, + CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { goto errorReturn; } if (!COSE_Enveloped_SetContent(hEncObj, (byte *)sz, strlen(sz), NULL)) { goto errorReturn; } if (!COSE_Enveloped_map_put_int(hEncObj, COSE_Header_IV, - cn_cbor_data_create(rgbKid, 13, CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_UNPROTECT_ONLY, NULL)) { + cn_cbor_data_create(rgbKid, 13, CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_UNPROTECT_ONLY, NULL)) { goto errorReturn; } @@ -870,7 +870,7 @@ int _ValidateEncrypt(const cn_cbor *pControl, for (int counterNo = 0; counterNo < count; counterNo++) { bool noSupportSign = false; bool failThis = false; - + HCOSE_COUNTERSIGN h = COSE_Encrypt0_get_countersignature(hEnc, counterNo, 0); if (h == NULL) { @@ -902,7 +902,7 @@ int _ValidateEncrypt(const cn_cbor *pControl, noSupportSign = true; fAlgSupport = false; } - + if (COSE_Encrypt0_CounterSign_validate(hEnc, h, 0)) { // I don't think we have any forced errors yet. } @@ -1332,8 +1332,8 @@ void Encrypt_Corners() CFails++; } if (!COSE_Encrypt_map_put_int(hEncrypt, COSE_Header_Algorithm, - cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) { + cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; } CHECK_FAILURE(COSE_Encrypt_encrypt(hEncrypt, rgb, sizeof(rgb), &cose_error), @@ -1348,8 +1348,8 @@ void Encrypt_Corners() CFails++; } if (!COSE_Encrypt_map_put_int(hEncrypt, COSE_Header_Algorithm, - cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) { + cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; } CHECK_FAILURE(COSE_Encrypt_encrypt(hEncrypt, rgb, sizeof(rgb), &cose_error), diff --git a/test/mac_test.c b/test/mac_test.c index 7503e1f9..2fd11811 100644 --- a/test/mac_test.c +++ b/test/mac_test.c @@ -231,7 +231,7 @@ int _ValidateMAC(const cn_cbor *pControl, for (int counterNo = 0; counterNo < count; counterNo++) { bool noSignSupport = false; - + HCOSE_COUNTERSIGN h = COSE_Mac_get_countersignature(hMAC, counterNo, 0); if (h == NULL) { @@ -709,7 +709,7 @@ int _ValidateMac0(const cn_cbor *pControl, for (int counterNo = 0; counterNo < count; counterNo++) { bool noSignAlg = false; - + HCOSE_COUNTERSIGN h = COSE_Mac0_get_countersignature(hMAC, counterNo, 0); if (h == NULL) { @@ -741,7 +741,6 @@ int _ValidateMac0(const cn_cbor *pControl, fUnsuportedAlg = true; noSignAlg = true; } - if (COSE_Mac0_CounterSign_validate(hMAC, h, 0)) { // I don't think we have any forced errors yet. @@ -838,7 +837,6 @@ int BuildMac0Message(const cn_cbor *pControl) cn_cbor *k = cn_cbor_mapget_int(pkey, -1); - #if INCLUDE_COUNTERSIGNATURE // On the sign body cn_cbor *countersigns = cn_cbor_mapget_string(pMac, "countersign"); @@ -881,7 +879,7 @@ int BuildMac0Message(const cn_cbor *pControl) if (!COSE_Mac0_encrypt(hMacObj, k->v.bytes, k->length, NULL)) { goto returnError; } - + size_t cb = COSE_Encode((HCOSE)hMacObj, NULL, 0, 0) + 1; byte *rgb = (byte *)malloc(cb); cb = COSE_Encode((HCOSE)hMacObj, rgb, 0, cb); diff --git a/test/sign.c b/test/sign.c index bef7b44a..870adc1e 100644 --- a/test/sign.c +++ b/test/sign.c @@ -141,7 +141,7 @@ int _ValidateSigned(const cn_cbor *pControl, for (int counterNo = 0; counterNo < count; counterNo++) { bool noSignAlg = false; - + HCOSE_COUNTERSIGN h = COSE_Signer_get_countersignature(hSigner, counterNo, 0); if (h == NULL) { @@ -173,7 +173,7 @@ int _ValidateSigned(const cn_cbor *pControl, fNoSupportAlg = true; noSignAlg = true; } - + if (COSE_Signer_CounterSign_validate(hSigner, h, 0)) { // I don't think we have any forced errors yet. } @@ -216,7 +216,7 @@ int _ValidateSigned(const cn_cbor *pControl, for (int counterNo = 0; counterNo < count; counterNo++) { bool noSignAlg = false; - + HCOSE_COUNTERSIGN h = COSE_Sign_get_countersignature(hSig, counterNo, 0); if (h == NULL) { @@ -242,13 +242,13 @@ int _ValidateSigned(const cn_cbor *pControl, continue; } - alg = COSE_CounterSign_map_get_int( + alg = COSE_CounterSign_map_get_int( h, COSE_Header_Algorithm, COSE_BOTH, 0); if (!IsAlgorithmSupported(alg)) { fNoSupportAlg = true; noSignAlg = true; } - + if (COSE_Sign_CounterSign_validate(hSig, h, 0)) { // I don't think we have any forced errors yet. } @@ -590,7 +590,7 @@ int _ValidateSign1(const cn_cbor *pControl, } hSig = (HCOSE_SIGN1)COSE_Decode(pbEncoded, cbEncoded, &type, - COSE_sign1_object, CBOR_CONTEXT_PARAM_COMMA NULL); + COSE_sign1_object, CBOR_CONTEXT_PARAM_COMMA NULL); if (hSig == NULL) { if (fFailBody) { return 0; @@ -600,7 +600,8 @@ int _ValidateSign1(const cn_cbor *pControl, } } - if (!SetReceivingAttributes((HCOSE)hSig, pSign, Attributes_Sign1_protected)) { + if (!SetReceivingAttributes( + (HCOSE)hSig, pSign, Attributes_Sign1_protected)) { goto returnError; } @@ -657,7 +658,7 @@ int _ValidateSign1(const cn_cbor *pControl, for (int counterNo = 0; counterNo < count; counterNo++) { bool noSignAlg = false; - + HCOSE_COUNTERSIGN h = COSE_Sign1_get_countersignature(hSig, counterNo, 0); if (h == NULL) { @@ -996,8 +997,8 @@ void Sign_Corners() CFails++; } if (!COSE_Signer_map_put_int(hSigner, COSE_Header_Algorithm, - cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) { + cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; } if (!COSE_Sign_AddSigner(hSign, hSigner, NULL)) { @@ -1024,8 +1025,8 @@ void Sign_Corners() CFails++; } if (!COSE_Signer_map_put_int(hSigner, COSE_Header_Algorithm, - cn_cbor_string_create("hmac", CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) { + cn_cbor_string_create("hmac", CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; } if (!COSE_Sign_AddSigner(hSign, hSigner, NULL)) { @@ -1138,8 +1139,8 @@ void Sign1_Corners() CFails++; } if (!COSE_Sign1_map_put_int(hSign, COSE_Header_Algorithm, - cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) { + cn_cbor_int_create(-99, CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; } CHECK_FAILURE(COSE_Sign1_Sign(hSign, cn, &cose_error), @@ -1156,8 +1157,8 @@ void Sign1_Corners() } if (!COSE_Sign1_map_put_int(hSign, COSE_Header_Algorithm, - cn_cbor_string_create("hmac", CBOR_CONTEXT_PARAM_COMMA NULL), - COSE_PROTECT_ONLY, NULL)) { + cn_cbor_string_create("hmac", CBOR_CONTEXT_PARAM_COMMA NULL), + COSE_PROTECT_ONLY, NULL)) { CFails++; } CHECK_FAILURE(COSE_Sign1_Sign(hSign, cn, &cose_error), diff --git a/test/test.c b/test/test.c index 921c6895..ec1a25af 100644 --- a/test/test.c +++ b/test/test.c @@ -488,11 +488,11 @@ bool SetSendingAttributes(HCOSE hMsg, const cn_cbor* pIn, int base) goto returnError; } if (!SetAttributes(hMsg, cn_cbor_mapget_string(pIn, "unprotected"), - COSE_UNPROTECT_ONLY, base, true)) { + COSE_UNPROTECT_ONLY, base, true)) { goto returnError; } if (!SetAttributes(hMsg, cn_cbor_mapget_string(pIn, "unsent"), - COSE_DONT_SEND, base, false)) { + COSE_DONT_SEND, base, false)) { goto returnError; } @@ -743,8 +743,8 @@ cn_cbor* BuildKey(const cn_cbor* pKeyIn, bool fPublicKey) return NULL; } if (!cn_cbor_mapput_int(pKeyOut, - RgStringKeys[i].keyNew, p, - CBOR_CONTEXT_PARAM_COMMA NULL)) { + RgStringKeys[i].keyNew, p, + CBOR_CONTEXT_PARAM_COMMA NULL)) { return NULL; } break; @@ -762,8 +762,8 @@ cn_cbor* BuildKey(const cn_cbor* pKeyIn, bool fPublicKey) return NULL; } if (!cn_cbor_mapput_int(pKeyOut, - RgStringKeys[i].keyNew, p, - CBOR_CONTEXT_PARAM_COMMA NULL)) { + RgStringKeys[i].keyNew, p, + CBOR_CONTEXT_PARAM_COMMA NULL)) { return NULL; } break; @@ -776,8 +776,8 @@ cn_cbor* BuildKey(const cn_cbor* pKeyIn, bool fPublicKey) return NULL; } if (!cn_cbor_mapput_int(pKeyOut, - RgStringKeys[i].keyNew, p, - CBOR_CONTEXT_PARAM_COMMA NULL)) { + RgStringKeys[i].keyNew, p, + CBOR_CONTEXT_PARAM_COMMA NULL)) { return NULL; } break; @@ -794,8 +794,8 @@ cn_cbor* BuildKey(const cn_cbor* pKeyIn, bool fPublicKey) return NULL; } if (!cn_cbor_mapput_int(pKeyOut, - RgStringKeys[i].keyNew, p, - CBOR_CONTEXT_PARAM_COMMA NULL)) { + RgStringKeys[i].keyNew, p, + CBOR_CONTEXT_PARAM_COMMA NULL)) { return NULL; } break; From 97748d0ee22d1be5d332727171a09a8d83854bd1 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Tue, 28 Apr 2020 13:58:49 -0700 Subject: [PATCH 38/38] Push up code review changes --- .github/workflows/main.yml | 1 + src/CounterSign.c | 5 +++-- src/Encrypt.c | 26 -------------------------- test/test.c | 1 + 4 files changed, 5 insertions(+), 28 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 891ab4a0..d2054311 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -26,6 +26,7 @@ jobs: "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=ON -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=ON", "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=ON -DCOSE_C_INCLUDE_SIGN1=OFF -DCOSE_C_INCLUDE_COUNTERSIGN=ON", "-DCOSE_C_INCLUDE_ENCRYPT=OFF -DCOSE_C_INCLUDE_ENCRYPT0=OFF -DCOSE_C_INCLUDE_MAC0=OFF -DCOSE_C_INCLUDE_MAC=OFF -DCOSE_C_INCLUDE_SIGN=OFF -DCOSE_C_INCLUDE_SIGN1=ON -DCOSE_C_INCLUDE_COUNTERSIGN=ON", + "-DCOSE_C_OPTIMIZE -DCOSE_C_INCLUDE_COUNTERSIGN=ON" ] steps: diff --git a/src/CounterSign.c b/src/CounterSign.c index 4c52e701..7047aa09 100644 --- a/src/CounterSign.c +++ b/src/CounterSign.c @@ -139,6 +139,7 @@ bool _COSE_CounterSign_add(COSE* pMessage, return false; } +#if 0 /// Get the n-th counter signature from the attribute. /// HCOSE_COUNTERSIGN _COSE_CounterSign_get(COSE* pMessage, @@ -146,9 +147,8 @@ HCOSE_COUNTERSIGN _COSE_CounterSign_get(COSE* pMessage, cose_errback* perr) { COSE_CounterSign* pSigner = pMessage->m_counterSigners; - int i; - for (i = 0; i < iSigner; i++, pSigner = pSigner->m_next) { + for (int i = 0; i < iSigner && pSigner != NULL; i++, pSigner = pSigner->m_next) { CHECK_CONDITION(pSigner != NULL, COSE_ERR_INVALID_PARAMETER); } @@ -157,6 +157,7 @@ HCOSE_COUNTERSIGN _COSE_CounterSign_get(COSE* pMessage, errorReturn: return false; } +#endif /// _COSE_CounterSign_create /// diff --git a/src/Encrypt.c b/src/Encrypt.c index 0eb43abb..022fcc16 100644 --- a/src/Encrypt.c +++ b/src/Encrypt.c @@ -1103,30 +1103,4 @@ HCOSE_RECIPIENT COSE_Enveloped_GetRecipient(HCOSE_ENVELOPED cose, return (HCOSE_RECIPIENT)p; } -#if USE_COUNTER_SIGNATURES -bool COSE_Enveloped_AddCounterSigner(HCOSE_ENCRYPT hEnv, - HCOSE_COUNTERSIGN hSign, - cose_errback *perr) -{ - CHECK_CONDITION(IsValidEncryptHandle(hEnv), COSE_ERR_INVALID_HANDLE); - return _COSE_CounterSign_add( - &((COSE_Enveloped *)hEnv)->m_message, hSign, perr); - -errorReturn: - return false; -} - -HCOSE_COUNTERSIGN COSE_Enveloped_GetCounterSigner(HCOSE_ENCRYPT h, - int iSigner, - cose_errback *perr) -{ - CHECK_CONDITION(IsValidEncryptHandle(h), COSE_ERR_INVALID_HANDLE); - return _COSE_CounterSign_get( - &((COSE_Enveloped *)h)->m_message, iSigner, perr); - -errorReturn: - return NULL; -} -#endif - #endif diff --git a/test/test.c b/test/test.c index ec1a25af..d2f3c2d1 100644 --- a/test/test.c +++ b/test/test.c @@ -473,6 +473,7 @@ bool SetAttributes(HCOSE hHandle, assert(false); break; } + // If you uncomment this then the memory test will fail. // assert(fRet); }