mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
chore(vault): support backup/restore of passwords
This commit is contained in:
parent
8c0564bc22
commit
31cb7c9ae1
1 changed files with 98 additions and 45 deletions
|
|
@ -4,7 +4,7 @@ use locales::t;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ctap::hid::{send::HidPacketIterator, ChannelID, Message},
|
ctap::hid::{send::HidPacketIterator, ChannelID, Message},
|
||||||
storage::{Error, TotpRecord},
|
storage::{Error, PasswordRecord, TotpRecord},
|
||||||
};
|
};
|
||||||
|
|
||||||
// Vault-specific command to upload TOTP codes
|
// Vault-specific command to upload TOTP codes
|
||||||
|
|
@ -23,24 +23,30 @@ pub fn handle_vendor_command(
|
||||||
let payload = match cmd {
|
let payload = match cmd {
|
||||||
COMMAND_BENCHMARK => Message {
|
COMMAND_BENCHMARK => Message {
|
||||||
cid: channel_id,
|
cid: channel_id,
|
||||||
cmd: cmd,
|
cmd,
|
||||||
payload: handle_benchmark(),
|
payload: handle_benchmark(),
|
||||||
},
|
},
|
||||||
COMMAND_RESTORE_TOTP_CODES => match handle_restore(payload, &xns) {
|
COMMAND_RESTORE_TOTP_CODES => match handle_restore(payload, &xns) {
|
||||||
Ok(payload) => Message {
|
Ok(payload) => Message {
|
||||||
cid: channel_id,
|
cid: channel_id,
|
||||||
cmd: cmd,
|
cmd,
|
||||||
payload: payload,
|
payload,
|
||||||
},
|
},
|
||||||
Err(_) => error_message(channel_id, 41),
|
Err(error) => {
|
||||||
|
log::error!("error while restoring codes: {:?}", error);
|
||||||
|
error_message(channel_id, 41)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
COMMAND_BACKUP_TOTP_CODES => match handle_backup(&xns) {
|
COMMAND_BACKUP_TOTP_CODES => match handle_backup(&xns, payload) {
|
||||||
Ok(payload) => Message {
|
Ok(payload) => Message {
|
||||||
cid: channel_id,
|
cid: channel_id,
|
||||||
cmd: cmd,
|
cmd,
|
||||||
payload: payload,
|
payload,
|
||||||
},
|
},
|
||||||
Err(_) => error_message(channel_id, 42),
|
Err(error) => {
|
||||||
|
log::error!("error while restoring codes: {:?}", error);
|
||||||
|
error_message(channel_id, 42)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
_ => error_message(channel_id, 0x33),
|
_ => error_message(channel_id, 0x33),
|
||||||
};
|
};
|
||||||
|
|
@ -86,53 +92,100 @@ fn handle_restore(data: Vec<u8>, xns: &xous_names::XousNames) -> Result<Vec<u8>,
|
||||||
|
|
||||||
let c = cbor::read(&data)?;
|
let c = cbor::read(&data)?;
|
||||||
|
|
||||||
let data = backup::TotpEntries::try_from(c)?;
|
let data = backup::DataPacket::try_from(c)?;
|
||||||
|
|
||||||
for elem in data.0 {
|
match data {
|
||||||
let totp = TotpRecord {
|
backup::DataPacket::TOTP(totp_entries) => {
|
||||||
version: 1,
|
for elem in totp_entries.0 {
|
||||||
name: elem.name,
|
let totp = TotpRecord {
|
||||||
secret: elem.shared_secret,
|
version: 1,
|
||||||
algorithm: match elem.algorithm {
|
name: elem.name,
|
||||||
backup::HashAlgorithms::SHA1 => crate::totp::TotpAlgorithm::HmacSha1,
|
secret: elem.shared_secret,
|
||||||
backup::HashAlgorithms::SHA256 => crate::totp::TotpAlgorithm::HmacSha256,
|
algorithm: match elem.algorithm {
|
||||||
backup::HashAlgorithms::SHA512 => crate::totp::TotpAlgorithm::HmacSha512,
|
backup::HashAlgorithms::SHA1 => crate::totp::TotpAlgorithm::HmacSha1,
|
||||||
},
|
backup::HashAlgorithms::SHA256 => crate::totp::TotpAlgorithm::HmacSha256,
|
||||||
digits: elem.digit_count,
|
backup::HashAlgorithms::SHA512 => crate::totp::TotpAlgorithm::HmacSha512,
|
||||||
timestep: elem.step_seconds,
|
},
|
||||||
ctime: 0, // Will be filled in later by storage::new_totp_record();
|
digits: elem.digit_count,
|
||||||
notes: t!("vault.notes", xous::LANG).to_string(),
|
timestep: elem.step_seconds,
|
||||||
};
|
ctime: 0, // Will be filled in later by storage::new_totp_record();
|
||||||
|
notes: t!("vault.notes", xous::LANG).to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
storage.new_record(totp, None)?
|
storage.new_record(totp, None)?;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
backup::DataPacket::Password(password_entries) => {
|
||||||
|
for elem in password_entries.0 {
|
||||||
|
let password = PasswordRecord {
|
||||||
|
version: 1,
|
||||||
|
description: elem.description,
|
||||||
|
username: elem.username,
|
||||||
|
password: elem.password,
|
||||||
|
notes: elem.notes,
|
||||||
|
count: 0,
|
||||||
|
ctime: 0,
|
||||||
|
atime: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
storage.new_record(password, None)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
Ok(vec![0xca, 0xfe, 0xba, 0xbe])
|
Ok(vec![0xca, 0xfe, 0xba, 0xbe])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_backup(xns: &xous_names::XousNames) -> Result<Vec<u8>, BackupError> {
|
fn handle_backup(
|
||||||
|
xns: &xous_names::XousNames,
|
||||||
|
payload_type: Vec<u8>,
|
||||||
|
) -> Result<Vec<u8>, BackupError> {
|
||||||
let mut storage = crate::storage::Manager::new(xns);
|
let mut storage = crate::storage::Manager::new(xns);
|
||||||
|
|
||||||
let totp_codes: Vec<crate::storage::TotpRecord> = storage.all(crate::storage::ContentKind::TOTP)?;
|
let payload_type: backup::PayloadType = backup::PayloadType::from(payload_type);
|
||||||
|
|
||||||
let mut ret = vec![];
|
match payload_type {
|
||||||
|
backup::PayloadType::TOTP => {
|
||||||
|
let totp_codes: Vec<crate::storage::TotpRecord> =
|
||||||
|
storage.all(crate::storage::ContentKind::TOTP)?;
|
||||||
|
|
||||||
for raw_code in totp_codes {
|
let mut ret = vec![];
|
||||||
ret.push(backup::TotpEntry {
|
|
||||||
step_seconds: raw_code.timestep,
|
for raw_code in totp_codes {
|
||||||
shared_secret: raw_code.secret,
|
ret.push(backup::TotpEntry {
|
||||||
digit_count: raw_code.digits,
|
step_seconds: raw_code.timestep,
|
||||||
algorithm: match raw_code.algorithm {
|
shared_secret: raw_code.secret,
|
||||||
crate::totp::TotpAlgorithm::HmacSha1 => backup::HashAlgorithms::SHA1,
|
digit_count: raw_code.digits,
|
||||||
crate::totp::TotpAlgorithm::HmacSha256 => backup::HashAlgorithms::SHA256,
|
algorithm: match raw_code.algorithm {
|
||||||
crate::totp::TotpAlgorithm::HmacSha512 => backup::HashAlgorithms::SHA512,
|
crate::totp::TotpAlgorithm::HmacSha1 => backup::HashAlgorithms::SHA1,
|
||||||
_ => panic!("invalid algorithm")
|
crate::totp::TotpAlgorithm::HmacSha256 => backup::HashAlgorithms::SHA256,
|
||||||
},
|
crate::totp::TotpAlgorithm::HmacSha512 => backup::HashAlgorithms::SHA512,
|
||||||
name: raw_code.name,
|
_ => panic!("invalid algorithm"),
|
||||||
});
|
},
|
||||||
|
name: raw_code.name,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok((&backup::TotpEntries(ret)).into())
|
||||||
|
}
|
||||||
|
backup::PayloadType::Password => {
|
||||||
|
let passwords: Vec<crate::storage::PasswordRecord> =
|
||||||
|
storage.all(crate::storage::ContentKind::Password)?;
|
||||||
|
|
||||||
|
let mut ret = vec![];
|
||||||
|
|
||||||
|
for raw_pass in passwords {
|
||||||
|
ret.push(backup::PasswordEntry {
|
||||||
|
description: raw_pass.description,
|
||||||
|
username: raw_pass.username,
|
||||||
|
password: raw_pass.password,
|
||||||
|
notes: raw_pass.notes,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok((&backup::PasswordEntries(ret)).into())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(backup::TotpEntries(ret).bytes())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn error_message(cid: ChannelID, error_code: u8) -> Message {
|
fn error_message(cid: ChannelID, error_code: u8) -> Message {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue