Skip to content

Commit

Permalink
core: add initialization arguments property for pkcs11 provider
Browse files Browse the repository at this point in the history
  • Loading branch information
alonbl committed Sep 27, 2021
1 parent b78d21c commit 133f893
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Expand Up @@ -15,6 +15,7 @@ Copyright (c) 2005-2020 Alon Bar-Lev <alon.barlev@gmail.com>
* core: add pkcs11h_initializeProvider, pkcs11h_registerProvider,
pkcs11h_setProviderProperty, pkcs11h_setProviderPropertyByName to
support adding properties without breaking API thanks to Михалицын Петр.
* core: add initialization arguments property, thanks for Михалицын Петр.

2020-11-17 - Version 1.27

Expand Down
9 changes: 8 additions & 1 deletion include/pkcs11-helper-1.0/pkcs11h-core.h
Expand Up @@ -241,8 +241,15 @@ extern "C" {
*/
#define PKCS11H_PROVIDER_PROPERTY_CERT_IS_PRIVATE 5

/**
* @brief Provider initialize arguments.
* Value type is CK_C_INITIALIZE_ARGS_PTR.
* Default value is NULL.
*/
#define PKCS11H_PROVIDER_PROPERTY_INIT_ARGS 6

/** @private */
#define _PKCS11H_PROVIDER_PROPERTY_LAST 6
#define _PKCS11H_PROVIDER_PROPERTY_LAST 7

/** @} */

Expand Down
1 change: 1 addition & 0 deletions lib/_pkcs11h-core.h
Expand Up @@ -127,6 +127,7 @@ struct _pkcs11h_provider_s {
unsigned mask_decrypt_mode;
unsigned slot_event_method;
unsigned slot_poll_interval;
CK_C_INITIALIZE_ARGS_PTR init_args;

#if defined(ENABLE_PKCS11H_SLOTEVENT)
_pkcs11h_thread_t slotevent_thread;
Expand Down
31 changes: 28 additions & 3 deletions lib/pkcs11h-core.c
Expand Up @@ -135,6 +135,7 @@ static const char * __pkcs11h_provider_preperty_names[] = {
"slot_event_method",
"slot_poll_interval",
"cert_is_private",
"init_args",
NULL
};

Expand Down Expand Up @@ -835,6 +836,9 @@ pkcs11h_setProviderPropertyByName (
*(PKCS11H_BOOL *)value = (PKCS11H_BOOL)(strtol(value_str, 0, 0) != 0 ? 1 : 0);
value_size = sizeof(PKCS11H_BOOL);
break;
case PKCS11H_PROVIDER_PROPERTY_INIT_ARGS:
rv = CKR_ATTRIBUTE_TYPE_INVALID;
goto cleanup;
}

rv = pkcs11h_setProviderProperty (
Expand Down Expand Up @@ -995,6 +999,22 @@ pkcs11h_setProviderProperty (
}
break;

case PKCS11H_PROVIDER_PROPERTY_INIT_ARGS:
{
CK_C_INITIALIZE_ARGS_PTR init_args = *(CK_C_INITIALIZE_ARGS_PTR*) value;
_PKCS11H_ASSERT (sizeof(init_args) <= value_size);

_PKCS11H_DEBUG (
PKCS11H_LOG_DEBUG1,
"PKCS#11: Setting property %s={flags: 0x%08lx}",
__pkcs11h_provider_preperty_names[property],
init_args->flags
);

provider->init_args = init_args;
}
break;

default:
_PKCS11H_DEBUG (
PKCS11H_LOG_ERROR,
Expand Down Expand Up @@ -1097,9 +1117,14 @@ pkcs11h_initializeProvider (
goto cleanup;
}

memset(&init_args, 0, sizeof(init_args));
if ((init_args.pReserved = getenv("PKCS11H_INIT_ARGS_RESERVED")) != NULL) {
pinit_args = &init_args;
if (provider->init_args != NULL) {
pinit_args = provider->init_args;
}
else {
memset(&init_args, 0, sizeof(init_args));
if ((init_args.pReserved = getenv("PKCS11H_INIT_ARGS_RESERVED")) != NULL) {
pinit_args = &init_args;
}
}

if ((rv = provider->f->C_Initialize (pinit_args)) != CKR_OK) {
Expand Down
13 changes: 13 additions & 0 deletions tests/test-basic/test-basic2.c
Expand Up @@ -78,6 +78,19 @@ int main () {
}
}

memset(&init_args, 0, sizeof(init_args));
init_args.flags = CKF_OS_LOCKING_OK;
if (
(rv = pkcs11h_setProviderProperty (
reference,
PKCS11H_PROVIDER_PROPERTY_INIT_ARGS,
&init_args_ptr,
sizeof(init_args_ptr)
)) != CKR_OK
) {
fatal ("pkcs11h_setProviderProperty failed for PKCS11H_PROVIDER_PROPERTY_INIT_ARGS", rv);
}

if ((rv = pkcs11h_initializeProvider (reference)) != CKR_OK) {
fatal ("pkcs11h_initializeProvider failed", rv);
}
Expand Down

0 comments on commit 133f893

Please sign in to comment.