Get a build system to run on Linux with CMake.
Clean out build errors with gcc, but not yet all of them.
diff --git a/.gitignore b/.gitignore
index 6415927..1c0d7ca 100644
--- a/.gitignore
+++ b/.gitignore
@@ -34,3 +34,10 @@
*~
Debug
Release
+
+# CMAKE FILES
+CMakeFiles
+CMakeCache.txt
+Makefile
+*.cmake
+
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..22c2bd4
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,89 @@
+#
+# top level build file for COSE-C
+#
+
+## prepare CMAKE
+cmake_minimum_required( VERSION 3.0.0)
+
+set (VERSION_MAJOR 0 CACHE STRING "Project major version number")
+set (VERSION_MINOR "1" CACHE STRING "Project minor version number")
+set (VERSION_PATCH "0" CACHE STRING "Project patch version number")
+set (COSE_VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
+mark_as_advanced(VERSION_MAJOR VERSION_MINOR VERSION_PATCH COSE_VERSION)
+
+project ("cose-c" VERSION "${COSE_VERSION}")
+
+find_package(Doxygen)
+
+### setup options
+option (use_context "Use context pointer for COSE functions" ON)
+option (verbose "Produce verbose makefile output" OFF)
+option (optimize "Optimize for size" OFF)
+option (fatal_warnings "Treat build warnings as error" OFF)
+option (coveralls "Generate coveralls data" OFF)
+option (build_docs "Create docs using Doxygen" ${DOXYGEN_FOUND} )
+
+set ( dist_dir ${CMAKE_BINARY_DIR}/dist )
+set ( prefix ${CMAKE_INSTALL_PREFIX} )
+set ( exec_prefix ${CMAKE_INSTALL_PREFIX}/bin )
+set ( libdir ${CMAKE_INSTALL_PREFIX}/lib )
+set ( includedir ${CMAKE_INSTALL_PREFIX}/include )
+
+
+if (NOT CMAKE_BUILD_TYPE)
+ if (optimize)
+ set ( CMAKE_BUILD_TYPE MinSizeRel )
+ set ( coveralls OFF )
+ else ()
+ set ( CMAKE_BUILD_TYPE Debug)
+ endif ()
+endif ()
+
+message ( "Build type: ${CMAKE_BUILD_TYPE}" )
+
+add_definitions( -DUSE_ARRAY )
+if ( CMAKE_C_COMPILER_ID STREQUAL "GNU" OR
+ CMAKE_C_COMPILER_ID MATCHES "Clang")
+ message ( STATUS "adding GCC/Clang options ")
+ add_definitions( -std=gnu99 -Wall -Wextra -pedantic )
+ if ( fatal_warnings )
+ add_definitions( -Werror )
+ endif ()
+ if (optimize)
+ add_definitions( -Os )
+ endif ()
+elseif (MSVC)
+ add_defintions ( /W4 )
+ if (fatal_warnings)
+ add_definitions( /WX )
+ endif ()
+else ()
+ message ( FATAL_ERROR "unhandled compiler id: ${CMAKE_C_COMPILER_ID}" )
+endif ()
+
+if (versbose)
+ set (CMAKE_VERBOSE_MAKEFILE ON)
+endif ()
+
+## include the parts
+add_subdirectory(src)
+
+## try for documentation
+if (build_docs)
+ if (NOT DOXYGEN_FOUND)
+ message(FATAL_ERROR "Doxygen is needed to build the documenation")
+ endif()
+
+ set(doxyfile_in ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in)
+ set(doxyfile ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
+
+ configure_file(${doxyfile_in} ${doxyfile} @ONLY)
+
+ add_custom_target(doc
+ COMMAND ${DOXYGEN_EXECUTABLE} ${doxyfile}
+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
+ COMMENT "Generating API documentation with Doxygen"
+ VERBATIM)
+
+ install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html DESTINATION share/doc)
+endif()
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644
index 0000000..9f0355c
--- /dev/null
+++ b/src/CMakeLists.txt
@@ -0,0 +1,54 @@
+#
+# compiling/installing sources for COSE-C
+#
+
+set ( cose_sources
+ Cose.c
+ MacMessage.c
+ openssl.c
+ Sign.c
+ cbor.c
+ Encrypt.c
+ Message.c
+ Recipient.c
+ SignerInfo.c
+)
+
+if (use_context)
+ add_definitions(-DUSE_CBOR_CONTEXT)
+endif()
+add_library ( cose-c SHARED ${cose_sources} )
+
+target_include_directories ( cose-c PRIVATE ../src )
+target_include_directories ( cose-c PUBLIC ../../cose/cn-cbor/include )
+
+
+install ( TARGETS cose-c
+ LIBRARY DESTINATION lib
+ ARCHIVE DESTINATION lib
+ RUNTIME DESTINATION bin)
+
+set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake)
+if (coveralls)
+ ### include(Coveralls)
+ ## coveralls_turn_on_coverage()
+
+ set(COVERAGE_SRC "")
+ foreach (S ${cose_srcs})
+ get_filename_component(S_ABS ${S} ABSOLUTE)
+ list (APPEND COVERAGE_SRCS ${S_ABS})
+ endforeach()
+
+ # Create the coveralls target.
+ ## coveralls_setup(
+ ## "${COVERAGE_SRCS}"
+ ## ${coveralls_send} # If we should upload
+ ## )
+endif()
+
+add_custom_target(size
+ COMMAND echo "${CMAKE_BINARY_DIR}/src/CMakeFiles/cn-cbor.dir/cn-cbor.c.o"
+ COMMAND size "${CMAKE_BINARY_DIR}/src/CMakeFiles/cn-cbor.dir/cn-cbor.c.o"
+ COMMAND size -m "${CMAKE_BINARY_DIR}/src/CMakeFiles/cn-cbor.dir/cn-cbor.c.o"
+ DEPENDS cn-cbor
+COMMENT "Output the size of the parse routine")
diff --git a/src/Cose.c b/src/Cose.c
index d899fbb..7ad7668 100644
--- a/src/Cose.c
+++ b/src/Cose.c
@@ -40,7 +40,7 @@
pobj->m_cbor = cn_cbor_map_create(CBOR_CONTEXT_PARAM_COMMA &errState);
#endif
if (pobj->m_cbor == NULL) goto error_setup;
- pobj->m_ownMsg = true;
+ pobj->m_ownMsg = 1;
if (msgType > 0) {
cn_cbor * cn = cn_cbor_int_create(msgType, CBOR_CONTEXT_PARAM_COMMA NULL);
@@ -279,7 +279,7 @@
byte RgbDontUse3[1024];
-const cn_cbor * _COSE_encode_protected(COSE * pMessage, cose_errback * perr)
+cn_cbor * _COSE_encode_protected(COSE * pMessage, cose_errback * perr)
{
cn_cbor * pProtected;
int cbProtected;
diff --git a/src/Encrypt.c b/src/Encrypt.c
index 3334f94..51c44d2 100644
--- a/src/Encrypt.c
+++ b/src/Encrypt.c
@@ -115,7 +115,7 @@
void _COSE_Encrypt_Release(COSE_Encrypt * p)
{
- if (p->pbContent != NULL) COSE_FREE(p->pbContent, &p->m_message.m_allocContext);
+ if (p->pbContent != NULL) COSE_FREE((void *) p->pbContent, &p->m_message.m_allocContext);
// if (p->pbIV != NULL) COSE_FREE(p->pbIV, &p->m_message.m_allocContext);
if (p->pbKey != NULL) COSE_FREE(p ->pbKey, &p->m_message.m_allocContext);
@@ -401,7 +401,7 @@
if (cn_Alg == NULL) {
error:
if (perr != NULL) *perr = error;
- errorReturn:
+ //errorReturn:
if (pbAuthData != NULL) COSE_FREE(pbAuthData, context);
if (pAuthData != NULL) cn_cbor_free(pAuthData CBOR_CONTEXT_PARAM);
if (ptmp != NULL) cn_cbor_free(ptmp CBOR_CONTEXT_PARAM);
@@ -428,7 +428,7 @@
case COSE_Algorithm_AES_CCM_16_64_256:
cbitKey = 256;
break;
-#endif INCLUDE_AES_CCM
+#endif // INCLUDE_AES_CCM
case COSE_Algorithm_Direct:
cbitKey = 0;
@@ -550,12 +550,13 @@
void _COSE_Encrypt_SetContent(COSE_Encrypt * cose, const byte * rgb, size_t cb, cose_errback * perror)
{
- cose->pbContent = (byte *)COSE_CALLOC(cb, 1, &cose->m_message.m_allocContext);
+ byte * pb;
+ cose->pbContent = pb= (byte *)COSE_CALLOC(cb, 1, &cose->m_message.m_allocContext);
if (cose->pbContent == NULL) {
if (perror != NULL) perror->err = COSE_ERR_INVALID_PARAMETER;
return;
}
- memcpy(cose->pbContent, rgb, cb);
+ memcpy(pb, rgb, cb);
cose->cbContent = cb;
if (perror != NULL) perror->err = COSE_ERR_NONE;
diff --git a/src/MacMessage.c b/src/MacMessage.c
index bbf11ff..74dcc3a 100644
--- a/src/MacMessage.c
+++ b/src/MacMessage.c
@@ -116,6 +116,7 @@
COSE_MacMessage * pcose = (COSE_MacMessage *)hcose;
cn_cbor * pRecipients = NULL;
cn_cbor * pRecipientsNew = NULL;
+ byte * pbKey = NULL;
if (!IsValidMacHandle(hcose) || (rgbKey == NULL)) {
if (perr != NULL) perr->err = COSE_ERR_INVALID_PARAMETER;
@@ -164,10 +165,10 @@
}
- pobj->m_encrypt.pbKey = (byte *)COSE_CALLOC(cbKey, 1, context);
+ pobj->m_encrypt.pbKey = pbKey = (byte *)COSE_CALLOC(cbKey, 1, context);
CHECK_CONDITION(pobj->m_encrypt.pbKey != NULL, COSE_ERR_OUT_OF_MEMORY);
- memcpy(pobj->m_encrypt.pbKey, rgbKey, cbKey);
+ memcpy(pbKey, rgbKey, cbKey);
pobj->m_encrypt.cbKey = cbKey;
pobj->m_recipientNext = pcose->m_recipientFirst;
@@ -212,9 +213,6 @@
return;
}
- p->pbContent = rgbContent;
- p->cbContent = cbContent;
-
ptmp = cn_cbor_data_create(rgbContent, cbContent, CBOR_CONTEXT_PARAM_COMMA NULL);
CHECK_CONDITION(ptmp != NULL, CN_CBOR_ERR_OUT_OF_MEMORY);
@@ -279,7 +277,7 @@
if (cn_Alg == NULL) {
error:
if (perr != NULL) *perr = error;
- errorReturn:
+ // errorReturn:
if (pbAuthData != NULL) COSE_FREE(pbAuthData, context);
if (pAuthData != NULL) cn_cbor_free(pAuthData CBOR_CONTEXT_PARAM);
if (ptmp != NULL) cn_cbor_free(ptmp CBOR_CONTEXT_PARAM);
@@ -329,7 +327,7 @@
// Build protected headers
- cn_cbor * cbProtected = _COSE_encode_protected(&pcose->m_message, &error);
+ const cn_cbor * cbProtected = _COSE_encode_protected(&pcose->m_message, &error);
if (cbProtected == NULL) goto error;
// Add Unprotected headers
@@ -349,7 +347,7 @@
ssize_t cbAuthData = 0;
pAuthData = cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA NULL);
- ptmp = cn_cbor_data_create(cbProtected->v.str, cbProtected->length, CBOR_CONTEXT_PARAM_COMMA NULL);
+ ptmp = cn_cbor_data_create(cbProtected->v.bytes, cbProtected->length, CBOR_CONTEXT_PARAM_COMMA NULL);
if (ptmp == NULL) goto error;
cn_cbor_array_append(pAuthData, ptmp, NULL);
ptmp = NULL;
diff --git a/src/Sign.c b/src/Sign.c
index 4d4de71..a95f6b8 100644
--- a/src/Sign.c
+++ b/src/Sign.c
@@ -132,12 +132,12 @@
COSE_SignMessage * pMessage = (COSE_SignMessage *)hSign;
COSE_SignerInfo * pSigner = NULL;
const cn_cbor * cbor;
- const cn_cbor * cbor2 = NULL;
+ cn_cbor * cbor2 = NULL;
if (!IsValidSignHandle(hSign) || (pkey == NULL)) {
if (perr != NULL) perr->err = COSE_ERR_INVALID_PARAMETER;
errorReturn:
- if (cbor2 != NULL) CN_CBOR_FREE(cbor2, context);
+ if (cbor2 != NULL) CN_CBOR_FREE((void *) cbor2, context);
if (pSigner != NULL) _COSE_Signer_Free(pSigner);
return NULL;
}
@@ -165,7 +165,7 @@
cbor = cn_cbor_mapget_int(pkey, COSE_Key_ID);
if (cbor != NULL) {
CHECK_CONDITION(cbor->type == CN_CBOR_BYTES, CN_CBOR_ERR_INVALID_PARAMETER);
- cbor2 = cn_cbor_data_create(cbor->v.str, cbor->length, CBOR_CONTEXT_PARAM_COMMA NULL);
+ 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);
cbor2 = NULL;
diff --git a/src/SignerInfo.c b/src/SignerInfo.c
index bffe1ef..2a840f5 100644
--- a/src/SignerInfo.c
+++ b/src/SignerInfo.c
@@ -96,13 +96,13 @@
pcborProtectedSign = _COSE_encode_protected(&pSigner->m_message, perr);
if (pcborProtectedSign == NULL) goto errorReturn;
- pcborBody2 = cn_cbor_data_create(pcborBody->v.str, pcborBody->length, CBOR_CONTEXT_PARAM_COMMA NULL);
+ pcborBody2 = cn_cbor_data_create(pcborBody->v.bytes, pcborBody->length, CBOR_CONTEXT_PARAM_COMMA NULL);
CHECK_CONDITION(pcborBody2 != NULL, COSE_ERR_OUT_OF_MEMORY);
- pcborProtected2 = cn_cbor_data_create(pcborProtected->v.str, pcborProtected->length, CBOR_CONTEXT_PARAM_COMMA NULL);
+ pcborProtected2 = cn_cbor_data_create(pcborProtected->v.bytes, pcborProtected->length, CBOR_CONTEXT_PARAM_COMMA NULL);
CHECK_CONDITION(pcborProtected2 != NULL, COSE_ERR_OUT_OF_MEMORY);
- pcborProtectedSign2 = cn_cbor_data_create(pcborProtectedSign->v.str, pcborProtectedSign->length, CBOR_CONTEXT_PARAM_COMMA NULL);
+ pcborProtectedSign2 = cn_cbor_data_create(pcborProtectedSign->v.bytes, pcborProtectedSign->length, CBOR_CONTEXT_PARAM_COMMA NULL);
CHECK_CONDITION(pcborProtectedSign2 != NULL, COSE_ERR_OUT_OF_MEMORY);
CHECK_CONDITION(cn_cbor_array_append(pArray, pcborProtected2, NULL), COSE_ERR_CBOR);
diff --git a/src/cbor.c b/src/cbor.c
index c4c7e7e..a94bc84 100644
--- a/src/cbor.c
+++ b/src/cbor.c
@@ -1,4 +1,6 @@
-#include "cn-cbor\cn-cbor.h"
+#include "cn-cbor/cn-cbor.h"
+#include <stdlib.h>
+
#define INIT_CB(v) \
if (errp) {errp->err = CN_CBOR_NO_ERROR;} \
(v) = CN_CALLOC_CONTEXT(); \
diff --git a/src/cose.h b/src/cose.h
index 82e4bf6..35869ff 100644
--- a/src/cose.h
+++ b/src/cose.h
@@ -1,4 +1,4 @@
-#include <cn-cbor\cn-cbor.h>
+#include <cn-cbor/cn-cbor.h>
#include "configure.h"
typedef unsigned char byte;
diff --git a/src/cose_int.h b/src/cose_int.h
index 0f8a0b7..7eb5ada 100644
--- a/src/cose_int.h
+++ b/src/cose_int.h
@@ -4,10 +4,10 @@
// interface, and they were quite confusing in cn-cbor.h
typedef struct {
- int m_flags; // Not sure what goes here yet
- int m_ownMsg : 1; // Do I own the pointer @ m_cbor?
- int m_ownUnprotectedMap : 1; // Do I own the pointer @ m_unportectedMap?
- int m_msgType : 4; // What message type is this?
+ int m_flags; // Not sure what goes here yet
+ int m_ownMsg; // Do I own the pointer @ m_cbor?
+ int m_ownUnprotectedMap; // Do I own the pointer @ m_unportectedMap?
+ int m_msgType; // What message type is this?
cn_cbor * m_cbor;
cn_cbor * m_protectedMap;
cn_cbor * m_unprotectMap;
@@ -25,13 +25,13 @@
COSE_SignerInfo * m_signerFirst;
} COSE_SignMessage;
-typedef struct _SignerInfo {
+struct _SignerInfo {
COSE m_message;
byte * pbKey;
size_t cbKey;
const cn_cbor * m_pkey;
COSE_SignerInfo * m_signerNext;
-} COSE_SignerInfo;
+};
struct _RecipientInfo;
typedef struct _RecipientInfo COSE_RecipientInfo;
@@ -39,22 +39,20 @@
typedef struct {
COSE m_message; // The message object
COSE_RecipientInfo * m_recipientFirst;
- byte * pbContent;
+ const byte * pbContent;
size_t cbContent;
byte * pbKey;
size_t cbKey;
} COSE_Encrypt;
-typedef struct _RecipientInfo {
+struct _RecipientInfo {
COSE_Encrypt m_encrypt;
COSE_RecipientInfo * m_recipientNext;
-} COSE_RecipientInfo;
+};
typedef struct {
COSE m_message; // The message object
COSE_RecipientInfo * m_recipientFirst;
- byte * pbContent;
- size_t cbContent;
byte * pbKey;
size_t cbKey;
} COSE_MacMessage;
@@ -87,7 +85,7 @@
* @param free_func [description]
* @return [description]
*/
-#define COSE_FREE(ptr, ctx) (((ctx) && (ctx)->free_func) ? \
+#define COSE_FREE(ptr, ctx) (((ctx)->free_func) ? \
((ctx)->free_func((ptr), (ctx)->context)) : \
free((ptr)))
@@ -111,7 +109,7 @@
#define UNUSED_PARAM(p) ((void)&(p))
#endif
-extern const cn_cbor * _COSE_encode_protected(COSE * pMessage, cose_errback * perr);
+extern cn_cbor * _COSE_encode_protected(COSE * pMessage, cose_errback * perr);
extern bool IsValidEncryptHandle(HCOSE_ENCRYPT h);
diff --git a/src/openssl.c b/src/openssl.c
index 1eef824..548110a 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -4,6 +4,7 @@
#include "crypto.h"
#include <assert.h>
+#include <memory.h>
#ifdef USE_OPEN_SSL
@@ -68,7 +69,7 @@
CHECK_CONDITION(EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_CCM_SET_L, (LSize/8), 0), COSE_ERR_DECRYPT_FAILED);
CHECK_CONDITION(EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_CCM_SET_IVLEN, NSize, 0), COSE_ERR_DECRYPT_FAILED);
- CHECK_CONDITION(EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_CCM_SET_TAG, TSize, &pcose->pbContent[pcose->cbContent - TSize]), COSE_ERR_DECRYPT_FAILED);
+ CHECK_CONDITION(EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_CCM_SET_TAG, TSize, (void *) &pcose->pbContent[pcose->cbContent - TSize]), COSE_ERR_DECRYPT_FAILED);
CHECK_CONDITION(EVP_DecryptInit(&ctx, 0, pbKey, rgbIV), COSE_ERR_DECRYPT_FAILED);
@@ -153,7 +154,7 @@
bool HMAC_Create(COSE_Encrypt * pcose, int HSize, int TSize, const byte * pbAuthData, int cbAuthData, cose_errback * perr)
{
HMAC_CTX ctx;
- EVP_MD * pmd = NULL;
+ const EVP_MD * pmd = NULL;
byte * rgbOut = NULL;
unsigned int cbOut;
#ifdef USE_CBOR_CONTEXT
@@ -194,7 +195,7 @@
bool HMAC_Validate(COSE_Encrypt * pcose, int HSize, int TSize, const byte * pbAuthData, int cbAuthData, cose_errback * perr)
{
HMAC_CTX ctx;
- EVP_MD * pmd = NULL;
+ const EVP_MD * pmd = NULL;
byte * rgbOut = NULL;
unsigned int cbOut;
bool f = false;
@@ -242,7 +243,7 @@
#define COSE_Key_EC_Y -3
#define COSE_Key_EC_d -4
-EC_KEY * ECKey_From(const cn_cbor * pKey, cose_errback * perr)
+EC_KEY * ECKey_From(const cn_cbor * pKey, cose_errback * /*perr*/)
{
EC_KEY * pNewKey = EC_KEY_new();
byte rgbKey[512+1];
@@ -291,7 +292,7 @@
if (p != NULL) {
BIGNUM * pbn;
- pbn = BN_bin2bn(p->v.str, p->length, NULL);
+ pbn = BN_bin2bn(p->v.bytes, p->length, NULL);
EC_KEY_set_private_key(pNewKey, pbn);
}
@@ -317,10 +318,9 @@
{
EC_KEY * eckey = NULL;
byte rgbDigest[EVP_MAX_MD_SIZE];
- size_t cbDigest = sizeof(rgbDigest);
- ECDSA_SIG * sig;
+ uint cbDigest = sizeof(rgbDigest);
byte * pbSig = NULL;
- size_t cbSig;
+ uint cbSig;
#ifdef USE_CBOR_CONTEXT
cn_cbor_context * context = &pSigner->m_message.m_allocContext;
#endif