From b7a9ecd1857c79698f47d3ef088bc1f11527ca4c Mon Sep 17 00:00:00 2001 From: Nicolas Stalder Date: Thu, 10 Jun 2021 18:17:17 +0200 Subject: [PATCH] Use iso7816::Aid --- Cargo.toml | 3 ++- src/app.rs | 33 +-------------------------------- src/dispatch.rs | 22 +++++++++++++--------- 3 files changed, 16 insertions(+), 42 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8429dd5..7a8403b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,8 @@ std = ["delog/std", "serial_test"] log-all = [] log-none = [] -log-info = [] log-debug = [] +log-info = [] +log-trace = [] log-warn = [] log-error = [] diff --git a/src/app.rs b/src/app.rs index c704ad1..517f718 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3,39 +3,8 @@ pub type Result = iso7816::Result<()>; pub use crate::{ArrayLength, dispatch::Interface}; -/// The Aid is used to determine whether or not the App will be selected. -/// Only `aid()` and `right_truncated_length()` need to be implemented. -pub trait Aid { - - fn aid(&self) -> &'static [u8]; - - fn right_truncated_length(&self) -> usize; - - fn len(&self) -> usize { - self.aid().len() - } - - fn full(&self) -> &'static [u8] { - self.aid() - } - - fn right_truncated(&self) -> &'static [u8] { - &self.aid()[..self.right_truncated_length()] - } - - fn pix(&self) -> &'static [u8] { - &self.aid()[5..] - } - - fn rid(&self) -> &'static [u8] { - &self.aid()[..5] - } -} - - - /// An App can receive and respond APDUs at behest of the ApduDispatch. -pub trait App, R: ArrayLength>: Aid { +pub trait App, R: ArrayLength>: iso7816::App { /// Given parsed APDU for select command. /// Write response data back to buf, and return length of payload. Return APDU Error code on error. /// Alternatively, the app can defer the response until later by returning it in `poll()`. diff --git a/src/dispatch.rs b/src/dispatch.rs index 4d0f6ab..22083da 100644 --- a/src/dispatch.rs +++ b/src/dispatch.rs @@ -83,7 +83,8 @@ impl ApduDispatch { fn apdu_type(apdu: &iso7816::Command>) -> RequestType { if apdu.instruction() == Instruction::Select && (apdu.p1 & 0x04) != 0 { - RequestType::Select(Aid::try_from_slice(apdu.data()).unwrap()) + // RequestType::Select(Aid::try_from_slice(apdu.data()).unwrap()) + RequestType::Select(Aid::new(apdu.data(), apdu.data().len())) } else if apdu.instruction() == Instruction::GetResponse { RequestType::GetResponse } else { @@ -118,11 +119,14 @@ impl ApduDispatch // Some(aid) => apps.iter_mut().find(|app| aid.starts_with(app.rid())), // None => None, // } - aid.and_then(move |aid| - apps.iter_mut().find(|app| - aid.starts_with(app.aid()) - ) - ) + aid.and_then(move |aid| { + debug_now!("matching {:?}", aid); + apps.iter_mut().find(|app| { + // aid.starts_with(app.aid().truncated()) + debug_now!("...against {:?}", app.aid()); + app.aid().matches(aid) + } ) + }) } fn busy(&self) -> bool { @@ -354,8 +358,8 @@ impl ApduDispatch // not necessarily the case for other apps // if there is a selected app with a different AID, deselect it - if let Some(current_aid) = self.current_aid.as_ref() { - if *current_aid != *aid { + if let Some(current_aid) = self.current_aid { + if current_aid != aid { let app = Self::find_app(self.current_aid.as_ref(), apps).unwrap(); // for now all apps will be happy with this. app.deselect(); @@ -381,7 +385,7 @@ impl ApduDispatch } else { - info!("could not find app by aid: {}", hex_str!(&aid)); + info!("could not find app by aid: {}", hex_str!(&aid.as_bytes())); self.reply_error(Status::NotFound); };