From 2dbe536cdf0704c5173aa5904cd641e586652609 Mon Sep 17 00:00:00 2001 From: Per Nilsson Date: Thu, 16 Dec 2021 13:17:23 +0100 Subject: [PATCH] Consolitade p11 init --- examples/p11_generate_rsa.c | 15 ++++++++++++--- pkcs11/tests/common.c | 24 +++++++++++------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/examples/p11_generate_rsa.c b/examples/p11_generate_rsa.c index 344c2739..8d554f4e 100644 --- a/examples/p11_generate_rsa.c +++ b/examples/p11_generate_rsa.c @@ -31,18 +31,27 @@ int main(int argc, char *argv[]) { exit(EXIT_FAILURE); } - CK_C_GetFunctionList fn; void *handle = dlopen(argv[1], RTLD_NOW | RTLD_GLOBAL); assert(handle != NULL); + CK_C_GetFunctionList fn; *(void **) (&fn) = dlsym(handle, "C_GetFunctionList"); assert(fn != NULL); - CK_FUNCTION_LIST_PTR p11; + CK_FUNCTION_LIST_PTR p11 = NULL; CK_RV rv = fn(&p11); assert(rv == CKR_OK); - rv = p11->C_Initialize(NULL_PTR); + char config[256] = {0}; + CK_C_INITIALIZE_ARGS initArgs = {0}; + const char *connector_url = getenv("DEFAULT_CONNECTOR_URL"); + if (connector_url) { + assert(strlen(connector_url) + strlen("connector=") < 256); + sprintf(config, "connector=%s", connector_url); + initArgs.pReserved = (void *) config; + } + + rv = p11->C_Initialize(&initArgs); assert(rv == CKR_OK); CK_SESSION_HANDLE session; diff --git a/pkcs11/tests/common.c b/pkcs11/tests/common.c index c651bebb..b8171710 100644 --- a/pkcs11/tests/common.c +++ b/pkcs11/tests/common.c @@ -42,7 +42,7 @@ CK_FUNCTION_LIST_PTR get_function_list(void *handle) { *(void **) (&fn) = dlsym(handle, "C_GetFunctionList"); assert(fn != NULL); - CK_FUNCTION_LIST_PTR p11; + CK_FUNCTION_LIST_PTR p11 = NULL; CK_RV rv = fn(&p11); assert(rv == CKR_OK); @@ -50,19 +50,17 @@ CK_FUNCTION_LIST_PTR get_function_list(void *handle) { } CK_SESSION_HANDLE open_session(CK_FUNCTION_LIST_PTR p11) { - CK_SESSION_HANDLE session; - CK_C_INITIALIZE_ARGS initArgs; - memset(&initArgs, 0, sizeof(initArgs)); - - const char *connector_url; - connector_url = getenv("DEFAULT_CONNECTOR_URL"); - if (connector_url == NULL) { - connector_url = DEFAULT_CONNECTOR_URL; + CK_SESSION_HANDLE session = 0; + CK_C_INITIALIZE_ARGS initArgs = {0}; + + char config[256] = {0}; + const char *connector_url = getenv("DEFAULT_CONNECTOR_URL"); + if (connector_url) { + assert(strlen(connector_url) + strlen("connector=") < 256); + sprintf(config, "connector=%s", connector_url); + initArgs.pReserved = (void *) config; } - char config[256]; - assert(strlen(connector_url) + strlen("connector=") < 256); - sprintf(config, "connector=%s", connector_url); - initArgs.pReserved = (void *) config; + CK_RV rv = p11->C_Initialize(&initArgs); assert(rv == CKR_OK);