@@ -94,21 +94,37 @@ impl Pkcs11 {
94
94
unsafe {
95
95
let pkcs11_lib =
96
96
cryptoki_sys:: Pkcs11 :: new ( filename. as_ref ( ) ) . map_err ( Error :: LibraryLoading ) ?;
97
- let mut list = mem:: MaybeUninit :: uninit ( ) ;
97
+ Self :: _new ( pkcs11_lib)
98
+ }
99
+ }
100
+
101
+ /// Instantiate a new context from current executable, the PKCS11 implementation is contained in the current executable
102
+ pub fn new_from_self ( ) -> Result < Self > {
103
+ unsafe {
104
+ #[ cfg( not( windows) ) ]
105
+ let this_lib = libloading:: os:: unix:: Library :: this ( ) ;
106
+ #[ cfg( windows) ]
107
+ let this_lib = libloading:: os:: windows:: Library :: this ( ) ?;
108
+ let pkcs11_lib = cryptoki_sys:: Pkcs11 :: from_library ( this_lib) ?;
109
+ Self :: _new ( pkcs11_lib)
110
+ }
111
+ }
98
112
99
- Rv :: from ( pkcs11_lib . C_GetFunctionList ( list . as_mut_ptr ( ) ) )
100
- . into_result ( Function :: GetFunctionList ) ? ;
113
+ unsafe fn _new ( pkcs11_lib : cryptoki_sys :: Pkcs11 ) -> Result < Self > {
114
+ let mut list = mem :: MaybeUninit :: uninit ( ) ;
101
115
102
- let list_ptr = * list. as_ptr ( ) ;
116
+ Rv :: from ( pkcs11_lib. C_GetFunctionList ( list. as_mut_ptr ( ) ) )
117
+ . into_result ( Function :: GetFunctionList ) ?;
103
118
104
- Ok ( Pkcs11 {
105
- impl_ : Arc :: new ( Pkcs11Impl {
106
- _pkcs11_lib : pkcs11_lib,
107
- function_list : * list_ptr,
108
- } ) ,
109
- initialized : Arc :: new ( RwLock :: new ( false ) ) ,
110
- } )
111
- }
119
+ let list_ptr = * list. as_ptr ( ) ;
120
+
121
+ Ok ( Pkcs11 {
122
+ impl_ : Arc :: new ( Pkcs11Impl {
123
+ _pkcs11_lib : pkcs11_lib,
124
+ function_list : * list_ptr,
125
+ } ) ,
126
+ initialized : Arc :: new ( RwLock :: new ( false ) ) ,
127
+ } )
112
128
}
113
129
114
130
/// Initialize the PKCS11 library
0 commit comments