Skip to content

Commit b63539d

Browse files
authored
Merge pull request #230 from EliseChouleur/static-bindings
Add capability to load symbols from current executable
2 parents 9e3f1b6 + a8e8498 commit b63539d

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

cryptoki/src/context/mod.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,37 @@ impl Pkcs11 {
9494
unsafe {
9595
let pkcs11_lib =
9696
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+
}
98112

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();
101115

102-
let list_ptr = *list.as_ptr();
116+
Rv::from(pkcs11_lib.C_GetFunctionList(list.as_mut_ptr()))
117+
.into_result(Function::GetFunctionList)?;
103118

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+
})
112128
}
113129

114130
/// Initialize the PKCS11 library

0 commit comments

Comments
 (0)