diff --git a/apps/vault/src/actions.rs b/apps/vault/src/actions.rs index 45a3851cf..fd6acf9d8 100644 --- a/apps/vault/src/actions.rs +++ b/apps/vault/src/actions.rs @@ -441,33 +441,46 @@ impl ActionManager { VaultMode::Fido => crate::fido::U2F_APP_DICT, VaultMode::Totp => VAULT_TOTP_DICT, }; - match self.pddb.borrow().delete_key(dict, entry.key_name.as_str().unwrap_or("UTF8-error"), None) { - Ok(_) => { - self.modals.show_notification(t!("vault.completed", xous::LANG), None).ok(); - } - Err(e) => { - match e.kind() { - std::io::ErrorKind::NotFound => { - // handle special case of FIDO which is two dicts combined - if entry.mode == VaultMode::Fido { - // try the "other" dictionary - match self.pddb.borrow() - .delete_key( - crate::ctap::FIDO_CRED_DICT, - entry.key_name.as_str().unwrap_or("UTF8-error"), - None) { - Ok(_) => { - self.modals.show_notification(t!("vault.completed", xous::LANG), None).ok(); - return; - } - _ => {} - } - } - self.report_err(t!("vault.error.not_found", xous::LANG), None::); + // first "get" the key, to resolve exactly what basis the key is in. This is because `delete_key()` will + // only look in the most recently unlocked secret basis, it won't automatically descend into the database + // and try to cull something willy-nilly. + match self.pddb.borrow().get(dict, entry.key_name.as_str().unwrap_or("UTF8-error"), + None, false, false, None, None:: + ) { + Ok(candidate) => { + let attr = candidate.attributes().expect("couldn't get key attributes"); + match self.pddb.borrow().delete_key(dict, + entry.key_name.as_str().unwrap_or("UTF8-error"), + Some(&attr.basis)) { + Ok(_) => { + self.modals.show_notification(t!("vault.completed", xous::LANG), None).ok(); + } + Err(e) => { + match e.kind() { + std::io::ErrorKind::NotFound => { + // handle special case of FIDO which is two dicts combined + if entry.mode == VaultMode::Fido { + // try the "other" dictionary + match self.pddb.borrow() + .delete_key( + crate::ctap::FIDO_CRED_DICT, + entry.key_name.as_str().unwrap_or("UTF8-error"), + Some(&attr.basis)) { + Ok(_) => { + self.modals.show_notification(t!("vault.completed", xous::LANG), None).ok(); + return; + } + _ => {} + } + } + self.report_err(t!("vault.error.not_found", xous::LANG), None::); + } + _ => self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)), + } } - _ => self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)), } } + Err(e) => self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)), } } } @@ -485,6 +498,8 @@ impl ActionManager { false, false, None, Some(crate::basis_change) ) { Ok(mut record) => { + // resolve the basis of the key, so that we are editing it "in place" + let attr = record.attributes().expect("couldn't get key attributes"); let mut data = Vec::::new(); let maybe_update = match record.read_to_end(&mut data) { Ok(_len) => { @@ -501,13 +516,13 @@ impl ActionManager { pw.password = edit_data.content()[2].content.as_str().unwrap().to_string(); pw.notes = edit_data.content()[3].content.as_str().unwrap().to_string(); pw.atime = utc_now().timestamp() as u64; - Some(pw) + pw } else { log::error!("record error"); - self.report_err(t!("vault.error.record_error", xous::LANG), None::); None } + self.report_err(t!("vault.error.record_error", xous::LANG), None::); return } } - Err(e) => { log::error!("internal error"); self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)); None } + Err(e) => { log::error!("internal error"); self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)); return } }; - maybe_update + Some((maybe_update, attr.basis)) } Err(e) => { match e.kind() { @@ -523,12 +538,12 @@ impl ActionManager { None } }; - if let Some(update) = maybe_update { - self.pddb.borrow().delete_key(dict, entry.key_name.as_str().unwrap(), None) + if let Some((update, basis)) = maybe_update { + self.pddb.borrow().delete_key(dict, entry.key_name.as_str().unwrap(), Some(&basis)) .unwrap_or_else(|e| {log::error!("internal error"); self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e))}); match self.pddb.borrow().get( - dict, entry.key_name.as_str().unwrap(), None, + dict, entry.key_name.as_str().unwrap(), Some(&basis), false, true, Some(VAULT_ALLOC_HINT), Some(crate::basis_change) ) { @@ -555,6 +570,8 @@ impl ActionManager { false, false, None, Some(crate::basis_change) ) { Ok(mut record) => { + // resolve the basis of the key, so that we are editing it "in place" + let attr = record.attributes().expect("couldn't get key attributes"); let mut data = Vec::::new(); let maybe_update = match record.read_to_end(&mut data) { Ok(_len) => { @@ -568,26 +585,26 @@ impl ActionManager { ai.name = edit_data.content()[0].content.as_str().unwrap().to_string(); ai.notes = edit_data.content()[1].content.as_str().unwrap().to_string(); ai.atime = utc_now().timestamp() as u64; - Some(ai) - } else { self.report_err(t!("vault.error.record_error", xous::LANG), None::); None } + ai + } else { self.report_err(t!("vault.error.record_error", xous::LANG), None::); return } } - Err(e) => { self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)); None } + Err(e) => { self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)); return } }; - maybe_update + Some((maybe_update, attr.basis)) } Err(e) => { match e.kind() { std::io::ErrorKind::NotFound => self.report_err(t!("vault.error.fido2", xous::LANG), None::), _ => self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)), } - None + return } }; - if let Some(update) = maybe_update { - self.pddb.borrow().delete_key(dict, entry.key_name.as_str().unwrap(), None) + if let Some((update, basis)) = maybe_update { + self.pddb.borrow().delete_key(dict, entry.key_name.as_str().unwrap(), Some(&basis)) .unwrap_or_else(|e| self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e))); match self.pddb.borrow().get( - dict, entry.key_name.as_str().unwrap(), None, + dict, entry.key_name.as_str().unwrap(), Some(&basis), false, true, Some(VAULT_ALLOC_HINT), Some(crate::basis_change) ) { @@ -607,6 +624,8 @@ impl ActionManager { false, false, None, Some(crate::basis_change) ) { Ok(mut record) => { + // resolve the basis of the key, so that we are editing it "in place" + let attr = record.attributes().expect("couldn't get key attributes"); let mut data = Vec::::new(); let maybe_update = match record.read_to_end(&mut data) { Ok(_len) => { @@ -633,26 +652,26 @@ impl ActionManager { if let Ok(d) = u32::from_str_radix(edit_data.content()[5].content.as_str().unwrap(), 10) { pw.digits = d; } - Some(pw) - } else { self.report_err(t!("vault.error.record_error", xous::LANG), None::); None } + pw + } else { self.report_err(t!("vault.error.record_error", xous::LANG), None::); return } } - Err(e) => { self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)); None } + Err(e) => { self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)); return } }; - maybe_update + Some((maybe_update, attr.basis)) } Err(e) => { match e.kind() { std::io::ErrorKind::NotFound => self.report_err(t!("vault.error.not_found", xous::LANG), None::), _ => self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)), } - None + return } }; - if let Some(update) = maybe_update { - self.pddb.borrow().delete_key(dict, entry.key_name.as_str().unwrap(), None) + if let Some((update, basis)) = maybe_update { + self.pddb.borrow().delete_key(dict, entry.key_name.as_str().unwrap(), Some(&basis)) .unwrap_or_else(|e| self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e))); match self.pddb.borrow().get( - dict, entry.key_name.as_str().unwrap(), None, + dict, entry.key_name.as_str().unwrap(), Some(&basis), false, true, Some(VAULT_ALLOC_HINT), Some(crate::basis_change) ) { diff --git a/apps/vault/src/ux/fido.rs b/apps/vault/src/ux/fido.rs index a78fb5b8e..d7a8890fc 100644 --- a/apps/vault/src/ux/fido.rs +++ b/apps/vault/src/ux/fido.rs @@ -457,12 +457,28 @@ pub(crate) fn start_fido_ux_thread() { } } }; - pddb.delete_key(U2F_APP_DICT, &app_id_str, None).ok(); - match pddb.get( + // this get determines which basis the key is in + let basis = match pddb.get( U2F_APP_DICT, &app_id_str, None, true, true, Some(256), Some(crate::basis_change) + ) { + Ok(app_data) => { + let attr = app_data.attributes().expect("couldn't get attributes"); + attr.basis + } + Err(e) => { + log::error!("error updating app atime: {:?}", e); + continue; + } + }; + pddb.delete_key(U2F_APP_DICT, &app_id_str, Some(&basis)).ok(); + match pddb.get( + U2F_APP_DICT, + &app_id_str, + Some(&basis), true, true, + Some(256), Some(crate::basis_change) ) { Ok(mut app_data) => { app_data.write(&ser).expect("couldn't update atime"); diff --git a/apps/vault/src/ux/framework.rs b/apps/vault/src/ux/framework.rs index 257592d4a..e66a98941 100644 --- a/apps/vault/src/ux/framework.rs +++ b/apps/vault/src/ux/framework.rs @@ -686,14 +686,32 @@ impl VaultUx { return Err(xous::Error::ProcessNotFound); } }; - match self.pddb.borrow().delete_key(crate::actions::VAULT_PASSWORD_DICT, entry, None) { + + // this get determines which basis the key is in + let basis = match self.pddb.borrow().get( + crate::actions::VAULT_PASSWORD_DICT, + entry, + None, true, true, + Some(256), Some(crate::basis_change) + ) { + Ok(app_data) => { + let attr = app_data.attributes().expect("couldn't get attributes"); + attr.basis + } + Err(e) => { + log::error!("error updating key atime: {:?}", e); + return Err(xous::Error::InternalError); + } + }; + + match self.pddb.borrow().delete_key(crate::actions::VAULT_PASSWORD_DICT, entry, Some(&basis)) { Ok(_) => {} Err(_e) => { return Err(xous::Error::InternalError); } } match self.pddb.borrow().get( - crate::actions::VAULT_PASSWORD_DICT, entry, None, + crate::actions::VAULT_PASSWORD_DICT, entry, Some(&basis), false, true, Some(crate::actions::VAULT_ALLOC_HINT), Some(crate::basis_change) ) {