Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions cryptoki/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,32 @@ pub struct Pkcs11 {
initialized: Arc<RwLock<bool>>,
}

#[derive(Debug)]
/// Type of library to load in the instantiation of a new Pkcs11 context.
pub enum LibLoadingType<P: AsRef<Path>> {
/// Load current executable, the PKCS11 implementation is contained in the current executable
OpenSelf,
/// Open dynamic library specify in input
Open(P),
}

impl Pkcs11 {
/// Instantiate a new context from the path of a PKCS11 dynamic library implementation.
pub fn new<P>(filename: P) -> Result<Self>
pub fn new<P>(filename: LibLoadingType<P>) -> Result<Self>
where
P: AsRef<Path>,
{
unsafe {
let pkcs11_lib =
cryptoki_sys::Pkcs11::new(filename.as_ref()).map_err(Error::LibraryLoading)?;
let pkcs11_lib = match filename {
LibLoadingType::OpenSelf => {
#[cfg(not(windows))]
let this_lib = libloading::os::unix::Library::this();
#[cfg(windows)]
let this_lib = libloading::os::windows::Library::this();
cryptoki_sys::Pkcs11::from_library(this_lib)?
}
LibLoadingType::Open(filename) => cryptoki_sys::Pkcs11::new(filename.as_ref()).map_err(Error::LibraryLoading)?
};
let mut list = mem::MaybeUninit::uninit();

Rv::from(pkcs11_lib.C_GetFunctionList(list.as_mut_ptr()))
Expand Down
6 changes: 3 additions & 3 deletions cryptoki/src/context/session_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ impl Pkcs11 {
/// ```rust
/// # fn main() -> testresult::TestResult {
/// use cryptoki::session::Session;
/// use cryptoki::context::Pkcs11;
/// use cryptoki::context::{LibLoadingType, Pkcs11};
///
/// let mut client = Pkcs11::new(
/// let mut client = Pkcs11::new(LibLoadingType::Open(
/// std::env::var("PKCS11_SOFTHSM2_MODULE")
/// .unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
/// )?;
/// ))?;
/// client.initialize(cryptoki::context::CInitializeArgs::OsThreads)?;
///
/// // Use the first slot
Expand Down
18 changes: 9 additions & 9 deletions cryptoki/src/session/object_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const MAX_OBJECT_COUNT: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(10)
///
/// ```no_run
/// use cryptoki::context::CInitializeArgs;
/// use cryptoki::context::Pkcs11;
/// use cryptoki::context::{Pkcs11, LibLoadingType};
/// use cryptoki::error::Error;
/// use cryptoki::object::Attribute;
/// use cryptoki::object::AttributeType;
Expand All @@ -38,10 +38,10 @@ const MAX_OBJECT_COUNT: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(10)
/// use std::env;
///
/// # fn main() -> testresult::TestResult {
/// # let pkcs11 = Pkcs11::new(
/// # let pkcs11 = Pkcs11::new(LibLoadingType::Open(
/// # env::var("PKCS11_SOFTHSM2_MODULE")
/// # .unwrap_or_else(|_| "/usr/local/lib/libsofthsm2.so".to_string()),
/// # )?;
/// # ))?;
/// #
/// # pkcs11.initialize(CInitializeArgs::OsThreads)?;
/// # let slot = pkcs11.get_slots_with_token()?.remove(0);
Expand Down Expand Up @@ -278,13 +278,13 @@ impl Session {
/// ```rust
/// # fn main() -> testresult::TestResult {
/// # use cryptoki::session::Session;
/// # use cryptoki::context::Pkcs11;
/// # use cryptoki::context::{LibLoadingType, Pkcs11};
/// # use cryptoki::object::{Attribute, AttributeType, CertificateType, ObjectClass, ObjectHandle};
/// #
/// # let mut client = Pkcs11::new(
/// # let mut client = Pkcs11::new(LibLoadingType::Open(
/// # std::env::var("PKCS11_SOFTHSM2_MODULE")
/// # .unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
/// # )?;
/// # ))?;
/// # client.initialize(cryptoki::context::CInitializeArgs::OsThreads)?;
/// #
/// # // Use the first slot
Expand Down Expand Up @@ -392,18 +392,18 @@ impl Session {
/// types. If you wish, you may create a hash table simply by:
///
/// ```no_run
/// use cryptoki::context::Pkcs11;
/// use cryptoki::context::{LibLoadingType, Pkcs11};
/// use cryptoki::context::CInitializeArgs;
/// use cryptoki::object::AttributeType;
/// use cryptoki::session::UserType;
/// use cryptoki::types::AuthPin;
/// use std::collections::HashMap;
/// use std::env;
///
/// let mut pkcs11 = Pkcs11::new(
/// let mut pkcs11 = Pkcs11::new(LibLoadingType::Open(
/// env::var("PKCS11_SOFTHSM2_MODULE")
/// .unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
/// )
/// ))
/// .unwrap();
///
/// pkcs11.initialize(CInitializeArgs::OsThreads).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions cryptoki/tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2021 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
use cryptoki::context::{CInitializeArgs, Pkcs11};
use cryptoki::context::{CInitializeArgs, LibLoadingType, Pkcs11};
use cryptoki::session::UserType;
use cryptoki::slot::Slot;
use cryptoki::types::AuthPin;
Expand All @@ -12,10 +12,10 @@ pub static USER_PIN: &str = "fedcba";
pub static SO_PIN: &str = "abcdef";

pub fn get_pkcs11() -> Pkcs11 {
Pkcs11::new(
Pkcs11::new(LibLoadingType::Open(
env::var("PKCS11_SOFTHSM2_MODULE")
.unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
)
))
.unwrap()
}

Expand Down
Loading