xref: /titanic_51/usr/src/lib/pkcs11/pkcs11_softtoken/common/softGeneral.c (revision 7b3411b6ce9d5ba373d2e36a6c8db48b2dfb74b7)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <strings.h>
27 #include <errno.h>
28 #include <cryptoutil.h>
29 #include <unistd.h> /* for pid_t */
30 #include <pthread.h>
31 #include <security/cryptoki.h>
32 #include "softGlobal.h"
33 #include "softSession.h"
34 #include "softObject.h"
35 #include "softKeystore.h"
36 #include "softKeystoreUtil.h"
37 
38 #pragma init(softtoken_init)
39 #pragma fini(softtoken_fini)
40 
41 extern soft_session_t token_session; /* for fork handler */
42 
43 static struct CK_FUNCTION_LIST functionList = {
44 	{ 2, 20 },	/* version */
45 	C_Initialize,
46 	C_Finalize,
47 	C_GetInfo,
48 	C_GetFunctionList,
49 	C_GetSlotList,
50 	C_GetSlotInfo,
51 	C_GetTokenInfo,
52 	C_GetMechanismList,
53 	C_GetMechanismInfo,
54 	C_InitToken,
55 	C_InitPIN,
56 	C_SetPIN,
57 	C_OpenSession,
58 	C_CloseSession,
59 	C_CloseAllSessions,
60 	C_GetSessionInfo,
61 	C_GetOperationState,
62 	C_SetOperationState,
63 	C_Login,
64 	C_Logout,
65 	C_CreateObject,
66 	C_CopyObject,
67 	C_DestroyObject,
68 	C_GetObjectSize,
69 	C_GetAttributeValue,
70 	C_SetAttributeValue,
71 	C_FindObjectsInit,
72 	C_FindObjects,
73 	C_FindObjectsFinal,
74 	C_EncryptInit,
75 	C_Encrypt,
76 	C_EncryptUpdate,
77 	C_EncryptFinal,
78 	C_DecryptInit,
79 	C_Decrypt,
80 	C_DecryptUpdate,
81 	C_DecryptFinal,
82 	C_DigestInit,
83 	C_Digest,
84 	C_DigestUpdate,
85 	C_DigestKey,
86 	C_DigestFinal,
87 	C_SignInit,
88 	C_Sign,
89 	C_SignUpdate,
90 	C_SignFinal,
91 	C_SignRecoverInit,
92 	C_SignRecover,
93 	C_VerifyInit,
94 	C_Verify,
95 	C_VerifyUpdate,
96 	C_VerifyFinal,
97 	C_VerifyRecoverInit,
98 	C_VerifyRecover,
99 	C_DigestEncryptUpdate,
100 	C_DecryptDigestUpdate,
101 	C_SignEncryptUpdate,
102 	C_DecryptVerifyUpdate,
103 	C_GenerateKey,
104 	C_GenerateKeyPair,
105 	C_WrapKey,
106 	C_UnwrapKey,
107 	C_DeriveKey,
108 	C_SeedRandom,
109 	C_GenerateRandom,
110 	C_GetFunctionStatus,
111 	C_CancelFunction,
112 	C_WaitForSlotEvent
113 };
114 
115 boolean_t softtoken_initialized = B_FALSE;
116 
117 static pid_t softtoken_pid = 0;
118 
119 /* This mutex protects soft_session_list, all_sessions_closing */
120 pthread_mutex_t soft_sessionlist_mutex;
121 soft_session_t *soft_session_list = NULL;
122 
123 int all_sessions_closing = 0;
124 
125 slot_t soft_slot;
126 obj_to_be_freed_list_t obj_delay_freed;
127 ses_to_be_freed_list_t ses_delay_freed;
128 
129 /* protects softtoken_initialized and access to C_Initialize/C_Finalize */
130 pthread_mutex_t soft_giant_mutex = PTHREAD_MUTEX_INITIALIZER;
131 
132 static CK_RV finalize_common(boolean_t force, CK_VOID_PTR pReserved);
133 static void softtoken_init();
134 static void softtoken_fini();
135 static void softtoken_fork_prepare();
136 static void softtoken_fork_after();
137 
138 CK_RV
139 C_Initialize(CK_VOID_PTR pInitArgs)
140 {
141 
142 	int initialize_pid;
143 	boolean_t supplied_ok;
144 	CK_RV rv;
145 
146 	/*
147 	 * Get lock to insure only one thread enters this
148 	 * function at a time.
149 	 */
150 	(void) pthread_mutex_lock(&soft_giant_mutex);
151 
152 	initialize_pid = getpid();
153 
154 	if (softtoken_initialized) {
155 		if (initialize_pid == softtoken_pid) {
156 			/*
157 			 * This process has called C_Initialize already
158 			 */
159 			(void) pthread_mutex_unlock(&soft_giant_mutex);
160 			return (CKR_CRYPTOKI_ALREADY_INITIALIZED);
161 		} else {
162 			/*
163 			 * A fork has happened and the child is
164 			 * reinitializing.  Do a finalize_common to close
165 			 * out any state from the parent, and then
166 			 * continue on.
167 			 */
168 			(void) finalize_common(B_TRUE, NULL);
169 		}
170 	}
171 
172 	if (pInitArgs != NULL) {
173 		CK_C_INITIALIZE_ARGS *initargs1 =
174 		    (CK_C_INITIALIZE_ARGS *) pInitArgs;
175 
176 		/* pReserved must be NULL */
177 		if (initargs1->pReserved != NULL) {
178 			(void) pthread_mutex_unlock(&soft_giant_mutex);
179 			return (CKR_ARGUMENTS_BAD);
180 		}
181 
182 		/*
183 		 * ALL supplied function pointers need to have the value
184 		 * either NULL or non-NULL.
185 		 */
186 		supplied_ok = (initargs1->CreateMutex == NULL &&
187 		    initargs1->DestroyMutex == NULL &&
188 		    initargs1->LockMutex == NULL &&
189 		    initargs1->UnlockMutex == NULL) ||
190 		    (initargs1->CreateMutex != NULL &&
191 		    initargs1->DestroyMutex != NULL &&
192 		    initargs1->LockMutex != NULL &&
193 		    initargs1->UnlockMutex != NULL);
194 
195 		if (!supplied_ok) {
196 			(void) pthread_mutex_unlock(&soft_giant_mutex);
197 			return (CKR_ARGUMENTS_BAD);
198 		}
199 
200 		/*
201 		 * When the CKF_OS_LOCKING_OK flag isn't set and mutex
202 		 * function pointers are supplied by an application,
203 		 * return an error.  We must be able to use our own primitives.
204 		 */
205 		if (!(initargs1->flags & CKF_OS_LOCKING_OK) &&
206 		    (initargs1->CreateMutex != NULL)) {
207 			(void) pthread_mutex_unlock(&soft_giant_mutex);
208 			return (CKR_CANT_LOCK);
209 		}
210 	}
211 
212 	/* Initialize the session list lock */
213 	if (pthread_mutex_init(&soft_sessionlist_mutex, NULL) != 0) {
214 		(void) pthread_mutex_unlock(&soft_giant_mutex);
215 		return (CKR_CANT_LOCK);
216 	}
217 
218 	/*
219 	 * token object related initialization
220 	 */
221 	soft_slot.authenticated = 0;
222 	soft_slot.userpin_change_needed = 0;
223 	soft_slot.token_object_list = NULL;
224 	soft_slot.keystore_load_status = KEYSTORE_UNINITIALIZED;
225 
226 	if ((rv = soft_init_token_session()) != CKR_OK) {
227 		(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
228 		(void) pthread_mutex_unlock(&soft_giant_mutex);
229 		return (rv);
230 	}
231 
232 	/* Initialize the slot lock */
233 	if (pthread_mutex_init(&soft_slot.slot_mutex, NULL) != 0) {
234 		(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
235 		(void) soft_destroy_token_session();
236 		(void) pthread_mutex_unlock(&soft_giant_mutex);
237 		return (CKR_CANT_LOCK);
238 	}
239 
240 	/* Initialize the keystore lock */
241 	if (pthread_mutex_init(&soft_slot.keystore_mutex, NULL) != 0) {
242 		(void) pthread_mutex_destroy(&soft_slot.slot_mutex);
243 		(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
244 		(void) soft_destroy_token_session();
245 		(void) pthread_mutex_unlock(&soft_giant_mutex);
246 		return (CKR_CANT_LOCK);
247 	}
248 
249 	/* Initialize the object_to_be_freed list */
250 	if (pthread_mutex_init(&obj_delay_freed.obj_to_be_free_mutex, NULL)
251 	    != 0) {
252 		(void) pthread_mutex_destroy(&soft_slot.keystore_mutex);
253 		(void) pthread_mutex_destroy(&soft_slot.slot_mutex);
254 		(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
255 		(void) soft_destroy_token_session();
256 		(void) pthread_mutex_unlock(&soft_giant_mutex);
257 		return (CKR_CANT_LOCK);
258 	}
259 	obj_delay_freed.count = 0;
260 	obj_delay_freed.first = NULL;
261 	obj_delay_freed.last = NULL;
262 
263 	if (pthread_mutex_init(&ses_delay_freed.ses_to_be_free_mutex, NULL)
264 	    != 0) {
265 		(void) pthread_mutex_destroy(
266 		    &obj_delay_freed.obj_to_be_free_mutex);
267 		(void) pthread_mutex_destroy(&soft_slot.keystore_mutex);
268 		(void) pthread_mutex_destroy(&soft_slot.slot_mutex);
269 		(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
270 		(void) soft_destroy_token_session();
271 		(void) pthread_mutex_unlock(&soft_giant_mutex);
272 		return (CKR_CANT_LOCK);
273 	}
274 	ses_delay_freed.count = 0;
275 	ses_delay_freed.first = NULL;
276 	ses_delay_freed.last = NULL;
277 
278 	softtoken_pid = initialize_pid;
279 	softtoken_initialized = B_TRUE;
280 	(void) pthread_mutex_unlock(&soft_giant_mutex);
281 
282 	return (CKR_OK);
283 }
284 
285 /*
286  * C_Finalize is a wrapper around finalize_common. The
287  * soft_giant_mutex should be locked by C_Finalize().
288  */
289 CK_RV
290 C_Finalize(CK_VOID_PTR pReserved)
291 {
292 
293 	CK_RV rv;
294 
295 	(void) pthread_mutex_lock(&soft_giant_mutex);
296 
297 	rv = finalize_common(B_FALSE, pReserved);
298 
299 	(void) pthread_mutex_unlock(&soft_giant_mutex);
300 
301 	return (rv);
302 
303 }
304 
305 /*
306  * finalize_common() does the work for C_Finalize.  soft_giant_mutex
307  * must be held before calling this function.
308  */
309 static CK_RV
310 finalize_common(boolean_t force, CK_VOID_PTR pReserved) {
311 
312 	CK_RV rv = CKR_OK;
313 	struct object *delay_free_obj, *tmpo;
314 	struct session *delay_free_ses, *tmps;
315 
316 	if (!softtoken_initialized) {
317 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
318 	}
319 
320 	/* Check to see if pReseved is NULL */
321 	if (pReserved != NULL) {
322 		return (CKR_ARGUMENTS_BAD);
323 	}
324 
325 	(void) pthread_mutex_lock(&soft_sessionlist_mutex);
326 	/*
327 	 * Set all_sessions_closing flag so any access to any
328 	 * existing sessions will be rejected.
329 	 */
330 	all_sessions_closing = 1;
331 	(void) pthread_mutex_unlock(&soft_sessionlist_mutex);
332 
333 	/* Delete all the sessions and release the allocated resources */
334 	rv = soft_delete_all_sessions(force);
335 
336 	(void) pthread_mutex_lock(&soft_sessionlist_mutex);
337 	/* Reset all_sessions_closing flag. */
338 	all_sessions_closing = 0;
339 	(void) pthread_mutex_unlock(&soft_sessionlist_mutex);
340 
341 	softtoken_initialized = B_FALSE;
342 	softtoken_pid = 0;
343 
344 	pkcs11_close_urandom();
345 	pkcs11_close_urandom_seed();
346 	pkcs11_close_random();
347 
348 	/* Destroy the session list lock here */
349 	(void) pthread_mutex_destroy(&soft_sessionlist_mutex);
350 
351 	/*
352 	 * Destroy token object related stuffs
353 	 * 1. Clean up the token object list
354 	 * 2. Destroy slot mutex
355 	 * 3. Destroy mutex in token_session
356 	 */
357 	soft_delete_all_in_core_token_objects(ALL_TOKEN);
358 	(void) pthread_mutex_destroy(&soft_slot.slot_mutex);
359 	(void) pthread_mutex_destroy(&soft_slot.keystore_mutex);
360 	(void) soft_destroy_token_session();
361 
362 	/*
363 	 * free all entries in the delay_freed list
364 	 */
365 	delay_free_obj = obj_delay_freed.first;
366 	while (delay_free_obj != NULL) {
367 		tmpo = delay_free_obj->next;
368 		free(delay_free_obj);
369 		delay_free_obj = tmpo;
370 	}
371 
372 	soft_slot.keystore_load_status = KEYSTORE_UNINITIALIZED;
373 	(void) pthread_mutex_destroy(&obj_delay_freed.obj_to_be_free_mutex);
374 
375 	delay_free_ses = ses_delay_freed.first;
376 	while (delay_free_ses != NULL) {
377 		tmps = delay_free_ses->next;
378 		free(delay_free_ses);
379 		delay_free_ses = tmps;
380 	}
381 	(void) pthread_mutex_destroy(&ses_delay_freed.ses_to_be_free_mutex);
382 
383 	return (rv);
384 }
385 
386 static void
387 softtoken_init()
388 {
389 	/* Children inherit parent's atfork handlers */
390 	(void) pthread_atfork(softtoken_fork_prepare,
391 	    softtoken_fork_after, softtoken_fork_after);
392 }
393 
394 /*
395  * softtoken_fini() function required to make sure complete cleanup
396  * is done if softtoken is ever unloaded without a C_Finalize() call.
397  */
398 static void
399 softtoken_fini()
400 {
401 	(void) pthread_mutex_lock(&soft_giant_mutex);
402 
403 	/* if we're not initilized, do not attempt to finalize */
404 	if (!softtoken_initialized) {
405 		(void) pthread_mutex_unlock(&soft_giant_mutex);
406 		return;
407 	}
408 
409 	(void) finalize_common(B_TRUE, NULL_PTR);
410 
411 	(void) pthread_mutex_unlock(&soft_giant_mutex);
412 }
413 
414 CK_RV
415 C_GetInfo(CK_INFO_PTR pInfo)
416 {
417 	if (!softtoken_initialized)
418 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
419 
420 	if (pInfo == NULL) {
421 		return (CKR_ARGUMENTS_BAD);
422 	}
423 
424 	/* Provide general information in the provided buffer */
425 	pInfo->cryptokiVersion.major = CRYPTOKI_VERSION_MAJOR;
426 	pInfo->cryptokiVersion.minor = CRYPTOKI_VERSION_MINOR;
427 	(void) strncpy((char *)pInfo->manufacturerID,
428 	    SOFT_MANUFACTURER_ID, 32);
429 	pInfo->flags = 0;
430 	(void) strncpy((char *)pInfo->libraryDescription,
431 	    LIBRARY_DESCRIPTION, 32);
432 	pInfo->libraryVersion.major = LIBRARY_VERSION_MAJOR;
433 	pInfo->libraryVersion.major = LIBRARY_VERSION_MINOR;
434 
435 	return (CKR_OK);
436 }
437 
438 CK_RV
439 C_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR ppFunctionList)
440 {
441 	if (ppFunctionList == NULL) {
442 		return (CKR_ARGUMENTS_BAD);
443 	}
444 
445 	*ppFunctionList = &functionList;
446 
447 	return (CKR_OK);
448 }
449 
450 /*
451  * PKCS#11 states that C_GetFunctionStatus should always return
452  * CKR_FUNCTION_NOT_PARALLEL
453  */
454 /*ARGSUSED*/
455 CK_RV
456 C_GetFunctionStatus(CK_SESSION_HANDLE hSession)
457 {
458 	return (CKR_FUNCTION_NOT_PARALLEL);
459 }
460 
461 /*
462  * PKCS#11 states that C_CancelFunction should always return
463  * CKR_FUNCTION_NOT_PARALLEL
464  */
465 /*ARGSUSED*/
466 CK_RV
467 C_CancelFunction(CK_SESSION_HANDLE hSession)
468 {
469 	return (CKR_FUNCTION_NOT_PARALLEL);
470 }
471 
472 /*
473  * Take out all mutexes before fork.
474  *
475  * Order:
476  * 1. soft_giant_mutex
477  * 2. soft_sessionlist_mutex
478  * 3. soft_slot.slot_mutex
479  * 4. soft_slot.keystore_mutex
480  * 5. token_session mutexes via soft_acquire_all_session_mutexes()
481  * 6. all soft_session_list mutexes via soft_acquire_all_session_mutexes()
482  * 7. obj_delay_freed.obj_to_be_free_mutex;
483  * 8. ses_delay_freed.ses_to_be_free_mutex
484  */
485 void
486 softtoken_fork_prepare()
487 {
488 	(void) pthread_mutex_lock(&soft_giant_mutex);
489 	if (softtoken_initialized) {
490 		(void) pthread_mutex_lock(&soft_sessionlist_mutex);
491 		(void) pthread_mutex_lock(&soft_slot.slot_mutex);
492 		(void) pthread_mutex_lock(&soft_slot.keystore_mutex);
493 		soft_acquire_all_session_mutexes(&token_session);
494 		soft_acquire_all_session_mutexes(soft_session_list);
495 		(void) pthread_mutex_lock(
496 		    &obj_delay_freed.obj_to_be_free_mutex);
497 		(void) pthread_mutex_lock(
498 		    &ses_delay_freed.ses_to_be_free_mutex);
499 	}
500 }
501 
502 /*
503  * Release in opposite order to softtoken_fork_prepare().
504  * Function is used for parent and child.
505  */
506 void
507 softtoken_fork_after()
508 {
509 	if (softtoken_initialized) {
510 		(void) pthread_mutex_unlock(
511 		    &ses_delay_freed.ses_to_be_free_mutex);
512 		(void) pthread_mutex_unlock(
513 		    &obj_delay_freed.obj_to_be_free_mutex);
514 		soft_release_all_session_mutexes(soft_session_list);
515 		soft_release_all_session_mutexes(&token_session);
516 		(void) pthread_mutex_unlock(&soft_slot.keystore_mutex);
517 		(void) pthread_mutex_unlock(&soft_slot.slot_mutex);
518 		(void) pthread_mutex_unlock(&soft_sessionlist_mutex);
519 	}
520 	(void) pthread_mutex_unlock(&soft_giant_mutex);
521 }
522