use crate::{
asn1::{Asn1DecodeWrapper, Asn1EncodeWrapper, Len, SEQUENCE_TAG, asn1_writer, decode_asn1_tlv},
codec::{Decode, DecodeWrapper, Encode, EncodeWrapper, GenericCodec},
x509::{Time, X509Error},
};
#[derive(Clone, Debug, PartialEq)]
pub struct Validity {
pub not_before: Time,
pub not_after: Time,
}
impl<'de> Decode<'de, GenericCodec<Asn1DecodeWrapper, ()>> for Validity {
#[inline]
fn decode(dw: &mut DecodeWrapper<'de, Asn1DecodeWrapper>) -> crate::Result<Self> {
let (SEQUENCE_TAG, _, value, rest) = decode_asn1_tlv(dw.bytes)? else {
return Err(X509Error::InvalidValidity.into());
};
dw.bytes = value;
let not_before = Time::decode(dw)?;
let not_after = Time::decode(dw)?;
dw.bytes = rest;
Ok(Self { not_before, not_after })
}
}
impl Encode<GenericCodec<(), Asn1EncodeWrapper>> for Validity {
#[inline]
fn encode(&self, ew: &mut EncodeWrapper<'_, Asn1EncodeWrapper>) -> crate::Result<()> {
asn1_writer(ew, Len::MAX_ONE_BYTE, SEQUENCE_TAG, |local_ew| {
self.not_before.encode(local_ew)?;
self.not_after.encode(local_ew)?;
Ok(())
})
}
}