This hopefully resolves #325 but I'm unable to test it because
the U2F demo site I use for testing seems to have been disabled.
This commit is contained in:
bunnie 2023-05-22 18:36:22 +08:00
parent 2aa51736f5
commit 6e16ef163f

View file

@ -13,6 +13,7 @@ use std::sync::atomic::AtomicBool;
use std::time::{Duration, Instant};
use persistent_store::Store;
use ctap_crypto::rng256::XousRng256;
use xous::try_send_message;
use xous_names::XousNames;
use crate::env::xous::storage::XousUpgradeStorage;
use usbd_human_interface_device::device::fido::*;
@ -83,6 +84,14 @@ struct Ctap1Request {
enum Ctap1Op {
PollPermission,
UpdateAppInfo,
ForceTimeout,
Invalid,
}
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
enum Ctap1TimeoutOp {
Run,
Pump,
Stop,
Invalid,
}
pub struct XousEnv {
@ -108,6 +117,65 @@ impl XousEnv {
let ctap1_sid = xous::create_server().unwrap();
let ctap1_cid = xous::connect(ctap1_sid).unwrap();
let ctap1_timeout_sid = xous::create_server().unwrap();
let ctap1_timeout_cid = xous::connect(ctap1_timeout_sid).unwrap();
std::thread::spawn({
const MARGIN_MS: u128 = 2000; // auto-clears the box 2 seconds after the timeout deadline.
// some margin is desired because this is basically a huge race condition.
move || {
let tt = ticktimer_server::Ticktimer::new().unwrap();
let mut msg_opt = None;
let mut _return_type = 0;
let ctap1_timeout_cid = ctap1_timeout_cid.clone();
let ctap1_cid = ctap1_cid.clone();
let mut start_time = Instant::now();
let mut run = false;
loop {
xous::reply_and_receive_next_legacy(ctap1_timeout_sid, &mut msg_opt, &mut _return_type)
.unwrap();
let msg = msg_opt.as_mut().unwrap();
log::debug!("msg: {:x?}", msg);
match num_traits::FromPrimitive::from_usize(msg.body.id())
.unwrap_or(Ctap1TimeoutOp::Invalid)
{
Ctap1TimeoutOp::Run => {
if !run { // only kick off the loop on the first request to run
start_time = Instant::now();
try_send_message(ctap1_timeout_cid, xous::Message::new_scalar(
Ctap1TimeoutOp::Pump.to_usize().unwrap(), 0, 0, 0, 0)).ok();
}
run = true;
},
Ctap1TimeoutOp::Pump => {
let elapsed = Instant::now().duration_since(start_time);
if run {
if elapsed.as_millis() > crate::ctap::U2F_UP_PROMPT_TIMEOUT.as_millis() + MARGIN_MS {
run = false;
// timed out, force the dialog box to close
try_send_message(ctap1_cid, xous::Message::new_scalar(
Ctap1Op::ForceTimeout.to_usize().unwrap(), 0, 0, 0, 0)).ok();
// no pump message, so the thread should stop running.
} else {
// no timeout, keep pinging
tt.sleep_ms(1000).unwrap();
try_send_message(ctap1_timeout_cid, xous::Message::new_scalar(
Ctap1TimeoutOp::Pump.to_usize().unwrap(), 0, 0, 0, 0)).ok();
}
}
},
Ctap1TimeoutOp::Stop => {
// this can come asynchronously from the ctap1 thread once the box is ack'd.
run = false;
// this will stop the pump from running, and the thread will block at waiting for incoming messages
},
Ctap1TimeoutOp::Invalid => {
log::error!("invalid opcode received: {:?}", msg);
}
}
}
}
});
std::thread::spawn({
let main_cid = conn.clone();
let lefty_mode = lefty_mode.clone();
@ -146,6 +214,8 @@ impl XousEnv {
if *id != request.app_id {
log::error!("Request ID changed while request is in progress. Ignoring request");
buf.replace(request).ok();
xous::send_message(ctap1_timeout_cid, xous::Message::new_scalar(
Ctap1TimeoutOp::Stop.to_usize().unwrap(), 0, 0, 0, 0)).ok();
modals.dynamic_notification_close().ok();
current_id = None;
continue;
@ -158,6 +228,8 @@ impl XousEnv {
log::trace!("approved");
request.approved = true;
current_id = None;
xous::send_message(ctap1_timeout_cid, xous::Message::new_scalar(
Ctap1TimeoutOp::Stop.to_usize().unwrap(), 0, 0, 0, 0)).ok();
modals.dynamic_notification_close().ok();
} else if
(!lefty_mode.load(Ordering::SeqCst) && (key == 0x11))
@ -165,6 +237,8 @@ impl XousEnv {
log::trace!("denied");
request.approved = false;
denied_id = current_id.take();
xous::send_message(ctap1_timeout_cid, xous::Message::new_scalar(
Ctap1TimeoutOp::Stop.to_usize().unwrap(), 0, 0, 0, 0)).ok();
modals.dynamic_notification_close().ok();
} else {
log::trace!("waiting");
@ -198,6 +272,8 @@ impl XousEnv {
current_id = None;
denied_id = None;
request.approved = false;
xous::send_message(ctap1_timeout_cid, xous::Message::new_scalar(
Ctap1TimeoutOp::Stop.to_usize().unwrap(), 0, 0, 0, 0)).ok();
modals.dynamic_notification_close().ok();
}
}
@ -314,6 +390,10 @@ impl XousEnv {
}
});
current_id = Some(request.app_id);
// start the timeout watchdog, because if another token responds to the request,
// we will be left hanging.
xous::try_send_message(ctap1_timeout_cid, xous::Message::new_scalar(
Ctap1TimeoutOp::Run.to_usize().unwrap(), 0, 0, 0, 0)).ok();
denied_id = None; // reset the denial timers, too
request.approved = false;
buf.replace(request).ok();
@ -415,6 +495,12 @@ impl XousEnv {
0, 0, 0, 0)
).unwrap();
}
Ctap1Op::ForceTimeout => {
log::info!("polling timed out");
current_id.take(); // clear out the ID token
// close the dynamic notification box
modals.dynamic_notification_close().ok();
}
Ctap1Op::Invalid => {
log::error!("got invalid opcode: {}, ignoring", msg.body.id());
}