Skip to main content

nono_proxy/tls_intercept/
cert_cache.rs

1//! Per-hostname leaf certificate minting and cache.
2//!
3//! The cache lives for the duration of one proxy session. Each entry is a
4//! freshly minted ECDSA P-256 leaf certificate signed by the session's
5//! [`EphemeralCa`] and matched against the SNI hostname presented by the
6//! agent during the inner TLS handshake.
7//!
8//! ## Why no LRU eviction
9//!
10//! Typical agent workloads hit a handful of distinct hosts (`api.openai.com`,
11//! `api.anthropic.com`, `api.github.com`, …). The cache is naturally bounded
12//! by the per-session host set and is dropped — along with the CA — when the
13//! proxy shuts down. An LRU policy would add complexity without payoff.
14//!
15//! ## Failure mode
16//!
17//! When `resolve()` is invoked by rustls during a handshake and minting
18//! fails, the resolver returns `None`. rustls then fails the handshake,
19//! the agent sees a TLS error, and the proxy's CONNECT handler records
20//! the failure as a denied audit event. This matches the design constraint
21//! "hard fail on cert pinning": we never silently fall back to a transparent
22//! tunnel for a route that asked for L7 visibility.
23
24use crate::error::{ProxyError, Result};
25use crate::tls_intercept::ca::EphemeralCa;
26use rcgen::{
27    CertificateParams, DistinguishedName, DnType, KeyPair, PKCS_ECDSA_P256_SHA256, SanType,
28};
29use rustls::pki_types::{PrivateKeyDer, PrivatePkcs8KeyDer};
30use rustls::server::{ClientHello, ResolvesServerCert};
31use rustls::sign::CertifiedKey;
32use std::collections::HashMap;
33use std::sync::{Arc, Mutex};
34use std::time::{Duration, SystemTime};
35use time::OffsetDateTime;
36use tracing::{debug, warn};
37
38/// Validity window for minted leaf certificates. Short enough that even a
39/// stolen leaf becomes useless quickly; long enough that no plausible
40/// HTTP request will outlive it.
41const LEAF_VALIDITY: Duration = Duration::from_secs(60 * 60);
42
43/// Per-hostname leaf certificate cache backed by the session's [`EphemeralCa`].
44pub struct CertCache {
45    ca: Arc<EphemeralCa>,
46    /// Hostname → minted leaf. Kept behind a `Mutex` because rustls' cert
47    /// resolver is invoked from sync handshake context.
48    cache: Mutex<HashMap<String, Arc<CertifiedKey>>>,
49}
50
51impl CertCache {
52    /// Construct a new cache backed by `ca`.
53    #[must_use]
54    pub fn new(ca: Arc<EphemeralCa>) -> Self {
55        Self {
56            ca,
57            cache: Mutex::new(HashMap::new()),
58        }
59    }
60
61    /// Number of cached entries (test-only visibility).
62    #[cfg(test)]
63    fn len(&self) -> usize {
64        self.cache
65            .lock()
66            .map(|guard| guard.len())
67            .unwrap_or_default()
68    }
69
70    /// Look up or mint a leaf certificate for `hostname`.
71    ///
72    /// Used by tests; production code goes through [`ResolvesServerCert`].
73    pub fn get_or_mint(&self, hostname: &str) -> Result<Arc<CertifiedKey>> {
74        // Reject empty hostnames defensively. rustls already validates SNI
75        // shape, but we don't trust upstream invariants for a key path.
76        if hostname.is_empty() {
77            return Err(ProxyError::Config(
78                "cannot mint leaf certificate for empty hostname".to_string(),
79            ));
80        }
81
82        let mut cache = self.cache.lock().map_err(|_| {
83            ProxyError::Config("tls_intercept cert cache mutex poisoned".to_string())
84        })?;
85        if let Some(existing) = cache.get(hostname) {
86            return Ok(Arc::clone(existing));
87        }
88        let minted = mint_leaf(self.ca.as_ref(), hostname)?;
89        cache.insert(hostname.to_string(), Arc::clone(&minted));
90        debug!("tls_intercept: minted leaf certificate for {}", hostname);
91        Ok(minted)
92    }
93}
94
95impl std::fmt::Debug for CertCache {
96    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97        let len = self.cache.lock().map(|g| g.len()).unwrap_or(0);
98        f.debug_struct("CertCache")
99            .field("entries", &len)
100            .field("ca", &self.ca)
101            .finish()
102    }
103}
104
105impl ResolvesServerCert for CertCache {
106    /// rustls invokes this synchronously during the server-side handshake.
107    /// We extract the SNI hostname, look up (or mint) a leaf, and return it.
108    /// On failure — empty SNI, mint error, mutex poison — we return `None`,
109    /// causing rustls to fail the handshake. That's what we want: the agent
110    /// sees a TLS error, the CONNECT handler records the failure, no
111    /// silent fallback occurs.
112    fn resolve(&self, client_hello: ClientHello<'_>) -> Option<Arc<CertifiedKey>> {
113        let hostname = client_hello.server_name()?;
114        match self.get_or_mint(hostname) {
115            Ok(ck) => Some(ck),
116            Err(e) => {
117                warn!(
118                    "tls_intercept: failed to mint leaf for SNI '{}': {}",
119                    hostname, e
120                );
121                None
122            }
123        }
124    }
125}
126
127/// Mint a fresh leaf certificate for `hostname`, signed by `ca`.
128fn mint_leaf(ca: &EphemeralCa, hostname: &str) -> Result<Arc<CertifiedKey>> {
129    // Generate a new key pair for this leaf. Distinct from the CA key:
130    // we never expose the CA's signing key in any TLS handshake.
131    let leaf_key = KeyPair::generate_for(&PKCS_ECDSA_P256_SHA256)
132        .map_err(|e| ProxyError::Config(format!("failed to generate leaf key pair: {}", e)))?;
133    let leaf_key_der = leaf_key.serialize_der();
134
135    let mut params = CertificateParams::default();
136    params.subject_alt_names = vec![dns_san(hostname)?];
137    // RFC 5280 §4.2.1.1 requires Authority Key Identifier on certs issued
138    // by a CA; rcgen defaults the flag to false. Stricter verifiers
139    // (OpenSSL 3.6+, BoringSSL) reject leaves without AKI with
140    // "Missing Authority Key Identifier".
141    params.use_authority_key_identifier_extension = true;
142
143    let now = SystemTime::now();
144    params.not_before = system_time_to_offset(now)?;
145    params.not_after = system_time_to_offset(now + LEAF_VALIDITY)?;
146
147    let mut dn = DistinguishedName::new();
148    dn.push(DnType::CommonName, hostname);
149    params.distinguished_name = dn;
150
151    let cert = params
152        .signed_by(&leaf_key, ca.issuer())
153        .map_err(|e| ProxyError::Config(format!("failed to sign leaf certificate: {}", e)))?;
154    let leaf_der = cert.der().clone();
155
156    let private_key_der = PrivateKeyDer::Pkcs8(PrivatePkcs8KeyDer::from(leaf_key_der));
157    let signing_key = rustls::crypto::ring::sign::any_supported_type(&private_key_der)
158        .map_err(|e| ProxyError::Config(format!("rustls rejected minted leaf key: {}", e)))?;
159
160    Ok(Arc::new(CertifiedKey::new(vec![leaf_der], signing_key)))
161}
162
163/// Build a Subject Alternative Name entry for `hostname`. Reject anything
164/// that isn't a plausible DNS name to avoid emitting bogus certs for
165/// IP-literal or malformed CONNECT targets.
166fn dns_san(hostname: &str) -> Result<SanType> {
167    if !is_plausible_dns_name(hostname) {
168        return Err(ProxyError::Config(format!(
169            "tls_intercept: refusing to mint leaf for non-DNS hostname '{}'",
170            hostname
171        )));
172    }
173    Ok(SanType::DnsName(hostname.to_string().try_into().map_err(
174        |e| ProxyError::Config(format!("invalid DNS name '{}': {}", hostname, e)),
175    )?))
176}
177
178/// Lightweight DNS-name shape check. Not a full RFC 1035 validator —
179/// rustls will reject syntactically malformed certs at handshake time —
180/// but keeps obvious garbage out of the cache key.
181fn is_plausible_dns_name(s: &str) -> bool {
182    if s.is_empty() || s.len() > 253 {
183        return false;
184    }
185    s.chars()
186        .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '.')
187        && s.contains(|c: char| c.is_ascii_alphabetic())
188}
189
190fn system_time_to_offset(t: SystemTime) -> Result<OffsetDateTime> {
191    OffsetDateTime::from_unix_timestamp(
192        t.duration_since(SystemTime::UNIX_EPOCH)
193            .map_err(|e| ProxyError::Config(format!("system time before unix epoch: {}", e)))?
194            .as_secs()
195            .try_into()
196            .map_err(|_| ProxyError::Config("system time exceeds i64::MAX".to_string()))?,
197    )
198    .map_err(|e| ProxyError::Config(format!("invalid system time for cert validity: {}", e)))
199}
200
201#[cfg(test)]
202#[allow(clippy::unwrap_used)]
203mod tests {
204    use super::*;
205
206    fn fresh_cache() -> CertCache {
207        CertCache::new(Arc::new(EphemeralCa::generate().unwrap()))
208    }
209
210    #[test]
211    fn mint_returns_well_formed_cert() {
212        let cache = fresh_cache();
213        let ck = cache.get_or_mint("api.openai.com").unwrap();
214        assert_eq!(ck.cert.len(), 1, "should be a single-cert chain");
215        assert!(
216            !ck.cert[0].as_ref().is_empty(),
217            "DER body must be non-empty"
218        );
219        // The first byte of an X.509 certificate's DER encoding is 0x30
220        // (SEQUENCE). A trivial sanity check that we produced something
221        // shaped like a certificate.
222        assert_eq!(ck.cert[0].as_ref()[0], 0x30);
223    }
224
225    #[test]
226    fn minted_leaf_carries_authority_key_identifier() {
227        // OpenSSL 3.6+ (Python 3.14) rejects issued certs without AKI with
228        // "Missing Authority Key Identifier". rcgen defaults the flag off,
229        // so we set it explicitly in `mint_leaf`. Verify the extension OID
230        // 2.5.29.35 (DER bytes 06 03 55 1d 23) is present in the leaf DER.
231        let cache = fresh_cache();
232        let ck = cache.get_or_mint("api.example.com").unwrap();
233        let der = ck.cert[0].as_ref();
234        let aki_oid = [0x06, 0x03, 0x55, 0x1d, 0x23];
235        assert!(
236            der.windows(aki_oid.len()).any(|w| w == aki_oid),
237            "minted leaf must include Authority Key Identifier (OID 2.5.29.35)"
238        );
239    }
240
241    #[test]
242    fn cache_hits_on_repeated_lookup() {
243        let cache = fresh_cache();
244        let a = cache.get_or_mint("api.example.com").unwrap();
245        let b = cache.get_or_mint("api.example.com").unwrap();
246        assert!(Arc::ptr_eq(&a, &b), "second lookup should be a cache hit");
247        assert_eq!(cache.len(), 1);
248    }
249
250    #[test]
251    fn distinct_hostnames_get_distinct_certs() {
252        let cache = fresh_cache();
253        let a = cache.get_or_mint("api.openai.com").unwrap();
254        let b = cache.get_or_mint("api.anthropic.com").unwrap();
255        assert!(!Arc::ptr_eq(&a, &b));
256        assert_ne!(a.cert[0].as_ref(), b.cert[0].as_ref());
257        assert_eq!(cache.len(), 2);
258    }
259
260    #[test]
261    fn empty_hostname_rejected() {
262        let cache = fresh_cache();
263        assert!(cache.get_or_mint("").is_err());
264    }
265
266    #[test]
267    fn ip_literal_rejected() {
268        // We refuse to mint for IP-literal CONNECT targets — the SNI shape
269        // would be wrong and the agent would reject the cert anyway.
270        let cache = fresh_cache();
271        assert!(cache.get_or_mint("127.0.0.1").is_err());
272        assert!(cache.get_or_mint("::1").is_err());
273    }
274
275    #[test]
276    fn plausible_dns_name_filter() {
277        assert!(is_plausible_dns_name("api.openai.com"));
278        assert!(is_plausible_dns_name("internal-service.corp"));
279        assert!(!is_plausible_dns_name(""));
280        assert!(!is_plausible_dns_name("127.0.0.1")); // no alphabetic
281        assert!(!is_plausible_dns_name("evil host"));
282        assert!(!is_plausible_dns_name(&"a".repeat(254)));
283    }
284}