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