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.
2405d57413SDan McDonald  *
2505d57413SDan McDonald  * Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
26*cfcec266SJason King  *
27*cfcec266SJason King  * Copyright 2020 Joyent, Inc.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <strings.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <cryptoutil.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h> /* for pid_t */
347c478bd9Sstevel@tonic-gate #include <pthread.h>
35*cfcec266SJason King #include <stddef.h>
367c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
37*cfcec266SJason King #include <sys/debug.h>
387c478bd9Sstevel@tonic-gate #include "softGlobal.h"
397c478bd9Sstevel@tonic-gate #include "softSession.h"
407c478bd9Sstevel@tonic-gate #include "softObject.h"
417c478bd9Sstevel@tonic-gate #include "softKeystore.h"
427c478bd9Sstevel@tonic-gate #include "softKeystoreUtil.h"
437c478bd9Sstevel@tonic-gate 
4483140133SZdenek Kotala #pragma init(softtoken_init)
457c478bd9Sstevel@tonic-gate #pragma fini(softtoken_fini)
467c478bd9Sstevel@tonic-gate 
4783140133SZdenek Kotala extern soft_session_t token_session; /* for fork handler */
4883140133SZdenek Kotala 
497c478bd9Sstevel@tonic-gate static struct CK_FUNCTION_LIST functionList = {
50f66d273dSizick 	{ 2, 20 },	/* version */
517c478bd9Sstevel@tonic-gate 	C_Initialize,
527c478bd9Sstevel@tonic-gate 	C_Finalize,
537c478bd9Sstevel@tonic-gate 	C_GetInfo,
547c478bd9Sstevel@tonic-gate 	C_GetFunctionList,
557c478bd9Sstevel@tonic-gate 	C_GetSlotList,
567c478bd9Sstevel@tonic-gate 	C_GetSlotInfo,
577c478bd9Sstevel@tonic-gate 	C_GetTokenInfo,
587c478bd9Sstevel@tonic-gate 	C_GetMechanismList,
597c478bd9Sstevel@tonic-gate 	C_GetMechanismInfo,
607c478bd9Sstevel@tonic-gate 	C_InitToken,
617c478bd9Sstevel@tonic-gate 	C_InitPIN,
627c478bd9Sstevel@tonic-gate 	C_SetPIN,
637c478bd9Sstevel@tonic-gate 	C_OpenSession,
647c478bd9Sstevel@tonic-gate 	C_CloseSession,
657c478bd9Sstevel@tonic-gate 	C_CloseAllSessions,
667c478bd9Sstevel@tonic-gate 	C_GetSessionInfo,
677c478bd9Sstevel@tonic-gate 	C_GetOperationState,
687c478bd9Sstevel@tonic-gate 	C_SetOperationState,
697c478bd9Sstevel@tonic-gate 	C_Login,
707c478bd9Sstevel@tonic-gate 	C_Logout,
717c478bd9Sstevel@tonic-gate 	C_CreateObject,
727c478bd9Sstevel@tonic-gate 	C_CopyObject,
737c478bd9Sstevel@tonic-gate 	C_DestroyObject,
747c478bd9Sstevel@tonic-gate 	C_GetObjectSize,
757c478bd9Sstevel@tonic-gate 	C_GetAttributeValue,
767c478bd9Sstevel@tonic-gate 	C_SetAttributeValue,
777c478bd9Sstevel@tonic-gate 	C_FindObjectsInit,
787c478bd9Sstevel@tonic-gate 	C_FindObjects,
797c478bd9Sstevel@tonic-gate 	C_FindObjectsFinal,
807c478bd9Sstevel@tonic-gate 	C_EncryptInit,
817c478bd9Sstevel@tonic-gate 	C_Encrypt,
827c478bd9Sstevel@tonic-gate 	C_EncryptUpdate,
837c478bd9Sstevel@tonic-gate 	C_EncryptFinal,
847c478bd9Sstevel@tonic-gate 	C_DecryptInit,
857c478bd9Sstevel@tonic-gate 	C_Decrypt,
867c478bd9Sstevel@tonic-gate 	C_DecryptUpdate,
877c478bd9Sstevel@tonic-gate 	C_DecryptFinal,
887c478bd9Sstevel@tonic-gate 	C_DigestInit,
897c478bd9Sstevel@tonic-gate 	C_Digest,
907c478bd9Sstevel@tonic-gate 	C_DigestUpdate,
917c478bd9Sstevel@tonic-gate 	C_DigestKey,
927c478bd9Sstevel@tonic-gate 	C_DigestFinal,
937c478bd9Sstevel@tonic-gate 	C_SignInit,
947c478bd9Sstevel@tonic-gate 	C_Sign,
957c478bd9Sstevel@tonic-gate 	C_SignUpdate,
967c478bd9Sstevel@tonic-gate 	C_SignFinal,
977c478bd9Sstevel@tonic-gate 	C_SignRecoverInit,
987c478bd9Sstevel@tonic-gate 	C_SignRecover,
997c478bd9Sstevel@tonic-gate 	C_VerifyInit,
1007c478bd9Sstevel@tonic-gate 	C_Verify,
1017c478bd9Sstevel@tonic-gate 	C_VerifyUpdate,
1027c478bd9Sstevel@tonic-gate 	C_VerifyFinal,
1037c478bd9Sstevel@tonic-gate 	C_VerifyRecoverInit,
1047c478bd9Sstevel@tonic-gate 	C_VerifyRecover,
1057c478bd9Sstevel@tonic-gate 	C_DigestEncryptUpdate,
1067c478bd9Sstevel@tonic-gate 	C_DecryptDigestUpdate,
1077c478bd9Sstevel@tonic-gate 	C_SignEncryptUpdate,
1087c478bd9Sstevel@tonic-gate 	C_DecryptVerifyUpdate,
1097c478bd9Sstevel@tonic-gate 	C_GenerateKey,
1107c478bd9Sstevel@tonic-gate 	C_GenerateKeyPair,
1117c478bd9Sstevel@tonic-gate 	C_WrapKey,
1127c478bd9Sstevel@tonic-gate 	C_UnwrapKey,
1137c478bd9Sstevel@tonic-gate 	C_DeriveKey,
1147c478bd9Sstevel@tonic-gate 	C_SeedRandom,
1157c478bd9Sstevel@tonic-gate 	C_GenerateRandom,
1167c478bd9Sstevel@tonic-gate 	C_GetFunctionStatus,
1177c478bd9Sstevel@tonic-gate 	C_CancelFunction,
1187c478bd9Sstevel@tonic-gate 	C_WaitForSlotEvent
1197c478bd9Sstevel@tonic-gate };
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate boolean_t softtoken_initialized = B_FALSE;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static pid_t softtoken_pid = 0;
1247c478bd9Sstevel@tonic-gate 
125*cfcec266SJason King /*
126*cfcec266SJason King  * Lock ordering in pkcs11_softtoken is as follows:
127*cfcec266SJason King  *	soft_giant_mutex
128*cfcec266SJason King  *	soft_sessionlist_mutex
129*cfcec266SJason King  *	soft_slot.slot_mutex
130*cfcec266SJason King  *	soft_slot.keystore_mutex
131*cfcec266SJason King  *	token session mutexes
132*cfcec266SJason King  *	soft_session_t->session_mutex
133*cfcec266SJason King  *	soft_object_mutex
134*cfcec266SJason King  *	soft_object_t->object_mutex
135*cfcec266SJason King  *	obj_delay_freed.obj_to_be_free_mutex
136*cfcec266SJason King  *	ses_delay_freed.ses_to_br_free_mutex
137*cfcec266SJason King  */
138*cfcec266SJason King 
139*cfcec266SJason King /*
140*cfcec266SJason King  * This mutex protects soft_session_list, all_sessions_closing,
141*cfcec266SJason King  * and soft_session_tree
142*cfcec266SJason King  */
1437c478bd9Sstevel@tonic-gate pthread_mutex_t soft_sessionlist_mutex;
1447c478bd9Sstevel@tonic-gate soft_session_t *soft_session_list = NULL;
145*cfcec266SJason King avl_tree_t soft_session_tree;
146*cfcec266SJason King 
147*cfcec266SJason King /* This protects soft_object_tree */
148*cfcec266SJason King pthread_mutex_t soft_object_mutex;
149*cfcec266SJason King avl_tree_t soft_object_tree;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate int all_sessions_closing = 0;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate slot_t soft_slot;
1547c478bd9Sstevel@tonic-gate obj_to_be_freed_list_t obj_delay_freed;
1557c478bd9Sstevel@tonic-gate ses_to_be_freed_list_t ses_delay_freed;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /* protects softtoken_initialized and access to C_Initialize/C_Finalize */
1587c478bd9Sstevel@tonic-gate pthread_mutex_t soft_giant_mutex = PTHREAD_MUTEX_INITIALIZER;
1597c478bd9Sstevel@tonic-gate 
160a62b4373Sdarrenm static CK_RV finalize_common(boolean_t force, CK_VOID_PTR pReserved);
161*cfcec266SJason King static void softtoken_init(void);
162*cfcec266SJason King static void softtoken_fini(void);
163*cfcec266SJason King static void softtoken_fork_prepare(void);
164*cfcec266SJason King static void softtoken_fork_after(void);
165*cfcec266SJason King static int session_compare(const void *a, const void *b);
166*cfcec266SJason King static int object_compare(const void *a, const void *b);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate CK_RV
C_Initialize(CK_VOID_PTR pInitArgs)1697c478bd9Sstevel@tonic-gate C_Initialize(CK_VOID_PTR pInitArgs)
1707c478bd9Sstevel@tonic-gate {
171*cfcec266SJason King 	pthread_mutexattr_t attr = { 0 };
1727c478bd9Sstevel@tonic-gate 	int initialize_pid;
1737c478bd9Sstevel@tonic-gate 	boolean_t supplied_ok;
1747c478bd9Sstevel@tonic-gate 	CK_RV rv;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * Get lock to insure only one thread enters this
1787c478bd9Sstevel@tonic-gate 	 * function at a time.
1797c478bd9Sstevel@tonic-gate 	 */
1807c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&soft_giant_mutex);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	initialize_pid = getpid();
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (softtoken_initialized) {
1857c478bd9Sstevel@tonic-gate 		if (initialize_pid == softtoken_pid) {
1867c478bd9Sstevel@tonic-gate 			/*
1877c478bd9Sstevel@tonic-gate 			 * This process has called C_Initialize already
1887c478bd9Sstevel@tonic-gate 			 */
1897c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&soft_giant_mutex);
1907c478bd9Sstevel@tonic-gate 			return (CKR_CRYPTOKI_ALREADY_INITIALIZED);
1917c478bd9Sstevel@tonic-gate 		} else {
1927c478bd9Sstevel@tonic-gate 			/*
1937c478bd9Sstevel@tonic-gate 			 * A fork has happened and the child is
1947c478bd9Sstevel@tonic-gate 			 * reinitializing.  Do a finalize_common to close
1957c478bd9Sstevel@tonic-gate 			 * out any state from the parent, and then
1967c478bd9Sstevel@tonic-gate 			 * continue on.
1977c478bd9Sstevel@tonic-gate 			 */
198a62b4373Sdarrenm 			(void) finalize_common(B_TRUE, NULL);
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	if (pInitArgs != NULL) {
2037c478bd9Sstevel@tonic-gate 		CK_C_INITIALIZE_ARGS *initargs1 =
2047c478bd9Sstevel@tonic-gate 		    (CK_C_INITIALIZE_ARGS *) pInitArgs;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 		/* pReserved must be NULL */
2077c478bd9Sstevel@tonic-gate 		if (initargs1->pReserved != NULL) {
2087c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&soft_giant_mutex);
2097c478bd9Sstevel@tonic-gate 			return (CKR_ARGUMENTS_BAD);
2107c478bd9Sstevel@tonic-gate 		}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		/*
2137c478bd9Sstevel@tonic-gate 		 * ALL supplied function pointers need to have the value
2147c478bd9Sstevel@tonic-gate 		 * either NULL or non-NULL.
2157c478bd9Sstevel@tonic-gate 		 */
2167c478bd9Sstevel@tonic-gate 		supplied_ok = (initargs1->CreateMutex == NULL &&
2177c478bd9Sstevel@tonic-gate 		    initargs1->DestroyMutex == NULL &&
2187c478bd9Sstevel@tonic-gate 		    initargs1->LockMutex == NULL &&
2197c478bd9Sstevel@tonic-gate 		    initargs1->UnlockMutex == NULL) ||
2207c478bd9Sstevel@tonic-gate 		    (initargs1->CreateMutex != NULL &&
2217c478bd9Sstevel@tonic-gate 		    initargs1->DestroyMutex != NULL &&
2227c478bd9Sstevel@tonic-gate 		    initargs1->LockMutex != NULL &&
2237c478bd9Sstevel@tonic-gate 		    initargs1->UnlockMutex != NULL);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		if (!supplied_ok) {
2267c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&soft_giant_mutex);
2277c478bd9Sstevel@tonic-gate 			return (CKR_ARGUMENTS_BAD);
2287c478bd9Sstevel@tonic-gate 		}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 		/*
2317c478bd9Sstevel@tonic-gate 		 * When the CKF_OS_LOCKING_OK flag isn't set and mutex
2327c478bd9Sstevel@tonic-gate 		 * function pointers are supplied by an application,
2337c478bd9Sstevel@tonic-gate 		 * return an error.  We must be able to use our own primitives.
2347c478bd9Sstevel@tonic-gate 		 */
2357c478bd9Sstevel@tonic-gate 		if (!(initargs1->flags & CKF_OS_LOCKING_OK) &&
2367c478bd9Sstevel@tonic-gate 		    (initargs1->CreateMutex != NULL)) {
2377c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&soft_giant_mutex);
2387c478bd9Sstevel@tonic-gate 			return (CKR_CANT_LOCK);
2397c478bd9Sstevel@tonic-gate 		}
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
242*cfcec266SJason King 	if (pthread_mutexattr_init(&attr) != 0) {
243*cfcec266SJason King 		(void) pthread_mutex_unlock(&soft_giant_mutex);
244*cfcec266SJason King 		return (CKR_HOST_MEMORY);
245*cfcec266SJason King 	}
246*cfcec266SJason King 
247*cfcec266SJason King 	VERIFY0(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
248*cfcec266SJason King 
2497c478bd9Sstevel@tonic-gate 	/* Initialize the session list lock */
250*cfcec266SJason King 	if (pthread_mutex_init(&soft_sessionlist_mutex, &attr) != 0) {
251*cfcec266SJason King 		(void) pthread_mutexattr_destroy(&attr);
2527c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&soft_giant_mutex);
2537c478bd9Sstevel@tonic-gate 		return (CKR_CANT_LOCK);
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 
256*cfcec266SJason King 	if (pthread_mutex_init(&soft_object_mutex, &attr) != 0) {
257*cfcec266SJason King 		(void) pthread_mutexattr_destroy(&attr);
258*cfcec266SJason King 		(void) pthread_mutex_unlock(&soft_giant_mutex);
259*cfcec266SJason King 		return (CKR_CANT_LOCK);
260*cfcec266SJason King 	}
261*cfcec266SJason King 
262*cfcec266SJason King 	VERIFY0(pthread_mutexattr_destroy(&attr));
263*cfcec266SJason King 
2647c478bd9Sstevel@tonic-gate 	/*
2657c478bd9Sstevel@tonic-gate 	 * token object related initialization
2667c478bd9Sstevel@tonic-gate 	 */
2677c478bd9Sstevel@tonic-gate 	soft_slot.authenticated = 0;
2687c478bd9Sstevel@tonic-gate 	soft_slot.userpin_change_needed = 0;
2697c478bd9Sstevel@tonic-gate 	soft_slot.token_object_list = NULL;
27090e0e8c4Sizick 	soft_slot.keystore_load_status = KEYSTORE_UNINITIALIZED;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if ((rv = soft_init_token_session()) != CKR_OK) {
27383140133SZdenek Kotala 		(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
2747c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&soft_giant_mutex);
2757c478bd9Sstevel@tonic-gate 		return (rv);
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/* Initialize the slot lock */
2797c478bd9Sstevel@tonic-gate 	if (pthread_mutex_init(&soft_slot.slot_mutex, NULL) != 0) {
28083140133SZdenek Kotala 		(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
2817c478bd9Sstevel@tonic-gate 		(void) soft_destroy_token_session();
2827c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&soft_giant_mutex);
2837c478bd9Sstevel@tonic-gate 		return (CKR_CANT_LOCK);
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
28690e0e8c4Sizick 	/* Initialize the keystore lock */
28790e0e8c4Sizick 	if (pthread_mutex_init(&soft_slot.keystore_mutex, NULL) != 0) {
28883140133SZdenek Kotala 		(void) pthread_mutex_destroy(&soft_slot.slot_mutex);
28983140133SZdenek Kotala 		(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
29083140133SZdenek Kotala 		(void) soft_destroy_token_session();
29190e0e8c4Sizick 		(void) pthread_mutex_unlock(&soft_giant_mutex);
29290e0e8c4Sizick 		return (CKR_CANT_LOCK);
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	/* Initialize the object_to_be_freed list */
29683140133SZdenek Kotala 	if (pthread_mutex_init(&obj_delay_freed.obj_to_be_free_mutex, NULL)
29783140133SZdenek Kotala 	    != 0) {
29883140133SZdenek Kotala 		(void) pthread_mutex_destroy(&soft_slot.keystore_mutex);
29983140133SZdenek Kotala 		(void) pthread_mutex_destroy(&soft_slot.slot_mutex);
30083140133SZdenek Kotala 		(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
30183140133SZdenek Kotala 		(void) soft_destroy_token_session();
30283140133SZdenek Kotala 		(void) pthread_mutex_unlock(&soft_giant_mutex);
30383140133SZdenek Kotala 		return (CKR_CANT_LOCK);
30483140133SZdenek Kotala 	}
3057c478bd9Sstevel@tonic-gate 	obj_delay_freed.count = 0;
3067c478bd9Sstevel@tonic-gate 	obj_delay_freed.first = NULL;
3077c478bd9Sstevel@tonic-gate 	obj_delay_freed.last = NULL;
3087c478bd9Sstevel@tonic-gate 
30983140133SZdenek Kotala 	if (pthread_mutex_init(&ses_delay_freed.ses_to_be_free_mutex, NULL)
31083140133SZdenek Kotala 	    != 0) {
31183140133SZdenek Kotala 		(void) pthread_mutex_destroy(
31283140133SZdenek Kotala 		    &obj_delay_freed.obj_to_be_free_mutex);
31383140133SZdenek Kotala 		(void) pthread_mutex_destroy(&soft_slot.keystore_mutex);
31483140133SZdenek Kotala 		(void) pthread_mutex_destroy(&soft_slot.slot_mutex);
31583140133SZdenek Kotala 		(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
31683140133SZdenek Kotala 		(void) soft_destroy_token_session();
31783140133SZdenek Kotala 		(void) pthread_mutex_unlock(&soft_giant_mutex);
31883140133SZdenek Kotala 		return (CKR_CANT_LOCK);
31983140133SZdenek Kotala 	}
3207c478bd9Sstevel@tonic-gate 	ses_delay_freed.count = 0;
3217c478bd9Sstevel@tonic-gate 	ses_delay_freed.first = NULL;
3227c478bd9Sstevel@tonic-gate 	ses_delay_freed.last = NULL;
3237c478bd9Sstevel@tonic-gate 
324b5a2d845SHai-May Chao 	if (rv != CKR_OK) {
325b5a2d845SHai-May Chao 		(void) pthread_mutex_destroy(
326b5a2d845SHai-May Chao 		    &ses_delay_freed.ses_to_be_free_mutex);
327b5a2d845SHai-May Chao 		(void) pthread_mutex_destroy(
328b5a2d845SHai-May Chao 		    &obj_delay_freed.obj_to_be_free_mutex);
329b5a2d845SHai-May Chao 		(void) pthread_mutex_destroy(&soft_slot.keystore_mutex);
330b5a2d845SHai-May Chao 		(void) pthread_mutex_destroy(&soft_slot.slot_mutex);
331b5a2d845SHai-May Chao 		(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
332b5a2d845SHai-May Chao 		(void) soft_destroy_token_session();
333b5a2d845SHai-May Chao 		(void) pthread_mutex_unlock(&soft_giant_mutex);
334b5a2d845SHai-May Chao 		return (CKR_FUNCTION_FAILED);
335b5a2d845SHai-May Chao 	}
336b5a2d845SHai-May Chao 
33783140133SZdenek Kotala 	softtoken_pid = initialize_pid;
33883140133SZdenek Kotala 	softtoken_initialized = B_TRUE;
33983140133SZdenek Kotala 	(void) pthread_mutex_unlock(&soft_giant_mutex);
34083140133SZdenek Kotala 
34183140133SZdenek Kotala 	return (CKR_OK);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  * C_Finalize is a wrapper around finalize_common. The
3467c478bd9Sstevel@tonic-gate  * soft_giant_mutex should be locked by C_Finalize().
3477c478bd9Sstevel@tonic-gate  */
3487c478bd9Sstevel@tonic-gate CK_RV
C_Finalize(CK_VOID_PTR pReserved)3497c478bd9Sstevel@tonic-gate C_Finalize(CK_VOID_PTR pReserved)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	CK_RV rv;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&soft_giant_mutex);
3557c478bd9Sstevel@tonic-gate 
356a62b4373Sdarrenm 	rv = finalize_common(B_FALSE, pReserved);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&soft_giant_mutex);
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	return (rv);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate  * finalize_common() does the work for C_Finalize.  soft_giant_mutex
3667c478bd9Sstevel@tonic-gate  * must be held before calling this function.
3677c478bd9Sstevel@tonic-gate  */
3687c478bd9Sstevel@tonic-gate static CK_RV
finalize_common(boolean_t force,CK_VOID_PTR pReserved)369*cfcec266SJason King finalize_common(boolean_t force, CK_VOID_PTR pReserved)
370*cfcec266SJason King {
3717c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
3727c478bd9Sstevel@tonic-gate 	struct object *delay_free_obj, *tmpo;
3737c478bd9Sstevel@tonic-gate 	struct session *delay_free_ses, *tmps;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	if (!softtoken_initialized) {
3767c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/* Check to see if pReseved is NULL */
3807c478bd9Sstevel@tonic-gate 	if (pReserved != NULL) {
3817c478bd9Sstevel@tonic-gate 		return (CKR_ARGUMENTS_BAD);
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&soft_sessionlist_mutex);
3857c478bd9Sstevel@tonic-gate 	/*
3867c478bd9Sstevel@tonic-gate 	 * Set all_sessions_closing flag so any access to any
3877c478bd9Sstevel@tonic-gate 	 * existing sessions will be rejected.
3887c478bd9Sstevel@tonic-gate 	 */
3897c478bd9Sstevel@tonic-gate 	all_sessions_closing = 1;
3907c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&soft_sessionlist_mutex);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	/* Delete all the sessions and release the allocated resources */
393a62b4373Sdarrenm 	rv = soft_delete_all_sessions(force);
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&soft_sessionlist_mutex);
3967c478bd9Sstevel@tonic-gate 	/* Reset all_sessions_closing flag. */
3977c478bd9Sstevel@tonic-gate 	all_sessions_closing = 0;
3987c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&soft_sessionlist_mutex);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	softtoken_initialized = B_FALSE;
4017c478bd9Sstevel@tonic-gate 	softtoken_pid = 0;
4027c478bd9Sstevel@tonic-gate 
40305d57413SDan McDonald 	/*
40405d57413SDan McDonald 	 * There used to be calls to cleanup libcryptoutil here.  Given that
40505d57413SDan McDonald 	 * libcryptoutil can be linked and invoked independently of PKCS#11,
40605d57413SDan McDonald 	 * cleaning up libcryptoutil here makes no sense.  Decoupling these
40705d57413SDan McDonald 	 * two also prevent deadlocks and other artificial dependencies.
40805d57413SDan McDonald 	 */
4097c478bd9Sstevel@tonic-gate 
410*cfcec266SJason King 	(void) pthread_mutex_destroy(&soft_object_mutex);
411*cfcec266SJason King 
4127c478bd9Sstevel@tonic-gate 	/* Destroy the session list lock here */
4137c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	/*
4167c478bd9Sstevel@tonic-gate 	 * Destroy token object related stuffs
4177c478bd9Sstevel@tonic-gate 	 * 1. Clean up the token object list
4187c478bd9Sstevel@tonic-gate 	 * 2. Destroy slot mutex
4197c478bd9Sstevel@tonic-gate 	 * 3. Destroy mutex in token_session
4207c478bd9Sstevel@tonic-gate 	 */
4217c478bd9Sstevel@tonic-gate 	soft_delete_all_in_core_token_objects(ALL_TOKEN);
4227c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_destroy(&soft_slot.slot_mutex);
42390e0e8c4Sizick 	(void) pthread_mutex_destroy(&soft_slot.keystore_mutex);
4247c478bd9Sstevel@tonic-gate 	(void) soft_destroy_token_session();
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * free all entries in the delay_freed list
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 	delay_free_obj = obj_delay_freed.first;
4307c478bd9Sstevel@tonic-gate 	while (delay_free_obj != NULL) {
4317c478bd9Sstevel@tonic-gate 		tmpo = delay_free_obj->next;
4327c478bd9Sstevel@tonic-gate 		free(delay_free_obj);
4337c478bd9Sstevel@tonic-gate 		delay_free_obj = tmpo;
4347c478bd9Sstevel@tonic-gate 	}
43590e0e8c4Sizick 
43690e0e8c4Sizick 	soft_slot.keystore_load_status = KEYSTORE_UNINITIALIZED;
4377c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_destroy(&obj_delay_freed.obj_to_be_free_mutex);
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	delay_free_ses = ses_delay_freed.first;
4407c478bd9Sstevel@tonic-gate 	while (delay_free_ses != NULL) {
4417c478bd9Sstevel@tonic-gate 		tmps = delay_free_ses->next;
4427c478bd9Sstevel@tonic-gate 		free(delay_free_ses);
4437c478bd9Sstevel@tonic-gate 		delay_free_ses = tmps;
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_destroy(&ses_delay_freed.ses_to_be_free_mutex);
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	return (rv);
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate 
45083140133SZdenek Kotala static void
softtoken_init()45183140133SZdenek Kotala softtoken_init()
45283140133SZdenek Kotala {
453*cfcec266SJason King 	avl_create(&soft_session_tree, session_compare, sizeof (soft_session_t),
454*cfcec266SJason King 	    offsetof(soft_session_t, node));
455*cfcec266SJason King 
456*cfcec266SJason King 	avl_create(&soft_object_tree, object_compare, sizeof (soft_object_t),
457*cfcec266SJason King 	    offsetof(soft_object_t, node));
458*cfcec266SJason King 
45983140133SZdenek Kotala 	/* Children inherit parent's atfork handlers */
46083140133SZdenek Kotala 	(void) pthread_atfork(softtoken_fork_prepare,
46183140133SZdenek Kotala 	    softtoken_fork_after, softtoken_fork_after);
46283140133SZdenek Kotala }
46383140133SZdenek Kotala 
4647c478bd9Sstevel@tonic-gate /*
4657c478bd9Sstevel@tonic-gate  * softtoken_fini() function required to make sure complete cleanup
4667c478bd9Sstevel@tonic-gate  * is done if softtoken is ever unloaded without a C_Finalize() call.
4677c478bd9Sstevel@tonic-gate  */
4687c478bd9Sstevel@tonic-gate static void
softtoken_fini()4697c478bd9Sstevel@tonic-gate softtoken_fini()
4707c478bd9Sstevel@tonic-gate {
4717c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&soft_giant_mutex);
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	/* if we're not initilized, do not attempt to finalize */
4747c478bd9Sstevel@tonic-gate 	if (!softtoken_initialized) {
4757c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&soft_giant_mutex);
4767c478bd9Sstevel@tonic-gate 		return;
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 
479a62b4373Sdarrenm 	(void) finalize_common(B_TRUE, NULL_PTR);
4807c478bd9Sstevel@tonic-gate 
481*cfcec266SJason King 	avl_destroy(&soft_object_tree);
482*cfcec266SJason King 	avl_destroy(&soft_session_tree);
483*cfcec266SJason King 
4847c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&soft_giant_mutex);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate CK_RV
C_GetInfo(CK_INFO_PTR pInfo)4887c478bd9Sstevel@tonic-gate C_GetInfo(CK_INFO_PTR pInfo)
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate 	if (!softtoken_initialized)
4917c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (pInfo == NULL) {
4947c478bd9Sstevel@tonic-gate 		return (CKR_ARGUMENTS_BAD);
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	/* Provide general information in the provided buffer */
4987c478bd9Sstevel@tonic-gate 	pInfo->cryptokiVersion.major = CRYPTOKI_VERSION_MAJOR;
4997c478bd9Sstevel@tonic-gate 	pInfo->cryptokiVersion.minor = CRYPTOKI_VERSION_MINOR;
5007c478bd9Sstevel@tonic-gate 	(void) strncpy((char *)pInfo->manufacturerID,
5017c478bd9Sstevel@tonic-gate 	    SOFT_MANUFACTURER_ID, 32);
5027c478bd9Sstevel@tonic-gate 	pInfo->flags = 0;
5037c478bd9Sstevel@tonic-gate 	(void) strncpy((char *)pInfo->libraryDescription,
5047c478bd9Sstevel@tonic-gate 	    LIBRARY_DESCRIPTION, 32);
5057c478bd9Sstevel@tonic-gate 	pInfo->libraryVersion.major = LIBRARY_VERSION_MAJOR;
5064ff712c4SValerie Bubb Fenwick 	pInfo->libraryVersion.minor = LIBRARY_VERSION_MINOR;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	return (CKR_OK);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate CK_RV
C_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR ppFunctionList)5127c478bd9Sstevel@tonic-gate C_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR ppFunctionList)
5137c478bd9Sstevel@tonic-gate {
5147c478bd9Sstevel@tonic-gate 	if (ppFunctionList == NULL) {
5157c478bd9Sstevel@tonic-gate 		return (CKR_ARGUMENTS_BAD);
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	*ppFunctionList = &functionList;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	return (CKR_OK);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate /*
5247c478bd9Sstevel@tonic-gate  * PKCS#11 states that C_GetFunctionStatus should always return
5257c478bd9Sstevel@tonic-gate  * CKR_FUNCTION_NOT_PARALLEL
5267c478bd9Sstevel@tonic-gate  */
5277c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5287c478bd9Sstevel@tonic-gate CK_RV
C_GetFunctionStatus(CK_SESSION_HANDLE hSession)5297c478bd9Sstevel@tonic-gate C_GetFunctionStatus(CK_SESSION_HANDLE hSession)
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate 	return (CKR_FUNCTION_NOT_PARALLEL);
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate /*
5357c478bd9Sstevel@tonic-gate  * PKCS#11 states that C_CancelFunction should always return
5367c478bd9Sstevel@tonic-gate  * CKR_FUNCTION_NOT_PARALLEL
5377c478bd9Sstevel@tonic-gate  */
5387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5397c478bd9Sstevel@tonic-gate CK_RV
C_CancelFunction(CK_SESSION_HANDLE hSession)5407c478bd9Sstevel@tonic-gate C_CancelFunction(CK_SESSION_HANDLE hSession)
5417c478bd9Sstevel@tonic-gate {
5427c478bd9Sstevel@tonic-gate 	return (CKR_FUNCTION_NOT_PARALLEL);
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate 
5454daf2311Srupertk /*
5464daf2311Srupertk  * Take out all mutexes before fork.
54783140133SZdenek Kotala  *
5484daf2311Srupertk  * Order:
5494daf2311Srupertk  * 1. soft_giant_mutex
5504daf2311Srupertk  * 2. soft_sessionlist_mutex
5514daf2311Srupertk  * 3. soft_slot.slot_mutex
5524daf2311Srupertk  * 4. soft_slot.keystore_mutex
55383140133SZdenek Kotala  * 5. token_session mutexes via soft_acquire_all_session_mutexes()
55483140133SZdenek Kotala  * 6. all soft_session_list mutexes via soft_acquire_all_session_mutexes()
55583140133SZdenek Kotala  * 7. obj_delay_freed.obj_to_be_free_mutex;
55683140133SZdenek Kotala  * 8. ses_delay_freed.ses_to_be_free_mutex
5574daf2311Srupertk  */
5584daf2311Srupertk void
softtoken_fork_prepare()5594daf2311Srupertk softtoken_fork_prepare()
5604daf2311Srupertk {
5614daf2311Srupertk 	(void) pthread_mutex_lock(&soft_giant_mutex);
56283140133SZdenek Kotala 	if (softtoken_initialized) {
56383140133SZdenek Kotala 		(void) pthread_mutex_lock(&soft_sessionlist_mutex);
56483140133SZdenek Kotala 		(void) pthread_mutex_lock(&soft_slot.slot_mutex);
56583140133SZdenek Kotala 		(void) pthread_mutex_lock(&soft_slot.keystore_mutex);
56683140133SZdenek Kotala 		soft_acquire_all_session_mutexes(&token_session);
56783140133SZdenek Kotala 		soft_acquire_all_session_mutexes(soft_session_list);
568*cfcec266SJason King 		VERIFY0(pthread_mutex_lock(&soft_object_mutex));
56983140133SZdenek Kotala 		(void) pthread_mutex_lock(
57083140133SZdenek Kotala 		    &obj_delay_freed.obj_to_be_free_mutex);
57183140133SZdenek Kotala 		(void) pthread_mutex_lock(
57283140133SZdenek Kotala 		    &ses_delay_freed.ses_to_be_free_mutex);
57383140133SZdenek Kotala 	}
5744daf2311Srupertk }
5754daf2311Srupertk 
5761f49a79aSZdenek Kotala /*
5771f49a79aSZdenek Kotala  * Release in opposite order to softtoken_fork_prepare().
57883140133SZdenek Kotala  * Function is used for parent and child.
5791f49a79aSZdenek Kotala  */
5804daf2311Srupertk void
softtoken_fork_after()58183140133SZdenek Kotala softtoken_fork_after()
5824daf2311Srupertk {
58383140133SZdenek Kotala 	if (softtoken_initialized) {
58483140133SZdenek Kotala 		(void) pthread_mutex_unlock(
58583140133SZdenek Kotala 		    &ses_delay_freed.ses_to_be_free_mutex);
58683140133SZdenek Kotala 		(void) pthread_mutex_unlock(
58783140133SZdenek Kotala 		    &obj_delay_freed.obj_to_be_free_mutex);
588*cfcec266SJason King 		VERIFY0(pthread_mutex_unlock(&soft_object_mutex));
58983140133SZdenek Kotala 		soft_release_all_session_mutexes(soft_session_list);
59083140133SZdenek Kotala 		soft_release_all_session_mutexes(&token_session);
59183140133SZdenek Kotala 		(void) pthread_mutex_unlock(&soft_slot.keystore_mutex);
59283140133SZdenek Kotala 		(void) pthread_mutex_unlock(&soft_slot.slot_mutex);
59383140133SZdenek Kotala 		(void) pthread_mutex_unlock(&soft_sessionlist_mutex);
59483140133SZdenek Kotala 	}
5954daf2311Srupertk 	(void) pthread_mutex_unlock(&soft_giant_mutex);
5964daf2311Srupertk }
597*cfcec266SJason King 
598*cfcec266SJason King static int
session_compare(const void * a,const void * b)599*cfcec266SJason King session_compare(const void *a, const void *b)
600*cfcec266SJason King {
601*cfcec266SJason King 	const soft_session_t *l = a;
602*cfcec266SJason King 	const soft_session_t *r = b;
603*cfcec266SJason King 
604*cfcec266SJason King 	if (l->handle < r->handle)
605*cfcec266SJason King 		return (-1);
606*cfcec266SJason King 	if (l->handle > r->handle)
607*cfcec266SJason King 		return (1);
608*cfcec266SJason King 	return (0);
609*cfcec266SJason King }
610*cfcec266SJason King 
611*cfcec266SJason King static int
object_compare(const void * a,const void * b)612*cfcec266SJason King object_compare(const void *a, const void *b)
613*cfcec266SJason King {
614*cfcec266SJason King 	const soft_object_t *l = a;
615*cfcec266SJason King 	const soft_object_t *r = b;
616*cfcec266SJason King 
617*cfcec266SJason King 	if (l->handle < r->handle)
618*cfcec266SJason King 		return (-1);
619*cfcec266SJason King 	if (l->handle > r->handle)
620*cfcec266SJason King 		return (1);
621*cfcec266SJason King 	return (0);
622*cfcec266SJason King }
623