Skip to content

Commit 9b35715

Browse files
committed
Modify Pkcs11::new to be able to load an external library OR self
Signed-off-by: Elise Chouleur <[email protected]>
1 parent 9e3f1b6 commit 9b35715

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

cryptoki/src/context/mod.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,32 @@ 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+
8897
impl Pkcs11 {
8998
/// Instantiate a new context from the path of a PKCS11 dynamic library implementation.
90-
pub fn new<P>(filename: P) -> Result<Self>
99+
pub fn new<P>(filename: LibLoadingType<P>) -> Result<Self>
91100
where
92101
P: AsRef<Path>,
93102
{
94103
unsafe {
95-
let pkcs11_lib =
96-
cryptoki_sys::Pkcs11::new(filename.as_ref()).map_err(Error::LibraryLoading)?;
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+
};
97114
let mut list = mem::MaybeUninit::uninit();
98115

99116
Rv::from(pkcs11_lib.C_GetFunctionList(list.as_mut_ptr()))

cryptoki/src/context/session_management.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ impl Pkcs11 {
4747
/// ```rust
4848
/// # fn main() -> testresult::TestResult {
4949
/// use cryptoki::session::Session;
50-
/// use cryptoki::context::Pkcs11;
50+
/// use cryptoki::context::{LibLoadingType, Pkcs11};
5151
///
52-
/// let mut client = Pkcs11::new(
52+
/// let mut client = Pkcs11::new(LibLoadingType::Open(
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: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const MAX_OBJECT_COUNT: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(10)
2929
///
3030
/// ```no_run
3131
/// use cryptoki::context::CInitializeArgs;
32-
/// use cryptoki::context::Pkcs11;
32+
/// use cryptoki::context::{Pkcs11, LibLoadingType};
3333
/// use cryptoki::error::Error;
3434
/// use cryptoki::object::Attribute;
3535
/// use cryptoki::object::AttributeType;
@@ -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(
41+
/// # let pkcs11 = Pkcs11::new(LibLoadingType::Open(
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);
@@ -278,13 +278,13 @@ impl Session {
278278
/// ```rust
279279
/// # fn main() -> testresult::TestResult {
280280
/// # use cryptoki::session::Session;
281-
/// # use cryptoki::context::Pkcs11;
281+
/// # use cryptoki::context::{LibLoadingType, Pkcs11};
282282
/// # use cryptoki::object::{Attribute, AttributeType, CertificateType, ObjectClass, ObjectHandle};
283283
/// #
284-
/// # let mut client = Pkcs11::new(
284+
/// # let mut client = Pkcs11::new(LibLoadingType::Open(
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
@@ -392,18 +392,18 @@ impl Session {
392392
/// types. If you wish, you may create a hash table simply by:
393393
///
394394
/// ```no_run
395-
/// use cryptoki::context::Pkcs11;
395+
/// use cryptoki::context::{LibLoadingType, Pkcs11};
396396
/// use cryptoki::context::CInitializeArgs;
397397
/// use cryptoki::object::AttributeType;
398398
/// use cryptoki::session::UserType;
399399
/// use cryptoki::types::AuthPin;
400400
/// use std::collections::HashMap;
401401
/// use std::env;
402402
///
403-
/// let mut pkcs11 = Pkcs11::new(
403+
/// let mut pkcs11 = Pkcs11::new(LibLoadingType::Open(
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, Pkcs11};
3+
use cryptoki::context::{CInitializeArgs, LibLoadingType, 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(
15+
Pkcs11::new(LibLoadingType::Open(
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)