pem_parser/
lib.rs

1extern crate regex;
2extern crate rustc_serialize;
3
4use self::regex::Regex;
5use self::rustc_serialize::base64::FromBase64;
6
7const REGEX: &'static str = r"(-----BEGIN .*-----\n)((?:(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)*\n)+)(-----END .*-----)";
8
9/// Parse the contents of a PEM file and return a DER-serialized byte slice.
10/// This won't work if `pem_file_contents` contains more than a single key / certificate.
11pub fn pem_to_der(pem_file_contents: &str) -> Vec<u8> {
12  let re = Regex::new(REGEX).unwrap();
13
14  let contents_without_headers = re.replace(pem_file_contents, "$2");
15  let base64_body = contents_without_headers.replace("\n", "");
16
17  base64_body.from_base64().unwrap()
18}