Skip to content

Commit 2141f15

Browse files
hcldanwiktor-k
authored andcommitted
Add an example to find_objects
Closes: #143 Co-authored-by: Wiktor Kwapisiewicz <[email protected]> Signed-off-by: Dan Dumont <[email protected]> Signed-off-by: Wiktor Kwapisiewicz <[email protected]>
1 parent e57920a commit 2141f15

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cryptoki/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ secrecy = "0.8.0"
2525
num-traits = "0.2.14"
2626
hex = "0.4.3"
2727
serial_test = "0.5.1"
28-
testresult = "0.2.0"
28+
testresult = "0.4.1"
2929

3030
[features]
3131
psa-crypto-conversions = ["psa-crypto"]

cryptoki/src/context/session_management.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ impl Pkcs11 {
4141
/// For a Read-Write session, use `open_rw_session`
4242
///
4343
/// Note: No callback is set when opening the session.
44+
///
45+
/// # Examples
46+
///
47+
/// ```rust
48+
/// # fn main() -> testresult::TestResult {
49+
/// use cryptoki::session::Session;
50+
/// use cryptoki::context::Pkcs11;
51+
///
52+
/// let mut client = Pkcs11::new(
53+
/// std::env::var("PKCS11_SOFTHSM2_MODULE")
54+
/// .unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
55+
/// )?;
56+
/// client.initialize(cryptoki::context::CInitializeArgs::OsThreads)?;
57+
///
58+
/// // Use the first slot
59+
/// let slot = client.get_all_slots()?[0];
60+
/// let session = client.open_ro_session(slot)?;
61+
/// # let _ = session; Ok(()) }
62+
/// ```
4463
pub fn open_ro_session(&self, slot_id: Slot) -> Result<Session> {
4564
self.open_session(slot_id, false)
4665
}

cryptoki/src/session/object_management.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,41 @@ const MAX_OBJECT_COUNT: usize = 10;
1515

1616
impl Session {
1717
/// Search for session objects matching a template
18+
///
19+
/// # Arguments
20+
/// * `template` - A [Attribute] of search parameters that will be used
21+
/// to find objects.
22+
///
23+
/// # Examples
24+
///
25+
/// ```rust
26+
/// # fn main() -> testresult::TestResult {
27+
/// # use cryptoki::session::Session;
28+
/// # use cryptoki::context::Pkcs11;
29+
/// # use cryptoki::object::{Attribute, AttributeType, CertificateType, ObjectClass, ObjectHandle};
30+
/// #
31+
/// # let mut client = Pkcs11::new(
32+
/// # std::env::var("PKCS11_SOFTHSM2_MODULE")
33+
/// # .unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
34+
/// # )?;
35+
/// # client.initialize(cryptoki::context::CInitializeArgs::OsThreads)?;
36+
/// #
37+
/// # // Use the first slot
38+
/// # let slot = client.get_all_slots()?[0];
39+
/// # let session = client.open_ro_session(slot)?;
40+
/// #
41+
/// // Get handles to all of the x509 certificates on the card
42+
/// let search = vec![Attribute::Class(ObjectClass::CERTIFICATE), Attribute::CertificateType(CertificateType::X_509)];
43+
/// for handle in session.find_objects(&search)? {
44+
/// // each cert: get the "value" which will be the raw certificate data
45+
/// for value in session.get_attributes(handle, &[AttributeType::Value])? {
46+
/// if let Attribute::Value(value) = value {
47+
/// println!("Certificate value: {value:?}");
48+
/// }
49+
/// }
50+
/// }
51+
/// # Ok(()) }
52+
/// ```
1853
pub fn find_objects(&self, template: &[Attribute]) -> Result<Vec<ObjectHandle>> {
1954
let mut template: Vec<CK_ATTRIBUTE> = template.iter().map(|attr| attr.into()).collect();
2055

0 commit comments

Comments
 (0)