Skip to content

Commit

Permalink
Merge pull request #10 from jimsch/master
Browse files Browse the repository at this point in the history
Add more build options
  • Loading branch information
jimsch committed Dec 30, 2015
2 parents 62149a0 + 45e5e1f commit bc8080b
Show file tree
Hide file tree
Showing 18 changed files with 309 additions and 60 deletions.
9 changes: 8 additions & 1 deletion .travis.yml
Expand Up @@ -6,6 +6,13 @@ sudo: false
before_install:
- pip install cpp-coveralls --user
- export LD_LIBRARY_PATH=$PWD/build/dist/lib:$LD_LIBRARY_PATH
env:
- USE_CONTEXT=ON
- USE_CONTEXT=OFF
matrix:
exclude:
- compiler: gcc
env: USE_CONTEXT=OFF
addons:
apt:
sources:
Expand All @@ -16,7 +23,7 @@ addons:
- cmake-data
script:
- mkdir build
- cd build && cmake -D coveralls_send=ON .. && make all test
- cd build && cmake -Duse_context=$USE_CONTEXT -Dcoveralls_send=ON .. && make all test
- make coveralls


2 changes: 1 addition & 1 deletion CMakeLists.txt
Expand Up @@ -105,7 +105,7 @@ ExternalProject_Add(
project_cn-cbor
GIT_REPOSITORY https://github.com/jimsch/cn-cbor
GIT_TAG PrettyPrint
CMAKE_ARGS -Doptimize=OFF -Duse_context=ON -Dbuild_docs=OFF -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> -Dcoveralls=${COVERALLS} -Dbuild_shared_libs=$(build_shared_libs)
CMAKE_ARGS -Doptimize=OFF -Duse_context=${use_context} -Dbuild_docs=OFF -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> -Dcoveralls=OFF -Dbuild_shared_libs=${build_shared_libs} -Dfatal_warnings=OFF
INSTALL_DIR "${dist_dir}"
UPDATE_DISCONNECTED 1
)
Expand Down
26 changes: 0 additions & 26 deletions dump/dump.c

This file was deleted.

10 changes: 8 additions & 2 deletions dumper/dumper.c
Expand Up @@ -29,6 +29,12 @@ typedef struct _FOO {
FOO Recipients;
extern FOO Signer[];

#ifdef USE_CBOR_CONTEXT
#define CBOR_CONTEXT_PARAM , NULL
#else
#define CBOR_CONTEXT_PARAM
#endif

FOO AlgorithmMap[32] = {
{ "ES512", CN_CBOR_INT, -9, NULL, 0, 0 },
{ "ES384", CN_CBOR_INT, -8, NULL, 0, 0 },
Expand Down Expand Up @@ -384,7 +390,7 @@ void DumpTree(const cn_cbor * cbor, FILE * out, const FOO *pFOO, int depth, int
case CN_CBOR_BYTES:
DumpBytes(out, cbor, depth);
if ((pFOO != NULL) && (pFOO->children != NULL)) {
const cn_cbor * cbor3 = cn_cbor_decode(cbor->v.bytes, cbor->length, NULL, NULL);
const cn_cbor * cbor3 = cn_cbor_decode(cbor->v.bytes, cbor->length CBOR_CONTEXT_PARAM, NULL);
if (cbor3 != NULL) {
WrapPrintF(out, fInComment ? " \\ " : " / ");
DumpTree(cbor3, out, pFOO->children, depth+1, true, true, true);
Expand Down Expand Up @@ -542,7 +548,7 @@ int main(int argc, char ** argv)

// Parse it

cn_cbor * cbor = cn_cbor_decode(pb, cb, NULL, NULL);
cn_cbor * cbor = cn_cbor_decode(pb, cb CBOR_CONTEXT_PARAM, NULL);
if (cbor == NULL) {
fprintf(stderr, "Error parsing CBOR");
exit(1);
Expand Down
8 changes: 6 additions & 2 deletions src/Cose.c
Expand Up @@ -88,7 +88,7 @@ bool _COSE_Init_From_Object(COSE* pobj, cn_cbor * pcbor, CBOR_CONTEXT_COMMA cose
CHECK_CONDITION(pobj->m_protectedMap, COSE_ERR_OUT_OF_MEMORY);
}
else {
pobj->m_protectedMap = cn_cbor_decode((const byte *)pmap->v.str, pmap->length, context, &errState);
pobj->m_protectedMap = cn_cbor_decode((const byte *)pmap->v.str, pmap->length, CBOR_CONTEXT_PARAM_COMMA &errState);
CHECK_CONDITION(pobj->m_protectedMap != NULL, COSE_ERR_INVALID_PARAMETER);
}
}
Expand All @@ -108,7 +108,9 @@ bool _COSE_Init_From_Object(COSE* pobj, cn_cbor * pcbor, CBOR_CONTEXT_COMMA cose

void _COSE_Release(COSE * pobj)
{
#ifdef USE_CBOR_CONTEXT
cn_cbor_context * context = &pobj->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);
Expand Down Expand Up @@ -243,7 +245,9 @@ cn_cbor * _COSE_map_get_str(COSE * pcose, const char * key, int flags, cose_errb

bool _COSE_map_put(COSE * pCose, int key, cn_cbor * value, int flags, cose_errback * perr)
{
#ifdef USE_CBOR_CONTEXT
cn_cbor_context * context = &pCose->m_allocContext;
#endif
cn_cbor_errback error;
bool f;

Expand Down Expand Up @@ -277,7 +281,7 @@ bool _COSE_map_put(COSE * pCose, int key, cn_cbor * value, int flags, cose_errba
break;
}

CHECK_CONDITION(f, error.err);
CHECK_CONDITION(f, _MapFromCBOR(error));

return f;
}
Expand Down
10 changes: 7 additions & 3 deletions src/Encrypt.c
Expand Up @@ -193,12 +193,12 @@ HCOSE_RECIPIENT COSE_Encrypt_add_shared_secret(HCOSE_ENCRYPT hcose, COSE_Algorit
if (pRecipients == NULL) goto error;
#ifdef USE_ARRAY
if (!_COSE_array_replace(&pcose->m_message, pRecipients, INDEX_RECIPIENTS, CBOR_CONTEXT_PARAM_COMMA NULL)) {
cn_cbor_free(pRecipients, context);
CN_CBOR_FREE(pRecipients, context);
goto error;
}
#else
if (!cn_cbor_mapput_int(pcose->m_message.m_cbor, COSE_Header_Recipients, pRecipients, CBOR_CONTEXT_PARAM_COMMA NULL)) {
cn_cbor_free(pRecipients, context);
CN_CBOR_FREE(pRecipients, context);
goto error;
}
#endif
Expand Down Expand Up @@ -233,7 +233,9 @@ bool _COSE_Encrypt_decrypt(COSE_Encrypt * pcose, COSE_RecipientInfo * pRecip, in
const cn_cbor * cn = NULL;

byte * pbKey = pbKeyIn;
#ifdef USE_CBOR_CONTEXT
cn_cbor_context * context;
#endif
byte * pbAuthData = NULL;
ssize_t cbAuthData;
cn_cbor * pAuthData = NULL;
Expand All @@ -258,7 +260,7 @@ bool _COSE_Encrypt_decrypt(COSE_Encrypt * pcose, COSE_RecipientInfo * pRecip, in
}
return false;
}
CHECK_CONDITION((cn->type == CN_CBOR_UINT) || (cn->type == CN_CBOR_INT), CN_CBOR_ERR_INVALID_PARAMETER);
CHECK_CONDITION((cn->type == CN_CBOR_UINT) || (cn->type == CN_CBOR_INT), COSE_ERR_INVALID_PARAMETER);
alg = (int) cn->v.uint;

switch (alg) {
Expand Down Expand Up @@ -384,7 +386,9 @@ bool COSE_Encrypt_encrypt(HCOSE_ENCRYPT h, cose_errback * perr)
cn_cbor * pAuthData = NULL;
cn_cbor * ptmp = NULL;
size_t cbitKey;
#ifdef USE_CBOR_CONTEXT
cn_cbor_context * context = NULL;
#endif
COSE_Encrypt * pcose = (COSE_Encrypt *) h;
cn_cbor_errback cbor_error;

Expand Down
6 changes: 6 additions & 0 deletions src/MacMessage.c
Expand Up @@ -202,7 +202,9 @@ HCOSE_RECIPIENT COSE_Mac_add_shared_secret(HCOSE_MAC hcose, COSE_Algorithms alg,
bool COSE_Mac_SetContent(HCOSE_MAC cose, const byte * rgbContent, size_t cbContent, cose_errback * perr)
{
COSE_MacMessage * p = (COSE_MacMessage *)cose;
#ifdef USE_CBOR_CONTEXT
cn_cbor_context * context = &p->m_message.m_allocContext;
#endif
cn_cbor * ptmp = NULL;
cn_cbor_errback cbor_error;

Expand Down Expand Up @@ -260,7 +262,9 @@ bool COSE_Mac_encrypt(HCOSE_MAC h, cose_errback * perr)
cn_cbor * pAuthData = NULL;
cn_cbor * ptmp = NULL;
size_t cbitKey;
#ifdef USE_CBOR_CONTEXT
cn_cbor_context * context = NULL;
#endif
COSE_MacMessage * pcose = (COSE_MacMessage *)h;
cn_cbor_errback cbor_error;

Expand Down Expand Up @@ -407,7 +411,9 @@ bool COSE_Mac_validate(HCOSE_MAC h, HCOSE_RECIPIENT hRecip, cose_errback * perr)
const cn_cbor * cn = NULL;

byte * pbKey = pbKeyIn;
#ifdef USE_CBOR_CONTEXT
cn_cbor_context * context = NULL;
#endif
ssize_t cbAuthData;
cn_cbor * pAuthData = NULL;
cn_cbor * ptmp = NULL;
Expand Down
16 changes: 15 additions & 1 deletion src/Recipient.c
Expand Up @@ -119,4 +119,18 @@ bool COSE_Recipient_SetKey(HCOSE_RECIPIENT h, const byte * pbKey, int cbKey, cos
p->m_encrypt.cbKey = cbKey;

return true;
}
}

bool COSE_Recipient_SetKey_EC(HCOSE_RECIPIENT h, const char* szCurve, const byte * pbX, int cbX, const byte * pbY, int cbY, cose_errback * perror)
{
COSE_RecipientInfo * p;

if (!IsValidRecipientHandle(h) || (szCurve == NULL) || (pbX == NULL) || (pbY == NULL)) {
if (perror != NULL) perror->err = COSE_ERR_INVALID_PARAMETER;
return false;
}

p = (COSE_RecipientInfo *)h;

return true;
}
8 changes: 4 additions & 4 deletions src/Sign.c
Expand Up @@ -113,7 +113,7 @@ bool COSE_Sign_SetContent(HCOSE_SIGN h, const byte * rgb, size_t cb, cose_errbac
#endif

p = cn_cbor_data_create(rgb, cb, CBOR_CONTEXT_PARAM_COMMA NULL);
CHECK_CONDITION(p != NULL, CN_CBOR_ERR_OUT_OF_DATA);
CHECK_CONDITION(p != NULL, COSE_ERR_OUT_OF_MEMORY);

#ifdef USE_ARRAY
CHECK_CONDITION(_COSE_array_replace(&pMessage->m_message, p, INDEX_BODY, CBOR_CONTEXT_PARAM_COMMA NULL), COSE_ERR_OUT_OF_MEMORY);
Expand Down Expand Up @@ -164,7 +164,7 @@ HCOSE_SIGNER COSE_Sign_add_signer(HCOSE_SIGN hSign, const cn_cbor * pkey, int al

cbor = cn_cbor_mapget_int(pkey, COSE_Key_ID);
if (cbor != NULL) {
CHECK_CONDITION(cbor->type == CN_CBOR_BYTES, CN_CBOR_ERR_INVALID_PARAMETER);
CHECK_CONDITION(cbor->type == CN_CBOR_BYTES, COSE_ERR_INVALID_PARAMETER);
cbor2 = cn_cbor_data_create(cbor->v.bytes, cbor->length, CBOR_CONTEXT_PARAM_COMMA NULL);
CHECK_CONDITION(cbor2 != NULL, COSE_ERR_CBOR);
CHECK_CONDITION(cn_cbor_mapput_int(pSigner->m_message.m_unprotectMap, COSE_Parameter_KID, cbor2, CBOR_CONTEXT_PARAM_COMMA NULL), COSE_ERR_CBOR);
Expand All @@ -186,12 +186,12 @@ HCOSE_SIGNER COSE_Sign_add_signer(HCOSE_SIGN hSign, const cn_cbor * pkey, int al
CHECK_CONDITION(pSigners != NULL, COSE_ERR_OUT_OF_MEMORY);
#ifdef USE_ARRAY
if (!_COSE_array_replace(&pMessage->m_message, pSigners, INDEX_SIGNATURES, CBOR_CONTEXT_PARAM_COMMA NULL)) {
cn_cbor_free(pSigners, context);
CN_CBOR_FREE(pSigners, context);
CHECK_CONDITION(false, COSE_ERR_CBOR);
}
#else
if (!cn_cbor_mapput_int(pMessage->m_message.m_cbor, COSE_Header_Signers, pSigners, CBOR_CONTEXT_PARAM_COMMA NULL)) {
cn_cbor_free(pSigners, context);
CN_CBOR_FREE(pSigners, context);
CHECK_CONDITION(false, COSE_ERR_CBOR);
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/SignerInfo.c
Expand Up @@ -82,7 +82,7 @@ bool _COSE_Signer_sign(COSE_SignerInfo * pSigner, const cn_cbor * pcborBody, con
byte * pbToSign = NULL;
bool f;

pArray = cn_cbor_array_create(context, NULL);
pArray = cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA NULL);
if (pArray == NULL) {
if (perr != NULL) perr->err = COSE_ERR_OUT_OF_MEMORY;
errorReturn:
Expand Down
6 changes: 6 additions & 0 deletions src/cbor.c
Expand Up @@ -6,13 +6,19 @@
(v) = CN_CALLOC_CONTEXT(); \
if (!(v)) { if (errp) {errp->err = CN_CBOR_ERR_OUT_OF_MEMORY;} return false; }

#ifdef USE_CBOR_CONTEXT
#define CBOR_CONTEXT_PARAM , context

#define CN_CALLOC(ctx) ((ctx) && (ctx)->calloc_func) ? \
(ctx)->calloc_func(1, sizeof(cn_cbor), (ctx)->context) : \
calloc(1, sizeof(cn_cbor));

#define CN_CALLOC_CONTEXT() CN_CALLOC(context)
#else
#define CBOR_CONTEXT_PARAM
#define CN_CALLOC(ctx) calloc(1, sizeof(cn_cbor));
#define CN_CALLOC_CONTEXT() CN_CALLOC(context)
#endif


/***
Expand Down
1 change: 1 addition & 0 deletions src/cose.h
Expand Up @@ -122,6 +122,7 @@ HCOSE_RECIPIENT COSE_Encrypt_add_shared_secret(HCOSE_ENCRYPT cose, COSE_Algorith

HCOSE_RECIPIENT COSE_Encrypt_GetRecipient(HCOSE_ENCRYPT cose, int iRecipient, cose_errback * perr);
bool COSE_Recipient_SetKey(HCOSE_RECIPIENT h, const byte * rgb, int cb, cose_errback * perr);
bool COSE_Recipient_SetKey_EC(HCOSE_RECIPIENT h, const char* szCurve, const byte * pbX, int cbX, const byte * pbY, int cbY, cose_errback * perror);

//
//
Expand Down
5 changes: 4 additions & 1 deletion src/cose_int.h
Expand Up @@ -97,10 +97,13 @@ typedef struct {
#else

#define CBOR_CONTEXT_PARAM
#define CBOR_CONTEXT_PARAM_COMMA
#define CN_CALLOC_CONTEXT() CN_CALLOC
#define COSE_CALLOC(ctx, count, size) calloc(count, size)
#define COSE_CALLOC(count, size, ctx) calloc(count, size)
#define CN_CBOR_FREE(p, context) cn_cbor_free(p)



#define COSE_FREE(ptr, ctx) free(ptr)

#endif // USE_CBOR_CONTEXT
Expand Down
2 changes: 1 addition & 1 deletion src/openssl.c
Expand Up @@ -78,7 +78,7 @@ bool AES_CCM_Decrypt(COSE_Encrypt * pcose, int TSize, int LSize, const byte * pb

cbOut = (int) pcose->cbContent - TSize;
rgbOut = (byte *)COSE_CALLOC(cbOut, 1, context);
CHECK_CONDITION(rgbOut != NULL, CN_CBOR_ERR_OUT_OF_MEMORY);
CHECK_CONDITION(rgbOut != NULL, COSE_ERR_OUT_OF_MEMORY);

CHECK_CONDITION(EVP_DecryptUpdate(&ctx, NULL, &outl, pbAuthData, cbAuthData), COSE_ERR_DECRYPT_FAILED);

Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Expand Up @@ -8,7 +8,7 @@ endif()

set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${dist_dir}/test )

add_executable ( cose_test test.c )
add_executable ( cose_test test.c json.c )

target_link_libraries (cose_test PRIVATE cose-c )

Expand Down

0 comments on commit bc8080b

Please sign in to comment.