Trait FromDer

Source
pub trait FromDer<'a, E = Error>: Sized {
    // Required method
    fn from_der(bytes: &'a [u8]) -> Result<(&'a [u8], Self), Err<E>>;
}
Expand description

Base trait for DER object parsers

Library authors should usually not directly implement this trait, but should prefer implementing the TryFrom<Any> + CheckDerConstraints traits, which offers greater flexibility and provides an equivalent FromDer implementation for free (in fact, it provides both FromBer and FromDer).

Note: if you already implemented TryFrom<Any> and CheckDerConstraints, you can get a free FromDer implementation by implementing the DerAutoDerive trait. This is not automatic, so it is also possible to manually implement FromDer if preferred.

§Examples

use asn1_rs::{Any, CheckDerConstraints, DerAutoDerive, Result, Tag};
use std::convert::TryFrom;

// The type to be decoded
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MyType(pub u32);

impl<'a> TryFrom<Any<'a>> for MyType {
    type Error = asn1_rs::Error;

    fn try_from(any: Any<'a>) -> Result<MyType> {
        any.tag().assert_eq(Tag::Integer)?;
        // for this fictive example, the type contains the number of characters
        let n = any.data.len() as u32;
        Ok(MyType(n))
    }
}

impl CheckDerConstraints for MyType {
    fn check_constraints(any: &Any) -> Result<()> {
        any.header.assert_primitive()?;
        Ok(())
    }
}

impl DerAutoDerive for MyType {}

// The above code provides a `FromDer` implementation for free.

// Example of parsing code:
use asn1_rs::FromDer;

let input = &[2, 1, 2];
// Objects can be parsed using `from_der`, which returns the remaining bytes
// and the parsed object:
let (rem, my_type) = MyType::from_der(input).expect("parsing failed");

Required Methods§

Source

fn from_der(bytes: &'a [u8]) -> Result<(&'a [u8], Self), Err<E>>

Attempt to parse input bytes into a DER object (enforcing constraints)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<'a> FromDer<'a> for Option<Any<'a>>

Source§

fn from_der(bytes: &'a [u8]) -> Result<(&'a [u8], Option<Any<'a>>), Err<Error>>

Source§

impl<'a, T> FromDer<'a> for Option<T>
where T: FromDer<'a> + Tagged,

Source§

fn from_der(bytes: &'a [u8]) -> Result<(&'a [u8], Option<T>), Err<Error>>

Source§

impl<'a, T, E> FromDer<'a, E> for BTreeSet<T>
where T: FromDer<'a, E> + Ord, E: From<Error> + Debug,

manual impl of FromDer, so we do not need to require TryFrom<Any> + CheckDerConstraints

Source§

fn from_der(bytes: &'a [u8]) -> Result<(&'a [u8], BTreeSet<T>), Err<E>>

Source§

impl<'a, T, E> FromDer<'a, E> for Vec<T>
where T: FromDer<'a, E>, E: From<Error> + Debug,

manual impl of FromDer, so we do not need to require TryFrom<Any> + CheckDerConstraints

Source§

fn from_der(bytes: &'a [u8]) -> Result<(&'a [u8], Vec<T>), Err<E>>

Source§

impl<'a, T, E> FromDer<'a, E> for HashSet<T>
where T: FromDer<'a, E> + Hash + Eq, E: From<Error> + Debug,

manual impl of FromDer, so we do not need to require TryFrom<Any> + CheckDerConstraints

Source§

fn from_der(bytes: &'a [u8]) -> Result<(&'a [u8], HashSet<T>), Err<E>>

Implementors§

Source§

impl FromDer<'_, X509Error> for Validity

Source§

impl FromDer<'_, X509Error> for ASN1Time

Source§

impl<'a> FromDer<'a> for Any<'a>

Source§

impl<'a> FromDer<'a> for Header<'a>

Source§

impl<'a> FromDer<'a, X509Error> for GeneralName<'a>

Source§

impl<'a> FromDer<'a, X509Error> for TbsCertificate<'a>

Source§

impl<'a> FromDer<'a, X509Error> for X509Certificate<'a>

Source§

impl<'a> FromDer<'a, X509Error> for X509CertificationRequest<'a>

CertificationRequest ::= SEQUENCE {
    certificationRequestInfo CertificationRequestInfo,
    signatureAlgorithm AlgorithmIdentifier{{ SignatureAlgorithms }},
    signature          BIT STRING
}
Source§

impl<'a> FromDer<'a, X509Error> for X509CertificationRequestInfo<'a>

CertificationRequestInfo ::= SEQUENCE {
     version       INTEGER { v1(0) } (v1,...),
     subject       Name,
     subjectPKInfo SubjectPublicKeyInfo{{ PKInfoAlgorithms }},
     attributes    [0] Attributes{{ CRIAttributes }}
}
Source§

impl<'a> FromDer<'a, X509Error> for ExtensionRequest<'a>

Source§

impl<'a> FromDer<'a, X509Error> for X509CriAttribute<'a>

Source§

impl<'a> FromDer<'a, X509Error> for AuthorityInfoAccess<'a>

Source§

impl<'a> FromDer<'a, X509Error> for AuthorityKeyIdentifier<'a>

Source§

impl<'a> FromDer<'a, X509Error> for BasicConstraints

Source§

impl<'a> FromDer<'a, X509Error> for CRLDistributionPoints<'a>

Source§

impl<'a> FromDer<'a, X509Error> for ExtendedKeyUsage<'a>

Source§

impl<'a> FromDer<'a, X509Error> for InhibitAnyPolicy

Source§

impl<'a> FromDer<'a, X509Error> for IssuerAlternativeName<'a>

Source§

impl<'a> FromDer<'a, X509Error> for KeyIdentifier<'a>

Source§

impl<'a> FromDer<'a, X509Error> for KeyUsage

Source§

impl<'a> FromDer<'a, X509Error> for NSCertType

Source§

impl<'a> FromDer<'a, X509Error> for NameConstraints<'a>

Source§

impl<'a> FromDer<'a, X509Error> for PolicyConstraints

Source§

impl<'a> FromDer<'a, X509Error> for PolicyMappings<'a>

Source§

impl<'a> FromDer<'a, X509Error> for SubjectAlternativeName<'a>

Source§

impl<'a> FromDer<'a, X509Error> for X509Extension<'a>

Extension  ::=  SEQUENCE  {
    extnID      OBJECT IDENTIFIER,
    critical    BOOLEAN DEFAULT FALSE,
    extnValue   OCTET STRING  }
Source§

impl<'a> FromDer<'a, X509Error> for RSAPublicKey<'a>

Source§

impl<'a> FromDer<'a, X509Error> for CertificateRevocationList<'a>

CertificateList  ::=  SEQUENCE  {
     tbsCertList          TBSCertList,
     signatureAlgorithm   AlgorithmIdentifier,
     signatureValue       BIT STRING  }
Source§

impl<'a> FromDer<'a, X509Error> for RevokedCertificate<'a>

Source§

impl<'a> FromDer<'a, X509Error> for TbsCertList<'a>

Source§

impl<'a> FromDer<'a, X509Error> for AttributeTypeAndValue<'a>

Source§

impl<'a> FromDer<'a, X509Error> for RelativeDistinguishedName<'a>

Source§

impl<'a> FromDer<'a, X509Error> for SubjectPublicKeyInfo<'a>

Source§

impl<'a> FromDer<'a, X509Error> for X509Name<'a>

Source§

impl<'a> FromDer<'a, X509Error> for X509Version

Source§

impl<'a, T, E> FromDer<'a, E> for SequenceOf<T>
where T: FromDer<'a, E>, E: From<Error> + Display + Debug,

manual impl of FromDer, so we do not need to require TryFrom<Any> + CheckDerConstraints

Source§

impl<'a, T, E> FromDer<'a, E> for SetOf<T>
where T: FromDer<'a, E>, E: From<Error> + Display + Debug,

manual impl of FromDer, so we do not need to require TryFrom<Any> + CheckDerConstraints

Source§

impl<'a, T, E> FromDer<'a, E> for TaggedParser<'a, Explicit, T, E>
where T: FromDer<'a, E>, E: From<Error>,

Source§

impl<'a, T, E> FromDer<'a, E> for TaggedParser<'a, Implicit, T, E>
where T: TryFrom<Any<'a>, Error = E> + CheckDerConstraints + Tagged, E: From<Error>,

Source§

impl<'a, T, E> FromDer<'a, E> for T
where T: TryFrom<Any<'a>, Error = E> + CheckDerConstraints + DerAutoDerive, E: From<Error> + Display + Debug,

Source§

impl<'a, T, E, const CLASS: u8, const TAG: u32> FromDer<'a, E> for TaggedValue<T, E, Explicit, CLASS, TAG>
where T: FromDer<'a, E>, E: From<Error>,

Source§

impl<'a, T, E, const CLASS: u8, const TAG: u32> FromDer<'a, E> for TaggedValue<T, E, Implicit, CLASS, TAG>
where T: TryFrom<Any<'a>, Error = E> + Tagged, E: From<Error>,

Source§

impl<'ber, 'a> FromDer<'ber> for PolicyMapping<'a>
where 'ber: 'a,

Source§

impl<'ber, 'a> FromDer<'ber> for EcdsaSigValue<'a>
where 'ber: 'a,

Source§

impl<'ber, 'a> FromDer<'ber, X509Error> for AlgorithmIdentifier<'a>
where 'ber: 'a,