Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IdCred: Support lengths up to 23 long KIDs #326

Merged
merged 5 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions shared/src/cred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,18 @@ impl From<u8> for IdCredType {
}
}

/// A value of ID_CRED_x: a credential identifier
/// A value of ID_CRED_x: a credential identifier.
///
/// Possible values include key IDs, credentials by value and others.
///
/// ```rust
/// # use hexlit::hex;
/// # use lakers_shared::IdCred;
/// let short_kid = IdCred::from_encoded_value(&hex!("17")).unwrap(); // 23
/// assert_eq!(short_kid.as_full_value(), &hex!("a1044117")); // {4: h'17'}
/// let long_kid = IdCred::from_encoded_value(&hex!("43616263")).unwrap(); // 'abc'
/// assert_eq!(long_kid.as_full_value(), &hex!("a10443616263")); // {4: 'abc'}
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[repr(C)]
pub struct IdCred {
Expand Down Expand Up @@ -73,8 +82,21 @@ impl IdCred {
BufferIdCred::new_from_slice(&[0xa1, KID_LABEL, 0x41, x])
.map_err(|_| EDHOCError::CredentialTooLongError)? // TODO: how to avoid map_err overuse?
}
// kid that has been encoded as CBOR byte string
&[0x41, x, ..] if !Self::bstr_representable_as_int(x) => {
// kid that has been encoded as CBOR byte string; supporting up to 23 long because
// those are easy
&[0x40..=0x57, ..] => {
let tail = &value[1..];
if let &[single_byte] = tail {
if Self::bstr_representable_as_int(single_byte) {
// We require precise encoding
return Err(EDHOCError::ParsingError);
}
}
if usize::from(value[0] - 0x40) != tail.len() {
// Missing or trailing bytes. This is impossible when called from within Lakers
// where the value is a `.any_as_encoded()`.
return Err(EDHOCError::ParsingError);
}
let mut bytes = BufferIdCred::new_from_slice(&[0xa1, KID_LABEL])
.map_err(|_| EDHOCError::CredentialTooLongError)?;
bytes
Expand All @@ -83,7 +105,7 @@ impl IdCred {
bytes
}
// CCS by value
&[0xa1, KCSS_LABEL, ..] => BufferIdCred::new_from_slice(value)
&[0xa1, KCCS_LABEL, ..] => BufferIdCred::new_from_slice(value)
.map_err(|_| EDHOCError::CredentialTooLongError)?,
_ => return Err(EDHOCError::ParsingError),
};
Expand Down Expand Up @@ -304,7 +326,7 @@ impl Credential {
let mut id_cred = IdCred::new();
id_cred
.bytes
.extend_from_slice(&[CBOR_MAJOR_MAP + 1, KCSS_LABEL])
.extend_from_slice(&[CBOR_MAJOR_MAP + 1, KCCS_LABEL])
.map_err(|_| EDHOCError::CredentialTooLongError)?;
id_cred
.bytes
Expand Down
4 changes: 3 additions & 1 deletion shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ pub const MAX_INFO_LEN: usize = 2 + SHA256_DIGEST_LEN + // 32-byte digest as bst
1 + MAX_KDF_CONTEXT_LEN + // context <24 bytes as bstr
1; // length as u8

pub const KCSS_LABEL: u8 = 14;
pub const KCCS_LABEL: u8 = 14;
#[deprecated(note = "Typo for KCCS_LABEL")]
pub const KCSS_LABEL: u8 = KCCS_LABEL;
pub const KID_LABEL: u8 = 4;

pub const ENC_STRUCTURE_LEN: usize = 8 + 5 + SHA256_DIGEST_LEN; // 8 for ENCRYPT0
Expand Down