From 31e577c89675e1333d2f05be5b9de527d94d02b2 Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Tue, 13 Jul 2021 00:08:45 +0200 Subject: [PATCH] Refactoring low level API for card-functionality tests --- openpgp-card/src/apdu/mod.rs | 16 +++++++++++++--- openpgp-card/src/card_app.rs | 12 ++++++++++++ openpgp-card/src/lib.rs | 2 +- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/openpgp-card/src/apdu/mod.rs b/openpgp-card/src/apdu/mod.rs index ef633dd..4de8ac6 100644 --- a/openpgp-card/src/apdu/mod.rs +++ b/openpgp-card/src/apdu/mod.rs @@ -11,6 +11,8 @@ use std::convert::TryFrom; use crate::apdu::command::Command; use crate::apdu::response::Response; +use crate::card; +use crate::card_app::CardApp; use crate::errors::{OcErrorStatus, OpenpgpCardError, SmartcardError}; use crate::{CardBase, CardCaps, CardClient, CardClientBox}; @@ -188,6 +190,14 @@ impl PcscClient { Self { card } } + pub fn list_cards() -> Result> { + Ok(card::get_cards() + .map_err(|err| anyhow!(err))? + .into_iter() + .map(PcscClient::new) + .collect()) + } + /// Take a PCSC Card object and try to open the OpenPGP card applet. /// If successful, wrap and return the resulting CardClient as a /// CardBase object (which involves caching the "application related @@ -196,11 +206,11 @@ impl PcscClient { let card_client = PcscClient::new(card); let mut ccb = Box::new(card_client) as CardClientBox; - let select_openpgp = commands::select_openpgp(); - let resp = send_command(&mut ccb, select_openpgp, false, None)?; + let mut ca = CardApp::new(ccb); + let resp = ca.select()?; if resp.is_ok() { - CardBase::open_card(ccb) + CardBase::open_card(ca.take_card()) } else { Err(anyhow!("Couldn't open OpenPGP application").into()) } diff --git a/openpgp-card/src/card_app.rs b/openpgp-card/src/card_app.rs index 1608cf6..b10a2aa 100644 --- a/openpgp-card/src/card_app.rs +++ b/openpgp-card/src/card_app.rs @@ -41,6 +41,10 @@ impl CardApp { } } + pub(crate) fn take_card(self) -> CardClientBox { + self.card_client + } + pub fn set_caps(self, card_caps: CardCaps) -> Self { Self { card_client: self.card_client, @@ -56,6 +60,14 @@ impl CardApp { self.card_caps.as_ref() } + // --- select --- + + /// "Select" the OpenPGP card application + pub fn select(&mut self) -> Result { + let select_openpgp = commands::select_openpgp(); + apdu::send_command(&mut self.card_client, select_openpgp, false, None) + } + // --- application data --- /// Load "application related data". diff --git a/openpgp-card/src/lib.rs b/openpgp-card/src/lib.rs index 770165e..c7b40fe 100644 --- a/openpgp-card/src/lib.rs +++ b/openpgp-card/src/lib.rs @@ -18,7 +18,7 @@ use crate::card_app::CardApp; use crate::errors::{OpenpgpCardError, SmartcardError}; use std::ops::{Deref, DerefMut}; -mod apdu; +pub mod apdu; mod card; pub mod card_app; pub mod errors;