keyfork-shard: add prompt for loading any smartcard

This commit is contained in:
Ryan Heywood 2024-01-09 19:28:02 -05:00
parent 4384964ea5
commit 1b33e3cf0e
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
1 changed files with 26 additions and 20 deletions

View File

@ -24,7 +24,7 @@ pub enum Error {
#[error("Smart card could not decrypt any matching PKESK packets")]
SmartCardCouldNotDecrypt,
#[error("No smart card backend was stored")]
#[error("No smart card was found")]
SmartCardNotFound,
#[error("Selected smart card has no decryption key")]
@ -92,23 +92,28 @@ impl SmartcardManager {
/// Load any backend.
pub fn load_any_card(&mut self) -> Result<Fingerprint> {
PcscBackend::cards(None)?
.next()
.transpose()?
.ok_or(Error::SmartCardNotFound)
.and_then(|backend| {
let mut card = Card::<Open>::new(backend).map_err(Error::OpenSmartCard)?;
let transaction = card.transaction().map_err(Error::Transaction)?;
let fingerprint = transaction
.fingerprints()
.map_err(Error::Fingerprints)?
.decryption()
.map(|fp| Fingerprint::from_bytes(fp.as_bytes()))
.ok_or(Error::SmartCardHasNoDecrypt)?;
drop(transaction);
self.current_card.replace(card);
Ok(fingerprint)
})
let card_backend = loop {
self.pm.prompt_message(&Message::Text(
"Please plug in a smart card and press enter".to_string(),
))?;
if let Some(c) = PcscBackend::cards(None)?.next().transpose()? {
break c;
}
self.pm.prompt_message(&Message::Text(
"No smart card was found".to_string(),
))?;
};
let mut card = Card::<Open>::new(card_backend).map_err(Error::OpenSmartCard)?;
let transaction = card.transaction().map_err(Error::Transaction)?;
let fingerprint = transaction
.fingerprints()
.map_err(Error::Fingerprints)?
.decryption()
.map(|fp| Fingerprint::from_bytes(fp.as_bytes()))
.ok_or(Error::SmartCardHasNoDecrypt)?;
drop(transaction);
self.current_card.replace(card);
Ok(fingerprint)
}
/// Load a backend if any [`Fingerprint`] has been matched by a currently active card.
@ -150,8 +155,9 @@ impl SmartcardManager {
}
}
#[rustfmt::skip]
self.pm.prompt_message(&Message::Text("Please plug in a smart card and press enter".to_string()))?;
self.pm.prompt_message(&Message::Text(
"Please plug in a smart card and press enter".to_string(),
))?;
}
Ok(None)