Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into build--simplify-d…
Browse files Browse the repository at this point in the history
…epenencies
  • Loading branch information
gocarlos committed Apr 22, 2020
2 parents 348f2d1 + 2a14519 commit 6552445
Show file tree
Hide file tree
Showing 22 changed files with 130 additions and 130 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -67,6 +67,8 @@ Testing
dist
test/test.cbor
*.tcl
CMakeLists.txt.user


# Coverity
cov-int
Expand Down
4 changes: 3 additions & 1 deletion dumper/CMakeLists.txt
Expand Up @@ -6,6 +6,8 @@ if(MSVC)
target_link_libraries(cose_dumper PRIVATE ws2_32)
endif()

target_include_directories(cose_dumper PRIVATE ../src)

if(COSE_C_USE_MBEDTLS)
# mbedtls
target_include_directories(cose_dumper
Expand All @@ -19,4 +21,4 @@ endif()

if(CLANG_TIDY_EXE)
set_target_properties(cose_dumper PROPERTIES C_CLANG_TIDY "${CLANG_TIDY_EXE}")
endif()
endif()
15 changes: 6 additions & 9 deletions dumper/dumper.c
@@ -1,22 +1,19 @@
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <cose/cose.h>
#include <sys/stat.h>
#include <cn-cbor/cn-cbor.h>
#include <cose/cose.h>
#include <cose_int.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/stat.h>

#ifdef _MSC_VER
#include <io.h>
#endif

#ifndef _countof
#define _countof(x) (sizeof(x)/sizeof(x[0]))
#endif

#ifndef _MSC_VER
#define strcat_s(a, b, c) strcat(a, c)
#endif
Expand Down Expand Up @@ -395,7 +392,7 @@ void DumpTree(const cn_cbor * cbor, FILE * out, const FOO *pFOO, int depth, int
if (pFOO != NULL) {
// Locate the right entry in foo
for (i2 = 0, pFoo2 = pFOO->children; i2 < pFOO->count; pFoo2++, i2 += 1) {
if (pFoo2->type != cbor2->type) continue;
if ((unsigned)pFoo2->type != cbor2->type) continue;
switch (cbor2->type) {
case CN_CBOR_UINT:
if ((group != 0) && (pFoo2->group != 0) && (pFoo2->group != group)) continue;
Expand Down
13 changes: 7 additions & 6 deletions include/cose/cose.h
@@ -1,8 +1,9 @@
#pragma once

#include <stdbool.h>
#include <cn-cbor/cn-cbor.h>
#include "cose/cose_configure.h"

#pragma once

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -368,10 +369,10 @@ bool COSE_CounterSign_map_put_int(HCOSE_COUNTERSIGN cose, int key, cn_cbor * val
/*
*/

extern cn_cbor * cn_cbor_clone(const cn_cbor * pIn, CBOR_CONTEXT_COMMA cn_cbor_errback * perr);
extern cn_cbor * cn_cbor_tag_create(int tag, cn_cbor * child, CBOR_CONTEXT_COMMA cn_cbor_errback * perr);
extern cn_cbor * cn_cbor_bool_create(int boolValue, CBOR_CONTEXT_COMMA cn_cbor_errback * errp);
extern cn_cbor * cn_cbor_null_create(CBOR_CONTEXT_COMMA cn_cbor_errback * errp);
cn_cbor * cn_cbor_clone(const cn_cbor * pIn, CBOR_CONTEXT_COMMA cn_cbor_errback * perr);
cn_cbor * cn_cbor_tag_create(int tag, cn_cbor * child, CBOR_CONTEXT_COMMA cn_cbor_errback * perr);
cn_cbor * cn_cbor_bool_create(int boolValue, CBOR_CONTEXT_COMMA cn_cbor_errback * errp);
cn_cbor * cn_cbor_null_create(CBOR_CONTEXT_COMMA cn_cbor_errback * errp);

#ifdef __cplusplus
}
Expand Down
18 changes: 11 additions & 7 deletions src/Cose.c
Expand Up @@ -498,15 +498,19 @@ void _COSE_InsertInList(COSE ** root, COSE * newMsg)
return;
}

bool _COSE_IsInList(COSE * root, COSE * thisMsg)
bool _COSE_IsInList(const COSE *const root, const COSE *const thisMsg)
{
COSE * walk;

if (root == NULL) return false;
if (thisMsg == NULL) return false;
if (root == NULL) {
return false;
}
if (thisMsg == NULL) {
return false;
}

for (walk = root; walk != NULL; walk = walk->m_handleList) {
if (walk == thisMsg) return true;
for (const COSE * walk = root; walk != NULL; walk = walk->m_handleList) {
if (walk == thisMsg) {
return true;
}
}
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Encrypt.c
Expand Up @@ -129,7 +129,7 @@ HCOSE_ENVELOPED _COSE_Enveloped_Init_From_Object(cn_cbor * cbor, COSE_Enveloped
bool COSE_Enveloped_Free(HCOSE_ENVELOPED h)
{
#ifdef USE_CBOR_CONTEXT
cn_cbor_context context;
cn_cbor_context *context;
#endif
COSE_Enveloped * p = (COSE_Enveloped *)h;

Expand All @@ -141,14 +141,14 @@ bool COSE_Enveloped_Free(HCOSE_ENVELOPED h)
}

#ifdef USE_CBOR_CONTEXT
context = ((COSE_Enveloped *)h)->m_message.m_allocContext;
context = &((COSE_Enveloped *)h)->m_message.m_allocContext;
#endif

_COSE_RemoveFromList(&EnvelopedRoot, &p->m_message);

_COSE_Enveloped_Release((COSE_Enveloped *)h);

COSE_FREE((COSE_Enveloped *)h, &context);
COSE_FREE((COSE_Enveloped *)h, context);

return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Encrypt0.c
Expand Up @@ -128,21 +128,21 @@ HCOSE_ENCRYPT _COSE_Encrypt_Init_From_Object(cn_cbor * cbor, COSE_Encrypt * pIn,
bool COSE_Encrypt_Free(HCOSE_ENCRYPT h)
{
#ifdef USE_CBOR_CONTEXT
cn_cbor_context context;
cn_cbor_context *context;
#endif
COSE_Encrypt * pEncrypt = (COSE_Encrypt *)h;

if (!IsValidEncryptHandle(h)) return false;

#ifdef USE_CBOR_CONTEXT
context = ((COSE_Encrypt *)h)->m_message.m_allocContext;
context = &((COSE_Encrypt *)h)->m_message.m_allocContext;
#endif

_COSE_Encrypt_Release(pEncrypt);

_COSE_RemoveFromList(&EncryptRoot, &pEncrypt->m_message);

COSE_FREE((COSE_Encrypt *)h, &context);
COSE_FREE((COSE_Encrypt *)h, context);

return true;
}
Expand Down
8 changes: 4 additions & 4 deletions src/MacMessage.c
Expand Up @@ -113,7 +113,7 @@ HCOSE_MAC _COSE_Mac_Init_From_Object(cn_cbor * cbor, COSE_MacMessage * pIn, CBOR
bool COSE_Mac_Free(HCOSE_MAC h)
{
#ifdef USE_CBOR_CONTEXT
cn_cbor_context context;
cn_cbor_context *context;
#endif
COSE_MacMessage * p = (COSE_MacMessage *)h;

Expand All @@ -127,12 +127,12 @@ bool COSE_Mac_Free(HCOSE_MAC h)
_COSE_RemoveFromList(&MacRoot, &p->m_message);

#ifdef USE_CBOR_CONTEXT
context = ((COSE_MacMessage *)h)->m_message.m_allocContext;
context = &((COSE_MacMessage *)h)->m_message.m_allocContext;
#endif

_COSE_Mac_Release((COSE_MacMessage *)h);

COSE_FREE((COSE_MacMessage *)h, &context);
COSE_FREE((COSE_MacMessage *)h, context);

return true;
}
Expand Down Expand Up @@ -286,7 +286,7 @@ bool _COSE_Mac_Build_AAD(COSE * pCose, const char * szContext, byte ** ppbAuthDa
CHECK_CONDITION(cbAuthData > 0, COSE_ERR_CBOR);
pbAuthData = (byte *)COSE_CALLOC(cbAuthData, 1, context);
CHECK_CONDITION(pbAuthData != NULL, COSE_ERR_OUT_OF_MEMORY);
CHECK_CONDITION(cn_cbor_encoder_write(pbAuthData, 0, cbAuthData, pAuthData) == cbAuthData, COSE_ERR_CBOR);
CHECK_CONDITION(cn_cbor_encoder_write(pbAuthData, 0, cbAuthData, pAuthData) == (ssize_t)cbAuthData, COSE_ERR_CBOR);

*ppbAuthData = pbAuthData;
*pcbAuthData = cbAuthData;
Expand Down
6 changes: 3 additions & 3 deletions src/MacMessage0.c
Expand Up @@ -100,7 +100,7 @@ HCOSE_MAC0 _COSE_Mac0_Init_From_Object(cn_cbor * cbor, COSE_Mac0Message * pIn, C
bool COSE_Mac0_Free(HCOSE_MAC0 h)
{
#ifdef USE_CBOR_CONTEXT
cn_cbor_context context;
cn_cbor_context *context;
#endif
COSE_Mac0Message * p = (COSE_Mac0Message *)h;

Expand All @@ -114,12 +114,12 @@ bool COSE_Mac0_Free(HCOSE_MAC0 h)
_COSE_RemoveFromList(&Mac0Root, &p->m_message);

#ifdef USE_CBOR_CONTEXT
context = p->m_message.m_allocContext;
context = &p->m_message.m_allocContext;
#endif

_COSE_Mac0_Release(p);

COSE_FREE(p, &context);
COSE_FREE(p, context);

return true;
}
Expand Down
12 changes: 5 additions & 7 deletions src/Recipient.c
Expand Up @@ -234,6 +234,7 @@ static bool HKDF_X(COSE * pCose, bool fHMAC, bool fECDH, bool fStatic, bool fSen

bool _COSE_Recipient_decrypt(COSE_RecipientInfo * pRecip, COSE_RecipientInfo * pRecipUse, int algIn, size_t cbitKeyOut, byte * pbKeyOut, cose_errback * perr)
{
UNUSED(pRecipUse);
int alg;
const cn_cbor * cn = NULL;
COSE_RecipientInfo * pRecip2;
Expand All @@ -251,8 +252,6 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo * pRecip, COSE_RecipientInfo * p
int cbitKeyX = 0;
byte rgbKey[256 / 8];

UNUSED(pcose);

#ifdef USE_CBOR_CONTEXT
context = &pcose->m_message.m_allocContext;
#else
Expand All @@ -279,7 +278,7 @@ bool _COSE_Recipient_decrypt(COSE_RecipientInfo * pRecip, COSE_RecipientInfo * p
CHECK_CONDITION(pRecip->m_pkey != NULL, COSE_ERR_INVALID_PARAMETER);
cn = cn_cbor_mapget_int(pRecip->m_pkey, -1);
CHECK_CONDITION((cn != NULL) && (cn->type == CN_CBOR_BYTES), COSE_ERR_INVALID_PARAMETER);
CHECK_CONDITION((cn->length == (unsigned int)cbitKeyOut / 8), COSE_ERR_INVALID_PARAMETER);
CHECK_CONDITION(((size_t)cn->length == cbitKeyOut / 8), COSE_ERR_INVALID_PARAMETER);
memcpy(pbKeyOut, cn->v.bytes, cn->length);

return true;
Expand Down Expand Up @@ -881,7 +880,7 @@ byte * _COSE_RecipientInfo_generateKey(COSE_RecipientInfo * pRecipient, int algI
CHECK_CONDITION(pRecipient->m_pkey != NULL, COSE_ERR_INVALID_PARAMETER);
pK = cn_cbor_mapget_int(pRecipient->m_pkey, -1);
CHECK_CONDITION((pK != NULL) && (pK->type == CN_CBOR_BYTES), COSE_ERR_INVALID_PARAMETER);
CHECK_CONDITION(pK->length == cbitKeySize / 8, COSE_ERR_INVALID_PARAMETER);
CHECK_CONDITION((size_t)pK->length == cbitKeySize / 8, COSE_ERR_INVALID_PARAMETER);
memcpy(pb, pK->v.bytes, cbitKeySize / 8);
break;

Expand Down Expand Up @@ -1225,7 +1224,6 @@ bool BuildContextBytes(COSE * pcose, int algID, size_t cbitKey, byte ** ppbConte
cn_cbor * cnArrayT = NULL;
cn_cbor * cnParam;
byte * pbContext = NULL;
size_t cbContext;

pArray = cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA &cbor_error);
CHECK_CONDITION_CBOR(pArray != NULL, cbor_error);
Expand Down Expand Up @@ -1346,11 +1344,11 @@ bool BuildContextBytes(COSE * pcose, int algID, size_t cbitKey, byte ** ppbConte
cnParam = NULL;
}

cbContext = cn_cbor_encode_size(pArray);
size_t cbContext = cn_cbor_encode_size(pArray);
CHECK_CONDITION(cbContext > 0, COSE_ERR_CBOR);
pbContext = (byte *)COSE_CALLOC(cbContext, 1, context);
CHECK_CONDITION(pbContext != NULL, COSE_ERR_OUT_OF_MEMORY);
CHECK_CONDITION(cn_cbor_encoder_write(pbContext, 0, cbContext, pArray) == cbContext, COSE_ERR_CBOR);
CHECK_CONDITION(cn_cbor_encoder_write(pbContext, 0, cbContext, pArray) == (ssize_t)cbContext, COSE_ERR_CBOR);

*ppbContext = pbContext;
*pcbContext = cbContext;
Expand Down
6 changes: 3 additions & 3 deletions src/Sign.c
Expand Up @@ -107,7 +107,7 @@ HCOSE_SIGN _COSE_Sign_Init_From_Object(cn_cbor * cbor, COSE_SignMessage * pIn, C
bool COSE_Sign_Free(HCOSE_SIGN h)
{
#ifdef USE_CBOR_CONTEXT
cn_cbor_context context;
cn_cbor_context *context;
#endif
COSE_SignMessage * pMessage = (COSE_SignMessage *)h;

Expand All @@ -122,12 +122,12 @@ bool COSE_Sign_Free(HCOSE_SIGN h)
_COSE_RemoveFromList(&SignRoot, &pMessage->m_message);

#ifdef USE_CBOR_CONTEXT
context = pMessage->m_message.m_allocContext;
context = &pMessage->m_message.m_allocContext;
#endif

_COSE_Sign_Release(pMessage);

COSE_FREE(pMessage, &context);
COSE_FREE(pMessage, context);

return true;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Sign1.c
Expand Up @@ -92,7 +92,7 @@ HCOSE_SIGN1 _COSE_Sign1_Init_From_Object(cn_cbor * cbor, COSE_Sign1Message * pIn
bool COSE_Sign1_Free(HCOSE_SIGN1 h)
{
#ifdef USE_CBOR_CONTEXT
cn_cbor_context context;
cn_cbor_context *context;
#endif
COSE_Sign1Message * pMessage = (COSE_Sign1Message *)h;

Expand All @@ -107,12 +107,12 @@ bool COSE_Sign1_Free(HCOSE_SIGN1 h)
_COSE_RemoveFromList(&Sign1Root, &pMessage->m_message);

#ifdef USE_CBOR_CONTEXT
context = pMessage->m_message.m_allocContext;
context = &pMessage->m_message.m_allocContext;
#endif

_COSE_Sign1_Release(pMessage);

COSE_FREE(pMessage, &context);
COSE_FREE(pMessage, context);

return true;
}
Expand Down Expand Up @@ -294,7 +294,7 @@ static bool CreateSign1AAD(COSE_Sign1Message * pMessage, byte ** ppbToSign, size
CHECK_CONDITION(cbToSign > 0, COSE_ERR_CBOR);
pbToSign = (byte *)COSE_CALLOC(cbToSign, 1, context);
CHECK_CONDITION(pbToSign != NULL, COSE_ERR_OUT_OF_MEMORY);
CHECK_CONDITION(cn_cbor_encoder_write(pbToSign, 0, cbToSign, pArray) == cbToSign, COSE_ERR_CBOR);
CHECK_CONDITION(cn_cbor_encoder_write(pbToSign, 0, cbToSign, pArray) == (ssize_t)cbToSign, COSE_ERR_CBOR);

*ppbToSign = pbToSign;
*pcbToSign = cbToSign;
Expand Down
8 changes: 4 additions & 4 deletions src/SignerInfo.c
Expand Up @@ -153,7 +153,9 @@ bool BuildToBeSigned(byte ** ppbToSign, size_t * pcbToSign, const cn_cbor * pcbo
CHECK_CONDITION(cbToSign > 0, COSE_ERR_CBOR);
pbToSign = (byte *)COSE_CALLOC(cbToSign, 1, context);
CHECK_CONDITION(pbToSign != NULL, COSE_ERR_OUT_OF_MEMORY);
CHECK_CONDITION(cn_cbor_encoder_write(pbToSign, 0, cbToSign, pArray) == cbToSign, COSE_ERR_CBOR);
const ssize_t writtenBits = cn_cbor_encoder_write(pbToSign, 0, cbToSign, pArray);
CHECK_CONDITION(writtenBits >= 0, COSE_ERR_CBOR);
CHECK_CONDITION((size_t)writtenBits == cbToSign, COSE_ERR_CBOR);

*ppbToSign = pbToSign;
*pcbToSign = cbToSign;
Expand Down Expand Up @@ -370,15 +372,13 @@ cn_cbor * COSE_Signer_map_get_int(HCOSE_SIGNER h, int key, int flags, cose_errba

bool COSE_Signer_map_put_int(HCOSE_SIGNER h, int key, cn_cbor * value, int flags, cose_errback * perr)
{
bool fRet = false;

CHECK_CONDITION(IsValidSignerHandle(h), COSE_ERR_INVALID_HANDLE);
CHECK_CONDITION(value != NULL, COSE_ERR_INVALID_PARAMETER);

return _COSE_map_put(&((COSE_SignerInfo *)h)->m_message, key, value, flags, perr);

errorReturn:
return fRet;
return false;
}

#endif

0 comments on commit 6552445

Please sign in to comment.