use crate::{
asn1::{
Asn1DecodeWrapper, Asn1EncodeWrapper, BitString, Len, SEQUENCE_TAG, asn1_writer,
decode_asn1_tlv,
},
codec::{Decode, DecodeWrapper, Encode, EncodeWrapper, GenericCodec},
x509::{AlgorithmIdentifier, X509Error},
};
#[derive(Clone, Debug, PartialEq)]
pub struct SubjectPublicKeyInfo<'bytes> {
pub algorithm: AlgorithmIdentifier<'bytes>,
pub subject_public_key: BitString<&'bytes [u8]>,
}
impl<'bytes> SubjectPublicKeyInfo<'bytes> {
pub const fn new(
algorithm: AlgorithmIdentifier<'bytes>,
subject_public_key: BitString<&'bytes [u8]>,
) -> Self {
Self { algorithm, subject_public_key }
}
}
impl<'de> Decode<'de, GenericCodec<Asn1DecodeWrapper, ()>> for SubjectPublicKeyInfo<'de> {
#[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::InvalidSubjectPublicKeyInfo.into());
};
dw.bytes = value;
let algorithm = AlgorithmIdentifier::decode(dw)?;
let subject_public_key = BitString::decode(dw)?;
dw.bytes = rest;
Ok(Self { algorithm, subject_public_key })
}
}
impl<'bytes> Encode<GenericCodec<(), Asn1EncodeWrapper>> for SubjectPublicKeyInfo<'bytes> {
#[inline]
fn encode(&self, ew: &mut EncodeWrapper<'_, Asn1EncodeWrapper>) -> crate::Result<()> {
asn1_writer(ew, Len::MAX_THREE_BYTES, SEQUENCE_TAG, |local_ew| {
self.algorithm.encode(local_ew)?;
self.subject_public_key.encode(local_ew)?;
Ok(())
})
}
}