diff --git a/Cargo.lock b/Cargo.lock index fcd8ad5f..de0bc8ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -513,9 +513,9 @@ dependencies = [ [[package]] name = "testresult" -version = "0.2.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "649113ab56eab59f78f02314a05b24bda0200d322c9eb1c60d0af8554e94c5ef" +checksum = "614b328ff036a4ef882c61570f72918f7e9c5bee1da33f8e7f91e01daee7e56c" [[package]] name = "unicode-ident" diff --git a/cryptoki/Cargo.toml b/cryptoki/Cargo.toml index 963da79e..fd970270 100644 --- a/cryptoki/Cargo.toml +++ b/cryptoki/Cargo.toml @@ -25,7 +25,7 @@ secrecy = "0.8.0" num-traits = "0.2.14" hex = "0.4.3" serial_test = "0.5.1" -testresult = "0.2.0" +testresult = "0.4.1" [features] psa-crypto-conversions = ["psa-crypto"] diff --git a/cryptoki/src/context/session_management.rs b/cryptoki/src/context/session_management.rs index 93798ec7..ee393039 100644 --- a/cryptoki/src/context/session_management.rs +++ b/cryptoki/src/context/session_management.rs @@ -41,6 +41,25 @@ impl Pkcs11 { /// For a Read-Write session, use `open_rw_session` /// /// Note: No callback is set when opening the session. + /// + /// # Examples + /// + /// ```rust + /// # fn main() -> testresult::TestResult { + /// use cryptoki::session::Session; + /// use cryptoki::context::Pkcs11; + /// + /// let mut client = Pkcs11::new( + /// 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 + /// let slot = client.get_all_slots()?[0]; + /// let session = client.open_ro_session(slot)?; + /// # let _ = session; Ok(()) } + /// ``` pub fn open_ro_session(&self, slot_id: Slot) -> Result { self.open_session(slot_id, false) } diff --git a/cryptoki/src/session/object_management.rs b/cryptoki/src/session/object_management.rs index 6dcf84f6..350fce69 100644 --- a/cryptoki/src/session/object_management.rs +++ b/cryptoki/src/session/object_management.rs @@ -15,6 +15,41 @@ const MAX_OBJECT_COUNT: usize = 10; impl Session { /// Search for session objects matching a template + /// + /// # Arguments + /// * `template` - A [Attribute] of search parameters that will be used + /// to find objects. + /// + /// # Examples + /// + /// ```rust + /// # fn main() -> testresult::TestResult { + /// # use cryptoki::session::Session; + /// # use cryptoki::context::Pkcs11; + /// # use cryptoki::object::{Attribute, AttributeType, CertificateType, ObjectClass, ObjectHandle}; + /// # + /// # let mut client = Pkcs11::new( + /// # 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 + /// # let slot = client.get_all_slots()?[0]; + /// # let session = client.open_ro_session(slot)?; + /// # + /// // Get handles to all of the x509 certificates on the card + /// let search = vec![Attribute::Class(ObjectClass::CERTIFICATE), Attribute::CertificateType(CertificateType::X_509)]; + /// for handle in session.find_objects(&search)? { + /// // each cert: get the "value" which will be the raw certificate data + /// for value in session.get_attributes(handle, &[AttributeType::Value])? { + /// if let Attribute::Value(value) = value { + /// println!("Certificate value: {value:?}"); + /// } + /// } + /// } + /// # Ok(()) } + /// ``` pub fn find_objects(&self, template: &[Attribute]) -> Result> { let mut template: Vec = template.iter().map(|attr| attr.into()).collect();