aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / der / tests / pem.rs
| 1 | //! PEM decoding and encoding tests. |
| 2 | |
| 3 | #![cfg(all(feature = "derive", feature = "oid", feature = "pem"))] |
| 4 | |
| 5 | use der::{ |
| 6 | asn1::{BitString, ObjectIdentifier}, |
| 7 | pem::{LineEnding, PemLabel}, |
| 8 | Decode, DecodePem, EncodePem, Sequence, |
| 9 | }; |
| 10 | |
| 11 | /// Example SPKI document encoded as DER. |
| 12 | const SPKI_DER: &[u8] = include_bytes!("examples/spki.der"); |
| 13 | |
| 14 | /// Example SPKI document encoded as PEM. |
| 15 | const SPKI_PEM: &str = include_str!("examples/spki.pem"); |
| 16 | |
| 17 | /// X.509 `AlgorithmIdentifier` |
| 18 | #[derive(Copy, Clone, Debug, Eq, PartialEq, Sequence)] |
| 19 | pub struct AlgorithmIdentifier { |
| 20 | pub algorithm: ObjectIdentifier, |
| 21 | // pub parameters: ... (not used in spki.pem) |
| 22 | } |
| 23 | |
| 24 | /// X.509 `SubjectPublicKeyInfo` (SPKI) in borrowed form |
| 25 | #[derive(Copy, Clone, Debug, Eq, PartialEq, Sequence)] |
| 26 | pub struct SpkiBorrowed<'a> { |
| 27 | pub algorithm: AlgorithmIdentifier, |
| 28 | #[asn1(type = "BIT STRING")] |
| 29 | pub subject_public_key: &'a [u8], |
| 30 | } |
| 31 | |
| 32 | impl PemLabel for SpkiBorrowed<'_> { |
| 33 | const PEM_LABEL: &'static str = "PUBLIC KEY"; |
| 34 | } |
| 35 | |
| 36 | /// X.509 `SubjectPublicKeyInfo` (SPKI) in owned form |
| 37 | #[derive(Clone, Debug, Eq, PartialEq, Sequence)] |
| 38 | pub struct SpkiOwned { |
| 39 | pub algorithm: AlgorithmIdentifier, |
| 40 | pub subject_public_key: BitString, |
| 41 | } |
| 42 | |
| 43 | impl PemLabel for SpkiOwned { |
| 44 | const PEM_LABEL: &'static str = "PUBLIC KEY"; |
| 45 | } |
| 46 | |
| 47 | #[test] |
| 48 | fn from_pem() { |
| 49 | // Decode PEM to owned form. |
| 50 | let pem_spki = SpkiOwned::from_pem(SPKI_PEM).unwrap(); |
| 51 | |
| 52 | // Decode DER to borrowed form. |
| 53 | let der_spki = SpkiBorrowed::from_der(SPKI_DER).unwrap(); |
| 54 | |
| 55 | assert_eq!(pem_spki.algorithm, der_spki.algorithm); |
| 56 | assert_eq!( |
| 57 | pem_spki.subject_public_key.raw_bytes(), |
| 58 | der_spki.subject_public_key |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn to_pem() { |
| 64 | let spki = SpkiBorrowed::from_der(SPKI_DER).unwrap(); |
| 65 | let pem = spki.to_pem(LineEnding::LF).unwrap(); |
| 66 | assert_eq!(&pem, SPKI_PEM); |
| 67 | } |