#include <strings.h>
#include <errno.h>
#include <cryptoutil.h>
#include <unistd.h>
#include <pthread.h>
#include <stddef.h>
#include <security/cryptoki.h>
#include <sys/debug.h>
#include "softGlobal.h"
#include "softSession.h"
#include "softObject.h"
#include "softKeystore.h"
#include "softKeystoreUtil.h"
#pragma init(softtoken_init)
#pragma fini(softtoken_fini)
extern soft_session_t token_session;
static struct CK_FUNCTION_LIST functionList = {
{ 2, 20 },
C_Initialize,
C_Finalize,
C_GetInfo,
C_GetFunctionList,
C_GetSlotList,
C_GetSlotInfo,
C_GetTokenInfo,
C_GetMechanismList,
C_GetMechanismInfo,
C_InitToken,
C_InitPIN,
C_SetPIN,
C_OpenSession,
C_CloseSession,
C_CloseAllSessions,
C_GetSessionInfo,
C_GetOperationState,
C_SetOperationState,
C_Login,
C_Logout,
C_CreateObject,
C_CopyObject,
C_DestroyObject,
C_GetObjectSize,
C_GetAttributeValue,
C_SetAttributeValue,
C_FindObjectsInit,
C_FindObjects,
C_FindObjectsFinal,
C_EncryptInit,
C_Encrypt,
C_EncryptUpdate,
C_EncryptFinal,
C_DecryptInit,
C_Decrypt,
C_DecryptUpdate,
C_DecryptFinal,
C_DigestInit,
C_Digest,
C_DigestUpdate,
C_DigestKey,
C_DigestFinal,
C_SignInit,
C_Sign,
C_SignUpdate,
C_SignFinal,
C_SignRecoverInit,
C_SignRecover,
C_VerifyInit,
C_Verify,
C_VerifyUpdate,
C_VerifyFinal,
C_VerifyRecoverInit,
C_VerifyRecover,
C_DigestEncryptUpdate,
C_DecryptDigestUpdate,
C_SignEncryptUpdate,
C_DecryptVerifyUpdate,
C_GenerateKey,
C_GenerateKeyPair,
C_WrapKey,
C_UnwrapKey,
C_DeriveKey,
C_SeedRandom,
C_GenerateRandom,
C_GetFunctionStatus,
C_CancelFunction,
C_WaitForSlotEvent
};
boolean_t softtoken_initialized = B_FALSE;
static pid_t softtoken_pid = 0;
pthread_mutex_t soft_sessionlist_mutex;
soft_session_t *soft_session_list = NULL;
avl_tree_t soft_session_tree;
pthread_mutex_t soft_object_mutex;
avl_tree_t soft_object_tree;
int all_sessions_closing = 0;
slot_t soft_slot;
obj_to_be_freed_list_t obj_delay_freed;
ses_to_be_freed_list_t ses_delay_freed;
pthread_mutex_t soft_giant_mutex = PTHREAD_MUTEX_INITIALIZER;
static CK_RV finalize_common(boolean_t force, CK_VOID_PTR pReserved);
static void softtoken_init(void);
static void softtoken_fini(void);
static void softtoken_fork_prepare(void);
static void softtoken_fork_after(void);
static int session_compare(const void *a, const void *b);
static int object_compare(const void *a, const void *b);
CK_RV
C_Initialize(CK_VOID_PTR pInitArgs)
{
pthread_mutexattr_t attr = { 0 };
int initialize_pid;
boolean_t supplied_ok;
CK_RV rv;
(void) pthread_mutex_lock(&soft_giant_mutex);
initialize_pid = getpid();
if (softtoken_initialized) {
if (initialize_pid == softtoken_pid) {
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_CRYPTOKI_ALREADY_INITIALIZED);
} else {
(void) finalize_common(B_TRUE, NULL);
}
}
if (pInitArgs != NULL) {
CK_C_INITIALIZE_ARGS *initargs1 =
(CK_C_INITIALIZE_ARGS *) pInitArgs;
if (initargs1->pReserved != NULL) {
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_ARGUMENTS_BAD);
}
supplied_ok = (initargs1->CreateMutex == NULL &&
initargs1->DestroyMutex == NULL &&
initargs1->LockMutex == NULL &&
initargs1->UnlockMutex == NULL) ||
(initargs1->CreateMutex != NULL &&
initargs1->DestroyMutex != NULL &&
initargs1->LockMutex != NULL &&
initargs1->UnlockMutex != NULL);
if (!supplied_ok) {
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_ARGUMENTS_BAD);
}
if (!(initargs1->flags & CKF_OS_LOCKING_OK) &&
(initargs1->CreateMutex != NULL)) {
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_CANT_LOCK);
}
}
if (pthread_mutexattr_init(&attr) != 0) {
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_HOST_MEMORY);
}
VERIFY0(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
if (pthread_mutex_init(&soft_sessionlist_mutex, &attr) != 0) {
(void) pthread_mutexattr_destroy(&attr);
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_CANT_LOCK);
}
if (pthread_mutex_init(&soft_object_mutex, &attr) != 0) {
(void) pthread_mutexattr_destroy(&attr);
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_CANT_LOCK);
}
VERIFY0(pthread_mutexattr_destroy(&attr));
soft_slot.authenticated = 0;
soft_slot.userpin_change_needed = 0;
soft_slot.token_object_list = NULL;
soft_slot.keystore_load_status = KEYSTORE_UNINITIALIZED;
if ((rv = soft_init_token_session()) != CKR_OK) {
(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (rv);
}
if (pthread_mutex_init(&soft_slot.slot_mutex, NULL) != 0) {
(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
(void) soft_destroy_token_session();
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_CANT_LOCK);
}
if (pthread_mutex_init(&soft_slot.keystore_mutex, NULL) != 0) {
(void) pthread_mutex_destroy(&soft_slot.slot_mutex);
(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
(void) soft_destroy_token_session();
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_CANT_LOCK);
}
if (pthread_mutex_init(&obj_delay_freed.obj_to_be_free_mutex, NULL)
!= 0) {
(void) pthread_mutex_destroy(&soft_slot.keystore_mutex);
(void) pthread_mutex_destroy(&soft_slot.slot_mutex);
(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;
obj_delay_freed.first = NULL;
obj_delay_freed.last = NULL;
if (pthread_mutex_init(&ses_delay_freed.ses_to_be_free_mutex, NULL)
!= 0) {
(void) pthread_mutex_destroy(
&obj_delay_freed.obj_to_be_free_mutex);
(void) pthread_mutex_destroy(&soft_slot.keystore_mutex);
(void) pthread_mutex_destroy(&soft_slot.slot_mutex);
(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;
ses_delay_freed.first = NULL;
ses_delay_freed.last = NULL;
if (rv != CKR_OK) {
(void) pthread_mutex_destroy(
&ses_delay_freed.ses_to_be_free_mutex);
(void) pthread_mutex_destroy(
&obj_delay_freed.obj_to_be_free_mutex);
(void) pthread_mutex_destroy(&soft_slot.keystore_mutex);
(void) pthread_mutex_destroy(&soft_slot.slot_mutex);
(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
(void) soft_destroy_token_session();
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_FUNCTION_FAILED);
}
softtoken_pid = initialize_pid;
softtoken_initialized = B_TRUE;
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (CKR_OK);
}
CK_RV
C_Finalize(CK_VOID_PTR pReserved)
{
CK_RV rv;
(void) pthread_mutex_lock(&soft_giant_mutex);
rv = finalize_common(B_FALSE, pReserved);
(void) pthread_mutex_unlock(&soft_giant_mutex);
return (rv);
}
static CK_RV
finalize_common(boolean_t force, CK_VOID_PTR pReserved)
{
CK_RV rv = CKR_OK;
struct object *delay_free_obj, *tmpo;
struct session *delay_free_ses, *tmps;
if (!softtoken_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
if (pReserved != NULL) {
return (CKR_ARGUMENTS_BAD);
}
(void) pthread_mutex_lock(&soft_sessionlist_mutex);
all_sessions_closing = 1;
(void) pthread_mutex_unlock(&soft_sessionlist_mutex);
rv = soft_delete_all_sessions(force);
(void) pthread_mutex_lock(&soft_sessionlist_mutex);
all_sessions_closing = 0;
(void) pthread_mutex_unlock(&soft_sessionlist_mutex);
softtoken_initialized = B_FALSE;
softtoken_pid = 0;
(void) pthread_mutex_destroy(&soft_object_mutex);
(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
soft_delete_all_in_core_token_objects(ALL_TOKEN);
(void) pthread_mutex_destroy(&soft_slot.slot_mutex);
(void) pthread_mutex_destroy(&soft_slot.keystore_mutex);
(void) soft_destroy_token_session();
delay_free_obj = obj_delay_freed.first;
while (delay_free_obj != NULL) {
tmpo = delay_free_obj->next;
free(delay_free_obj);
delay_free_obj = tmpo;
}
soft_slot.keystore_load_status = KEYSTORE_UNINITIALIZED;
(void) pthread_mutex_destroy(&obj_delay_freed.obj_to_be_free_mutex);
delay_free_ses = ses_delay_freed.first;
while (delay_free_ses != NULL) {
tmps = delay_free_ses->next;
free(delay_free_ses);
delay_free_ses = tmps;
}
(void) pthread_mutex_destroy(&ses_delay_freed.ses_to_be_free_mutex);
return (rv);
}
static void
softtoken_init()
{
avl_create(&soft_session_tree, session_compare, sizeof (soft_session_t),
offsetof(soft_session_t, node));
avl_create(&soft_object_tree, object_compare, sizeof (soft_object_t),
offsetof(soft_object_t, node));
(void) pthread_atfork(softtoken_fork_prepare,
softtoken_fork_after, softtoken_fork_after);
}
static void
softtoken_fini()
{
(void) pthread_mutex_lock(&soft_giant_mutex);
if (!softtoken_initialized) {
(void) pthread_mutex_unlock(&soft_giant_mutex);
return;
}
(void) finalize_common(B_TRUE, NULL_PTR);
avl_destroy(&soft_object_tree);
avl_destroy(&soft_session_tree);
(void) pthread_mutex_unlock(&soft_giant_mutex);
}
CK_RV
C_GetInfo(CK_INFO_PTR pInfo)
{
if (!softtoken_initialized)
return (CKR_CRYPTOKI_NOT_INITIALIZED);
if (pInfo == NULL) {
return (CKR_ARGUMENTS_BAD);
}
pInfo->cryptokiVersion.major = CRYPTOKI_VERSION_MAJOR;
pInfo->cryptokiVersion.minor = CRYPTOKI_VERSION_MINOR;
(void) strncpy((char *)pInfo->manufacturerID,
SOFT_MANUFACTURER_ID, 32);
pInfo->flags = 0;
(void) strncpy((char *)pInfo->libraryDescription,
LIBRARY_DESCRIPTION, 32);
pInfo->libraryVersion.major = LIBRARY_VERSION_MAJOR;
pInfo->libraryVersion.minor = LIBRARY_VERSION_MINOR;
return (CKR_OK);
}
CK_RV
C_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR ppFunctionList)
{
if (ppFunctionList == NULL) {
return (CKR_ARGUMENTS_BAD);
}
*ppFunctionList = &functionList;
return (CKR_OK);
}
CK_RV
C_GetFunctionStatus(CK_SESSION_HANDLE hSession)
{
return (CKR_FUNCTION_NOT_PARALLEL);
}
CK_RV
C_CancelFunction(CK_SESSION_HANDLE hSession)
{
return (CKR_FUNCTION_NOT_PARALLEL);
}
void
softtoken_fork_prepare()
{
(void) pthread_mutex_lock(&soft_giant_mutex);
if (softtoken_initialized) {
(void) pthread_mutex_lock(&soft_sessionlist_mutex);
(void) pthread_mutex_lock(&soft_slot.slot_mutex);
(void) pthread_mutex_lock(&soft_slot.keystore_mutex);
soft_acquire_all_session_mutexes(&token_session);
soft_acquire_all_session_mutexes(soft_session_list);
VERIFY0(pthread_mutex_lock(&soft_object_mutex));
(void) pthread_mutex_lock(
&obj_delay_freed.obj_to_be_free_mutex);
(void) pthread_mutex_lock(
&ses_delay_freed.ses_to_be_free_mutex);
}
}
void
softtoken_fork_after()
{
if (softtoken_initialized) {
(void) pthread_mutex_unlock(
&ses_delay_freed.ses_to_be_free_mutex);
(void) pthread_mutex_unlock(
&obj_delay_freed.obj_to_be_free_mutex);
VERIFY0(pthread_mutex_unlock(&soft_object_mutex));
soft_release_all_session_mutexes(soft_session_list);
soft_release_all_session_mutexes(&token_session);
(void) pthread_mutex_unlock(&soft_slot.keystore_mutex);
(void) pthread_mutex_unlock(&soft_slot.slot_mutex);
(void) pthread_mutex_unlock(&soft_sessionlist_mutex);
}
(void) pthread_mutex_unlock(&soft_giant_mutex);
}
static int
session_compare(const void *a, const void *b)
{
const soft_session_t *l = a;
const soft_session_t *r = b;
if (l->handle < r->handle)
return (-1);
if (l->handle > r->handle)
return (1);
return (0);
}
static int
object_compare(const void *a, const void *b)
{
const soft_object_t *l = a;
const soft_object_t *r = b;
if (l->handle < r->handle)
return (-1);
if (l->handle > r->handle)
return (1);
return (0);
}