Trait FromDer
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§
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§
§impl<'a> FromDer<'a, X509Error> for CertificateRevocationList<'a>
CertificateList ::= SEQUENCE {
tbsCertList TBSCertList,
signatureAlgorithm AlgorithmIdentifier,
signatureValue BIT STRING }
impl<'a> FromDer<'a, X509Error> for CertificateRevocationList<'a>
CertificateList ::= SEQUENCE { tbsCertList TBSCertList, signatureAlgorithm AlgorithmIdentifier, signatureValue BIT STRING }
§impl<'a> FromDer<'a, X509Error> for TbsCertificate<'a>
impl<'a> FromDer<'a, X509Error> for TbsCertificate<'a>
§fn from_der(
i: &'a [u8],
) -> Result<(&'a [u8], TbsCertificate<'a>), Err<X509Error>>
fn from_der( i: &'a [u8], ) -> Result<(&'a [u8], TbsCertificate<'a>), Err<X509Error>>
Parse a DER-encoded TbsCertificate object
TBSCertificate ::= SEQUENCE { version [0] Version DEFAULT v1, serialNumber CertificateSerialNumber, signature AlgorithmIdentifier, issuer Name, validity Validity, subject Name, subjectPublicKeyInfo SubjectPublicKeyInfo, issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL, -- If present, version MUST be v2 or v3 subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL, -- If present, version MUST be v2 or v3 extensions [3] Extensions OPTIONAL -- If present, version MUST be v3 -- }
§impl<'a> FromDer<'a, X509Error> for X509Certificate<'a>
impl<'a> FromDer<'a, X509Error> for X509Certificate<'a>
§fn from_der(
i: &'a [u8],
) -> Result<(&'a [u8], X509Certificate<'a>), Err<X509Error>>
fn from_der( i: &'a [u8], ) -> Result<(&'a [u8], X509Certificate<'a>), Err<X509Error>>
Parse a DER-encoded X.509 Certificate, and return the remaining of the input and the built object.
The returned object uses zero-copy, and so has the same lifetime as the input.
Note that only parsing is done, not validation.
Certificate ::= SEQUENCE { tbsCertificate TBSCertificate, signatureAlgorithm AlgorithmIdentifier, signatureValue BIT STRING }
§Example
To parse a certificate and print the subject and issuer:
let res = parse_x509_certificate(DER);
match res {
Ok((_rem, x509)) => {
let subject = x509.subject();
let issuer = x509.issuer();
println!("X.509 Subject: {}", subject);
println!("X.509 Issuer: {}", issuer);
},
_ => panic!("x509 parsing failed: {:?}", res),
}
§impl<'a> FromDer<'a, X509Error> for X509CertificationRequest<'a>
CertificationRequest ::= SEQUENCE {
certificationRequestInfo CertificationRequestInfo,
signatureAlgorithm AlgorithmIdentifier{{ SignatureAlgorithms }},
signature BIT STRING
}
impl<'a> FromDer<'a, X509Error> for X509CertificationRequest<'a>
CertificationRequest ::= SEQUENCE { certificationRequestInfo CertificationRequestInfo, signatureAlgorithm AlgorithmIdentifier{{ SignatureAlgorithms }}, signature BIT STRING }
§impl<'a> FromDer<'a, X509Error> for X509CertificationRequestInfo<'a>
CertificationRequestInfo ::= SEQUENCE {
version INTEGER { v1(0) } (v1,...),
subject Name,
subjectPKInfo SubjectPublicKeyInfo{{ PKInfoAlgorithms }},
attributes [0] Attributes{{ CRIAttributes }}
}
impl<'a> FromDer<'a, X509Error> for X509CertificationRequestInfo<'a>
CertificationRequestInfo ::= SEQUENCE { version INTEGER { v1(0) } (v1,...), subject Name, subjectPKInfo SubjectPublicKeyInfo{{ PKInfoAlgorithms }}, attributes [0] Attributes{{ CRIAttributes }} }
§impl<'a> FromDer<'a, X509Error> for X509Extension<'a>
Extension ::= SEQUENCE {
extnID OBJECT IDENTIFIER,
critical BOOLEAN DEFAULT FALSE,
extnValue OCTET STRING }
impl<'a> FromDer<'a, X509Error> for X509Extension<'a>
Extension ::= SEQUENCE { extnID OBJECT IDENTIFIER, critical BOOLEAN DEFAULT FALSE, extnValue OCTET STRING }
Implementors§
impl FromDer<'_, X509Error> for rama::crypto::dep::x509_parser::certificate::Validity
impl FromDer<'_, X509Error> for rama::crypto::dep::x509_parser::prelude::ASN1Time
impl<'a> FromDer<'a> for Option<Any<'a>>
impl<'a> FromDer<'a> for Any<'a>
impl<'a> FromDer<'a> for Header<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::extensions::GeneralName<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::certificate::TbsCertificate<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::certificate::X509Certificate<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::certification_request::X509CertificationRequest<'a>
CertificationRequest ::= SEQUENCE { certificationRequestInfo CertificationRequestInfo, signatureAlgorithm AlgorithmIdentifier{{ SignatureAlgorithms }}, signature BIT STRING }
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::certification_request::X509CertificationRequestInfo<'a>
CertificationRequestInfo ::= SEQUENCE { version INTEGER { v1(0) } (v1,...), subject Name, subjectPKInfo SubjectPublicKeyInfo{{ PKInfoAlgorithms }}, attributes [0] Attributes{{ CRIAttributes }} }
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::cri_attributes::ExtensionRequest<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::cri_attributes::X509CriAttribute<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::extensions::AuthorityInfoAccess<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::extensions::AuthorityKeyIdentifier<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::extensions::BasicConstraints
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::extensions::CRLDistributionPoints<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::extensions::ExtendedKeyUsage<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::extensions::InhibitAnyPolicy
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::extensions::IssuerAlternativeName<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::extensions::KeyIdentifier<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::extensions::KeyUsage
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::extensions::NSCertType
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::extensions::NameConstraints<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::extensions::PolicyConstraints
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::extensions::PolicyMappings<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::extensions::SubjectAlternativeName<'a>
impl<'a> FromDer<'a, X509Error> for SubjectInfoAccess<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::extensions::X509Extension<'a>
Extension ::= SEQUENCE { extnID OBJECT IDENTIFIER, critical BOOLEAN DEFAULT FALSE, extnValue OCTET STRING }
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::prelude::public_key::RSAPublicKey<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::prelude::AttributeTypeAndValue<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::prelude::CertificateRevocationList<'a>
CertificateList ::= SEQUENCE { tbsCertList TBSCertList, signatureAlgorithm AlgorithmIdentifier, signatureValue BIT STRING }
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::prelude::RelativeDistinguishedName<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::prelude::RevokedCertificate<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::prelude::SubjectPublicKeyInfo<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::prelude::TbsCertList<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::prelude::X509Name<'a>
impl<'a> FromDer<'a, X509Error> for rama::crypto::dep::x509_parser::prelude::X509Version
impl<'a, T> FromDer<'a> for Option<T>
impl<'a, T, E> FromDer<'a, E> for BTreeSet<T>
manual impl of FromDer, so we do not need to require TryFrom<Any> + CheckDerConstraints
impl<'a, T, E> FromDer<'a, E> for HashSet<T>
manual impl of FromDer, so we do not need to require TryFrom<Any> + CheckDerConstraints
impl<'a, T, E> FromDer<'a, E> for Vec<T>
manual impl of FromDer, so we do not need to require TryFrom<Any> + CheckDerConstraints
impl<'a, T, E> FromDer<'a, E> for SequenceOf<T>
manual impl of FromDer, so we do not need to require TryFrom<Any> + CheckDerConstraints
impl<'a, T, E> FromDer<'a, E> for SetOf<T>
manual impl of FromDer, so we do not need to require TryFrom<Any> + CheckDerConstraints