xref: /titanic_41/usr/src/lib/pkcs11/pkcs11_kernel/common/kernelSession.h (revision 414388d7cb2ee98771e2ac7c2338c460abd44304)
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 _KERNELSESSION_H
28 #define	_KERNELSESSION_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 <sys/crypto/common.h>
38 #include <security/pkcs11t.h>
39 
40 
41 #define	KERNELTOKEN_SESSION_MAGIC	0xECF00003
42 
43 typedef struct crypto_active_op {
44 	CK_MECHANISM	mech;
45 	void		*context;
46 	uint32_t	flags;
47 } crypto_active_op_t;
48 
49 
50 /*
51  * Definition for flags in crypto_active_op_t
52  */
53 #define	CRYPTO_OPERATION_ACTIVE		1 /* Cryptoki operation is active */
54 #define	CRYPTO_OPERATION_UPDATE		2 /* Cryptoki multi-part op active */
55 
56 typedef struct session {
57 	CK_ULONG	magic_marker;	/* magic # be validated for integrity */
58 	pthread_mutex_t	session_mutex;	/* session's mutex lock */
59 	pthread_mutex_t ses_free_mutex;	/* mutex used during closing session */
60 	pthread_cond_t	ses_free_cond;	/* cond variable for signal and wait */
61 	uint32_t	ses_refcnt;	/* session reference count */
62 	uint32_t	ses_close_sync;	/* session closing flags */
63 	crypto_session_id_t k_session;	/* kernel session ID */
64 	boolean_t	ses_RO;		/* RO or RW session flag */
65 	CK_SLOT_ID	ses_slotid;	/* slotID saved from C_OpenSession() */
66 
67 	/* Place holder for parameters passed in the C_OpenSession */
68 	CK_FLAGS	flags;
69 	CK_NOTIFY	Notify;
70 	CK_VOID_PTR	pApplication;
71 
72 	/* Pointers to form the global session list */
73 	struct session	*next;		/* points to next session on the list */
74 	struct session	*prev;		/* points to prev session on the list */
75 
76 	struct object	*object_list;	/* points to list of objects */
77 
78 	crypto_active_op_t	digest;	/* context of active digest operation */
79 	crypto_active_op_t	encrypt; /* context of active encrypt op */
80 	crypto_active_op_t	decrypt; /* context of active decrypt op */
81 	crypto_active_op_t	sign;	/* context of active sign op */
82 	crypto_active_op_t	verify;	/* context of active verify op */
83 	crypto_active_op_t	find_objects;
84 } kernel_session_t;
85 
86 /*
87  * The following structure is used to link the to-be-freed sessions
88  * into a linked list. The sessions on this linked list have
89  * not yet been freed via free() after C_CloseSession() call; instead
90  * they are added to this list. The actual free will take place when
91  * the number of sessions queued reaches MAX_SES_TO_BE_FREED, at which
92  * time the first session in the list will be freed.
93  */
94 #define	MAX_SES_TO_BE_FREED		300
95 
96 typedef struct ses_to_be_freed_list {
97 	kernel_session_t *first; /* points to the first session in the list */
98 	kernel_session_t *last;  /* points to the last session in the list */
99 	uint32_t	count;   /* current total sessions in the list */
100 	pthread_mutex_t ses_to_be_free_mutex;
101 } ses_to_be_freed_list_t;
102 
103 extern ses_to_be_freed_list_t ses_delay_freed;
104 
105 /*
106  * Flag definitions for ses_close_sync
107  */
108 #define	SESSION_IS_CLOSING	1	/* Session is in a closing state */
109 #define	SESSION_REFCNT_WAITING	2	/* Waiting for session reference */
110 					/* count to become zero */
111 /*
112  * This macro is used to decrement the session reference count by one.
113  *
114  * The caller of this macro uses the argument lock_held to indicate that
115  * whether the caller holds the lock on the session or not.
116  *
117  * REFRELE macro does the following:
118  * 1) Get the session lock if the caller does not hold it.
119  * 2) Decrement the session reference count by one.
120  * 3) If the session reference count becomes zero after being decremented,
121  *    and there is a closing session thread in the wait state, then
122  *    call pthread_cond_signal() to wake up that thread who is blocked
123  *    in the session deletion routine due to non-zero reference ount.
124  * 4) Always release the session lock.
125  */
126 #define	REFRELE(s, ses_lock_held) { \
127 	if (!ses_lock_held) \
128 		(void) pthread_mutex_lock(&s->session_mutex);   \
129 	if ((--((s)->ses_refcnt) == 0) &&    \
130 	    (s->ses_close_sync & SESSION_REFCNT_WAITING)) {     \
131 		(void) pthread_mutex_unlock(&s->session_mutex);   \
132 		(void) pthread_cond_signal(&s->ses_free_cond); \
133 	} else {        \
134 		(void) pthread_mutex_unlock(&s->session_mutex);   \
135 	}       \
136 }
137 
138 
139 /*
140  * Function Prototypes.
141  */
142 CK_RV handle2session(CK_SESSION_HANDLE hSession, kernel_session_t **session_p);
143 
144 void kernel_delete_all_sessions(CK_SLOT_ID slotID, boolean_t wrapper_only);
145 
146 void kernel_delete_all_objects_in_session(kernel_session_t *sp,
147     boolean_t wrapper_only);
148 
149 CK_RV kernel_add_session(CK_SLOT_ID slotID, CK_FLAGS flags,
150     CK_VOID_PTR pApplication, CK_NOTIFY notify, CK_ULONG *phSession);
151 
152 void kernel_delete_session(CK_SLOT_ID slotID, kernel_session_t *sp,
153     boolean_t lock_held, boolean_t wrapper_only);
154 
155 void kernel_session_delay_free(kernel_session_t *sp);
156 
157 #ifdef	__cplusplus
158 }
159 #endif
160 
161 #endif /* _KERNELSESSION_H */
162