@@ -14,7 +14,7 @@ macro_rules! get_pkcs11 {
14
14
/// Suitable only if the caller can't return a Result.
15
15
macro_rules! get_pkcs11_func {
16
16
( $pkcs11: expr, $func_name: ident) => {
17
- ( $pkcs11. impl_. function_list . $func_name)
17
+ ( $pkcs11. impl_. get_function_list ( ) . $func_name)
18
18
} ;
19
19
}
20
20
@@ -38,31 +38,51 @@ use std::ptr;
38
38
use std:: sync:: Arc ;
39
39
use std:: sync:: RwLock ;
40
40
41
+ /// Enum for various function lists
42
+ /// Each following is super-set of the previous one with overlapping start so we store them
43
+ /// in the largest one so we can reference also potentially NULL/non-existing functions
44
+ #[ derive( Debug ) ]
45
+ enum FunctionList {
46
+ /// PKCS #11 2.40 CK_FUNCTION_LIST
47
+ V2 ( cryptoki_sys:: CK_FUNCTION_LIST_3_0 ) ,
48
+ /// PKCS #11 3.0 CK_FUNCTION_LIST_3_0
49
+ V3_0 ( cryptoki_sys:: CK_FUNCTION_LIST_3_0 ) ,
50
+ // TODO when PKCS #11 3.2 will be imported, change the above to 3_2 too!
51
+ // PKCS #11 3.2 CK_FUNCTION_LIST_3_2
52
+ //V3_2(cryptoki_sys::CK_FUNCTION_LIST_3_2),
53
+ }
54
+
41
55
// Implementation of Pkcs11 class that can be enclosed in a single Arc
42
56
pub ( crate ) struct Pkcs11Impl {
43
57
// Even if this field is never read, it is needed for the pointers in function_list to remain
44
58
// valid.
45
59
_pkcs11_lib : cryptoki_sys:: Pkcs11 ,
46
- pub ( crate ) function_list : cryptoki_sys:: CK_FUNCTION_LIST ,
47
- pub ( crate ) function_list_30 : Option < cryptoki_sys:: CK_FUNCTION_LIST_3_0 > ,
60
+ function_list : FunctionList ,
48
61
}
49
62
50
63
impl fmt:: Debug for Pkcs11Impl {
51
64
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
52
65
f. debug_struct ( "Pkcs11Impl" )
53
66
. field ( "function_list" , & self . function_list )
54
- . field ( "function_list_30" , & self . function_list_30 )
55
67
. finish ( )
56
68
}
57
69
}
58
70
59
71
impl Pkcs11Impl {
72
+ #[ inline( always) ]
73
+ pub ( crate ) fn get_function_list ( & self ) -> cryptoki_sys:: CK_FUNCTION_LIST_3_0 {
74
+ match self . function_list {
75
+ FunctionList :: V2 ( l) => l,
76
+ FunctionList :: V3_0 ( l) => l,
77
+ }
78
+ }
79
+
60
80
// Private finalize call
61
81
#[ inline( always) ]
62
82
fn finalize ( & self ) -> Result < ( ) > {
63
83
unsafe {
64
84
Rv :: from ( self
65
- . function_list
85
+ . get_function_list ( )
66
86
. C_Finalize
67
87
. ok_or ( Error :: NullFunctionPointer ) ?(
68
88
ptr:: null_mut ( )
@@ -137,8 +157,7 @@ impl Pkcs11 {
137
157
return Ok ( Pkcs11 {
138
158
impl_ : Arc :: new ( Pkcs11Impl {
139
159
_pkcs11_lib : pkcs11_lib,
140
- function_list : * list_ptr, /* the function list aliases */
141
- function_list_30 : Some ( * list30_ptr) ,
160
+ function_list : FunctionList :: V3_0 ( * list30_ptr) ,
142
161
} ) ,
143
162
initialized : Arc :: new ( RwLock :: new ( false ) ) ,
144
163
} ) ;
@@ -157,8 +176,7 @@ impl Pkcs11 {
157
176
Ok ( Pkcs11 {
158
177
impl_ : Arc :: new ( Pkcs11Impl {
159
178
_pkcs11_lib : pkcs11_lib,
160
- function_list : * list_ptr,
161
- function_list_30 : None ,
179
+ function_list : FunctionList :: V2 ( v2tov3 ( * list_ptr) ) ,
162
180
} ) ,
163
181
initialized : Arc :: new ( RwLock :: new ( false ) ) ,
164
182
} )
@@ -200,3 +218,102 @@ impl Pkcs11 {
200
218
is_fn_supported ( self , function)
201
219
}
202
220
}
221
+
222
+ /// This would be great to be From/Into, but it would have to live inside of the cryptoki-sys
223
+ fn v2tov3 ( f : cryptoki_sys:: CK_FUNCTION_LIST ) -> cryptoki_sys:: CK_FUNCTION_LIST_3_0 {
224
+ cryptoki_sys:: CK_FUNCTION_LIST_3_0 {
225
+ version : f. version ,
226
+ C_Initialize : f. C_Initialize ,
227
+ C_Finalize : f. C_Finalize ,
228
+ C_GetInfo : f. C_GetInfo ,
229
+ C_GetFunctionList : f. C_GetFunctionList ,
230
+ C_GetSlotList : f. C_GetSlotList ,
231
+ C_GetSlotInfo : f. C_GetSlotInfo ,
232
+ C_GetTokenInfo : f. C_GetTokenInfo ,
233
+ C_GetMechanismList : f. C_GetMechanismList ,
234
+ C_GetMechanismInfo : f. C_GetMechanismInfo ,
235
+ C_InitToken : f. C_InitToken ,
236
+ C_InitPIN : f. C_InitPIN ,
237
+ C_SetPIN : f. C_SetPIN ,
238
+ C_OpenSession : f. C_OpenSession ,
239
+ C_CloseSession : f. C_CloseSession ,
240
+ C_CloseAllSessions : f. C_CloseAllSessions ,
241
+ C_GetSessionInfo : f. C_GetSessionInfo ,
242
+ C_GetOperationState : f. C_GetOperationState ,
243
+ C_SetOperationState : f. C_SetOperationState ,
244
+ C_Login : f. C_Login ,
245
+ C_Logout : f. C_Logout ,
246
+ C_CreateObject : f. C_CreateObject ,
247
+ C_CopyObject : f. C_CopyObject ,
248
+ C_DestroyObject : f. C_DestroyObject ,
249
+ C_GetObjectSize : f. C_GetObjectSize ,
250
+ C_GetAttributeValue : f. C_GetAttributeValue ,
251
+ C_SetAttributeValue : f. C_SetAttributeValue ,
252
+ C_FindObjectsInit : f. C_FindObjectsInit ,
253
+ C_FindObjects : f. C_FindObjects ,
254
+ C_FindObjectsFinal : f. C_FindObjectsFinal ,
255
+ C_EncryptInit : f. C_EncryptInit ,
256
+ C_Encrypt : f. C_Encrypt ,
257
+ C_EncryptUpdate : f. C_EncryptUpdate ,
258
+ C_EncryptFinal : f. C_EncryptFinal ,
259
+ C_DecryptInit : f. C_DecryptInit ,
260
+ C_Decrypt : f. C_Decrypt ,
261
+ C_DecryptUpdate : f. C_DecryptUpdate ,
262
+ C_DecryptFinal : f. C_DecryptFinal ,
263
+ C_DigestInit : f. C_DigestInit ,
264
+ C_Digest : f. C_Digest ,
265
+ C_DigestUpdate : f. C_DigestUpdate ,
266
+ C_DigestKey : f. C_DigestKey ,
267
+ C_DigestFinal : f. C_DigestFinal ,
268
+ C_SignInit : f. C_SignInit ,
269
+ C_Sign : f. C_Sign ,
270
+ C_SignUpdate : f. C_SignUpdate ,
271
+ C_SignFinal : f. C_SignFinal ,
272
+ C_SignRecoverInit : f. C_SignRecoverInit ,
273
+ C_SignRecover : f. C_SignRecover ,
274
+ C_VerifyInit : f. C_VerifyInit ,
275
+ C_Verify : f. C_Verify ,
276
+ C_VerifyUpdate : f. C_VerifyUpdate ,
277
+ C_VerifyFinal : f. C_VerifyFinal ,
278
+ C_VerifyRecoverInit : f. C_VerifyRecoverInit ,
279
+ C_VerifyRecover : f. C_VerifyRecover ,
280
+ C_DigestEncryptUpdate : f. C_DigestEncryptUpdate ,
281
+ C_DecryptDigestUpdate : f. C_DecryptDigestUpdate ,
282
+ C_SignEncryptUpdate : f. C_SignEncryptUpdate ,
283
+ C_DecryptVerifyUpdate : f. C_DecryptVerifyUpdate ,
284
+ C_GenerateKey : f. C_GenerateKey ,
285
+ C_GenerateKeyPair : f. C_GenerateKeyPair ,
286
+ C_WrapKey : f. C_WrapKey ,
287
+ C_UnwrapKey : f. C_UnwrapKey ,
288
+ C_DeriveKey : f. C_DeriveKey ,
289
+ C_SeedRandom : f. C_SeedRandom ,
290
+ C_GenerateRandom : f. C_GenerateRandom ,
291
+ C_GetFunctionStatus : f. C_GetFunctionStatus ,
292
+ C_CancelFunction : f. C_CancelFunction ,
293
+ C_WaitForSlotEvent : f. C_WaitForSlotEvent ,
294
+ C_GetInterfaceList : None ,
295
+ C_GetInterface : None ,
296
+ C_LoginUser : None ,
297
+ C_SessionCancel : None ,
298
+ C_MessageEncryptInit : None ,
299
+ C_EncryptMessage : None ,
300
+ C_EncryptMessageBegin : None ,
301
+ C_EncryptMessageNext : None ,
302
+ C_MessageEncryptFinal : None ,
303
+ C_MessageDecryptInit : None ,
304
+ C_DecryptMessage : None ,
305
+ C_DecryptMessageBegin : None ,
306
+ C_DecryptMessageNext : None ,
307
+ C_MessageDecryptFinal : None ,
308
+ C_MessageSignInit : None ,
309
+ C_SignMessage : None ,
310
+ C_SignMessageBegin : None ,
311
+ C_SignMessageNext : None ,
312
+ C_MessageSignFinal : None ,
313
+ C_MessageVerifyInit : None ,
314
+ C_VerifyMessage : None ,
315
+ C_VerifyMessageBegin : None ,
316
+ C_VerifyMessageNext : None ,
317
+ C_MessageVerifyFinal : None ,
318
+ }
319
+ }
0 commit comments