/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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
*/
/*
*/
#include <strings.h>
#include <errno.h>
#include <cryptoutil.h>
#include <unistd.h> /* for pid_t */
#include <pthread.h>
#include <security/cryptoki.h>
#include "softGlobal.h"
#include "softSession.h"
#include "softObject.h"
#include "softKeystore.h"
#include "softKeystoreUtil.h"
#pragma init(softtoken_init)
#pragma fini(softtoken_fini)
{ 2, 20 }, /* version */
};
/* This mutex protects soft_session_list, all_sessions_closing */
int all_sessions_closing = 0;
/* protects softtoken_initialized and access to C_Initialize/C_Finalize */
/* fips mode setting */
static void softtoken_init(void);
static void softtoken_fini(void);
static void softtoken_fork_prepare(void);
static void softtoken_fork_after(void);
{
int initialize_pid;
/*
* Get lock to insure only one thread enters this
* function at a time.
*/
(void) pthread_mutex_lock(&soft_giant_mutex);
initialize_pid = getpid();
if (softtoken_initialized) {
if (initialize_pid == softtoken_pid) {
/*
* This process has called C_Initialize already
*/
(void) pthread_mutex_unlock(&soft_giant_mutex);
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.
*/
}
}
/* pReserved must be NULL */
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_ARGUMENTS_BAD);
}
/*
* ALL supplied function pointers need to have the value
* either NULL or non-NULL.
*/
if (!supplied_ok) {
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_ARGUMENTS_BAD);
}
/*
* When the CKF_OS_LOCKING_OK flag isn't set and mutex
* function pointers are supplied by an application,
* return an error. We must be able to use our own primitives.
*/
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_CANT_LOCK);
}
}
/* Initialize the session list lock */
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_CANT_LOCK);
}
/*
* token object related initialization
*/
soft_slot.authenticated = 0;
(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (rv);
}
/* Initialize the slot lock */
(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
(void) soft_destroy_token_session();
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_CANT_LOCK);
}
/* Initialize the keystore lock */
(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
(void) soft_destroy_token_session();
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_CANT_LOCK);
}
/* Initialize the object_to_be_freed list */
!= 0) {
(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
(void) soft_destroy_token_session();
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_CANT_LOCK);
}
obj_delay_freed.count = 0;
!= 0) {
(void) pthread_mutex_destroy(
(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
(void) soft_destroy_token_session();
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_CANT_LOCK);
}
ses_delay_freed.count = 0;
/*
* Perform POST when fips mode is enabled.
*/
if (softtoken_fips_mode == CRYPTO_FIPS_MODE_ENABLED) {
rv = soft_fips_post();
}
}
(void) pthread_mutex_destroy(
(void) pthread_mutex_destroy(
(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
(void) soft_destroy_token_session();
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_FUNCTION_FAILED);
}
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_OK);
}
/*
* C_Finalize is a wrapper around finalize_common. The
* soft_giant_mutex should be locked by C_Finalize().
*/
{
(void) pthread_mutex_lock(&soft_giant_mutex);
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (rv);
}
/*
* finalize_common() does the work for C_Finalize. soft_giant_mutex
* must be held before calling this function.
*/
static CK_RV
if (!softtoken_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* C_Finalize() pReserved must be NULL; reserved for future use. */
return (CKR_ARGUMENTS_BAD);
}
(void) pthread_mutex_lock(&soft_sessionlist_mutex);
/*
* Set all_sessions_closing flag so any access to any
* existing sessions will be rejected.
*/
all_sessions_closing = 1;
(void) pthread_mutex_unlock(&soft_sessionlist_mutex);
/* Delete all the sessions and release the allocated resources */
(void) pthread_mutex_lock(&soft_sessionlist_mutex);
/* Reset all_sessions_closing flag. */
all_sessions_closing = 0;
(void) pthread_mutex_unlock(&soft_sessionlist_mutex);
softtoken_pid = 0;
/* Destroy the session list lock here */
(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
/*
* Destroy token object related stuff
* 1. Clean up the token object list
* 2. Destroy slot mutex
* 3. Destroy mutex in token_session
*/
(void) soft_destroy_token_session();
/*
* free all entries in the delay_freed list
*/
while (delay_free_obj != NULL) {
}
while (delay_free_ses != NULL) {
}
return (rv);
}
static void
softtoken_init(void)
{
/* Children inherit parent's atfork handlers */
(void) pthread_atfork(softtoken_fork_prepare,
}
/*
* softtoken_fini() function required to make sure complete cleanup
* is done if softtoken is ever unloaded without a C_Finalize() call.
*/
static void
softtoken_fini(void)
{
(void) pthread_mutex_lock(&soft_giant_mutex);
/* if we're not initialized, do not attempt to finalize */
if (!softtoken_initialized) {
(void) pthread_mutex_unlock(&soft_giant_mutex);
return;
}
(void) pthread_mutex_unlock(&soft_giant_mutex);
}
{
if (!softtoken_initialized)
return (CKR_CRYPTOKI_NOT_INITIALIZED);
return (CKR_ARGUMENTS_BAD);
}
/* Provide general information in the provided buffer */
SOFT_MANUFACTURER_ID, 32);
LIBRARY_DESCRIPTION, 32);
return (CKR_OK);
}
{
if (ppFunctionList == NULL) {
return (CKR_ARGUMENTS_BAD);
}
return (CKR_OK);
}
/*
* PKCS#11 states that C_GetFunctionStatus should always return
* CKR_FUNCTION_NOT_PARALLEL
*/
/*ARGSUSED*/
{
return (CKR_FUNCTION_NOT_PARALLEL);
}
/*
* PKCS#11 states that C_CancelFunction should always return
* CKR_FUNCTION_NOT_PARALLEL
*/
/*ARGSUSED*/
{
return (CKR_FUNCTION_NOT_PARALLEL);
}
/*
* Take out all mutexes before fork.
*
* Order:
* 1. soft_giant_mutex
* 2. soft_sessionlist_mutex
* 3. soft_slot.slot_mutex
* 4. soft_slot.keystore_mutex
* 5. token_session mutexes via soft_acquire_all_session_mutexes()
* 6. all soft_session_list mutexes via soft_acquire_all_session_mutexes()
* 7. obj_delay_freed.obj_to_be_free_mutex;
* 8. ses_delay_freed.ses_to_be_free_mutex
*/
void
softtoken_fork_prepare(void)
{
(void) pthread_mutex_lock(&soft_giant_mutex);
if (softtoken_initialized) {
(void) pthread_mutex_lock(&soft_sessionlist_mutex);
(void) pthread_mutex_lock(
(void) pthread_mutex_lock(
}
}
/*
* Release in opposite order to softtoken_fork_prepare().
* Function is used for parent and child.
*/
void
softtoken_fork_after(void)
{
if (softtoken_initialized) {
(void) pthread_mutex_unlock(
(void) pthread_mutex_unlock(
(void) pthread_mutex_unlock(&soft_sessionlist_mutex);
}
(void) pthread_mutex_unlock(&soft_giant_mutex);
}