From 7b00f63d7447f70ef3d62133e55e62a78d6bfa18 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 27 Nov 2025 17:56:04 +0800 Subject: [PATCH] fix libraries to refer to local vendored-in version this allows us to tweak the libs locally without affecting the legacy stuff --- .../vault2/libraries/backup/Cargo.toml | 11 + .../vault2/libraries/backup/src/lib.rs | 531 ++++++++++++++++++ apps-baosec/vault2/libraries/cbor/Cargo.toml | 3 +- .../vault2/libraries/cbor/src/writer.rs | 2 +- .../vault2/libraries/crypto/Cargo.toml | 8 +- .../vault2/libraries/crypto/src/rng256.rs | 4 +- 6 files changed, 550 insertions(+), 9 deletions(-) create mode 100644 apps-baosec/vault2/libraries/backup/Cargo.toml create mode 100644 apps-baosec/vault2/libraries/backup/src/lib.rs diff --git a/apps-baosec/vault2/libraries/backup/Cargo.toml b/apps-baosec/vault2/libraries/backup/Cargo.toml new file mode 100644 index 000000000..961c20c3a --- /dev/null +++ b/apps-baosec/vault2/libraries/backup/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "backup-vault2" +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", package = "cbor-vault2" } +serde = { version = "1.0.138", features = ["derive"] } +serde_json = "1.0.82" diff --git a/apps-baosec/vault2/libraries/backup/src/lib.rs b/apps-baosec/vault2/libraries/backup/src/lib.rs new file mode 100644 index 000000000..0a2df5b0d --- /dev/null +++ b/apps-baosec/vault2/libraries/backup/src/lib.rs @@ -0,0 +1,531 @@ +use std::fmt::Display; + +use cbor::{self, cbor_array_vec, cbor_int, cbor_map, cbor_unsigned, destructure_cbor_map}; +use serde::{Deserialize, Serialize}; + +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 { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + 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)] +pub enum HashFromStrError { + UnknownHash, +} + +impl Display for HashFromStrError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + HashFromStrError::UnknownHash => write!(f, "unknown hash type"), + } + } +} + +impl std::error::Error for HashFromStrError {} + +#[derive(Debug, Clone, Serialize, Deserialize, Default)] +pub enum HashAlgorithms { + #[default] + 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 { + type Error = CborConversionError; + + fn try_from(value: cbor::Value) -> Result { + let v = extract_unsigned(value)?; + match v { + 1 => Ok(HashAlgorithms::SHA1), + 2 => Ok(HashAlgorithms::SHA256), + 3 => Ok(HashAlgorithms::SHA512), + _ => Err(CborConversionError::UnknownAlgorithm(v)), + } + } +} + +impl std::str::FromStr for HashAlgorithms { + type Err = HashFromStrError; + + fn from_str(input: &str) -> Result { + match input { + "SHA1" => Ok(HashAlgorithms::SHA1), + "SHA256" => Ok(HashAlgorithms::SHA256), + "SHA512" => Ok(HashAlgorithms::SHA512), + _ => Err(HashFromStrError::UnknownHash), + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize, Default)] +pub struct TotpEntry { + pub step_seconds: u64, + pub shared_secret: String, + pub digit_count: u32, + pub algorithm: HashAlgorithms, + pub name: String, + #[serde(default)] // if hotp is missing from the JSON representation, it's assumed to be false. + pub hotp: bool, +} + +impl From for cbor::Value { + fn from(te: TotpEntry) -> Self { + cbor_map! { + cbor_int!(1) => te.step_seconds as i64, + cbor_int!(2) => te.shared_secret, + cbor_int!(3) => te.digit_count as i64, + cbor_int!(4) => te.algorithm, + cbor_int!(5) => te.name, + } + } +} + +impl TryFrom for TotpEntry { + 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 => step_seconds, + 2 => shared_secret, + 3 => digit_count, + 4 => algorithm, + 5 => name, + 6 => hotp, + } = 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())? as u32; + let algorithm: HashAlgorithms = algorithm.unwrap().try_into()?; + let name = extract_string(name.unwrap())?; + let hotp = + extract_bool(hotp.unwrap_or(cbor::Value::Simple(cbor::SimpleValue::FalseValue))).unwrap_or(false); + + Ok(TotpEntry { step_seconds, shared_secret, digit_count, algorithm, name, hotp }) + } +} + +#[derive(Debug, Serialize, Deserialize, Default)] +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 { + type Error = CborConversionError; + + 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)) + } +} + +impl From<&TotpEntries> for Vec { + fn from(te: &TotpEntries) -> Self { + let mut ret = vec![]; + let te_cbor: cbor::Value = te.into(); + cbor::write(te_cbor, &mut ret).ok(); + ret + } +} + +#[derive(Default, Debug, Clone, Serialize, Deserialize)] +pub struct PasswordEntry { + pub description: String, + pub username: String, + pub password: String, + pub notes: String, +} + +impl From for cbor::Value { + fn from(te: PasswordEntry) -> Self { + cbor_map! { + cbor_int!(1) => te.description, + cbor_int!(2) => te.username, + cbor_int!(3) => te.password, + cbor_int!(4) => te.notes, + } + } +} + +impl TryFrom for PasswordEntry { + 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 => description, + 2 => username, + 3 => password, + 4 => notes, + } = rawmap; + } + + let description = extract_string(description.unwrap())?; + let username = extract_string(username.unwrap())?; + let password = extract_string(password.unwrap())?; + let notes = extract_string(notes.unwrap())?; + + Ok(PasswordEntry { description, username, password, notes }) + } +} + +#[derive(Debug, Serialize, Deserialize, Default)] +pub struct PasswordEntries(pub Vec); + +impl From<&PasswordEntries> for cbor::Value { + fn from(te: &PasswordEntries) -> Self { cbor_array_vec!(te.0.clone()) } +} + +impl From<&PasswordEntries> for Vec { + fn from(te: &PasswordEntries) -> Self { + let mut ret = vec![]; + let te_cbor: cbor::Value = te.into(); + cbor::write(te_cbor, &mut ret).ok(); + ret + } +} + +impl TryFrom for PasswordEntries { + type Error = CborConversionError; + + 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)) + } +} + +#[derive(Debug, Serialize, Deserialize)] +pub enum DataPacket { + Password(PasswordEntries), + TOTP(TotpEntries), +} + +impl DataPacket { + fn type_to_int(&self) -> u64 { + match self { + DataPacket::Password(_) => 1, + DataPacket::TOTP(_) => 2, + } + } +} + +impl From for Vec { + fn from(dp: DataPacket) -> Self { + let mut ret = vec![]; + let te_cbor: cbor::Value = dp.into(); + cbor::write(te_cbor, &mut ret).ok(); + ret + } +} + +impl From for cbor::Value { + fn from(dp: DataPacket) -> Self { + let dpt = dp.type_to_int(); + match dp { + DataPacket::Password(ref p) => { + cbor_map! { + cbor_int!(1) => dpt, + cbor_int!(2) => p, + } + } + DataPacket::TOTP(ref t) => { + cbor_map! { + cbor_int!(1) => dpt, + cbor_int!(2) => t, + } + } + } + } +} + +impl TryFrom for DataPacket { + 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 => data_packet_type, + 2 => shared_secret, + } = rawmap; + } + + let data_packet_type = extract_unsigned(data_packet_type.unwrap())?; + let shared_secret = shared_secret.unwrap(); + let dp = match data_packet_type { + 1 => { + // DataPacket::Password + let pes: PasswordEntries = shared_secret.try_into()?; + DataPacket::Password(pes) + } + 2 => { + // DataPacket::TOTP + let pes: TotpEntries = shared_secret.try_into()?; + DataPacket::TOTP(pes) + } + others => panic!("cannot convert from data packet type {} from cbor::Value!", others), + }; + + Ok(dp) + } +} + +pub enum PayloadType { + TOTP, + Password, +} + +impl From<&PayloadType> for u8 { + fn from(t: &PayloadType) -> u8 { + match t { + PayloadType::TOTP => 1, + PayloadType::Password => 2, + } + } +} + +#[derive(Debug)] +pub enum PayloadTypeError { + BadType, +} + +impl TryFrom<&Vec> for PayloadType { + type Error = PayloadTypeError; + + fn try_from(u: &Vec) -> Result { + if u.is_empty() { + return Err(PayloadTypeError::BadType); + } + + match u[0] { + 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_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).ok(); + 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_int!(1) => te.index, + cbor_int!(2) => te.size, + cbor_int!(3) => te.more_data, + cbor_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).ok(); + 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::Unsigned(unsigned) => Ok(unsigned), + _ => Err(CborConversionError::BadCbor), + } +} + +fn extract_byte_string(cbor_value: cbor::Value) -> Result, CborConversionError> { + match cbor_value { + cbor::Value::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::TextString(text_string) => Ok(text_string), + _ => Err(CborConversionError::BadCbor), + } +} diff --git a/apps-baosec/vault2/libraries/cbor/Cargo.toml b/apps-baosec/vault2/libraries/cbor/Cargo.toml index f8a71f33d..16bdfaf96 100644 --- a/apps-baosec/vault2/libraries/cbor/Cargo.toml +++ b/apps-baosec/vault2/libraries/cbor/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "cbor" +name = "cbor-vault2" version = "0.1.0" authors = [ "Fabian Kaczmarczyck ", @@ -12,4 +12,3 @@ edition = "2018" [dependencies] [features] - diff --git a/apps-baosec/vault2/libraries/cbor/src/writer.rs b/apps-baosec/vault2/libraries/cbor/src/writer.rs index 2776f99a3..0e7328e81 100644 --- a/apps-baosec/vault2/libraries/cbor/src/writer.rs +++ b/apps-baosec/vault2/libraries/cbor/src/writer.rs @@ -49,7 +49,7 @@ struct Writer<'a> { } impl<'a> Writer<'a> { - pub fn new(encoded_cbor: &mut Vec) -> Writer { + pub fn new(encoded_cbor: &mut Vec) -> Writer<'_> { Writer { encoded_cbor } } diff --git a/apps-baosec/vault2/libraries/crypto/Cargo.toml b/apps-baosec/vault2/libraries/crypto/Cargo.toml index 4346cd660..fe3e3b2d7 100644 --- a/apps-baosec/vault2/libraries/crypto/Cargo.toml +++ b/apps-baosec/vault2/libraries/crypto/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "ctap-crypto" +name = "ctap-crypto-vault2" version = "0.1.0" authors = [ "Fabian Kaczmarczyck ", @@ -10,10 +10,10 @@ license = "Apache-2.0" edition = "2018" [dependencies] -cbor = { path = "../cbor" } +cbor = { path = "../cbor", package = "cbor-vault2" } arrayref = "0.3.6" subtle = { version = "2.2.3", default-features = false } -trng = { path = "../../../../services/trng" } +bao1x-hal-service = { path = "../../../../services/bao1x-hal-service" } xous-names = { package = "xous-api-names", version = "0.9.67" } rand_core = "0.6.3" p256 = { version = "0.11.1", default-features = false, features = [ @@ -30,7 +30,7 @@ log = "0.4.14" # xous debugging [target.'cfg(any(windows,unix))'.dependencies] rand = "0.8.5" rand_chacha = "0.3.1" -cbor = { path = "../cbor" } +cbor = { path = "../cbor", package = "cbor-vault2" } regex = { version = "1" } untrusted = { version = "0.7.0" } hex = { version = "0.3.2", default-features = false } # do not update this, it's required to be at this version for tests to work. diff --git a/apps-baosec/vault2/libraries/crypto/src/rng256.rs b/apps-baosec/vault2/libraries/crypto/src/rng256.rs index cc1fdb442..6017a238e 100644 --- a/apps-baosec/vault2/libraries/crypto/src/rng256.rs +++ b/apps-baosec/vault2/libraries/crypto/src/rng256.rs @@ -38,12 +38,12 @@ fn bytes_to_u32(bytes: [u8; 32]) -> [u32; 8] { } pub struct XousRng256 { - trng: trng::Trng, + trng: bao1x_hal_service::trng::Trng, } impl XousRng256 { pub fn new(xns: &xous_names::XousNames) -> Self { - XousRng256 { trng: trng::Trng::new(xns).unwrap() } + XousRng256 { trng: bao1x_hal_service::trng::Trng::new(xns).unwrap() } } } impl Rng256 for XousRng256 {