From 42f0589010715c2436f7beec152cfc418f576c60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gianguido=20Sor=C3=A0?= Date: Mon, 11 Jul 2022 18:14:25 +0200 Subject: [PATCH] chore(totp): abstract backup/restore models --- apps/vault/libraries/backup/src/lib.rs | 8 +- apps/vault/tools/totpbackup-rs/Cargo.toml | 1 + apps/vault/tools/totpbackup-rs/src/main.rs | 167 +-------------------- 3 files changed, 9 insertions(+), 167 deletions(-) diff --git a/apps/vault/libraries/backup/src/lib.rs b/apps/vault/libraries/backup/src/lib.rs index 57e4bfad4..a250fbc86 100644 --- a/apps/vault/libraries/backup/src/lib.rs +++ b/apps/vault/libraries/backup/src/lib.rs @@ -42,9 +42,9 @@ impl TryFrom 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 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, diff --git a/apps/vault/tools/totpbackup-rs/Cargo.toml b/apps/vault/tools/totpbackup-rs/Cargo.toml index 34e6851c8..54788f242 100644 --- a/apps/vault/tools/totpbackup-rs/Cargo.toml +++ b/apps/vault/tools/totpbackup-rs/Cargo.toml @@ -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"]} diff --git a/apps/vault/tools/totpbackup-rs/src/main.rs b/apps/vault/tools/totpbackup-rs/src/main.rs index e1f487c28..5dc1cec12 100644 --- a/apps/vault/tools/totpbackup-rs/src/main.rs +++ b/apps/vault/tools/totpbackup-rs/src/main.rs @@ -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 for WithPositional { } } -#[derive(Debug)] -enum CborConversionError { - BadCbor, - UnknownAlgorithm(u64), -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -enum HashAlgorithms { - SHA1, - SHA256, - SHA512, -} - -impl From 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 for HashAlgorithms { - fn try_from(value: cbor::Value) -> Result { - 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 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 for TotpEntry { - fn try_from(value: cbor::Value) -> Result { - 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); - -impl From<&TotpEntries> for cbor::Value { - fn from(te: &TotpEntries) -> Self { - cbor_array_vec!(te.0.clone()) - } -} - -impl TryFrom for TotpEntries { - fn try_from(value: cbor::Value) -> Result { - 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 { - let mut ret = vec![]; - cbor::write(self.into(), &mut ret); - ret - } -} - #[derive(Debug)] enum ProgramError { NoDevicesFound, @@ -217,7 +86,7 @@ fn main() -> Result<(), Box> { 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> { fn read_human_backup_file(path: &str) -> Result, Box> { 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 { - match cbor_value { - cbor::Value::KeyValue(cbor::KeyType::Unsigned(unsigned)) => Ok(unsigned), - _ => Err(CborConversionError::BadCbor), - } -} - -fn extract_byte_string(cbor_value: cbor::Value) -> Result, 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, CborConversionError> { - match cbor_value { - cbor::Value::Array(array) => Ok(array), - _ => Err(CborConversionError::BadCbor), - } -} - -fn extract_string(cbor_value: cbor::Value) -> Result { - match cbor_value { - cbor::Value::KeyValue(cbor::KeyType::TextString(string)) => Ok(string), - _ => Err(CborConversionError::BadCbor), - } } \ No newline at end of file