mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
feat(vault): add Wire type for >7601 bytes of CTAPHID payload
This commit is contained in:
parent
5758b1d4c0
commit
2e50699c82
1 changed files with 170 additions and 16 deletions
|
|
@ -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<cbor::Value> 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<cbor::Value> for TotpEntry {
|
|||
type Error = CborConversionError;
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(Serialize, Deserialize, Default)]
|
||||
pub struct TotpEntries(pub Vec<TotpEntry>);
|
||||
|
||||
impl From<&TotpEntries> for cbor::Value {
|
||||
|
|
@ -228,6 +235,7 @@ impl TryFrom<cbor::Value> 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<Vec<u8>> for PayloadType {
|
||||
fn from(u: Vec<u8>) -> PayloadType {
|
||||
#[derive(Debug)]
|
||||
pub enum PayloadTypeError {
|
||||
BadType
|
||||
}
|
||||
|
||||
impl TryFrom<&Vec<u8>> for PayloadType {
|
||||
type Error = PayloadTypeError;
|
||||
fn try_from(u: &Vec<u8>) -> Result<PayloadType, Self::Error> {
|
||||
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<cbor::Value> for PayloadSize {
|
||||
type Error = CborConversionError;
|
||||
|
||||
fn try_from(value: cbor::Value) -> Result<Self, Self::Error> {
|
||||
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<u8> {
|
||||
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<u8>,
|
||||
}
|
||||
|
||||
impl Wire {
|
||||
const MAX_DATA: u16 = 7588;
|
||||
}
|
||||
|
||||
pub type Wires = Vec<Wire>;
|
||||
|
||||
impl From<DataPacket> for Wires {
|
||||
fn from(dp: DataPacket) -> Self {
|
||||
let mut ret = vec![];
|
||||
let bytes: Vec<u8> = 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<Vec<Wire>> for DataPacket {
|
||||
type Error = CborConversionError;
|
||||
|
||||
fn try_from(vw: Vec<Wire>) -> Result<Self, Self::Error> {
|
||||
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<u8> {
|
||||
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<cbor::Value> for Wire {
|
||||
type Error = CborConversionError;
|
||||
|
||||
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 => 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<bool, CborConversionError> {
|
||||
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<u64, CborConversionError> {
|
||||
match cbor_value {
|
||||
cbor::Value::KeyValue(cbor::KeyType::Unsigned(unsigned)) => Ok(unsigned),
|
||||
|
|
@ -341,12 +495,12 @@ fn extract_unsigned(cbor_value: cbor::Value) -> Result<u64, CborConversionError>
|
|||
}
|
||||
}
|
||||
|
||||
// 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_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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue