Skip to content

Commit 61e5ead

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 <ionut.mihalcea@arm.com>
1 parent efc79d4 commit 61e5ead

File tree

6 files changed

+56
-6
lines changed

6 files changed

+56
-6
lines changed

cryptoki/src/context/general_purpose.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::convert::TryFrom;
1010

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

cryptoki/src/context/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ impl Drop for Pkcs11Impl {
7575
#[derive(Clone, Debug)]
7676
pub struct Pkcs11 {
7777
pub(crate) impl_: Arc<Pkcs11Impl>,
78+
initialized: bool,
7879
}
7980

8081
impl Pkcs11 {
@@ -97,13 +98,23 @@ impl Pkcs11 {
9798
_pkcs11_lib: pkcs11_lib,
9899
function_list: *list_ptr,
99100
}),
101+
initialized: false,
100102
})
101103
}
102104
}
103105

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

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

cryptoki/src/error/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ pub enum Error {
4646

4747
/// The PIN was not set before logging in.
4848
PinNotSet,
49+
50+
/// The PKCS11 library has already been initialized
51+
AlreadyInitialized,
4952
}
5053

5154
impl fmt::Display for Error {
@@ -62,6 +65,7 @@ impl fmt::Display for Error {
6265
Error::NullFunctionPointer => write!(f, "Calling a NULL function pointer"),
6366
Error::InvalidValue => write!(f, "The value is not one of the expected options"),
6467
Error::PinNotSet => write!(f, "Pin has not been set before trying to log in"),
68+
Error::AlreadyInitialized => write!(f, "PKCS11 library has already been initialized"),
6569
}
6670
}
6771
}
@@ -79,7 +83,8 @@ impl std::error::Error for Error {
7983
| Error::NotSupported
8084
| Error::NullFunctionPointer
8185
| Error::PinNotSet
82-
| Error::InvalidValue => None,
86+
| Error::InvalidValue
87+
| Error::AlreadyInitialized => None,
8388
}
8489
}
8590
}

cryptoki/src/session/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl Session {
150150
/// use std::collections::HashMap;
151151
/// use std::env;
152152
///
153-
/// let pkcs11 = Pkcs11::new(
153+
/// let mut pkcs11 = Pkcs11::new(
154154
/// env::var("PKCS11_SOFTHSM2_MODULE")
155155
/// .unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
156156
/// )

cryptoki/tests/basic.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,3 +646,34 @@ fn is_fn_supported_test() {
646646
"C_DigestFinal function reports as not supported"
647647
);
648648
}
649+
650+
#[test]
651+
#[serial]
652+
fn is_initialized_test() {
653+
use cryptoki::context::{CInitializeArgs, Pkcs11};
654+
655+
let mut pkcs11 = Pkcs11::new(
656+
std::env::var("PKCS11_SOFTHSM2_MODULE")
657+
.unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
658+
)
659+
.unwrap();
660+
661+
assert!(
662+
!pkcs11.is_initialized(),
663+
"Context created with initialized flag on"
664+
);
665+
666+
// initialize the library
667+
pkcs11.initialize(CInitializeArgs::OsThreads).unwrap();
668+
669+
assert!(
670+
pkcs11.is_initialized(),
671+
"Context was not marked as initialized"
672+
);
673+
674+
match pkcs11.initialize(CInitializeArgs::OsThreads) {
675+
Err(Error::AlreadyInitialized) => (),
676+
Err(e) => panic!("Got unexpected error when initializing: {}", e),
677+
Ok(()) => panic!("Initializing twice should not have been allowed"),
678+
}
679+
}

cryptoki/tests/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub static USER_PIN: &str = "fedcba";
1111
pub static SO_PIN: &str = "abcdef";
1212

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

0 commit comments

Comments
 (0)