xref: /illumos-gate/usr/src/lib/pkcs11/pkcs11_softtoken/common/softSession.h (revision 69a119caa6570c7077699161b7c28b6ee9f8b0f4)
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 #ifndef _SOFTSESSION_H
27 #define	_SOFTSESSION_H
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #include <pthread.h>
34 #include <security/pkcs11t.h>
35 
36 
37 #define	SOFTTOKEN_SESSION_MAGIC	0xECF00002
38 
39 /*
40  * This is only used by the C_G(S)etOperationState.
41  */
42 #define	DIGEST_OP		1
43 
44 /*
45  * This is only used by the C_G(S)etOperationState.
46  */
47 typedef struct internal_op_state {
48 	/* Holds the length of the saved state */
49 	CK_ULONG	op_len;
50 
51 	/* crypto operation to be saved or restored */
52 	CK_ULONG	op_active;
53 
54 	/* Holds the saved session state */
55 	CK_STATE	op_session_state;
56 
57 } internal_op_state_t;
58 
59 typedef struct crypto_active_op {
60 	CK_MECHANISM	mech;
61 	void		*context;
62 	uint32_t	flags;
63 } crypto_active_op_t;
64 
65 
66 /*
67  * Definition for flags in crypto_active_op_t
68  */
69 #define	CRYPTO_OPERATION_ACTIVE		1 /* Cryptoki operation is active */
70 #define	CRYPTO_OPERATION_UPDATE		2 /* Cryptoki multi-part op active */
71 #define	CRYPTO_KEY_DIGESTED		3 /* A C_DigestKey() was called */
72 
73 typedef struct session {
74 	ulong_t		magic_marker;	/* magic # be validated for integrity */
75 	pthread_mutex_t	session_mutex;	/* session's mutex lock */
76 	pthread_cond_t	ses_free_cond;	/* cond variable for signal and wait */
77 	uint32_t	ses_refcnt;	/* session reference count */
78 	uint32_t	ses_close_sync;	/* session closing flags */
79 	CK_STATE	state;		/* session state */
80 
81 	/* Place holder for parameters passed in the C_OpenSession */
82 	CK_FLAGS	flags;
83 	CK_NOTIFY	Notify;
84 	CK_VOID_PTR	pApplication;
85 
86 	/* Pointers to form the global session list */
87 	struct session	*next;		/* points to next session on the list */
88 	struct session	*prev;		/* points to prev session on the list */
89 
90 	struct object	*object_list;	/* points to list of objects */
91 
92 	crypto_active_op_t	digest;	/* context of active digest operation */
93 	crypto_active_op_t	encrypt; /* context of active encrypt op */
94 	crypto_active_op_t	decrypt; /* context of active decrypt op */
95 	crypto_active_op_t	sign;	/* context of active sign op */
96 	crypto_active_op_t	verify;	/* context of active verify op */
97 	/* context of active FindObjects op */
98 	crypto_active_op_t	find_objects;
99 } soft_session_t;
100 
101 /*
102  * slot_t is a global structure to be used only by the
103  * token objects to hold the token object related
104  * in-core information.
105  */
106 typedef struct slot {
107 	uint_t		ks_version;	/* in-core keystore version number */
108 	boolean_t	authenticated;	/* Has C_Login called */
109 	boolean_t	userpin_change_needed; /* set if PIN expired */
110 	pthread_mutex_t	slot_mutex;
111 	pthread_mutex_t	keystore_mutex; /* Protects keystore_load_status */
112 	uint_t		keystore_load_status; /* Keystore load status */
113 	/* points to in-core token object list */
114 	struct object	*token_object_list;
115 } slot_t;
116 
117 /*
118  * The following structure is used to link the to-be-freed sessions
119  * into a linked list. The sessions on this linked list have
120  * not yet been freed via free() after C_CloseSession() call; instead
121  * they are added to this list. The actual free will take place when
122  * the number of sessions queued reaches MAX_SES_TO_BE_FREED, at which
123  * time the first session in the list will be freed.
124  */
125 #define	MAX_SES_TO_BE_FREED		300
126 
127 typedef struct ses_to_be_freed_list {
128 	struct session	*first;	/* points to the first session in the list */
129 	struct session	*last;	/* points to the last session in the list */
130 	uint32_t	count;	/* current total sessions in the list */
131 	pthread_mutex_t	ses_to_be_free_mutex;
132 } ses_to_be_freed_list_t;
133 
134 /*
135  * Flag definitions for ses_close_sync
136  */
137 #define	SESSION_IS_CLOSING	1	/* Session is in a closing state */
138 #define	SESSION_REFCNT_WAITING	2	/* Waiting for session reference */
139 					/* count to become zero */
140 
141 /*
142  * This macro is used to decrement the session reference count by one.
143  *
144  * The caller of this macro uses the argument lock_held to indicate that
145  * whether the caller holds the lock on the session or not.
146  *
147  * SES_REFRELE macro does the following:
148  * 1) Get the session lock if the caller does not hold it.
149  * 2) Decrement the session reference count by one.
150  * 3) If the session reference count becomes zero after being decremented,
151  *    and there is a closing session thread in the wait state, then
152  *    call pthread_cond_signal() to wake up that thread who is blocked
153  *    in the session deletion routine due to non-zero reference ount.
154  * 4) Always release the session lock.
155  */
156 #define	SES_REFRELE(s, lock_held) { \
157 	if (!lock_held) \
158 		(void) pthread_mutex_lock(&s->session_mutex);   \
159 	if ((--((s)->ses_refcnt) == 0) &&    \
160 	    (s->ses_close_sync & SESSION_REFCNT_WAITING)) {     \
161 		(void) pthread_mutex_unlock(&s->session_mutex);   \
162 		(void) pthread_cond_signal(&s->ses_free_cond); \
163 	} else {        \
164 		(void) pthread_mutex_unlock(&s->session_mutex);   \
165 	}       \
166 }
167 
168 
169 extern pthread_mutex_t soft_sessionlist_mutex;
170 extern soft_session_t *soft_session_list;
171 extern int all_sessions_closing;
172 extern CK_ULONG soft_session_cnt;	/* the number of opened sessions */
173 extern CK_ULONG soft_session_rw_cnt;	/* the number of opened R/W sessions */
174 
175 
176 /*
177  * Function Prototypes.
178  */
179 CK_RV handle2session(CK_SESSION_HANDLE hSession, soft_session_t **session_p);
180 
181 CK_RV soft_delete_all_sessions(boolean_t force);
182 
183 void soft_delete_all_objects_in_session(soft_session_t *sp, boolean_t force);
184 
185 CK_RV soft_add_session(CK_FLAGS flags, CK_VOID_PTR pApplication,
186     CK_NOTIFY notify, CK_ULONG *phSession);
187 
188 CK_RV soft_delete_session(soft_session_t *sp,
189     boolean_t force, boolean_t lock_held);
190 
191 CK_RV soft_get_operationstate(soft_session_t *, CK_BYTE_PTR, CK_ULONG_PTR);
192 CK_RV soft_set_operationstate(soft_session_t *, CK_BYTE_PTR, CK_ULONG,
193     CK_OBJECT_HANDLE, CK_OBJECT_HANDLE);
194 
195 
196 /* Token object related function prototypes. */
197 
198 CK_RV soft_login(CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen);
199 
200 void soft_logout(void);
201 
202 void soft_acquire_all_session_mutexes(soft_session_t *session_p);
203 void soft_release_all_session_mutexes(soft_session_t *session_p);
204 
205 #ifdef	__cplusplus
206 }
207 #endif
208 
209 #endif /* _SOFTSESSION_H */
210