Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
nickray committed Jan 10, 2021
1 parent c227c81 commit f79c390
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pkcs11-uri"
version = "0.1.0"
version = "0.1.1"
authors = ["Nicolas Stalder <n@stalder.io>"]
edition = "2018"
description = "PKCS #11 URI parser"
Expand Down
1 change: 1 addition & 0 deletions examples/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
14 changes: 10 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f79c390

Please sign in to comment.