Function parse_x509_pem

Source
pub fn parse_x509_pem(i: &[u8]) -> IResult<&[u8], Pem, PEMError>
Expand description

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

Return a structure describing the PEM object: the enclosing tag, and the data. Allocates a new buffer for the decoded data.

Note that only the first PEM block is decoded. To iterate all blocks from PEM data, use Pem::iter_from_buffer.

For X.509 (CERTIFICATE tag), the data is a certificate, encoded in DER. To parse the certificate content, use Pem::parse_x509 or parse_x509_certificate.

Examples found in repository?
examples/print-crl.rs (line 146)
134pub fn main() -> io::Result<()> {
135    for file_name in env::args().skip(1) {
136        // placeholder to store decoded PEM data, if needed
137        let tmpdata;
138
139        println!("File: {}", file_name);
140        let data = std::fs::read(file_name.clone()).expect("Unable to read file");
141        let der_data: &[u8] = if (data[0], data[1]) == (0x30, 0x82) {
142            // probably DER
143            &data
144        } else {
145            // try as PEM
146            let (_, data) = parse_x509_pem(&data).expect("Could not decode the PEM file");
147            tmpdata = data;
148            &tmpdata.contents
149        };
150        let (_, crl) = parse_x509_crl(der_data).expect("Could not decode DER data");
151        print_crl_info(&crl);
152    }
153    Ok(())
154}