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	_PKCS11_SLOT_H
28 #define	_PKCS11_SLOT_H
29 
30 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 #include "pkcs11Session.h"
35 
36 #define	MECHLIST_SIZE	32
37 
38 /*
39  * Used to pass arguments to child threads for C_WaitForSlotEvent.
40  */
41 typedef struct wfse_args {
42 
43 	CK_FLAGS flags;
44 	CK_VOID_PTR pReserved;
45 	CK_SLOT_ID slotid;
46 
47 } wfse_args_t;
48 
49 typedef struct pkcs11_slot {
50 
51 	CK_SLOT_ID		sl_id;  	/* real slotID from provider */
52 	struct pkcs11_session 	*sl_sess_list;	/* all open sessions */
53 	pthread_mutex_t		sl_mutex;	/* protects: sl_sess_list, */
54 						/* sl_tid, sl_wfse_state, */
55 						/* and sl_wfse_args */
56 	CK_FUNCTION_LIST_PTR 	sl_func_list;	/* function entry points */
57 	boolean_t		sl_enabledpol;	/* TRUE if policy for enabled */
58 	CK_MECHANISM_TYPE_PTR	sl_pol_mechs;	/* policy restricted */
59 	uint_t			sl_pol_count;	/* policy restricted */
60 	boolean_t		sl_norandom;	/* TRUE if random is disabled */
61 	void			*sl_dldesc;	/* from dlopen */
62 	uint_t			sl_prov_id;	/* set by order read in */
63 	uchar_t			sl_wfse_state;	/* Used by C_WaitForSlotEvent */
64 	boolean_t		sl_no_wfse;	/* WaitForSlotEvent not impl */
65 	pthread_t		sl_tid;		/* Used to track child thread */
66 	wfse_args_t		*sl_wfse_args;	/* Used for WaitForSlotEvent */
67 
68 } pkcs11_slot_t;
69 
70 /*
71  * State definitions used for C_WaitForSlotEvent, stored in sl_wfse_state
72  * for each slot.  These states are mutually exclusive, ie only one should
73  * be set at a time.
74  */
75 #define	WFSE_CLEAR	0x0
76 #define	WFSE_EVENT	0x1
77 #define	WFSE_ACTIVE	0x2
78 
79 /*
80  * Dynamically allocated array of slots, indexed by the slotID assigned
81  * by the framework.  st_first will be initialized to 1.  Only if there
82  * is more than one other slot present, triggering the existence of the
83  * metaslot, with st_first be set to 0.  st_last will be set to the
84  * last slotID assigned, also used for looping through the slottable.
85  */
86 typedef struct pkcs11_slottable {
87 
88 	pkcs11_slot_t	**st_slots;
89 	pthread_mutex_t	st_mutex;	/* Protects all data in the slottable */
90 					/* except for st_start_cond. */
91 	CK_SLOT_ID	st_first;	/* First used slot ID, used for loops */
92 	CK_SLOT_ID	st_last;	/* Last slot ID allocated */
93 	ulong_t		st_cur_size; 	/* current memory allocated */
94 	pthread_cond_t  st_wait_cond;   /* Used for C_WaitForSlotEvent */
95 	CK_SLOT_ID	st_event_slot;	/* Slot with event */
96 	boolean_t	st_wfse_active; /* A thread is actively running WFSE */
97 	boolean_t	st_blocking;	/* Blocking for C_WaitForSlotEvent */
98 	boolean_t	st_list_signaled; /* Listener has been signaled */
99 	uint_t		st_thr_count;	/* Used for C_WaitForSlotEvent */
100 	pthread_t	st_tid;
101 	pthread_mutex_t st_start_mutex; /* wait for listener to start */
102 	pthread_cond_t	st_start_cond;	/* signal when listener has started */
103 
104 } pkcs11_slottable_t;
105 
106 
107 /*
108  * This macro is used to quickly derefence from a framework slot ID,
109  * provided by an application, to the function pointers for the correct
110  * underlying provider.
111  */
112 #define	FUNCLIST(slotID) (slottable->st_slots[(slotID)]->sl_func_list)
113 
114 /*
115  * This macro is used to quickly get the slot ID associated with this
116  * slot ID, that is used by the underlying provider.
117  */
118 #define	TRUEID(slotID) (slottable->st_slots[(slotID)]->sl_id)
119 
120 
121 extern pkcs11_slottable_t *slottable;
122 
123 extern CK_RV pkcs11_slottable_initialize();
124 extern CK_RV pkcs11_slottable_increase(ulong_t increase);
125 extern CK_RV pkcs11_slot_allocate(CK_SLOT_ID *slot);
126 extern CK_RV pkcs11_slottable_delete();
127 extern CK_RV pkcs11_is_valid_slot(CK_SLOT_ID slot_id);
128 extern CK_RV pkcs11_validate_and_convert_slotid(CK_SLOT_ID slot_id,
129     CK_SLOT_ID *real_slot_id);
130 
131 #ifdef __cplusplus
132 }
133 #endif
134 
135 #endif /* _PKCS11_SLOT_H */
136