• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use 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 ///
8 #[cfg_attr(
9     feature = "validate",
10     doc = r#"
11 Note 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#"
17 Note that only parsing is done, not validation.
18 "#
19 )]
20 ///
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 
44 pub trait FromDer<'a>: Sized {
45     /// Attempt to parse input bytes into a DER object
from_der(bytes: &'a [u8]) -> X509Result<'a, Self>46     fn from_der(bytes: &'a [u8]) -> X509Result<'a, Self>;
47 }
48