/*
* 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 <fcntl.h>
#include <pthread.h>
#include <strings.h>
#include <unistd.h> /* for pid */
#include <errno.h>
#include <security/cryptoki.h>
#include "kernelGlobal.h"
#include "kernelSession.h"
#include "kernelSlot.h"
#pragma init(kernel_init)
#pragma fini(kernel_fini)
{ 2, 20 }, /* version */
};
extern pthread_mutex_t mechhash_mutex;
/* protects kernel_initialized and entrance to C_Initialize/Finalize */
static void finalize_common(void);
static void cleanup_library(void);
static void kernel_init(void);
static void kernel_fini(void);
static void kernel_fork_prepare(void);
static void kernel_fork_after(void);
{
int initialize_pid;
/*
* Grab lock to insure that only one thread enters this
* function at a time.
*/
(void) pthread_mutex_lock(&globalmutex);
initialize_pid = getpid();
if (kernel_initialized) {
if (initialize_pid == kernel_pid) {
/*
* This process has called C_Initialize already
*/
(void) pthread_mutex_unlock(&globalmutex);
return (CKR_CRYPTOKI_ALREADY_INITIALIZED);
} else {
/*
* A fork has happened and the child is
* reinitializing. Do a cleanup_library to close
* out any state from the parent, and then
* continue on.
*/
}
}
/* pReserved must be NULL */
(void) pthread_mutex_unlock(&globalmutex);
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(&globalmutex);
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 locks.
*/
(void) pthread_mutex_unlock(&globalmutex);
return (CKR_CANT_LOCK);
}
}
break;
}
if (kernel_fd < 0) {
(void) pthread_mutex_unlock(&globalmutex);
return (CKR_FUNCTION_FAILED);
}
/* Mark kernel_fd "close on exec" */
/* Create the hash table */
if (kernel_mechhash == NULL) {
(void) pthread_mutex_unlock(&globalmutex);
return (CKR_HOST_MEMORY);
}
/* Initialize the slot table */
rv = kernel_slottable_init();
(void) pthread_mutex_unlock(&globalmutex);
return (rv);
}
/* Initialize the object_to_be_freed list */
obj_delay_freed.count = 0;
/* Initialize the session_to_be_freed list */
ses_delay_freed.count = 0;
(void) pthread_mutex_unlock(&globalmutex);
return (CKR_OK);
}
/*
* C_Finalize is a wrapper around finalize_common. The
* globalmutex should be locked by C_Finalize().
*/
{
int i;
(void) pthread_mutex_lock(&globalmutex);
if (!kernel_initialized) {
(void) pthread_mutex_unlock(&globalmutex);
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Check to see if pReserved is NULL */
(void) pthread_mutex_unlock(&globalmutex);
return (CKR_ARGUMENTS_BAD);
}
/*
* Delete all the sessions for each slot and release the allocated
* resources
*/
for (i = 0; i < slot_count; i++) {
}
(void) pthread_mutex_unlock(&globalmutex);
return (CKR_OK);
}
/*
* finalize_common() does the work for C_Finalize. globalmutex
* must be held before calling this function.
*/
static void
finalize_common() {
int i;
/*
* Free the resources allocated for the slot table and reset
* slot_count to 0.
*/
if (slot_count > 0) {
for (i = 0; i < slot_count; i++) {
(void) free(slot_table[i]);
}
(void) free(slot_table);
slot_count = 0;
}
/* Close CRYPTO_DEVICE */
if (kernel_fd >= 0) {
}
/* Walk the hash table and free all entries */
for (i = 0; i < KMECH_HASHTABLE_SIZE; i++) {
elem = kernel_mechhash[i];
}
}
kernel_fd = -1;
kernel_pid = 0;
/*
* free all entries in the delay_freed list
*/
while (delay_free_obj != NULL) {
}
while (delay_free_ses != NULL) {
}
}
/*
* This function cleans up all the resources in the library (user space only)
*/
static void
cleanup_library(void)
{
int i;
/*
* Delete all the sessions for each slot and release the allocated
* resources from the library. The boolean argument TRUE indicates
* that we only wants to clean up the resource in the library only.
* We don't want to clean up the corresponding kernel part of
* resources, because they are used by the parent process still.
*/
for (i = 0; i < slot_count; i++) {
}
}
static void
kernel_init(void)
{
}
/*
* kernel_fini() function required to make sure complete cleanup
* is done if pkcs11_kernel is ever unloaded without
* a C_Finalize() call.
*/
static void
kernel_fini(void)
{
(void) pthread_mutex_lock(&globalmutex);
/* if we're not initialized, do not attempt to finalize */
if (!kernel_initialized) {
(void) pthread_mutex_unlock(&globalmutex);
return;
}
(void) pthread_mutex_unlock(&globalmutex);
}
{
if (!kernel_initialized)
return (CKR_CRYPTOKI_NOT_INITIALIZED);
return (CKR_ARGUMENTS_BAD);
}
/* Check if the cryptoki was initialized */
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);
}
/*
* Take out all mutexes before fork.
* Order:
* 1. globalmutex
* 2. all slots mutexes (and all their sessions) via
* kernel_acquire_all_slots_mutexes()
* 3. obj_delay_freed.obj_to_be_free_mutex;
* 4. ses_delay_freed.ses_to_be_free_mutex
*/
void
kernel_fork_prepare(void)
{
(void) pthread_mutex_lock(&globalmutex);
if (kernel_initialized) {
(void) pthread_mutex_lock(
(void) pthread_mutex_lock(
(void) pthread_mutex_lock(&mechhash_mutex);
}
}
/*
* Release in opposite order to kernel_fork_prepare().
* Function is used for parent and child.
*/
void
kernel_fork_after(void)
{
if (kernel_initialized) {
(void) pthread_mutex_unlock(&mechhash_mutex);
(void) pthread_mutex_unlock(
(void) pthread_mutex_unlock(
}
(void) pthread_mutex_unlock(&globalmutex);
}
/*
* PKCS#11 states that C_CancelFunction should always return
* CKR_FUNCTION_NOT_PARALLEL
*/
/*ARGSUSED*/
{
return (CKR_FUNCTION_NOT_PARALLEL);
}