From 5f859e57fbe3e00fe854018c5ff40b7fc1f7017d Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 12 Nov 2025 18:24:23 +0800 Subject: [PATCH] cleanup warnings --- apps-baosec/vault2/Cargo.toml | 2 +- apps-baosec/vault2/src/actions.rs | 22 +-- apps-baosec/vault2/src/main.rs | 194 ++++++++++++++++++---- apps-baosec/vault2/src/storage.rs | 5 +- apps-baosec/vault2/src/ux.rs | 9 +- apps-baosec/vault2/src/vault_api.rs | 27 +-- apps-baosec/vault2/src/vendor_commands.rs | 4 +- 7 files changed, 182 insertions(+), 81 deletions(-) diff --git a/apps-baosec/vault2/Cargo.toml b/apps-baosec/vault2/Cargo.toml index 01ae7c96a..b8d1ead30 100644 --- a/apps-baosec/vault2/Cargo.toml +++ b/apps-baosec/vault2/Cargo.toml @@ -18,7 +18,7 @@ xous-names = { package = "xous-api-names", version = "0.9.67" } locales = { path = "../../locales" } pddb = { path = "../../services/pddb" } modals = { path = "../../services/modals", features = ["no-gam"] } -# backup = { path = "libraries/backup" } +backup = { path = "../../apps/vault/libraries/backup" } byteorder = { version = "1.4.3", default-features = false } arrayref = "0.3.6" subtle = { version = "2.5.0", features = ["core_hint_black_box"] } diff --git a/apps-baosec/vault2/src/actions.rs b/apps-baosec/vault2/src/actions.rs index f66803044..0e18ab0a9 100644 --- a/apps-baosec/vault2/src/actions.rs +++ b/apps-baosec/vault2/src/actions.rs @@ -20,7 +20,7 @@ use ux_api::widgets::TextEntryPayload; use vault2::env::xous::U2F_APP_DICT; use vault2::{ AppInfo, VAULT_ALLOC_HINT, VAULT_PASSWORD_DICT, VAULT_TOTP_DICT, atime_to_str, basis_change, - ctap::data_formats::PublicKeyCredentialSource, deserialize_app_info, serialize_app_info, utc_now, + deserialize_app_info, serialize_app_info, utc_now, }; use xous::{Message, send_message}; @@ -66,7 +66,6 @@ pub struct ActionManager { pddb: RefCell, tt: ticktimer_server::Ticktimer, action_active: Arc, - opensk_mutex: Arc>, mode_cache: VaultMode, main_conn: xous::CID, keystore: Keystore, @@ -77,7 +76,6 @@ impl ActionManager { mode: Arc>, item_lists: Arc>, action_active: Arc, - opensk_mutex: Arc>, ) -> ActionManager { let xns = xous_names::XousNames::new().unwrap(); let storage_manager = storage::Manager::new(&xns); @@ -96,7 +94,6 @@ impl ActionManager { pddb: RefCell::new(pddb::Pddb::new()), tt: ticktimer_server::Ticktimer::new().unwrap(), action_active, - opensk_mutex, main_conn, keystore: Keystore::new(&xns), } @@ -1472,7 +1469,7 @@ pub(crate) fn heap_usage() -> usize { } } -fn make_pw_name(description: &str, username: &str, dest: &mut String) { +fn make_pw_name(description: &str, _username: &str, dest: &mut String) { dest.clear(); dest.push_str(description); // dest.push_str("/"); @@ -1489,20 +1486,7 @@ fn make_u2f_item_from_record(guid: &str, ai: AppInfo) -> ListItem { let desc: String = format!("{} (U2F)", ai.name); ListItem::new(desc, extra, true, guid.to_owned(), ai.count, ai.atime) } -fn make_fido_item_from_record(guid: &str, result: PublicKeyCredentialSource) -> ListItem { - let name = if let Some(display_name) = result.user_display_name { - display_name - } else { - String::from_utf8(result.user_handle).unwrap_or("".to_string()) - }; - let desc = format!( - "{} / {} (FIDO2)", - result.rp_id, - String::from_utf8(result.credential_id).unwrap_or("---".to_string()) - ); - let extra = format!("{}", name); - ListItem::new(desc, extra, true, guid.to_owned(), 0, 0) -} + fn make_totp_item_from_record(guid: &str, totp: TotpRecord) -> ListItem { let extra = format!( "{}:{}:{}:{}:{}", diff --git a/apps-baosec/vault2/src/main.rs b/apps-baosec/vault2/src/main.rs index 5aa6c00ca..7d2a594b1 100644 --- a/apps-baosec/vault2/src/main.rs +++ b/apps-baosec/vault2/src/main.rs @@ -1,14 +1,3 @@ -use core::sync::atomic::{AtomicBool, Ordering}; -use std::cell::RefCell; -use std::sync::{Arc, Mutex}; -use std::thread; - -use num_traits::*; -use pddb::Pddb; -use totp::PumpOp; -use xous::msg_blocking_scalar_unpack; -use xous_ipc::Buffer; - mod ux; use ux::*; mod itemcache; @@ -18,8 +7,29 @@ use actions::ActionOp; mod storage; mod submenu; mod totp; -pub(crate) mod vault_api; +pub mod vault_api; +pub use vault_api::*; +mod vendor_commands; + +use core::sync::atomic::{AtomicBool, Ordering}; +use std::cell::RefCell; +use std::sync::{Arc, Mutex}; +use std::thread; +use std::time::Instant; + use locales::t; +use num_traits::*; +use pddb::Pddb; +use totp::PumpOp; +use vault2::Transport; +use vault2::ctap::main_hid::HidIterType; +use vault2::env::Env; +use vault2::env::xous::XousEnv; +use xous::msg_blocking_scalar_unpack; +use xous_ipc::Buffer; +use xous_usb_hid::device::fido::*; + +use crate::vendor_commands::VendorSession; /* Dev status & notes -- @@ -119,26 +129,6 @@ necessarily listed in order of implementation: pub(crate) const SERVER_NAME_VAULT2: &str = "_Vault2_"; -/// Top level application events. -#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] -pub(crate) enum VaultOp { - /// Redraw the screen - Redraw = 0, - KeyPress, - - /// Partial menu - MenuChangeFont, - MenuDeleteStage1, - MenuEditStage1, - MenuAutotype, - MenuReadoutMode, - MenuAutotypeRate, - MenuLeftyMode, - MenuDone, - MenuTotpMode, - MenuPwMode, -} - #[derive(Copy, Clone, PartialEq, Eq, Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] pub enum VaultMode { Totp, @@ -170,6 +160,9 @@ fn main() -> ! { let action_active = Arc::new(AtomicBool::new(false)); // Protects access to the openSK PDDB entries from simultaneous readout on the UX while OpenSK is updating let opensk_mutex = Arc::new(Mutex::new(0)); + let allow_host = Arc::new(AtomicBool::new(false)); + // storage for lefty mode + let lefty_mode = Arc::new(AtomicBool::new(false)); let mut vault_ui = VaultUi::new(&xns, conn, item_lists.clone(), mode.clone()); @@ -193,10 +186,8 @@ fn main() -> ! { let mode = mode.clone(); let item_lists = item_lists.clone(); let action_active = action_active.clone(); - let opensk_mutex = opensk_mutex.clone(); move || { - let mut manager = - crate::actions::ActionManager::new(main_conn, mode, item_lists, action_active, opensk_mutex); + let mut manager = crate::actions::ActionManager::new(main_conn, mode, item_lists, action_active); loop { let msg = xous::receive_message(sid).unwrap(); let opcode: Option = FromPrimitive::from_usize(msg.body.id()); @@ -282,6 +273,116 @@ fn main() -> ! { let actions_conn = xous::connect(actions_sid).unwrap(); + // spawn the FIDO2 USB handler + let _ = thread::spawn({ + let allow_host = allow_host.clone(); + let opensk_mutex = opensk_mutex.clone(); + let conn = conn.clone(); + let lefty_mode = lefty_mode.clone(); + move || { + let mut vendor_session = VendorSession::default(); + // block until the PDDB is mounted + let pddb = pddb::Pddb::new(); + pddb.is_mounted_blocking(); + + let env = XousEnv::new(conn, lefty_mode); // lefty_mode is now owned by env + let mut ctap = vault2::Ctap::new(env, Instant::now()); + loop { + match ctap.env().main_hid_connection().u2f_wait_incoming() { + Ok(msg) => { + ctap.update_timeouts(Instant::now()); + let mutex = opensk_mutex.lock().unwrap(); + log::trace!("Received U2F packet"); + let typed_reply = + ctap.process_hid_packet(&msg.packet, Transport::MainHid, Instant::now()); + match typed_reply { + HidIterType::Ctap(reply) => { + for pkt_reply in reply { + let mut reply = RawFidoReport::default(); + reply.packet.copy_from_slice(&pkt_reply); + let status = ctap.env().main_hid_connection().u2f_send(reply); + match status { + Ok(()) => { + log::trace!("Sent U2F packet"); + } + Err(e) => { + log::error!("Error sending U2F packet: {:?}", e); + } + } + } + } + HidIterType::Vendor(msg) => { + let reply = match vendor_commands::handle_vendor_data( + msg.cmd as u8, + msg.cid, + msg.payload, + &mut vendor_session, + ) { + Ok(return_payload) => { + // if None, this means we've finished parsing all that + // was needed, and we handle/respond with real data + + match return_payload { + Some(data) => data, + None => { + log::debug!("starting processing of vendor data..."); + let resp = vendor_commands::handle_vendor_command( + &mut vendor_session, + allow_host.load(Ordering::SeqCst), + ); + log::debug!("finished processing of vendor data!"); + + match vendor_session.is_backup() { + true => { + if vendor_session.has_backup_data() { + resp + } else { + vendor_session = VendorSession::default(); + resp + } + } + false => { + vendor_session = VendorSession::default(); + resp + } + } + } + } + } + Err(session_error) => { + // reset the session + vendor_session = VendorSession::default(); + + session_error.ctaphid_error(msg.cid) + } + }; + for pkt_reply in reply { + let mut reply = RawFidoReport::default(); + reply.packet.copy_from_slice(&pkt_reply); + let status = ctap.env().main_hid_connection().u2f_send(reply); + match status { + Ok(()) => { + log::trace!("Sent U2F packet"); + } + Err(e) => { + log::error!("Error sending U2F packet: {:?}", e); + } + } + } + } + } + drop(mutex); + } + Err(e) => match e { + _ => { + log::warn!("FIDO listener got an error: {:?}", e); + } + }, + } + } + } + }); + let menu_sid = xous::create_server().unwrap(); let menu_mgr = submenu::create_submenu(conn, actions_conn, menu_sid); let modals = modals::Modals::new(&xns).unwrap(); @@ -306,6 +407,15 @@ fn main() -> ! { Some(VaultOp::Redraw) => { vault_ui.redraw(); } + Some(VaultOp::ReloadDbAndFullRedraw) => { + xous::send_message( + actions_conn, + xous::Message::new_blocking_scalar(ActionOp::ReloadDb.to_usize().unwrap(), 0, 0, 0, 0), + ) + .ok(); + vault_ui.refresh_totp(); + vault_ui.redraw(); + } Some(VaultOp::MenuDone) => { menu_active = false; // update the TOTP codes, in case there were changes @@ -404,6 +514,20 @@ fn main() -> ! { } allow_totp_rendering.store(true, Ordering::SeqCst); } + Some(VaultOp::BasisChange) => { + vault_ui.basis_change(); + xous::send_message( + conn, + xous::Message::new_blocking_scalar( + VaultOp::ReloadDbAndFullRedraw.to_usize().unwrap(), + 0, + 0, + 0, + 0, + ), + ) + .ok(); + } _ => { log::error!("Got unknown message: {:?}", msg); } diff --git a/apps-baosec/vault2/src/storage.rs b/apps-baosec/vault2/src/storage.rs index 0d6ac8a4d..bb4fc2eed 100644 --- a/apps-baosec/vault2/src/storage.rs +++ b/apps-baosec/vault2/src/storage.rs @@ -9,9 +9,8 @@ use chrono::{DateTime, NaiveDateTime, Utc}; use ctap_crypto::Hash256; use crate::totp::TotpAlgorithm; - -const VAULT_PASSWORD_DICT: &'static str = "vault.passwords"; -const VAULT_TOTP_DICT: &'static str = "vault.totp"; +use crate::vault_api::VAULT_PASSWORD_DICT; +use crate::vault_api::VAULT_TOTP_DICT; const VAULT_TOTP_ALLOC_HINT: usize = 128; const VAULT_PASSWORD_REC_VERSION: u32 = 1; diff --git a/apps-baosec/vault2/src/ux.rs b/apps-baosec/vault2/src/ux.rs index 38a3518d5..9c226a873 100644 --- a/apps-baosec/vault2/src/ux.rs +++ b/apps-baosec/vault2/src/ux.rs @@ -143,6 +143,11 @@ impl VaultUi { } } + pub(crate) fn basis_change(&mut self) { + self.item_lists.lock().unwrap().clear_all(); + self.totp_list.clear(); + } + pub(crate) fn store_glyph_style(&mut self, style: GlyphStyle) { self.pddb .borrow() @@ -213,7 +218,7 @@ impl VaultUi { // to reduce locking thrash, we cache a copy of the current mode at the top of redraw. let mode_at_entry = (*self.mode.lock().unwrap()).clone(); - self.gfx.clear().ok(); + self.clear_area(); match mode_at_entry { VaultMode::Totp => { @@ -337,7 +342,7 @@ impl VaultUi { match self.storage_manager.get_record(&ContentKind::Password, guid) { Ok(record) => record, Err(error) => { - log::error!("internal error rendering password"); + log::error!("internal error rendering password: {:?}", error); self.gfx.flush().ok(); return; } diff --git a/apps-baosec/vault2/src/vault_api.rs b/apps-baosec/vault2/src/vault_api.rs index caa00f4b7..0c7d0583f 100644 --- a/apps-baosec/vault2/src/vault_api.rs +++ b/apps-baosec/vault2/src/vault_api.rs @@ -16,20 +16,13 @@ pub const VAULT_TOTP_DICT: &'static str = "vault.totp"; /// bytes to reserve for a key entry. Making this slightly larger saves on some churn as stuff gets updated pub const VAULT_ALLOC_HINT: usize = 256; +/// Top level application events. #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] -pub enum VaultOp { - /// a line of text has arrived - Line = 0, // make sure we occupy opcodes with discriminants < 1000, as the rest are used for callbacks - /// incremental line of text - IncrementalLine, - /// redraw our UI - Redraw, - /// ignore dirty rectangles and redraw everything - FullRedraw, - /// reload the database (slow), and ignore dirty rectangles and redraw everything +pub(crate) enum VaultOp { + /// Redraw the screen + Redraw = 0, ReloadDbAndFullRedraw, - /// change focus - ChangeFocus, + KeyPress, /// Partial menu MenuChangeFont, @@ -39,15 +32,11 @@ pub enum VaultOp { MenuReadoutMode, MenuAutotypeRate, MenuLeftyMode, + MenuDone, + MenuTotpMode, + MenuPwMode, - /// PDDB basis change BasisChange, - - /// Nop while waiting for prerequisites to be filled - Nop, - - /// exit the application - Quit, } pub fn atime_to_str(req_atime: u64) -> String { diff --git a/apps-baosec/vault2/src/vendor_commands.rs b/apps-baosec/vault2/src/vendor_commands.rs index 51cd566c9..21b170eba 100644 --- a/apps-baosec/vault2/src/vendor_commands.rs +++ b/apps-baosec/vault2/src/vendor_commands.rs @@ -2,8 +2,8 @@ use core::convert::TryFrom; use cbor::reader::DecoderError; use locales::t; -use vault::ctap::hid::{ChannelID, CtapHidCommand, Message, send::HidPacketIterator}; -use vault::vault_api::{COMMAND_BACKUP_TOTP_CODES, COMMAND_RESET_SESSION, COMMAND_RESTORE_TOTP_CODES}; +use vault2::ctap::hid::{ChannelID, CtapHidCommand, Message, send::HidPacketIterator}; +use vault2::vault_api::{COMMAND_BACKUP_TOTP_CODES, COMMAND_RESET_SESSION, COMMAND_RESTORE_TOTP_CODES}; use crate::storage::{Error, PasswordRecord, TotpRecord}; use crate::totp::TotpAlgorithm;