Implement From<CardClientBox>, Into<CardClientBox> for CardApp and adjust client code.

This commit is contained in:
Heiko Schaefer 2021-09-01 22:26:25 +02:00
parent 88c924c7d9
commit f8d998b3a6
6 changed files with 30 additions and 43 deletions

View file

@ -98,7 +98,7 @@ impl TestCard {
log::trace!(" Attempt to shutdown scd: {:?}", res);
for card_client in PcscClient::list_cards()? {
let mut ca = CardApp::new(card_client);
let mut ca = CardApp::from(card_client);
// Set Card Capabilities (chaining, command length, ..)
let ard = ca.get_app_data()?;
@ -117,7 +117,7 @@ impl TestCard {
}
Self::Scdc(serial) => {
let card_client = ScdClient::open_by_serial(None, serial)?;
let mut ca = CardApp::new(card_client);
let mut ca = CardApp::from(card_client);
// Set Card Capabilities (chaining, command length, ..)
let ard = ca.get_app_data()?;

View file

@ -582,7 +582,7 @@ impl CardBase {
/// CardClient, on which the openpgp applet has already been opened.
pub fn open_card(ccb: CardClientBox) -> Result<Self, OpenpgpCardError> {
// read and cache "application related data"
let mut card_app = CardApp::new(ccb);
let mut card_app = CardApp::from(ccb);
let ard = card_app.get_app_data()?;
@ -788,15 +788,6 @@ impl CardUser {
) -> Result<Vec<u8>, OpenpgpCardError> {
self.card_app.decrypt(dm)
}
/// Run decryption operation on the smartcard
/// (7.2.11 PSO: DECIPHER)
pub(crate) fn pso_decipher(
&mut self,
data: Vec<u8>,
) -> Result<Vec<u8>, OpenpgpCardError> {
self.card_app.pso_decipher(data)
}
}
/// An OpenPGP card after successful verification of PW1 in mode 81
@ -831,15 +822,6 @@ impl CardSign {
) -> Result<Vec<u8>, OpenpgpCardError> {
self.card_app.signature_for_hash(hash)
}
/// Run signing operation on the smartcard
/// (7.2.10 PSO: COMPUTE DIGITAL SIGNATURE)
pub(crate) fn compute_digital_signature(
&mut self,
data: Vec<u8>,
) -> Result<Vec<u8>, OpenpgpCardError> {
self.card_app.pso_compute_digital_signature(data)
}
}
/// An OpenPGP card after successful verification of PW3 ("Admin privileges")

View file

@ -240,7 +240,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let cards = PcscClient::list_cards()?;
for c in cards {
let mut ca = CardApp::new(c);
let mut ca = CardApp::from(c);
let ard = ca.get_app_data()?;
let app_id = ard.get_application_id()?;

View file

@ -33,21 +33,28 @@ pub struct CardApp {
card_client: CardClientBox,
}
impl CardApp {
/// Create a CardApp object based on a [`CardClientBox`].
pub fn new(card_client: CardClientBox) -> Self {
impl From<CardClientBox> for CardApp {
fn from(card_client: CardClientBox) -> Self {
Self { card_client }
}
/// Take the CardClientBox out of a CardApp
pub fn take_card(self) -> CardClientBox {
self.card_client
}
/// Read capabilities from the card, and set them in the CardApp.
impl Into<CardClientBox> for CardApp {
fn into(self) -> CardClientBox {
self.card_client
}
}
impl CardApp {
/// Get the CardClient for this CardApp
pub(crate) fn get_card_client(&mut self) -> &mut CardClientBox {
&mut self.card_client
}
/// Initialize the CardCaps settings in the underlying CardClient
/// from the data in `ard`.
///
/// Also initializes the underlying CardClient with the caps - some
/// implementations may need this information.
/// This should be done at an early point, soon after opening the card.
pub fn init_caps(&mut self, ard: &ApplicationRelatedData) -> Result<()> {
// Determine chaining/extended length support from card
// metadata and cache this information in CardApp (as a
@ -82,10 +89,6 @@ impl CardApp {
Ok(())
}
pub fn card(&mut self) -> &mut CardClientBox {
&mut self.card_client
}
// --- select ---
/// "Select" the OpenPGP card application
@ -552,7 +555,7 @@ impl CardApp {
fp.as_bytes().to_vec(),
);
apdu::send_command(self.card(), fp_cmd, false)?.try_into()
apdu::send_command(self.get_card_client(), fp_cmd, false)?.try_into()
}
/// Set PW Status Bytes.
@ -574,7 +577,7 @@ impl CardApp {
let data = pw_status.serialize_for_put(long);
let cmd = commands::put_pw_status(data);
apdu::send_command(self.card(), cmd, false)?.try_into()
apdu::send_command(self.get_card_client(), cmd, false)?.try_into()
}
/// Set cardholder certificate (for AUT, DEC or SIG).

View file

@ -117,7 +117,7 @@ pub(crate) fn generate_asymmetric_key_pair(
let crt = get_crt(key_type)?;
let gen_key_cmd = commands::gen_key(crt.serialize().to_vec());
let card_client = card_app.card();
let card_client = card_app.get_card_client();
let resp = apdu::send_command(card_client, gen_key_cmd, true)?;
resp.check_ok()?;
@ -145,7 +145,8 @@ pub(crate) fn get_pub_key(
let crt = get_crt(key_type)?;
let get_pub_key_cmd = commands::get_pub_key(crt.serialize().to_vec());
let resp = apdu::send_command(card_app.card(), get_pub_key_cmd, true)?;
let resp =
apdu::send_command(card_app.get_card_client(), get_pub_key_cmd, true)?;
resp.check_ok()?;
let tlv = Tlv::try_from(resp.data()?)?;
@ -248,7 +249,8 @@ pub(crate) fn key_import(
// Send all the commands
card_app.set_algorithm_attributes(key_type, &algo)?;
apdu::send_command(card_app.card(), key_cmd, false)?.check_ok()?;
apdu::send_command(card_app.get_card_client(), key_cmd, false)?
.check_ok()?;
card_app.set_fingerprint(fp, key_type)?;
card_app.set_creation_time(ts, key_type)?;

View file

@ -87,7 +87,7 @@ impl PcscClient {
.map(Self::select)
.map(|res| res.ok())
.flatten()
.map(|ca| ca.take_card())
.map(|ca| ca.into())
.collect();
Ok(cards)
@ -97,7 +97,7 @@ impl PcscClient {
fn select(card_client: PcscClient) -> Result<CardApp, OpenpgpCardError> {
let ccb = Box::new(card_client) as CardClientBox;
let mut ca = CardApp::new(ccb);
let mut ca = CardApp::from(ccb);
if ca.select().is_ok() {
Ok(ca)
} else {
@ -114,7 +114,7 @@ impl PcscClient {
pub fn open_yolo() -> Result<CardClientBox, OpenpgpCardError> {
for card in Self::unopened_cards()? {
if let Ok(ca) = Self::select(card) {
return Ok(ca.take_card());
return Ok(ca.into());
}
}
@ -133,7 +133,7 @@ impl PcscClient {
let aid = ard.get_application_id()?;
if aid.ident() == ident {
Ok(Some(ca.take_card()))
Ok(Some(ca.into()))
} else {
Ok(None)
}