@@ -14,7 +14,7 @@ macro_rules! get_pkcs11 {
1414/// Suitable only if the caller can't return a Result.
1515macro_rules! get_pkcs11_func {
1616 ( $pkcs11: expr, $func_name: ident) => {
17- ( $pkcs11. impl_. function_list . $func_name)
17+ ( $pkcs11. impl_. get_function_list ( ) . $func_name)
1818 } ;
1919}
2020
@@ -38,31 +38,51 @@ use std::ptr;
3838use std:: sync:: Arc ;
3939use std:: sync:: RwLock ;
4040
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+
4155// Implementation of Pkcs11 class that can be enclosed in a single Arc
4256pub ( crate ) struct Pkcs11Impl {
4357 // Even if this field is never read, it is needed for the pointers in function_list to remain
4458 // valid.
4559 _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 ,
4861}
4962
5063impl fmt:: Debug for Pkcs11Impl {
5164 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
5265 f. debug_struct ( "Pkcs11Impl" )
5366 . field ( "function_list" , & self . function_list )
54- . field ( "function_list_30" , & self . function_list_30 )
5567 . finish ( )
5668 }
5769}
5870
5971impl 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+
6080 // Private finalize call
6181 #[ inline( always) ]
6282 fn finalize ( & self ) -> Result < ( ) > {
6383 unsafe {
6484 Rv :: from ( self
65- . function_list
85+ . get_function_list ( )
6686 . C_Finalize
6787 . ok_or ( Error :: NullFunctionPointer ) ?(
6888 ptr:: null_mut ( )
@@ -137,8 +157,7 @@ impl Pkcs11 {
137157 return Ok ( Pkcs11 {
138158 impl_ : Arc :: new ( Pkcs11Impl {
139159 _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) ,
142161 } ) ,
143162 initialized : Arc :: new ( RwLock :: new ( false ) ) ,
144163 } ) ;
@@ -157,8 +176,7 @@ impl Pkcs11 {
157176 Ok ( Pkcs11 {
158177 impl_ : Arc :: new ( Pkcs11Impl {
159178 _pkcs11_lib : pkcs11_lib,
160- function_list : * list_ptr,
161- function_list_30 : None ,
179+ function_list : FunctionList :: V2 ( v2tov3 ( * list_ptr) ) ,
162180 } ) ,
163181 initialized : Arc :: new ( RwLock :: new ( false ) ) ,
164182 } )
@@ -200,3 +218,102 @@ impl Pkcs11 {
200218 is_fn_supported ( self , function)
201219 }
202220}
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