kernelGeneral.c revision f66d273d14eede3a1bb803a39414588d8f143a98
/*
* 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 <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 fini(kernel_fini)
static struct CK_FUNCTION_LIST functionList = {
{ 2, 20 }, /* version */
};
static pid_t kernel_pid = 0;
int kernel_fd = -1;
/* protects kernel_initialized and entrance to C_Initialize/Finalize */
static void finalize_common();
static void cleanup_library();
static void kernel_fini();
{
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" */
/* 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) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Check to see if pReseved is NULL */
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) {
}
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
{
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++) {
}
}
/*
* kernel_fini() function required to make sure complete cleanup
* is done if pkcs11_kernel is ever unloaded without
* a C_Finalize() call.
*/
static void
{
(void) pthread_mutex_lock(&globalmutex);
/* if we're not initilized, 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);
}
/*
* PKCS#11 states that C_CancelFunction should always return
* CKR_FUNCTION_NOT_PARALLEL
*/
/*ARGSUSED*/
{
return (CKR_FUNCTION_NOT_PARALLEL);
}