wtx 0.50.0

A collection of different transport implementations and related tools focused primarily on web technologies.
Documentation
use crate::{
  asn1::{
    Any, Asn1DecodeWrapperAux, Asn1EncodeWrapperAux, Len, Oid, SEQUENCE_TAG, asn1_writer,
    decode_asn1_tlv,
  },
  codec::{Decode, DecodeWrapper, Encode, EncodeWrapper, GenericCodec},
  misc::Lease,
  x509::X509Error,
};

/// The algorithm identifier is used to identify a cryptographic algorithm.
#[derive(Clone, Default, Debug, PartialEq)]
pub struct AlgorithmIdentifier<B> {
  /// The OID that uniquely identifies the algorithm.
  pub algorithm: Oid,
  /// Optional DER-encoded algorithm parameters (may be NULL or absent).
  pub parameters: Option<Any<B>>,
}

impl<B> AlgorithmIdentifier<B> {
  /// Shortcut
  #[inline]
  pub const fn from_algorithm(algorithm: Oid) -> Self {
    Self { algorithm, parameters: None }
  }

  /// Shortcut
  #[inline]
  pub const fn new(algorithm: Oid, parameters: Option<Any<B>>) -> Self {
    Self { algorithm, parameters }
  }
}

impl<'de, B> Decode<'de, GenericCodec<Asn1DecodeWrapperAux, ()>> for AlgorithmIdentifier<B>
where
  B: Lease<[u8]> + TryFrom<&'de [u8]>,
  B::Error: Into<crate::Error>,
{
  #[inline]
  fn decode(dw: &mut DecodeWrapper<'de, Asn1DecodeWrapperAux>) -> crate::Result<Self> {
    let (SEQUENCE_TAG, _, value, rest) = decode_asn1_tlv(dw.bytes)? else {
      return Err(X509Error::InvalidAlgorithmIdentifier.into());
    };
    dw.bytes = value;
    let algorithm = Oid::decode(dw)?;
    let parameters = if dw.bytes.is_empty() { None } else { Some(Any::decode(dw)?) };
    dw.bytes = rest;
    Ok(Self { algorithm, parameters })
  }
}

impl<B> Encode<GenericCodec<(), Asn1EncodeWrapperAux>> for AlgorithmIdentifier<B>
where
  B: Lease<[u8]>,
{
  #[inline]
  fn encode(&self, ew: &mut EncodeWrapper<'_, Asn1EncodeWrapperAux>) -> crate::Result<()> {
    asn1_writer(ew, Len::MAX_ONE_BYTE, SEQUENCE_TAG, |local_ew| {
      self.algorithm.encode(local_ew)?;
      if let Some(params) = &self.parameters {
        params.encode(local_ew)?;
      }
      Ok(())
    })
  }
}