diff --git a/apps/vault/src/ux/framework.rs b/apps/vault/src/ux/framework.rs index d76c9b697..b5e3ac4e5 100644 --- a/apps/vault/src/ux/framework.rs +++ b/apps/vault/src/ux/framework.rs @@ -190,7 +190,7 @@ impl VaultUx { /* Entry Users ------------------------------------- - - autotype pw + - autotype pw totp - add new pw totp - edit pw totp fido - delete pw totp fido @@ -203,19 +203,7 @@ impl VaultUx { self.menu_mgr.delete_item(t!("vault.menu_addnew", xous::LANG)); match *self.mode.lock().unwrap() { VaultMode::Fido => (), - VaultMode::Totp => { - self.menu_mgr.insert_item( - MenuItem { - name: xous_ipc::String::from_str(t!("vault.menu_addnew", xous::LANG)), - action_conn: Some(self.actions_conn), - action_opcode: ActionOp::MenuAddnew.to_u32().unwrap(), - action_payload: MenuPayload::Scalar([0, 0, 0, 0]), - close_on_select: true, - }, - 0 - ); - }, - VaultMode::Password => { + VaultMode::Password | VaultMode::Totp => { self.menu_mgr.insert_item( MenuItem { name: xous_ipc::String::from_str(t!("vault.menu_addnew", xous::LANG)), @@ -647,74 +635,102 @@ impl VaultUx { if self.selection_index >= self.filtered_list.len() { return Err(xous::Error::InvalidPID); } - let entry = &self.filtered_list[self.selection_index].guid; - // we re-fetch the entry for autotype, because the PDDB could have unmounted a basis. - let updated_pw = match self.pddb.borrow().get( - crate::actions::VAULT_PASSWORD_DICT, - entry, - None, - false, false, None, - Some(crate::basis_change) - ) { - Ok(mut record) => { - let mut data = Vec::::new(); - match record.read_to_end(&mut data) { - Ok(_len) => { - if let Some(mut pw) = crate::actions::deserialize_password(data) { - match self.usb_dev.send_str(&pw.password) { - Ok(_) => { - pw.count += 1; - pw.atime = utc_now().timestamp() as u64; - pw - }, - Err(e) => { - log::error!("couldn't autotype password: {:?}", e); - return Err(xous::Error::UseBeforeInit); + let mode_cache = (*self.mode.lock().unwrap()).clone(); + match mode_cache { + VaultMode::Password => { + let entry = &self.filtered_list[self.selection_index].guid; + // we re-fetch the entry for autotype, because the PDDB could have unmounted a basis. + let updated_pw = match self.pddb.borrow().get( + crate::actions::VAULT_PASSWORD_DICT, + entry, + None, + false, false, None, + Some(crate::basis_change) + ) { + Ok(mut record) => { + let mut data = Vec::::new(); + match record.read_to_end(&mut data) { + Ok(_len) => { + if let Some(mut pw) = crate::actions::deserialize_password(data) { + match self.usb_dev.send_str(&pw.password) { + Ok(_) => { + pw.count += 1; + pw.atime = utc_now().timestamp() as u64; + pw + }, + Err(e) => { + log::error!("couldn't autotype password: {:?}", e); + return Err(xous::Error::UseBeforeInit); + } + } + } else { + log::error!("couldn't deserialize {}", entry); + return Err(xous::Error::InvalidString); } } - } else { - log::error!("couldn't deserialize {}", entry); - return Err(xous::Error::InvalidString); + Err(e) => { + log::error!("couldn't access key {}: {:?}", entry, e); + return Err(xous::Error::ProcessNotFound); + }, } } Err(e) => { log::error!("couldn't access key {}: {:?}", entry, e); return Err(xous::Error::ProcessNotFound); - }, - } - } - Err(e) => { - log::error!("couldn't access key {}: {:?}", entry, e); - return Err(xous::Error::ProcessNotFound); - } - }; - match self.pddb.borrow().delete_key(crate::actions::VAULT_PASSWORD_DICT, entry, None) { - Ok(_) => {} - Err(_e) => { - return Err(xous::Error::InternalError); - } - } - match self.pddb.borrow().get( - crate::actions::VAULT_PASSWORD_DICT, entry, None, - false, true, Some(crate::actions::VAULT_ALLOC_HINT), - Some(crate::basis_change) - ) { - Ok(mut record) => { - let ser = crate::actions::serialize_password(&updated_pw); - match record.write(&ser) { + } + }; + match self.pddb.borrow().delete_key(crate::actions::VAULT_PASSWORD_DICT, entry, None) { Ok(_) => {} + Err(_e) => { + return Err(xous::Error::InternalError); + } + } + match self.pddb.borrow().get( + crate::actions::VAULT_PASSWORD_DICT, entry, None, + false, true, Some(crate::actions::VAULT_ALLOC_HINT), + Some(crate::basis_change) + ) { + Ok(mut record) => { + let ser = crate::actions::serialize_password(&updated_pw); + match record.write(&ser) { + Ok(_) => {} + Err(e) => { + log::error!("couldn't update key {}: {:?}", entry, e); + return Err(xous::Error::OutOfMemory); + } + } + } Err(e) => { log::error!("couldn't update key {}: {:?}", entry, e); return Err(xous::Error::OutOfMemory); } } + self.pddb.borrow().sync().ok(); } - Err(e) => { - log::error!("couldn't update key {}: {:?}", entry, e); - return Err(xous::Error::OutOfMemory); + VaultMode::Totp => { + let fields = self.filtered_list[self.selection_index].extra.split(':').collect::>(); + if fields.len() == 4 { + let shared_secret = base32::decode( + base32::Alphabet::RFC4648 { padding: false }, fields[0]) + .unwrap_or(vec![]); + let digit_count = u8::from_str_radix(fields[1], 10).unwrap_or(6); + let step_seconds = u16::from_str_radix(fields[2], 10).unwrap_or(30); + let algorithm = TotpAlgorithm::try_from(fields[2]).unwrap_or(TotpAlgorithm::HmacSha1); + let totp = totp::TotpEntry { + step_seconds, + shared_secret, + digit_count, + algorithm + }; + let code = generate_totp_code( + totp::get_current_unix_time().unwrap_or(0), + &totp + ).unwrap_or(t!("vault.error.record_error", xous::LANG).to_string()); + self.usb_dev.send_str(&code).ok(); + } } + _ => log::error!("Illegal state! we shouldn't be having an autotype on {:?}", mode_cache), } - self.pddb.borrow().sync().ok(); Ok(()) } pub(crate) fn selected_entry(&self) -> Option {