#![cfg(all(feature = "derive", feature = "oid", feature = "pem"))]
use der::{
asn1::{BitString, ObjectIdentifier},
pem::{LineEnding, PemLabel},
Decode, DecodePem, EncodePem, Sequence,
};
const SPKI_DER: &[u8] = include_bytes!("examples/spki.der");
const SPKI_PEM: &str = include_str!("examples/spki.pem");
#[derive(Copy, Clone, Debug, Eq, PartialEq, Sequence)]
pub struct AlgorithmIdentifier {
pub algorithm: ObjectIdentifier,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Sequence)]
pub struct SpkiBorrowed<'a> {
pub algorithm: AlgorithmIdentifier,
#[asn1(type = "BIT STRING")]
pub subject_public_key: &'a [u8],
}
impl PemLabel for SpkiBorrowed<'_> {
const PEM_LABEL: &'static str = "PUBLIC KEY";
}
#[derive(Clone, Debug, Eq, PartialEq, Sequence)]
pub struct SpkiOwned {
pub algorithm: AlgorithmIdentifier,
pub subject_public_key: BitString,
}
impl PemLabel for SpkiOwned {
const PEM_LABEL: &'static str = "PUBLIC KEY";
}
#[test]
fn from_pem() {
let pem_spki = SpkiOwned::from_pem(SPKI_PEM).unwrap();
let der_spki = SpkiBorrowed::from_der(SPKI_DER).unwrap();
assert_eq!(pem_spki.algorithm, der_spki.algorithm);
assert_eq!(
pem_spki.subject_public_key.raw_bytes(),
der_spki.subject_public_key
);
}
#[test]
fn to_pem() {
let spki = SpkiBorrowed::from_der(SPKI_DER).unwrap();
let pem = spki.to_pem(LineEnding::LF).unwrap();
assert_eq!(&pem, SPKI_PEM);
}