blob: aae96e5c3798f208b955c2b770c1d822b99914d3 [file] [log] [blame]
Joel Galenson1d906392021-08-11 15:58:47 +00001use crate::error::X509Result;
2
3/// Parse a DER-encoded object, and return the remaining of the input and the built
4/// object.
5///
6/// The returned object uses zero-copy, and so has the same lifetime as the input.
7///
Joel Galensona1302f72021-08-17 09:12:34 -07008#[cfg_attr(
9 feature = "validate",
10 doc = r#"
11Note that only parsing is done, not validation (see the [`Validate`](crate::validate::Validate) trait).
12"#
13)]
14#[cfg_attr(
15 not(feature = "validate"),
16 doc = r#"
17Note that only parsing is done, not validation.
18"#
19)]
Joel Galenson1d906392021-08-11 15:58:47 +000020///
21/// # Example
22///
23/// To parse a certificate and print the subject and issuer:
24///
25/// ```rust
26/// # use x509_parser::prelude::*;
27/// #
28/// # static DER: &'static [u8] = include_bytes!("../assets/IGC_A.der");
29/// #
30/// # fn main() {
31/// let res = X509Certificate::from_der(DER);
32/// match res {
33/// Ok((_rem, x509)) => {
34/// let subject = x509.subject();
35/// let issuer = x509.issuer();
36/// println!("X.509 Subject: {}", subject);
37/// println!("X.509 Issuer: {}", issuer);
38/// },
39/// _ => panic!("x509 parsing failed: {:?}", res),
40/// }
41/// # }
42/// ```
43
44pub trait FromDer<'a>: Sized {
45 /// Attempt to parse input bytes into a DER object
46 fn from_der(bytes: &'a [u8]) -> X509Result<'a, Self>;
47}