diff --git a/CMakeLists.txt b/CMakeLists.txt index 40a673302..64b1d3af8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,14 +11,16 @@ set(VERSION_PATCH 1) option(BUILD_TESTS "Create Test Application with library" OFF) if(UNIX) -option(SETUP_INSTALLER "Setup installation and packaging as well" ON) + option(SETUP_INSTALLER "Setup installation and packaging as well" ON) + # Default install root which is normally /usr/local/ + set(CMAKE_INSTALL_PREFIX "/" CACHE INTERNAL "") +elseif(MINGW) + option(SETUP_INSTALLER "Setup installation and packaging as well" ON) + set(CMAKE_INSTALL_PREFIX "$ENV{MSYSTEM_PREFIX}" CACHE INTERNAL "") else() -set(SETUP_INSTALLER OFF CACHE INTERNAL "Disabling installation on this platform") + set(SETUP_INSTALLER OFF CACHE INTERNAL "Disabling installation on this platform") endif() -# Default install root which is normally /usr/local/ -set(CMAKE_INSTALL_PREFIX "/" CACHE INTERNAL "") - # If including certificate definitions into the library then include them as ATCACERT_DEF_SRC #file(GLOB ATCACERT_DEF_SRC ABSOLUTE "app/*.c") diff --git a/cmake/check_environment.cmake b/cmake/check_environment.cmake index f0215cc06..9d4d7bac6 100644 --- a/cmake/check_environment.cmake +++ b/cmake/check_environment.cmake @@ -5,46 +5,53 @@ if (TARGET zephyr_interface) SET(ATCA_ZEPHYR_SUPPORT ON CACHE INTERNAL "Build is part of a zephyr project") endif() -if (UNIX AND NOT ATCA_ZEPHYR_SUPPORT) -include(GNUInstallDirs) -include(CheckSymbolExists) - -# Check for gnu extensions -if (NOT DEFINED _GNU_SOURCE) - check_symbol_exists(__GNU_LIBRARY__ "features.h" _GNU_SOURCE) -endif() - -if(_GNU_SOURCE) - set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE) - add_definitions(-D_GNU_SOURCE) -endif() - -# Check and configure packaging options -set(DEFAULT_LIB_PATH "${CMAKE_INSTALL_FULL_LIBDIR}" CACHE - STRING "The default absolute library path") -set(DEFAULT_INC_PATH "${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}" CACHE - STRING "The default include install path") -set(DEFAULT_CONF_PATH "${CMAKE_INSTALL_FULL_SYSCONFDIR}/${PROJECT_NAME}" CACHE - STRING "The default location of ${PROJECT_NAME}.conf") -set(DEFAULT_STORE_PATH "${CMAKE_INSTALL_FULL_LOCALSTATEDIR}/lib/${PROJECT_NAME}" CACHE - STRING "The default location of the filestore directory") -set(DEFAULT_CONF_FILE_NAME "${PROJECT_NAME}.conf" CACHE - STRING "The default file for library configuration") - if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Default build type" FORCE) endif() -# Packaging -set(CPACK_PACKAGE_VENDOR "Microchip Technology Inc") -set(CPACK_PACKAGE_VERSION_MAJOR ${VERSION_MAJOR}) -set(CPACK_PACKAGE_VERSION_MINOR ${VERSION_MINOR}) -set(CPACK_PACKAGE_VERSION_PATCH ${VERSION_PATCH}) -set(CPACK_GENERATOR "TGZ") -set(CPACK_SOURCE_GENERATOR "TGZ") -set(CPACK_SOURCE_IGNORE_FILES "build/*;\\.git/*") - -include(CPack) +if ((UNIX AND NOT ATCA_ZEPHYR_SUPPORT) OR MINGW) + include(GNUInstallDirs) + include(CheckSymbolExists) + + # Check for gnu extensions + if (NOT DEFINED _GNU_SOURCE) + check_symbol_exists(__GNU_LIBRARY__ "features.h" _GNU_SOURCE) + endif() + + if(_GNU_SOURCE) + set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE) + add_definitions(-D_GNU_SOURCE) + endif() + + # Check and configure packaging options + set(DEFAULT_LIB_PATH "${CMAKE_INSTALL_FULL_LIBDIR}" CACHE + STRING "The default absolute library path" FORCE) + set(DEFAULT_INC_PATH "${CMAKE_INSTALL_FULL_INCLUDEDIR}/${PROJECT_NAME}" CACHE + STRING "The default include install path" FORCE) + set(DEFAULT_CONF_PATH "${CMAKE_INSTALL_FULL_SYSCONFDIR}/${PROJECT_NAME}" CACHE + STRING "The default location of ${PROJECT_NAME}.conf" FORCE) + set(DEFAULT_STORE_PATH "${CMAKE_INSTALL_FULL_LOCALSTATEDIR}/lib/${PROJECT_NAME}" CACHE + STRING "The default location of the filestore directory" FORCE) + set(DEFAULT_CONF_FILE_NAME "${PROJECT_NAME}.conf" CACHE + STRING "The default file for library configuration" FORCE) + + message(NOTICE "CMAKE_INSTALL_PREFIX is ${CMAKE_INSTALL_PREFIX}") + message(NOTICE "DEFAULT_LIB_PATH is ${DEFAULT_LIB_PATH}") + message(NOTICE "DEFAULT_INC_PATH is ${DEFAULT_INC_PATH}") + message(NOTICE "DEFAULT_CONF_PATH is ${DEFAULT_CONF_PATH}") + message(NOTICE "DEFAULT_STORE_PATH is ${DEFAULT_STORE_PATH}") + message(NOTICE "DEFAULT_CONF_FILE_NAME is ${DEFAULT_CONF_FILE_NAME}") + + # Packaging + set(CPACK_PACKAGE_VENDOR "Microchip Technology Inc") + set(CPACK_PACKAGE_VERSION_MAJOR ${VERSION_MAJOR}) + set(CPACK_PACKAGE_VERSION_MINOR ${VERSION_MINOR}) + set(CPACK_PACKAGE_VERSION_PATCH ${VERSION_PATCH}) + set(CPACK_GENERATOR "TGZ") + set(CPACK_SOURCE_GENERATOR "TGZ") + set(CPACK_SOURCE_IGNORE_FILES "build/*;\\.git/*") + + include(CPack) endif() diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index eabddbe27..2113cdb3d 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -344,7 +344,9 @@ endif() if(ATCA_PKCS11) include(cmake/pkcs11.cmake) set(CRYPTOAUTH_SRC ${CRYPTOAUTH_SRC} ${PKCS11_SRC}) -set(ATCA_LIBRARY_CONF ${DEFAULT_CONF_PATH}/${DEFAULT_CONF_FILE_NAME} CACHE STRING "" FORCE) +if(NOT WIN32) + set(ATCA_LIBRARY_CONF ${DEFAULT_CONF_PATH}/${DEFAULT_CONF_FILE_NAME} CACHE STRING "" FORCE) +endif(NOT WIN32) endif() if(ATCA_BUILD_SHARED_LIBS) diff --git a/lib/atca_compiler.h b/lib/atca_compiler.h index c746ccb33..4e71db090 100644 --- a/lib/atca_compiler.h +++ b/lib/atca_compiler.h @@ -119,7 +119,7 @@ #define ATCA_UINT64_BE_TO_HOST(x) __builtin_bswap64(x) #endif -#ifdef WIN32 +#if defined(WIN32) && !defined(__MINGW64__) && !defined(__MINGW32__) #define SHARED_LIB_EXPORT __declspec(dllexport) #define SHARED_LIB_IMPORT __declspec(dllimport) #else @@ -208,7 +208,7 @@ #endif #ifdef ATCA_BUILD_SHARED_LIBS -#if defined(cryptoauth_EXPORTS) && defined(_WIN32) +#if defined(cryptoauth_EXPORTS) && defined(_WIN32) && !defined(__MINGW64__) && !defined(__MINGW32__) #define ATCA_DLL SHARED_LIB_EXPORT #else #define ATCA_DLL SHARED_LIB_IMPORT diff --git a/lib/pkcs11/pkcs11.h b/lib/pkcs11/pkcs11.h index 2496e3209..c098bb5e4 100644 --- a/lib/pkcs11/pkcs11.h +++ b/lib/pkcs11/pkcs11.h @@ -181,6 +181,9 @@ extern "C" { * #endif */ +#if defined(_WIN32) || defined(__MINGW32__) || defined(__MINGW64__) +#pragma pack(push, cryptoki, 1) +#endif /* All the various Cryptoki types and #define'd values are in the * file pkcs11t.h. @@ -258,6 +261,10 @@ struct CK_FUNCTION_LIST #undef __PASTE +#if defined(_WIN32) || defined(__MINGW32__) || defined(__MINGW64__) +#pragma pack(pop, cryptoki) +#endif + #ifdef __cplusplus } #endif diff --git a/lib/pkcs11/pkcs11_config.c b/lib/pkcs11/pkcs11_config.c index 708880544..e1bb7f0f4 100644 --- a/lib/pkcs11/pkcs11_config.c +++ b/lib/pkcs11/pkcs11_config.c @@ -35,7 +35,11 @@ #include "pkcs11_os.h" #include "pkcs11_util.h" +#if defined(_WIN32) || defined(__MINGW32__) || defined(__MINGW64__) +#include +#else #include +#endif #if defined(ATCA_TNGTLS_SUPPORT) || defined(ATCA_TNGLORA_SUPPORT) || defined(ATCA_TFLEX_SUPPORT) CK_RV pkcs11_trust_load_objects(pkcs11_slot_ctx_ptr pSlot); @@ -821,6 +825,197 @@ CK_RV pkcs11_config_remove_object(pkcs11_lib_ctx_ptr pLibCtx, pkcs11_slot_ctx_pt } /* Load configuration from the filesystem */ +#if defined(_WIN32) || defined(__MINGW32__) || defined(__MINGW64__) +CK_RV pkcs11_config_load_objects(pkcs11_slot_ctx_ptr slot_ctx) +{ + HANDLE d = NULL; + WIN32_FIND_DATA d_data; + BOOL next_d = 1; + FILE* fp; + char* buffer; + size_t buflen = 0; + char* argv[2 * (PKCS11_MAX_OBJECTS_ALLOWED + PKCS11_MAX_CONFIG_ALLOWED)]; + int argc = 0; + + pkcs11_lib_ctx_ptr pLibCtx = pkcs11_get_context(); + CK_RV rv = CKR_OK; + + /* Open the general library configuration */ + fp = fopen(ATCA_LIBRARY_CONF, "rb"); + if (fp) + { + buflen = pkcs11_config_load_file(fp, &buffer); + fclose(fp); + fp = NULL; + + if (0 < buflen) + { + if (0 < (argc = pkcs11_config_parse_buffer(buffer, buflen, sizeof(argv) / sizeof(argv[0]), argv))) + { + if (strcmp("filestore", argv[0]) == 0) + { + buflen = strlen(argv[1]); + memcpy(pLibCtx->config_path, argv[1], buflen); + + if (pLibCtx->config_path[buflen - 1] != '/') + { + pLibCtx->config_path[buflen++] = '/'; + } + pLibCtx->config_path[buflen] = '\0'; + } + } + else + { + PKCS11_DEBUG("Failed to parse the configuration file: %s\n", ATCA_LIBRARY_CONF); + } + pkcs11_os_free(buffer); + } + } + + PKCS11_DEBUG("Config path --> %s\n", pLibCtx->config_path); + char* query = pkcs11_os_malloc(buflen+1); + snprintf(query, buflen+1, "%s", pLibCtx->config_path); + query[buflen] = '*'; + query[buflen+1] = '\0'; + if (INVALID_HANDLE_VALUE != (d = FindFirstFileA(query, &d_data))) + { + while((CKR_OK == rv) && (0 != next_d)) + { + if(FILE_ATTRIBUTE_NORMAL == d_data.dwFileAttributes || + FILE_ATTRIBUTE_ARCHIVE == d_data.dwFileAttributes) + { + argc = sizeof(argv)/sizeof(argv[0]); + size_t fnlen = strlen((char*)pLibCtx->config_path) + strlen(d_data.cFileName) + 1; + char* filename = pkcs11_os_malloc(fnlen); + + if (!filename) + { + rv = CKR_HOST_MEMORY; + PKCS11_DEBUG("Failed to allocated a filename buffer\n"); + break; + } + snprintf(filename, fnlen, "%s%s", pLibCtx->config_path, d_data.cFileName); + pkcs11_config_split_string(d_data.cFileName, '.', &argc, argv); + + if (!strcmp(argv[argc-1], "conf")) + { + CK_SLOT_ID slot_id = (CK_SLOT_ID)strtol(argv[0], NULL, 10 ); + + PKCS11_DEBUG("Opening Configuration: %s\n", filename); + fp = fopen(filename, "rb"); + pkcs11_os_free(filename); + if (fp) + { + buflen = pkcs11_config_load_file(fp, &buffer); + + if (0 < buflen) + { + if (2 == argc) + { + if (!slot_ctx->label[0]) + { + slot_ctx->slot_id = slot_id; + } + else if (slot_ctx->slot_id == slot_id) + { + PKCS11_DEBUG("Tried to reload the same configuration file for the same slot\n"); + rv = CKR_GENERAL_ERROR; + } + else + { + /* Move to the next context */ + slot_ctx = pkcs11_slot_get_new_context(pLibCtx); + if(slot_ctx) + { + slot_ctx->slot_id = slot_id; + } + else + { + rv = CKR_GENERAL_ERROR; + } + } + + if (CKR_OK == rv) + { + if (0 < (argc = pkcs11_config_parse_buffer(buffer, buflen, sizeof(argv) / sizeof(argv[0]), argv))) + { + rv = pkcs11_config_parse_slot_file(slot_ctx, argc, argv); + } + else + { + rv = CKR_GENERAL_ERROR; + PKCS11_DEBUG("Failed to parse the slot configuration file\n"); + } + } +#ifndef PKCS11_LABEL_IS_SERNUM + if (CKR_OK == rv) + { + /* If a label wasn't set - configure a default */ + if (!slot_ctx->label[0]) + { + snprintf((char*)slot_ctx->label, sizeof(slot_ctx->label) - 1, "%02XABC", (uint8_t)slot_ctx->slot_id); + } + } +#endif + } + else if (3 == argc) + { + uint16_t handle = (uint16_t)strtol(argv[1], NULL, 10); + + if (!slot_ctx->label[0] || (slot_ctx->slot_id != slot_id)) + { + rv = CKR_GENERAL_ERROR; + PKCS11_DEBUG("Trying to load an object configuration without a slot configuration file\n"); + } + + if (CKR_OK == rv) + { + if (0 < (argc = pkcs11_config_parse_buffer(buffer, buflen, sizeof(argv) / sizeof(argv[0]), argv))) + { + rv = pkcs11_config_parse_object_file(slot_ctx, handle, argc, argv); + } + else + { + rv = CKR_GENERAL_ERROR; + PKCS11_DEBUG("Failed to parse the slot configuration file\n"); + } + } + + if (CKR_OK == rv) + { + #if ATCA_CA_SUPPORT + if(atcab_is_ca_device(slot_ctx->interface_config.devtype)) + { + /* Remove the slot from the free list*/ + slot_ctx->flags &= ~(1 << handle); + } + #endif + } + } + pkcs11_os_free(buffer); + } + fclose(fp); + } + else + { + rv = CKR_GENERAL_ERROR; + PKCS11_DEBUG("Unable to open the configuration file\n"); + } + } + } + next_d = FindNextFileA(d, &d_data); + } + } + else + { + PKCS11_DEBUG("Handle returned INVALID_HANDLE_VALUE: %p\n", d); + } + + if(d) + FindClose(d); + return rv; +} +#else CK_RV pkcs11_config_load_objects(pkcs11_slot_ctx_ptr slot_ctx) { DIR * d; @@ -1031,6 +1226,6 @@ CK_RV pkcs11_config_load(pkcs11_slot_ctx_ptr slot_ctx) return rv; } - +#endif /** @} */ diff --git a/test/api_atcab/atca_tests_sha.c b/test/api_atcab/atca_tests_sha.c index 8d77b69fd..d8cf10201 100644 --- a/test/api_atcab/atca_tests_sha.c +++ b/test/api_atcab/atca_tests_sha.c @@ -203,17 +203,7 @@ static void hex_to_uint8(const char hex_str[2], uint8_t* num) TEST_FAIL_MESSAGE("Not a hex digit."); } } -void hex_to_data(const char* hex_str, uint8_t* data, size_t data_size) -{ - size_t i = 0; - - TEST_ASSERT_EQUAL_MESSAGE(data_size * 2, strlen(hex_str) - 1, "Hex string unexpected length."); - for (i = 0; i < data_size; i++) - { - hex_to_uint8(&hex_str[i * 2], &data[i]); - } -} static int read_rsp_hex_value(FILE* file, const char* name, uint8_t* data, size_t data_size) { char line[16384]; diff --git a/test/api_crypto/test_crypto_sha.c b/test/api_crypto/test_crypto_sha.c index 8f3c2530c..a1a746f00 100644 --- a/test/api_crypto/test_crypto_sha.c +++ b/test/api_crypto/test_crypto_sha.c @@ -154,7 +154,7 @@ static void hex_to_uint8(const char hex_str[2], uint8_t* num) } } -static void hex_to_data(const char* hex_str, uint8_t* data, size_t data_size) +void hex_to_data(const char* hex_str, uint8_t* data, size_t data_size) { size_t i = 0; diff --git a/third_party/hidapi/hidapi/hidapi.h b/third_party/hidapi/hidapi/hidapi.h index e5bc2dc40..03d72c4fd 100644 --- a/third_party/hidapi/hidapi/hidapi.h +++ b/third_party/hidapi/hidapi/hidapi.h @@ -29,7 +29,7 @@ #include -#ifdef _WIN32 +#if defined(_WIN32) && !defined(__MINGW64__) && !defined(__MINGW32__) #define HID_API_EXPORT __declspec(dllexport) #define HID_API_CALL #else