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.
33 #include <security/cryptoki.h>
34 #include "softGlobal.h"
35 #include "softSession.h"
36 #include "softObject.h"
38 #include "softKeystore.h"
39 #include "softKeystoreUtil.h"
42 CK_ULONG soft_session_cnt
= 0; /* the number of opened sessions */
43 CK_ULONG soft_session_rw_cnt
= 0; /* the number of opened R/W sessions */
45 #define DIGEST_MECH_OK(_m_) ((_m_) == CKM_MD5 || (_m_) == CKM_SHA_1)
48 * Delete all the sessions. First, obtain the global session
49 * list lock. Then start to delete one session at a time.
50 * Release the global session list lock before returning to
54 soft_delete_all_sessions(boolean_t force
)
59 soft_session_t
*session_p
;
60 soft_session_t
*session_p1
;
62 /* Acquire the global session list lock */
63 (void) pthread_mutex_lock(&soft_sessionlist_mutex
);
65 session_p
= soft_session_list
;
67 /* Delete all the sessions in the session list */
69 session_p1
= session_p
->next
;
72 * Delete a session by calling soft_delete_session()
73 * with a session pointer and a boolean arguments.
74 * Boolean value TRUE is used to indicate that the
75 * caller holds the lock on the global session list.
78 rv1
= soft_delete_session(session_p
, force
, B_TRUE
);
80 /* Record the very first error code */
85 session_p
= session_p1
;
89 soft_session_list
= NULL
;
91 /* Release the global session list lock */
92 (void) pthread_mutex_unlock(&soft_sessionlist_mutex
);
100 * Create a new session struct, and add it to the session linked list.
102 * This function will acquire the global session list lock, and release
103 * it after adding the session to the session linked list.
106 soft_add_session(CK_FLAGS flags
, CK_VOID_PTR pApplication
,
107 CK_NOTIFY notify
, CK_ULONG
*sessionhandle_p
)
110 soft_session_t
*new_sp
= NULL
;
112 /* Allocate a new session struct */
113 new_sp
= calloc(1, sizeof (soft_session_t
));
114 if (new_sp
== NULL
) {
115 return (CKR_HOST_MEMORY
);
118 new_sp
->magic_marker
= SOFTTOKEN_SESSION_MAGIC
;
119 new_sp
->pApplication
= pApplication
;
120 new_sp
->Notify
= notify
;
121 new_sp
->flags
= flags
;
122 new_sp
->state
= CKS_RO_PUBLIC_SESSION
;
123 new_sp
->object_list
= NULL
;
124 new_sp
->ses_refcnt
= 0;
125 new_sp
->ses_close_sync
= 0;
127 (void) pthread_mutex_lock(&soft_giant_mutex
);
128 if (soft_slot
.authenticated
) {
129 (void) pthread_mutex_unlock(&soft_giant_mutex
);
130 if (flags
& CKF_RW_SESSION
) {
131 new_sp
->state
= CKS_RW_USER_FUNCTIONS
;
133 new_sp
->state
= CKS_RO_USER_FUNCTIONS
;
136 (void) pthread_mutex_unlock(&soft_giant_mutex
);
137 if (flags
& CKF_RW_SESSION
) {
138 new_sp
->state
= CKS_RW_PUBLIC_SESSION
;
140 new_sp
->state
= CKS_RO_PUBLIC_SESSION
;
144 /* Initialize the lock for the newly created session */
145 if (pthread_mutex_init(&new_sp
->session_mutex
, NULL
) != 0) {
147 return (CKR_CANT_LOCK
);
150 (void) pthread_cond_init(&new_sp
->ses_free_cond
, NULL
);
152 /* Acquire the global session list lock */
153 (void) pthread_mutex_lock(&soft_sessionlist_mutex
);
155 /* Insert the new session in front of session list */
156 if (soft_session_list
== NULL
) {
157 soft_session_list
= new_sp
;
161 soft_session_list
->prev
= new_sp
;
162 new_sp
->next
= soft_session_list
;
164 soft_session_list
= new_sp
;
167 /* Type casting the address of a session struct to a session handle */
168 *sessionhandle_p
= (CK_ULONG
)new_sp
;
170 if (flags
& CKF_RW_SESSION
)
171 ++soft_session_rw_cnt
;
173 if (soft_session_cnt
== 1)
175 * This is the first session to be opened, so we can set
176 * validate the public token objects in token list now.
178 soft_validate_token_objects(B_TRUE
);
180 /* Release the global session list lock */
181 (void) pthread_mutex_unlock(&soft_sessionlist_mutex
);
188 * This function adds the to-be-freed session to a linked list.
189 * When the number of sessions queued in the linked list reaches the
190 * maximum threshold MAX_SES_TO_BE_FREED, it will free the first
191 * session (FIFO) in the list.
194 session_delay_free(soft_session_t
*sp
)
198 (void) pthread_mutex_lock(&ses_delay_freed
.ses_to_be_free_mutex
);
200 /* Add the newly deleted session at the end of the list */
202 if (ses_delay_freed
.first
== NULL
) {
203 ses_delay_freed
.last
= sp
;
204 ses_delay_freed
.first
= sp
;
206 ses_delay_freed
.last
->next
= sp
;
207 ses_delay_freed
.last
= sp
;
210 if (++ses_delay_freed
.count
>= MAX_SES_TO_BE_FREED
) {
212 * Free the first session in the list only if
213 * the total count reaches maximum threshold.
215 ses_delay_freed
.count
--;
216 tmp
= ses_delay_freed
.first
->next
;
217 free(ses_delay_freed
.first
);
218 ses_delay_freed
.first
= tmp
;
220 (void) pthread_mutex_unlock(&ses_delay_freed
.ses_to_be_free_mutex
);
225 * - Remove the session from the session linked list.
226 * Holding the lock on the global session list is needed to do this.
227 * - Release all the objects created by the session.
229 * The boolean argument lock_held is used to indicate that whether
230 * the caller of this function holds the lock on the global session
232 * - When called by soft_delete_all_sessions(), which is called by
233 * C_Finalize() or C_CloseAllSessions() -- the lock_held = TRUE.
234 * - When called by C_CloseSession() -- the lock_held = FALSE.
236 * When the caller does not hold the lock on the global session
237 * list, this function will acquire that lock in order to proceed,
238 * and also release that lock before returning to caller.
241 soft_delete_session(soft_session_t
*session_p
,
242 boolean_t force
, boolean_t lock_held
)
246 * Check to see if the caller holds the lock on the global
247 * session list. If not, we need to acquire that lock in
251 /* Acquire the global session list lock */
252 (void) pthread_mutex_lock(&soft_sessionlist_mutex
);
256 * Remove the session from the session linked list first.
258 if (soft_session_list
== session_p
) {
259 /* Session is the first one in the list */
260 if (session_p
->next
) {
261 soft_session_list
= session_p
->next
;
262 session_p
->next
->prev
= NULL
;
264 /* Session is the only one in the list */
265 soft_session_list
= NULL
;
268 /* Session is not the first one in the list */
269 if (session_p
->next
) {
270 /* Session is in the middle of the list */
271 session_p
->prev
->next
= session_p
->next
;
272 session_p
->next
->prev
= session_p
->prev
;
274 /* Session is the last one in the list */
275 session_p
->prev
->next
= NULL
;
280 if (session_p
->flags
& CKF_RW_SESSION
)
281 --soft_session_rw_cnt
;
285 * If the global session list lock is obtained by
286 * this function, then release that lock after
287 * removing the session from session linked list.
288 * We want the releasing of the objects of the
289 * session, and freeing of the session itself to
290 * be done without holding the global session list
293 (void) pthread_mutex_unlock(&soft_sessionlist_mutex
);
297 /* Acquire the individual session lock */
298 (void) pthread_mutex_lock(&session_p
->session_mutex
);
300 * Make sure another thread hasn't freed the session.
302 if (session_p
->magic_marker
!= SOFTTOKEN_SESSION_MAGIC
) {
303 (void) pthread_mutex_unlock(&session_p
->session_mutex
);
308 * The deletion of a session must be blocked when the session
309 * reference count is not zero. This means if any session related
310 * operation starts prior to the session close operation gets in,
311 * the session closing thread must wait for the non-closing
312 * operation to be completed before it can proceed the close
315 * Unless we are being forced to shut everything down, this only
316 * happens if the libraries _fini() is running not of someone
317 * explicitly called C_Finalize().
320 session_p
->ses_refcnt
= 0;
322 while (session_p
->ses_refcnt
!= 0) {
324 * We set the SESSION_REFCNT_WAITING flag before we put
325 * this closing thread in a wait state, so other non-closing
326 * operation thread will signal to wake it up only when
327 * the session reference count becomes zero and this flag
330 session_p
->ses_close_sync
|= SESSION_REFCNT_WAITING
;
331 (void) pthread_cond_wait(&session_p
->ses_free_cond
,
332 &session_p
->session_mutex
);
335 session_p
->ses_close_sync
&= ~SESSION_REFCNT_WAITING
;
338 * Remove all the objects created in this session.
340 soft_delete_all_objects_in_session(session_p
, force
);
343 * Mark session as no longer valid. This can only be done after all
344 * objects created by this session are free'd since the marker is
345 * still needed in the process of removing objects from the session.
347 session_p
->magic_marker
= 0;
349 (void) pthread_cond_destroy(&session_p
->ses_free_cond
);
351 /* In case application did not call Final */
352 free(session_p
->digest
.context
);
354 if (session_p
->encrypt
.context
!= NULL
)
356 * 1st B_TRUE: encrypt
357 * 2nd B_TRUE: caller is holding session_mutex.
359 soft_crypt_cleanup(session_p
, B_TRUE
, B_TRUE
);
361 if (session_p
->decrypt
.context
!= NULL
)
363 * 1st B_FALSE: decrypt
364 * 2nd B_TRUE: caller is holding session_mutex.
366 soft_crypt_cleanup(session_p
, B_FALSE
, B_TRUE
);
368 free(session_p
->sign
.context
);
370 free(session_p
->verify
.context
);
372 if (session_p
->find_objects
.context
!= NULL
) {
373 find_context_t
*fcontext
;
374 fcontext
= (find_context_t
*)session_p
->find_objects
.context
;
375 free(fcontext
->objs_found
);
379 /* Reset SESSION_IS_CLOSIN flag. */
380 session_p
->ses_close_sync
&= ~SESSION_IS_CLOSING
;
382 (void) pthread_mutex_unlock(&session_p
->session_mutex
);
383 /* Destroy the individual session lock */
384 (void) pthread_mutex_destroy(&session_p
->session_mutex
);
386 /* Delay freeing the session */
387 session_delay_free(session_p
);
394 * This function is used to type cast a session handle to a pointer to
395 * the session struct. Also, it does the following things:
396 * 1) Check to see if the session struct is tagged with a session
397 * magic number. This is to detect when an application passes
398 * a bogus session pointer.
399 * 2) Acquire the lock on the designated session.
400 * 3) Check to see if the session is in the closing state that another
401 * thread is performing.
402 * 4) Increment the session reference count by one. This is to prevent
403 * this session from being closed by other thread.
404 * 5) Release the lock held on the designated session.
407 handle2session(CK_SESSION_HANDLE hSession
, soft_session_t
**session_p
)
410 soft_session_t
*sp
= (soft_session_t
*)(hSession
);
413 * No need to hold soft_sessionlist_mutex as we are
414 * just reading the value and 32-bit reads are atomic.
416 if (all_sessions_closing
) {
417 return (CKR_SESSION_CLOSED
);
421 (sp
->magic_marker
!= SOFTTOKEN_SESSION_MAGIC
)) {
422 return (CKR_SESSION_HANDLE_INVALID
);
424 (void) pthread_mutex_lock(&sp
->session_mutex
);
426 if (sp
->ses_close_sync
& SESSION_IS_CLOSING
) {
427 (void) pthread_mutex_unlock(&sp
->session_mutex
);
428 return (CKR_SESSION_CLOSED
);
431 /* Increment session ref count. */
434 (void) pthread_mutex_unlock(&sp
->session_mutex
);
442 * The format to be saved in the pOperationState will be:
443 * 1. internal_op_state_t
444 * 2. crypto_active_op_t
445 * 3. actual context of the active operation
448 soft_get_operationstate(soft_session_t
*session_p
, CK_BYTE_PTR pOperationState
,
449 CK_ULONG_PTR pulOperationStateLen
)
452 internal_op_state_t
*p_op_state
;
453 CK_ULONG op_data_len
= 0;
456 if (pulOperationStateLen
== NULL
)
457 return (CKR_ARGUMENTS_BAD
);
459 (void) pthread_mutex_lock(&session_p
->session_mutex
);
461 /* Check to see if encrypt operation is active. */
462 if (session_p
->encrypt
.flags
& CRYPTO_OPERATION_ACTIVE
) {
463 rv
= CKR_STATE_UNSAVEABLE
;
467 /* Check to see if decrypt operation is active. */
468 if (session_p
->decrypt
.flags
& CRYPTO_OPERATION_ACTIVE
) {
469 rv
= CKR_STATE_UNSAVEABLE
;
473 /* Check to see if sign operation is active. */
474 if (session_p
->sign
.flags
& CRYPTO_OPERATION_ACTIVE
) {
475 rv
= CKR_STATE_UNSAVEABLE
;
479 /* Check to see if verify operation is active. */
480 if (session_p
->verify
.flags
& CRYPTO_OPERATION_ACTIVE
) {
481 rv
= CKR_STATE_UNSAVEABLE
;
485 /* Check to see if digest operation is active. */
486 if (session_p
->digest
.flags
& CRYPTO_OPERATION_ACTIVE
) {
487 op_data_len
= sizeof (internal_op_state_t
) +
488 sizeof (crypto_active_op_t
);
490 switch (session_p
->digest
.mech
.mechanism
) {
492 op_data_len
+= sizeof (MD5_CTX
);
495 op_data_len
+= sizeof (SHA1_CTX
);
498 rv
= CKR_STATE_UNSAVEABLE
;
502 if (pOperationState
== NULL_PTR
) {
503 *pulOperationStateLen
= op_data_len
;
506 if (*pulOperationStateLen
< op_data_len
) {
507 *pulOperationStateLen
= op_data_len
;
508 rv
= CKR_BUFFER_TOO_SMALL
;
513 /* Save internal_op_state_t */
514 /* LINTED E_BAD_PTR_CAST_ALIGN */
515 p_op_state
= (internal_op_state_t
*)pOperationState
;
516 p_op_state
->op_len
= op_data_len
;
517 p_op_state
->op_active
= DIGEST_OP
;
518 p_op_state
->op_session_state
= session_p
->state
;
520 /* Save crypto_active_op_t */
521 (void) memcpy((CK_BYTE
*)pOperationState
+
522 sizeof (internal_op_state_t
),
524 sizeof (crypto_active_op_t
));
526 switch (session_p
->digest
.mech
.mechanism
) {
528 /* Save MD5_CTX for the active digest operation */
529 (void) memcpy((CK_BYTE
*)pOperationState
+
530 sizeof (internal_op_state_t
) +
531 sizeof (crypto_active_op_t
),
532 session_p
->digest
.context
,
537 /* Save SHA1_CTX for the active digest operation */
538 (void) memcpy((CK_BYTE
*)pOperationState
+
539 sizeof (internal_op_state_t
) +
540 sizeof (crypto_active_op_t
),
541 session_p
->digest
.context
,
546 rv
= CKR_STATE_UNSAVEABLE
;
549 rv
= CKR_OPERATION_NOT_INITIALIZED
;
553 *pulOperationStateLen
= op_data_len
;
556 (void) pthread_mutex_unlock(&session_p
->session_mutex
);
562 static CK_BYTE_PTR
alloc_digest(CK_ULONG mech
)
568 ret_val
= (CK_BYTE_PTR
) malloc(sizeof (MD5_CTX
));
571 ret_val
= (CK_BYTE_PTR
) malloc(sizeof (SHA1_CTX
));
573 default: ret_val
= NULL
;
580 * The format to be restored from the pOperationState will be:
581 * 1. internal_op_state_t
582 * 2. crypto_active_op_t
583 * 3. actual context of the saved operation
586 soft_set_operationstate(soft_session_t
*session_p
, CK_BYTE_PTR pOperationState
,
587 CK_ULONG ulOperationStateLen
, CK_OBJECT_HANDLE hEncryptionKey
,
588 CK_OBJECT_HANDLE hAuthenticationKey
)
592 internal_op_state_t
*p_op_state
;
593 crypto_active_op_t
*p_active_op
;
596 void *free_it
= NULL
;
598 /* LINTED E_BAD_PTR_CAST_ALIGN */
599 p_op_state
= (internal_op_state_t
*)pOperationState
;
601 if (p_op_state
->op_len
!= ulOperationStateLen
) {
603 * The supplied data length does not match with
604 * the saved data length.
606 return (CKR_SAVED_STATE_INVALID
);
609 if (p_op_state
->op_active
!= DIGEST_OP
)
610 return (CKR_SAVED_STATE_INVALID
);
612 if ((hAuthenticationKey
!= 0) || (hEncryptionKey
!= 0)) {
613 return (CKR_KEY_NOT_NEEDED
);
616 offset
= sizeof (internal_op_state_t
);
617 /* LINTED E_BAD_PTR_CAST_ALIGN */
618 p_active_op
= (crypto_active_op_t
*)(pOperationState
+ offset
);
619 offset
+= sizeof (crypto_active_op_t
);
620 mech
= p_active_op
->mech
.mechanism
;
622 if (!DIGEST_MECH_OK(mech
)) {
623 return (CKR_SAVED_STATE_INVALID
);
627 * We may reuse digest.context in case the digest mechanisms (the one,
628 * which belongs to session and the operation, which we are restoring)
629 * are the same. If digest mechanisms are different, we have to release
630 * the digest context, which belongs to session and allocate a new one.
632 (void) pthread_mutex_lock(&session_p
->session_mutex
);
634 if (session_p
->state
!= p_op_state
->op_session_state
) {
636 * The supplied session state does not match with
637 * the saved session state.
639 rv
= CKR_SAVED_STATE_INVALID
;
643 if (session_p
->digest
.context
&&
644 (session_p
->digest
.mech
.mechanism
!= mech
)) {
645 free_it
= session_p
->digest
.context
;
646 session_p
->digest
.context
= NULL
;
649 if (session_p
->digest
.context
== NULL
) {
650 session_p
->digest
.context
= alloc_digest(mech
);
652 if (session_p
->digest
.context
== NULL
) {
654 * put back original context into session in case
655 * allocation of new context has failed.
657 session_p
->digest
.context
= free_it
;
659 rv
= CKR_HOST_MEMORY
;
664 /* Restore crypto_active_op_t */
665 session_p
->digest
.mech
.mechanism
= mech
;
666 session_p
->digest
.flags
= p_active_op
->flags
;
670 /* Restore MD5_CTX from the saved digest operation */
671 (void) memcpy((CK_BYTE
*)session_p
->digest
.context
,
672 (CK_BYTE
*)pOperationState
+ offset
,
676 /* Restore SHA1_CTX from the saved digest operation */
677 (void) memcpy((CK_BYTE
*)session_p
->digest
.context
,
678 (CK_BYTE
*)pOperationState
+ offset
,
683 rv
= CKR_SAVED_STATE_INVALID
;
687 (void) pthread_mutex_unlock(&session_p
->session_mutex
);
696 soft_login(CK_UTF8CHAR_PTR pPin
, CK_ULONG ulPinLen
)
700 * Authenticate the input PIN.
702 return (soft_verify_pin(pPin
, ulPinLen
));
711 * Delete all the private token objects from the "token_object_list".
713 soft_delete_all_in_core_token_objects(PRIVATE_TOKEN
);
719 soft_acquire_all_session_mutexes(soft_session_t
*session_p
)
721 /* Iterate through sessions acquiring all mutexes */
723 soft_object_t
*object_p
;
725 (void) pthread_mutex_lock(&session_p
->session_mutex
);
726 object_p
= session_p
->object_list
;
728 /* Lock also all objects related to session */
730 (void) pthread_mutex_lock(&object_p
->object_mutex
);
731 object_p
= object_p
->next
;
733 session_p
= session_p
->next
;
738 soft_release_all_session_mutexes(soft_session_t
*session_p
)
740 /* Iterate through sessions releasing all mutexes */
743 * N.B. Ideally, should go in opposite order to guarantee
744 * lock-order requirements but there is no tail pointer.
746 soft_object_t
*object_p
= session_p
->object_list
;
748 /* Unlock also all objects related to session */
750 (void) pthread_mutex_unlock(&object_p
->object_mutex
);
751 object_p
= object_p
->next
;
753 (void) pthread_mutex_unlock(&session_p
->session_mutex
);
754 session_p
= session_p
->next
;