pkcs11General.c revision 9e1a718fa89856ea8192b1cc4fbd6c0a6df7fea4
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <unistd.h>
#include <string.h>
#include <cryptoutil.h>
#include <pthread.h>
#include <security/cryptoki.h>
#include "pkcs11Global.h"
#include "pkcs11Slot.h"
#include "pkcs11Conf.h"
#include "pkcs11Session.h"
#pragma fini(pkcs11_fini)
static struct CK_FUNCTION_LIST functionList = {
{ 2, 11 }, /* version */
};
static pid_t pkcs11_pid = 0;
/* protects pkcs11_[initialized|pid], and fastpath */
static void pkcs11_fini();
/*
* Ensure that before a fork, globalmutex is taken.
*/
static void
pkcs11_fork_prepare(void)
{
(void) pthread_mutex_lock(&globalmutex);
}
/*
* Ensure that after a fork, in the parent, globalmutex is released.
*/
static void
pkcs11_fork_parent(void)
{
(void) pthread_mutex_unlock(&globalmutex);
}
/*
* Ensure that after a fork, in the child, globalmutex is released
* and cleanup is done.
*/
static void
pkcs11_fork_child(void)
{
(void) pthread_mutex_unlock(&globalmutex);
pkcs11_fini();
}
{
int initialize_pid;
/*
* Grab lock to insure only one thread enters
* this function at a time.
*/
(void) pthread_mutex_lock(&globalmutex);
initialize_pid = getpid();
/* Make sure function hasn't been called twice */
if (pkcs11_initialized) {
if (initialize_pid == pkcs11_pid) {
(void) pthread_mutex_unlock(&globalmutex);
return (CKR_CRYPTOKI_ALREADY_INITIALIZED);
} else {
/*
* A fork has happened and the child is
* reinitializing. Do a finalize_common() to close
* out any state from the parent, and then
* continue on.
*/
(void) finalize_common(NULL);
}
}
/* Check if application has provided mutex-handling functions */
/* pReserved should not be set */
goto errorexit;
}
/*
* Make sure function pointers are either all NULL or
* all set.
*/
goto errorexit;
}
/*
* Do not accept application supplied
* locking primitives.
*/
rv = CKR_CANT_LOCK;
goto errorexit;
}
}
/*
* Calling application does not want the library
* to create threads. This will effect
* C_WaitForSlotEvent().
*/
}
}
/* Initialize slot table */
goto errorexit;
/* Get the list of providers */
goto errorexit;
}
/*
* Load each provider, check for accessible slots,
* and populate slottable. If metaslot is enabled,
* it will be initialized as well.
*/
goto errorexit;
(void) pthread_atfork(pkcs11_fork_prepare,
(void) pthread_mutex_unlock(&globalmutex);
/* Cleanup data structures no longer needed */
return (CKR_OK);
/* Cleanup any data structures that have already been allocated */
if (slottable)
(void) pkcs11_slottable_delete();
if (pliblist)
(void) free_uentrylist(pliblist);
(void) pthread_mutex_unlock(&globalmutex);
return (rv);
}
/*
* C_Finalize is a wrapper around finalize_common. The
* globalmutex should be locked by C_Finalize().
*
* When an explicit C_Finalize() call is received, all
* plugins currently in the slottable will also be
* finalized. This must occur, even if libpkcs11(3lib)
* was not the first one to initialize the plugins, since it
* is the only way in PKCS#11 to force a refresh of the
* slot listings (ie to get new hardware devices).
*/
{
(void) pthread_mutex_lock(&globalmutex);
(void) pthread_mutex_unlock(&globalmutex);
return (rv);
}
/*
* finalize_common() does the work for C_Finalize. globalmutex
* must be held before calling this function.
*/
static CK_RV
{
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
return (CKR_ARGUMENTS_BAD);
}
fast_funcs = NULL;
fast_slot = 0;
pkcs11_pid = 0;
/* Check if C_WaitForSlotEvent() is currently active */
if (slottable->st_wfse_active) {
/*
* Wait for this thread to proceed far enough to block or
* end on its own. Otherwise, teardown of slottable may
* occurr before this active function completes.
*/
while (slottable->st_wfse_active) {
/*
* If C_WaitForSlotEvent is blocking, wake it up and
* return error to calling application.
*/
if (slottable->st_blocking) {
(void) pthread_cond_signal(
(void) pthread_mutex_unlock(
}
}
} else {
}
return (rv);
}
/*
* pkcs11_fini() function required to make sure complete cleanup
* is done of plugins if the framework is ever unloaded without
* a C_Finalize() call. This would be common when applications
* load and unload other libraries that use libpkcs11(3lib), since
* shared libraries should not call C_Finalize().
*
* If pkcs11_fini() is used, we set fini_called to B_TRUE so that
* pkcs11_slottable_delete() will not call C_Finalize() on the plugins.
*
* This is to protect in cases where the application has dlopened
* an object (for example, dlobj) that links to libpkcs11(3lib), but
* the application is unaware that the object is doing PKCS#11 calls
* underneath. This application may later directly dlopen one of the
* plugins (like pkcs11_softtoken.so, or any other 3rd party provided
* plugin) in order to directly perform PKCS#11 operations.
*
* While it is still actively using the PKCS#11 plugin directly,
* the application may finish with dlobj and dlclose it. As the
* reference count for libpkcs11(3lib) has become 0, pkcs11_fini()
* will be run by the linker. Even though libpkcs11(3lib) was the
* first to initialize the plugin in this case, it is not safe for
* libpkcs11(3lib) to finalize the plugin, as the application would
* lose state.
*/
static void
{
(void) pthread_mutex_lock(&globalmutex);
/* if we're not initilized, do not attempt to finalize */
if (!pkcs11_initialized) {
(void) pthread_mutex_unlock(&globalmutex);
return;
}
(void) finalize_common(NULL_PTR);
(void) pthread_mutex_unlock(&globalmutex);
}
{
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
}
if (!pkcs11_initialized)
return (CKR_CRYPTOKI_NOT_INITIALIZED);
return (CKR_ARGUMENTS_BAD);
}
/*
* Copy data into the provided buffer, use strncpy() instead
* of strlcpy() so that the strings are NOT NULL terminated,
* as required by the PKCS#11 standard
*/
return (CKR_OK);
}
/*
* This function is unaffected by the fast-path, since it is likely
* called before C_Initialize is, so we will not yet know the status
* of the fast-path. Additionally, policy will still need to be
* enforced if applicable.
*/
{
if (ppFunctionList == NULL) {
return (CKR_ARGUMENTS_BAD);
}
return (CKR_OK);
}
/*
* This function is no longer supported in this revision of the PKCS#11
* standard. It is maintained for backwards compatibility only.
*/
/*ARGSUSED*/
{
return (CKR_FUNCTION_NOT_PARALLEL);
}
/*
* This function is no longer supported in this revision of the PKCS#11
* standard. It is maintained for backwards compatibility only.
*/
/*ARGSUSED*/
{
return (CKR_FUNCTION_NOT_PARALLEL);
}