diff --git a/Cargo.toml b/Cargo.toml index 56e7f47..b922839 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pkcs11-uri" -version = "0.1.0" +version = "0.1.1" authors = ["Nicolas Stalder "] edition = "2018" description = "PKCS #11 URI parser" diff --git a/examples/lookup.rs b/examples/lookup.rs index dcef4c1..075c09a 100644 --- a/examples/lookup.rs +++ b/examples/lookup.rs @@ -45,5 +45,6 @@ fn try_main() -> anyhow::Result<()> { let signature = context.sign(session, &data).unwrap(); println!("signature: {:x?}", signature.as_slice()); + assert_eq!(signature.len(), 256); Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 46891f7..843bc87 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -244,6 +244,11 @@ impl<'a> TryFrom<&'a str> for Pkcs11Uri { } } +pub fn split_once(s: &str, delimiter: char) -> Option<(&str, &str)> { + let i = s.find(delimiter)?; + Some((&s[..i], &s[i + 1..])) +} + impl Pkcs11Uri { fn matches_slot(&self, ctx: &pkcs11::Ctx, slot_id: pkcs11::types::CK_SLOT_ID) -> bool { // slot_id, slot_description, slot_manufacturer @@ -376,17 +381,18 @@ impl Pkcs11Uri { ctx.login(session, pkcs11::types::CKU_USER, Some(pin)) .unwrap(); } else if let Some(source) = self.query_attributes.pin_source.as_deref() { - if let Some(index) = source.find(':') { - let scheme = &source[..index]; + if let Some((scheme, content)) = split_once(source, ':') { match scheme { "env" => { - let pin = std::env::var(&source[4..]).unwrap(); + let pin = std::env::var(content).unwrap(); trace!("{:?}", pin); ctx.login(session, pkcs11::types::CKU_USER, Some(&pin)) .unwrap(); } "file" => { - let pin = String::from_utf8_lossy(&std::fs::read(&source[5..]).unwrap()).trim().to_string(); + let pin = String::from_utf8_lossy(&std::fs::read(content).unwrap()) + .trim() + .to_string(); trace!("{:?}", pin); ctx.login(session, pkcs11::types::CKU_USER, Some(pin.as_str())) .unwrap();