From 930fb5d34aeb75b2daaceefd243738cbf6cd1fbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Thu, 23 Mar 2023 17:19:57 +0100 Subject: [PATCH] Add options to the authenticator --- examples/usbip.rs | 2 +- examples/virtual.rs | 4 +++- src/lib.rs | 25 ++++++++++++++++++++++++- tests/card/mod.rs | 4 ++-- tests/setup/mod.rs | 3 ++- 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/examples/usbip.rs b/examples/usbip.rs index 7fe20f9..fa62ce9 100644 --- a/examples/usbip.rs +++ b/examples/usbip.rs @@ -22,7 +22,7 @@ impl trussed_usbip::Apps>>, ()> _data: (), ) -> Self { PivApp { - piv: piv::Authenticator::new(make_client("piv")), + piv: piv::Authenticator::new(make_client("piv"), piv::Options::default()), } } diff --git a/examples/virtual.rs b/examples/virtual.rs index 4f20b27..8404d1c 100644 --- a/examples/virtual.rs +++ b/examples/virtual.rs @@ -11,11 +11,13 @@ // TODO: add CLI +use piv_authenticator::{Authenticator, Options}; + fn main() { env_logger::init(); trussed_rsa_alloc::virt::with_ram_client("piv-authenticator", |client| { - let card = piv_authenticator::Authenticator::new(client); + let card = Authenticator::new(client, Options::default()); let mut virtual_card = piv_authenticator::vpicc::VirtualCard::new(card); let vpicc = vpicc::connect().expect("failed to connect to vpicc"); vpicc diff --git a/src/lib.rs b/src/lib.rs index 96330e7..38aa7af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,17 +45,38 @@ pub type Result = iso7816::Result; use reply::Reply; use state::{AdministrationAlgorithm, CommandCache, KeyWithAlg, LoadedState, State, TouchPolicy}; +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Options { + storage: Location, +} + +impl Default for Options { + fn default() -> Self { + Self { + storage: Location::External, + } + } +} + +impl Options { + pub fn storage(self, storage: Location) -> Self { + Self { storage, ..self } + } +} + /// PIV authenticator Trussed app. /// /// The `C` parameter is necessary, as PIV includes command sequences, /// where we need to store the previous command, so we need to know how /// much space to allocate. pub struct Authenticator { + options: Options, state: State, trussed: T, } struct LoadedAuthenticator<'a, T> { + options: &'a mut Options, state: LoadedState<'a>, trussed: &'a mut T, } @@ -70,13 +91,14 @@ impl Authenticator where T: client::Client + client::Ed255 + client::Tdes, { - pub fn new(trussed: T) -> Self { + pub fn new(trussed: T, options: Options) -> Self { // seems like RefCell is not the right thing, we want something like `Rc` instead, // which can be cloned and injected into other parts of the App that use Trussed. // let trussed = RefCell::new(trussed); Self { // state: state::State::new(trussed.clone()), state: Default::default(), + options, trussed, } } @@ -85,6 +107,7 @@ where Ok(LoadedAuthenticator { state: self.state.load(&mut self.trussed)?, trussed: &mut self.trussed, + options: &mut self.options, }) } diff --git a/tests/card/mod.rs b/tests/card/mod.rs index de845ae..bdfd344 100644 --- a/tests/card/mod.rs +++ b/tests/card/mod.rs @@ -1,7 +1,7 @@ // Copyright (C) 2022 Nitrokey GmbH // SPDX-License-Identifier: LGPL-3.0-only -use piv_authenticator::{vpicc::VirtualCard, Authenticator}; +use piv_authenticator::{vpicc::VirtualCard, Authenticator, Options}; use std::{sync::mpsc, thread::sleep, time::Duration}; use stoppable_thread::spawn; @@ -18,7 +18,7 @@ pub fn with_vsc R, R>(f: F) -> R { let (tx, rx) = mpsc::channel(); let handle = spawn(move |stopped| { trussed_rsa_alloc::virt::with_ram_client("opcard", |client| { - let card = Authenticator::new(client); + let card = Authenticator::new(client, Options::default()); let mut virtual_card = VirtualCard::new(card); let mut result = Ok(()); while !stopped.get() && result.is_ok() { diff --git a/tests/setup/mod.rs b/tests/setup/mod.rs index d559ced..a355692 100644 --- a/tests/setup/mod.rs +++ b/tests/setup/mod.rs @@ -11,6 +11,7 @@ macro_rules! cmd { }; } +use piv_authenticator::{Authenticator, Options}; use trussed::virt::Ram; use trussed_rsa_alloc::virt::Client; @@ -18,7 +19,7 @@ pub type Piv = piv_authenticator::Authenticator>; pub fn piv(test: impl FnOnce(&mut Piv) -> R) -> R { trussed_rsa_alloc::virt::with_ram_client("test", |client| { - let mut piv_app = piv_authenticator::Authenticator::new(client); + let mut piv_app = Authenticator::new(client, Options::default()); test(&mut piv_app) }) }