17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
58047c9fbSmcpowers * Common Development and Distribution License (the "License").
68047c9fbSmcpowers * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
2219193bb6SDina K Nimeh * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
24*05d57413SDan McDonald *
25*05d57413SDan McDonald * Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <strings.h>
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <cryptoutil.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h> /* for pid_t */
327c478bd9Sstevel@tonic-gate #include <pthread.h>
337c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
347c478bd9Sstevel@tonic-gate #include "softGlobal.h"
357c478bd9Sstevel@tonic-gate #include "softSession.h"
367c478bd9Sstevel@tonic-gate #include "softObject.h"
377c478bd9Sstevel@tonic-gate #include "softKeystore.h"
387c478bd9Sstevel@tonic-gate #include "softKeystoreUtil.h"
397c478bd9Sstevel@tonic-gate
4083140133SZdenek Kotala #pragma init(softtoken_init)
417c478bd9Sstevel@tonic-gate #pragma fini(softtoken_fini)
427c478bd9Sstevel@tonic-gate
4383140133SZdenek Kotala extern soft_session_t token_session; /* for fork handler */
4483140133SZdenek Kotala
457c478bd9Sstevel@tonic-gate static struct CK_FUNCTION_LIST functionList = {
46f66d273dSizick { 2, 20 }, /* version */
477c478bd9Sstevel@tonic-gate C_Initialize,
487c478bd9Sstevel@tonic-gate C_Finalize,
497c478bd9Sstevel@tonic-gate C_GetInfo,
507c478bd9Sstevel@tonic-gate C_GetFunctionList,
517c478bd9Sstevel@tonic-gate C_GetSlotList,
527c478bd9Sstevel@tonic-gate C_GetSlotInfo,
537c478bd9Sstevel@tonic-gate C_GetTokenInfo,
547c478bd9Sstevel@tonic-gate C_GetMechanismList,
557c478bd9Sstevel@tonic-gate C_GetMechanismInfo,
567c478bd9Sstevel@tonic-gate C_InitToken,
577c478bd9Sstevel@tonic-gate C_InitPIN,
587c478bd9Sstevel@tonic-gate C_SetPIN,
597c478bd9Sstevel@tonic-gate C_OpenSession,
607c478bd9Sstevel@tonic-gate C_CloseSession,
617c478bd9Sstevel@tonic-gate C_CloseAllSessions,
627c478bd9Sstevel@tonic-gate C_GetSessionInfo,
637c478bd9Sstevel@tonic-gate C_GetOperationState,
647c478bd9Sstevel@tonic-gate C_SetOperationState,
657c478bd9Sstevel@tonic-gate C_Login,
667c478bd9Sstevel@tonic-gate C_Logout,
677c478bd9Sstevel@tonic-gate C_CreateObject,
687c478bd9Sstevel@tonic-gate C_CopyObject,
697c478bd9Sstevel@tonic-gate C_DestroyObject,
707c478bd9Sstevel@tonic-gate C_GetObjectSize,
717c478bd9Sstevel@tonic-gate C_GetAttributeValue,
727c478bd9Sstevel@tonic-gate C_SetAttributeValue,
737c478bd9Sstevel@tonic-gate C_FindObjectsInit,
747c478bd9Sstevel@tonic-gate C_FindObjects,
757c478bd9Sstevel@tonic-gate C_FindObjectsFinal,
767c478bd9Sstevel@tonic-gate C_EncryptInit,
777c478bd9Sstevel@tonic-gate C_Encrypt,
787c478bd9Sstevel@tonic-gate C_EncryptUpdate,
797c478bd9Sstevel@tonic-gate C_EncryptFinal,
807c478bd9Sstevel@tonic-gate C_DecryptInit,
817c478bd9Sstevel@tonic-gate C_Decrypt,
827c478bd9Sstevel@tonic-gate C_DecryptUpdate,
837c478bd9Sstevel@tonic-gate C_DecryptFinal,
847c478bd9Sstevel@tonic-gate C_DigestInit,
857c478bd9Sstevel@tonic-gate C_Digest,
867c478bd9Sstevel@tonic-gate C_DigestUpdate,
877c478bd9Sstevel@tonic-gate C_DigestKey,
887c478bd9Sstevel@tonic-gate C_DigestFinal,
897c478bd9Sstevel@tonic-gate C_SignInit,
907c478bd9Sstevel@tonic-gate C_Sign,
917c478bd9Sstevel@tonic-gate C_SignUpdate,
927c478bd9Sstevel@tonic-gate C_SignFinal,
937c478bd9Sstevel@tonic-gate C_SignRecoverInit,
947c478bd9Sstevel@tonic-gate C_SignRecover,
957c478bd9Sstevel@tonic-gate C_VerifyInit,
967c478bd9Sstevel@tonic-gate C_Verify,
977c478bd9Sstevel@tonic-gate C_VerifyUpdate,
987c478bd9Sstevel@tonic-gate C_VerifyFinal,
997c478bd9Sstevel@tonic-gate C_VerifyRecoverInit,
1007c478bd9Sstevel@tonic-gate C_VerifyRecover,
1017c478bd9Sstevel@tonic-gate C_DigestEncryptUpdate,
1027c478bd9Sstevel@tonic-gate C_DecryptDigestUpdate,
1037c478bd9Sstevel@tonic-gate C_SignEncryptUpdate,
1047c478bd9Sstevel@tonic-gate C_DecryptVerifyUpdate,
1057c478bd9Sstevel@tonic-gate C_GenerateKey,
1067c478bd9Sstevel@tonic-gate C_GenerateKeyPair,
1077c478bd9Sstevel@tonic-gate C_WrapKey,
1087c478bd9Sstevel@tonic-gate C_UnwrapKey,
1097c478bd9Sstevel@tonic-gate C_DeriveKey,
1107c478bd9Sstevel@tonic-gate C_SeedRandom,
1117c478bd9Sstevel@tonic-gate C_GenerateRandom,
1127c478bd9Sstevel@tonic-gate C_GetFunctionStatus,
1137c478bd9Sstevel@tonic-gate C_CancelFunction,
1147c478bd9Sstevel@tonic-gate C_WaitForSlotEvent
1157c478bd9Sstevel@tonic-gate };
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate boolean_t softtoken_initialized = B_FALSE;
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate static pid_t softtoken_pid = 0;
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate /* This mutex protects soft_session_list, all_sessions_closing */
1227c478bd9Sstevel@tonic-gate pthread_mutex_t soft_sessionlist_mutex;
1237c478bd9Sstevel@tonic-gate soft_session_t *soft_session_list = NULL;
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate int all_sessions_closing = 0;
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate slot_t soft_slot;
1287c478bd9Sstevel@tonic-gate obj_to_be_freed_list_t obj_delay_freed;
1297c478bd9Sstevel@tonic-gate ses_to_be_freed_list_t ses_delay_freed;
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate /* protects softtoken_initialized and access to C_Initialize/C_Finalize */
1327c478bd9Sstevel@tonic-gate pthread_mutex_t soft_giant_mutex = PTHREAD_MUTEX_INITIALIZER;
1337c478bd9Sstevel@tonic-gate
134a62b4373Sdarrenm static CK_RV finalize_common(boolean_t force, CK_VOID_PTR pReserved);
13583140133SZdenek Kotala static void softtoken_init();
1367c478bd9Sstevel@tonic-gate static void softtoken_fini();
1374daf2311Srupertk static void softtoken_fork_prepare();
13883140133SZdenek Kotala static void softtoken_fork_after();
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate CK_RV
C_Initialize(CK_VOID_PTR pInitArgs)1417c478bd9Sstevel@tonic-gate C_Initialize(CK_VOID_PTR pInitArgs)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate int initialize_pid;
1457c478bd9Sstevel@tonic-gate boolean_t supplied_ok;
1467c478bd9Sstevel@tonic-gate CK_RV rv;
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate * Get lock to insure only one thread enters this
1507c478bd9Sstevel@tonic-gate * function at a time.
1517c478bd9Sstevel@tonic-gate */
1527c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&soft_giant_mutex);
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate initialize_pid = getpid();
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate if (softtoken_initialized) {
1577c478bd9Sstevel@tonic-gate if (initialize_pid == softtoken_pid) {
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate * This process has called C_Initialize already
1607c478bd9Sstevel@tonic-gate */
1617c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&soft_giant_mutex);
1627c478bd9Sstevel@tonic-gate return (CKR_CRYPTOKI_ALREADY_INITIALIZED);
1637c478bd9Sstevel@tonic-gate } else {
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate * A fork has happened and the child is
1667c478bd9Sstevel@tonic-gate * reinitializing. Do a finalize_common to close
1677c478bd9Sstevel@tonic-gate * out any state from the parent, and then
1687c478bd9Sstevel@tonic-gate * continue on.
1697c478bd9Sstevel@tonic-gate */
170a62b4373Sdarrenm (void) finalize_common(B_TRUE, NULL);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate if (pInitArgs != NULL) {
1757c478bd9Sstevel@tonic-gate CK_C_INITIALIZE_ARGS *initargs1 =
1767c478bd9Sstevel@tonic-gate (CK_C_INITIALIZE_ARGS *) pInitArgs;
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate /* pReserved must be NULL */
1797c478bd9Sstevel@tonic-gate if (initargs1->pReserved != NULL) {
1807c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&soft_giant_mutex);
1817c478bd9Sstevel@tonic-gate return (CKR_ARGUMENTS_BAD);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate * ALL supplied function pointers need to have the value
1867c478bd9Sstevel@tonic-gate * either NULL or non-NULL.
1877c478bd9Sstevel@tonic-gate */
1887c478bd9Sstevel@tonic-gate supplied_ok = (initargs1->CreateMutex == NULL &&
1897c478bd9Sstevel@tonic-gate initargs1->DestroyMutex == NULL &&
1907c478bd9Sstevel@tonic-gate initargs1->LockMutex == NULL &&
1917c478bd9Sstevel@tonic-gate initargs1->UnlockMutex == NULL) ||
1927c478bd9Sstevel@tonic-gate (initargs1->CreateMutex != NULL &&
1937c478bd9Sstevel@tonic-gate initargs1->DestroyMutex != NULL &&
1947c478bd9Sstevel@tonic-gate initargs1->LockMutex != NULL &&
1957c478bd9Sstevel@tonic-gate initargs1->UnlockMutex != NULL);
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate if (!supplied_ok) {
1987c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&soft_giant_mutex);
1997c478bd9Sstevel@tonic-gate return (CKR_ARGUMENTS_BAD);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate * When the CKF_OS_LOCKING_OK flag isn't set and mutex
2047c478bd9Sstevel@tonic-gate * function pointers are supplied by an application,
2057c478bd9Sstevel@tonic-gate * return an error. We must be able to use our own primitives.
2067c478bd9Sstevel@tonic-gate */
2077c478bd9Sstevel@tonic-gate if (!(initargs1->flags & CKF_OS_LOCKING_OK) &&
2087c478bd9Sstevel@tonic-gate (initargs1->CreateMutex != NULL)) {
2097c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&soft_giant_mutex);
2107c478bd9Sstevel@tonic-gate return (CKR_CANT_LOCK);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate /* Initialize the session list lock */
2157c478bd9Sstevel@tonic-gate if (pthread_mutex_init(&soft_sessionlist_mutex, NULL) != 0) {
2167c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&soft_giant_mutex);
2177c478bd9Sstevel@tonic-gate return (CKR_CANT_LOCK);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate * token object related initialization
2227c478bd9Sstevel@tonic-gate */
2237c478bd9Sstevel@tonic-gate soft_slot.authenticated = 0;
2247c478bd9Sstevel@tonic-gate soft_slot.userpin_change_needed = 0;
2257c478bd9Sstevel@tonic-gate soft_slot.token_object_list = NULL;
22690e0e8c4Sizick soft_slot.keystore_load_status = KEYSTORE_UNINITIALIZED;
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate if ((rv = soft_init_token_session()) != CKR_OK) {
22983140133SZdenek Kotala (void) pthread_mutex_destroy(&soft_sessionlist_mutex);
2307c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&soft_giant_mutex);
2317c478bd9Sstevel@tonic-gate return (rv);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate /* Initialize the slot lock */
2357c478bd9Sstevel@tonic-gate if (pthread_mutex_init(&soft_slot.slot_mutex, NULL) != 0) {
23683140133SZdenek Kotala (void) pthread_mutex_destroy(&soft_sessionlist_mutex);
2377c478bd9Sstevel@tonic-gate (void) soft_destroy_token_session();
2387c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&soft_giant_mutex);
2397c478bd9Sstevel@tonic-gate return (CKR_CANT_LOCK);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate
24290e0e8c4Sizick /* Initialize the keystore lock */
24390e0e8c4Sizick if (pthread_mutex_init(&soft_slot.keystore_mutex, NULL) != 0) {
24483140133SZdenek Kotala (void) pthread_mutex_destroy(&soft_slot.slot_mutex);
24583140133SZdenek Kotala (void) pthread_mutex_destroy(&soft_sessionlist_mutex);
24683140133SZdenek Kotala (void) soft_destroy_token_session();
2477c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&soft_giant_mutex);
24890e0e8c4Sizick return (CKR_CANT_LOCK);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate /* Initialize the object_to_be_freed list */
25283140133SZdenek Kotala if (pthread_mutex_init(&obj_delay_freed.obj_to_be_free_mutex, NULL)
25383140133SZdenek Kotala != 0) {
25483140133SZdenek Kotala (void) pthread_mutex_destroy(&soft_slot.keystore_mutex);
25583140133SZdenek Kotala (void) pthread_mutex_destroy(&soft_slot.slot_mutex);
25683140133SZdenek Kotala (void) pthread_mutex_destroy(&soft_sessionlist_mutex);
25783140133SZdenek Kotala (void) soft_destroy_token_session();
25883140133SZdenek Kotala (void) pthread_mutex_unlock(&soft_giant_mutex);
25983140133SZdenek Kotala return (CKR_CANT_LOCK);
26083140133SZdenek Kotala }
2617c478bd9Sstevel@tonic-gate obj_delay_freed.count = 0;
2627c478bd9Sstevel@tonic-gate obj_delay_freed.first = NULL;
2637c478bd9Sstevel@tonic-gate obj_delay_freed.last = NULL;
2647c478bd9Sstevel@tonic-gate
26583140133SZdenek Kotala if (pthread_mutex_init(&ses_delay_freed.ses_to_be_free_mutex, NULL)
26683140133SZdenek Kotala != 0) {
26783140133SZdenek Kotala (void) pthread_mutex_destroy(
26883140133SZdenek Kotala &obj_delay_freed.obj_to_be_free_mutex);
26983140133SZdenek Kotala (void) pthread_mutex_destroy(&soft_slot.keystore_mutex);
27083140133SZdenek Kotala (void) pthread_mutex_destroy(&soft_slot.slot_mutex);
27183140133SZdenek Kotala (void) pthread_mutex_destroy(&soft_sessionlist_mutex);
27283140133SZdenek Kotala (void) soft_destroy_token_session();
27383140133SZdenek Kotala (void) pthread_mutex_unlock(&soft_giant_mutex);
27483140133SZdenek Kotala return (CKR_CANT_LOCK);
27583140133SZdenek Kotala }
2767c478bd9Sstevel@tonic-gate ses_delay_freed.count = 0;
2777c478bd9Sstevel@tonic-gate ses_delay_freed.first = NULL;
2787c478bd9Sstevel@tonic-gate ses_delay_freed.last = NULL;
2797c478bd9Sstevel@tonic-gate
280b5a2d845SHai-May Chao if (rv != CKR_OK) {
281b5a2d845SHai-May Chao (void) pthread_mutex_destroy(
282b5a2d845SHai-May Chao &ses_delay_freed.ses_to_be_free_mutex);
283b5a2d845SHai-May Chao (void) pthread_mutex_destroy(
284b5a2d845SHai-May Chao &obj_delay_freed.obj_to_be_free_mutex);
285b5a2d845SHai-May Chao (void) pthread_mutex_destroy(&soft_slot.keystore_mutex);
286b5a2d845SHai-May Chao (void) pthread_mutex_destroy(&soft_slot.slot_mutex);
287b5a2d845SHai-May Chao (void) pthread_mutex_destroy(&soft_sessionlist_mutex);
288b5a2d845SHai-May Chao (void) soft_destroy_token_session();
289b5a2d845SHai-May Chao (void) pthread_mutex_unlock(&soft_giant_mutex);
290b5a2d845SHai-May Chao return (CKR_FUNCTION_FAILED);
291b5a2d845SHai-May Chao }
292b5a2d845SHai-May Chao
29383140133SZdenek Kotala softtoken_pid = initialize_pid;
29483140133SZdenek Kotala softtoken_initialized = B_TRUE;
29583140133SZdenek Kotala (void) pthread_mutex_unlock(&soft_giant_mutex);
29683140133SZdenek Kotala
29783140133SZdenek Kotala return (CKR_OK);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate
3007c478bd9Sstevel@tonic-gate /*
3017c478bd9Sstevel@tonic-gate * C_Finalize is a wrapper around finalize_common. The
3027c478bd9Sstevel@tonic-gate * soft_giant_mutex should be locked by C_Finalize().
3037c478bd9Sstevel@tonic-gate */
3047c478bd9Sstevel@tonic-gate CK_RV
C_Finalize(CK_VOID_PTR pReserved)3057c478bd9Sstevel@tonic-gate C_Finalize(CK_VOID_PTR pReserved)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate
3087c478bd9Sstevel@tonic-gate CK_RV rv;
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&soft_giant_mutex);
3117c478bd9Sstevel@tonic-gate
312a62b4373Sdarrenm rv = finalize_common(B_FALSE, pReserved);
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&soft_giant_mutex);
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate return (rv);
3177c478bd9Sstevel@tonic-gate
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate * finalize_common() does the work for C_Finalize. soft_giant_mutex
3227c478bd9Sstevel@tonic-gate * must be held before calling this function.
3237c478bd9Sstevel@tonic-gate */
3247c478bd9Sstevel@tonic-gate static CK_RV
finalize_common(boolean_t force,CK_VOID_PTR pReserved)325a62b4373Sdarrenm finalize_common(boolean_t force, CK_VOID_PTR pReserved) {
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate CK_RV rv = CKR_OK;
3287c478bd9Sstevel@tonic-gate struct object *delay_free_obj, *tmpo;
3297c478bd9Sstevel@tonic-gate struct session *delay_free_ses, *tmps;
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate if (!softtoken_initialized) {
3327c478bd9Sstevel@tonic-gate return (CKR_CRYPTOKI_NOT_INITIALIZED);
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate
3357c478bd9Sstevel@tonic-gate /* Check to see if pReseved is NULL */
3367c478bd9Sstevel@tonic-gate if (pReserved != NULL) {
3377c478bd9Sstevel@tonic-gate return (CKR_ARGUMENTS_BAD);
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&soft_sessionlist_mutex);
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate * Set all_sessions_closing flag so any access to any
3437c478bd9Sstevel@tonic-gate * existing sessions will be rejected.
3447c478bd9Sstevel@tonic-gate */
3457c478bd9Sstevel@tonic-gate all_sessions_closing = 1;
3467c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&soft_sessionlist_mutex);
3477c478bd9Sstevel@tonic-gate
3487c478bd9Sstevel@tonic-gate /* Delete all the sessions and release the allocated resources */
349a62b4373Sdarrenm rv = soft_delete_all_sessions(force);
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&soft_sessionlist_mutex);
3527c478bd9Sstevel@tonic-gate /* Reset all_sessions_closing flag. */
3537c478bd9Sstevel@tonic-gate all_sessions_closing = 0;
3547c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&soft_sessionlist_mutex);
3557c478bd9Sstevel@tonic-gate
3567c478bd9Sstevel@tonic-gate softtoken_initialized = B_FALSE;
3577c478bd9Sstevel@tonic-gate softtoken_pid = 0;
3587c478bd9Sstevel@tonic-gate
359*05d57413SDan McDonald /*
360*05d57413SDan McDonald * There used to be calls to cleanup libcryptoutil here. Given that
361*05d57413SDan McDonald * libcryptoutil can be linked and invoked independently of PKCS#11,
362*05d57413SDan McDonald * cleaning up libcryptoutil here makes no sense. Decoupling these
363*05d57413SDan McDonald * two also prevent deadlocks and other artificial dependencies.
364*05d57413SDan McDonald */
3657c478bd9Sstevel@tonic-gate
3667c478bd9Sstevel@tonic-gate /* Destroy the session list lock here */
3677c478bd9Sstevel@tonic-gate (void) pthread_mutex_destroy(&soft_sessionlist_mutex);
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate * Destroy token object related stuffs
3717c478bd9Sstevel@tonic-gate * 1. Clean up the token object list
3727c478bd9Sstevel@tonic-gate * 2. Destroy slot mutex
3737c478bd9Sstevel@tonic-gate * 3. Destroy mutex in token_session
3747c478bd9Sstevel@tonic-gate */
3757c478bd9Sstevel@tonic-gate soft_delete_all_in_core_token_objects(ALL_TOKEN);
3767c478bd9Sstevel@tonic-gate (void) pthread_mutex_destroy(&soft_slot.slot_mutex);
37790e0e8c4Sizick (void) pthread_mutex_destroy(&soft_slot.keystore_mutex);
3787c478bd9Sstevel@tonic-gate (void) soft_destroy_token_session();
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate /*
3817c478bd9Sstevel@tonic-gate * free all entries in the delay_freed list
3827c478bd9Sstevel@tonic-gate */
3837c478bd9Sstevel@tonic-gate delay_free_obj = obj_delay_freed.first;
3847c478bd9Sstevel@tonic-gate while (delay_free_obj != NULL) {
3857c478bd9Sstevel@tonic-gate tmpo = delay_free_obj->next;
3867c478bd9Sstevel@tonic-gate free(delay_free_obj);
3877c478bd9Sstevel@tonic-gate delay_free_obj = tmpo;
3887c478bd9Sstevel@tonic-gate }
38990e0e8c4Sizick
39090e0e8c4Sizick soft_slot.keystore_load_status = KEYSTORE_UNINITIALIZED;
3917c478bd9Sstevel@tonic-gate (void) pthread_mutex_destroy(&obj_delay_freed.obj_to_be_free_mutex);
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gate delay_free_ses = ses_delay_freed.first;
3947c478bd9Sstevel@tonic-gate while (delay_free_ses != NULL) {
3957c478bd9Sstevel@tonic-gate tmps = delay_free_ses->next;
3967c478bd9Sstevel@tonic-gate free(delay_free_ses);
3977c478bd9Sstevel@tonic-gate delay_free_ses = tmps;
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate (void) pthread_mutex_destroy(&ses_delay_freed.ses_to_be_free_mutex);
4007c478bd9Sstevel@tonic-gate
4017c478bd9Sstevel@tonic-gate return (rv);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate
40483140133SZdenek Kotala static void
softtoken_init()40583140133SZdenek Kotala softtoken_init()
40683140133SZdenek Kotala {
40783140133SZdenek Kotala /* Children inherit parent's atfork handlers */
40883140133SZdenek Kotala (void) pthread_atfork(softtoken_fork_prepare,
40983140133SZdenek Kotala softtoken_fork_after, softtoken_fork_after);
41083140133SZdenek Kotala }
41183140133SZdenek Kotala
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate * softtoken_fini() function required to make sure complete cleanup
4147c478bd9Sstevel@tonic-gate * is done if softtoken is ever unloaded without a C_Finalize() call.
4157c478bd9Sstevel@tonic-gate */
4167c478bd9Sstevel@tonic-gate static void
softtoken_fini()4177c478bd9Sstevel@tonic-gate softtoken_fini()
4187c478bd9Sstevel@tonic-gate {
4197c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&soft_giant_mutex);
4207c478bd9Sstevel@tonic-gate
4217c478bd9Sstevel@tonic-gate /* if we're not initilized, do not attempt to finalize */
4227c478bd9Sstevel@tonic-gate if (!softtoken_initialized) {
4237c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&soft_giant_mutex);
4247c478bd9Sstevel@tonic-gate return;
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate
427a62b4373Sdarrenm (void) finalize_common(B_TRUE, NULL_PTR);
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&soft_giant_mutex);
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate CK_RV
C_GetInfo(CK_INFO_PTR pInfo)4337c478bd9Sstevel@tonic-gate C_GetInfo(CK_INFO_PTR pInfo)
4347c478bd9Sstevel@tonic-gate {
4357c478bd9Sstevel@tonic-gate if (!softtoken_initialized)
4367c478bd9Sstevel@tonic-gate return (CKR_CRYPTOKI_NOT_INITIALIZED);
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate if (pInfo == NULL) {
4397c478bd9Sstevel@tonic-gate return (CKR_ARGUMENTS_BAD);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate
4427c478bd9Sstevel@tonic-gate /* Provide general information in the provided buffer */
4437c478bd9Sstevel@tonic-gate pInfo->cryptokiVersion.major = CRYPTOKI_VERSION_MAJOR;
4447c478bd9Sstevel@tonic-gate pInfo->cryptokiVersion.minor = CRYPTOKI_VERSION_MINOR;
4457c478bd9Sstevel@tonic-gate (void) strncpy((char *)pInfo->manufacturerID,
4467c478bd9Sstevel@tonic-gate SOFT_MANUFACTURER_ID, 32);
4477c478bd9Sstevel@tonic-gate pInfo->flags = 0;
4487c478bd9Sstevel@tonic-gate (void) strncpy((char *)pInfo->libraryDescription,
4497c478bd9Sstevel@tonic-gate LIBRARY_DESCRIPTION, 32);
4507c478bd9Sstevel@tonic-gate pInfo->libraryVersion.major = LIBRARY_VERSION_MAJOR;
4514ff712c4SValerie Bubb Fenwick pInfo->libraryVersion.minor = LIBRARY_VERSION_MINOR;
4527c478bd9Sstevel@tonic-gate
4537c478bd9Sstevel@tonic-gate return (CKR_OK);
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate CK_RV
C_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR ppFunctionList)4577c478bd9Sstevel@tonic-gate C_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR ppFunctionList)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate if (ppFunctionList == NULL) {
4607c478bd9Sstevel@tonic-gate return (CKR_ARGUMENTS_BAD);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate *ppFunctionList = &functionList;
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate return (CKR_OK);
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate
4687c478bd9Sstevel@tonic-gate /*
4697c478bd9Sstevel@tonic-gate * PKCS#11 states that C_GetFunctionStatus should always return
4707c478bd9Sstevel@tonic-gate * CKR_FUNCTION_NOT_PARALLEL
4717c478bd9Sstevel@tonic-gate */
4727c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4737c478bd9Sstevel@tonic-gate CK_RV
C_GetFunctionStatus(CK_SESSION_HANDLE hSession)4747c478bd9Sstevel@tonic-gate C_GetFunctionStatus(CK_SESSION_HANDLE hSession)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate return (CKR_FUNCTION_NOT_PARALLEL);
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate /*
4807c478bd9Sstevel@tonic-gate * PKCS#11 states that C_CancelFunction should always return
4817c478bd9Sstevel@tonic-gate * CKR_FUNCTION_NOT_PARALLEL
4827c478bd9Sstevel@tonic-gate */
4837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4847c478bd9Sstevel@tonic-gate CK_RV
C_CancelFunction(CK_SESSION_HANDLE hSession)4857c478bd9Sstevel@tonic-gate C_CancelFunction(CK_SESSION_HANDLE hSession)
4867c478bd9Sstevel@tonic-gate {
4877c478bd9Sstevel@tonic-gate return (CKR_FUNCTION_NOT_PARALLEL);
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate /*
4914daf2311Srupertk * Take out all mutexes before fork.
49283140133SZdenek Kotala *
4934daf2311Srupertk * Order:
4944daf2311Srupertk * 1. soft_giant_mutex
4954daf2311Srupertk * 2. soft_sessionlist_mutex
4964daf2311Srupertk * 3. soft_slot.slot_mutex
4974daf2311Srupertk * 4. soft_slot.keystore_mutex
49883140133SZdenek Kotala * 5. token_session mutexes via soft_acquire_all_session_mutexes()
49983140133SZdenek Kotala * 6. all soft_session_list mutexes via soft_acquire_all_session_mutexes()
50083140133SZdenek Kotala * 7. obj_delay_freed.obj_to_be_free_mutex;
50183140133SZdenek Kotala * 8. ses_delay_freed.ses_to_be_free_mutex
5024daf2311Srupertk */
5034daf2311Srupertk void
softtoken_fork_prepare()5044daf2311Srupertk softtoken_fork_prepare()
5054daf2311Srupertk {
5064daf2311Srupertk (void) pthread_mutex_lock(&soft_giant_mutex);
50783140133SZdenek Kotala if (softtoken_initialized) {
5084daf2311Srupertk (void) pthread_mutex_lock(&soft_sessionlist_mutex);
5094daf2311Srupertk (void) pthread_mutex_lock(&soft_slot.slot_mutex);
5104daf2311Srupertk (void) pthread_mutex_lock(&soft_slot.keystore_mutex);
51183140133SZdenek Kotala soft_acquire_all_session_mutexes(&token_session);
51283140133SZdenek Kotala soft_acquire_all_session_mutexes(soft_session_list);
51383140133SZdenek Kotala (void) pthread_mutex_lock(
51483140133SZdenek Kotala &obj_delay_freed.obj_to_be_free_mutex);
51583140133SZdenek Kotala (void) pthread_mutex_lock(
51683140133SZdenek Kotala &ses_delay_freed.ses_to_be_free_mutex);
5174daf2311Srupertk }
5184daf2311Srupertk }
5194daf2311Srupertk
5201f49a79aSZdenek Kotala /*
5211f49a79aSZdenek Kotala * Release in opposite order to softtoken_fork_prepare().
52283140133SZdenek Kotala * Function is used for parent and child.
5231f49a79aSZdenek Kotala */
5244daf2311Srupertk void
softtoken_fork_after()52583140133SZdenek Kotala softtoken_fork_after()
5264daf2311Srupertk {
52783140133SZdenek Kotala if (softtoken_initialized) {
52883140133SZdenek Kotala (void) pthread_mutex_unlock(
52983140133SZdenek Kotala &ses_delay_freed.ses_to_be_free_mutex);
53083140133SZdenek Kotala (void) pthread_mutex_unlock(
53183140133SZdenek Kotala &obj_delay_freed.obj_to_be_free_mutex);
53283140133SZdenek Kotala soft_release_all_session_mutexes(soft_session_list);
53383140133SZdenek Kotala soft_release_all_session_mutexes(&token_session);
5344daf2311Srupertk (void) pthread_mutex_unlock(&soft_slot.keystore_mutex);
5354daf2311Srupertk (void) pthread_mutex_unlock(&soft_slot.slot_mutex);
5364daf2311Srupertk (void) pthread_mutex_unlock(&soft_sessionlist_mutex);
53783140133SZdenek Kotala }
5384daf2311Srupertk (void) pthread_mutex_unlock(&soft_giant_mutex);
5394daf2311Srupertk }
540