mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
chore(totp): abstract backup/restore models
This commit is contained in:
parent
d40763b80f
commit
42f0589010
3 changed files with 9 additions and 167 deletions
|
|
@ -42,9 +42,9 @@ impl TryFrom<cbor::Value> for HashAlgorithms {
|
|||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TotpEntry {
|
||||
pub step_seconds: u32,
|
||||
pub step_seconds: u64,
|
||||
pub shared_secret: String,
|
||||
pub digit_count: u64,
|
||||
pub digit_count: u32,
|
||||
pub algorithm: HashAlgorithms,
|
||||
pub name: String,
|
||||
}
|
||||
|
|
@ -80,12 +80,12 @@ impl TryFrom<cbor::Value> for TotpEntry {
|
|||
|
||||
let step_seconds = extract_unsigned(step_seconds.unwrap())?;
|
||||
let shared_secret = extract_string(shared_secret.unwrap())?;
|
||||
let digit_count = extract_unsigned(digit_count.unwrap())?;
|
||||
let digit_count = extract_unsigned(digit_count.unwrap())? as u32;
|
||||
let algorithm: HashAlgorithms = algorithm.unwrap().try_into()?;
|
||||
let name = extract_string(name.unwrap())?;
|
||||
|
||||
Ok(TotpEntry {
|
||||
step_seconds: step_seconds as u32,
|
||||
step_seconds,
|
||||
shared_secret,
|
||||
digit_count,
|
||||
algorithm,
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ edition = "2021"
|
|||
argh = "0.1.8"
|
||||
ctaphid = "0.1.1"
|
||||
cbor = {path = "../../libraries/cbor"}
|
||||
backup = {path = "../../libraries/backup"}
|
||||
hidapi = {version = "1.4.1", default_features = false}
|
||||
serde_json = "1.0.82"
|
||||
serde = {version = "1.0.138", features = ["derive"]}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,7 @@
|
|||
use argh::FromArgs;
|
||||
use cbor::{
|
||||
self, cbor_array_vec, cbor_int, cbor_key_int, cbor_map, cbor_unsigned, destructure_cbor_map,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{collections::BTreeMap, error::Error};
|
||||
use std::error::Error;
|
||||
|
||||
#[derive(FromArgs, PartialEq, Debug)]
|
||||
#[derive(FromArgs, PartialEq, Debug)]
|
||||
#[argh(description = "A tool to backup/restore TOTP settings for the vault app via USB.")]
|
||||
struct WithPositional {
|
||||
#[argh(option)]
|
||||
|
|
@ -42,133 +38,6 @@ impl TryInto<CLIAction> for WithPositional {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum CborConversionError {
|
||||
BadCbor,
|
||||
UnknownAlgorithm(u64),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
enum HashAlgorithms {
|
||||
SHA1,
|
||||
SHA256,
|
||||
SHA512,
|
||||
}
|
||||
|
||||
impl From<HashAlgorithms> for cbor::Value {
|
||||
fn from(te: HashAlgorithms) -> Self {
|
||||
match te {
|
||||
HashAlgorithms::SHA1 => cbor_unsigned!(1),
|
||||
HashAlgorithms::SHA256 => cbor_unsigned!(2),
|
||||
HashAlgorithms::SHA512 => cbor_unsigned!(3),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<cbor::Value> for HashAlgorithms {
|
||||
fn try_from(value: cbor::Value) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
cbor::Value::KeyValue(cbor::KeyType::Unsigned(unsigned)) => match unsigned {
|
||||
1 => Ok(HashAlgorithms::SHA1),
|
||||
2 => Ok(HashAlgorithms::SHA256),
|
||||
3 => Ok(HashAlgorithms::SHA512),
|
||||
other => Err(CborConversionError::UnknownAlgorithm(other)),
|
||||
},
|
||||
_ => Err(CborConversionError::BadCbor),
|
||||
}
|
||||
}
|
||||
|
||||
type Error = CborConversionError;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
struct TotpEntry {
|
||||
step_seconds: u64,
|
||||
shared_secret: String,
|
||||
digit_count: u64,
|
||||
algorithm: HashAlgorithms,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl From<TotpEntry> for cbor::Value {
|
||||
fn from(te: TotpEntry) -> Self {
|
||||
cbor_map! {
|
||||
cbor_key_int!(1) => te.step_seconds as i64,
|
||||
cbor_key_int!(2) => te.shared_secret,
|
||||
cbor_key_int!(3) => te.digit_count as i64,
|
||||
cbor_key_int!(4) => te.algorithm,
|
||||
cbor_key_int!(5) => te.name,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<cbor::Value> for TotpEntry {
|
||||
fn try_from(value: cbor::Value) -> Result<Self, Self::Error> {
|
||||
let rawmap = match value {
|
||||
cbor::Value::Map(m) => m,
|
||||
_ => return Err(CborConversionError::BadCbor),
|
||||
};
|
||||
|
||||
destructure_cbor_map! {
|
||||
let {
|
||||
1 => step_seconds,
|
||||
2 => shared_secret,
|
||||
3 => digit_count,
|
||||
4 => algorithm,
|
||||
5 => name,
|
||||
} = rawmap;
|
||||
}
|
||||
|
||||
let step_seconds = extract_unsigned(step_seconds.unwrap())?;
|
||||
let shared_secret = extract_string(shared_secret.unwrap())?;
|
||||
let digit_count = extract_unsigned(digit_count.unwrap())?;
|
||||
let algorithm: HashAlgorithms = algorithm.unwrap().try_into()?;
|
||||
let name = extract_string(name.unwrap())?;
|
||||
|
||||
Ok(TotpEntry {
|
||||
step_seconds,
|
||||
shared_secret,
|
||||
digit_count,
|
||||
algorithm,
|
||||
name,
|
||||
})
|
||||
}
|
||||
|
||||
type Error = CborConversionError;
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct TotpEntries(Vec<TotpEntry>);
|
||||
|
||||
impl From<&TotpEntries> for cbor::Value {
|
||||
fn from(te: &TotpEntries) -> Self {
|
||||
cbor_array_vec!(te.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<cbor::Value> for TotpEntries {
|
||||
fn try_from(value: cbor::Value) -> Result<Self, Self::Error> {
|
||||
let raw = extract_array(value)?;
|
||||
let mut ret = vec![];
|
||||
|
||||
for e in raw {
|
||||
ret.push(e.try_into().unwrap())
|
||||
}
|
||||
|
||||
Ok(Self(ret))
|
||||
}
|
||||
|
||||
type Error = CborConversionError;
|
||||
}
|
||||
|
||||
impl TotpEntries {
|
||||
fn bytes(&self) -> Vec<u8> {
|
||||
let mut ret = vec![];
|
||||
cbor::write(self.into(), &mut ret);
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum ProgramError {
|
||||
NoDevicesFound,
|
||||
|
|
@ -217,7 +86,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
let device = ctaphid::Device::connect(&ha, precursor.unwrap())?;
|
||||
|
||||
match argument {
|
||||
CLIAction::Backup(path) => todo!(),
|
||||
CLIAction::Backup(_) => todo!(),
|
||||
CLIAction::Restore(path) => {
|
||||
let hbf = read_human_backup_file(&path)?;
|
||||
let vcres = device.vendor_command(ctaphid::command::VendorCommand::H41, &hbf)?;
|
||||
|
|
@ -234,35 +103,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
fn read_human_backup_file(path: &str) -> Result<Vec<u8>, Box<dyn Error>> {
|
||||
let f = std::fs::File::open(path)?;
|
||||
|
||||
let backup_json: TotpEntries = serde_json::from_reader(f)?;
|
||||
let backup_json: backup::TotpEntries = serde_json::from_reader(f)?;
|
||||
|
||||
Ok(backup_json.bytes())
|
||||
}
|
||||
|
||||
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_array(cbor_value: cbor::Value) -> Result<Vec<cbor::Value>, CborConversionError> {
|
||||
match cbor_value {
|
||||
cbor::Value::Array(array) => Ok(array),
|
||||
_ => 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),
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue