feat(vault): implemet totp backup on device-side

This commit is contained in:
Gianguido Sorà 2022-07-12 17:30:26 +02:00
parent bdeb68a158
commit 71979f81d7

View file

@ -4,12 +4,13 @@ use locales::t;
use crate::{
ctap::hid::{send::HidPacketIterator, ChannelID, Message},
storage::TotpRecord,
storage::{Error, TotpRecord},
};
// Vault-specific command to upload TOTP codes
pub const COMMAND_RESTORE_TOTP_CODES: u8 = 0x41;
pub const COMMAND_BENCHMARK: u8 = 0x42;
pub const COMMAND_BACKUP_TOTP_CODES: u8 = 0x42;
pub const COMMAND_BENCHMARK: u8 = 0x43;
pub fn handle_vendor_command(
cmd: u8,
@ -31,7 +32,15 @@ pub fn handle_vendor_command(
cmd: cmd,
payload: payload,
},
Err(_) => error_message(channel_id, 45),
Err(_) => error_message(channel_id, 41),
},
COMMAND_BACKUP_TOTP_CODES => match handle_backup(&xns) {
Ok(payload) => Message {
cid: channel_id,
cmd: cmd,
payload: payload,
},
Err(_) => error_message(channel_id, 42),
},
_ => error_message(channel_id, 0x33),
};
@ -44,25 +53,35 @@ fn handle_benchmark() -> Vec<u8> {
}
#[derive(Debug)]
enum RestoreError {
enum BackupError {
CborError(DecoderError),
CborConversionError(backup::CborConversionError),
PddbError(std::io::Error),
StorageError(crate::storage::Error),
}
impl From<DecoderError> for RestoreError {
impl From<DecoderError> for BackupError {
fn from(de: DecoderError) -> Self {
RestoreError::CborError(de)
BackupError::CborError(de)
}
}
impl From<backup::CborConversionError> for RestoreError {
impl From<crate::storage::Error> for BackupError {
fn from(e: crate::storage::Error) -> Self {
match e {
Error::IoError(pe) => Self::PddbError(pe),
generic => Self::StorageError(generic),
}
}
}
impl From<backup::CborConversionError> for BackupError {
fn from(cbe: backup::CborConversionError) -> Self {
RestoreError::CborConversionError(cbe)
BackupError::CborConversionError(cbe)
}
}
fn handle_restore(data: Vec<u8>, xns: &xous_names::XousNames) -> Result<Vec<u8>, RestoreError> {
fn handle_restore(data: Vec<u8>, xns: &xous_names::XousNames) -> Result<Vec<u8>, BackupError> {
let mut storage = crate::storage::Manager::new(xns);
let c = cbor::read(&data)?;
@ -85,14 +104,36 @@ fn handle_restore(data: Vec<u8>, xns: &xous_names::XousNames) -> Result<Vec<u8>,
notes: t!("vault.notes", xous::LANG).to_string(),
};
storage
.new_totp_record(totp)
.map_err(|err| RestoreError::PddbError(err))?;
storage.new_totp_record(totp, None)?
}
Ok(vec![0xca, 0xfe, 0xba, 0xbe])
}
fn handle_backup(xns: &xous_names::XousNames) -> Result<Vec<u8>, BackupError> {
let mut storage = crate::storage::Manager::new(xns);
let totp_codes = storage.all_totp()?;
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,
},
name: raw_code.name,
});
}
Ok(backup::TotpEntries(ret).bytes())
}
fn error_message(cid: ChannelID, error_code: u8) -> Message {
// This unwrap is safe because the payload length is 1 <= 7609 bytes.
Message {