Skip to content

Commit 0697c1b

Browse files
committed
Rather use another new function than changing new input
Signed-off-by: Elise Chouleur <[email protected]>
1 parent 9b35715 commit 0697c1b

File tree

4 files changed

+44
-45
lines changed

4 files changed

+44
-45
lines changed

cryptoki/src/context/mod.rs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -85,49 +85,48 @@ pub struct Pkcs11 {
8585
initialized: Arc<RwLock<bool>>,
8686
}
8787

88-
#[derive(Debug)]
89-
/// Type of library to load in the instantiation of a new Pkcs11 context.
90-
pub enum LibLoadingType<P: AsRef<Path>> {
91-
/// Load current executable, the PKCS11 implementation is contained in the current executable
92-
OpenSelf,
93-
/// Open dynamic library specify in input
94-
Open(P),
95-
}
96-
9788
impl Pkcs11 {
9889
/// Instantiate a new context from the path of a PKCS11 dynamic library implementation.
99-
pub fn new<P>(filename: LibLoadingType<P>) -> Result<Self>
90+
pub fn new<P>(filename: P) -> Result<Self>
10091
where
10192
P: AsRef<Path>,
10293
{
10394
unsafe {
104-
let pkcs11_lib = match filename {
105-
LibLoadingType::OpenSelf => {
106-
#[cfg(not(windows))]
107-
let this_lib = libloading::os::unix::Library::this();
108-
#[cfg(windows)]
109-
let this_lib = libloading::os::windows::Library::this();
110-
cryptoki_sys::Pkcs11::from_library(this_lib)?
111-
}
112-
LibLoadingType::Open(filename) => cryptoki_sys::Pkcs11::new(filename.as_ref()).map_err(Error::LibraryLoading)?
113-
};
114-
let mut list = mem::MaybeUninit::uninit();
115-
116-
Rv::from(pkcs11_lib.C_GetFunctionList(list.as_mut_ptr()))
117-
.into_result(Function::GetFunctionList)?;
118-
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-
})
95+
let pkcs11_lib =
96+
cryptoki_sys::Pkcs11::new(filename.as_ref()).map_err(Error::LibraryLoading)?;
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)
128110
}
129111
}
130112

113+
unsafe fn _new(pkcs11_lib: cryptoki_sys::Pkcs11) -> Result<Self> {
114+
let mut list = mem::MaybeUninit::uninit();
115+
116+
Rv::from(pkcs11_lib.C_GetFunctionList(list.as_mut_ptr()))
117+
.into_result(Function::GetFunctionList)?;
118+
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+
})
128+
}
129+
131130
/// Initialize the PKCS11 library
132131
pub fn initialize(&self, init_args: CInitializeArgs) -> Result<()> {
133132
let mut init_lock = self

cryptoki/src/context/session_management.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ impl Pkcs11 {
4949
/// use cryptoki::session::Session;
5050
/// use cryptoki::context::{LibLoadingType, Pkcs11};
5151
///
52-
/// let mut client = Pkcs11::new(LibLoadingType::Open(
52+
/// let mut client = Pkcs11::new(
5353
/// std::env::var("PKCS11_SOFTHSM2_MODULE")
5454
/// .unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
55-
/// ))?;
55+
/// )?;
5656
/// client.initialize(cryptoki::context::CInitializeArgs::OsThreads)?;
5757
///
5858
/// // Use the first slot

cryptoki/src/session/object_management.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ const MAX_OBJECT_COUNT: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(10)
3838
/// use std::env;
3939
///
4040
/// # fn main() -> testresult::TestResult {
41-
/// # let pkcs11 = Pkcs11::new(LibLoadingType::Open(
41+
/// # let pkcs11 = Pkcs11::new(
4242
/// # env::var("PKCS11_SOFTHSM2_MODULE")
4343
/// # .unwrap_or_else(|_| "/usr/local/lib/libsofthsm2.so".to_string()),
44-
/// # ))?;
44+
/// # )?;
4545
/// #
4646
/// # pkcs11.initialize(CInitializeArgs::OsThreads)?;
4747
/// # let slot = pkcs11.get_slots_with_token()?.remove(0);
@@ -281,10 +281,10 @@ impl Session {
281281
/// # use cryptoki::context::{LibLoadingType, Pkcs11};
282282
/// # use cryptoki::object::{Attribute, AttributeType, CertificateType, ObjectClass, ObjectHandle};
283283
/// #
284-
/// # let mut client = Pkcs11::new(LibLoadingType::Open(
284+
/// # let mut client = Pkcs11::new(
285285
/// # std::env::var("PKCS11_SOFTHSM2_MODULE")
286286
/// # .unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
287-
/// # ))?;
287+
/// # )?;
288288
/// # client.initialize(cryptoki::context::CInitializeArgs::OsThreads)?;
289289
/// #
290290
/// # // Use the first slot
@@ -400,10 +400,10 @@ impl Session {
400400
/// use std::collections::HashMap;
401401
/// use std::env;
402402
///
403-
/// let mut pkcs11 = Pkcs11::new(LibLoadingType::Open(
403+
/// let mut pkcs11 = Pkcs11::new(
404404
/// env::var("PKCS11_SOFTHSM2_MODULE")
405405
/// .unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
406-
/// ))
406+
/// )
407407
/// .unwrap();
408408
///
409409
/// pkcs11.initialize(CInitializeArgs::OsThreads).unwrap();

cryptoki/tests/common.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2021 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
3-
use cryptoki::context::{CInitializeArgs, LibLoadingType, Pkcs11};
3+
use cryptoki::context::{CInitializeArgs, Pkcs11};
44
use cryptoki::session::UserType;
55
use cryptoki::slot::Slot;
66
use cryptoki::types::AuthPin;
@@ -12,10 +12,10 @@ pub static USER_PIN: &str = "fedcba";
1212
pub static SO_PIN: &str = "abcdef";
1313

1414
pub fn get_pkcs11() -> Pkcs11 {
15-
Pkcs11::new(LibLoadingType::Open(
15+
Pkcs11::new(
1616
env::var("PKCS11_SOFTHSM2_MODULE")
1717
.unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
18-
))
18+
)
1919
.unwrap()
2020
}
2121

0 commit comments

Comments
 (0)