Refactoring low level API for card-functionality tests

This commit is contained in:
Heiko Schaefer 2021-07-13 00:08:45 +02:00
parent 89745c0268
commit 31e577c896
3 changed files with 26 additions and 4 deletions

View file

@ -11,6 +11,8 @@ use std::convert::TryFrom;
use crate::apdu::command::Command; use crate::apdu::command::Command;
use crate::apdu::response::Response; use crate::apdu::response::Response;
use crate::card;
use crate::card_app::CardApp;
use crate::errors::{OcErrorStatus, OpenpgpCardError, SmartcardError}; use crate::errors::{OcErrorStatus, OpenpgpCardError, SmartcardError};
use crate::{CardBase, CardCaps, CardClient, CardClientBox}; use crate::{CardBase, CardCaps, CardClient, CardClientBox};
@ -188,6 +190,14 @@ impl PcscClient {
Self { card } Self { card }
} }
pub fn list_cards() -> Result<Vec<PcscClient>> {
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. /// Take a PCSC Card object and try to open the OpenPGP card applet.
/// If successful, wrap and return the resulting CardClient as a /// If successful, wrap and return the resulting CardClient as a
/// CardBase object (which involves caching the "application related /// CardBase object (which involves caching the "application related
@ -196,11 +206,11 @@ impl PcscClient {
let card_client = PcscClient::new(card); let card_client = PcscClient::new(card);
let mut ccb = Box::new(card_client) as CardClientBox; let mut ccb = Box::new(card_client) as CardClientBox;
let select_openpgp = commands::select_openpgp(); let mut ca = CardApp::new(ccb);
let resp = send_command(&mut ccb, select_openpgp, false, None)?; let resp = ca.select()?;
if resp.is_ok() { if resp.is_ok() {
CardBase::open_card(ccb) CardBase::open_card(ca.take_card())
} else { } else {
Err(anyhow!("Couldn't open OpenPGP application").into()) Err(anyhow!("Couldn't open OpenPGP application").into())
} }

View file

@ -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 { pub fn set_caps(self, card_caps: CardCaps) -> Self {
Self { Self {
card_client: self.card_client, card_client: self.card_client,
@ -56,6 +60,14 @@ impl CardApp {
self.card_caps.as_ref() self.card_caps.as_ref()
} }
// --- select ---
/// "Select" the OpenPGP card application
pub fn select(&mut self) -> Result<Response, OpenpgpCardError> {
let select_openpgp = commands::select_openpgp();
apdu::send_command(&mut self.card_client, select_openpgp, false, None)
}
// --- application data --- // --- application data ---
/// Load "application related data". /// Load "application related data".

View file

@ -18,7 +18,7 @@ use crate::card_app::CardApp;
use crate::errors::{OpenpgpCardError, SmartcardError}; use crate::errors::{OpenpgpCardError, SmartcardError};
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
mod apdu; pub mod apdu;
mod card; mod card;
pub mod card_app; pub mod card_app;
pub mod errors; pub mod errors;