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.
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.
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
30 #include <cryptoutil.h>
31 #include <unistd.h> /* for pid_t */
33 #include <security/cryptoki.h>
34 #include "softGlobal.h"
35 #include "softSession.h"
36 #include "softObject.h"
37 #include "softKeystore.h"
38 #include "softKeystoreUtil.h"
40 #pragma init(softtoken_init)
41 #pragma fini(softtoken_fini)
43 extern soft_session_t token_session
; /* for fork handler */
45 static struct CK_FUNCTION_LIST functionList
= {
46 { 2, 20 }, /* version */
101 C_DigestEncryptUpdate
,
102 C_DecryptDigestUpdate
,
104 C_DecryptVerifyUpdate
,
117 boolean_t softtoken_initialized
= B_FALSE
;
119 static pid_t softtoken_pid
= 0;
121 /* This mutex protects soft_session_list, all_sessions_closing */
122 pthread_mutex_t soft_sessionlist_mutex
;
123 soft_session_t
*soft_session_list
= NULL
;
125 int all_sessions_closing
= 0;
128 obj_to_be_freed_list_t obj_delay_freed
;
129 ses_to_be_freed_list_t ses_delay_freed
;
131 /* protects softtoken_initialized and access to C_Initialize/C_Finalize */
132 pthread_mutex_t soft_giant_mutex
= PTHREAD_MUTEX_INITIALIZER
;
134 static CK_RV
finalize_common(boolean_t force
, CK_VOID_PTR pReserved
);
135 static void softtoken_init();
136 static void softtoken_fini();
137 static void softtoken_fork_prepare();
138 static void softtoken_fork_after();
141 C_Initialize(CK_VOID_PTR pInitArgs
)
145 boolean_t supplied_ok
;
149 * Get lock to insure only one thread enters this
150 * function at a time.
152 (void) pthread_mutex_lock(&soft_giant_mutex
);
154 initialize_pid
= getpid();
156 if (softtoken_initialized
) {
157 if (initialize_pid
== softtoken_pid
) {
159 * This process has called C_Initialize already
161 (void) pthread_mutex_unlock(&soft_giant_mutex
);
162 return (CKR_CRYPTOKI_ALREADY_INITIALIZED
);
165 * A fork has happened and the child is
166 * reinitializing. Do a finalize_common to close
167 * out any state from the parent, and then
170 (void) finalize_common(B_TRUE
, NULL
);
174 if (pInitArgs
!= NULL
) {
175 CK_C_INITIALIZE_ARGS
*initargs1
=
176 (CK_C_INITIALIZE_ARGS
*) pInitArgs
;
178 /* pReserved must be NULL */
179 if (initargs1
->pReserved
!= NULL
) {
180 (void) pthread_mutex_unlock(&soft_giant_mutex
);
181 return (CKR_ARGUMENTS_BAD
);
185 * ALL supplied function pointers need to have the value
186 * either NULL or non-NULL.
188 supplied_ok
= (initargs1
->CreateMutex
== NULL
&&
189 initargs1
->DestroyMutex
== NULL
&&
190 initargs1
->LockMutex
== NULL
&&
191 initargs1
->UnlockMutex
== NULL
) ||
192 (initargs1
->CreateMutex
!= NULL
&&
193 initargs1
->DestroyMutex
!= NULL
&&
194 initargs1
->LockMutex
!= NULL
&&
195 initargs1
->UnlockMutex
!= NULL
);
198 (void) pthread_mutex_unlock(&soft_giant_mutex
);
199 return (CKR_ARGUMENTS_BAD
);
203 * When the CKF_OS_LOCKING_OK flag isn't set and mutex
204 * function pointers are supplied by an application,
205 * return an error. We must be able to use our own primitives.
207 if (!(initargs1
->flags
& CKF_OS_LOCKING_OK
) &&
208 (initargs1
->CreateMutex
!= NULL
)) {
209 (void) pthread_mutex_unlock(&soft_giant_mutex
);
210 return (CKR_CANT_LOCK
);
214 /* Initialize the session list lock */
215 if (pthread_mutex_init(&soft_sessionlist_mutex
, NULL
) != 0) {
216 (void) pthread_mutex_unlock(&soft_giant_mutex
);
217 return (CKR_CANT_LOCK
);
221 * token object related initialization
223 soft_slot
.authenticated
= 0;
224 soft_slot
.userpin_change_needed
= 0;
225 soft_slot
.token_object_list
= NULL
;
226 soft_slot
.keystore_load_status
= KEYSTORE_UNINITIALIZED
;
228 if ((rv
= soft_init_token_session()) != CKR_OK
) {
229 (void) pthread_mutex_destroy(&soft_sessionlist_mutex
);
230 (void) pthread_mutex_unlock(&soft_giant_mutex
);
234 /* Initialize the slot lock */
235 if (pthread_mutex_init(&soft_slot
.slot_mutex
, NULL
) != 0) {
236 (void) pthread_mutex_destroy(&soft_sessionlist_mutex
);
237 (void) soft_destroy_token_session();
238 (void) pthread_mutex_unlock(&soft_giant_mutex
);
239 return (CKR_CANT_LOCK
);
242 /* Initialize the keystore lock */
243 if (pthread_mutex_init(&soft_slot
.keystore_mutex
, NULL
) != 0) {
244 (void) pthread_mutex_destroy(&soft_slot
.slot_mutex
);
245 (void) pthread_mutex_destroy(&soft_sessionlist_mutex
);
246 (void) soft_destroy_token_session();
247 (void) pthread_mutex_unlock(&soft_giant_mutex
);
248 return (CKR_CANT_LOCK
);
251 /* Initialize the object_to_be_freed list */
252 if (pthread_mutex_init(&obj_delay_freed
.obj_to_be_free_mutex
, NULL
)
254 (void) pthread_mutex_destroy(&soft_slot
.keystore_mutex
);
255 (void) pthread_mutex_destroy(&soft_slot
.slot_mutex
);
256 (void) pthread_mutex_destroy(&soft_sessionlist_mutex
);
257 (void) soft_destroy_token_session();
258 (void) pthread_mutex_unlock(&soft_giant_mutex
);
259 return (CKR_CANT_LOCK
);
261 obj_delay_freed
.count
= 0;
262 obj_delay_freed
.first
= NULL
;
263 obj_delay_freed
.last
= NULL
;
265 if (pthread_mutex_init(&ses_delay_freed
.ses_to_be_free_mutex
, NULL
)
267 (void) pthread_mutex_destroy(
268 &obj_delay_freed
.obj_to_be_free_mutex
);
269 (void) pthread_mutex_destroy(&soft_slot
.keystore_mutex
);
270 (void) pthread_mutex_destroy(&soft_slot
.slot_mutex
);
271 (void) pthread_mutex_destroy(&soft_sessionlist_mutex
);
272 (void) soft_destroy_token_session();
273 (void) pthread_mutex_unlock(&soft_giant_mutex
);
274 return (CKR_CANT_LOCK
);
276 ses_delay_freed
.count
= 0;
277 ses_delay_freed
.first
= NULL
;
278 ses_delay_freed
.last
= NULL
;
281 (void) pthread_mutex_destroy(
282 &ses_delay_freed
.ses_to_be_free_mutex
);
283 (void) pthread_mutex_destroy(
284 &obj_delay_freed
.obj_to_be_free_mutex
);
285 (void) pthread_mutex_destroy(&soft_slot
.keystore_mutex
);
286 (void) pthread_mutex_destroy(&soft_slot
.slot_mutex
);
287 (void) pthread_mutex_destroy(&soft_sessionlist_mutex
);
288 (void) soft_destroy_token_session();
289 (void) pthread_mutex_unlock(&soft_giant_mutex
);
290 return (CKR_FUNCTION_FAILED
);
293 softtoken_pid
= initialize_pid
;
294 softtoken_initialized
= B_TRUE
;
295 (void) pthread_mutex_unlock(&soft_giant_mutex
);
301 * C_Finalize is a wrapper around finalize_common. The
302 * soft_giant_mutex should be locked by C_Finalize().
305 C_Finalize(CK_VOID_PTR pReserved
)
310 (void) pthread_mutex_lock(&soft_giant_mutex
);
312 rv
= finalize_common(B_FALSE
, pReserved
);
314 (void) pthread_mutex_unlock(&soft_giant_mutex
);
321 * finalize_common() does the work for C_Finalize. soft_giant_mutex
322 * must be held before calling this function.
325 finalize_common(boolean_t force
, CK_VOID_PTR pReserved
) {
328 struct object
*delay_free_obj
, *tmpo
;
329 struct session
*delay_free_ses
, *tmps
;
331 if (!softtoken_initialized
) {
332 return (CKR_CRYPTOKI_NOT_INITIALIZED
);
335 /* Check to see if pReseved is NULL */
336 if (pReserved
!= NULL
) {
337 return (CKR_ARGUMENTS_BAD
);
340 (void) pthread_mutex_lock(&soft_sessionlist_mutex
);
342 * Set all_sessions_closing flag so any access to any
343 * existing sessions will be rejected.
345 all_sessions_closing
= 1;
346 (void) pthread_mutex_unlock(&soft_sessionlist_mutex
);
348 /* Delete all the sessions and release the allocated resources */
349 rv
= soft_delete_all_sessions(force
);
351 (void) pthread_mutex_lock(&soft_sessionlist_mutex
);
352 /* Reset all_sessions_closing flag. */
353 all_sessions_closing
= 0;
354 (void) pthread_mutex_unlock(&soft_sessionlist_mutex
);
356 softtoken_initialized
= B_FALSE
;
360 * There used to be calls to cleanup libcryptoutil here. Given that
361 * libcryptoutil can be linked and invoked independently of PKCS#11,
362 * cleaning up libcryptoutil here makes no sense. Decoupling these
363 * two also prevent deadlocks and other artificial dependencies.
366 /* Destroy the session list lock here */
367 (void) pthread_mutex_destroy(&soft_sessionlist_mutex
);
370 * Destroy token object related stuffs
371 * 1. Clean up the token object list
372 * 2. Destroy slot mutex
373 * 3. Destroy mutex in token_session
375 soft_delete_all_in_core_token_objects(ALL_TOKEN
);
376 (void) pthread_mutex_destroy(&soft_slot
.slot_mutex
);
377 (void) pthread_mutex_destroy(&soft_slot
.keystore_mutex
);
378 (void) soft_destroy_token_session();
381 * free all entries in the delay_freed list
383 delay_free_obj
= obj_delay_freed
.first
;
384 while (delay_free_obj
!= NULL
) {
385 tmpo
= delay_free_obj
->next
;
386 free(delay_free_obj
);
387 delay_free_obj
= tmpo
;
390 soft_slot
.keystore_load_status
= KEYSTORE_UNINITIALIZED
;
391 (void) pthread_mutex_destroy(&obj_delay_freed
.obj_to_be_free_mutex
);
393 delay_free_ses
= ses_delay_freed
.first
;
394 while (delay_free_ses
!= NULL
) {
395 tmps
= delay_free_ses
->next
;
396 free(delay_free_ses
);
397 delay_free_ses
= tmps
;
399 (void) pthread_mutex_destroy(&ses_delay_freed
.ses_to_be_free_mutex
);
407 /* Children inherit parent's atfork handlers */
408 (void) pthread_atfork(softtoken_fork_prepare
,
409 softtoken_fork_after
, softtoken_fork_after
);
413 * softtoken_fini() function required to make sure complete cleanup
414 * is done if softtoken is ever unloaded without a C_Finalize() call.
419 (void) pthread_mutex_lock(&soft_giant_mutex
);
421 /* if we're not initilized, do not attempt to finalize */
422 if (!softtoken_initialized
) {
423 (void) pthread_mutex_unlock(&soft_giant_mutex
);
427 (void) finalize_common(B_TRUE
, NULL_PTR
);
429 (void) pthread_mutex_unlock(&soft_giant_mutex
);
433 C_GetInfo(CK_INFO_PTR pInfo
)
435 if (!softtoken_initialized
)
436 return (CKR_CRYPTOKI_NOT_INITIALIZED
);
439 return (CKR_ARGUMENTS_BAD
);
442 /* Provide general information in the provided buffer */
443 pInfo
->cryptokiVersion
.major
= CRYPTOKI_VERSION_MAJOR
;
444 pInfo
->cryptokiVersion
.minor
= CRYPTOKI_VERSION_MINOR
;
445 (void) strncpy((char *)pInfo
->manufacturerID
,
446 SOFT_MANUFACTURER_ID
, 32);
448 (void) strncpy((char *)pInfo
->libraryDescription
,
449 LIBRARY_DESCRIPTION
, 32);
450 pInfo
->libraryVersion
.major
= LIBRARY_VERSION_MAJOR
;
451 pInfo
->libraryVersion
.minor
= LIBRARY_VERSION_MINOR
;
457 C_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR ppFunctionList
)
459 if (ppFunctionList
== NULL
) {
460 return (CKR_ARGUMENTS_BAD
);
463 *ppFunctionList
= &functionList
;
469 * PKCS#11 states that C_GetFunctionStatus should always return
470 * CKR_FUNCTION_NOT_PARALLEL
474 C_GetFunctionStatus(CK_SESSION_HANDLE hSession
)
476 return (CKR_FUNCTION_NOT_PARALLEL
);
480 * PKCS#11 states that C_CancelFunction should always return
481 * CKR_FUNCTION_NOT_PARALLEL
485 C_CancelFunction(CK_SESSION_HANDLE hSession
)
487 return (CKR_FUNCTION_NOT_PARALLEL
);
491 * Take out all mutexes before fork.
494 * 1. soft_giant_mutex
495 * 2. soft_sessionlist_mutex
496 * 3. soft_slot.slot_mutex
497 * 4. soft_slot.keystore_mutex
498 * 5. token_session mutexes via soft_acquire_all_session_mutexes()
499 * 6. all soft_session_list mutexes via soft_acquire_all_session_mutexes()
500 * 7. obj_delay_freed.obj_to_be_free_mutex;
501 * 8. ses_delay_freed.ses_to_be_free_mutex
504 softtoken_fork_prepare()
506 (void) pthread_mutex_lock(&soft_giant_mutex
);
507 if (softtoken_initialized
) {
508 (void) pthread_mutex_lock(&soft_sessionlist_mutex
);
509 (void) pthread_mutex_lock(&soft_slot
.slot_mutex
);
510 (void) pthread_mutex_lock(&soft_slot
.keystore_mutex
);
511 soft_acquire_all_session_mutexes(&token_session
);
512 soft_acquire_all_session_mutexes(soft_session_list
);
513 (void) pthread_mutex_lock(
514 &obj_delay_freed
.obj_to_be_free_mutex
);
515 (void) pthread_mutex_lock(
516 &ses_delay_freed
.ses_to_be_free_mutex
);
521 * Release in opposite order to softtoken_fork_prepare().
522 * Function is used for parent and child.
525 softtoken_fork_after()
527 if (softtoken_initialized
) {
528 (void) pthread_mutex_unlock(
529 &ses_delay_freed
.ses_to_be_free_mutex
);
530 (void) pthread_mutex_unlock(
531 &obj_delay_freed
.obj_to_be_free_mutex
);
532 soft_release_all_session_mutexes(soft_session_list
);
533 soft_release_all_session_mutexes(&token_session
);
534 (void) pthread_mutex_unlock(&soft_slot
.keystore_mutex
);
535 (void) pthread_mutex_unlock(&soft_slot
.slot_mutex
);
536 (void) pthread_mutex_unlock(&soft_sessionlist_mutex
);
538 (void) pthread_mutex_unlock(&soft_giant_mutex
);