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
impl Pem
Sourcepub fn read(r: impl BufRead + Seek) -> Result<(Pem, usize), PEMError>
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");
Sourcepub fn parse_x509(&self) -> Result<X509Certificate<'_>, Err<X509Error>>
pub fn parse_x509(&self) -> Result<X509Certificate<'_>, Err<X509Error>>
Decode the PEM contents into a X.509 object
Sourcepub fn iter_from_buffer(i: &[u8]) -> PemIterator<Cursor<&[u8]>> ⓘ
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?
393pub fn main() -> io::Result<()> {
394 for file_name in env::args().skip(1) {
395 println!("File: {}", file_name);
396 let data = std::fs::read(file_name.clone()).expect("Unable to read file");
397 if matches!((data[0], data[1]), (0x30, 0x81..=0x83)) {
398 // probably DER
399 handle_certificate(&file_name, &data)?;
400 } else {
401 // try as PEM
402 for (n, pem) in Pem::iter_from_buffer(&data).enumerate() {
403 match pem {
404 Ok(pem) => {
405 let data = &pem.contents;
406 println!("Certificate [{}]", n);
407 handle_certificate(&file_name, data)?;
408 }
409 Err(e) => {
410 eprintln!("Error while decoding PEM entry {}: {}", n, e);
411 }
412 }
413 }
414 }
415 }
416 Ok(())
417}
Sourcepub fn iter_from_reader<R: BufRead + Seek>(reader: R) -> PemIterator<R> ⓘ
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.