const BASE64: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const LINE: usize = 64;
pub(crate) fn public_key(der: &[u8]) -> String {
block("PUBLIC KEY", der)
}
pub(crate) fn block(label: &str, der: &[u8]) -> String {
let body = base64(der);
let lines: Vec<String> = body
.as_bytes()
.chunks(LINE)
.map(|line| String::from_utf8_lossy(line).into_owned())
.collect();
format!(
"-----BEGIN {label}-----\n{}\n-----END {label}-----\n",
lines.join("\n")
)
}
fn base64(bytes: &[u8]) -> String {
let mut out = String::new();
for chunk in bytes.chunks(3) {
let group = chunk
.iter()
.chain([0u8, 0u8].iter())
.take(3)
.fold(0u32, |group, byte| (group << 8) | u32::from(*byte));
for position in 0..4 {
let index = usize::try_from((group >> (18 - 6 * position)) & 63).unwrap_or_default();
let symbol = BASE64.get(index).copied().map_or('=', char::from);
out.push(if position <= chunk.len() { symbol } else { '=' });
}
}
out
}
#[cfg(test)]
mod tests {
use super::{base64, public_key};
#[test]
fn base64_matches_the_rfc_vectors() {
for (plain, encoded) in [
("", ""),
("f", "Zg=="),
("fo", "Zm8="),
("foo", "Zm9v"),
("foobar", "Zm9vYmFy"),
] {
assert_eq!(base64(plain.as_bytes()), encoded);
}
}
#[test]
fn a_public_key_round_trips_through_its_pem() {
use rustls::pki_types::SubjectPublicKeyInfoDer;
use rustls::pki_types::pem::PemObject;
let der: Vec<u8> = (0u8..91).collect();
let pem = public_key(&der);
assert!(pem.lines().all(|line| line.len() <= 64), "{pem}");
assert_eq!(pem.lines().count(), 4, "two body lines between two labels");
let back = SubjectPublicKeyInfoDer::from_pem_slice(pem.as_bytes()).expect("pem");
assert_eq!(back.as_ref(), der.as_slice());
}
}