1 /*
2  * COPYRIGHT (c) International Business Machines Corp. 2001-2017
3  *
4  * This program is provided under the terms of the Common Public License,
5  * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
6  * software constitutes recipient's acceptance of CPL-1.0 terms which can be
7  * found in the file LICENSE file or at
8  * https://opensource.org/licenses/cpl1.0.php
9  */
10 
11 //
12 //Slot Manager Daemon  Constants...
13 //
14 //
15 
16 
17 #include <pkcs11types.h>
18 #include <limits.h>
19 #include <local_types.h>
20 #include <pthread.h>
21 #include <sys/mman.h>
22 
23 #include "local_types.h"
24 
25 #ifndef _SLOTMGR_H
26 #define _SLOTMGR_H
27 
28 #define TOK_PATH  SBIN_PATH "/pkcsslotd"
29 #define OCK_API_LOCK_FILE LOCKDIR_PATH "/LCK..APIlock"
30 
31 #define SOCKET_FILE_PATH "/var/run/pkcsslotd.socket"
32 
33 #define PID_FILE_PATH "/var/run/pkcsslotd.pid"
34 #define OCK_CONFIG OCK_CONFDIR "/opencryptoki.conf"
35 
36 #ifndef CK_BOOL
37 #define CK_BOOL  CK_BBOOL
38 #endif                          /* CK_BOOL */
39 
40 #ifndef TEST_COND_VARS
41 #define TEST_COND_VARS 0
42 #endif                          /* TEST_COND_VARS */
43 
44 #define NUMBER_SLOTS_MANAGED 1024
45 #define NUMBER_PROCESSES_ALLOWED  1000
46 
47 //
48 // Per Process Data structure
49 // one entry in the table is grabbed by each process
50 // when it attaches to the shared memory and released
51 // when the C_Finalize is called.
52 
53 typedef struct {
54     pthread_mutex_t proc_mutex;
55     pthread_cond_t proc_slot_cond;
56 
57     CK_BOOL inuse;              // flag indicating if the entry is in use
58     pid_t proc_id;              /* This could also be used to indicate inuse.
59                                  * however we will actualy use it to provide
60                                  * a check for a bad process which did not
61                                  * C_finalize and remove itself properly.
62                                  */
63     uint32 slotmap;             /* Bit map of the slots with events App uses
64                                  * in the C_WaitForSlotEvent call
65                                  */
66 
67     uint8 blocking;             /* Flag to use if a thread is blocking on the
68                                  * condition variable Used by C_Finalize to
69                                  * wake up the
70                                  */
71 
72     uint8 error;                /* indication of an error causing the thread
73                                  * sleeping on the condition variable to wakeup.
74                                  */
75     uint32 slot_session_count[NUMBER_SLOTS_MANAGED];    /* Per process session
76                                                          * count for garbage
77                                                          * collection clean up
78                                                          * of the global
79                                                          * session count.
80                                                          */
81     time_t reg_time;            // Time application registered
82 } Slot_Mgr_Proc_t;
83 
84 
85 // Slot info structure which contains the PKCS11 CK_SLOT_INFO
86 // as well as the local information
87 typedef struct {
88     CK_SLOT_ID slot_number;
89     CK_BOOL present;
90     CK_SLOT_INFO pk_slot;
91     char dll_location[NAME_MAX + 1];    // location of slot management  DLL
92     char slot_init_fcn[NAME_MAX + 1];   /* function to call to initialize the
93                                          * token in the slot
94                                          */
95     LW_SHM_TYPE *shm_addr;      // token specific shm address
96 } Slot_Info_t;
97 
98 
99 #ifdef PKCS64
100 
101 /*
102  * Constant size types and structures to allow 32-bit daemon to work with
103  * 64-bit libraries.
104  *
105  * Note - ulong long is 8 bytes for both 32-bit and 64-bit applications.
106  *
107  */
108 
109 typedef signed long long pid_t_64;
110 typedef unsigned long long time_t_64;
111 typedef unsigned long long CK_SLOT_ID_64;
112 typedef unsigned long long CK_FLAGS_64;
113 
114 typedef struct CK_INFO_64 {
115     CK_VERSION cryptokiVersion; /* Cryptoki interface ver */
116     CK_CHAR manufacturerID[32]; /* blank padded */
117     CK_CHAR pad1[6];            /* pad for dword alignment */
118     CK_FLAGS_64 flags;          /* must be zero */
119 
120     /* libraryDescription and libraryVersion are new for v2.0 */
121     CK_CHAR libraryDescription[32];     /* blank padded */
122     CK_VERSION libraryVersion;  /* version of library */
123     CK_CHAR pad2[6];            /* pad for dword alignment */
124 } CK_INFO_64;
125 
126 typedef CK_INFO_64 CK_PTR CK_INFO_PTR_64;
127 
128 typedef struct CK_SLOT_INFO_64 {
129     CK_CHAR slotDescription[64];        /* blank padded */
130     CK_CHAR manufacturerID[32]; /* blank padded */
131     CK_FLAGS_64 flags;
132 
133     /* hardwareVersion and firmwareVersion are new for v2.0 */
134     CK_VERSION hardwareVersion; /* version of hardware */
135     CK_VERSION firmwareVersion; /* version of firmware */
136     CK_CHAR pad[4];             /* pad for dword alignment */
137 } CK_SLOT_INFO_64;
138 
139 
140 typedef struct Slot_Mgr_Proc_t_64 {
141     // pthread_cond_t   proc_slot_cond;
142 
143     CK_BOOL inuse;              // flag indicating if the entry is in use
144     pid_t proc_id;              /* This could also be used to indicate inuse.
145                                  * however we will actualy use it to provide
146                                  * a check for a bad process which did not
147                                  * C_finalize and remove itself properly.
148                                  */
149     uint32 slotmap;             /* Bit map of the slots with events App uses
150                                  * this in the C_WaitForSlotEvent call
151                                  */
152 
153     uint8 blocking;             /* Flag to use if a thread is blocking on the
154                                  * condition variable Used by C_Finalize to
155                                  * wake up the
156                                  */
157 
158     uint8 error;                /* indication of an error causing the thread
159                                  * sleeping on the condition variable to wakeup.
160                                  */
161     uint32 slot_session_count[NUMBER_SLOTS_MANAGED];    /* Per process session
162                                                          * count for garbage
163                                                          * collection clean up
164                                                          * of the global
165                                                          * session count.
166                                                          */
167     time_t_64 reg_time;         // Time application registered
168 } Slot_Mgr_Proc_t_64;
169 
170 //
171 // Shared Memory Region of Slot information
172 //
173 
174 // Slot info structure which contains the PKCS11 CK_SLOT_INFO
175 // as well as the local information
176 typedef struct {
177     CK_SLOT_ID_64 slot_number;
178     CK_BOOL present;
179     char pad1[7];               // pad for dword alignment
180     CK_SLOT_INFO_64 pk_slot;
181     char dll_location[NAME_MAX + 1];    // location of slot's  DLL
182     char confname[NAME_MAX + 1];        // token specific config file
183     char tokname[NAME_MAX + 1]; // token specific directory
184     LW_SHM_TYPE *shm_addr;      // token specific shm address
185 } Slot_Info_t_64;
186 
187 typedef Slot_Info_t_64 SLOT_INFO;
188 
189 typedef struct {
190 
191     /* Information that the API calls will use. */
192     uint32 slot_global_sessions[NUMBER_SLOTS_MANAGED];
193     Slot_Mgr_Proc_t_64 proc_table[NUMBER_PROCESSES_ALLOWED];
194 } Slot_Mgr_Shr_t;
195 
196 typedef struct {
197     uint8 num_slots;
198     CK_INFO_64 ck_info;
199     Slot_Info_t_64 slot_info[NUMBER_SLOTS_MANAGED];
200 } Slot_Mgr_Socket_t;
201 
202 #else                           // PKCS64
203 
204 typedef struct {
205     /* Information that the API calls will use. */
206     uint32 slot_global_sessions[NUMBER_SLOTS_MANAGED];
207     Slot_Mgr_Proc_t proc_table[NUMBER_PROCESSES_ALLOWED];
208 } Slot_Mgr_Shr_t;
209 
210 typedef struct {
211     uint8 num_slots;
212     CK_INFO ck_info;
213     Slot_Info_t slot_info[NUMBER_SLOTS_MANAGED];
214 } Slot_Mgr_Socket_t;
215 
216 typedef Slot_Info_t SLOT_INFO;
217 
218 #endif                          // PKCS64
219 
220 
221 // Loging type constants
222 //
223 #define ERROR 1
224 #define INFO  2
225 
226 
227 //  Call to populate the shared memory
228 #define STR "01234567890123456789012345678901"
229 #define MFG "IBM                             "
230 #define LIB "Meta PKCS11 LIBRARY             "
231 
232 
233 #define MAJOR_V   1
234 #define MINOR_V   2
235 
236 #ifndef CRYPTOKI_API_MAJOR_V
237 #define CRYPTOKI_API_MAJOR_V 0x2
238 #endif
239 
240 #ifndef CRYPTOKI_API_MINOR_V
241 #define CRYPTOKI_API_MINOR_V 0x14
242 #endif
243 
244 #define LIB_MAJOR_V 1
245 #define LIB_MINOR_V 4
246 
247 #define RESTART_SYS_CALLS 1
248 
249 #endif                          /* _SLOTMGR_H */
250