xref: /illumos-gate/usr/src/lib/pkcs11/pkcs11_softtoken/common/softSession.h (revision d6bb6a8465e557cb946ef49d56ed3202f6218652)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _SOFTSESSION_H
28 #define	_SOFTSESSION_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include <pthread.h>
37 #include <security/pkcs11t.h>
38 
39 
40 #define	SOFTTOKEN_SESSION_MAGIC	0xECF00002
41 
42 /*
43  * This is only used by the C_G(S)etOperationState.
44  */
45 #define	DIGEST_OP		1
46 
47 /*
48  * This is only used by the C_G(S)etOperationState.
49  */
50 typedef struct internal_op_state {
51 	/* Holds the length of the saved state */
52 	CK_ULONG	op_len;
53 
54 	/* crypto operation to be saved or restored */
55 	CK_ULONG	op_active;
56 
57 	/* Holds the saved session state */
58 	CK_STATE	op_session_state;
59 
60 } internal_op_state_t;
61 
62 typedef struct crypto_active_op {
63 	CK_MECHANISM	mech;
64 	void		*context;
65 	uint32_t	flags;
66 } crypto_active_op_t;
67 
68 
69 /*
70  * Definition for flags in crypto_active_op_t
71  */
72 #define	CRYPTO_OPERATION_ACTIVE		1 /* Cryptoki operation is active */
73 #define	CRYPTO_OPERATION_UPDATE		2 /* Cryptoki multi-part op active */
74 #define	CRYPTO_KEY_DIGESTED		3 /* A C_DigestKey() was called */
75 
76 typedef struct session {
77 	ulong_t		magic_marker;	/* magic # be validated for integrity */
78 	pthread_mutex_t	session_mutex;	/* session's mutex lock */
79 	pthread_cond_t	ses_free_cond;	/* cond variable for signal and wait */
80 	uint32_t	ses_refcnt;	/* session reference count */
81 	uint32_t	ses_close_sync;	/* session closing flags */
82 	CK_STATE	state;		/* session state */
83 
84 	/* Place holder for parameters passed in the C_OpenSession */
85 	CK_FLAGS	flags;
86 	CK_NOTIFY	Notify;
87 	CK_VOID_PTR	pApplication;
88 
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 */
92 
93 	struct object	*object_list;	/* points to list of objects */
94 
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 	/* context of active FindObjects op */
101 	crypto_active_op_t	find_objects;
102 } soft_session_t;
103 
104 /*
105  * slot_t is a global structure to be used only by the
106  * token objects to hold the token object related
107  * in-core information.
108  */
109 typedef struct slot {
110 	uint_t		ks_version;	/* in-core keystore version number */
111 	boolean_t	authenticated;	/* Has C_Login called */
112 	boolean_t	userpin_change_needed; /* set if PIN expired */
113 	pthread_mutex_t	slot_mutex;
114 	/* points to in-core token object list */
115 	struct object	*token_object_list;
116 } slot_t;
117 
118 /*
119  * The following structure is used to link the to-be-freed sessions
120  * into a linked list. The sessions on this linked list have
121  * not yet been freed via free() after C_CloseSession() call; instead
122  * they are added to this list. The actual free will take place when
123  * the number of sessions queued reaches MAX_SES_TO_BE_FREED, at which
124  * time the first session in the list will be freed.
125  */
126 #define	MAX_SES_TO_BE_FREED		300
127 
128 typedef struct ses_to_be_freed_list {
129 	struct session	*first;	/* points to the first session in the list */
130 	struct session	*last;	/* points to the last session in the list */
131 	uint32_t	count;	/* current total sessions in the list */
132 	pthread_mutex_t	ses_to_be_free_mutex;
133 } ses_to_be_freed_list_t;
134 
135 /*
136  * Flag definitions for ses_close_sync
137  */
138 #define	SESSION_IS_CLOSING	1	/* Session is in a closing state */
139 #define	SESSION_REFCNT_WAITING	2	/* Waiting for session reference */
140 					/* count to become zero */
141 
142 /*
143  * This macro is used to decrement the session reference count by one.
144  *
145  * The caller of this macro uses the argument lock_held to indicate that
146  * whether the caller holds the lock on the session or not.
147  *
148  * SES_REFRELE macro does the following:
149  * 1) Get the session lock if the caller does not hold it.
150  * 2) Decrement the session reference count by one.
151  * 3) If the session reference count becomes zero after being decremented,
152  *    and there is a closing session thread in the wait state, then
153  *    call pthread_cond_signal() to wake up that thread who is blocked
154  *    in the session deletion routine due to non-zero reference ount.
155  * 4) Always release the session lock.
156  */
157 #define	SES_REFRELE(s, lock_held) { \
158 	if (!lock_held) \
159 		(void) pthread_mutex_lock(&s->session_mutex);   \
160 	if ((--((s)->ses_refcnt) == 0) &&    \
161 	    (s->ses_close_sync & SESSION_REFCNT_WAITING)) {     \
162 		(void) pthread_mutex_unlock(&s->session_mutex);   \
163 		(void) pthread_cond_signal(&s->ses_free_cond); \
164 	} else {        \
165 		(void) pthread_mutex_unlock(&s->session_mutex);   \
166 	}       \
167 }
168 
169 
170 extern pthread_mutex_t soft_sessionlist_mutex;
171 extern soft_session_t *soft_session_list;
172 extern int all_sessions_closing;
173 extern CK_ULONG soft_session_cnt;	/* the number of opened sessions */
174 extern CK_ULONG soft_session_rw_cnt;	/* the number of opened R/W sessions */
175 
176 
177 /*
178  * Function Prototypes.
179  */
180 CK_RV handle2session(CK_SESSION_HANDLE hSession, soft_session_t **session_p);
181 
182 CK_RV soft_delete_all_sessions(boolean_t force);
183 
184 void soft_delete_all_objects_in_session(soft_session_t *sp);
185 
186 CK_RV soft_add_session(CK_FLAGS flags, CK_VOID_PTR pApplication,
187     CK_NOTIFY notify, CK_ULONG *phSession);
188 
189 CK_RV soft_delete_session(soft_session_t *sp,
190     boolean_t force, boolean_t lock_held);
191 
192 CK_RV soft_get_operationstate(soft_session_t *, CK_BYTE_PTR, CK_ULONG_PTR);
193 CK_RV soft_set_operationstate(soft_session_t *, CK_BYTE_PTR, CK_ULONG,
194     CK_OBJECT_HANDLE, CK_OBJECT_HANDLE);
195 
196 
197 /* Token object related function prototypes. */
198 
199 CK_RV soft_login(CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen);
200 
201 void soft_logout(void);
202 
203 #ifdef	__cplusplus
204 }
205 #endif
206 
207 #endif /* _SOFTSESSION_H */
208