From 7afe2f52c2381e69fb7b33e1d1c06f21c50b9c6b Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Wed, 14 Jul 2021 18:03:07 +0200 Subject: [PATCH] Refactor card initialisation --- openpgp-card/src/card_app.rs | 33 +++++++++++++++++++++++++++++++++ openpgp-card/src/lib.rs | 30 ++---------------------------- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/openpgp-card/src/card_app.rs b/openpgp-card/src/card_app.rs index b10a2aa..ea7c05b 100644 --- a/openpgp-card/src/card_app.rs +++ b/openpgp-card/src/card_app.rs @@ -45,6 +45,39 @@ impl CardApp { self.card_client } + /// Read capabilities from the card, and set them in the CardApp + pub fn init_caps(mut self, ard: &Tlv) -> Result { + // Determine chaining/extended length support from card + // metadata and cache this information in CardApp (as a + // CardCaps) + + let mut ext_support = false; + let mut chaining_support = false; + + if let Ok(hist) = CardApp::get_historical(&ard) { + if let Some(cc) = hist.get_card_capabilities() { + chaining_support = cc.get_command_chaining(); + ext_support = cc.get_extended_lc_le(); + } + } + + let max_cmd_bytes = if let Ok(Some(eli)) = + CardApp::get_extended_length_information(&ard) + { + eli.max_command_bytes + } else { + 255 + }; + + let caps = CardCaps { + ext_support, + chaining_support, + max_cmd_bytes, + }; + + Ok(self.set_caps(caps)) + } + pub fn set_caps(self, card_caps: CardCaps) -> Self { Self { card_client: self.card_client, diff --git a/openpgp-card/src/lib.rs b/openpgp-card/src/lib.rs index bb47181..6750e9a 100644 --- a/openpgp-card/src/lib.rs +++ b/openpgp-card/src/lib.rs @@ -314,36 +314,10 @@ impl CardBase { pub fn open_card(ccb: CardClientBox) -> Result { // read and cache "application related data" let mut card_app = CardApp::new(ccb); + let ard = card_app.get_app_data()?; - // Determine chaining/extended length support from card - // metadata and cache this information in CardApp (as a - // CardCaps) - - let mut ext_support = false; - let mut chaining_support = false; - - if let Ok(hist) = CardApp::get_historical(&ard) { - if let Some(cc) = hist.get_card_capabilities() { - chaining_support = cc.get_command_chaining(); - ext_support = cc.get_extended_lc_le(); - } - } - - let max_cmd_bytes = if let Ok(Some(eli)) = - CardApp::get_extended_length_information(&ard) - { - eli.max_command_bytes - } else { - 255 - }; - - let caps = CardCaps { - ext_support, - chaining_support, - max_cmd_bytes, - }; - let card_app = card_app.set_caps(caps); + card_app = card_app.init_caps(&ard)?; Ok(Self { card_app, ard }) }