From 4c264a59b451da4755719a38a813e1130a656164 Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Fri, 12 Nov 2021 19:07:15 +0100 Subject: [PATCH] Minor restructuring. --- pcsc/src/lib.rs | 89 +++++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/pcsc/src/lib.rs b/pcsc/src/lib.rs index 43671cb..b10c792 100644 --- a/pcsc/src/lib.rs +++ b/pcsc/src/lib.rs @@ -12,6 +12,46 @@ pub struct PcscClient { } impl PcscClient { + /// Return all cards on which the OpenPGP application could be selected. + /// + /// Each card has the OpenPGP application selected, CardCaps have been + /// initialized. + pub fn cards() -> Result> { + let mut cards = vec![]; + + for mut card in Self::unopened_cards()? { + if Self::select(&mut card).is_ok() { + if let Ok(ca) = card.into_card_app() { + cards.push(ca); + } + } + } + + Ok(cards) + } + + /// Returns the OpenPGP card that matches `ident`, if it is available. + /// A fully initialized CardApp is returned: the OpenPGP application has + /// been selected, CardCaps have been set. + pub fn open_by_ident(ident: &str) -> Result { + for mut card in Self::unopened_cards()? { + if Self::select(&mut card).is_ok() { + let mut ca = card.into_card_app()?; + + let ard = ca.get_application_related_data()?; + let aid = ard.get_application_id()?; + + if aid.ident() == ident { + return Ok(ca); + } + } + } + + Err(Error::Smartcard(SmartcardError::CardNotFound( + ident.to_string(), + ))) + } + fn new(card: Card) -> Self { Self { card, @@ -19,13 +59,14 @@ impl PcscClient { } } - /// Get an initialized CardApp from a card + /// Make an initialized CardApp from a PcscClient fn into_card_app(self) -> Result { CardApp::initialize(Box::new(self)) } - /// Opened PCSC Cards without selecting the OpenPGP card application - fn pcsc_cards() -> Result, SmartcardError> { + /// A list of "raw" opened PCSC Cards (without selecting the OpenPGP card + /// application) + fn raw_pcsc_cards() -> Result, SmartcardError> { let ctx = match Context::establish(Scope::User) { Ok(ctx) => ctx, Err(err) => { @@ -77,31 +118,13 @@ impl PcscClient { /// All PCSC cards, wrapped as PcscClient fn unopened_cards() -> Result> { - Ok(Self::pcsc_cards() + Ok(Self::raw_pcsc_cards() .map_err(|err| anyhow!(err))? .into_iter() .map(PcscClient::new) .collect()) } - /// Return all cards on which the OpenPGP application could be selected. - /// - /// Each card has the OpenPGP application selected, CardCaps have been - /// initialized. - pub fn cards() -> Result> { - let mut cards = vec![]; - - for mut card in Self::unopened_cards()? { - if Self::select(&mut card).is_ok() { - if let Ok(ca) = card.into_card_app() { - cards.push(ca); - } - } - } - - Ok(cards) - } - /// Try to select the OpenPGP application on a card fn select(card_client: &mut PcscClient) -> Result<(), Error> { if ::select(card_client).is_ok() { @@ -110,28 +133,6 @@ impl PcscClient { Err(Error::Smartcard(SmartcardError::SelectOpenPGPCardFailed)) } } - - /// Returns the OpenPGP card that matches `ident`, if it is available. - /// A fully initialized CardApp is returned: the OpenPGP application has - /// been selected, CardCaps have been set. - pub fn open_by_ident(ident: &str) -> Result { - for mut card in Self::unopened_cards()? { - if Self::select(&mut card).is_ok() { - let mut ca = card.into_card_app()?; - - let ard = ca.get_application_related_data()?; - let aid = ard.get_application_id()?; - - if aid.ident() == ident { - return Ok(ca); - } - } - } - - Err(Error::Smartcard(SmartcardError::CardNotFound( - ident.to_string(), - ))) - } } impl CardClient for PcscClient {