Adjust the backend API and interaction with openpgp-card some more.
This commit is contained in:
parent
7a71f88eb6
commit
90ae9398ed
5 changed files with 24 additions and 30 deletions
|
@ -112,15 +112,8 @@ impl TestCard {
|
|||
Err(anyhow!("Pcsc card {} not found", ident))
|
||||
}
|
||||
Self::Scdc(serial) => {
|
||||
let mut ca = ScdClient::open_by_serial(None, serial)?;
|
||||
|
||||
// Set Card Capabilities (chaining, command length, ..)
|
||||
let ard = ca.get_application_related_data()?;
|
||||
ca.init_caps(&ard)?;
|
||||
|
||||
// println!("opened scdc card {}", serial);
|
||||
|
||||
Ok(ca)
|
||||
// println!("open scdc card {}", serial);
|
||||
Ok(ScdClient::open_by_serial(None, serial)?)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,8 +50,6 @@ impl<'a> Open<'a> {
|
|||
pub fn new(card_app: &'a mut CardApp) -> Result<Self, Error> {
|
||||
let ard = card_app.get_application_related_data()?;
|
||||
|
||||
card_app.init_caps(&ard)?;
|
||||
|
||||
Ok(Self {
|
||||
card_app,
|
||||
ard,
|
||||
|
|
|
@ -35,11 +35,12 @@ pub struct CardApp {
|
|||
impl CardApp {
|
||||
/// Get a CardApp based on a CardClient.
|
||||
///
|
||||
/// It is expected that SELECT has already been performed on the card.
|
||||
/// It is expected that SELECT has already been performed on the card
|
||||
/// beforehand.
|
||||
///
|
||||
/// This fn calls CardClient::init_caps(). It should probably only be used
|
||||
/// by backend implementations, not by user code. User Code should get
|
||||
/// a fully initialized CardApp from their backend implementation.
|
||||
/// This fn initializes the CardCaps by requesting
|
||||
/// application_related_data from the card, and setting the
|
||||
/// capabilities accordingly.
|
||||
pub fn initialize(card_client: CardClientBox) -> Result<Self> {
|
||||
let mut ca = Self { card_client };
|
||||
|
||||
|
@ -49,7 +50,7 @@ impl CardApp {
|
|||
Ok(ca)
|
||||
}
|
||||
|
||||
/// Get the CardClient for this CardApp
|
||||
/// Get the CardClient of this CardApp
|
||||
pub(crate) fn card_client(&mut self) -> &mut dyn CardClient {
|
||||
&mut *self.card_client
|
||||
}
|
||||
|
@ -58,7 +59,7 @@ impl CardApp {
|
|||
/// from the data in `ard`.
|
||||
///
|
||||
/// This should be done at an early point, soon after opening the card.
|
||||
pub fn init_caps(&mut self, ard: &ApplicationRelatedData) -> Result<()> {
|
||||
fn init_caps(&mut self, ard: &ApplicationRelatedData) -> Result<()> {
|
||||
// Determine chaining/extended length support from card
|
||||
// metadata and cache this information in CardApp (as a
|
||||
// CardCaps)
|
||||
|
|
|
@ -86,8 +86,8 @@ impl PcscClient {
|
|||
|
||||
/// Return all cards on which the OpenPGP application could be selected.
|
||||
///
|
||||
/// Each card is opened and has the OpenPGP application selected.
|
||||
/// Cards are initialized via init_caps().
|
||||
/// Each card has the OpenPGP application selected, CardCaps have been
|
||||
/// initialized.
|
||||
pub fn cards() -> Result<Vec<CardApp>> {
|
||||
let mut cards = vec![];
|
||||
|
||||
|
@ -112,18 +112,19 @@ impl PcscClient {
|
|||
}
|
||||
|
||||
/// Returns the OpenPGP card that matches `ident`, if it is available.
|
||||
/// A fully initialized CardApp is returned: application has been
|
||||
/// selected, init_caps() has been performed.
|
||||
/// A fully initialized CardApp is returned: the OpenPGP application has
|
||||
/// been selected, CardCaps have been set.
|
||||
pub fn open_by_ident(ident: &str) -> Result<CardApp, Error> {
|
||||
for mut card in Self::unopened_cards()? {
|
||||
Self::select(&mut card)?;
|
||||
let mut ca = card.into_card_app()?;
|
||||
if Self::select(&mut card).is_ok() {
|
||||
let mut ca = card.into_card_app()?;
|
||||
|
||||
let ard = ca.get_application_related_data()?;
|
||||
let aid = ard.get_application_id()?;
|
||||
let ard = ca.get_application_related_data()?;
|
||||
let aid = ard.get_application_id()?;
|
||||
|
||||
if aid.ident() == ident {
|
||||
return Ok(ca);
|
||||
if aid.ident() == ident {
|
||||
return Ok(ca);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ use std::sync::Mutex;
|
|||
use tokio::runtime::Runtime;
|
||||
|
||||
use openpgp_card::{CardApp, Error};
|
||||
use openpgp_card::{CardCaps, CardClient, CardClientBox};
|
||||
use openpgp_card::{CardCaps, CardClient};
|
||||
|
||||
lazy_static! {
|
||||
static ref RT: Mutex<Runtime> =
|
||||
|
@ -108,9 +108,10 @@ impl ScdClient {
|
|||
/// Create a CardClientBox object that uses an scdaemon instance as its
|
||||
/// backend. If multiple cards are available, scdaemon implicitly
|
||||
/// selects one.
|
||||
pub fn open(agent: Option<Agent>) -> Result<CardClientBox, Error> {
|
||||
pub fn open(agent: Option<Agent>) -> Result<CardApp, Error> {
|
||||
let card = ScdClient::new(agent, true)?;
|
||||
Ok(Box::new(card) as CardClientBox)
|
||||
|
||||
Ok(CardApp::initialize(Box::new(card))?)
|
||||
}
|
||||
|
||||
/// Create a CardClientBox object that uses an scdaemon instance as its
|
||||
|
|
Loading…
Reference in a new issue