From cf5090bbc6377a29b0705a12f65642e58a17170a Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Mon, 1 May 2023 18:06:43 +0200 Subject: [PATCH] pcsc: add fn PcscBackend::activate_terminated_card() --- pcsc/Cargo.toml | 2 +- pcsc/src/lib.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/pcsc/Cargo.toml b/pcsc/Cargo.toml index 32677d7..cf69846 100644 --- a/pcsc/Cargo.toml +++ b/pcsc/Cargo.toml @@ -12,7 +12,7 @@ repository = "https://gitlab.com/openpgp-card/openpgp-card" documentation = "https://docs.rs/crate/openpgp-card-pcsc" [dependencies] -openpgp-card = { path = "../openpgp-card", version = "0.3" } +openpgp-card = { path = "../openpgp-card", version = "0.3.5" } iso7816-tlv = "0.4" pcsc = "2.7" log = "0.4" diff --git a/pcsc/src/lib.rs b/pcsc/src/lib.rs index 98dedd6..b7f52f8 100644 --- a/pcsc/src/lib.rs +++ b/pcsc/src/lib.rs @@ -646,6 +646,38 @@ impl PcscBackend { fn reader_caps(&self) -> HashMap { self.reader_caps.clone() } + + /// This command will try to activate an OpenPGP card, if: + /// - exactly one card is connected to the system + /// - that card replies to SELECT with Status 6285 + /// + /// See OpenPGP card spec (version 3.4.1): 7.2.17 ACTIVATE FILE + pub fn activate_terminated_card() -> Result<(), Error> { + let mut cards = + Self::raw_pcsc_cards(pcsc::ShareMode::Exclusive).map_err(Error::Smartcard)?; + if cards.len() != 1 { + return Err(Error::InternalError(format!( + "This command is only allowed if exactly one card is connected, found {}.", + cards.len() + ))); + } + + let card = cards.pop().unwrap(); + + let mut backend = PcscBackend::new(card, pcsc::ShareMode::Exclusive); + let mut card_tx = Box::new(PcscTransaction::new(&mut backend, false)?); + + match ::select(&mut card_tx) { + Err(Error::CardStatus(openpgp_card::StatusBytes::TerminationState)) => { + let _ = ::activate_file(&mut card_tx)?; + Ok(()) + } + + _ => Err(Error::InternalError( + "Card doesn't appear to be terminated.".to_string(), + )), + } + } } impl CardBackend for PcscBackend {