CK_FUNCTION_LIST_PTR funcs;
int ref_count;
int initialize_count;
+ CK_C_INITIALIZE_ARGS init_args;
} Module;
/*
* P11-KIT FUNCTIONALITY
*/
+static CK_RV
+create_mutex (CK_VOID_PTR_PTR mut)
+{
+ pthread_mutex_t *pmutex;
+ int err;
+
+ pmutex = malloc (sizeof (pthread_mutex_t));
+ if (!pmutex)
+ return CKR_HOST_MEMORY;
+ err = pthread_mutex_init (pmutex, NULL);
+ if (err == ENOMEM)
+ return CKR_HOST_MEMORY;
+ else if (err != 0)
+ return CKR_GENERAL_ERROR;
+ *mut = pmutex;
+ return CKR_OK;
+}
+
+static CK_RV
+destroy_mutex (CK_VOID_PTR mut)
+{
+ pthread_mutex_t *pmutex = mut;
+ int err;
+
+ err = pthread_mutex_destroy (pmutex);
+ if (err == EINVAL)
+ return CKR_MUTEX_BAD;
+ else if (err != 0)
+ return CKR_GENERAL_ERROR;
+ free (pmutex);
+ return CKR_OK;
+}
+
+static CK_RV
+lock_mutex (CK_VOID_PTR mut)
+{
+ pthread_mutex_t *pmutex = mut;
+ int err;
+
+ err = pthread_mutex_lock (pmutex);
+ if (err == EINVAL)
+ return CKR_MUTEX_BAD;
+ else if (err != 0)
+ return CKR_GENERAL_ERROR;
+ return CKR_OK;
+}
+
+static CK_RV
+unlock_mutex (CK_VOID_PTR mut)
+{
+ pthread_mutex_t *pmutex = mut;
+ int err;
+
+ err = pthread_mutex_unlock (pmutex);
+ if (err == EINVAL)
+ return CKR_MUTEX_BAD;
+ else if (err == EPERM)
+ return CKR_MUTEX_NOT_LOCKED;
+ else if (err != 0)
+ return CKR_GENERAL_ERROR;
+ return CKR_OK;
+}
+
static void
free_module_unlocked (void *data)
{
free (module);
}
+static Module*
+alloc_module_unlocked (void)
+{
+ Module *module;
+
+ module = calloc (1, sizeof (Module));
+ if (!module)
+ return NULL;
+
+ module->init_args.CreateMutex = create_mutex;
+ module->init_args.DestroyMutex = destroy_mutex;
+ module->init_args.LockMutex = lock_mutex;
+ module->init_args.UnlockMutex = unlock_mutex;
+ module->init_args.flags = CKF_OS_LOCKING_OK;
+
+ return module;
+}
+
static CK_RV
load_module_from_config_unlocked (const char *configfile, const char *name)
{
assert (configfile);
- module = calloc (sizeof (Module), 1);
+ module = calloc (1, sizeof (Module));
if (!module)
return CKR_HOST_MEMORY;
}
static CK_RV
-create_mutex (CK_VOID_PTR_PTR mut)
-{
- pthread_mutex_t *pmutex;
- int err;
-
- pmutex = malloc (sizeof (pthread_mutex_t));
- if (!pmutex)
- return CKR_HOST_MEMORY;
- err = pthread_mutex_init (pmutex, NULL);
- if (err == ENOMEM)
- return CKR_HOST_MEMORY;
- else if (err != 0)
- return CKR_GENERAL_ERROR;
- *mut = pmutex;
- return CKR_OK;
-}
-
-static CK_RV
-destroy_mutex (CK_VOID_PTR mut)
-{
- pthread_mutex_t *pmutex = mut;
- int err;
-
- err = pthread_mutex_destroy (pmutex);
- if (err == EINVAL)
- return CKR_MUTEX_BAD;
- else if (err != 0)
- return CKR_GENERAL_ERROR;
- free (pmutex);
- return CKR_OK;
-}
-
-static CK_RV
-lock_mutex (CK_VOID_PTR mut)
-{
- pthread_mutex_t *pmutex = mut;
- int err;
-
- err = pthread_mutex_lock (pmutex);
- if (err == EINVAL)
- return CKR_MUTEX_BAD;
- else if (err != 0)
- return CKR_GENERAL_ERROR;
- return CKR_OK;
-}
-
-static CK_RV
-unlock_mutex (CK_VOID_PTR mut)
-{
- pthread_mutex_t *pmutex = mut;
- int err;
-
- err = pthread_mutex_unlock (pmutex);
- if (err == EINVAL)
- return CKR_MUTEX_BAD;
- else if (err == EPERM)
- return CKR_MUTEX_NOT_LOCKED;
- else if (err != 0)
- return CKR_GENERAL_ERROR;
- return CKR_OK;
-}
-
-static CK_RV
-initialize_module_unlocked_reentrant (Module *module, CK_C_INITIALIZE_ARGS_PTR args)
+initialize_module_unlocked_reentrant (Module *module)
{
CK_RV rv = CKR_OK;
_p11_unlock ();
assert (module->funcs);
- rv = module->funcs->C_Initialize (args);
+ rv = module->funcs->C_Initialize (&module->init_args);
_p11_lock ();
static void
reinitialize_after_fork (void)
{
- CK_C_INITIALIZE_ARGS args;
hash_iter_t it;
Module *module;
/* WARNING: This function must be reentrant */
- memset (&args, 0, sizeof (args));
- args.CreateMutex = create_mutex;
- args.DestroyMutex = destroy_mutex;
- args.LockMutex = lock_mutex;
- args.UnlockMutex = unlock_mutex;
- args.flags = CKF_OS_LOCKING_OK;
-
_p11_lock ();
if (gl.modules) {
module->initialize_count = 0;
/* WARNING: Reentrancy can occur here */
- initialize_module_unlocked_reentrant (module, &args);
+ initialize_module_unlocked_reentrant (module);
}
}
}
static CK_RV
-finalize_module_unlocked_reentrant (Module *module, CK_VOID_PTR args)
+finalize_module_unlocked_reentrant (Module *module)
{
assert (module);
_p11_unlock ();
assert (module->funcs);
- module->funcs->C_Finalize (args);
+ module->funcs->C_Finalize (NULL);
_p11_lock ();
}
CK_RV
-_p11_kit_initialize_registered_unlocked_reentrant (CK_C_INITIALIZE_ARGS_PTR args)
+_p11_kit_initialize_registered_unlocked_reentrant (void)
{
Module *module;
hash_iter_t it;
if (!module->name)
continue;
- rv = initialize_module_unlocked_reentrant (module, args);
+ rv = initialize_module_unlocked_reentrant (module);
if (rv != CKR_OK)
break;
CK_RV
p11_kit_initialize_registered (void)
{
- CK_C_INITIALIZE_ARGS args;
CK_RV rv;
/* WARNING: This function must be reentrant */
- memset (&args, 0, sizeof (args));
- args.CreateMutex = create_mutex;
- args.DestroyMutex = destroy_mutex;
- args.LockMutex = lock_mutex;
- args.UnlockMutex = unlock_mutex;
- args.flags = CKF_OS_LOCKING_OK;
-
_p11_lock ();
/* WARNING: Reentrancy can occur here */
- rv = _p11_kit_initialize_registered_unlocked_reentrant (&args);
+ rv = _p11_kit_initialize_registered_unlocked_reentrant ();
_p11_unlock ();
}
CK_RV
-_p11_kit_finalize_registered_unlocked_reentrant (CK_VOID_PTR args)
+_p11_kit_finalize_registered_unlocked_reentrant (void)
{
Module *module;
hash_iter_t it;
for (i = 0; i < count; ++i) {
/* WARNING: Reentrant calls can occur here */
- finalize_module_unlocked_reentrant (to_finalize[i], args);
+ finalize_module_unlocked_reentrant (to_finalize[i]);
}
free (to_finalize);
_p11_lock ();
/* WARNING: Reentrant calls can occur here */
- rv = _p11_kit_finalize_registered_unlocked_reentrant (NULL);
+ rv = _p11_kit_finalize_registered_unlocked_reentrant ();
_p11_unlock ();
}
CK_RV
-p11_kit_initialize_module (CK_FUNCTION_LIST_PTR funcs, CK_C_INITIALIZE_ARGS_PTR init_args)
+p11_kit_initialize_module (CK_FUNCTION_LIST_PTR funcs)
{
Module *module;
Module *allocated = NULL;
}
/* WARNING: Reentrancy can occur here */
- rv = initialize_module_unlocked_reentrant (module, init_args);
+ rv = initialize_module_unlocked_reentrant (module);
/* If this was newly allocated, add it to the list */
if (rv == CKR_OK && allocated) {
}
CK_RV
-p11_kit_finalize_module (CK_FUNCTION_LIST_PTR funcs, CK_VOID_PTR reserved)
+p11_kit_finalize_module (CK_FUNCTION_LIST_PTR funcs)
{
Module *module;
CK_RV rv = CKR_OK;
rv = CKR_ARGUMENTS_BAD;
} else {
/* WARNING: Rentrancy can occur here */
- rv = finalize_module_unlocked_reentrant (module, reserved);
+ rv = finalize_module_unlocked_reentrant (module);
}
_p11_unlock ();