From 7fb9d15767867023510e57b69192c36d97e3609e Mon Sep 17 00:00:00 2001 From: Arjen Nienhuis Date: Sat, 17 Jun 2023 20:38:42 +0200 Subject: [PATCH] Deprecate is_initialized() Signed-off-by: Arjen Nienhuis --- cryptoki/src/context/general_purpose.rs | 5 +--- cryptoki/src/context/mod.rs | 13 +++----- cryptoki/src/error/rv.rs | 1 + cryptoki/tests/basic.rs | 40 ++++++++++++++++++++----- cryptoki/tests/common.rs | 10 +++++-- 5 files changed, 46 insertions(+), 23 deletions(-) diff --git a/cryptoki/src/context/general_purpose.rs b/cryptoki/src/context/general_purpose.rs index 0bf52a3a..884b9a88 100644 --- a/cryptoki/src/context/general_purpose.rs +++ b/cryptoki/src/context/general_purpose.rs @@ -10,7 +10,7 @@ use std::convert::TryFrom; // See public docs on stub in parent mod.rs #[inline(always)] -pub(super) fn initialize(ctx: &mut Pkcs11, init_args: CInitializeArgs) -> Result<()> { +pub(super) fn initialize(ctx: &Pkcs11, init_args: CInitializeArgs) -> Result<()> { // if no args are specified, library expects NULL let mut init_args = CK_C_INITIALIZE_ARGS::from(init_args); let init_args_ptr = &mut init_args; @@ -19,9 +19,6 @@ pub(super) fn initialize(ctx: &mut Pkcs11, init_args: CInitializeArgs) -> Result init_args_ptr as *mut CK_C_INITIALIZE_ARGS as *mut ::std::ffi::c_void, )) .into_result() - .map(|_| { - ctx.initialized = true; - }) } } diff --git a/cryptoki/src/context/mod.rs b/cryptoki/src/context/mod.rs index 5587a4fb..6e2f6727 100644 --- a/cryptoki/src/context/mod.rs +++ b/cryptoki/src/context/mod.rs @@ -76,7 +76,6 @@ impl Drop for Pkcs11Impl { #[derive(Clone, Debug)] pub struct Pkcs11 { pub(crate) impl_: Arc, - initialized: bool, } impl Pkcs11 { @@ -99,23 +98,19 @@ impl Pkcs11 { _pkcs11_lib: pkcs11_lib, function_list: *list_ptr, }), - initialized: false, }) } } /// Initialize the PKCS11 library - pub fn initialize(&mut self, init_args: CInitializeArgs) -> Result<()> { - if !self.initialized { - initialize(self, init_args) - } else { - Err(Error::AlreadyInitialized) - } + pub fn initialize(&self, init_args: CInitializeArgs) -> Result<()> { + initialize(self, init_args) } /// Check whether the PKCS11 library has been initialized + #[deprecated] pub fn is_initialized(&self) -> bool { - self.initialized + get_library_info(self).is_ok() } /// Finalize the PKCS11 library. Indicates that the application no longer needs to use PKCS11. diff --git a/cryptoki/src/error/rv.rs b/cryptoki/src/error/rv.rs index 660e1d30..40fb0d4a 100644 --- a/cryptoki/src/error/rv.rs +++ b/cryptoki/src/error/rv.rs @@ -131,6 +131,7 @@ impl Rv { pub fn into_result(self) -> Result<()> { match self { Rv::Ok => Ok(()), + Rv::Error(RvError::CryptokiAlreadyInitialized) => Err(Error::AlreadyInitialized), Rv::Error(rv_error) => Err(Error::Pkcs11(rv_error)), } } diff --git a/cryptoki/tests/basic.rs b/cryptoki/tests/basic.rs index 6a0fb197..d2341f89 100644 --- a/cryptoki/tests/basic.rs +++ b/cryptoki/tests/basic.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 mod common; -use crate::common::{SO_PIN, USER_PIN}; +use crate::common::{get_pkcs11, SO_PIN, USER_PIN}; use common::init_pins; use cryptoki::error::{Error, RvError}; use cryptoki::mechanism::aead::GcmParams; @@ -677,15 +677,12 @@ fn is_fn_supported_test() { } #[test] +#[allow(deprecated)] #[serial] fn is_initialized_test() { - use cryptoki::context::{CInitializeArgs, Pkcs11}; + use cryptoki::context::CInitializeArgs; - let mut pkcs11 = Pkcs11::new( - std::env::var("PKCS11_SOFTHSM2_MODULE") - .unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()), - ) - .unwrap(); + let pkcs11 = get_pkcs11(); assert!( !pkcs11.is_initialized(), @@ -707,6 +704,35 @@ fn is_initialized_test() { } } +#[test] +#[serial] +#[allow(deprecated)] +#[allow(clippy::redundant_clone)] +fn test_clone_initialize() { + use cryptoki::context::CInitializeArgs; + + let pkcs11 = get_pkcs11(); + + let clone = pkcs11.clone(); + assert!( + !pkcs11.is_initialized(), + "Before initialize() it should not be initialized" + ); + assert!( + !clone.is_initialized(), + "Before initialize() the clone should not be initialized" + ); + pkcs11.initialize(CInitializeArgs::OsThreads).unwrap(); + assert!( + pkcs11.is_initialized(), + "After initialize() it should be initialized" + ); + assert!( + clone.is_initialized(), + "After initialize() the clone should be initialized" + ); +} + #[test] #[serial] fn aes_key_attributes_test() -> TestResult { diff --git a/cryptoki/tests/common.rs b/cryptoki/tests/common.rs index b0de80cb..95e4202d 100644 --- a/cryptoki/tests/common.rs +++ b/cryptoki/tests/common.rs @@ -11,12 +11,16 @@ pub static USER_PIN: &str = "fedcba"; // The default SO pin pub static SO_PIN: &str = "abcdef"; -pub fn init_pins() -> (Pkcs11, Slot) { - let mut pkcs11 = Pkcs11::new( +pub fn get_pkcs11() -> Pkcs11 { + Pkcs11::new( env::var("PKCS11_SOFTHSM2_MODULE") .unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()), ) - .unwrap(); + .unwrap() +} + +pub fn init_pins() -> (Pkcs11, Slot) { + let pkcs11 = get_pkcs11(); // initialize the library pkcs11.initialize(CInitializeArgs::OsThreads).unwrap();