wtx 0.52.1

A collection of different transport implementations and related tools focused primarily on web technologies.
Documentation
use crate::{
  asn1::{
    Asn1DecodeWrapperAux, Asn1EncodeWrapperAux, ImplicitOpt, Len, SEQUENCE_TAG, SequenceBuffer,
    asn1_writer, decode_asn1_tlv,
  },
  codec::{Decode, DecodeWrapper, Encode, EncodeWrapper, GenericCodec},
  collections::Vector,
  misc::Lease,
  x509::{
    CRL_ISSUER_TAG, DISTRIBUTION_POINT_TAG, DistributionPointName, GeneralNames, REASONS_TAG,
    ReasonFlags, X509Error,
  },
};

/// Identifies how CRL information is obtained.
#[derive(Clone, Debug, PartialEq)]
pub struct CrlDistributionPoints<B> {
  /// Identifies how CRL information is obtained.
  pub entries: Vector<DistributionPoint<B>>,
}

impl<'de, B> Decode<'de, GenericCodec<Asn1DecodeWrapperAux, ()>> for CrlDistributionPoints<B>
where
  B: Lease<[u8]> + TryFrom<&'de [u8]>,
  B::Error: Into<crate::Error>,
{
  #[inline]
  fn decode(dw: &mut DecodeWrapper<'de, Asn1DecodeWrapperAux>) -> crate::Result<Self> {
    Ok(Self { entries: SequenceBuffer::decode(dw, SEQUENCE_TAG)?.0.0 })
  }
}

impl<B> Encode<GenericCodec<(), Asn1EncodeWrapperAux>> for CrlDistributionPoints<B>
where
  B: Lease<[u8]>,
{
  #[inline]
  fn encode(&self, ew: &mut EncodeWrapper<'_, Asn1EncodeWrapperAux>) -> crate::Result<()> {
    SequenceBuffer(&self.entries).encode(ew, Len::MAX_TWO_BYTES, SEQUENCE_TAG)
  }
}

/// An entry in the CRL Distribution Points extension.
#[derive(Clone, Debug, PartialEq)]
pub struct DistributionPoint<B> {
  /// See [`DistributionPointName`].
  pub distribution_point: Option<DistributionPointName<B>>,
  /// See [`ReasonFlags`].
  pub reasons: Option<ReasonFlags>,
  /// See [`GeneralNames`].
  pub crl_issuer: Option<GeneralNames<B>>,
}

impl<'de, B> Decode<'de, GenericCodec<Asn1DecodeWrapperAux, ()>> for DistributionPoint<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::InvalidExtensionCrlDistributionPoints.into());
    };
    dw.bytes = value;
    let distribution_point = ImplicitOpt::<_, DISTRIBUTION_POINT_TAG>::decode(dw)?.0;
    let reasons = ImplicitOpt::<_, REASONS_TAG>::decode(dw)?.0;
    let crl_issuer = ImplicitOpt::<_, CRL_ISSUER_TAG>::decode(dw)?.0;
    dw.bytes = rest;
    Ok(Self { distribution_point, reasons, crl_issuer })
  }
}

impl<B> Encode<GenericCodec<(), Asn1EncodeWrapperAux>> for DistributionPoint<B>
where
  B: Lease<[u8]>,
{
  #[inline]
  fn encode(&self, ew: &mut EncodeWrapper<'_, Asn1EncodeWrapperAux>) -> crate::Result<()> {
    asn1_writer(ew, Len::MAX_TWO_BYTES, SEQUENCE_TAG, |local_ew| {
      ImplicitOpt::<_, DISTRIBUTION_POINT_TAG>(&self.distribution_point).encode(local_ew)?;
      ImplicitOpt::<_, REASONS_TAG>(&self.reasons).encode(local_ew)?;
      ImplicitOpt::<_, CRL_ISSUER_TAG>(&self.crl_issuer).encode(local_ew)?;
      Ok(())
    })
  }
}