Rename pcsc::PcscCard -> pcsc::PcscBackend, pcsc::TxClient -> pcsc::PcscTransaction

This commit is contained in:
Heiko Schaefer 2022-02-18 15:58:12 +01:00
parent 282b16fdba
commit 1496da6dd5
No known key found for this signature in database
GPG key ID: 4A849A1904CCBD7D
9 changed files with 49 additions and 47 deletions

View file

@ -10,7 +10,7 @@ use serde_derive::Deserialize;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use openpgp_card::{CardBackend, Error}; use openpgp_card::{CardBackend, Error};
use openpgp_card_pcsc::PcscCard; use openpgp_card_pcsc::PcscBackend;
use openpgp_card_scdc::ScdClient; use openpgp_card_scdc::ScdClient;
const SHARE_MODE: Option<ShareMode> = Some(ShareMode::Shared); const SHARE_MODE: Option<ShareMode> = Some(ShareMode::Shared);
@ -104,7 +104,7 @@ impl TestCard {
// (this can be useful in ShareMode::Exclusive) // (this can be useful in ShareMode::Exclusive)
let mut i = 1; let mut i = 1;
let card: Result<Box<dyn CardBackend>, Error> = loop { let card: Result<Box<dyn CardBackend>, Error> = loop {
let res = PcscCard::open_by_ident(ident, SHARE_MODE); let res = PcscBackend::open_by_ident(ident, SHARE_MODE);
if i == 3 || res.is_ok() { if i == 3 || res.is_ok() {
break res.map(Into::into); break res.map(Into::into);

View file

@ -4,13 +4,13 @@
use anyhow::Result; use anyhow::Result;
use openpgp_card::CardBackend; use openpgp_card::CardBackend;
use openpgp_card_pcsc::PcscCard; use openpgp_card_pcsc::PcscBackend;
use openpgp_card_sequoia::card::Open; use openpgp_card_sequoia::card::Open;
fn main() -> Result<()> { fn main() -> Result<()> {
println!("The following OpenPGP cards are connected to your system:"); println!("The following OpenPGP cards are connected to your system:");
for mut card in PcscCard::cards(None)? { for mut card in PcscBackend::cards(None)? {
let mut txc = card.transaction()?; let mut txc = card.transaction()?;
let open = Open::new(&mut *txc)?; let open = Open::new(&mut *txc)?;

View file

@ -2,7 +2,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0 // SPDX-License-Identifier: MIT OR Apache-2.0
use openpgp_card::CardBackend; use openpgp_card::CardBackend;
use openpgp_card_pcsc::PcscCard; use openpgp_card_pcsc::PcscBackend;
use openpgp_card_sequoia::card::Open; use openpgp_card_sequoia::card::Open;
use openpgp::parse::{stream::DecryptorBuilder, Parse}; use openpgp::parse::{stream::DecryptorBuilder, Parse};
@ -22,7 +22,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let pin_file = &args[1]; let pin_file = &args[1];
let cert_file = &args[2]; let cert_file = &args[2];
let mut card = PcscCard::open_by_ident(card_ident, None)?; let mut card = PcscBackend::open_by_ident(card_ident, None)?;
let mut txc = card.transaction()?; let mut txc = card.transaction()?;
let mut open = Open::new(&mut *txc)?; let mut open = Open::new(&mut *txc)?;

View file

@ -2,7 +2,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0 // SPDX-License-Identifier: MIT OR Apache-2.0
use openpgp_card::CardBackend; use openpgp_card::CardBackend;
use openpgp_card_pcsc::PcscCard; use openpgp_card_pcsc::PcscBackend;
use openpgp_card_sequoia::card::Open; use openpgp_card_sequoia::card::Open;
use openpgp::parse::Parse; use openpgp::parse::Parse;
@ -22,7 +22,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let pin_file = &args[1]; let pin_file = &args[1];
let cert_file = &args[2]; let cert_file = &args[2];
let mut card = PcscCard::open_by_ident(card_ident, None)?; let mut card = PcscBackend::open_by_ident(card_ident, None)?;
let mut txc = card.transaction()?; let mut txc = card.transaction()?;
let mut open = Open::new(&mut *txc)?; let mut open = Open::new(&mut *txc)?;

View file

@ -12,12 +12,12 @@
//! With `openpgp-card-pcsc` you can either open all available cards: //! With `openpgp-card-pcsc` you can either open all available cards:
//! //!
//! ```no_run //! ```no_run
//! use openpgp_card_pcsc::PcscCard; //! use openpgp_card_pcsc::PcscBackend;
//! use openpgp_card::CardBackend; //! use openpgp_card::CardBackend;
//! use openpgp_card_sequoia::card::Open; //! use openpgp_card_sequoia::card::Open;
//! //!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> { //! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! for mut card in PcscCard::cards(None)? { //! for mut card in PcscBackend::cards(None)? {
//! let mut txc = card.transaction()?; //! let mut txc = card.transaction()?;
//! let open = Open::new(&mut *txc)?; //! let open = Open::new(&mut *txc)?;
//! println!("Found OpenPGP card with ident '{}'", //! println!("Found OpenPGP card with ident '{}'",
@ -30,12 +30,12 @@
//! Or you can open one particular card, by ident: //! Or you can open one particular card, by ident:
//! //!
//! ```no_run //! ```no_run
//! use openpgp_card_pcsc::PcscCard; //! use openpgp_card_pcsc::PcscBackend;
//! use openpgp_card::CardBackend; //! use openpgp_card::CardBackend;
//! use openpgp_card_sequoia::card::Open; //! use openpgp_card_sequoia::card::Open;
//! //!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> { //! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let mut card = PcscCard::open_by_ident("abcd:12345678", None)?; //! let mut card = PcscBackend::open_by_ident("abcd:12345678", None)?;
//! let mut txc = card.transaction()?; //! let mut txc = card.transaction()?;
//! let mut open = Open::new(&mut *txc)?; //! let mut open = Open::new(&mut *txc)?;
//! # Ok(()) //! # Ok(())
@ -52,14 +52,14 @@
//! that corresponds to the private encryption key on the card: //! that corresponds to the private encryption key on the card:
//! //!
//! ```no_run //! ```no_run
//! use openpgp_card_pcsc::PcscCard; //! use openpgp_card_pcsc::PcscBackend;
//! use openpgp_card::CardBackend; //! use openpgp_card::CardBackend;
//! use openpgp_card_sequoia::card::Open; //! use openpgp_card_sequoia::card::Open;
//! //!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> { //! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Open card via PCSC //! // Open card via PCSC
//! use sequoia_openpgp::policy::StandardPolicy; //! use sequoia_openpgp::policy::StandardPolicy;
//! let mut card = PcscCard::open_by_ident("abcd:12345678", None)?; //! let mut card = PcscBackend::open_by_ident("abcd:12345678", None)?;
//! let mut txc = card.transaction()?; //! let mut txc = card.transaction()?;
//! let mut open = Open::new(&mut *txc)?; //! let mut open = Open::new(&mut *txc)?;
//! //!
@ -95,14 +95,14 @@
//! user password before each signing operation!) //! user password before each signing operation!)
//! //!
//! ```no_run //! ```no_run
//! use openpgp_card_pcsc::PcscCard; //! use openpgp_card_pcsc::PcscBackend;
//! use openpgp_card::CardBackend; //! use openpgp_card::CardBackend;
//! use openpgp_card_sequoia::card::Open; //! use openpgp_card_sequoia::card::Open;
//! //!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> { //! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Open card via PCSC //! // Open card via PCSC
//! use sequoia_openpgp::policy::StandardPolicy; //! use sequoia_openpgp::policy::StandardPolicy;
//! let mut card = PcscCard::open_by_ident("abcd:12345678", None)?; //! let mut card = PcscBackend::open_by_ident("abcd:12345678", None)?;
//! let mut txc = card.transaction()?; //! let mut txc = card.transaction()?;
//! let mut open = Open::new(&mut *txc)?; //! let mut open = Open::new(&mut *txc)?;
//! //!
@ -128,13 +128,13 @@
//! # Setting up and configuring a card //! # Setting up and configuring a card
//! //!
//! ```no_run //! ```no_run
//! use openpgp_card_pcsc::PcscCard; //! use openpgp_card_pcsc::PcscBackend;
//! use openpgp_card::CardBackend; //! use openpgp_card::CardBackend;
//! use openpgp_card_sequoia::card::Open; //! use openpgp_card_sequoia::card::Open;
//! //!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> { //! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Open card via PCSC //! // Open card via PCSC
//! let mut card = PcscCard::open_by_ident("abcd:12345678", None)?; //! let mut card = PcscBackend::open_by_ident("abcd:12345678", None)?;
//! let mut txc = card.transaction()?; //! let mut txc = card.transaction()?;
//! let mut open = Open::new(&mut *txc)?; //! let mut open = Open::new(&mut *txc)?;
//! //!

View file

@ -11,7 +11,7 @@ use sequoia_openpgp::Cert;
use openpgp_card::card_do::Sex; use openpgp_card::card_do::Sex;
use openpgp_card::{CardBackend, KeyType}; use openpgp_card::{CardBackend, KeyType};
use openpgp_card_pcsc::PcscCard; use openpgp_card_pcsc::PcscBackend;
use openpgp_card_sequoia::card::Open; use openpgp_card_sequoia::card::Open;
use openpgp_card_sequoia::sq_util; use openpgp_card_sequoia::sq_util;
@ -35,7 +35,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let test_card_ident = env::var("TEST_CARD_IDENT"); let test_card_ident = env::var("TEST_CARD_IDENT");
if let Ok(test_card_ident) = test_card_ident { if let Ok(test_card_ident) = test_card_ident {
let mut card = PcscCard::open_by_ident(&test_card_ident, None)?; let mut card = PcscBackend::open_by_ident(&test_card_ident, None)?;
let mut txc = card.transaction()?; let mut txc = card.transaction()?;
let mut open = Open::new(&mut *txc)?; let mut open = Open::new(&mut *txc)?;
@ -146,7 +146,7 @@ fn main() -> Result<(), Box<dyn Error>> {
// ----------------------------- // -----------------------------
// Open fresh Card for decrypt // Open fresh Card for decrypt
// ----------------------------- // -----------------------------
let mut card = PcscCard::open_by_ident(&test_card_ident, None)?; let mut card = PcscBackend::open_by_ident(&test_card_ident, None)?;
let mut txc = card.transaction()?; let mut txc = card.transaction()?;
let mut open = Open::new(&mut *txc)?; let mut open = Open::new(&mut *txc)?;
@ -187,7 +187,7 @@ fn main() -> Result<(), Box<dyn Error>> {
// ----------------------------- // -----------------------------
// Open fresh Card for signing // Open fresh Card for signing
// ----------------------------- // -----------------------------
let mut card = PcscCard::open_by_ident(&test_card_ident, None)?; let mut card = PcscBackend::open_by_ident(&test_card_ident, None)?;
let mut txc = card.transaction()?; let mut txc = card.transaction()?;
let mut open = Open::new(&mut *txc)?; let mut open = Open::new(&mut *txc)?;
@ -219,7 +219,7 @@ fn main() -> Result<(), Box<dyn Error>> {
println!("The following OpenPGP cards are connected to your system:"); println!("The following OpenPGP cards are connected to your system:");
for mut card in PcscCard::cards(None)? { for mut card in PcscBackend::cards(None)? {
let mut txc = card.transaction()?; let mut txc = card.transaction()?;
let open = Open::new(&mut *txc)?; let open = Open::new(&mut *txc)?;

View file

@ -33,15 +33,15 @@ fn default_mode(mode: Option<ShareMode>) -> ShareMode {
/// This struct can be used to hold on to a Card, even while no operations /// This struct can be used to hold on to a Card, even while no operations
/// are performed on the Card. To perform operations on the card, a /// are performed on the Card. To perform operations on the card, a
/// `TxClient` object needs to be obtained (via PcscCard::transaction()). /// `TxClient` object needs to be obtained (via PcscCard::transaction()).
pub struct PcscCard { pub struct PcscBackend {
card: Card, card: Card,
mode: ShareMode, mode: ShareMode,
card_caps: Option<CardCaps>, card_caps: Option<CardCaps>,
reader_caps: HashMap<u8, Tlv>, reader_caps: HashMap<u8, Tlv>,
} }
impl From<PcscCard> for Box<dyn CardBackend> { impl From<PcscBackend> for Box<dyn CardBackend> {
fn from(card: PcscCard) -> Box<dyn CardBackend> { fn from(card: PcscBackend) -> Box<dyn CardBackend> {
Box::new(card) as Box<dyn CardBackend> Box::new(card) as Box<dyn CardBackend>
} }
} }
@ -57,19 +57,19 @@ impl From<PcscCard> for Box<dyn CardBackend> {
/// (e.g. Microsoft documents that on Windows, they will be closed after /// (e.g. Microsoft documents that on Windows, they will be closed after
/// 5s without a command: /// 5s without a command:
/// <https://docs.microsoft.com/en-us/windows/win32/api/winscard/nf-winscard-scardbegintransaction?redirectedfrom=MSDN#remarks>) /// <https://docs.microsoft.com/en-us/windows/win32/api/winscard/nf-winscard-scardbegintransaction?redirectedfrom=MSDN#remarks>)
pub struct TxClient<'b> { pub struct PcscTransaction<'b> {
tx: Transaction<'b>, tx: Transaction<'b>,
card_caps: Option<CardCaps>, // FIXME: manual copy from PcscCard card_caps: Option<CardCaps>, // FIXME: manual copy from PcscCard
reader_caps: HashMap<u8, Tlv>, // FIXME: manual copy from PcscCard reader_caps: HashMap<u8, Tlv>, // FIXME: manual copy from PcscCard
} }
impl<'b> TxClient<'b> { impl<'b> PcscTransaction<'b> {
/// Start a transaction on `card`. /// Start a transaction on `card`.
/// ///
/// `reselect` set to `false` is only used internally in this crate, /// `reselect` set to `false` is only used internally in this crate,
/// during initial setup of cards. Otherwise it must be `true`, to /// during initial setup of cards. Otherwise it must be `true`, to
/// cause a select() call on cards that have been reset. /// cause a select() call on cards that have been reset.
fn new(card: &'b mut PcscCard, reselect: bool) -> Result<Self, Error> { fn new(card: &'b mut PcscBackend, reselect: bool) -> Result<Self, Error> {
use pcsc::Disposition; use pcsc::Disposition;
let mut was_reset = false; let mut was_reset = false;
@ -101,7 +101,7 @@ impl<'b> TxClient<'b> {
// the caller always expects a card that has not // the caller always expects a card that has not
// been "select"ed yet. // been "select"ed yet.
if reselect { if reselect {
TxClient::select(&mut txc)?; PcscTransaction::select(&mut txc)?;
} }
tx = txc.tx; tx = txc.tx;
@ -155,7 +155,7 @@ impl<'b> TxClient<'b> {
} }
/// Try to select the OpenPGP application on a card /// Try to select the OpenPGP application on a card
fn select(card_tx: &mut TxClient) -> Result<(), Error> { fn select(card_tx: &mut PcscTransaction) -> Result<(), Error> {
if <dyn CardTransaction>::select(card_tx).is_ok() { if <dyn CardTransaction>::select(card_tx).is_ok() {
Ok(()) Ok(())
} else { } else {
@ -165,7 +165,7 @@ impl<'b> TxClient<'b> {
/// Get application_related_data from card /// Get application_related_data from card
fn application_related_data( fn application_related_data(
card_tx: &mut TxClient, card_tx: &mut PcscTransaction,
) -> Result<ApplicationRelatedData, Error> { ) -> Result<ApplicationRelatedData, Error> {
<dyn CardTransaction>::application_related_data(card_tx).map_err(|e| { <dyn CardTransaction>::application_related_data(card_tx).map_err(|e| {
Error::Smartcard(SmartcardError::Error(format!( Error::Smartcard(SmartcardError::Error(format!(
@ -216,7 +216,7 @@ impl<'b> TxClient<'b> {
} }
} }
impl CardTransaction for TxClient<'_> { impl CardTransaction for PcscTransaction<'_> {
fn transmit( fn transmit(
&mut self, &mut self,
cmd: &[u8], cmd: &[u8],
@ -451,7 +451,7 @@ impl CardTransaction for TxClient<'_> {
} }
} }
impl PcscCard { impl PcscBackend {
fn card(&mut self) -> &mut Card { fn card(&mut self) -> &mut Card {
&mut self.card &mut self.card
} }
@ -547,11 +547,11 @@ impl PcscCard {
let mut store_card = false; let mut store_card = false;
{ {
// start transaction // start transaction
let mut p = PcscCard::new(card, mode); let mut p = PcscBackend::new(card, mode);
let mut txc = TxClient::new(&mut p, false)?; let mut txc = PcscTransaction::new(&mut p, false)?;
{ {
if let Err(e) = TxClient::select(&mut txc) { if let Err(e) = PcscTransaction::select(&mut txc) {
log::debug!(" select error: {:?}", e); log::debug!(" select error: {:?}", e);
} else { } else {
// successfully opened the OpenPGP application // successfully opened the OpenPGP application
@ -560,7 +560,9 @@ impl PcscCard {
if let Some(ident) = ident { if let Some(ident) = ident {
if let Ok(ard) = if let Ok(ard) =
TxClient::application_related_data(&mut txc) PcscTransaction::application_related_data(
&mut txc,
)
{ {
let aid = ard.application_id()?; let aid = ard.application_id()?;
@ -593,7 +595,7 @@ impl PcscCard {
} }
if store_card { if store_card {
let pcsc = PcscCard::new(card, mode); let pcsc = PcscBackend::new(card, mode);
cards.push(pcsc.initialize_card()?); cards.push(pcsc.initialize_card()?);
} }
} }
@ -650,7 +652,7 @@ impl PcscCard {
let mut h: HashMap<u8, Tlv> = HashMap::default(); let mut h: HashMap<u8, Tlv> = HashMap::default();
let mut txc = TxClient::new(&mut self, true)?; let mut txc = PcscTransaction::new(&mut self, true)?;
// Get Features from reader (pinpad verify/modify) // Get Features from reader (pinpad verify/modify)
if let Ok(feat) = txc.features() { if let Ok(feat) = txc.features() {
@ -684,11 +686,11 @@ impl PcscCard {
} }
} }
impl CardBackend for PcscCard { impl CardBackend for PcscBackend {
/// Get a TxClient for this PcscCard (this starts a transaction) /// Get a TxClient for this PcscCard (this starts a transaction)
fn transaction( fn transaction(
&mut self, &mut self,
) -> Result<Box<dyn CardTransaction + Send + Sync + '_>, Error> { ) -> Result<Box<dyn CardTransaction + Send + Sync + '_>, Error> {
Ok(Box::new(TxClient::new(self, true)?)) Ok(Box::new(PcscTransaction::new(self, true)?))
} }
} }

View file

@ -5,7 +5,7 @@ use anyhow::Result;
use structopt::StructOpt; use structopt::StructOpt;
use openpgp_card::{CardBackend, Error, StatusBytes}; use openpgp_card::{CardBackend, Error, StatusBytes};
use openpgp_card_pcsc::PcscCard; use openpgp_card_pcsc::PcscBackend;
use openpgp_card_sequoia::card::Open; use openpgp_card_sequoia::card::Open;
mod cli; mod cli;
@ -15,7 +15,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let cli = cli::Cli::from_args(); let cli = cli::Cli::from_args();
let mut card = PcscCard::open_by_ident(&cli.ident, None)?; let mut card = PcscBackend::open_by_ident(&cli.ident, None)?;
let mut txc = card.transaction()?; let mut txc = card.transaction()?;
let pinpad_verify = txc.feature_pinpad_verify(); let pinpad_verify = txc.feature_pinpad_verify();

View file

@ -7,16 +7,16 @@ use std::path::{Path, PathBuf};
use openpgp_card::algorithm::{Algo, Curve}; use openpgp_card::algorithm::{Algo, Curve};
use openpgp_card::crypto_data::{EccType, PublicKeyMaterial}; use openpgp_card::crypto_data::{EccType, PublicKeyMaterial};
use openpgp_card::{CardBackend, Error}; use openpgp_card::{CardBackend, Error};
use openpgp_card_pcsc::PcscCard; use openpgp_card_pcsc::PcscBackend;
use openpgp_card_sequoia::card::{Admin, Open, Sign, User}; use openpgp_card_sequoia::card::{Admin, Open, Sign, User};
pub(crate) fn cards() -> Result<Vec<Box<dyn CardBackend>>, Error> { pub(crate) fn cards() -> Result<Vec<Box<dyn CardBackend>>, Error> {
PcscCard::cards(None) PcscBackend::cards(None)
.map(|cards| cards.into_iter().map(Into::into).collect()) .map(|cards| cards.into_iter().map(Into::into).collect())
} }
pub(crate) fn open_card(ident: &str) -> Result<Box<dyn CardBackend>, Error> { pub(crate) fn open_card(ident: &str) -> Result<Box<dyn CardBackend>, Error> {
PcscCard::open_by_ident(ident, None).map(Into::into) PcscBackend::open_by_ident(ident, None).map(Into::into)
} }
pub(crate) fn verify_to_user<'app, 'open>( pub(crate) fn verify_to_user<'app, 'open>(