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.
26 #ifndef _KERNELSESSION_H
27 #define _KERNELSESSION_H
34 #include <sys/crypto/common.h>
35 #include <security/pkcs11t.h>
38 #define KERNELTOKEN_SESSION_MAGIC 0xECF00003
40 typedef struct crypto_active_op
{
46 #define EDIGEST_LENGTH 1024
48 /* Used for emulating digest and HMAC mechs */
49 typedef struct digest_buf
{
57 * Definition for flags in crypto_active_op_t
59 * CRYPTO_EMULATE flag is set for a digest or sign/verify with a HMAC
60 * mechanism, if the session slot has a CRYPTO_LIMITED_HASH_SUPPORT flag set.
61 * CRYPTO_EMULATE_USING_SW flag is meaningful only when CRYPTO_EMULATE flag
62 * is set. And CRYPTO_EMULATE_UPDATE_DONE flag is meaningful only when
63 * CRYPTO_EMULATE_USING_SW flag is set.
65 #define CRYPTO_OPERATION_ACTIVE 0x00000001 /* Cryptoki operation is active */
66 #define CRYPTO_OPERATION_UPDATE 0x00000002 /* Cryptoki multi-part op active */
67 #define CRYPTO_EMULATE 0x00000004 /* op needs emulation */
68 #define CRYPTO_EMULATE_USING_SW 0x00000008 /* ... use software */
69 #define CRYPTO_EMULATE_UPDATE_DONE 0x00000010 /* did at least one update */
70 #define CRYPTO_EMULATE_INIT_DONE 0x00000020 /* did init */
71 #define CRYPTO_OPERATION_INPLACE_OK 0x00000040 /* INPLACE_MECHANISM is true */
73 typedef struct session
{
74 CK_ULONG magic_marker
; /* magic # be validated for integrity */
75 pthread_mutex_t session_mutex
; /* session's mutex lock */
76 pthread_mutex_t ses_free_mutex
; /* mutex used during closing session */
77 pthread_cond_t ses_free_cond
; /* cond variable for signal and wait */
78 uint32_t ses_refcnt
; /* session reference count */
79 uint32_t ses_close_sync
; /* session closing flags */
80 crypto_session_id_t k_session
; /* kernel session ID */
81 boolean_t ses_RO
; /* RO or RW session flag */
82 CK_SLOT_ID ses_slotid
; /* slotID saved from C_OpenSession() */
84 /* Place holder for parameters passed in the C_OpenSession */
87 CK_VOID_PTR pApplication
;
89 /* Pointers to form the global session list */
90 struct session
*next
; /* points to next session on the list */
91 struct session
*prev
; /* points to prev session on the list */
93 struct object
*object_list
; /* points to list of objects */
95 crypto_active_op_t digest
; /* context of active digest operation */
96 crypto_active_op_t encrypt
; /* context of active encrypt op */
97 crypto_active_op_t decrypt
; /* context of active decrypt op */
98 crypto_active_op_t sign
; /* context of active sign op */
99 crypto_active_op_t verify
; /* context of active verify op */
100 crypto_active_op_t find_objects
;
104 * The following structure is used to link the to-be-freed sessions
105 * into a linked list. The sessions on this linked list have
106 * not yet been freed via free() after C_CloseSession() call; instead
107 * they are added to this list. The actual free will take place when
108 * the number of sessions queued reaches MAX_SES_TO_BE_FREED, at which
109 * time the first session in the list will be freed.
111 #define MAX_SES_TO_BE_FREED 300
113 typedef struct ses_to_be_freed_list
{
114 kernel_session_t
*first
; /* points to the first session in the list */
115 kernel_session_t
*last
; /* points to the last session in the list */
116 uint32_t count
; /* current total sessions in the list */
117 pthread_mutex_t ses_to_be_free_mutex
;
118 } ses_to_be_freed_list_t
;
120 extern ses_to_be_freed_list_t ses_delay_freed
;
123 * Flag definitions for ses_close_sync
125 #define SESSION_IS_CLOSING 1 /* Session is in a closing state */
126 #define SESSION_REFCNT_WAITING 2 /* Waiting for session reference */
127 /* count to become zero */
129 * This macro is used to decrement the session reference count by one.
131 * The caller of this macro uses the argument lock_held to indicate that
132 * whether the caller holds the lock on the session or not.
134 * REFRELE macro does the following:
135 * 1) Get the session lock if the caller does not hold it.
136 * 2) Decrement the session reference count by one.
137 * 3) If the session reference count becomes zero after being decremented,
138 * and there is a closing session thread in the wait state, then
139 * call pthread_cond_signal() to wake up that thread who is blocked
140 * in the session deletion routine due to non-zero reference ount.
141 * 4) Always release the session lock.
143 #define REFRELE(s, ses_lock_held) { \
144 if (!ses_lock_held) \
145 (void) pthread_mutex_lock(&s->session_mutex); \
146 if ((--((s)->ses_refcnt) == 0) && \
147 (s->ses_close_sync & SESSION_REFCNT_WAITING)) { \
148 (void) pthread_mutex_unlock(&s->session_mutex); \
149 (void) pthread_cond_signal(&s->ses_free_cond); \
151 (void) pthread_mutex_unlock(&s->session_mutex); \
157 * Function Prototypes.
159 CK_RV
handle2session(CK_SESSION_HANDLE hSession
, kernel_session_t
**session_p
);
161 void kernel_delete_all_sessions(CK_SLOT_ID slotID
, boolean_t wrapper_only
);
163 void kernel_delete_all_objects_in_session(kernel_session_t
*sp
,
164 boolean_t wrapper_only
);
166 CK_RV
kernel_add_session(CK_SLOT_ID slotID
, CK_FLAGS flags
,
167 CK_VOID_PTR pApplication
, CK_NOTIFY notify
, CK_ULONG
*phSession
);
169 void kernel_delete_session(CK_SLOT_ID slotID
, kernel_session_t
*sp
,
170 boolean_t lock_held
, boolean_t wrapper_only
);
172 void kernel_session_delay_free(kernel_session_t
*sp
);
174 void kernel_acquire_all_slots_mutexes();
175 void kernel_release_all_slots_mutexes();
181 #endif /* _KERNELSESSION_H */