Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixes to support Cryptoauthlib in updated MinGW64/32 environments #329

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
79 changes: 43 additions & 36 deletions cmake/check_environment.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 3 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/atca_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions lib/pkcs11/pkcs11.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
197 changes: 196 additions & 1 deletion lib/pkcs11/pkcs11_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
#include "pkcs11_os.h"
#include "pkcs11_util.h"

#if defined(_WIN32) || defined(__MINGW32__) || defined(__MINGW64__)
#include <Windows.h>
#else
#include <dirent.h>
#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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1031,6 +1226,6 @@ CK_RV pkcs11_config_load(pkcs11_slot_ctx_ptr slot_ctx)

return rv;
}

#endif

/** @} */
10 changes: 0 additions & 10 deletions test/api_atcab/atca_tests_sha.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Loading