fix in-place edit of keys

The behavior with a secret basis is as follows:

- The key stays in the basis in which it was created.
- So, if you created k1 in .System basis, and k2 in Secret basis,
  if you edit k1 while Secret basis is open, k1 stays in .System
  basis. it does *not* migrate into Secret basis.

I think this is the "least surprising" behavior. There is an
argument that "maybe I want to keep secret notes in an overlay
on a key" but I feel like creating a second shadow copy of a key
with a different set of notes is not the most desired behavior
for this application (NB: it could be desired for others applications,
and that behavior can be programmed with the PDDB API; which is
why the API is a bit explicit at the moment. This flexibility goes
against the goal of "foolproof deniability", unfortunately, but
I think it is too early to know which default to set for the system).
This commit is contained in:
bunnie 2022-06-26 17:37:57 +08:00
parent 42b84ad890
commit 79c20eb498
3 changed files with 104 additions and 51 deletions

View file

@ -441,33 +441,46 @@ impl ActionManager {
VaultMode::Fido => crate::fido::U2F_APP_DICT, VaultMode::Fido => crate::fido::U2F_APP_DICT,
VaultMode::Totp => VAULT_TOTP_DICT, VaultMode::Totp => VAULT_TOTP_DICT,
}; };
match self.pddb.borrow().delete_key(dict, entry.key_name.as_str().unwrap_or("UTF8-error"), None) { // first "get" the key, to resolve exactly what basis the key is in. This is because `delete_key()` will
Ok(_) => { // only look in the most recently unlocked secret basis, it won't automatically descend into the database
self.modals.show_notification(t!("vault.completed", xous::LANG), None).ok(); // and try to cull something willy-nilly.
} match self.pddb.borrow().get(dict, entry.key_name.as_str().unwrap_or("UTF8-error"),
Err(e) => { None, false, false, None, None::<fn()>
match e.kind() { ) {
std::io::ErrorKind::NotFound => { Ok(candidate) => {
// handle special case of FIDO which is two dicts combined let attr = candidate.attributes().expect("couldn't get key attributes");
if entry.mode == VaultMode::Fido { match self.pddb.borrow().delete_key(dict,
// try the "other" dictionary entry.key_name.as_str().unwrap_or("UTF8-error"),
match self.pddb.borrow() Some(&attr.basis)) {
.delete_key( Ok(_) => {
crate::ctap::FIDO_CRED_DICT, self.modals.show_notification(t!("vault.completed", xous::LANG), None).ok();
entry.key_name.as_str().unwrap_or("UTF8-error"), }
None) { Err(e) => {
Ok(_) => { match e.kind() {
self.modals.show_notification(t!("vault.completed", xous::LANG), None).ok(); std::io::ErrorKind::NotFound => {
return; // 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(
self.report_err(t!("vault.error.not_found", xous::LANG), None::<std::io::Error>); 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::<std::io::Error>);
}
_ => 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) false, false, None, Some(crate::basis_change)
) { ) {
Ok(mut record) => { 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::<u8>::new(); let mut data = Vec::<u8>::new();
let maybe_update = match record.read_to_end(&mut data) { let maybe_update = match record.read_to_end(&mut data) {
Ok(_len) => { Ok(_len) => {
@ -501,13 +516,13 @@ impl ActionManager {
pw.password = edit_data.content()[2].content.as_str().unwrap().to_string(); 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.notes = edit_data.content()[3].content.as_str().unwrap().to_string();
pw.atime = utc_now().timestamp() as u64; pw.atime = utc_now().timestamp() as u64;
Some(pw) pw
} else { log::error!("record error"); } else { log::error!("record error");
self.report_err(t!("vault.error.record_error", xous::LANG), None::<std::io::Error>); None } self.report_err(t!("vault.error.record_error", xous::LANG), None::<std::io::Error>); 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) => { Err(e) => {
match e.kind() { match e.kind() {
@ -523,12 +538,12 @@ impl ActionManager {
None None
} }
}; };
if let Some(update) = maybe_update { if let Some((update, basis)) = maybe_update {
self.pddb.borrow().delete_key(dict, entry.key_name.as_str().unwrap(), None) self.pddb.borrow().delete_key(dict, entry.key_name.as_str().unwrap(), Some(&basis))
.unwrap_or_else(|e| {log::error!("internal error"); .unwrap_or_else(|e| {log::error!("internal error");
self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e))}); self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e))});
match self.pddb.borrow().get( 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), false, true, Some(VAULT_ALLOC_HINT),
Some(crate::basis_change) Some(crate::basis_change)
) { ) {
@ -555,6 +570,8 @@ impl ActionManager {
false, false, None, Some(crate::basis_change) false, false, None, Some(crate::basis_change)
) { ) {
Ok(mut record) => { 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::<u8>::new(); let mut data = Vec::<u8>::new();
let maybe_update = match record.read_to_end(&mut data) { let maybe_update = match record.read_to_end(&mut data) {
Ok(_len) => { Ok(_len) => {
@ -568,26 +585,26 @@ impl ActionManager {
ai.name = edit_data.content()[0].content.as_str().unwrap().to_string(); 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.notes = edit_data.content()[1].content.as_str().unwrap().to_string();
ai.atime = utc_now().timestamp() as u64; ai.atime = utc_now().timestamp() as u64;
Some(ai) ai
} else { self.report_err(t!("vault.error.record_error", xous::LANG), None::<std::io::Error>); None } } else { self.report_err(t!("vault.error.record_error", xous::LANG), None::<std::io::Error>); 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) => { Err(e) => {
match e.kind() { match e.kind() {
std::io::ErrorKind::NotFound => self.report_err(t!("vault.error.fido2", xous::LANG), None::<std::io::Error>), std::io::ErrorKind::NotFound => self.report_err(t!("vault.error.fido2", xous::LANG), None::<std::io::Error>),
_ => self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)), _ => self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)),
} }
None return
} }
}; };
if let Some(update) = maybe_update { if let Some((update, basis)) = maybe_update {
self.pddb.borrow().delete_key(dict, entry.key_name.as_str().unwrap(), None) 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))); .unwrap_or_else(|e| self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)));
match self.pddb.borrow().get( 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), false, true, Some(VAULT_ALLOC_HINT),
Some(crate::basis_change) Some(crate::basis_change)
) { ) {
@ -607,6 +624,8 @@ impl ActionManager {
false, false, None, Some(crate::basis_change) false, false, None, Some(crate::basis_change)
) { ) {
Ok(mut record) => { 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::<u8>::new(); let mut data = Vec::<u8>::new();
let maybe_update = match record.read_to_end(&mut data) { let maybe_update = match record.read_to_end(&mut data) {
Ok(_len) => { 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) { if let Ok(d) = u32::from_str_radix(edit_data.content()[5].content.as_str().unwrap(), 10) {
pw.digits = d; pw.digits = d;
} }
Some(pw) pw
} else { self.report_err(t!("vault.error.record_error", xous::LANG), None::<std::io::Error>); None } } else { self.report_err(t!("vault.error.record_error", xous::LANG), None::<std::io::Error>); 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) => { Err(e) => {
match e.kind() { match e.kind() {
std::io::ErrorKind::NotFound => self.report_err(t!("vault.error.not_found", xous::LANG), None::<std::io::Error>), std::io::ErrorKind::NotFound => self.report_err(t!("vault.error.not_found", xous::LANG), None::<std::io::Error>),
_ => self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)), _ => self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)),
} }
None return
} }
}; };
if let Some(update) = maybe_update { if let Some((update, basis)) = maybe_update {
self.pddb.borrow().delete_key(dict, entry.key_name.as_str().unwrap(), None) 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))); .unwrap_or_else(|e| self.report_err(t!("vault.error.internal_error", xous::LANG), Some(e)));
match self.pddb.borrow().get( 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), false, true, Some(VAULT_ALLOC_HINT),
Some(crate::basis_change) Some(crate::basis_change)
) { ) {

View file

@ -457,12 +457,28 @@ pub(crate) fn start_fido_ux_thread() {
} }
} }
}; };
pddb.delete_key(U2F_APP_DICT, &app_id_str, None).ok(); // this get determines which basis the key is in
match pddb.get( let basis = match pddb.get(
U2F_APP_DICT, U2F_APP_DICT,
&app_id_str, &app_id_str,
None, true, true, None, true, true,
Some(256), Some(crate::basis_change) 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) => { Ok(mut app_data) => {
app_data.write(&ser).expect("couldn't update atime"); app_data.write(&ser).expect("couldn't update atime");

View file

@ -686,14 +686,32 @@ impl VaultUx {
return Err(xous::Error::ProcessNotFound); 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(_) => {} Ok(_) => {}
Err(_e) => { Err(_e) => {
return Err(xous::Error::InternalError); return Err(xous::Error::InternalError);
} }
} }
match self.pddb.borrow().get( 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), false, true, Some(crate::actions::VAULT_ALLOC_HINT),
Some(crate::basis_change) Some(crate::basis_change)
) { ) {