Struct x509_parser::pem::Pem

source ·
pub struct Pem {
    pub label: String,
    pub contents: Vec<u8>,
}
Expand description

Representation of PEM data

Fields§

§label: String

The PEM label

§contents: Vec<u8>

The PEM decoded data

Implementations§

source§

impl Pem

source

pub fn read(r: impl BufRead + Seek) -> Result<(Pem, usize), PEMError>

Read the next PEM-encoded structure, and decode the base64 data

Returns the certificate (encoded in DER) and the number of bytes read. Allocates a new buffer for the decoded data.

Note that a PEM file can contain multiple PEM blocks. This function returns the first decoded object, starting from the current reader position. To get all objects, call this function repeatedly until PEMError::MissingHeader is returned.

Examples
let file = std::fs::File::open("assets/certificate.pem").unwrap();
let subject = x509_parser::pem::Pem::read(std::io::BufReader::new(file))
     .unwrap().0
    .parse_x509().unwrap()
    .tbs_certificate.subject.to_string();
assert_eq!(subject, "CN=lists.for-our.info");
source

pub fn parse_x509(&self) -> Result<X509Certificate<'_>, Err<X509Error>>

Decode the PEM contents into a X.509 object

source

pub fn iter_from_buffer(i: &[u8]) -> PemIterator<Cursor<&[u8]>>

Returns an iterator over the PEM-encapsulated parts of a buffer

Only the sections enclosed in blocks starting with -----BEGIN xxx----- and ending with -----END xxx----- will be considered. Lines before, between or after such blocks will be ignored.

The iterator is fallible: next() returns a Result<Pem, PEMError> object. An error indicates a block is present but invalid.

If the buffer does not contain any block, iterator will be empty.

Examples found in repository?
examples/print-cert.rs (line 402)
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
pub fn main() -> io::Result<()> {
    for file_name in env::args().skip(1) {
        println!("File: {}", file_name);
        let data = std::fs::read(file_name.clone()).expect("Unable to read file");
        if matches!((data[0], data[1]), (0x30, 0x81..=0x83)) {
            // probably DER
            handle_certificate(&file_name, &data)?;
        } else {
            // try as PEM
            for (n, pem) in Pem::iter_from_buffer(&data).enumerate() {
                match pem {
                    Ok(pem) => {
                        let data = &pem.contents;
                        println!("Certificate [{}]", n);
                        handle_certificate(&file_name, data)?;
                    }
                    Err(e) => {
                        eprintln!("Error while decoding PEM entry {}: {}", n, e);
                    }
                }
            }
        }
    }
    Ok(())
}
source

pub fn iter_from_reader<R: BufRead + Seek>(reader: R) -> PemIterator<R>

Returns an iterator over the PEM-encapsulated parts of a reader

Only the sections enclosed in blocks starting with -----BEGIN xxx----- and ending with -----END xxx----- will be considered. Lines before, between or after such blocks will be ignored.

The iterator is fallible: next() returns a Result<Pem, PEMError> object. An error indicates a block is present but invalid.

If the reader does not contain any block, iterator will be empty.

Trait Implementations§

source§

impl Clone for Pem

source§

fn clone(&self) -> Pem

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Pem

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq<Pem> for Pem

source§

fn eq(&self, other: &Pem) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Pem

source§

impl StructuralEq for Pem

source§

impl StructuralPartialEq for Pem

Auto Trait Implementations§

§

impl RefUnwindSafe for Pem

§

impl Send for Pem

§

impl Sync for Pem

§

impl Unpin for Pem

§

impl UnwindSafe for Pem

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere T: 'a,

source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere T: 'a,

source§

fn implicit( self, class: Class, constructed: bool, tag: u32 ) -> TaggedParser<'a, Implicit, Self, E>

source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.