Move protocol-specific user presence checking code from Env to CTAP library (#501)
* Common duration type for ctap library independent of TockOS * Implement Env-specific ctap-hid channels for I/O Common I/O Status, Error and Result types * Move common user presence checking code to ctap library * Move CtapHidChannel and UserPresence traits, with their accompanying types to separate API mods. Remove Default implementations of methods in these traits, to keep all implementation details inside of concrete Env types. Rename methods in UserPresence trait, for better readability. Remove duplicate code for finding appropriate HID channel for given transport. Rework check_user_presence() function so that there's no more need for quick_check() method in UserPresence trait. To short-circuit user presence check, Env implementation may use wait_with_timeout() method. * Fix button press wait with zero timeout for TockEnv * Fix formatting * Remove type for duration, use embedded_time::duration::Milliseconds directly, for better readability. Treat any unconfirmed result of user presence check as an error, which maps more naturally to CTAP spec status codes. Remove unneeded underscores in trait definition. Store usb endpoint directly, in TockEnv channels, to avoid unneeded conversions. * No need for separate error type for send_keepalive_up_needed() * Document UserPresence trait and types. Remove unused parameters in UserPresence trait's methods. Add conversion function from UserPresence errors to Ctap2 status codes. Do not check button status when tock user presence wait is called with zero timeout. * Make test environment always report success sending data * Rename CtapHidChannel to HidConnection, rename *_hid_channel -> *_hid_connection, for clarity. Use "Channel" to refer to the logical connection from authenticator to one client, and use "Connection" to refer to physical connection of authenticator to platform, on which clients run. Remove channel parameter from user presence API, it's not needed. * Remove duplicate comments. Co-authored-by: kaczmarczyck <43844792+kaczmarczyck@users.noreply.github.com>
This commit is contained in:
41
src/env/test/mod.rs
vendored
41
src/env/test/mod.rs
vendored
@@ -1,10 +1,12 @@
|
||||
use self::upgrade_storage::BufferUpgradeStorage;
|
||||
use crate::api::connection::{HidConnection, SendOrRecvResult, SendOrRecvStatus};
|
||||
use crate::api::customization::DEFAULT_CUSTOMIZATION;
|
||||
use crate::api::firmware_protection::FirmwareProtection;
|
||||
use crate::ctap::status_code::Ctap2StatusCode;
|
||||
use crate::ctap::Channel;
|
||||
use crate::env::{Env, UserPresence};
|
||||
use crate::api::user_presence::{UserPresence, UserPresenceResult};
|
||||
use crate::clock::ClockInt;
|
||||
use crate::env::Env;
|
||||
use customization::TestCustomization;
|
||||
use embedded_time::duration::Milliseconds;
|
||||
use persistent_store::{BufferOptions, BufferStorage, Store};
|
||||
use rand::rngs::StdRng;
|
||||
use rand::{Rng, SeedableRng};
|
||||
@@ -40,7 +42,7 @@ impl Rng256 for TestRng256 {
|
||||
}
|
||||
|
||||
pub struct TestUserPresence {
|
||||
check: Box<dyn Fn(Channel) -> Result<(), Ctap2StatusCode>>,
|
||||
check: Box<dyn Fn() -> UserPresenceResult>,
|
||||
}
|
||||
|
||||
pub struct TestWrite;
|
||||
@@ -66,13 +68,24 @@ fn new_storage() -> BufferStorage {
|
||||
BufferStorage::new(store, options)
|
||||
}
|
||||
|
||||
impl HidConnection for TestEnv {
|
||||
fn send_or_recv_with_timeout(
|
||||
&mut self,
|
||||
_buf: &mut [u8; 64],
|
||||
_timeout: Milliseconds<ClockInt>,
|
||||
) -> SendOrRecvResult {
|
||||
// TODO: Implement I/O from canned requests/responses for integration testing.
|
||||
Ok(SendOrRecvStatus::Sent)
|
||||
}
|
||||
}
|
||||
|
||||
impl TestEnv {
|
||||
pub fn new() -> Self {
|
||||
let rng = TestRng256 {
|
||||
rng: StdRng::seed_from_u64(0),
|
||||
};
|
||||
let user_presence = TestUserPresence {
|
||||
check: Box::new(|_| Ok(())),
|
||||
check: Box::new(|| Ok(())),
|
||||
};
|
||||
let storage = new_storage();
|
||||
let store = Store::new(storage).ok().unwrap();
|
||||
@@ -101,15 +114,17 @@ impl TestEnv {
|
||||
}
|
||||
|
||||
impl TestUserPresence {
|
||||
pub fn set(&mut self, check: impl Fn(Channel) -> Result<(), Ctap2StatusCode> + 'static) {
|
||||
pub fn set(&mut self, check: impl Fn() -> UserPresenceResult + 'static) {
|
||||
self.check = Box::new(check);
|
||||
}
|
||||
}
|
||||
|
||||
impl UserPresence for TestUserPresence {
|
||||
fn check(&mut self, channel: Channel) -> Result<(), Ctap2StatusCode> {
|
||||
(self.check)(channel)
|
||||
fn check_init(&mut self) {}
|
||||
fn wait_with_timeout(&mut self, _timeout: Milliseconds<ClockInt>) -> UserPresenceResult {
|
||||
(self.check)()
|
||||
}
|
||||
fn check_complete(&mut self) {}
|
||||
}
|
||||
|
||||
impl FirmwareProtection for TestEnv {
|
||||
@@ -126,6 +141,7 @@ impl Env for TestEnv {
|
||||
type FirmwareProtection = Self;
|
||||
type Write = TestWrite;
|
||||
type Customization = TestCustomization;
|
||||
type HidConnection = Self;
|
||||
|
||||
fn rng(&mut self) -> &mut Self::Rng {
|
||||
&mut self.rng
|
||||
@@ -154,4 +170,13 @@ impl Env for TestEnv {
|
||||
fn customization(&self) -> &Self::Customization {
|
||||
&self.customization
|
||||
}
|
||||
|
||||
fn main_hid_connection(&mut self) -> &mut Self::HidConnection {
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "vendor_hid")]
|
||||
fn vendor_hid_connection(&mut self) -> &mut Self::HidConnection {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user