xref: /titanic_52/usr/src/lib/pkcs11/pkcs11_kernel/common/kernelSession.h (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 2004 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  * Flag definitions for ses_close_sync
88  */
89 #define	SESSION_IS_CLOSING	1	/* Session is in a closing state */
90 #define	SESSION_REFCNT_WAITING	2	/* Waiting for session reference */
91 					/* count to become zero */
92 
93 /*
94  * This macro is used to decrement the session reference count by one.
95  *
96  * The caller of this macro uses the argument lock_held to indicate that
97  * whether the caller holds the lock on the session or not.
98  *
99  * REFRELE macro does the following:
100  * 1) Get the session lock if the caller does not hold it.
101  * 2) Decrement the session reference count by one.
102  * 3) If the session reference count becomes zero after being decremented,
103  *    and there is a closing session thread in the wait state, then
104  *    call pthread_cond_signal() to wake up that thread who is blocked
105  *    in the session deletion routine due to non-zero reference ount.
106  * 4) Always release the session lock.
107  */
108 #define	REFRELE(s, ses_lock_held) { \
109 	if (!ses_lock_held) \
110 		(void) pthread_mutex_lock(&s->session_mutex);   \
111 	if ((--((s)->ses_refcnt) == 0) &&    \
112 	    (s->ses_close_sync & SESSION_REFCNT_WAITING)) {     \
113 		(void) pthread_mutex_unlock(&s->session_mutex);   \
114 		(void) pthread_cond_signal(&s->ses_free_cond); \
115 	} else {        \
116 		(void) pthread_mutex_unlock(&s->session_mutex);   \
117 	}       \
118 }
119 
120 
121 /*
122  * Function Prototypes.
123  */
124 CK_RV handle2session(CK_SESSION_HANDLE hSession, kernel_session_t **session_p);
125 
126 CK_RV kernel_delete_all_sessions(CK_SLOT_ID slotID, boolean_t wrapper_only);
127 
128 void kernel_delete_all_objects_in_session(kernel_session_t *sp,
129     boolean_t wrapper_only);
130 
131 CK_RV kernel_add_session(CK_SLOT_ID slotID, CK_FLAGS flags,
132     CK_VOID_PTR pApplication, CK_NOTIFY notify, CK_ULONG *phSession);
133 
134 CK_RV kernel_delete_session(CK_SLOT_ID slotID, kernel_session_t *sp,
135     boolean_t lock_held, boolean_t wrapper_only);
136 
137 #ifdef	__cplusplus
138 }
139 #endif
140 
141 #endif /* _KERNELSESSION_H */
142