mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
feat(vault): implemet totp backup on device-side
This commit is contained in:
parent
bdeb68a158
commit
71979f81d7
1 changed files with 53 additions and 12 deletions
|
|
@ -4,12 +4,13 @@ use locales::t;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ctap::hid::{send::HidPacketIterator, ChannelID, Message},
|
ctap::hid::{send::HidPacketIterator, ChannelID, Message},
|
||||||
storage::TotpRecord,
|
storage::{Error, TotpRecord},
|
||||||
};
|
};
|
||||||
|
|
||||||
// Vault-specific command to upload TOTP codes
|
// Vault-specific command to upload TOTP codes
|
||||||
pub const COMMAND_RESTORE_TOTP_CODES: u8 = 0x41;
|
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(
|
pub fn handle_vendor_command(
|
||||||
cmd: u8,
|
cmd: u8,
|
||||||
|
|
@ -31,7 +32,15 @@ pub fn handle_vendor_command(
|
||||||
cmd: cmd,
|
cmd: cmd,
|
||||||
payload: payload,
|
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),
|
_ => error_message(channel_id, 0x33),
|
||||||
};
|
};
|
||||||
|
|
@ -44,25 +53,35 @@ fn handle_benchmark() -> Vec<u8> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum RestoreError {
|
enum BackupError {
|
||||||
CborError(DecoderError),
|
CborError(DecoderError),
|
||||||
CborConversionError(backup::CborConversionError),
|
CborConversionError(backup::CborConversionError),
|
||||||
PddbError(std::io::Error),
|
PddbError(std::io::Error),
|
||||||
|
StorageError(crate::storage::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<DecoderError> for RestoreError {
|
impl From<DecoderError> for BackupError {
|
||||||
fn from(de: DecoderError) -> Self {
|
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 {
|
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 mut storage = crate::storage::Manager::new(xns);
|
||||||
|
|
||||||
let c = cbor::read(&data)?;
|
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(),
|
notes: t!("vault.notes", xous::LANG).to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
storage
|
storage.new_totp_record(totp, None)?
|
||||||
.new_totp_record(totp)
|
|
||||||
.map_err(|err| RestoreError::PddbError(err))?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(vec![0xca, 0xfe, 0xba, 0xbe])
|
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 {
|
fn error_message(cid: ChannelID, error_code: u8) -> Message {
|
||||||
// This unwrap is safe because the payload length is 1 <= 7609 bytes.
|
// This unwrap is safe because the payload length is 1 <= 7609 bytes.
|
||||||
Message {
|
Message {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue