use crate::error::{Error, ErrorCode, Result};
pub const TAG_BOOLEAN: u8 = 0x01;
pub const TAG_INTEGER: u8 = 0x02;
pub const TAG_BIT_STRING: u8 = 0x03;
pub const TAG_OCTET_STRING: u8 = 0x04;
pub const TAG_NULL: u8 = 0x05;
pub const TAG_OID: u8 = 0x06;
pub const TAG_UTF8_STRING: u8 = 0x0C;
pub const TAG_PRINTABLE_STRING: u8 = 0x13;
pub const TAG_IA5_STRING: u8 = 0x16;
pub const TAG_UTC_TIME: u8 = 0x17;
pub const TAG_GENERALIZED_TIME: u8 = 0x18;
pub const TAG_SEQUENCE: u8 = 0x30;
pub const TAG_SET: u8 = 0x31;
#[must_use]
pub const fn context_constructed(number: u8) -> u8 {
0xA0 | (number & 0x1F)
}
#[must_use]
pub const fn context_primitive(number: u8) -> u8 {
0x80 | (number & 0x1F)
}
pub const OID_ECDSA_WITH_SHA256: &[u8] = &[0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02];
pub const OID_EC_PUBLIC_KEY: &[u8] = &[0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01];
pub const OID_PRIME256V1: &[u8] = &[0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07];
pub const OID_SHA256: &[u8] = &[0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01];
pub const OID_SIGNED_DATA: &[u8] = &[0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02];
pub const OID_PKCS7_DATA: &[u8] = &[0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01];
pub const OID_EXTENSION_REQUEST: &[u8] = &[0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x0E];
pub const OID_ORGANIZATION_NAME: &[u8] = &[0x55, 0x04, 0x0A];
pub const OID_COMMON_NAME: &[u8] = &[0x55, 0x04, 0x03];
pub const OID_CE_PREFIX: &[u8] = &[0x55, 0x1D];
pub const OID_KP_PREFIX: &[u8] = &[0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03];
pub const OID_MATTER_PREFIX: &[u8] = &[0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0xA2, 0x7C];
pub const OID_MATTER_DN_PREFIX: &[u8] = &[0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0xA2, 0x7C, 0x01];
pub const OID_MATTER_ATTESTATION_PREFIX: &[u8] =
&[0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0xA2, 0x7C, 0x02];
pub const OID_DOMAIN_COMPONENT: &[u8] =
&[0x09, 0x92, 0x26, 0x89, 0x93, 0xF2, 0x2C, 0x64, 0x01, 0x19];
fn malformed() -> Error {
Error::new(ErrorCode::DerMalformed)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Element<'a> {
pub tag: u8,
pub content: &'a [u8],
pub raw: &'a [u8],
}
impl<'a> Element<'a> {
#[must_use]
pub const fn content_reader(&self) -> DerReader<'a> {
DerReader::new(self.content)
}
pub fn bit_string_octets(&self) -> Result<&'a [u8]> {
if self.tag != TAG_BIT_STRING {
return Err(malformed());
}
match self.content.split_first() {
Some((0, rest)) => Ok(rest),
_ => Err(malformed()),
}
}
}
#[derive(Debug, Clone)]
pub struct DerReader<'a> {
buf: &'a [u8],
pos: usize,
}
impl<'a> DerReader<'a> {
#[must_use]
pub const fn new(buf: &'a [u8]) -> Self {
Self { buf, pos: 0 }
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.pos >= self.buf.len()
}
#[must_use]
pub fn remaining(&self) -> &'a [u8] {
self.buf.get(self.pos..).unwrap_or(&[])
}
#[must_use]
pub fn peek_tag(&self) -> Option<u8> {
self.buf.get(self.pos).copied()
}
pub fn next_element(&mut self) -> Result<Element<'a>> {
let start = self.pos;
let tag = *self.buf.get(self.pos).ok_or_else(malformed)?;
if tag & 0x1F == 0x1F {
return Err(Error::new(ErrorCode::Unsupported));
}
let mut at = self.pos.checked_add(1).ok_or_else(malformed)?;
let first = *self.buf.get(at).ok_or_else(malformed)?;
at = at.checked_add(1).ok_or_else(malformed)?;
let len = if first < 0x80 {
usize::from(first)
} else {
let count = usize::from(first & 0x7F);
if count == 0 || count > 4 {
return Err(malformed());
}
let end = at.checked_add(count).ok_or_else(malformed)?;
let octets = self.buf.get(at..end).ok_or_else(malformed)?;
if octets.first() == Some(&0) {
return Err(malformed());
}
let mut value = 0usize;
for byte in octets {
value = value
.checked_mul(256)
.and_then(|v| v.checked_add(usize::from(*byte)))
.ok_or_else(malformed)?;
}
if value < 0x80 {
return Err(malformed());
}
at = end;
value
};
let end = at.checked_add(len).ok_or_else(malformed)?;
let content = self.buf.get(at..end).ok_or_else(malformed)?;
let raw = self.buf.get(start..end).ok_or_else(malformed)?;
self.pos = end;
Ok(Element { tag, content, raw })
}
pub fn expect(&mut self, tag: u8) -> Result<Element<'a>> {
let element = self.next_element()?;
if element.tag == tag {
Ok(element)
} else {
Err(malformed())
}
}
pub fn expect_sequence(&mut self) -> Result<Self> {
Ok(self.expect(TAG_SEQUENCE)?.content_reader())
}
pub fn expect_set(&mut self) -> Result<Self> {
Ok(self.expect(TAG_SET)?.content_reader())
}
pub fn expect_oid(&mut self, expected: &[u8]) -> Result<()> {
if self.expect(TAG_OID)?.content == expected {
Ok(())
} else {
Err(malformed())
}
}
pub fn expect_unsigned(&mut self) -> Result<&'a [u8]> {
let content = self.expect(TAG_INTEGER)?.content;
let (first, rest) = content.split_first().ok_or_else(malformed)?;
if *first & 0x80 != 0 {
return Err(malformed());
}
if *first == 0 && rest.first().is_some_and(|b| *b < 0x80) {
return Err(malformed());
}
Ok(if *first == 0 { rest } else { content })
}
pub fn expect_uint(&mut self) -> Result<u64> {
let magnitude = self.expect_unsigned()?;
if magnitude.len() > 8 {
return Err(malformed());
}
let mut value = 0u64;
for byte in magnitude {
value = value
.checked_mul(256)
.and_then(|v| v.checked_add(u64::from(*byte)))
.ok_or_else(malformed)?;
}
Ok(value)
}
pub fn take_if(&mut self, tag: u8) -> Result<Option<Element<'a>>> {
if self.peek_tag() == Some(tag) {
Ok(Some(self.next_element()?))
} else {
Ok(None)
}
}
pub fn finish(&self) -> Result<()> {
if self.is_empty() {
Ok(())
} else {
Err(malformed())
}
}
}
#[derive(Debug)]
pub struct DerWriter<'a> {
buf: &'a mut [u8],
pos: usize,
}
impl<'a> DerWriter<'a> {
#[must_use]
pub fn new(buf: &'a mut [u8]) -> Self {
let pos = buf.len();
Self { buf, pos }
}
#[must_use]
pub const fn len(&self) -> usize {
self.buf.len().saturating_sub(self.pos)
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}
#[must_use]
pub fn finish(self) -> &'a [u8] {
let pos = self.pos;
self.buf.get(pos..).unwrap_or(&[])
}
#[must_use]
pub const fn mark(&self) -> usize {
self.pos
}
pub fn byte(&mut self, value: u8) -> Result<()> {
let next = self
.pos
.checked_sub(1)
.ok_or_else(|| Error::new(ErrorCode::BufferTooSmall))?;
let Some(slot) = self.buf.get_mut(next) else {
return Err(Error::new(ErrorCode::BufferTooSmall));
};
*slot = value;
self.pos = next;
Ok(())
}
pub fn slice(&mut self, value: &[u8]) -> Result<()> {
let next = self
.pos
.checked_sub(value.len())
.ok_or_else(|| Error::new(ErrorCode::BufferTooSmall))?;
let Some(slot) = self.buf.get_mut(next..self.pos) else {
return Err(Error::new(ErrorCode::BufferTooSmall));
};
slot.copy_from_slice(value);
self.pos = next;
Ok(())
}
pub fn header(&mut self, tag: u8, mark: usize) -> Result<()> {
let len = mark
.checked_sub(self.pos)
.ok_or_else(|| Error::new(ErrorCode::BufferTooSmall))?;
if len < 0x80 {
let short = u8::try_from(len).map_err(|_| malformed())?;
self.byte(short)?;
} else {
let bytes = len.to_be_bytes();
let first = bytes.iter().position(|b| *b != 0).unwrap_or(bytes.len());
let significant = bytes.get(first..).unwrap_or(&[]);
self.slice(significant)?;
let count = u8::try_from(significant.len()).map_err(|_| malformed())?;
self.byte(0x80 | count)?;
}
self.byte(tag)
}
pub fn primitive(&mut self, tag: u8, content: &[u8]) -> Result<()> {
let mark = self.mark();
self.slice(content)?;
self.header(tag, mark)
}
pub fn oid_with_arc(&mut self, prefix: &[u8], arc: u8) -> Result<()> {
let mark = self.mark();
self.byte(arc)?;
self.slice(prefix)?;
self.header(TAG_OID, mark)
}
pub fn bit_string(&mut self, octets: &[u8]) -> Result<()> {
let mark = self.mark();
self.slice(octets)?;
self.byte(0)?;
self.header(TAG_BIT_STRING, mark)
}
pub fn unsigned_integer(&mut self, scalar: &[u8]) -> Result<()> {
let first = scalar
.iter()
.position(|b| *b != 0)
.unwrap_or(scalar.len().saturating_sub(1));
let trimmed = scalar.get(first..).unwrap_or(&[0]);
let mark = self.mark();
self.slice(trimmed)?;
if trimmed.first().is_some_and(|b| *b & 0x80 != 0) {
self.byte(0)?;
}
self.header(TAG_INTEGER, mark)
}
pub fn algorithm_identifier(&mut self, oid: &[u8]) -> Result<()> {
let mark = self.mark();
self.primitive(TAG_OID, oid)?;
self.header(TAG_SEQUENCE, mark)
}
pub fn subject_public_key_info(&mut self, public_key: &[u8]) -> Result<()> {
let mark = self.mark();
self.bit_string(public_key)?;
let algo = self.mark();
self.primitive(TAG_OID, OID_PRIME256V1)?;
self.primitive(TAG_OID, OID_EC_PUBLIC_KEY)?;
self.header(TAG_SEQUENCE, algo)?;
self.header(TAG_SEQUENCE, mark)
}
pub fn ecdsa_sig_value(&mut self, raw: &[u8]) -> Result<()> {
let half = raw.len().checked_div(2).ok_or_else(malformed)?;
let (r, s) = raw.split_at(half);
let mark = self.mark();
self.unsigned_integer(s)?;
self.unsigned_integer(r)?;
self.header(TAG_SEQUENCE, mark)
}
}
pub fn ecdsa_sig_value_to_raw(der: &[u8], out: &mut [u8; 64]) -> Result<()> {
let mut reader = DerReader::new(der);
let mut seq = reader.expect_sequence()?;
reader.finish()?;
for half in 0..2usize {
let value = seq.expect_unsigned()?;
if value.len() > 32 {
return Err(malformed());
}
let start = half
.checked_mul(32)
.and_then(|s| s.checked_add(32))
.and_then(|e| e.checked_sub(value.len()))
.ok_or_else(malformed)?;
let end = start.checked_add(value.len()).ok_or_else(malformed)?;
out.get_mut(start..end)
.ok_or_else(malformed)?
.copy_from_slice(value);
}
seq.finish()?;
Ok(())
}
pub const MATTER_EPOCH_UNIX: i64 = 946_684_800;
pub const GENERALIZED_TIME_FROM: u32 = 2050;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Civil {
pub year: u32,
pub month: u32,
pub day: u32,
pub hour: u32,
pub minute: u32,
pub second: u32,
}
#[expect(
clippy::arithmetic_side_effects,
reason = "every intermediate is bounded by the u32 input; see above"
)]
pub fn civil_from_epoch_s(epoch_s: u32) -> Result<Civil> {
let unix = i64::from(epoch_s)
.checked_add(MATTER_EPOCH_UNIX)
.ok_or_else(malformed)?;
let days = unix.div_euclid(86_400);
let secs_of_day = unix.rem_euclid(86_400);
let z = days.checked_add(719_468).ok_or_else(malformed)?;
let era = z.div_euclid(146_097);
let doe = z.rem_euclid(146_097); let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146_096) / 365; let y = yoe + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); let mp = (5 * doy + 2) / 153; let d = doy - (153 * mp + 2) / 5 + 1; let m = if mp < 10 { mp + 3 } else { mp - 9 }; let year = if m <= 2 { y + 1 } else { y };
Ok(Civil {
year: u32::try_from(year).map_err(|_| malformed())?,
month: u32::try_from(m).map_err(|_| malformed())?,
day: u32::try_from(d).map_err(|_| malformed())?,
hour: u32::try_from(secs_of_day / 3600).map_err(|_| malformed())?,
minute: u32::try_from((secs_of_day / 60) % 60).map_err(|_| malformed())?,
second: u32::try_from(secs_of_day % 60).map_err(|_| malformed())?,
})
}
pub fn write_time(writer: &mut DerWriter<'_>, epoch_s: u32) -> Result<()> {
let civil = civil_from_epoch_s(epoch_s)?;
let mut out = [0u8; 15];
if civil.year < GENERALIZED_TIME_FROM {
let digits = out.get_mut(..13).ok_or_else(malformed)?;
write_two(digits, 0, civil.year % 100)?;
write_two(digits, 2, civil.month)?;
write_two(digits, 4, civil.day)?;
write_two(digits, 6, civil.hour)?;
write_two(digits, 8, civil.minute)?;
write_two(digits, 10, civil.second)?;
*digits.get_mut(12).ok_or_else(malformed)? = b'Z';
writer.primitive(TAG_UTC_TIME, digits)
} else {
write_two(&mut out, 0, civil.year / 100)?;
write_two(&mut out, 2, civil.year % 100)?;
write_two(&mut out, 4, civil.month)?;
write_two(&mut out, 6, civil.day)?;
write_two(&mut out, 8, civil.hour)?;
write_two(&mut out, 10, civil.minute)?;
write_two(&mut out, 12, civil.second)?;
*out.get_mut(14).ok_or_else(malformed)? = b'Z';
writer.primitive(TAG_GENERALIZED_TIME, &out)
}
}
fn write_two(out: &mut [u8], at: usize, value: u32) -> Result<()> {
if value > 99 {
return Err(malformed());
}
let tens = u8::try_from(value / 10).map_err(|_| malformed())?;
let ones = u8::try_from(value % 10).map_err(|_| malformed())?;
*out.get_mut(at).ok_or_else(malformed)? = b'0'.wrapping_add(tens);
*out.get_mut(at.checked_add(1).ok_or_else(malformed)?)
.ok_or_else(malformed)? = b'0'.wrapping_add(ones);
Ok(())
}
#[expect(
clippy::arithmetic_side_effects,
reason = "every intermediate is bounded by the validated field widths; see civil_from_epoch_s"
)]
#[must_use]
pub fn epoch_s_from_civil(civil: &Civil) -> Option<u32> {
if civil.month < 1 || civil.month > 12 || civil.day < 1 || civil.day > 31 {
return None;
}
if civil.hour > 23 || civil.minute > 59 || civil.second > 59 {
return None;
}
let y = i64::from(civil.year) - i64::from(civil.month <= 2);
let era = y.div_euclid(400);
let yoe = y - era * 400; let m = i64::from(civil.month);
let d = i64::from(civil.day);
let doy = (153 * (if m > 2 { m - 3 } else { m + 9 }) + 2) / 5 + d - 1; let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; let days = era * 146_097 + doe - 719_468;
let secs = days * 86_400
+ i64::from(civil.hour) * 3600
+ i64::from(civil.minute) * 60
+ i64::from(civil.second);
u32::try_from(secs - MATTER_EPOCH_UNIX).ok()
}
pub const NO_EXPIRY: &[u8] = b"99991231235959Z";
pub fn read_time(element: &Element<'_>) -> Result<Option<u32>> {
let (text, century_from_yy) = match element.tag {
TAG_UTC_TIME => (element.content, true),
TAG_GENERALIZED_TIME => (element.content, false),
_ => return Err(malformed()),
};
if text == NO_EXPIRY {
return Ok(None);
}
let expected = if century_from_yy { 13 } else { 15 };
if text.len() != expected || text.last() != Some(&b'Z') {
return Err(malformed());
}
let mut at = 0usize;
let mut take = |digits: usize| -> Result<u32> {
let end = at.checked_add(digits).ok_or_else(malformed)?;
let slot = text.get(at..end).ok_or_else(malformed)?;
at = end;
let mut value = 0u32;
for byte in slot {
let digit = byte
.checked_sub(b'0')
.filter(|d| *d < 10)
.ok_or_else(malformed)?;
value = value
.checked_mul(10)
.and_then(|v| v.checked_add(u32::from(digit)))
.ok_or_else(malformed)?;
}
Ok(value)
};
let year = if century_from_yy {
let yy = take(2)?;
if yy >= 50 {
yy.saturating_add(1900)
} else {
yy.saturating_add(2000)
}
} else {
take(4)?
};
let civil = Civil {
year,
month: take(2)?,
day: take(2)?,
hour: take(2)?,
minute: take(2)?,
second: take(2)?,
};
epoch_s_from_civil(&civil).map(Some).ok_or_else(malformed)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_short_form_length_reads() {
let mut reader = DerReader::new(&[0x02, 0x01, 0x2A]);
let element = reader.next_element().expect("element");
assert_eq!(element.tag, TAG_INTEGER);
assert_eq!(element.content, &[0x2A]);
assert_eq!(element.raw, &[0x02, 0x01, 0x2A]);
reader.finish().expect("consumed");
}
#[test]
fn a_long_form_length_reads() {
let mut bytes = [0u8; 132];
bytes[0] = TAG_OCTET_STRING;
bytes[1] = 0x81;
bytes[2] = 129;
let mut reader = DerReader::new(&bytes);
assert_eq!(reader.next_element().expect("element").content.len(), 129);
}
#[test]
fn a_non_minimal_length_is_refused() {
for bytes in [
&[0x04, 0x81, 0x01, 0xAA][..],
&[0x04, 0x82, 0x00, 0x01, 0xAA][..],
&[0x30, 0x80, 0x00, 0x00][..],
] {
assert!(
DerReader::new(bytes).next_element().is_err(),
"{bytes:02x?}"
);
}
}
#[test]
fn a_length_past_the_end_is_an_error_not_a_truncation() {
assert!(DerReader::new(&[0x04, 0x05, 0xAA]).next_element().is_err());
}
#[test]
fn truncation_at_every_length_is_an_error_not_a_panic() {
let full = [0x30, 0x06, 0x02, 0x01, 0x2A, 0x04, 0x01, 0xFF];
for len in 0..full.len() {
let mut reader = DerReader::new(&full[..len]);
while let Ok(element) = reader.next_element() {
let _ = element;
}
}
}
#[test]
fn a_multi_octet_tag_is_refused_rather_than_misread() {
assert_eq!(
DerReader::new(&[0x1F, 0x81, 0x00, 0x00])
.next_element()
.map(|_| ())
.unwrap_err()
.code(),
ErrorCode::Unsupported
);
}
#[test]
fn integers_are_read_as_der_defines_them() {
let cases: &[(&[u8], Option<u64>)] = &[
(&[0x02, 0x01, 0x00], Some(0)),
(&[0x02, 0x01, 0x7F], Some(127)),
(&[0x02, 0x02, 0x00, 0x80], Some(128)),
(&[0x02, 0x02, 0x01, 0x00], Some(256)),
(&[0x02, 0x01, 0x80], None),
(&[0x02, 0x02, 0x00, 0x01], None),
];
for (bytes, expected) in cases {
assert_eq!(
DerReader::new(bytes).expect_uint().ok(),
*expected,
"{bytes:02x?}"
);
}
}
#[test]
fn a_bit_string_with_unused_bits_is_refused() {
let element = DerReader::new(&[0x03, 0x02, 0x00, 0xAB])
.next_element()
.expect("element");
assert_eq!(element.bit_string_octets().expect("octets"), &[0xAB]);
let element = DerReader::new(&[0x03, 0x02, 0x04, 0xB0])
.next_element()
.expect("element");
assert!(element.bit_string_octets().is_err());
}
#[test]
fn take_if_does_not_consume_the_wrong_element() {
let mut reader = DerReader::new(&[0x02, 0x01, 0x07, 0x04, 0x01, 0xFF]);
assert!(reader.take_if(TAG_OCTET_STRING).expect("look").is_none());
assert_eq!(reader.expect_uint().expect("int"), 7);
assert_eq!(
reader
.take_if(TAG_OCTET_STRING)
.expect("look")
.expect("present")
.content,
&[0xFF]
);
}
#[test]
fn trailing_octets_are_refused() {
let mut reader = DerReader::new(&[0x02, 0x01, 0x07, 0xFF]);
reader.expect_uint().expect("int");
assert!(reader.finish().is_err());
}
#[test]
fn the_writer_and_the_reader_agree() {
let mut buf = [0u8; 128];
let mut w = DerWriter::new(&mut buf);
let mark = w.mark();
w.primitive(TAG_OCTET_STRING, &[0xDE, 0xAD])
.expect("octets");
w.primitive(TAG_INTEGER, &[0x2A]).expect("int");
w.header(TAG_SEQUENCE, mark).expect("sequence");
let bytes = w.finish();
assert_eq!(
bytes,
&[0x30, 0x07, 0x02, 0x01, 0x2A, 0x04, 0x02, 0xDE, 0xAD]
);
let mut reader = DerReader::new(bytes);
let mut seq = reader.expect_sequence().expect("sequence");
assert_eq!(seq.expect_uint().expect("int"), 42);
assert_eq!(
seq.expect(TAG_OCTET_STRING).expect("octets").content,
&[0xDE, 0xAD]
);
seq.finish().expect("consumed");
reader.finish().expect("consumed");
}
#[test]
fn a_signature_survives_the_round_trip_through_der() {
let cases: [[u8; 64]; 4] = [
[0x01; 64],
[0xFF; 64],
{
let mut v = [0u8; 64];
v[31] = 1;
v[63] = 2;
v
},
{
let mut v = [0x7Fu8; 64];
v[0] = 0x80;
v[32] = 0x00;
v
},
];
for raw in cases {
let mut buf = [0u8; 128];
let mut w = DerWriter::new(&mut buf);
w.ecdsa_sig_value(&raw).expect("write");
let der = w.finish();
let mut back = [0u8; 64];
ecdsa_sig_value_to_raw(der, &mut back).expect("read");
assert_eq!(back, raw, "{raw:02x?}");
}
}
#[test]
fn the_two_time_directions_are_inverses() {
let mut at = 0u32;
while at < 1_900_000_000 {
let civil = civil_from_epoch_s(at).expect("civil");
assert_eq!(epoch_s_from_civil(&civil), Some(at), "{at}");
at = at.saturating_add(1_000_003);
}
}
#[test]
fn leap_days_land_where_they_should() {
let cases = [
(0u32, 2000, 1, 1),
(59 * 86_400, 2000, 2, 29),
(60 * 86_400, 2000, 3, 1),
(365 * 86_400, 2000, 12, 31),
(366 * 86_400, 2001, 1, 1),
];
for (epoch, year, month, day) in cases {
let civil = civil_from_epoch_s(epoch).expect("civil");
assert_eq!(
(civil.year, civil.month, civil.day),
(year, month, day),
"{epoch}"
);
}
}
#[test]
fn an_x509_time_reads_as_the_spec_prints_it() {
let element = DerReader::new(&[
0x17, 0x0D, b'2', b'0', b'1', b'0', b'1', b'5', b'1', b'4', b'2', b'3', b'4', b'3',
b'Z',
])
.next_element()
.expect("element");
assert_eq!(read_time(&element).expect("time"), Some(0x271B_17EF));
}
#[test]
fn a_two_digit_year_uses_rfc_5280s_pivot() {
let mut bytes = [
0x17, 0x0D, b'4', b'9', b'0', b'1', b'0', b'1', b'0', b'0', b'0', b'0', b'0', b'0',
b'Z',
];
let element = DerReader::new(&bytes).next_element().expect("element");
let civil =
civil_from_epoch_s(read_time(&element).expect("time").expect("a time")).expect("civil");
assert_eq!(civil.year, 2049);
bytes[2] = b'5';
bytes[3] = b'0';
let element = DerReader::new(&bytes).next_element().expect("element");
assert!(
read_time(&element).is_err(),
"1950 is before the Matter epoch"
);
}
#[test]
fn the_no_expiry_time_is_a_value_not_an_error() {
let mut bytes = [0u8; 17];
bytes[0] = TAG_GENERALIZED_TIME;
bytes[1] = 15;
bytes[2..17].copy_from_slice(NO_EXPIRY);
let element = DerReader::new(&bytes).next_element().expect("element");
assert_eq!(read_time(&element).expect("time"), None);
}
#[test]
fn a_time_that_is_not_zulu_is_refused() {
let element = DerReader::new(&[
0x17, 0x0D, b'2', b'0', b'1', b'0', b'1', b'5', b'1', b'4', b'2', b'3', b'4', b'3',
b'+',
])
.next_element()
.expect("element");
assert!(read_time(&element).is_err());
}
#[test]
fn a_time_with_a_non_digit_is_refused() {
let element = DerReader::new(&[
0x17, 0x0D, b'2', b'0', b'1', b'0', b'1', b'5', b'1', b'4', b'2', b'3', b'4', b'x',
b'Z',
])
.next_element()
.expect("element");
assert!(read_time(&element).is_err());
}
#[test]
fn a_buffer_that_is_too_small_is_an_error_at_every_length() {
let mut full = [0u8; 32];
let written = {
let mut w = DerWriter::new(&mut full);
w.primitive(TAG_OCTET_STRING, &[0xAA; 8]).expect("write");
w.len()
};
for len in 0..written {
let mut small = [0u8; 32];
let slot = small.get_mut(..len).expect("in range");
let mut w = DerWriter::new(slot);
assert!(w.primitive(TAG_OCTET_STRING, &[0xAA; 8]).is_err(), "{len}");
}
}
}