summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStef Walter <stefw@collabora.co.uk>2011-03-31 10:24:08 +0200
committerStef Walter <stefw@collabora.co.uk>2011-03-31 10:24:08 +0200
commit9985957799fd7142125f1d2dd0fae4366ec83f32 (patch)
tree9bd5330ef59460ca4c152749593ab9ec1011a31d
parent1104f03d9b34cc659838124e00ac864c35af4f82 (diff)
Custom initialization and finalization arguments cannot be supported.
When multiple consumers are using a PKCS#11 module, initialization (and finalization) arguments cannot be supported. The first one calling would win out, and the others would get unexpected behavior.
-rw-r--r--module/p11-kit-lib.c193
-rw-r--r--module/p11-kit-private.h4
-rw-r--r--module/p11-kit-proxy.c4
-rw-r--r--module/p11-kit.h6
4 files changed, 104 insertions, 103 deletions
diff --git a/module/p11-kit-lib.c b/module/p11-kit-lib.c
index a868c4a..87bb6cc 100644
--- a/module/p11-kit-lib.c
+++ b/module/p11-kit-lib.c
@@ -63,6 +63,7 @@ typedef struct _Module {
CK_FUNCTION_LIST_PTR funcs;
int ref_count;
int initialize_count;
+ CK_C_INITIALIZE_ARGS init_args;
} Module;
/*
@@ -151,6 +152,69 @@ strequal (const char *one, const char *two)
* 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)
{
@@ -171,6 +235,24 @@ 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)
{
@@ -181,7 +263,7 @@ 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;
@@ -459,70 +541,7 @@ load_registered_modules_unlocked (void)
}
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;
@@ -539,7 +558,7 @@ initialize_module_unlocked_reentrant (Module *module, CK_C_INITIALIZE_ARGS_PTR a
_p11_unlock ();
assert (module->funcs);
- rv = module->funcs->C_Initialize (args);
+ rv = module->funcs->C_Initialize (&module->init_args);
_p11_lock ();
@@ -567,19 +586,11 @@ initialize_module_unlocked_reentrant (Module *module, CK_C_INITIALIZE_ARGS_PTR a
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) {
@@ -588,7 +599,7 @@ reinitialize_after_fork (void)
module->initialize_count = 0;
/* WARNING: Reentrancy can occur here */
- initialize_module_unlocked_reentrant (module, &args);
+ initialize_module_unlocked_reentrant (module);
}
}
@@ -637,7 +648,7 @@ free_modules_when_no_refs_unlocked (void)
}
static CK_RV
-finalize_module_unlocked_reentrant (Module *module, CK_VOID_PTR args)
+finalize_module_unlocked_reentrant (Module *module)
{
assert (module);
@@ -663,7 +674,7 @@ finalize_module_unlocked_reentrant (Module *module, CK_VOID_PTR args)
_p11_unlock ();
assert (module->funcs);
- module->funcs->C_Finalize (args);
+ module->funcs->C_Finalize (NULL);
_p11_lock ();
@@ -694,7 +705,7 @@ find_module_for_name_unlocked (const char *name)
}
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;
@@ -711,7 +722,7 @@ _p11_kit_initialize_registered_unlocked_reentrant (CK_C_INITIALIZE_ARGS_PTR args
if (!module->name)
continue;
- rv = initialize_module_unlocked_reentrant (module, args);
+ rv = initialize_module_unlocked_reentrant (module);
if (rv != CKR_OK)
break;
@@ -724,22 +735,14 @@ _p11_kit_initialize_registered_unlocked_reentrant (CK_C_INITIALIZE_ARGS_PTR args
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 ();
@@ -751,7 +754,7 @@ p11_kit_initialize_registered (void)
}
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;
@@ -778,7 +781,7 @@ _p11_kit_finalize_registered_unlocked_reentrant (CK_VOID_PTR args)
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);
@@ -795,7 +798,7 @@ p11_kit_finalize_registered (void)
_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 ();
@@ -899,7 +902,7 @@ p11_kit_registered_option (CK_FUNCTION_LIST_PTR funcs, const char *field)
}
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;
@@ -919,7 +922,7 @@ p11_kit_initialize_module (CK_FUNCTION_LIST_PTR funcs, CK_C_INITIALIZE_ARGS_PTR
}
/* 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) {
@@ -936,7 +939,7 @@ p11_kit_initialize_module (CK_FUNCTION_LIST_PTR funcs, CK_C_INITIALIZE_ARGS_PTR
}
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;
@@ -950,7 +953,7 @@ p11_kit_finalize_module (CK_FUNCTION_LIST_PTR funcs, CK_VOID_PTR reserved)
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 ();
diff --git a/module/p11-kit-private.h b/module/p11-kit-private.h
index 3760de4..5fbe1eb 100644
--- a/module/p11-kit-private.h
+++ b/module/p11-kit-private.h
@@ -43,9 +43,9 @@ extern pthread_mutex_t _p11_mutex;
CK_FUNCTION_LIST_PTR_PTR _p11_kit_registered_modules_unlocked (void);
-CK_RV _p11_kit_initialize_registered_unlocked_reentrant (CK_C_INITIALIZE_ARGS_PTR args);
+CK_RV _p11_kit_initialize_registered_unlocked_reentrant (void);
-CK_RV _p11_kit_finalize_registered_unlocked_reentrant (CK_VOID_PTR args);
+CK_RV _p11_kit_finalize_registered_unlocked_reentrant (void);
void _p11_kit_proxy_after_fork (void);
diff --git a/module/p11-kit-proxy.c b/module/p11-kit-proxy.c
index 3b43a5f..fd28a19 100644
--- a/module/p11-kit-proxy.c
+++ b/module/p11-kit-proxy.c
@@ -210,7 +210,7 @@ proxy_C_Finalize (CK_VOID_PTR reserved)
_p11_lock ();
/* WARNING: Reentrancy can occur here */
- rv = _p11_kit_finalize_registered_unlocked_reentrant (reserved);
+ rv = _p11_kit_finalize_registered_unlocked_reentrant ();
/*
* If modules are all gone, then this was the last
@@ -305,7 +305,7 @@ proxy_C_Initialize (CK_VOID_PTR init_args)
_p11_lock ();
/* WARNING: Reentrancy can occur here */
- rv = _p11_kit_initialize_registered_unlocked_reentrant (init_args);
+ rv = _p11_kit_initialize_registered_unlocked_reentrant ();
/* WARNING: Reentrancy can occur here */
if (rv == CKR_OK && !gl.mappings_refs == 0)
diff --git a/module/p11-kit.h b/module/p11-kit.h
index 25af193..b035f56 100644
--- a/module/p11-kit.h
+++ b/module/p11-kit.h
@@ -50,11 +50,9 @@ CK_FUNCTION_LIST_PTR p11_kit_registered_name_to_module (const char *name);
char* p11_kit_registered_option (CK_FUNCTION_LIST_PTR funcs,
const char *field);
-CK_RV p11_kit_initialize_module (CK_FUNCTION_LIST_PTR funcs,
- CK_C_INITIALIZE_ARGS_PTR init_args);
+CK_RV p11_kit_initialize_module (CK_FUNCTION_LIST_PTR funcs);
-CK_RV p11_kit_finalize_module (CK_FUNCTION_LIST_PTR funcs,
- CK_VOID_PTR reserved);
+CK_RV p11_kit_finalize_module (CK_FUNCTION_LIST_PTR funcs);
const char* p11_kit_strerror (CK_RV rv);