diff --git a/apps/vault/libraries/backup/src/lib.rs b/apps/vault/libraries/backup/src/lib.rs index da3b72565..4888fc4e5 100644 --- a/apps/vault/libraries/backup/src/lib.rs +++ b/apps/vault/libraries/backup/src/lib.rs @@ -1,13 +1,18 @@ -use std::fmt::Display; - use cbor::{self, cbor_array_vec, cbor_key_int, cbor_map, cbor_unsigned, destructure_cbor_map}; use serde::{Deserialize, Serialize}; +use std::fmt::Display; + +pub const CONTINUE_RESPONSE: &[u8] = &[42,43,44,45]; +pub const OKAY_CANARY: &[u8] = &[0xca, 0xfe, 0xba, 0xbe]; +pub const ERROR_VENDOR_HANDLING: u8 = 0x35; +pub const VENDOR_SESSION_ERROR: u8 = 0x36; #[derive(Debug)] pub enum CborConversionError { BadCbor, UnknownAlgorithm(u64), UnknownPayloadType(u8), + WrongPayloadSize, } impl Display for CborConversionError { @@ -16,14 +21,16 @@ impl Display for CborConversionError { CborConversionError::BadCbor => write!(f, "bad cbor"), CborConversionError::UnknownAlgorithm(algo) => write!(f, "unknown algorithm {}", algo), CborConversionError::UnknownPayloadType(pt) => write!(f, "unknown payload type {}", pt), + CborConversionError::WrongPayloadSize => write!(f, "wrong payload size"), } } } impl std::error::Error for CborConversionError {} -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Default)] pub enum HashAlgorithms { + #[default] SHA1, SHA256, SHA512, @@ -55,7 +62,7 @@ impl TryFrom for HashAlgorithms { type Error = CborConversionError; } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Default)] pub struct TotpEntry { pub step_seconds: u64, pub shared_secret: String, @@ -111,7 +118,7 @@ impl TryFrom for TotpEntry { type Error = CborConversionError; } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Default)] pub struct TotpEntries(pub Vec); impl From<&TotpEntries> for cbor::Value { @@ -228,6 +235,7 @@ impl TryFrom for PasswordEntries { type Error = CborConversionError; } +#[derive(Serialize, Deserialize)] pub enum DataPacket { Password(PasswordEntries), TOTP(TotpEntries), @@ -324,16 +332,162 @@ impl From<&PayloadType> for u8 { } } -impl From> for PayloadType { - fn from(u: Vec) -> PayloadType { +#[derive(Debug)] +pub enum PayloadTypeError { + BadType +} + +impl TryFrom<&Vec> for PayloadType { + type Error = PayloadTypeError; + fn try_from(u: &Vec) -> Result { match u[0] { - 1 => PayloadType::TOTP, - 2 => PayloadType::Password, - _ => PayloadType::TOTP, + 1 => Ok(PayloadType::TOTP), + 2 => Ok(PayloadType::Password), + _ => Err(PayloadTypeError::BadType), } } } +pub struct PayloadSize(pub u64); + +impl TryFrom for PayloadSize { + type Error = CborConversionError; + + fn try_from(value: cbor::Value) -> Result { + match extract_unsigned(value) { + Ok(value) => Ok(PayloadSize(value)), + Err(error) => Err(error), + } + } +} + +impl From<&PayloadSize> for cbor::Value { + fn from(te: &PayloadSize) -> Self { + cbor_map! { + cbor_key_int!(0) => te.0, + } + } +} + +impl From<&PayloadSize> for Vec { + fn from(te: &PayloadSize) -> Self { + let mut ret = vec![]; + let te_cbor: cbor::Value = te.into(); + cbor::write(te_cbor, &mut ret); + ret + } +} + +#[derive(Debug)] +pub struct Wire { + pub index: u64, + pub size: u64, + pub more_data: bool, + pub data: Vec, +} + +impl Wire { + const MAX_DATA: u16 = 7588; +} + +pub type Wires = Vec; + +impl From for Wires { + fn from(dp: DataPacket) -> Self { + let mut ret = vec![]; + let bytes: Vec = dp.into(); + + for (idx, chunk) in bytes.chunks(Wire::MAX_DATA.into()).into_iter().enumerate() { + ret.push(Wire { + index: idx as u64, + size: chunk.len() as u64, + more_data: true, + data: chunk.to_vec(), + }) + } + + if let Some(last) = ret.last_mut() { + last.more_data = false; + } + ret + } +} + +impl TryFrom> for DataPacket { + type Error = CborConversionError; + + fn try_from(vw: Vec) -> Result { + let mut orig_data = vec![]; + for chunk in vw { + orig_data.push(chunk.data[chunk.size as usize]); + } + + let dp = cbor::read(&orig_data).map_err(|_| CborConversionError::BadCbor)?; + + DataPacket::try_from(dp) + } +} + +impl From<&Wire> for cbor::Value { + fn from(te: &Wire) -> Self { + cbor_map! { + cbor_key_int!(1) => te.index, + cbor_key_int!(2) => te.size, + cbor_key_int!(3) => te.more_data, + cbor_key_int!(4) => te.data.clone(), + } + } +} + +impl From<&Wire> for Vec { + fn from(te: &Wire) -> Self { + let mut ret = vec![]; + let te_cbor: cbor::Value = te.into(); + cbor::write(te_cbor, &mut ret); + ret + } +} + +impl TryFrom for Wire { + type Error = CborConversionError; + + 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 => index, + 2 => size, + 3 => more_data, + 4 => data, + } = rawmap; + } + + let index = extract_unsigned(index.unwrap())?; + let size = extract_unsigned(size.unwrap())?; + let data = extract_byte_string(data.unwrap())?; + let more_data = extract_bool(more_data.unwrap())?; + + Ok(Self { + index, + more_data, + size, + data, + }) + } +} + +fn extract_bool(cbor_value: cbor::Value) -> Result { + match cbor_value { + cbor::Value::Simple(cbor::SimpleValue::FalseValue) => Ok(false), + cbor::Value::Simple(cbor::SimpleValue::TrueValue) => Ok(true), + _ => Err(CborConversionError::BadCbor), + } +} + fn extract_unsigned(cbor_value: cbor::Value) -> Result { match cbor_value { cbor::Value::KeyValue(cbor::KeyType::Unsigned(unsigned)) => Ok(unsigned), @@ -341,12 +495,12 @@ fn extract_unsigned(cbor_value: cbor::Value) -> Result } } -// 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_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 {