Skip to content

Commit 38d7ffb

Browse files
committed
Allow checking for initialization status
The `Pkcs11` struct is enhanced to store a flag identifying whether the library has been initialized or not. This flag can then be used to signal re-initialization attempts and to simply inform users of the status. Signed-off-by: Ionut Mihalcea <[email protected]>
1 parent 3eb3686 commit 38d7ffb

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

cryptoki/src/context/general_purpose.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use paste::paste;
99

1010
// See public docs on stub in parent mod.rs
1111
#[inline(always)]
12-
pub(super) fn initialize(ctx: &Pkcs11, init_args: CInitializeArgs) -> Result<()> {
12+
pub(super) fn initialize(ctx: &mut Pkcs11, init_args: CInitializeArgs) -> Result<()> {
1313
// if no args are specified, library expects NULL
1414
let mut init_args = CK_C_INITIALIZE_ARGS::from(init_args);
1515
let init_args_ptr = &mut init_args;
@@ -18,6 +18,9 @@ pub(super) fn initialize(ctx: &Pkcs11, init_args: CInitializeArgs) -> Result<()>
1818
init_args_ptr as *mut CK_C_INITIALIZE_ARGS as *mut ::std::ffi::c_void,
1919
))
2020
.into_result()
21+
.map(|_| {
22+
ctx.initialized = true;
23+
})
2124
}
2225
}
2326

cryptoki/src/context/mod.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ impl Drop for Pkcs11Impl {
7676
#[derive(Clone, Debug)]
7777
pub struct Pkcs11 {
7878
pub(crate) impl_: Arc<Pkcs11Impl>,
79+
initialized: bool,
7980
}
8081

8182
impl Pkcs11 {
@@ -98,13 +99,23 @@ impl Pkcs11 {
9899
_pkcs11_lib: pkcs11_lib,
99100
function_list: *list_ptr,
100101
}),
102+
initialized: false,
101103
})
102104
}
103105
}
104106

105107
/// Initialize the PKCS11 library
106-
pub fn initialize(&self, init_args: CInitializeArgs) -> Result<()> {
107-
initialize(self, init_args)
108+
pub fn initialize(&mut self, init_args: CInitializeArgs) -> Result<()> {
109+
if !self.initialized {
110+
initialize(self, init_args)
111+
} else {
112+
Err(Error::AlreadyInitialized)
113+
}
114+
}
115+
116+
/// Check whether the PKCS11 library has been initialized
117+
pub fn is_initialized(&self) -> bool {
118+
self.initialized
108119
}
109120

110121
/// Finalize the PKCS11 library. Indicates that the application no longer needs to use PKCS11.

cryptoki/src/error/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ pub enum Error {
4040

4141
/// The PIN was not set before logging in.
4242
PinNotSet,
43+
44+
/// The PKCS11 library has already been initialized
45+
AlreadyInitialized,
4346
}
4447

4548
impl fmt::Display for Error {
@@ -54,6 +57,7 @@ impl fmt::Display for Error {
5457
Error::NullFunctionPointer => write!(f, "Calling a NULL function pointer"),
5558
Error::InvalidValue => write!(f, "The value is not one of the expected options"),
5659
Error::PinNotSet => write!(f, "Pin has not been set before trying to log in"),
60+
Error::AlreadyInitialized => write!(f, "PKCS11 library has already been initialized"),
5761
}
5862
}
5963
}
@@ -69,7 +73,8 @@ impl std::error::Error for Error {
6973
| Error::NotSupported
7074
| Error::NullFunctionPointer
7175
| Error::PinNotSet
72-
| Error::InvalidValue => None,
76+
| Error::InvalidValue
77+
| Error::AlreadyInitialized => None,
7378
}
7479
}
7580
}

cryptoki/tests/basic.rs

+31
Original file line numberDiff line numberDiff line change
@@ -698,3 +698,34 @@ fn is_fn_supported_test() {
698698
"C_DigestFinal function reports as not supported"
699699
);
700700
}
701+
702+
#[test]
703+
#[serial]
704+
fn is_initialized_test() {
705+
use cryptoki::context::{CInitializeArgs, Pkcs11};
706+
707+
let mut pkcs11 = Pkcs11::new(
708+
std::env::var("PKCS11_SOFTHSM2_MODULE")
709+
.unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
710+
)
711+
.unwrap();
712+
713+
assert!(
714+
!pkcs11.is_initialized(),
715+
"Context created with initialized flag on"
716+
);
717+
718+
// initialize the library
719+
pkcs11.initialize(CInitializeArgs::OsThreads).unwrap();
720+
721+
assert!(
722+
pkcs11.is_initialized(),
723+
"Context was not marked as initialized"
724+
);
725+
726+
match pkcs11.initialize(CInitializeArgs::OsThreads) {
727+
Err(Error::AlreadyInitialized) => (),
728+
Err(e) => panic!("Got unexpected error when initializing: {}", e),
729+
Ok(()) => panic!("Initializing twice should not have been allowed"),
730+
}
731+
}

cryptoki/tests/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub static USER_PIN: &str = "fedcba";
1212
pub static SO_PIN: &str = "abcdef";
1313

1414
pub fn init_pins() -> (Pkcs11, Slot) {
15-
let pkcs11 = Pkcs11::new(
15+
let mut pkcs11 = Pkcs11::new(
1616
env::var("PKCS11_SOFTHSM2_MODULE")
1717
.unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
1818
)

0 commit comments

Comments
 (0)