From f8d998b3a6c27e5114bdb4bcbadaacd6389f8227 Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Wed, 1 Sep 2021 22:26:25 +0200 Subject: [PATCH] Implement From, Into for CardApp and adjust client code. --- card-functionality/src/cards.rs | 4 ++-- openpgp-card-sequoia/src/lib.rs | 20 +------------------- openpgp-card-sequoia/src/main.rs | 2 +- openpgp-card/src/card_app.rs | 31 +++++++++++++++++-------------- openpgp-card/src/keys.rs | 8 +++++--- pcsc/src/lib.rs | 8 ++++---- 6 files changed, 30 insertions(+), 43 deletions(-) diff --git a/card-functionality/src/cards.rs b/card-functionality/src/cards.rs index 6b96c1d..64e90a8 100644 --- a/card-functionality/src/cards.rs +++ b/card-functionality/src/cards.rs @@ -98,7 +98,7 @@ impl TestCard { log::trace!(" Attempt to shutdown scd: {:?}", res); for card_client in PcscClient::list_cards()? { - let mut ca = CardApp::new(card_client); + let mut ca = CardApp::from(card_client); // Set Card Capabilities (chaining, command length, ..) let ard = ca.get_app_data()?; @@ -117,7 +117,7 @@ impl TestCard { } Self::Scdc(serial) => { let card_client = ScdClient::open_by_serial(None, serial)?; - let mut ca = CardApp::new(card_client); + let mut ca = CardApp::from(card_client); // Set Card Capabilities (chaining, command length, ..) let ard = ca.get_app_data()?; diff --git a/openpgp-card-sequoia/src/lib.rs b/openpgp-card-sequoia/src/lib.rs index d410b7a..1dab6e2 100644 --- a/openpgp-card-sequoia/src/lib.rs +++ b/openpgp-card-sequoia/src/lib.rs @@ -582,7 +582,7 @@ impl CardBase { /// CardClient, on which the openpgp applet has already been opened. pub fn open_card(ccb: CardClientBox) -> Result { // read and cache "application related data" - let mut card_app = CardApp::new(ccb); + let mut card_app = CardApp::from(ccb); let ard = card_app.get_app_data()?; @@ -788,15 +788,6 @@ impl CardUser { ) -> Result, OpenpgpCardError> { self.card_app.decrypt(dm) } - - /// Run decryption operation on the smartcard - /// (7.2.11 PSO: DECIPHER) - pub(crate) fn pso_decipher( - &mut self, - data: Vec, - ) -> Result, OpenpgpCardError> { - self.card_app.pso_decipher(data) - } } /// An OpenPGP card after successful verification of PW1 in mode 81 @@ -831,15 +822,6 @@ impl CardSign { ) -> Result, OpenpgpCardError> { self.card_app.signature_for_hash(hash) } - - /// Run signing operation on the smartcard - /// (7.2.10 PSO: COMPUTE DIGITAL SIGNATURE) - pub(crate) fn compute_digital_signature( - &mut self, - data: Vec, - ) -> Result, OpenpgpCardError> { - self.card_app.pso_compute_digital_signature(data) - } } /// An OpenPGP card after successful verification of PW3 ("Admin privileges") diff --git a/openpgp-card-sequoia/src/main.rs b/openpgp-card-sequoia/src/main.rs index 5885fb2..6f4a3e8 100644 --- a/openpgp-card-sequoia/src/main.rs +++ b/openpgp-card-sequoia/src/main.rs @@ -240,7 +240,7 @@ fn main() -> Result<(), Box> { let cards = PcscClient::list_cards()?; for c in cards { - let mut ca = CardApp::new(c); + let mut ca = CardApp::from(c); let ard = ca.get_app_data()?; let app_id = ard.get_application_id()?; diff --git a/openpgp-card/src/card_app.rs b/openpgp-card/src/card_app.rs index 7442191..7b968b7 100644 --- a/openpgp-card/src/card_app.rs +++ b/openpgp-card/src/card_app.rs @@ -33,21 +33,28 @@ pub struct CardApp { card_client: CardClientBox, } -impl CardApp { - /// Create a CardApp object based on a [`CardClientBox`]. - pub fn new(card_client: CardClientBox) -> Self { +impl From for CardApp { + fn from(card_client: CardClientBox) -> Self { Self { card_client } } +} - /// Take the CardClientBox out of a CardApp - pub fn take_card(self) -> CardClientBox { +impl Into for CardApp { + fn into(self) -> CardClientBox { self.card_client } +} - /// Read capabilities from the card, and set them in the CardApp. +impl CardApp { + /// Get the CardClient for this CardApp + pub(crate) fn get_card_client(&mut self) -> &mut CardClientBox { + &mut self.card_client + } + + /// Initialize the CardCaps settings in the underlying CardClient + /// from the data in `ard`. /// - /// Also initializes the underlying CardClient with the caps - some - /// implementations may need this information. + /// This should be done at an early point, soon after opening the card. pub fn init_caps(&mut self, ard: &ApplicationRelatedData) -> Result<()> { // Determine chaining/extended length support from card // metadata and cache this information in CardApp (as a @@ -82,10 +89,6 @@ impl CardApp { Ok(()) } - pub fn card(&mut self) -> &mut CardClientBox { - &mut self.card_client - } - // --- select --- /// "Select" the OpenPGP card application @@ -552,7 +555,7 @@ impl CardApp { fp.as_bytes().to_vec(), ); - apdu::send_command(self.card(), fp_cmd, false)?.try_into() + apdu::send_command(self.get_card_client(), fp_cmd, false)?.try_into() } /// Set PW Status Bytes. @@ -574,7 +577,7 @@ impl CardApp { let data = pw_status.serialize_for_put(long); let cmd = commands::put_pw_status(data); - apdu::send_command(self.card(), cmd, false)?.try_into() + apdu::send_command(self.get_card_client(), cmd, false)?.try_into() } /// Set cardholder certificate (for AUT, DEC or SIG). diff --git a/openpgp-card/src/keys.rs b/openpgp-card/src/keys.rs index 2543f0b..c254771 100644 --- a/openpgp-card/src/keys.rs +++ b/openpgp-card/src/keys.rs @@ -117,7 +117,7 @@ pub(crate) fn generate_asymmetric_key_pair( let crt = get_crt(key_type)?; let gen_key_cmd = commands::gen_key(crt.serialize().to_vec()); - let card_client = card_app.card(); + let card_client = card_app.get_card_client(); let resp = apdu::send_command(card_client, gen_key_cmd, true)?; resp.check_ok()?; @@ -145,7 +145,8 @@ pub(crate) fn get_pub_key( let crt = get_crt(key_type)?; let get_pub_key_cmd = commands::get_pub_key(crt.serialize().to_vec()); - let resp = apdu::send_command(card_app.card(), get_pub_key_cmd, true)?; + let resp = + apdu::send_command(card_app.get_card_client(), get_pub_key_cmd, true)?; resp.check_ok()?; let tlv = Tlv::try_from(resp.data()?)?; @@ -248,7 +249,8 @@ pub(crate) fn key_import( // Send all the commands card_app.set_algorithm_attributes(key_type, &algo)?; - apdu::send_command(card_app.card(), key_cmd, false)?.check_ok()?; + apdu::send_command(card_app.get_card_client(), key_cmd, false)? + .check_ok()?; card_app.set_fingerprint(fp, key_type)?; card_app.set_creation_time(ts, key_type)?; diff --git a/pcsc/src/lib.rs b/pcsc/src/lib.rs index e68083f..8b87e3b 100644 --- a/pcsc/src/lib.rs +++ b/pcsc/src/lib.rs @@ -87,7 +87,7 @@ impl PcscClient { .map(Self::select) .map(|res| res.ok()) .flatten() - .map(|ca| ca.take_card()) + .map(|ca| ca.into()) .collect(); Ok(cards) @@ -97,7 +97,7 @@ impl PcscClient { fn select(card_client: PcscClient) -> Result { let ccb = Box::new(card_client) as CardClientBox; - let mut ca = CardApp::new(ccb); + let mut ca = CardApp::from(ccb); if ca.select().is_ok() { Ok(ca) } else { @@ -114,7 +114,7 @@ impl PcscClient { pub fn open_yolo() -> Result { for card in Self::unopened_cards()? { if let Ok(ca) = Self::select(card) { - return Ok(ca.take_card()); + return Ok(ca.into()); } } @@ -133,7 +133,7 @@ impl PcscClient { let aid = ard.get_application_id()?; if aid.ident() == ident { - Ok(Some(ca.take_card())) + Ok(Some(ca.into())) } else { Ok(None) }