[][src]Module x509_parser::pem

Decoding functions for PEM-encoded data

Examples

To parse a certificate in PEM format, first create the Pem object, then decode contents:

use std::io::Cursor;
use x509_parser::pem::Pem;

static IGCA_PEM: &'static [u8] = include_bytes!("../assets/IGC_A.pem");

let reader = Cursor::new(IGCA_PEM);
let (pem,bytes_read) = Pem::read(reader).expect("Reading PEM failed");
let x509 = pem.parse_x509().expect("X.509: decoding DER failed");
assert_eq!(x509.tbs_certificate.version, 2);

This is the most direct method to parse PEM data.

Another method to parse the certificate is to use pem_to_der:

use x509_parser::pem::pem_to_der;
use x509_parser::parse_x509_der;

static IGCA_PEM: &'static [u8] = include_bytes!("../assets/IGC_A.pem");

let res = pem_to_der(IGCA_PEM);
match res {
    Ok((rem, pem)) => {
        assert!(rem.is_empty());
        //
        assert_eq!(pem.label, String::from("CERTIFICATE"));
        //
        let res_x509 = parse_x509_der(&pem.contents);
        assert!(res_x509.is_ok());
    },
    _ => panic!("PEM parsing failed: {:?}", res),
}

Note that all methods require to store the Pem object in a variable, mainly because decoding the PEM object requires allocation of buffers, and that the lifetime of X.509 certificates will be bound to these buffers.

Structs

Pem

Representation of PEM data

Functions

pem_to_der

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