mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +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::{
|
||||
ctap::hid::{send::HidPacketIterator, ChannelID, Message},
|
||||
storage::{Error, TotpRecord},
|
||||
storage::{Error, PasswordRecord, TotpRecord},
|
||||
};
|
||||
|
||||
// Vault-specific command to upload TOTP codes
|
||||
|
|
@ -23,24 +23,30 @@ pub fn handle_vendor_command(
|
|||
let payload = match cmd {
|
||||
COMMAND_BENCHMARK => Message {
|
||||
cid: channel_id,
|
||||
cmd: cmd,
|
||||
cmd,
|
||||
payload: handle_benchmark(),
|
||||
},
|
||||
COMMAND_RESTORE_TOTP_CODES => match handle_restore(payload, &xns) {
|
||||
Ok(payload) => Message {
|
||||
cid: channel_id,
|
||||
cmd: cmd,
|
||||
payload: payload,
|
||||
cmd,
|
||||
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 {
|
||||
cid: channel_id,
|
||||
cmd: cmd,
|
||||
payload: payload,
|
||||
cmd,
|
||||
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),
|
||||
};
|
||||
|
|
@ -86,53 +92,100 @@ fn handle_restore(data: Vec<u8>, xns: &xous_names::XousNames) -> Result<Vec<u8>,
|
|||
|
||||
let c = cbor::read(&data)?;
|
||||
|
||||
let data = backup::TotpEntries::try_from(c)?;
|
||||
let data = backup::DataPacket::try_from(c)?;
|
||||
|
||||
for elem in data.0 {
|
||||
let totp = TotpRecord {
|
||||
version: 1,
|
||||
name: elem.name,
|
||||
secret: elem.shared_secret,
|
||||
algorithm: match elem.algorithm {
|
||||
backup::HashAlgorithms::SHA1 => crate::totp::TotpAlgorithm::HmacSha1,
|
||||
backup::HashAlgorithms::SHA256 => crate::totp::TotpAlgorithm::HmacSha256,
|
||||
backup::HashAlgorithms::SHA512 => crate::totp::TotpAlgorithm::HmacSha512,
|
||||
},
|
||||
digits: elem.digit_count,
|
||||
timestep: elem.step_seconds,
|
||||
ctime: 0, // Will be filled in later by storage::new_totp_record();
|
||||
notes: t!("vault.notes", xous::LANG).to_string(),
|
||||
};
|
||||
match data {
|
||||
backup::DataPacket::TOTP(totp_entries) => {
|
||||
for elem in totp_entries.0 {
|
||||
let totp = TotpRecord {
|
||||
version: 1,
|
||||
name: elem.name,
|
||||
secret: elem.shared_secret,
|
||||
algorithm: match elem.algorithm {
|
||||
backup::HashAlgorithms::SHA1 => crate::totp::TotpAlgorithm::HmacSha1,
|
||||
backup::HashAlgorithms::SHA256 => crate::totp::TotpAlgorithm::HmacSha256,
|
||||
backup::HashAlgorithms::SHA512 => crate::totp::TotpAlgorithm::HmacSha512,
|
||||
},
|
||||
digits: elem.digit_count,
|
||||
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])
|
||||
}
|
||||
|
||||
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 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 {
|
||||
ret.push(backup::TotpEntry {
|
||||
step_seconds: raw_code.timestep,
|
||||
shared_secret: raw_code.secret,
|
||||
digit_count: raw_code.digits,
|
||||
algorithm: match raw_code.algorithm {
|
||||
crate::totp::TotpAlgorithm::HmacSha1 => backup::HashAlgorithms::SHA1,
|
||||
crate::totp::TotpAlgorithm::HmacSha256 => backup::HashAlgorithms::SHA256,
|
||||
crate::totp::TotpAlgorithm::HmacSha512 => backup::HashAlgorithms::SHA512,
|
||||
_ => panic!("invalid algorithm")
|
||||
},
|
||||
name: raw_code.name,
|
||||
});
|
||||
let mut ret = vec![];
|
||||
|
||||
for raw_code in totp_codes {
|
||||
ret.push(backup::TotpEntry {
|
||||
step_seconds: raw_code.timestep,
|
||||
shared_secret: raw_code.secret,
|
||||
digit_count: raw_code.digits,
|
||||
algorithm: match raw_code.algorithm {
|
||||
crate::totp::TotpAlgorithm::HmacSha1 => backup::HashAlgorithms::SHA1,
|
||||
crate::totp::TotpAlgorithm::HmacSha256 => backup::HashAlgorithms::SHA256,
|
||||
crate::totp::TotpAlgorithm::HmacSha512 => backup::HashAlgorithms::SHA512,
|
||||
_ => 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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue