Add Enveloded and Encrypt memory tests
diff --git a/src/openssl.c b/src/openssl.c
index efa2480..4345037 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -302,6 +302,9 @@
#endif
cn_cbor_errback cbor_error;
+ // Make it first so we can clean it up
+ EVP_CIPHER_CTX_init(&ctx);
+
// Setup the IV/Nonce and put it into the message
cbor_iv = _COSE_map_get_int(&pcose->m_message, COSE_Header_IV, COSE_BOTH, perr);
@@ -344,7 +347,6 @@
// Setup and run the OpenSSL code
- EVP_CIPHER_CTX_init(&ctx);
CHECK_CONDITION(EVP_EncryptInit_ex(&ctx, cipher, NULL, NULL, NULL), COSE_ERR_CRYPTO_FAIL);
CHECK_CONDITION(EVP_EncryptInit(&ctx, 0, pbKey, rgbIV), COSE_ERR_CRYPTO_FAIL);
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 1fdafaf..cac9c43 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -59,4 +59,6 @@
add_test (NAME corner-cases WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND cose_test --corners )
add_test (NAME Memory-mac WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND cose_test --memory Examples/hmac-examples/HMac-01.json )
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 )
diff --git a/test/encrypt.c b/test/encrypt.c
index 6887bd5..709df7c 100644
--- a/test/encrypt.c
+++ b/test/encrypt.c
@@ -30,19 +30,19 @@
fFailBody = true;
}
- if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) exit(1);
+ 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)) exit(1);
+ 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)) exit(1);
+ if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) goto errorReturn;
iRecipient = (int) pRecipients->length - 1;
pRecipients = pRecipients->first_child;
for (; pRecipients != NULL; iRecipient--, pRecipients = pRecipients->next) {
hEnc = (HCOSE_ENVELOPED)COSE_Decode(pbEncoded, cbEncoded, &type, COSE_enveloped_object, CBOR_CONTEXT_PARAM_COMMA NULL);
- if (hEnc == NULL) exit(1);
+ if (hEnc == NULL) goto errorReturn;
if (!SetAttributes((HCOSE)hEnc, cn_cbor_mapget_string(pEnveloped, "unsent"), Attributes_Enveloped_unsent)) {
fFail = true;
@@ -90,6 +90,10 @@
if (fFail) CFails += 1;
return 0;
+
+errorReturn:
+ CFails += 1;
+ return 0;
}
int ValidateEnveloped(const cn_cbor * pControl)
@@ -114,9 +118,9 @@
HCOSE_ENVELOPED hEncObj = COSE_Enveloped_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 * pEnveloped = cn_cbor_mapget_string(pInputs, "enveloped");
- if (pEnveloped == NULL) exit(1);
+ 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)) goto returnError;
@@ -126,30 +130,31 @@
if (!SetAttributes((HCOSE)hEncObj, cn_cbor_mapget_string(pEnveloped, "unsent"), Attributes_Enveloped_unsent)) goto returnError;
const cn_cbor * pAlg = COSE_Enveloped_map_get_int(hEncObj, 1, COSE_BOTH, NULL);
+ if (pAlg == NULL) goto returnError;
const cn_cbor * pRecipients = cn_cbor_mapget_string(pEnveloped, "recipients");
- if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) exit(1);
+ 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"));
- if (pkey == NULL) exit(1);
+ if (pkey == NULL) goto returnError;
HCOSE_RECIPIENT hRecip = COSE_Recipient_Init(CBOR_CONTEXT_PARAM_COMMA NULL);
- if (hRecip == NULL) exit(1);
+ if (hRecip == NULL) goto returnError;
if (!SetAttributes((HCOSE)hRecip, cn_cbor_mapget_string(pRecipients, "protected"), Attributes_Recipient_protected)) goto returnError;
if (!SetAttributes((HCOSE)hRecip, cn_cbor_mapget_string(pRecipients, "unprotected"), Attributes_Recipient_unprotected)) goto returnError;
if (!SetAttributes((HCOSE)hRecip, cn_cbor_mapget_string(pRecipients, "unsent"), Attributes_Recipient_unsent)) goto returnError;
- if (!COSE_Recipient_SetKey(hRecip, pkey, NULL)) exit(1);
+ if (!COSE_Recipient_SetKey(hRecip, pkey, NULL)) goto returnError;
- if (!COSE_Enveloped_AddRecipient(hEncObj, hRecip, NULL)) exit(1);
+ if (!COSE_Enveloped_AddRecipient(hEncObj, hRecip, NULL)) goto returnError;
COSE_Recipient_Free(hRecip);
}
- if (!COSE_Enveloped_encrypt(hEncObj, NULL)) exit(1);
+ if (!COSE_Enveloped_encrypt(hEncObj, NULL)) goto returnError;
size_t cb = COSE_Encode((HCOSE)hEncObj, NULL, 0, 0) + 1;
byte * rgb = (byte *)malloc(cb);
@@ -252,17 +257,17 @@
fFailBody = true;
}
- if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) exit(1);
+ 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)) exit(1);
+ 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)) exit(1);
+ if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) goto returnError;
pRecipients = pRecipients->first_child;
hEnc = (HCOSE_ENCRYPT)COSE_Decode(pbEncoded, cbEncoded, &type, COSE_encrypt_object, CBOR_CONTEXT_PARAM_COMMA NULL);
- if (hEnc == NULL) exit(1);
+ if (hEnc == NULL) goto returnError;
if (!SetAttributes((HCOSE)hEnc, cn_cbor_mapget_string(pEncrypt, "unsent"), Attributes_Encrypt_unsent)) {
fFail = true;
@@ -270,9 +275,7 @@
}
cn_cbor * pkey = BuildKey(cn_cbor_mapget_string(pRecipients, "key"));
- if (pkey == NULL) {
- fFail = true;
- }
+ if (pkey == NULL) goto returnError;
cn_cbor * k = cn_cbor_mapget_int(pkey, -1);
if (k == NULL) {
@@ -299,6 +302,10 @@
if (fFail) CFails += 1;
return 0;
+
+returnError:
+ CFails += 1;
+ return 0;
}
int ValidateEncrypt(const cn_cbor * pControl)
@@ -322,9 +329,9 @@
HCOSE_ENCRYPT hEncObj = COSE_Encrypt_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 * pEncrypt = cn_cbor_mapget_string(pInputs, "encrypted");
- if (pEncrypt == NULL) exit(1);
+ 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)) goto returnError;
@@ -334,18 +341,19 @@
if (!SetAttributes((HCOSE)hEncObj, cn_cbor_mapget_string(pEncrypt, "unsent"), Attributes_Encrypt_unsent)) goto returnError;
const cn_cbor * pAlg = COSE_Encrypt_map_get_int(hEncObj, 1, COSE_BOTH, NULL);
+ if (pAlg == NULL) goto returnError;
const cn_cbor * pRecipients = cn_cbor_mapget_string(pEncrypt, "recipients");
- if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) exit(1);
+ if ((pRecipients == NULL) || (pRecipients->type != CN_CBOR_ARRAY)) goto returnError;
pRecipients = pRecipients->first_child;
cn_cbor * pkey = BuildKey(cn_cbor_mapget_string(pRecipients, "key"));
- if (pkey == NULL) exit(1);
+ if (pkey == NULL) goto returnError;
cn_cbor * k = cn_cbor_mapget_int(pkey, -1);
- if (!COSE_Encrypt_encrypt(hEncObj, k->v.bytes, k->length, NULL)) exit(1);
+ 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);
diff --git a/test/test.c b/test/test.c
index 75e420c..e9f1e9c 100644
--- a/test/test.c
+++ b/test/test.c
@@ -450,7 +450,7 @@
for (iFail = 0; !fValidateDone || !fBuildDone; iFail++) {
allocator = CreateContext(iFail);
-
+
if (cn_cbor_mapget_string(pInput, "mac") != NULL) {
if (!fValidateDone) {
allocator = CreateContext(iFail);
@@ -481,6 +481,36 @@
if (CFails == 0) fBuildDone = true;
}
}
+ else if (cn_cbor_mapget_string(pInput, "encrypted") != NULL) {
+ if (!fValidateDone) {
+ allocator = CreateContext(iFail);
+ CFails = 0;
+ ValidateEncrypt(pControl);
+ if (CFails == 0) fValidateDone = true;
+ }
+
+ if (!fBuildDone) {
+ allocator = CreateContext(iFail);
+ CFails = 0;
+ BuildEncryptMessage(pControl);
+ if (CFails == 0) fBuildDone = true;
+ }
+ }
+ else if (cn_cbor_mapget_string(pInput, "enveloped") != NULL) {
+ if (!fValidateDone) {
+ allocator = CreateContext(iFail);
+ CFails = 0;
+ ValidateEnveloped(pControl);
+ if (CFails == 0) fValidateDone = true;
+ }
+
+ if (!fBuildDone) {
+ allocator = CreateContext(iFail);
+ CFails = 0;
+ BuildEnvelopedMessage(pControl);
+ if (CFails == 0) fBuildDone = true;
+ }
+ }
}
CFails = 0;
allocator = NULL;