pub const INTEGER: u8 = 0x02;
pub const BIT_STRING: u8 = 0x03;
pub const OCTET_STRING: u8 = 0x04;
pub const OID: u8 = 0x06;
pub const SEQUENCE: u8 = 0x30;
pub const SET: u8 = 0x31;
pub fn next(input: &[u8]) -> Option<(u8, &[u8], &[u8])> {
let (&tag, after_tag) = input.split_first()?;
let (&first_len, after_first) = after_tag.split_first()?;
let (len, body) = if first_len & 0x80 == 0 {
(first_len as usize, after_first)
} else {
let n = (first_len & 0x7f) as usize;
if n == 0 || n > 4 || after_first.len() < n {
return None;
}
let len_bytes = &after_first[..n];
if len_bytes[0] == 0 {
return None;
}
let mut len = 0usize;
for &b in len_bytes {
len = len.checked_mul(256)?.checked_add(b as usize)?;
}
if len < 0x80 {
return None;
}
(len, &after_first[n..])
};
if body.len() < len {
return None;
}
Some((tag, &body[..len], &body[len..]))
}
pub fn take(input: &[u8], tag: u8) -> Option<(&[u8], &[u8])> {
let (t, contents, rest) = next(input)?;
(t == tag).then_some((contents, rest))
}
pub fn expect(input: &[u8], tag: u8) -> Option<&[u8]> {
let (t, contents, rest) = next(input)?;
(t == tag && rest.is_empty()).then_some(contents)
}
pub fn oid_arcs(contents: &[u8]) -> Option<Vec<u64>> {
let mut subids = Vec::new();
let mut value: u64 = 0;
let mut pending = false;
let mut fresh = true; for &b in contents {
if fresh && b == 0x80 {
return None;
}
fresh = b & 0x80 == 0;
value = value.checked_mul(128)?.checked_add((b & 0x7f) as u64)?;
if b & 0x80 == 0 {
subids.push(value);
value = 0;
pending = false;
} else {
pending = true;
}
}
if pending {
return None;
}
let first = *subids.first()?;
let (arc1, arc2) = if first < 80 {
(first / 40, first % 40)
} else {
(2, first - 80)
};
let mut arcs = vec![arc1, arc2];
arcs.extend_from_slice(&subids[1..]);
Some(arcs)
}
pub fn bit_string_bytes(contents: &[u8]) -> Option<&[u8]> {
let (&unused, bits) = contents.split_first()?;
(unused == 0).then_some(bits)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_short_form() {
let (tag, contents, rest) = next(&[0x02, 0x02, 0x00, 0xca, 0xff]).unwrap();
assert_eq!(tag, INTEGER);
assert_eq!(contents, &[0x00, 0xca]);
assert_eq!(rest, &[0xff]);
}
#[test]
fn reads_long_form() {
let mut input = vec![SEQUENCE, 0x82, 0x01, 0x00]; input.extend(std::iter::repeat_n(0xAA, 256));
let (tag, contents, rest) = next(&input).unwrap();
assert_eq!(tag, SEQUENCE);
assert_eq!(contents.len(), 256);
assert!(rest.is_empty());
}
#[test]
fn a_length_running_past_the_buffer_is_rejected_not_panicked() {
assert!(next(&[0x30, 0x7f, 0x01]).is_none()); assert!(next(&[0x30, 0x84, 0xff, 0xff, 0xff, 0xff]).is_none()); assert!(next(&[0x30]).is_none()); assert!(next(&[]).is_none());
assert!(next(&[0x30, 0x80]).is_none()); }
#[test]
fn non_minimal_lengths_are_rejected_as_ber_not_der() {
assert!(next(&[0x04, 0x81, 0x01, 0xAA]).is_none());
let mut ber = vec![0x04, 0x82, 0x00, 0x80];
ber.extend(std::iter::repeat_n(0xAA, 0x80));
assert!(next(&ber).is_none());
let mut der = vec![0x04, 0x7f];
der.extend(std::iter::repeat_n(0xAA, 0x7f));
assert!(next(&der).is_some());
}
#[test]
fn expect_rejects_trailing_bytes() {
assert!(expect(&[0x02, 0x01, 0x05], INTEGER).is_some());
assert!(expect(&[0x02, 0x01, 0x05, 0x02, 0x01, 0x06], INTEGER).is_none());
}
#[test]
fn decodes_oids() {
let sha256 = [0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01];
assert_eq!(
oid_arcs(&sha256).unwrap(),
vec![2, 16, 840, 1, 101, 3, 4, 2, 1]
);
let sha1 = [0x2b, 0x0e, 0x03, 0x02, 0x1a];
assert_eq!(oid_arcs(&sha1).unwrap(), vec![1, 3, 14, 3, 2, 26]);
assert!(oid_arcs(&[0x2b, 0x80]).is_none());
}
#[test]
fn decodes_a_multi_byte_first_subidentifier() {
let oid = [0x81, 0x34, 0x03];
assert_eq!(oid_arcs(&oid).unwrap(), vec![2, 100, 3]);
}
#[test]
fn non_minimal_base128_oid_subidentifiers_are_rejected() {
assert!(oid_arcs(&[0x80, 0x01]).is_none());
assert!(oid_arcs(&[0x55, 0x80, 0x03]).is_none());
assert_eq!(oid_arcs(&[0x55, 0x03]).unwrap(), vec![2, 5, 3]);
}
#[test]
fn unwraps_bit_strings() {
assert_eq!(
bit_string_bytes(&[0x00, 0xde, 0xad]).unwrap(),
&[0xde, 0xad]
);
assert!(bit_string_bytes(&[0x03, 0xde]).is_none()); assert!(bit_string_bytes(&[]).is_none());
}
}