mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
chore(vault): use backup structs, storage abstraction in vendor command handling
This commit is contained in:
parent
8b4eca753e
commit
9722bbc573
1 changed files with 30 additions and 106 deletions
|
|
@ -1,16 +1,10 @@
|
||||||
use cbor::{destructure_cbor_map, reader::DecoderError};
|
use cbor::reader::DecoderError;
|
||||||
use chrono::{DateTime, NaiveDateTime, Utc};
|
use core::convert::TryFrom;
|
||||||
use locales::t;
|
use locales::t;
|
||||||
use pddb::Pddb;
|
|
||||||
use std::{time::{SystemTime, UNIX_EPOCH}, io::Write};
|
|
||||||
|
|
||||||
const VAULT_TOTP_DICT: &'static str = "vault.totp";
|
|
||||||
/// bytes to reserve for a key entry. Making this slightly larger saves on some churn as stuff gets updated
|
|
||||||
const VAULT_TOTP_ALLOC_HINT: usize = 256;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
actions::{TotpRecord, serialize_totp},
|
ctap::hid::{send::HidPacketIterator, ChannelID, Message},
|
||||||
ctap::hid::{send::HidPacketIterator, ChannelID, CtapHid, Message},
|
storage::TotpRecord,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Vault-specific command to upload TOTP codes
|
// Vault-specific command to upload TOTP codes
|
||||||
|
|
@ -24,7 +18,6 @@ pub fn handle_vendor_command(
|
||||||
) -> HidPacketIterator {
|
) -> HidPacketIterator {
|
||||||
log::debug!("got vendor command: {}, payload: {:?}", cmd, payload);
|
log::debug!("got vendor command: {}, payload: {:?}", cmd, payload);
|
||||||
let xns = xous_names::XousNames::new().unwrap();
|
let xns = xous_names::XousNames::new().unwrap();
|
||||||
let mut trng = trng::Trng::new(&xns).unwrap();
|
|
||||||
|
|
||||||
let payload = match cmd {
|
let payload = match cmd {
|
||||||
COMMAND_BENCHMARK => Message {
|
COMMAND_BENCHMARK => Message {
|
||||||
|
|
@ -32,7 +25,7 @@ pub fn handle_vendor_command(
|
||||||
cmd: cmd,
|
cmd: cmd,
|
||||||
payload: handle_benchmark(),
|
payload: handle_benchmark(),
|
||||||
},
|
},
|
||||||
COMMAND_RESTORE_TOTP_CODES => match handle_restore(payload, &mut trng) {
|
COMMAND_RESTORE_TOTP_CODES => match handle_restore(payload, &xns) {
|
||||||
Ok(payload) => Message {
|
Ok(payload) => Message {
|
||||||
cid: channel_id,
|
cid: channel_id,
|
||||||
cmd: cmd,
|
cmd: cmd,
|
||||||
|
|
@ -53,8 +46,7 @@ fn handle_benchmark() -> Vec<u8> {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum RestoreError {
|
enum RestoreError {
|
||||||
CborError(DecoderError),
|
CborError(DecoderError),
|
||||||
NotAnArray,
|
CborConversionError(backup::CborConversionError),
|
||||||
NotAMap,
|
|
||||||
PddbError(std::io::Error),
|
PddbError(std::io::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -64,64 +56,38 @@ impl From<DecoderError> for RestoreError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_restore(data: Vec<u8>, trng: &mut trng::Trng) -> Result<Vec<u8>, RestoreError> {
|
impl From<backup::CborConversionError> for RestoreError {
|
||||||
// Unmarshal bytes to cbor map
|
fn from(cbe: backup::CborConversionError) -> Self {
|
||||||
|
RestoreError::CborConversionError(cbe)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_restore(data: Vec<u8>, xns: &xous_names::XousNames) -> Result<Vec<u8>, RestoreError> {
|
||||||
|
let mut storage = crate::storage::Manager::new(xns);
|
||||||
|
|
||||||
let c = cbor::read(&data)?;
|
let c = cbor::read(&data)?;
|
||||||
|
|
||||||
let array_c = match c {
|
let data = backup::TotpEntries::try_from(c)?;
|
||||||
cbor::Value::Array(array) => array,
|
|
||||||
_ => return Err(RestoreError::NotAnArray),
|
|
||||||
};
|
|
||||||
|
|
||||||
let pddb = Pddb::new();
|
|
||||||
|
|
||||||
for elem in array_c {
|
|
||||||
let rawmap = match elem {
|
|
||||||
cbor::Value::Map(m) => m,
|
|
||||||
_ => return Err(RestoreError::NotAMap),
|
|
||||||
};
|
|
||||||
|
|
||||||
destructure_cbor_map! {
|
|
||||||
let {
|
|
||||||
1 => step_seconds,
|
|
||||||
2 => shared_secret,
|
|
||||||
3 => digit_count,
|
|
||||||
4 => algorithm,
|
|
||||||
5 => name,
|
|
||||||
} = rawmap;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
for elem in data.0 {
|
||||||
let totp = TotpRecord {
|
let totp = TotpRecord {
|
||||||
version: 1,
|
version: 1,
|
||||||
name: extract_string(name.unwrap()).unwrap(),
|
name: elem.name,
|
||||||
secret: extract_string(shared_secret.unwrap()).unwrap(),
|
secret: elem.shared_secret,
|
||||||
algorithm: crate::totp::TotpAlgorithm::HmacSha1,
|
algorithm: match elem.algorithm {
|
||||||
digits: extract_unsigned(digit_count.unwrap()).unwrap() as u32,
|
backup::HashAlgorithms::SHA1 => crate::totp::TotpAlgorithm::HmacSha1,
|
||||||
timestep: extract_unsigned(step_seconds.unwrap()).unwrap(),
|
backup::HashAlgorithms::SHA256 => crate::totp::TotpAlgorithm::HmacSha256,
|
||||||
ctime: utc_now().timestamp() as u64,
|
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(),
|
notes: t!("vault.notes", xous::LANG).to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let ser = serialize_totp(&totp);
|
storage
|
||||||
let guid = gen_guid(trng);
|
.new_totp_record(totp)
|
||||||
log::debug!("storing into guid: {}", guid);
|
.map_err(|err| RestoreError::PddbError(err))?;
|
||||||
match pddb.get(
|
|
||||||
VAULT_TOTP_DICT,
|
|
||||||
&guid,
|
|
||||||
None,
|
|
||||||
true,
|
|
||||||
true,
|
|
||||||
Some(VAULT_TOTP_ALLOC_HINT),
|
|
||||||
Some(crate::basis_change),
|
|
||||||
) {
|
|
||||||
Ok(mut data) => match data.write(&ser) {
|
|
||||||
Ok(len) => log::debug!("wrote {} bytes", len),
|
|
||||||
Err(e) => return Err(RestoreError::PddbError(e))
|
|
||||||
},
|
|
||||||
Err(e) => return Err(RestoreError::PddbError(e)),
|
|
||||||
}
|
|
||||||
log::debug!("syncing...");
|
|
||||||
pddb.sync().ok();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(vec![0xca, 0xfe, 0xba, 0xbe])
|
Ok(vec![0xca, 0xfe, 0xba, 0xbe])
|
||||||
|
|
@ -135,45 +101,3 @@ fn error_message(cid: ChannelID, error_code: u8) -> Message {
|
||||||
payload: vec![error_code],
|
payload: vec![error_code],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
enum CborConversionError {
|
|
||||||
BadCbor,
|
|
||||||
UnknownAlgorithm(u64),
|
|
||||||
}
|
|
||||||
|
|
||||||
fn extract_unsigned(cbor_value: cbor::Value) -> Result<u64, CborConversionError> {
|
|
||||||
match cbor_value {
|
|
||||||
cbor::Value::KeyValue(cbor::KeyType::Unsigned(unsigned)) => Ok(unsigned),
|
|
||||||
_ => Err(CborConversionError::BadCbor),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn extract_byte_string(cbor_value: cbor::Value) -> Result<Vec<u8>, CborConversionError> {
|
|
||||||
match cbor_value {
|
|
||||||
cbor::Value::KeyValue(cbor::KeyType::ByteString(byte_string)) => Ok(byte_string),
|
|
||||||
_ => Err(CborConversionError::BadCbor),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn extract_string(cbor_value: cbor::Value) -> Result<String, CborConversionError> {
|
|
||||||
match cbor_value {
|
|
||||||
cbor::Value::KeyValue(cbor::KeyType::TextString(string)) => Ok(string),
|
|
||||||
_ => Err(CborConversionError::BadCbor),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// because we don't get Utc::now, as the crate checks your architecture and xous is not recognized as a valid target
|
|
||||||
fn utc_now() -> DateTime<Utc> {
|
|
||||||
let now = SystemTime::now()
|
|
||||||
.duration_since(UNIX_EPOCH)
|
|
||||||
.expect("system time before Unix epoch");
|
|
||||||
let naive = NaiveDateTime::from_timestamp(now.as_secs() as i64, now.subsec_nanos() as u32);
|
|
||||||
DateTime::from_utc(naive, Utc)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn gen_guid(trng: &mut trng::Trng) -> String {
|
|
||||||
let mut guid = [0u8; 16];
|
|
||||||
trng.fill_bytes(&mut guid);
|
|
||||||
hex::encode(guid)
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue