add a compatibility gate on USB HID interface

If the SoC isn't compatible, don't try starting the U2F server.
This commit is contained in:
bunnie 2022-06-27 02:00:22 +08:00
parent 9cf48fcaa5
commit 0e5fb14894
4 changed files with 60 additions and 28 deletions

View file

@ -192,41 +192,46 @@ fn main() -> ! {
let mut rng = ctap_crypto::rng256::XousRng256::new(&xns); let mut rng = ctap_crypto::rng256::XousRng256::new(&xns);
// this call will block until the PDDB is mounted. // this call will block until the PDDB is mounted.
let usb = usb_device_xous::UsbHid::new(); let usb = usb_device_xous::UsbHid::new();
let mut ctap_state = CtapState::new(&mut rng, check_user_presence, boot_time); // only run the main loop if the SoC is compatible
let mut ctap_hid = CtapHid::new(); if usb.is_soc_compatible() {
loop { let mut ctap_state = CtapState::new(&mut rng, check_user_presence, boot_time);
match usb.u2f_wait_incoming() { let mut ctap_hid = CtapHid::new();
Ok(msg) => { loop {
log::trace!("FIDO listener got message: {:?}", msg); match usb.u2f_wait_incoming() {
let now = ClockValue::new(tt.elapsed_ms() as i64, 1000); Ok(msg) => {
let reply = ctap_hid.process_hid_packet(&msg.packet, now, &mut ctap_state); log::trace!("FIDO listener got message: {:?}", msg);
// This block handles sending packets. let now = ClockValue::new(tt.elapsed_ms() as i64, 1000);
for pkt_reply in reply { let reply = ctap_hid.process_hid_packet(&msg.packet, now, &mut ctap_state);
let mut reply = RawFidoMsg::default(); // This block handles sending packets.
reply.packet.copy_from_slice(&pkt_reply); for pkt_reply in reply {
let status = usb.u2f_send(reply); let mut reply = RawFidoMsg::default();
match status { reply.packet.copy_from_slice(&pkt_reply);
Ok(()) => { let status = usb.u2f_send(reply);
log::trace!("Sent U2F packet"); match status {
} Ok(()) => {
Err(e) => { log::trace!("Sent U2F packet");
log::error!("Error sending U2F packet: {:?}", e); }
Err(e) => {
log::error!("Error sending U2F packet: {:?}", e);
}
} }
} }
} }
} Err(e) => {
Err(e) => { match e {
match e { xous::Error::ProcessTerminated => { // unplug happened, reset the authenticator
xous::Error::ProcessTerminated => { // unplug happened, reset the authenticator log::info!("CTAP unplug_reset");
log::info!("CTAP unplug_reset"); ctap_state.unplug_reset();
ctap_state.unplug_reset(); },
}, _ => {
_ => { log::warn!("FIDO listener got an error: {:?}", e);
log::warn!("FIDO listener got an error: {:?}", e); }
} }
} }
} }
} }
} else {
log::warn!("SoC rev is incompatible with USB HID operations, the U2F/FIDO2 server is not started");
} }
} }
}); });

View file

@ -28,6 +28,9 @@ pub(crate) enum Opcode {
/// Blocks the caller, waiting for a U2F message /// Blocks the caller, waiting for a U2F message
U2fRxDeferred, U2fRxDeferred,
/// Query if the HID driver was able to start
IsSocCompatible,
/// Handle the USB interrupt /// Handle the USB interrupt
UsbIrqHandler, UsbIrqHandler,
/// Suspend/resume callback /// Suspend/resume callback

View file

@ -29,6 +29,24 @@ impl UsbHid {
conn conn
} }
} }
/// used to query if the HID core was able to start. Mainly to handle edge cases between updates.
pub fn is_soc_compatible(&self) -> bool {
match send_message(
self.conn,
Message::new_blocking_scalar(
Opcode::IsSocCompatible.to_usize().unwrap(),
0, 0, 0, 0
)
) {
Ok(xous::Result::Scalar1(code)) => {
match code {
0 => false,
_ => true
}
}
_ => panic!("Internal error: illegal return type"),
}
}
/// this will always trigger a reset, even if it's the same core we're switching to /// this will always trigger a reset, even if it's the same core we're switching to
pub fn switch_to_core(&self, core: UsbDeviceType) -> Result<(), xous::Error> { pub fn switch_to_core(&self, core: UsbDeviceType) -> Result<(), xous::Error> {
match send_message( match send_message(

View file

@ -112,6 +112,9 @@ fn main() -> ! {
// block any rx requests forever // block any rx requests forever
fido_listener = Some(msg); fido_listener = Some(msg);
} }
Some(Opcode::IsSocCompatible) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
xous::return_scalar(msg.sender, 0).expect("couldn't return compatibility status")
}),
Some(Opcode::Quit) => { Some(Opcode::Quit) => {
break; break;
} }
@ -200,6 +203,9 @@ fn main() -> ! {
usbmgmt.xous_resume(); usbmgmt.xous_resume();
lockstatus_force_update = true; // notify the status bar that yes, it does need to redraw the lock status, even if the value hasn't changed since the last read lockstatus_force_update = true; // notify the status bar that yes, it does need to redraw the lock status, even if the value hasn't changed since the last read
}), }),
Some(Opcode::IsSocCompatible) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
xous::return_scalar(msg.sender, 1).expect("couldn't return compatibility status")
}),
Some(Opcode::U2fRxDeferred) => { Some(Opcode::U2fRxDeferred) => {
if fido_listener_pid.is_none() { if fido_listener_pid.is_none() {
fido_listener_pid = msg.sender.pid(); fido_listener_pid = msg.sender.pid();