From 1b33e3cf0e8e05e5e77d77f03d0dee24c3a9a87b Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 9 Jan 2024 19:28:02 -0500 Subject: [PATCH] keyfork-shard: add prompt for loading any smartcard --- keyfork-shard/src/openpgp/smartcard.rs | 46 +++++++++++++++----------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/keyfork-shard/src/openpgp/smartcard.rs b/keyfork-shard/src/openpgp/smartcard.rs index a4b0039..c9f4000 100644 --- a/keyfork-shard/src/openpgp/smartcard.rs +++ b/keyfork-shard/src/openpgp/smartcard.rs @@ -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 { - PcscBackend::cards(None)? - .next() - .transpose()? - .ok_or(Error::SmartCardNotFound) - .and_then(|backend| { - let mut card = Card::::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::::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)