From 99ba5fa075c483a7682722703d1d0ec181478995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gianguido=20Sor=C3=A0?= Date: Mon, 11 Jul 2022 15:07:34 +0200 Subject: [PATCH] chore: move backup/restore models into libraries subdir --- apps/vault/libraries/backup/Cargo.toml | 11 ++ apps/vault/libraries/backup/src/lib.rs | 157 +++++++++++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 apps/vault/libraries/backup/Cargo.toml create mode 100644 apps/vault/libraries/backup/src/lib.rs diff --git a/apps/vault/libraries/backup/Cargo.toml b/apps/vault/libraries/backup/Cargo.toml new file mode 100644 index 000000000..2b624c666 --- /dev/null +++ b/apps/vault/libraries/backup/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "backup" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +cbor = {path = "../cbor"} +serde = {version = "1.0.138", features = ["derive"]} +serde_json = "1.0.82" diff --git a/apps/vault/libraries/backup/src/lib.rs b/apps/vault/libraries/backup/src/lib.rs new file mode 100644 index 000000000..57e4bfad4 --- /dev/null +++ b/apps/vault/libraries/backup/src/lib.rs @@ -0,0 +1,157 @@ +use cbor::{self, cbor_array_vec, cbor_key_int, cbor_map, cbor_unsigned, destructure_cbor_map}; +use serde::{Deserialize, Serialize}; + +#[derive(Debug)] +pub enum CborConversionError { + BadCbor, + UnknownAlgorithm(u64), +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub 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)] +pub struct TotpEntry { + pub step_seconds: u32, + pub shared_secret: String, + pub digit_count: u64, + pub algorithm: HashAlgorithms, + pub 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: step_seconds as u32, + shared_secret, + digit_count, + algorithm, + name, + }) + } + + type Error = CborConversionError; +} + +#[derive(Serialize, Deserialize)] +pub struct TotpEntries(pub 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 { + pub fn bytes(&self) -> Vec { + let mut ret = vec![]; + cbor::write(self.into(), &mut ret); + ret + } +} + +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