Run memory test on Sign and Sign0
diff --git a/src/Sign0.c b/src/Sign0.c
index e404a53..1708142 100644
--- a/src/Sign0.c
+++ b/src/Sign0.c
@@ -273,7 +273,6 @@
if (pArray == NULL) {
if (perr != NULL) perr->err = COSE_ERR_OUT_OF_MEMORY;
errorReturn:
- if (cn != NULL) CN_CBOR_FREE(cn, context);
if (pcborBody2 != NULL) CN_CBOR_FREE(pcborBody2, context);
if (pcborProtected2 != NULL) CN_CBOR_FREE(pcborProtected2, context);
if (pArray != NULL) COSE_FREE(pArray, context);
diff --git a/src/SignerInfo.c b/src/SignerInfo.c
index 8165bbd..3d0d9d9 100644
--- a/src/SignerInfo.c
+++ b/src/SignerInfo.c
@@ -97,6 +97,7 @@
cn_cbor * pcborProtected2 = NULL;
cn_cbor * pcborProtectedSign = NULL;
cn_cbor * pArray = NULL;
+ cn_cbor * cnX = NULL;
cn_cbor * cn = NULL;
cn_cbor_errback cbor_error;
size_t cbToSign;
@@ -116,16 +117,16 @@
return false;
}
- cn = _COSE_map_get_int(&pSigner->m_message, COSE_Header_Algorithm, COSE_BOTH, perr);
- if (cn == NULL) goto errorReturn;
+ cnX = _COSE_map_get_int(&pSigner->m_message, COSE_Header_Algorithm, COSE_BOTH, perr);
+ if (cnX == NULL) goto errorReturn;
- if (cn->type == CN_CBOR_TEXT) {
+ if (cnX->type == CN_CBOR_TEXT) {
FAIL_CONDITION(COSE_ERR_UNKNOWN_ALGORITHM);
}
else {
- CHECK_CONDITION((cn->type == CN_CBOR_UINT || cn->type == CN_CBOR_INT), COSE_ERR_INVALID_PARAMETER);
+ CHECK_CONDITION((cnX->type == CN_CBOR_UINT || cnX->type == CN_CBOR_INT), COSE_ERR_INVALID_PARAMETER);
- alg = (int)cn->v.uint;
+ alg = (int)cnX->v.uint;
}
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index cac9c43..4a8d1f2 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -61,4 +61,6 @@
add_test (NAME Memory-mac0 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND cose_test --memory Examples/hmac-examples/HMac-enc-01.json )
add_test (NAME Memory-encrypt WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND cose_test --memory Examples/aes-gcm-examples/aes-gcm-enc-01.json )
add_test (NAME Memory-enveloped WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND cose_test --memory Examples/aes-gcm-examples/aes-gcm-01.json )
+add_test (NAME Memory-sign0 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND cose_test --memory Examples/ecdsa-examples/ecdsa-sig-01.json )
+add_test (NAME Memory-sign WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND cose_test --memory Examples/ecdsa-examples/ecdsa-01.json )
diff --git a/test/sign.c b/test/sign.c
index 9af7d3b..e83d430 100644
--- a/test/sign.c
+++ b/test/sign.c
@@ -29,19 +29,19 @@
fFailBody = true;
}
- if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) exit(1);
+ 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)) exit(1);
+ 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)) exit(1);
+ if ((pSigners == NULL) || (pSigners->type != CN_CBOR_ARRAY)) goto returnError;
iSigner = (int) pSigners->length - 1;
pSigners = pSigners->first_child;
for (; pSigners != NULL; iSigner--, pSigners = pSigners->next) {
hSig = (HCOSE_SIGN)COSE_Decode(pbEncoded, cbEncoded, &type, COSE_sign_object, CBOR_CONTEXT_PARAM_COMMA NULL);
- if (hSig == NULL) exit(1);
+ if (hSig == NULL) goto returnError;
cn_cbor * pkey = BuildKey(cn_cbor_mapget_string(pSigners, "key"));
@@ -80,6 +80,10 @@
if (fFail) CFails += 1;
return 0;
+
+returnError:
+ CFails += 1;
+ return 0;
}
int ValidateSigned(const cn_cbor * pControl)
@@ -104,9 +108,9 @@
HCOSE_SIGN hSignObj = COSE_Sign_Init(CBOR_CONTEXT_PARAM_COMMA NULL);
const cn_cbor * pInputs = cn_cbor_mapget_string(pControl, "input");
- if (pInputs == NULL) exit(1);
+ if (pInputs == NULL) goto returnError;
const cn_cbor * pSign = cn_cbor_mapget_string(pInputs, "sign");
- if (pSign == NULL) exit(1);
+ 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)) goto returnError;
@@ -118,28 +122,28 @@
const cn_cbor * pAlg = COSE_Sign_map_get_int(hSignObj, 1, COSE_BOTH, NULL);
const cn_cbor * pSigners = cn_cbor_mapget_string(pSign, "signers");
- if ((pSigners == NULL) || (pSigners->type != CN_CBOR_ARRAY)) exit(1);
+ 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"));
- if (pkey == NULL) exit(1);
+ if (pkey == NULL) goto returnError;
HCOSE_SIGNER hSigner = COSE_Signer_Init(CBOR_CONTEXT_PARAM_COMMA NULL);
- if (hSigner == NULL) exit(1);
+ if (hSigner == NULL) goto returnError;
if (!SetAttributes((HCOSE)hSigner, cn_cbor_mapget_string(pSigners, "protected"), Attributes_Signer_protected)) goto returnError;
if (!SetAttributes((HCOSE)hSigner, cn_cbor_mapget_string(pSigners, "unprotected"), Attributes_Signer_unprotected)) goto returnError;
if (!SetAttributes((HCOSE)hSigner, cn_cbor_mapget_string(pSigners, "unsent"), Attributes_Signer_unsent)) goto returnError;
- if (!COSE_Signer_SetKey(hSigner, pkey, NULL)) exit(1);
+ if (!COSE_Signer_SetKey(hSigner, pkey, NULL)) goto returnError;
- if (!COSE_Sign_AddSigner(hSignObj, hSigner, NULL)) exit(1);
+ if (!COSE_Sign_AddSigner(hSignObj, hSigner, NULL)) goto returnError;
COSE_Signer_Free(hSigner);
}
- if (!COSE_Sign_Sign(hSignObj, NULL)) exit(1);
+ if (!COSE_Sign_Sign(hSignObj, NULL)) goto returnError;
size_t cb = COSE_Encode((HCOSE)hSignObj, NULL, 0, 0) + 1;
byte * rgb = (byte *)malloc(cb);
@@ -247,12 +251,12 @@
fFailBody = true;
}
- if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) exit(1);
+ 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)) exit(1);
+ if ((pSign == NULL) || (pSign->type != CN_CBOR_MAP)) goto returnError;
hSig = (HCOSE_SIGN0)COSE_Decode(pbEncoded, cbEncoded, &type, COSE_sign0_object, CBOR_CONTEXT_PARAM_COMMA NULL);
- if (hSig == NULL) exit(1);
+ if (hSig == NULL) goto returnError;
cn_cbor * pkey = BuildKey(cn_cbor_mapget_string(pSign, "key"));
@@ -281,6 +285,10 @@
if (fFail) CFails += 1;
return 0;
+
+returnError:
+ CFails += 1;
+ return 0;
}
int ValidateSign0(const cn_cbor * pControl)
@@ -303,9 +311,9 @@
HCOSE_SIGN0 hSignObj = COSE_Sign0_Init(CBOR_CONTEXT_PARAM_COMMA NULL);
const cn_cbor * pInputs = cn_cbor_mapget_string(pControl, "input");
- if (pInputs == NULL) exit(1);
+ if (pInputs == NULL) goto returnError;
const cn_cbor * pSign = cn_cbor_mapget_string(pInputs, "sign0");
- if (pSign == NULL) exit(1);
+ if (pSign == NULL) goto returnError;
const cn_cbor * pContent = cn_cbor_mapget_string(pInputs, "plaintext");
if (!COSE_Sign0_SetContent(hSignObj, pContent->v.bytes, pContent->length, NULL)) goto returnError;
@@ -317,10 +325,10 @@
const cn_cbor * pAlg = COSE_Sign0_map_get_int(hSignObj, 1, COSE_BOTH, NULL);
cn_cbor * pkey = BuildKey(cn_cbor_mapget_string(pSign, "key"));
- if (pkey == NULL) exit(1);
+ if (pkey == NULL) goto returnError;
- if (!COSE_Sign0_Sign(hSignObj, pkey, NULL)) exit(1);
+ if (!COSE_Sign0_Sign(hSignObj, pkey, NULL)) goto returnError;
size_t cb = COSE_Encode((HCOSE)hSignObj, NULL, 0, 0) + 1;
byte * rgb = (byte *)malloc(cb);
diff --git a/test/test.c b/test/test.c
index e9f1e9c..17c2c99 100644
--- a/test/test.c
+++ b/test/test.c
@@ -511,6 +511,36 @@
if (CFails == 0) fBuildDone = true;
}
}
+ else if (cn_cbor_mapget_string(pInput, "sign") != NULL) {
+ if (!fValidateDone) {
+ allocator = CreateContext(iFail);
+ CFails = 0;
+ ValidateSigned(pControl);
+ if (CFails == 0) fValidateDone = true;
+ }
+
+ if (!fBuildDone) {
+ allocator = CreateContext(iFail);
+ CFails = 0;
+ BuildSignedMessage(pControl);
+ if (CFails == 0) fBuildDone = true;
+ }
+ }
+ else if (cn_cbor_mapget_string(pInput, "sign0") != NULL) {
+ if (!fValidateDone) {
+ allocator = CreateContext(iFail);
+ CFails = 0;
+ ValidateSign0(pControl);
+ if (CFails == 0) fValidateDone = true;
+ }
+
+ if (!fBuildDone) {
+ allocator = CreateContext(iFail);
+ CFails = 0;
+ BuildSign0Message(pControl);
+ if (CFails == 0) fBuildDone = true;
+ }
+ }
}
CFails = 0;
allocator = NULL;