xref: /titanic_41/usr/src/lib/pkcs11/pkcs11_kernel/common/kernelSession.h (revision 4eaa471005973e11a6110b69fe990530b3b95a38)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _KERNELSESSION_H
27 #define	_KERNELSESSION_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 #include <pthread.h>
36 #include <sys/crypto/common.h>
37 #include <security/pkcs11t.h>
38 
39 
40 #define	KERNELTOKEN_SESSION_MAGIC	0xECF00003
41 
42 typedef struct crypto_active_op {
43 	CK_MECHANISM	mech;
44 	void		*context;
45 	uint32_t	flags;
46 } crypto_active_op_t;
47 
48 #define	EDIGEST_LENGTH  1024
49 
50 /* Used for emulating digest and HMAC mechs */
51 typedef struct digest_buf {
52 	uint8_t		*buf;
53 	int		buf_len;
54 	int		indata_len;
55 	void		*soft_sp;
56 } digest_buf_t;
57 
58 /*
59  * Definition for flags in crypto_active_op_t
60  *
61  * CRYPTO_EMULATE flag is set for a digest or sign/verify with a HMAC
62  * mechanism, if the session slot has a CRYPTO_LIMITED_HASH_SUPPORT flag set.
63  * CRYPTO_EMULATE_USING_SW flag is meaningful only when CRYPTO_EMULATE flag
64  * is set. And CRYPTO_EMULATE_UPDATE_DONE flag is meaningful only when
65  * CRYPTO_EMULATE_USING_SW flag is set.
66  */
67 #define	CRYPTO_OPERATION_ACTIVE	0x00000001 /* Cryptoki operation is active */
68 #define	CRYPTO_OPERATION_UPDATE	0x00000002 /* Cryptoki multi-part op active */
69 #define	CRYPTO_EMULATE		0x00000004 /* op needs emulation */
70 #define	CRYPTO_EMULATE_USING_SW	0x00000008 /* ... use software */
71 #define	CRYPTO_EMULATE_UPDATE_DONE 0x00000010 /* did at least one update */
72 #define	CRYPTO_EMULATE_INIT_DONE 0x00000020 /* did init */
73 
74 typedef struct session {
75 	CK_ULONG	magic_marker;	/* magic # be validated for integrity */
76 	pthread_mutex_t	session_mutex;	/* session's mutex lock */
77 	pthread_mutex_t ses_free_mutex;	/* mutex used during closing session */
78 	pthread_cond_t	ses_free_cond;	/* cond variable for signal and wait */
79 	uint32_t	ses_refcnt;	/* session reference count */
80 	uint32_t	ses_close_sync;	/* session closing flags */
81 	crypto_session_id_t k_session;	/* kernel session ID */
82 	boolean_t	ses_RO;		/* RO or RW session flag */
83 	CK_SLOT_ID	ses_slotid;	/* slotID saved from C_OpenSession() */
84 
85 	/* Place holder for parameters passed in the C_OpenSession */
86 	CK_FLAGS	flags;
87 	CK_NOTIFY	Notify;
88 	CK_VOID_PTR	pApplication;
89 
90 	/* Pointers to form the global session list */
91 	struct session	*next;		/* points to next session on the list */
92 	struct session	*prev;		/* points to prev session on the list */
93 
94 	struct object	*object_list;	/* points to list of objects */
95 
96 	crypto_active_op_t	digest;	/* context of active digest operation */
97 	crypto_active_op_t	encrypt; /* context of active encrypt op */
98 	crypto_active_op_t	decrypt; /* context of active decrypt op */
99 	crypto_active_op_t	sign;	/* context of active sign op */
100 	crypto_active_op_t	verify;	/* context of active verify op */
101 	crypto_active_op_t	find_objects;
102 } kernel_session_t;
103 
104 /*
105  * The following structure is used to link the to-be-freed sessions
106  * into a linked list. The sessions on this linked list have
107  * not yet been freed via free() after C_CloseSession() call; instead
108  * they are added to this list. The actual free will take place when
109  * the number of sessions queued reaches MAX_SES_TO_BE_FREED, at which
110  * time the first session in the list will be freed.
111  */
112 #define	MAX_SES_TO_BE_FREED		300
113 
114 typedef struct ses_to_be_freed_list {
115 	kernel_session_t *first; /* points to the first session in the list */
116 	kernel_session_t *last;  /* points to the last session in the list */
117 	uint32_t	count;   /* current total sessions in the list */
118 	pthread_mutex_t ses_to_be_free_mutex;
119 } ses_to_be_freed_list_t;
120 
121 extern ses_to_be_freed_list_t ses_delay_freed;
122 
123 /*
124  * Flag definitions for ses_close_sync
125  */
126 #define	SESSION_IS_CLOSING	1	/* Session is in a closing state */
127 #define	SESSION_REFCNT_WAITING	2	/* Waiting for session reference */
128 					/* count to become zero */
129 /*
130  * This macro is used to decrement the session reference count by one.
131  *
132  * The caller of this macro uses the argument lock_held to indicate that
133  * whether the caller holds the lock on the session or not.
134  *
135  * REFRELE macro does the following:
136  * 1) Get the session lock if the caller does not hold it.
137  * 2) Decrement the session reference count by one.
138  * 3) If the session reference count becomes zero after being decremented,
139  *    and there is a closing session thread in the wait state, then
140  *    call pthread_cond_signal() to wake up that thread who is blocked
141  *    in the session deletion routine due to non-zero reference ount.
142  * 4) Always release the session lock.
143  */
144 #define	REFRELE(s, ses_lock_held) { \
145 	if (!ses_lock_held) \
146 		(void) pthread_mutex_lock(&s->session_mutex);   \
147 	if ((--((s)->ses_refcnt) == 0) &&    \
148 	    (s->ses_close_sync & SESSION_REFCNT_WAITING)) {     \
149 		(void) pthread_mutex_unlock(&s->session_mutex);   \
150 		(void) pthread_cond_signal(&s->ses_free_cond); \
151 	} else {        \
152 		(void) pthread_mutex_unlock(&s->session_mutex);   \
153 	}       \
154 }
155 
156 
157 /*
158  * Function Prototypes.
159  */
160 CK_RV handle2session(CK_SESSION_HANDLE hSession, kernel_session_t **session_p);
161 
162 void kernel_delete_all_sessions(CK_SLOT_ID slotID, boolean_t wrapper_only);
163 
164 void kernel_delete_all_objects_in_session(kernel_session_t *sp,
165     boolean_t wrapper_only);
166 
167 CK_RV kernel_add_session(CK_SLOT_ID slotID, CK_FLAGS flags,
168     CK_VOID_PTR pApplication, CK_NOTIFY notify, CK_ULONG *phSession);
169 
170 void kernel_delete_session(CK_SLOT_ID slotID, kernel_session_t *sp,
171     boolean_t lock_held, boolean_t wrapper_only);
172 
173 void kernel_session_delay_free(kernel_session_t *sp);
174 
175 void kernel_acquire_all_slots_mutexes();
176 void kernel_release_all_slots_mutexes();
177 
178 #ifdef	__cplusplus
179 }
180 #endif
181 
182 #endif /* _KERNELSESSION_H */
183