ppoppo_token/signing_key.rs
1//! Opaque Ed25519 signing key with associated `kid`.
2//!
3//! Wraps `jsonwebtoken::EncodingKey` so the public surface stays free of
4//! `jsonwebtoken::*` (M51 boundary). The `kid` travels with the key
5//! because JWT kids are issuer-chosen — there's no library-derived
6//! identifier the way PASETO has PASERK pid. Pairing kid+key here lets
7//! the engine reject mismatched configurations (`IssueError::KeyMismatch`)
8//! before any encoding work happens.
9//!
10//! Companion: [`ed25519_public_from_pem`] derives the 32-byte public key
11//! half from the same PEM that builds a `SigningKey`. PAS uses it at boot
12//! to populate the JWKS document without re-encoding the key material.
13
14use jsonwebtoken::{DecodingKey, EncodingKey};
15
16use crate::access_token::IssueError;
17use crate::KeySet;
18
19pub struct SigningKey {
20 inner: EncodingKey,
21 kid: String,
22}
23
24impl SigningKey {
25 /// Parse an Ed25519 private key from PEM and pair it with the given
26 /// `kid`. Returns `KeyParse` on any decode failure — the variant
27 /// name is the audit signal; the wrapped string carries the
28 /// library's diagnostic for incident response.
29 pub fn from_ed25519_pem(pem: &[u8], kid: impl Into<String>) -> Result<Self, IssueError> {
30 let inner =
31 EncodingKey::from_ed_pem(pem).map_err(|e| IssueError::KeyParse(e.to_string()))?;
32 Ok(Self {
33 inner,
34 kid: kid.into(),
35 })
36 }
37
38 /// Returns the `kid` associated with this signer.
39 pub fn kid(&self) -> &str {
40 &self.kid
41 }
42
43 /// Test-only constructor — returns a `(SigningKey, KeySet)` pair
44 /// where the KeySet already carries the matching decoding key under
45 /// the same kid. Round-trip integration tests rely on this contract:
46 /// `issue` with the signing half and `verify` against the key set
47 /// must agree without manual wiring.
48 ///
49 /// The PEM constants are deterministic across runs (checked-in test
50 /// material; same key as `tests/jwt_negative.rs::TEST_PRIVATE_KEY_PEM`),
51 /// so failures are reproducible. The private key has no production
52 /// value.
53 #[allow(clippy::expect_used)]
54 pub fn test_pair() -> (Self, KeySet) {
55 const TEST_PRIVATE_KEY_PEM: &[u8] = b"-----BEGIN PRIVATE KEY-----
56MC4CAQAwBQYDK2VwBCIEIG+00IvEd4uv6IWtGFVUEBVdqnXiuI/ESQHu6rmcDvAs
57-----END PRIVATE KEY-----
58";
59 const TEST_PUBLIC_KEY_PEM: &[u8] = b"-----BEGIN PUBLIC KEY-----
60MCowBQYDK2VwAyEAh//e6j3It3xhjghg8Kpn2pM0jMCH/cvemGu4vv7D1Q4=
61-----END PUBLIC KEY-----
62";
63 const TEST_KID: &str = "k4.test.0";
64
65 let signer = Self::from_ed25519_pem(TEST_PRIVATE_KEY_PEM, TEST_KID)
66 .expect("checked-in test PEM should always parse");
67 let mut key_set = KeySet::new();
68 let dec = DecodingKey::from_ed_pem(TEST_PUBLIC_KEY_PEM)
69 .expect("checked-in test PEM should always parse");
70 key_set.insert(TEST_KID, dec);
71 (signer, key_set)
72 }
73
74 #[allow(dead_code)] // wired up by `engine::encode::issue` in commit 3.3
75 pub(crate) fn encoding(&self) -> &EncodingKey {
76 &self.inner
77 }
78}
79
80/// Derive the 32-byte Ed25519 public key from a PKCS8-encoded private
81/// PEM. Used by PAS at boot to populate `/.well-known/jwks.json` from the
82/// same key material that produces issuance signatures, so issuer and
83/// publisher cannot drift.
84///
85/// Internals: `ed25519-compact` parses the PEM and exposes the matching
86/// public key. We avoid `jsonwebtoken`'s own EncodingKey here because it
87/// does not expose pubkey extraction on its public API. `ed25519-compact`
88/// is the smallest pure-Rust path; it carries no `unsafe`, no I/O, no
89/// global state.
90///
91/// Errors as `IssueError::KeyParse` to match `from_ed25519_pem`'s error
92/// shape (operators see one variant for "private PEM didn't load");
93/// the wrapped string carries the underlying library's diagnostic.
94pub fn ed25519_public_from_pem(pem: &[u8]) -> Result<[u8; 32], IssueError> {
95 let pem_str = std::str::from_utf8(pem)
96 .map_err(|e| IssueError::KeyParse(format!("PEM utf8: {e}")))?;
97 let secret = ed25519_compact::SecretKey::from_pem(pem_str)
98 .map_err(|e| IssueError::KeyParse(format!("ed25519 pem decode: {e}")))?;
99 Ok(*secret.public_key())
100}
101
102#[cfg(test)]
103mod tests {
104 use super::*;
105
106 #[test]
107 fn public_from_pem_matches_test_pair_public_key() {
108 // The test_pair public key bytes come from a known DER blob in
109 // signing_key.rs. The PEM-side derivation must agree with the
110 // public PEM that test_pair already encodes — this is the
111 // round-trip that PAS depends on at boot for JWKS construction.
112 const TEST_PRIVATE_KEY_PEM: &[u8] = b"-----BEGIN PRIVATE KEY-----
113MC4CAQAwBQYDK2VwBCIEIG+00IvEd4uv6IWtGFVUEBVdqnXiuI/ESQHu6rmcDvAs
114-----END PRIVATE KEY-----
115";
116 // SPKI DER for the matching public key. Last 32 bytes = raw pk.
117 const TEST_PUBLIC_KEY_SPKI_B64: &str =
118 "MCowBQYDK2VwAyEAh//e6j3It3xhjghg8Kpn2pM0jMCH/cvemGu4vv7D1Q4=";
119
120 use base64::Engine as _;
121 let spki = base64::engine::general_purpose::STANDARD
122 .decode(TEST_PUBLIC_KEY_SPKI_B64)
123 .expect("test SPKI must decode");
124 let expected: [u8; 32] = spki[12..].try_into().expect("SPKI carries 32-byte pk");
125
126 let derived = ed25519_public_from_pem(TEST_PRIVATE_KEY_PEM)
127 .expect("checked-in test PEM must derive");
128 assert_eq!(
129 derived, expected,
130 "PEM-derived public key must match the test_pair fixture",
131 );
132 }
133
134 #[test]
135 fn public_from_pem_rejects_non_pem() {
136 let err = ed25519_public_from_pem(b"not a pem at all").expect_err("garbage must reject");
137 match err {
138 IssueError::KeyParse(_) => {}
139 other => panic!("expected KeyParse, got {other:?}"),
140 }
141 }
142}