Thing for extracting DER from PEM files

This commit is contained in:
Joseph Birr-Pixton 2016-05-30 09:54:20 +01:00
parent 88d2cd9af6
commit 96714fcc69
3 changed files with 43 additions and 0 deletions

1
Cargo.lock generated
View File

@ -4,6 +4,7 @@ version = "0.1.0"
dependencies = [
"mio 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"ring 0.1.0 (git+https://github.com/briansmith/ring)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
"webpki 0.1.0 (git+file:///home/jbp/webpki/)",
]

View File

@ -9,3 +9,4 @@ webpki = { version = "0.1.0", git = "file:///home/jbp/webpki/" }
mio = "0.5.1"
time = "0.1.35"
rustc-serialize = "0.3"

41
src/pemfile.rs Normal file
View File

@ -0,0 +1,41 @@
use std::io;
use rustc_serialize::base64::FromBase64;
static START_MARKER: &'static str = "-----BEGIN CERTIFICATE-----";
static END_MARKER: &'static str = "-----END CERTIFICATE-----";
/// Extract all the certificates from rd, and return a vec of bytevecs
/// containing the der-format contents.
pub fn certs(rd: &mut io::BufRead) -> Result<Vec<Vec<u8>>, ()> {
let mut ders = Vec::new();
let mut b64buf = String::new();
let mut take_base64 = false;
loop {
let mut line = String::new();
let len = try!(rd.read_line(&mut line)
.map_err(|_| ()));
if len == 0 {
return Ok(ders);
}
if line.starts_with(START_MARKER) {
take_base64 = true;
continue;
}
if line.starts_with(END_MARKER) {
take_base64 = false;
let der = try!(b64buf.from_base64()
.map_err(|_| ()));
ders.push(der);
b64buf = String::new();
continue;
}
if take_base64 {
b64buf.push_str(&line.trim());
}
}
}