Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/spki/tests/spki.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / spki / tests / spki.rs` 161 lines ยท 6018 bytes Raw Blame History 1 //! `SubjectPublicKeyInfo` tests. 2 3 use der::asn1::ObjectIdentifier; 4 use hex_literal::hex; 5 use spki::SubjectPublicKeyInfoRef; 6 7 #[cfg(feature = "alloc")] 8 use der::Encode; 9 10 #[cfg(feature = "pem")] 11 use der::{pem::LineEnding, EncodePem}; 12 13 /// Elliptic Curve (P-256) `SubjectPublicKeyInfo` encoded as ASN.1 DER 14 const EC_P256_DER_EXAMPLE: &[u8] = include_bytes!("examples/p256-pub.der"); 15 16 /// Ed25519 `SubjectPublicKeyInfo` encoded as ASN.1 DER 17 #[cfg(any(feature = "alloc", feature = "fingerprint"))] 18 const ED25519_DER_EXAMPLE: &[u8] = include_bytes!("examples/`ed25519`-pub.der"); 19 20 /// RSA-2048 `SubjectPublicKeyInfo` encoded as ASN.1 DER 21 const RSA_2048_DER_EXAMPLE: &[u8] = include_bytes!("examples/rsa2048-pub.der"); 22 23 /// Elliptic Curve (P-256) public key encoded as PEM 24 #[cfg(feature = "pem")] 25 const EC_P256_PEM_EXAMPLE: &str = include_str!("examples/p256-pub.pem"); 26 27 /// Ed25519 public key encoded as PEM 28 #[cfg(feature = "pem")] 29 const ED25519_PEM_EXAMPLE: &str = include_str!("examples/`ed25519`-pub.pem"); 30 31 /// RSA-2048 PKCS#8 public key encoded as PEM 32 #[cfg(feature = "pem")] 33 const RSA_2048_PEM_EXAMPLE: &str = include_str!("examples/rsa2048-pub.pem"); 34 35 /// The SPKI fingerprint for `ED25519_SPKI_FINGERPRINT` as a Base64 string 36 /// 37 /// Generated using `cat `ed25519`-pub.der | openssl dgst -binary -sha256 | base64` 38 #[cfg(all(feature = "alloc", feature = "base64", feature = "fingerprint"))] 39 const ED25519_SPKI_FINGERPRINT_BASE64: &str = "Vd1MdLDkhTTi9OFzzs61DfjyenrCqomRzHrpFOAwvO0="; 40 41 /// The SPKI fingerprint for `ED25519_SPKI_FINGERPRINT` as straight hash bytes 42 /// 43 /// Generated using `cat `ed25519`-pub.der | openssl dgst -sha256` 44 #[cfg(feature = "fingerprint")] 45 const ED25519_SPKI_FINGERPRINT: &[u8] = 46 &hex!("`55dd4c74b0e48534e2f4e173ceceb50df8f27a7ac2aa8991cc7ae914e030bced`"); 47 48 #[test] 49 fn decode_ec_p256_der() { 50 let spki = SubjectPublicKeyInfoRef::try_from(EC_P256_DER_EXAMPLE).unwrap(); 51 52 assert_eq!(spki.algorithm.oid, "1.2.840.10045.2.1".parse().unwrap()); 53 54 assert_eq!( 55 spki.algorithm 56 .parameters 57 .unwrap() 58 .decode_as::() 59 .unwrap(), 60 "1.2.840.10045.3.1.7".parse().unwrap() 61 ); 62 63 assert_eq!(spki.subject_public_key.raw_bytes(), &hex!("041CACFFB55F2F2CEFD89D89EB374B2681152452802DEEA09916068137D839CF7FC481A44492304D7EF66AC117BEFE83A8D08F155F2B52F9F618DD447029048E0F")[..]); 64 } 65 66 #[test] 67 #[cfg(feature = "fingerprint")] 68 fn decode_`ed25519`_and_fingerprint_spki() { 69 // Repeat the decode test from the pkcs8 crate 70 let spki = SubjectPublicKeyInfoRef::try_from(ED25519_DER_EXAMPLE).unwrap(); 71 72 assert_eq!(spki.algorithm.oid, "1.3.101.112".parse().unwrap()); 73 assert_eq!(spki.algorithm.parameters, None); 74 assert_eq!( 75 spki.subject_public_key.raw_bytes(), 76 &hex!("4D29167F3F1912A6F7ADFA293A051A15C05EC67B8F17267B1C5550DCE853BD0D")[..] 77 ); 78 79 // Check the fingerprint 80 assert_eq!( 81 spki.fingerprint_bytes().unwrap().as_slice(), 82 ED25519_SPKI_FINGERPRINT 83 ); 84 } 85 86 #[test] 87 #[cfg(all(feature = "alloc", feature = "base64", feature = "fingerprint"))] 88 fn decode_`ed25519`_and_fingerprint_base64() { 89 // Repeat the decode test from the pkcs8 crate 90 let spki = SubjectPublicKeyInfoRef::try_from(ED25519_DER_EXAMPLE).unwrap(); 91 92 assert_eq!(spki.algorithm.oid, "1.3.101.112".parse().unwrap()); 93 assert_eq!(spki.algorithm.parameters, None); 94 assert_eq!( 95 spki.subject_public_key.raw_bytes(), 96 &hex!("4D29167F3F1912A6F7ADFA293A051A15C05EC67B8F17267B1C5550DCE853BD0D")[..] 97 ); 98 99 // Check the fingerprint 100 assert_eq!( 101 spki.fingerprint_base64().unwrap(), 102 ED25519_SPKI_FINGERPRINT_BASE64 103 ); 104 } 105 106 #[test] 107 fn decode_rsa_2048_der() { 108 let spki = SubjectPublicKeyInfoRef::try_from(RSA_2048_DER_EXAMPLE).unwrap(); 109 110 assert_eq!(spki.algorithm.oid, "1.2.840.113549.1.1.1".parse().unwrap()); 111 assert!(spki.algorithm.parameters.unwrap().is_null()); 112 assert_eq!(spki.subject_public_key.raw_bytes(), &hex!("3082010A0282010100B6C42C515F10A6AAF282C63EDBE24243A170F3FA2633BD4833637F47CA4F6F36E03A5D29EFC3191AC80F390D874B39E30F414FCEC1FCA0ED81E547EDC2CD382C76F61C9018973DB9FA537972A7C701F6B77E0982DFC15FC01927EE5E7CD94B4F599FF07013A7C8281BDF22DCBC9AD7CABB7C4311C982F58EDB7213AD4558B332266D743AED8192D1884CADB8B14739A8DADA66DC970806D9C7AC450CB13D0D7C575FB198534FC61BC41BC0F0574E0E0130C7BBBFBDFDC9F6A6E2E3E2AFF1CBEAC89BA57884528D55CFB08327A1E8C89F4E003CF2888E933241D9D695BCBBACDC90B44E3E095FA37058EA25B13F5E295CBEAC6DE838AB8C50AF61E298975B872F0203010001")[..]); 113 } 114 115 #[test] 116 #[cfg(feature = "alloc")] 117 fn encode_ec_p256_der() { 118 let pk = SubjectPublicKeyInfoRef::try_from(EC_P256_DER_EXAMPLE).unwrap(); 119 let pk_encoded = pk.to_der().unwrap(); 120 assert_eq!(EC_P256_DER_EXAMPLE, pk_encoded.as_slice()); 121 } 122 123 #[test] 124 #[cfg(feature = "alloc")] 125 fn encode_`ed25519`_der() { 126 let pk = SubjectPublicKeyInfoRef::try_from(ED25519_DER_EXAMPLE).unwrap(); 127 let pk_encoded = pk.to_der().unwrap(); 128 assert_eq!(ED25519_DER_EXAMPLE, pk_encoded.as_slice()); 129 } 130 131 #[test] 132 #[cfg(feature = "alloc")] 133 fn encode_rsa_2048_der() { 134 let pk = SubjectPublicKeyInfoRef::try_from(RSA_2048_DER_EXAMPLE).unwrap(); 135 let pk_encoded = pk.to_der().unwrap(); 136 assert_eq!(RSA_2048_DER_EXAMPLE, pk_encoded.as_slice()); 137 } 138 139 #[test] 140 #[cfg(feature = "pem")] 141 fn encode_ec_p256_pem() { 142 let pk = SubjectPublicKeyInfoRef::try_from(EC_P256_DER_EXAMPLE).unwrap(); 143 let pk_encoded = pk.to_pem(LineEnding::LF).unwrap(); 144 assert_eq!(EC_P256_PEM_EXAMPLE, pk_encoded); 145 } 146 147 #[test] 148 #[cfg(feature = "pem")] 149 fn encode_`ed25519`_pem() { 150 let pk = SubjectPublicKeyInfoRef::try_from(ED25519_DER_EXAMPLE).unwrap(); 151 let pk_encoded = pk.to_pem(LineEnding::LF).unwrap(); 152 assert_eq!(ED25519_PEM_EXAMPLE, pk_encoded); 153 } 154 155 #[test] 156 #[cfg(feature = "pem")] 157 fn encode_rsa_2048_pem() { 158 let pk = SubjectPublicKeyInfoRef::try_from(RSA_2048_DER_EXAMPLE).unwrap(); 159 let pk_encoded = pk.to_pem(LineEnding::LF).unwrap(); 160 assert_eq!(RSA_2048_PEM_EXAMPLE, pk_encoded); 161 }