Skip to content

Commit

Permalink
Merge d92f925 into f8434ce
Browse files Browse the repository at this point in the history
  • Loading branch information
jimsch committed Apr 29, 2020
2 parents f8434ce + d92f925 commit 140f4fa
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 30 deletions.
13 changes: 8 additions & 5 deletions src/CounterSign.c
Expand Up @@ -25,8 +25,6 @@ bool _COSE_CounterSign_Free(COSE_CounterSign* pSigner)
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);
Expand Down Expand Up @@ -54,7 +52,11 @@ COSE_CounterSign* _COSE_CounterSign_Init_From_Object(cn_cbor* cbor,
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_SignerInfo_Release(&pobj->m_signer);
if (pIn == NULL) {
COSE_FREE(pobj, context);
}
return NULL;
}

if (pIn == NULL) {
Expand Down Expand Up @@ -112,6 +114,8 @@ bool COSE_CounterSign_Free(HCOSE_COUNTERSIGN h)
return true;
}

_COSE_RemoveFromList(&CountersignRoot, &p->m_signer.m_message);

fRet = _COSE_CounterSign_Free(p);

errorReturn:
Expand All @@ -127,8 +131,7 @@ bool _COSE_CounterSign_add(COSE* pMessage,
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_next == NULL, COSE_ERR_INVALID_PARAMETER);

pSigner->m_next = pMessage->m_counterSigners;
pMessage->m_counterSigners = pSigner;
Expand Down
2 changes: 1 addition & 1 deletion src/Encrypt.c
Expand Up @@ -962,6 +962,7 @@ bool COSE_Enveloped_AddRecipient(HCOSE_ENVELOPED hEnc,

pRecip->m_recipientNext = pEncrypt->m_recipientFirst;
pEncrypt->m_recipientFirst = pRecip;
pRecip->m_encrypt.m_message.m_refCount++;

pRecipients = _COSE_arrayget_int(&pEncrypt->m_message, INDEX_RECIPIENTS);
if (pRecipients == NULL) {
Expand All @@ -983,7 +984,6 @@ bool COSE_Enveloped_AddRecipient(HCOSE_ENVELOPED hEnc,
pRecip->m_encrypt.m_message.m_cbor, &cbor_error),
cbor_error);

pRecip->m_encrypt.m_message.m_refCount++;

return true;

Expand Down
6 changes: 2 additions & 4 deletions src/SignerInfo.c
Expand Up @@ -123,11 +123,9 @@ COSE_SignerInfo *_COSE_SignerInfo_Init_From_Object(cn_cbor *cbor,
return pSigner;

errorReturn:
if (pSigner != NULL) {
if (pSigner != NULL && pIn == NULL) {
_COSE_SignerInfo_Release(pSigner);
if (pIn == NULL) {
COSE_FREE(pSigner, context);
}
COSE_FREE(pSigner, context);
}
return NULL;
}
Expand Down
9 changes: 8 additions & 1 deletion test/CMakeLists.txt
@@ -1,6 +1,6 @@
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${dist_dir}/test)

add_executable(cose_test test.c json.c encrypt.c sign.c context.c mac_test.c)
add_executable(cose_test test.c json.c encrypt.c sign.c context.c mac_test.c CounterSign_test.c)

target_link_libraries(cose_test PRIVATE cose-c::cose-c cn-cbor::cn-cbor)

Expand Down Expand Up @@ -176,3 +176,10 @@ add_test(
NAME Memory-sign-eddsa
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND cose_test --memory Examples/eddsa-examples/eddsa-01.json)

if(COSE_C_INCLUDE_COUNTERSIGN)
add_test(
NAME Memory-Countersign
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND cose_test --memory Examples/countersign/mac0-01.json)
endif()
92 changes: 92 additions & 0 deletions test/CounterSign_test.c
@@ -0,0 +1,92 @@
// encrypt.c

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cose/cose.h>
#include <cose/cose_configure.h>
#include <cn-cbor/cn-cbor.h>

#if INCLUDE_COUNTERSIGNATURE

#include "test.h"
#include "cose_int.h"

void CounterSign_Corners()
{
HCOSE_COUNTERSIGN hCounterSign = NULL;
HCOSE_COUNTERSIGN hBadHandle = NULL;
HCOSE_COUNTERSIGN hNULL = NULL;
cose_errback cose_error;
byte rgb[10];

cn_cbor *cn = cn_cbor_int_create(5, CBOR_CONTEXT_PARAM_COMMA NULL);

hCounterSign = COSE_CounterSign_Init(CBOR_CONTEXT_PARAM_COMMA NULL);
hBadHandle = (HCOSE_COUNTERSIGN)COSE_CALLOC(1, sizeof(COSE), context);
#if INCLUDE_SIGN1
HCOSE_SIGN1 hSign1 =
COSE_Sign1_Init(0, CBOR_CONTEXT_PARAM_COMMA & cose_error);
#endif
// Look for invalid parameter
// Null handle checks
// bad handle checks
// null pointers

CHECK_FAILURE(
COSE_CounterSign_map_get_int(hNULL, 1, COSE_BOTH, &cose_error),
COSE_ERR_INVALID_HANDLE, CFails++);
CHECK_FAILURE(
COSE_CounterSign_map_get_int(hNULL, 1, COSE_BOTH, &cose_error),
COSE_ERR_INVALID_HANDLE, CFails++);
CHECK_FAILURE(
COSE_CounterSign_map_get_int(hCounterSign, 1, COSE_BOTH, &cose_error),
COSE_ERR_INVALID_PARAMETER, CFails++);

CHECK_FAILURE(COSE_CounterSign_map_put_int(
hNULL, 1, cn, COSE_PROTECT_ONLY, &cose_error),
COSE_ERR_INVALID_HANDLE, CFails++);
CHECK_FAILURE(COSE_CounterSign_map_put_int(
hNULL, 1, cn, COSE_PROTECT_ONLY, &cose_error),
COSE_ERR_INVALID_HANDLE, CFails++);
CHECK_FAILURE(COSE_CounterSign_map_put_int(
hNULL, 1, NULL, COSE_PROTECT_ONLY, &cose_error),
COSE_ERR_INVALID_HANDLE, CFails++);
CHECK_FAILURE(COSE_CounterSign_map_put_int(hCounterSign, 1, cn,
COSE_PROTECT_ONLY | COSE_UNPROTECT_ONLY, &cose_error),
COSE_ERR_INVALID_PARAMETER, CFails++);

#if INCLUDE_SIGN1
CHECK_FAILURE(COSE_Sign1_add_countersignature(hSign1, hBadHandle, &cose_error),
COSE_ERR_INVALID_HANDLE, CFails++);
((COSE_CounterSign *)hCounterSign)->m_next = (COSE_CounterSign *)hBadHandle;
CHECK_FAILURE(
COSE_Sign1_add_countersignature(hSign1, hCounterSign, &cose_error),
COSE_ERR_INVALID_PARAMETER, CFails++);
CHECK_FAILURE(COSE_Sign1_get_countersignature(hSign1, 3, &cose_error),
COSE_ERR_INVALID_PARAMETER, CFails++);
((COSE_CounterSign *)hCounterSign)->m_next = NULL;
COSE_Sign1_add_countersignature(hSign1, hCounterSign, &cose_error);
CHECK_FAILURE(COSE_Sign1_get_countersignature(hSign1, 3, &cose_error),
COSE_ERR_INVALID_PARAMETER, CFails++);
#endif

CHECK_FAILURE(
COSE_CounterSign_SetExternal(hNULL, rgb, sizeof(rgb), &cose_error),
COSE_ERR_INVALID_HANDLE, CFails++);
CHECK_FAILURE(
COSE_CounterSign_SetExternal(hBadHandle, rgb, sizeof(rgb), &cose_error),
COSE_ERR_INVALID_HANDLE, CFails++);
COSE_CounterSign_SetExternal(hCounterSign, rgb, sizeof(rgb), &cose_error);


CHECK_RETURN(!COSE_CounterSign_Free(hBadHandle), false, CFails++);

COSE_CounterSign_Free(hCounterSign);
#if INCLUDE_SIGN1
COSE_Sign1_Free(hSign1);
#endif
}
#endif
25 changes: 23 additions & 2 deletions test/encrypt.c
Expand Up @@ -545,6 +545,7 @@ int BuildEnvelopedMessage(const cn_cbor *pControl)
}

if (!COSE_Enveloped_AddRecipient(hEncObj, hRecip, NULL)) {
COSE_Recipient_Free(hRecip);
goto returnError;
}

Expand Down Expand Up @@ -573,15 +574,18 @@ int BuildEnvelopedMessage(const cn_cbor *pControl)

if (!SetSendingAttributes((HCOSE)hCountersign, countersign,
Attributes_Countersign_protected)) {
COSE_CounterSign_Free(hCountersign);
goto returnError;
}

if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) {
COSE_CounterSign_Free(hCountersign);
goto returnError;
}

if (!COSE_Enveloped_add_countersignature(
hEncObj, hCountersign, NULL)) {
COSE_CounterSign_Free(hCountersign);
goto returnError;
}

Expand All @@ -606,6 +610,10 @@ int BuildEnvelopedMessage(const cn_cbor *pControl)
return f;

returnError:
if (hEncObj != NULL) {
COSE_Enveloped_Free(hEncObj);
}

CFails += 1;
return 0;
}
Expand Down Expand Up @@ -748,7 +756,7 @@ int _ValidateEncrypt(const cn_cbor *pControl,
const cn_cbor *pFail;
const cn_cbor *pEncrypt;
const cn_cbor *pRecipients;
HCOSE_ENCRYPT hEnc;
HCOSE_ENCRYPT hEnc = NULL;
int type;
bool fFail = false;
bool fFailBody = false;
Expand Down Expand Up @@ -922,9 +930,11 @@ int _ValidateEncrypt(const cn_cbor *pControl,
}
#endif

COSE_Encrypt_Free(hEnc);

exitHere:
if (hEnc != NULL) {
COSE_Encrypt_Free(hEnc);
}

if (fAlgSupport) {
if (fFailBody) {
Expand All @@ -946,6 +956,10 @@ int _ValidateEncrypt(const cn_cbor *pControl,
return fAlgSupport ? 1 : 0;

returnError:
if (hEnc != NULL) {
COSE_Encrypt_Free(hEnc);
}

CFails += 1;
return 0;
}
Expand Down Expand Up @@ -1043,15 +1057,18 @@ int BuildEncryptMessage(const cn_cbor *pControl)

if (!SetSendingAttributes((HCOSE)hCountersign, countersign,
Attributes_Countersign_protected)) {
COSE_CounterSign_Free(hCountersign);
goto returnError;
}

if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) {
COSE_CounterSign_Free(hCountersign);
goto returnError;
}

if (!COSE_Encrypt0_add_countersignature(
hEncObj, hCountersign, NULL)) {
COSE_CounterSign_Free(hCountersign);
goto returnError;
}

Expand All @@ -1076,6 +1093,10 @@ int BuildEncryptMessage(const cn_cbor *pControl)
return f;

returnError:
if (hEncObj != NULL) {
COSE_Encrypt_Free(hEncObj);
}

CFails += 1;
return 1;
}
Expand Down
29 changes: 25 additions & 4 deletions test/mac_test.c
Expand Up @@ -316,7 +316,8 @@ int ValidateMAC(const cn_cbor *pControl)
int BuildMacMessage(const cn_cbor *pControl)
{
int iRecipient = 0;

HCOSE_RECIPIENT hRecip = NULL;

//
// We don't run this for all control sequences - skip those marked fail.
//
Expand Down Expand Up @@ -361,8 +362,7 @@ int BuildMacMessage(const cn_cbor *pControl)
goto returnError;
}

HCOSE_RECIPIENT hRecip =
COSE_Recipient_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
hRecip = COSE_Recipient_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
if (hRecip == NULL) {
goto returnError;
}
Expand Down Expand Up @@ -431,6 +431,7 @@ int BuildMacMessage(const cn_cbor *pControl)
#endif

COSE_Recipient_Free(hRecip);
hRecip = NULL;
}

#if INCLUDE_COUNTERSIGNATURE
Expand All @@ -455,14 +456,17 @@ int BuildMacMessage(const cn_cbor *pControl)

if (!SetSendingAttributes((HCOSE)hCountersign, countersign,
Attributes_Countersign_protected)) {
COSE_CounterSign_Free(hCountersign);
goto returnError;
}

if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) {
COSE_CounterSign_Free(hCountersign);
goto returnError;
}

if (!COSE_Mac_add_countersignature(hMacObj, hCountersign, NULL)) {
COSE_CounterSign_Free(hCountersign);
goto returnError;
}

Expand All @@ -488,6 +492,12 @@ int BuildMacMessage(const cn_cbor *pControl)
return f;

returnError:
if (hMacObj != NULL) {
COSE_Mac_Free(hMacObj);
}
if (hRecip != NULL) {
COSE_Recipient_Free(hRecip);
}
CFails += 1;
return 1;
}
Expand Down Expand Up @@ -761,7 +771,6 @@ int _ValidateMac0(const cn_cbor *pControl,
}
#endif

COSE_Mac0_Free(hMAC);

if (fFailBody) {
if (!fFail) {
Expand All @@ -771,13 +780,21 @@ int _ValidateMac0(const cn_cbor *pControl,
fFail = false;
}
}

exitHere:
if (hMAC != NULL) {
COSE_Mac0_Free(hMAC);
}

if (fFail) {
CFails += 1;
}
return fUnsuportedAlg ? 0 : 1;

errorReturn:
if (hMAC != NULL) {
COSE_Mac0_Free(hMAC);
}
CFails += 1;
return (fFail || fUnsuportedAlg) ? 0 : 1;
}
Expand Down Expand Up @@ -859,14 +876,17 @@ int BuildMac0Message(const cn_cbor *pControl)

if (!SetSendingAttributes((HCOSE)hCountersign, countersign,
Attributes_Countersign_protected)) {
COSE_CounterSign_Free(hCountersign);
goto returnError;
}

if (!COSE_CounterSign_SetKey(hCountersign, pkeyCountersign, NULL)) {
COSE_CounterSign_Free(hCountersign);
goto returnError;
}

if (!COSE_Mac0_add_countersignature(hMacObj, hCountersign, NULL)) {
COSE_CounterSign_Free(hCountersign);
goto returnError;
}

Expand All @@ -892,6 +912,7 @@ int BuildMac0Message(const cn_cbor *pControl)
return f;

returnError:
COSE_Mac0_Free(hMacObj);
CFails += 1;
return 1;
}
Expand Down

0 comments on commit 140f4fa

Please sign in to comment.