Skip to main content

moq_native/
tls.rs

1//! TLS trust, certificates, and keys, split by role.
2//!
3//! [`Client`] (`--client-tls-*`) picks who to trust: system roots, custom roots,
4//! a pinned SHA-256 fingerprint, or nothing at all. [`Server`] (`--server-tls-*`)
5//! supplies the certificate chain to serve, loaded from disk or self-signed on
6//! startup, and optionally the roots that authenticate mTLS clients.
7//!
8//! Certificates loaded from disk are watched and hot reloaded, so rotating them
9//! needs no restart. [`Certificates`] reads the current set back out.
10
11use crate::crypto;
12use rustls::pki_types::pem::PemObject;
13use rustls::pki_types::{CertificateDer, PrivateKeyDer, ServerName, UnixTime};
14use std::path::{Path, PathBuf};
15use std::sync::Arc;
16use std::{fs, io};
17
18#[cfg(all(
19	any(feature = "quinn", feature = "noq", feature = "quiche"),
20	any(feature = "aws-lc-rs", feature = "ring")
21))]
22use rustls::pki_types::PrivatePkcs8KeyDer;
23#[cfg(any(feature = "quinn", feature = "noq", feature = "quiche"))]
24use std::sync::RwLock;
25
26/// Errors loading or generating TLS certificates and keys.
27///
28/// Shared by the client TLS config and the quinn/noq servers so each backend's
29/// error type can compose it via `#[from]`.
30#[derive(Debug, thiserror::Error)]
31#[non_exhaustive]
32pub enum Error {
33	/// A certificate file couldn't be opened, usually a bad path or permissions.
34	#[error("failed to open certificate file")]
35	Open(#[source] std::io::Error),
36
37	/// A certificate or key file was opened but couldn't be read to the end.
38	#[error("failed to read file")]
39	ReadFile(#[source] std::io::Error),
40
41	/// A file's contents aren't valid PEM certificates.
42	#[error("failed to read certificates")]
43	Read(#[source] rustls::pki_types::pem::Error),
44
45	/// A file's contents aren't a valid PEM private key.
46	#[error("failed to parse private key")]
47	Key(#[source] rustls::pki_types::pem::Error),
48
49	/// A PEM file parsed cleanly but held no certificates.
50	#[error("no certificates found")]
51	Empty,
52
53	/// A root PEM file parsed cleanly but held no certificates, so it would trust nothing.
54	#[error("no roots found in {}", .0.display())]
55	EmptyRoots(PathBuf),
56
57	/// Nothing is configured that could ever verify a server certificate.
58	#[error(
59		"no trusted roots: provide --client-tls-root, enable --client-tls-system-roots, or use --client-tls-fingerprint / --client-tls-disable-verify"
60	)]
61	NoRoots,
62
63	/// A configured fingerprint isn't valid hex.
64	#[error("invalid TLS fingerprint (expected hex-encoded SHA-256)")]
65	Fingerprint(#[source] hex::FromHexError),
66
67	/// A configured fingerprint is valid hex but the wrong size for a SHA-256 digest.
68	#[error("invalid TLS fingerprint length: expected 32 bytes (SHA-256), got {0}")]
69	FingerprintLength(usize),
70
71	/// Fingerprint pinning was combined with CA roots. Pinning bypasses the chain, so one of
72	/// the two would be silently ignored.
73	#[error(
74		"--client-tls-fingerprint cannot be combined with --client-tls-root or --client-tls-system-roots: fingerprint pinning bypasses CA verification"
75	)]
76	FingerprintWithRoots,
77
78	/// Trust material was configured alongside the flag that ignores all of it.
79	#[error(
80		"--client-tls-disable-verify cannot be combined with --client-tls-fingerprint, --client-tls-root or --client-tls-system-roots: it accepts every certificate, so the trust material would be ignored"
81	)]
82	DisableVerifyWithTrust,
83
84	/// A root certificate parsed as PEM but rustls rejected it as a trust anchor.
85	#[error("failed to add root certificate")]
86	AddRoot(#[source] rustls::Error),
87
88	/// The JNI call in [`init_android`] failed, so the platform verifier is unavailable.
89	#[cfg(target_os = "android")]
90	#[error("failed to initialize the Android platform verifier")]
91	AndroidInit(#[source] jni::errors::Error),
92
93	/// rustls rejected the mTLS client certificate and key, e.g. they don't match.
94	#[error("failed to configure client certificate")]
95	ClientAuth(#[source] rustls::Error),
96
97	/// Only one half of the mTLS client identity was given; it needs both a cert and a key.
98	#[error("both --client-tls-cert and --client-tls-key must be provided")]
99	IncompleteClientAuth,
100
101	/// The server was given a different number of certificates than keys. They pair by index.
102	#[error("must provide both cert and key")]
103	CertKeyCountMismatch,
104
105	/// The server has no certificate to serve: no cert/key pair and no hostnames to generate one for.
106	#[error("must provide at least one cert/key pair or generate entry")]
107	NoCertSource,
108
109	/// A server cert/key pair was paired up by index but the key isn't the certificate's.
110	#[error("private key {} doesn't match certificate {}", key.display(), cert.display())]
111	KeyMismatch {
112		/// Path of the private key file.
113		key: PathBuf,
114		/// Path of the certificate file it was paired with.
115		cert: PathBuf,
116		/// Why rustls says the two don't match.
117		#[source]
118		source: rustls::Error,
119	},
120
121	/// A rustls error with no more specific context, e.g. building a config.
122	#[error(transparent)]
123	Rustls(#[from] rustls::Error),
124
125	/// The mTLS client-certificate verifier couldn't be built from the configured roots.
126	#[cfg(any(feature = "quinn", feature = "noq", feature = "quiche"))]
127	#[error("failed to build client certificate verifier")]
128	ClientVerifier(#[source] rustls::server::VerifierBuilderError),
129
130	/// Generating a self-signed certificate failed.
131	#[cfg(any(feature = "quinn", feature = "noq", feature = "quiche"))]
132	#[error(transparent)]
133	Rcgen(#[from] rcgen::Error),
134
135	/// The crate was built without a crypto provider, so no TLS is possible.
136	#[error("no crypto provider available; enable aws-lc-rs or ring feature")]
137	NoCryptoProvider,
138}
139
140/// Convenience alias for results produced by this module.
141pub type Result<T> = std::result::Result<T, Error>;
142
143/// Parse a hex-encoded SHA-256 certificate fingerprint.
144pub fn parse_fingerprint(value: &str) -> Result<[u8; 32]> {
145	let bytes = hex::decode(value.trim()).map_err(Error::Fingerprint)?;
146	bytes.try_into().map_err(|v: Vec<u8>| Error::FingerprintLength(v.len()))
147}
148
149/// Read a PEM file into its list of certificates.
150pub(crate) fn read_certs(path: &Path) -> Result<Vec<CertificateDer<'static>>> {
151	let file = fs::File::open(path).map_err(Error::Open)?;
152	let mut reader = io::BufReader::new(file);
153	CertificateDer::pem_reader_iter(&mut reader)
154		.collect::<std::result::Result<_, _>>()
155		.map_err(Error::Read)
156}
157
158// ── Client ──────────────────────────────────────────────────────────
159
160/// TLS configuration for the client.
161#[serde_with::serde_as]
162#[derive(Clone, Default, Debug, clap::Args, serde::Serialize, serde::Deserialize)]
163#[serde(default, deny_unknown_fields)]
164#[group(id = "tls-client")]
165#[non_exhaustive]
166pub struct Client {
167	/// Trust the TLS root at this path, encoded as PEM.
168	///
169	/// This value can be provided multiple times for multiple roots.
170	/// In config files, accepts either a single string or a TOML array.
171	///
172	/// These roots are added on top of the system roots. By default the system
173	/// roots are only loaded when no custom root is given, so passing a root
174	/// replaces them; set `--client-tls-system-roots` to trust both (e.g. to reach a
175	/// local relay with a private CA and a remote one with a public CA).
176	#[serde(skip_serializing_if = "Vec::is_empty")]
177	#[arg(id = "client-tls-root", long = "client-tls-root", env = "MOQ_CLIENT_TLS_ROOT")]
178	#[serde_as(as = "serde_with::OneOrMany<_>")]
179	pub root: Vec<PathBuf>,
180
181	/// Also trust the platform's native root certificates.
182	///
183	/// Defaults to enabled only when no `--client-tls-root` is given. Set it
184	/// explicitly to trust the system roots alongside any custom roots, or set it
185	/// to false to trust only the custom roots. Trusting neither (no custom root
186	/// and system roots disabled) is rejected, since verification could never pass.
187	#[serde(skip_serializing_if = "Option::is_none")]
188	#[arg(
189		id = "client-tls-system-roots",
190		long = "client-tls-system-roots",
191		env = "MOQ_CLIENT_TLS_SYSTEM_ROOTS",
192		default_missing_value = "true",
193		num_args = 0..=1,
194		require_equals = true,
195		value_parser = clap::value_parser!(bool),
196	)]
197	pub system_roots: Option<bool>,
198
199	/// Pin the peer to a certificate with one of these SHA-256 fingerprints, encoded as hex.
200	///
201	/// This is the native equivalent of the browser's WebTransport `serverCertificateHashes`,
202	/// and accepts the same values a server reports via its certificate fingerprints. Use it to
203	/// trust a self-signed certificate without disabling verification or fetching the hash over
204	/// an insecure `http://` request. When set, the normal CA/root chain is bypassed: only the
205	/// leaf certificate's fingerprint is checked.
206	///
207	/// This value can be provided multiple times to accept any of several fingerprints (e.g.
208	/// across a certificate rotation). In config files, accepts either a single string or a TOML array.
209	#[serde(skip_serializing_if = "Vec::is_empty")]
210	#[arg(
211		id = "client-tls-fingerprint",
212		long = "client-tls-fingerprint",
213		env = "MOQ_CLIENT_TLS_FINGERPRINT"
214	)]
215	#[serde_as(as = "serde_with::OneOrMany<_>")]
216	pub fingerprint: Vec<String>,
217
218	/// PEM file containing the client certificate chain for mTLS.
219	///
220	/// Only certificates are extracted; any private keys in the file are ignored.
221	/// Must be paired with `--client-tls-key`.
222	#[serde(skip_serializing_if = "Option::is_none")]
223	#[arg(id = "client-tls-cert", long = "client-tls-cert", env = "MOQ_CLIENT_TLS_CERT")]
224	pub cert: Option<PathBuf>,
225
226	/// PEM file containing the private key for mTLS.
227	///
228	/// Only the private key is extracted; any certificates in the file are ignored.
229	/// Must be paired with `--client-tls-cert`.
230	#[serde(skip_serializing_if = "Option::is_none")]
231	#[arg(id = "client-tls-key", long = "client-tls-key", env = "MOQ_CLIENT_TLS_KEY")]
232	pub key: Option<PathBuf>,
233
234	/// Danger: Disable TLS certificate verification.
235	///
236	/// Fine for local development and between relays, but should be used in caution in production.
237	#[serde(skip_serializing_if = "Option::is_none")]
238	#[arg(
239		id = "client-tls-disable-verify",
240		long = "client-tls-disable-verify",
241		env = "MOQ_CLIENT_TLS_DISABLE_VERIFY",
242		default_missing_value = "true",
243		num_args = 0..=1,
244		require_equals = true,
245		value_parser = clap::value_parser!(bool),
246	)]
247	pub disable_verify: Option<bool>,
248
249	/// Override the TLS SNI and certificate verification hostname for outbound connections.
250	///
251	/// When unset, the connect URL's host is used (default behavior). Useful when dialing a
252	/// raw IP address but needing to present/verify a DNS name the server certificate covers.
253	#[serde(skip_serializing_if = "Option::is_none")]
254	#[arg(
255		id = "client-tls-host-name",
256		long = "client-tls-host-name",
257		env = "MOQ_CLIENT_TLS_HOST_NAME"
258	)]
259	pub host_name: Option<String>,
260
261	/// Deprecated `--tls-*` spellings, folded into the canonical fields above with
262	/// a warning. Private and hidden so they stay off the public surface; not a
263	/// TOML field (config files use the canonical names).
264	#[command(flatten)]
265	#[serde(skip)]
266	deprecated: Deprecated,
267}
268
269/// Holds the deprecated bare `--tls-*` flag spellings (renamed to `--client-tls-*`).
270/// Flattened into [`Client`] so they keep parsing; folded into the canonical
271/// fields by [`Client::build`] with a deprecation warning. No env (the env names
272/// were never renamed) and no TOML.
273#[derive(Clone, Default, Debug, clap::Args)]
274struct Deprecated {
275	#[arg(long = "tls-root", hide = true)]
276	root: Vec<PathBuf>,
277
278	#[arg(
279		long = "tls-system-roots",
280		hide = true,
281		default_missing_value = "true",
282		num_args = 0..=1,
283		require_equals = true,
284		value_parser = clap::value_parser!(bool),
285	)]
286	system_roots: Option<bool>,
287
288	#[arg(long = "tls-fingerprint", hide = true)]
289	fingerprint: Vec<String>,
290
291	#[arg(
292		long = "tls-disable-verify",
293		hide = true,
294		default_missing_value = "true",
295		num_args = 0..=1,
296		require_equals = true,
297		value_parser = clap::value_parser!(bool),
298	)]
299	disable_verify: Option<bool>,
300}
301
302/// The resolved server-certificate verification policy.
303///
304/// Computed once by [Client::verification] and shared by every backend (the
305/// rustls-based quinn/noq via [Client::build], and quiche directly) so they
306/// agree on precedence, the system-roots default, and which flag combinations
307/// are valid.
308#[derive(Clone)]
309pub(crate) enum Verification {
310	/// No verification at all. Insecure; only via `--client-tls-disable-verify`.
311	Disabled,
312
313	/// Pin the leaf certificate by SHA-256. The CA chain is not consulted, so
314	/// this is mutually exclusive with any roots.
315	Fingerprints(Vec<[u8; 32]>),
316
317	/// Standard CA verification. When `system` is set the platform/default trust
318	/// store is trusted too; each backend resolves that its own way (the rustls
319	/// backends use the OS platform verifier, quiche loads the native roots).
320	/// `custom` are extra PEM roots trusted in addition.
321	Roots {
322		custom: Vec<CertificateDer<'static>>,
323		system: bool,
324	},
325}
326
327impl Client {
328	/// Log a warning for each deprecated `--tls-*` flag in use. Called once from
329	/// [`Self::verification`], which every backend runs, so a deprecated flag warns once.
330	pub(crate) fn warn_deprecated(&self) {
331		if !self.deprecated.root.is_empty() {
332			tracing::warn!("--tls-root is deprecated; use --client-tls-root");
333		}
334		if self.deprecated.system_roots.is_some() {
335			tracing::warn!("--tls-system-roots is deprecated; use --client-tls-system-roots");
336		}
337		if !self.deprecated.fingerprint.is_empty() {
338			tracing::warn!("--tls-fingerprint is deprecated; use --client-tls-fingerprint");
339		}
340		if self.deprecated.disable_verify.is_some() {
341			tracing::warn!("--tls-disable-verify is deprecated; use --client-tls-disable-verify");
342		}
343	}
344
345	/// Roots from the canonical field plus the deprecated `--tls-root` spelling.
346	pub(crate) fn effective_root(&self) -> Vec<PathBuf> {
347		let mut root = self.root.clone();
348		root.extend(self.deprecated.root.iter().cloned());
349		root
350	}
351
352	/// Fingerprints from the canonical field plus the deprecated `--tls-fingerprint`.
353	pub(crate) fn effective_fingerprint(&self) -> Vec<String> {
354		let mut fp = self.fingerprint.clone();
355		fp.extend(self.deprecated.fingerprint.iter().cloned());
356		fp
357	}
358
359	/// `system_roots`, preferring the canonical flag over the deprecated alias.
360	pub(crate) fn effective_system_roots(&self) -> Option<bool> {
361		self.system_roots.or(self.deprecated.system_roots)
362	}
363
364	/// `disable_verify`, preferring the canonical flag over the deprecated alias.
365	pub(crate) fn effective_disable_verify(&self) -> Option<bool> {
366		self.disable_verify.or(self.deprecated.disable_verify)
367	}
368
369	/// Resolve the verification policy from the configured flags.
370	///
371	/// Precedence and rules (shared by all backends):
372	/// - `--client-tls-disable-verify` disables verification, and combining it with any
373	///   trust material is rejected rather than silently ignoring that material.
374	/// - `--client-tls-fingerprint` pins the leaf and bypasses the CA chain; combining
375	///   it with `--client-tls-root` or `--client-tls-system-roots` is rejected rather than
376	///   silently ignoring one of them.
377	/// - Otherwise, verify against the system roots (default) plus any custom
378	///   roots. The system roots are dropped once a custom root is given unless
379	///   `--client-tls-system-roots` re-enables them.
380	///
381	/// Every combination that would quietly drop one setting is an error. Silently
382	/// weakening trust is the worst outcome here: someone moving off
383	/// `disable_verify` by adding a fingerprint would otherwise still accept every
384	/// certificate, with the UI showing the pin as configured.
385	pub(crate) fn verification(&self) -> Result<Verification> {
386		self.warn_deprecated();
387
388		let fingerprints = self.fingerprints()?;
389		let roots = self.effective_root();
390		let system_roots = self.effective_system_roots();
391
392		if self.effective_disable_verify().unwrap_or_default() {
393			if !fingerprints.is_empty() || !roots.is_empty() || system_roots == Some(true) {
394				return Err(Error::DisableVerifyWithTrust);
395			}
396			return Ok(Verification::Disabled);
397		}
398
399		if !fingerprints.is_empty() {
400			if !roots.is_empty() || system_roots == Some(true) {
401				return Err(Error::FingerprintWithRoots);
402			}
403			return Ok(Verification::Fingerprints(fingerprints));
404		}
405
406		// Default to system roots only when no custom root is given, so passing a
407		// root replaces them unless the system roots are explicitly re-enabled.
408		let system = system_roots.unwrap_or(roots.is_empty());
409
410		let mut custom = Vec::new();
411		for root in &roots {
412			let certs = read_certs(root)?;
413			if certs.is_empty() {
414				return Err(Error::EmptyRoots(root.clone()));
415			}
416			custom.extend(certs);
417		}
418
419		// WebPKI needs at least one trusted root to ever succeed, so fail fast
420		// instead of producing confusing handshake errors later. With system
421		// trust enabled the verifier supplies its own roots, so custom roots are
422		// optional.
423		if !system && custom.is_empty() {
424			return Err(Error::NoRoots);
425		}
426
427		Ok(Verification::Roots { custom, system })
428	}
429
430	/// Whether an insecure `http://` certificate-fingerprint bootstrap may be
431	/// honored for a connection.
432	///
433	/// Only when no stronger verification is configured: an explicit
434	/// `--client-tls-fingerprint` must never be weakened by an attacker-controlled
435	/// plaintext fetch, and there is nothing to bootstrap when verification is
436	/// disabled. With CA roots (the default), `http://` is the deliberate
437	/// per-connection way to pin a self-signed relay, so it is allowed.
438	pub(crate) fn allows_http_bootstrap(&self) -> bool {
439		self.effective_fingerprint().is_empty() && !self.effective_disable_verify().unwrap_or_default()
440	}
441
442	/// Parse the configured fingerprints into fixed-size SHA-256 digests.
443	fn fingerprints(&self) -> Result<Vec<[u8; 32]>> {
444		self.effective_fingerprint()
445			.iter()
446			.map(|fp| parse_fingerprint(fp))
447			.collect()
448	}
449
450	/// Build a [`rustls::ClientConfig`] from this configuration.
451	///
452	/// Resolves the verification policy, optionally attaches a client identity
453	/// for mTLS, and installs the matching verifier.
454	pub fn build(&self) -> Result<rustls::ClientConfig> {
455		let provider = crypto::provider();
456		let verification = self.verification()?;
457
458		// Allow TLS 1.2 in addition to 1.3 for WebSocket compatibility.
459		// QUIC always negotiates TLS 1.3 regardless of this setting.
460		let builder = rustls::ClientConfig::builder_with_provider(provider.clone())
461			.with_protocol_versions(&[&rustls::version::TLS13, &rustls::version::TLS12])?;
462
463		// Install the server-certificate verifier. Disabled/Fingerprints get a
464		// placeholder empty store here and swap in their own verifier below.
465		let builder = match &verification {
466			Verification::Roots { custom, system: true } => Self::system_verifier(builder, custom, &provider)?,
467			Verification::Roots { custom, system: false } => builder.with_root_certificates(root_store(custom)?),
468			Verification::Disabled | Verification::Fingerprints(_) => {
469				builder.with_root_certificates(rustls::RootCertStore::empty())
470			}
471		};
472
473		let mut tls = self.with_client_auth(builder)?;
474
475		match verification {
476			Verification::Disabled => {
477				tracing::warn!(
478					"TLS server certificate verification is disabled; A man-in-the-middle attack is possible."
479				);
480				tls.dangerous()
481					.set_certificate_verifier(Arc::new(NoCertificateVerification(provider)));
482			}
483			Verification::Fingerprints(fingerprints) => {
484				let fingerprints = fingerprints.into_iter().map(|fp| fp.to_vec()).collect();
485				let verifier = FingerprintVerifier::new(provider, fingerprints);
486				tls.dangerous().set_certificate_verifier(Arc::new(verifier));
487			}
488			// The verifier was installed by the builder above.
489			Verification::Roots { .. } => {}
490		}
491
492		Ok(tls)
493	}
494
495	/// Build the verifier for system/default trust on the rustls backends.
496	///
497	/// Uses the OS-native platform verifier (Keychain/SecTrust, Windows
498	/// CryptoAPI, or the native store on Linux) everywhere it works, optionally
499	/// extended with `custom` PEM roots. Android's platform verifier needs JNI
500	/// setup (see [`init_android`]); until that has run we trust the bundled
501	/// Mozilla roots so verification still works out of the box.
502	fn system_verifier(
503		builder: rustls::ConfigBuilder<rustls::ClientConfig, rustls::WantsVerifier>,
504		custom: &[CertificateDer<'static>],
505		provider: &crypto::Provider,
506	) -> Result<rustls::ConfigBuilder<rustls::ClientConfig, rustls::client::WantsClientCert>> {
507		// Android's platform verifier needs JNI init (see `init_android`) and,
508		// unlike the other platforms, can't be extended with custom roots. So use
509		// it only once initialized and with no custom roots; otherwise trust the
510		// bundled Mozilla roots (plus any custom roots) so verification still works.
511		#[cfg(target_os = "android")]
512		{
513			if ANDROID_INITIALIZED.load(std::sync::atomic::Ordering::Acquire) && custom.is_empty() {
514				let verifier = rustls_platform_verifier::Verifier::new(provider.clone())?;
515				return Ok(builder.dangerous().with_custom_certificate_verifier(Arc::new(verifier)));
516			}
517
518			let mut roots = rustls::RootCertStore::empty();
519			roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
520			for cert in custom {
521				roots.add(cert.clone()).map_err(Error::AddRoot)?;
522			}
523			Ok(builder.with_root_certificates(roots))
524		}
525
526		#[cfg(not(target_os = "android"))]
527		{
528			let verifier = if custom.is_empty() {
529				rustls_platform_verifier::Verifier::new(provider.clone())?
530			} else {
531				rustls_platform_verifier::Verifier::new_with_extra_roots(custom.iter().cloned(), provider.clone())?
532			};
533			Ok(builder.dangerous().with_custom_certificate_verifier(Arc::new(verifier)))
534		}
535	}
536
537	/// Attach the optional mTLS client identity, finishing the rustls builder.
538	fn with_client_auth(
539		&self,
540		builder: rustls::ConfigBuilder<rustls::ClientConfig, rustls::client::WantsClientCert>,
541	) -> Result<rustls::ClientConfig> {
542		Ok(match (&self.cert, &self.key) {
543			(Some(cert_path), Some(key_path)) => {
544				let cert_pem = fs::read(cert_path).map_err(Error::ReadFile)?;
545				let chain: Vec<CertificateDer<'static>> = CertificateDer::pem_slice_iter(&cert_pem)
546					.collect::<std::result::Result<_, _>>()
547					.map_err(Error::Read)?;
548				if chain.is_empty() {
549					return Err(Error::Empty);
550				}
551				let key_pem = fs::read(key_path).map_err(Error::ReadFile)?;
552				let key = PrivateKeyDer::from_pem_slice(&key_pem).map_err(Error::Key)?;
553				builder.with_client_auth_cert(chain, key).map_err(Error::ClientAuth)?
554			}
555			(None, None) => builder.with_no_client_auth(),
556			_ => return Err(Error::IncompleteClientAuth),
557		})
558	}
559}
560
561/// Build a [`rustls::RootCertStore`] from a list of custom PEM roots.
562fn root_store(custom: &[CertificateDer<'static>]) -> Result<rustls::RootCertStore> {
563	let mut roots = rustls::RootCertStore::empty();
564	for cert in custom {
565		roots.add(cert.clone()).map_err(Error::AddRoot)?;
566	}
567	Ok(roots)
568}
569
570/// Whether [`init_android`] has successfully wired up the platform verifier.
571#[cfg(target_os = "android")]
572static ANDROID_INITIALIZED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
573
574/// Initialize Android platform certificate verification.
575///
576/// On Android the OS trust store is only reachable through the JVM, so the
577/// platform verifier needs a JNI handle to the application `Context` before it
578/// can be used. Call this once at startup (e.g. from `JNI_OnLoad`) with an
579/// attached [`jni::Env`] for the calling thread and the application `Context`.
580/// The `moq-ffi` bindings call it automatically, so most consumers never touch
581/// this directly.
582///
583/// Until it succeeds, clients fall back to the bundled Mozilla roots, so a
584/// missing or failed init degrades to webpki verification rather than failing.
585#[cfg(target_os = "android")]
586pub fn init_android(env: &mut jni::Env, context: jni::objects::JObject) -> Result<()> {
587	rustls_platform_verifier::android::init_with_env(env, context).map_err(Error::AndroidInit)?;
588	ANDROID_INITIALIZED.store(true, std::sync::atomic::Ordering::Release);
589	Ok(())
590}
591
592// ── Server ──────────────────────────────────────────────────────────
593
594/// TLS configuration for the server.
595///
596/// Certificate and keys must currently be files on disk.
597/// Alternatively, you can generate a self-signed certificate given a list of hostnames.
598///
599/// In config files, each list field accepts either a single string or a TOML array.
600#[serde_with::serde_as]
601#[derive(clap::Args, Clone, Default, Debug, serde::Serialize, serde::Deserialize)]
602#[serde(deny_unknown_fields)]
603#[group(id = "tls-server")]
604#[non_exhaustive]
605pub struct Server {
606	/// Load the given certificate from disk.
607	#[arg(long = "tls-cert", id = "tls-cert", env = "MOQ_SERVER_TLS_CERT")]
608	#[serde(default, skip_serializing_if = "Vec::is_empty")]
609	#[serde_as(as = "serde_with::OneOrMany<_>")]
610	pub cert: Vec<PathBuf>,
611
612	/// Load the given key from disk.
613	#[arg(long = "tls-key", id = "tls-key", env = "MOQ_SERVER_TLS_KEY")]
614	#[serde(default, skip_serializing_if = "Vec::is_empty")]
615	#[serde_as(as = "serde_with::OneOrMany<_>")]
616	pub key: Vec<PathBuf>,
617
618	/// Or generate a new certificate and key with the given hostnames.
619	/// This won't be valid unless the client uses the fingerprint or disables verification.
620	#[arg(
621		long = "tls-generate",
622		id = "tls-generate",
623		value_delimiter = ',',
624		env = "MOQ_SERVER_TLS_GENERATE"
625	)]
626	#[serde(default, skip_serializing_if = "Vec::is_empty")]
627	#[serde_as(as = "serde_with::OneOrMany<_>")]
628	pub generate: Vec<String>,
629
630	/// PEM file(s) of root CAs for validating optional client certificates (mTLS).
631	///
632	/// When set, clients *may* present a certificate during the TLS handshake.
633	/// Valid presentations are reported via [`crate::Request::peer_identity`]
634	/// and can be used by the application to grant elevated access. Clients that
635	/// do not present a certificate are unaffected.
636	///
637	/// Plain-TLS listeners built via [`Self::server_config`] also use these roots
638	/// for optional mTLS.
639	#[arg(
640		long = "server-tls-root",
641		id = "server-tls-root",
642		value_delimiter = ',',
643		env = "MOQ_SERVER_TLS_ROOT"
644	)]
645	#[serde(default, skip_serializing_if = "Vec::is_empty")]
646	#[serde_as(as = "serde_with::OneOrMany<_>")]
647	pub root: Vec<PathBuf>,
648}
649
650impl Server {
651	/// Load all configured root CAs into a [`rustls::RootCertStore`].
652	pub fn load_roots(&self) -> Result<rustls::RootCertStore> {
653		let mut roots = rustls::RootCertStore::empty();
654		for path in &self.root {
655			let certs = read_certs(path)?;
656			if certs.is_empty() {
657				return Err(Error::Empty);
658			}
659			for cert in certs {
660				roots.add(cert).map_err(Error::AddRoot)?;
661			}
662		}
663		Ok(roots)
664	}
665
666	/// Build a [`rustls::ServerConfig`] for a plain-TLS (non-QUIC) server, e.g. an
667	/// RTMPS or HTTPS listener fronting the QUIC endpoint, reusing the QUIC
668	/// backend's certificate handling: on-disk `cert`/`key` pairs, `generate`
669	/// self-signed certs, and optional mTLS `root` client CAs.
670	///
671	/// `alpn` sets the advertised ALPN protocols (e.g.
672	/// `vec![b"h2".to_vec(), b"http/1.1".to_vec()]`); pass an empty list for a
673	/// protocol like RTMPS that doesn't use ALPN.
674	#[cfg(any(feature = "noq", feature = "quinn", feature = "quiche"))]
675	pub fn server_config(&self, alpn: Vec<Vec<u8>>) -> Result<Arc<rustls::ServerConfig>> {
676		server_config(self, alpn)
677	}
678}
679
680/// Build a [`rustls::ServerConfig`] from a [`Server`] for a plain-TLS listener.
681#[cfg(any(feature = "noq", feature = "quinn", feature = "quiche"))]
682fn server_config(config: &Server, alpn: Vec<Vec<u8>>) -> Result<Arc<rustls::ServerConfig>> {
683	let provider = crypto::provider();
684
685	let certs = ServeCerts::new(provider.clone());
686	certs.load_certs(config)?;
687	let certs = Arc::new(certs);
688
689	// TCP can negotiate TLS 1.2 as well as 1.3, unlike QUIC which is 1.3-only.
690	let builder =
691		rustls::ServerConfig::builder_with_provider(provider.clone()).with_safe_default_protocol_versions()?;
692
693	let mut tls = if config.root.is_empty() {
694		builder.with_no_client_auth().with_cert_resolver(certs)
695	} else {
696		let roots = config.load_roots()?;
697		let verifier = rustls::server::WebPkiClientVerifier::builder_with_provider(Arc::new(roots), provider)
698			.allow_unauthenticated()
699			.build()
700			.map_err(Error::ClientVerifier)?;
701		builder.with_client_cert_verifier(verifier).with_cert_resolver(certs)
702	};
703
704	tls.alpn_protocols = alpn;
705	Ok(Arc::new(tls))
706}
707
708/// A peer's validated client-certificate chain from the mTLS handshake.
709///
710/// Returned by [`crate::Request::peer_identity`] when the peer presented a
711/// certificate that chained to a configured [`Server::root`]. Owns the chain
712/// (leaf first) so callers can inspect it, e.g. [`expiry`](Self::expiry),
713/// without re-parsing the type-erased QUIC identity.
714#[derive(Clone)]
715pub struct PeerIdentity {
716	chain: Vec<CertificateDer<'static>>,
717}
718
719impl PeerIdentity {
720	/// Wrap the type-erased identity from `quinn::Connection::peer_identity`.
721	/// Returns `None` if the peer presented no certificate or the identity is
722	/// not a certificate chain.
723	#[cfg(any(feature = "quinn", feature = "noq"))]
724	pub(crate) fn from_any(identity: Option<Box<dyn std::any::Any>>) -> Option<Self> {
725		let chain = identity?.downcast::<Vec<CertificateDer<'static>>>().ok()?;
726		Some(Self { chain: *chain })
727	}
728
729	/// Wrap a certificate chain already exposed by a QUIC backend.
730	#[cfg(feature = "quiche")]
731	pub(crate) fn from_chain(chain: Vec<CertificateDer<'static>>) -> Self {
732		Self { chain }
733	}
734
735	/// The validated certificate chain, leaf first.
736	///
737	/// Exposes [`rustls::pki_types::CertificateDer`] directly (already part of
738	/// this crate's public API via the `rustls` re-export), so a major `rustls`
739	/// bump is a breaking change for consumers of this method.
740	pub fn chain(&self) -> &[CertificateDer<'static>] {
741		&self.chain
742	}
743
744	/// The leaf certificate's `notAfter`, if it parses. A `notAfter` before the
745	/// Unix epoch is reported as `None`.
746	pub fn expiry(&self) -> Option<std::time::SystemTime> {
747		use std::time::{Duration, UNIX_EPOCH};
748
749		let leaf = self.chain.first()?;
750		let (_, cert) = x509_parser::parse_x509_certificate(leaf).ok()?;
751		let secs = u64::try_from(cert.validity().not_after.timestamp()).ok()?;
752		Some(UNIX_EPOCH + Duration::from_secs(secs))
753	}
754}
755
756/// The certificates a server is currently serving.
757#[derive(Debug, Default)]
758pub(crate) struct Info {
759	#[cfg(any(feature = "noq", feature = "quinn", feature = "quiche"))]
760	pub(crate) certs: Vec<Arc<rustls::sign::CertifiedKey>>,
761	pub(crate) fingerprints: Vec<String>,
762}
763
764/// A live handle to the certificates a [`crate::Server`] is serving.
765///
766/// Cheap to clone, and every read reflects the latest hot reload of the files on
767/// disk, so a caller can build one at startup and hold it for the process
768/// lifetime. Obtained from [`crate::Server::certificates`].
769#[derive(Clone, Debug)]
770pub struct Certificates {
771	#[cfg(any(feature = "noq", feature = "quinn", feature = "quiche"))]
772	info: Arc<RwLock<Info>>,
773}
774
775impl Certificates {
776	#[cfg(any(feature = "noq", feature = "quinn", feature = "quiche"))]
777	pub(crate) fn new(info: Arc<RwLock<Info>>) -> Self {
778		Self { info }
779	}
780
781	/// An empty set, used when no TLS-bearing backend is configured.
782	pub(crate) fn empty() -> Self {
783		Self {
784			#[cfg(any(feature = "noq", feature = "quinn", feature = "quiche"))]
785			info: Arc::new(RwLock::new(Info::default())),
786		}
787	}
788
789	/// The SHA-256 fingerprints of the certificates being served right now, hex
790	/// encoded, one per certificate and in configuration order.
791	///
792	/// Empty when the server has no TLS-bearing backend. Re-read this per use
793	/// rather than caching it: a cert rotation on disk changes the values.
794	pub fn fingerprints(&self) -> Vec<String> {
795		#[cfg(any(feature = "noq", feature = "quinn", feature = "quiche"))]
796		{
797			// A panicking writer can't leave the cert list half-updated (it is
798			// replaced wholesale), so a poisoned lock is still safe to read.
799			let info = self.info.read().unwrap_or_else(std::sync::PoisonError::into_inner);
800			info.fingerprints.clone()
801		}
802		#[cfg(not(any(feature = "noq", feature = "quinn", feature = "quiche")))]
803		Vec::new()
804	}
805}
806
807// ── NoCertificateVerification ───────────────────────────────────────
808
809#[derive(Debug)]
810struct NoCertificateVerification(crypto::Provider);
811
812impl rustls::client::danger::ServerCertVerifier for NoCertificateVerification {
813	fn verify_server_cert(
814		&self,
815		_end_entity: &CertificateDer<'_>,
816		_intermediates: &[CertificateDer<'_>],
817		_server_name: &ServerName<'_>,
818		_ocsp: &[u8],
819		_now: UnixTime,
820	) -> std::result::Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
821		Ok(rustls::client::danger::ServerCertVerified::assertion())
822	}
823
824	fn verify_tls12_signature(
825		&self,
826		message: &[u8],
827		cert: &CertificateDer<'_>,
828		dss: &rustls::DigitallySignedStruct,
829	) -> std::result::Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
830		rustls::crypto::verify_tls12_signature(message, cert, dss, &self.0.signature_verification_algorithms)
831	}
832
833	fn verify_tls13_signature(
834		&self,
835		message: &[u8],
836		cert: &CertificateDer<'_>,
837		dss: &rustls::DigitallySignedStruct,
838	) -> std::result::Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
839		rustls::crypto::verify_tls13_signature(message, cert, dss, &self.0.signature_verification_algorithms)
840	}
841
842	fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
843		self.0.signature_verification_algorithms.supported_schemes()
844	}
845}
846
847// ── FingerprintVerifier ─────────────────────────────────────────────
848
849#[derive(Debug)]
850pub(crate) struct FingerprintVerifier {
851	provider: crypto::Provider,
852	fingerprints: Vec<Vec<u8>>,
853}
854
855impl FingerprintVerifier {
856	pub fn new(provider: crypto::Provider, fingerprints: Vec<Vec<u8>>) -> Self {
857		Self { provider, fingerprints }
858	}
859}
860
861impl rustls::client::danger::ServerCertVerifier for FingerprintVerifier {
862	fn verify_server_cert(
863		&self,
864		end_entity: &CertificateDer<'_>,
865		_intermediates: &[CertificateDer<'_>],
866		_server_name: &ServerName<'_>,
867		_ocsp: &[u8],
868		_now: UnixTime,
869	) -> std::result::Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
870		let fingerprint = crypto::sha256(&self.provider, end_entity);
871		if self.fingerprints.iter().any(|fp| fingerprint.as_ref() == fp.as_slice()) {
872			Ok(rustls::client::danger::ServerCertVerified::assertion())
873		} else {
874			Err(rustls::Error::General("fingerprint mismatch".into()))
875		}
876	}
877
878	fn verify_tls12_signature(
879		&self,
880		message: &[u8],
881		cert: &CertificateDer<'_>,
882		dss: &rustls::DigitallySignedStruct,
883	) -> std::result::Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
884		rustls::crypto::verify_tls12_signature(message, cert, dss, &self.provider.signature_verification_algorithms)
885	}
886
887	fn verify_tls13_signature(
888		&self,
889		message: &[u8],
890		cert: &CertificateDer<'_>,
891		dss: &rustls::DigitallySignedStruct,
892	) -> std::result::Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
893		rustls::crypto::verify_tls13_signature(message, cert, dss, &self.provider.signature_verification_algorithms)
894	}
895
896	fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
897		self.provider.signature_verification_algorithms.supported_schemes()
898	}
899}
900
901#[cfg(test)]
902#[cfg(all(any(feature = "quinn", feature = "noq", feature = "quiche"), feature = "aws-lc-rs"))]
903mod tests {
904	/// Disabling verification cannot be combined with trust material that it would
905	/// otherwise ignore.
906	#[test]
907	fn disable_verify_rejects_trust_material() {
908		let insecure = Client {
909			disable_verify: Some(true),
910			..Default::default()
911		};
912		assert!(matches!(insecure.verification(), Ok(Verification::Disabled)));
913
914		let with_fingerprint = Client {
915			disable_verify: Some(true),
916			fingerprint: vec!["ab".repeat(32)],
917			..Default::default()
918		};
919		assert!(matches!(
920			with_fingerprint.verification(),
921			Err(Error::DisableVerifyWithTrust)
922		));
923
924		let with_root = Client {
925			disable_verify: Some(true),
926			root: vec!["/tmp/root.pem".into()],
927			..Default::default()
928		};
929		assert!(matches!(with_root.verification(), Err(Error::DisableVerifyWithTrust)));
930
931		let with_system_roots = Client {
932			disable_verify: Some(true),
933			system_roots: Some(true),
934			..Default::default()
935		};
936		assert!(matches!(
937			with_system_roots.verification(),
938			Err(Error::DisableVerifyWithTrust)
939		));
940
941		let without_system_roots = Client {
942			disable_verify: Some(true),
943			system_roots: Some(false),
944			..Default::default()
945		};
946		assert!(matches!(
947			without_system_roots.verification(),
948			Ok(Verification::Disabled)
949		));
950	}
951
952	use super::*;
953	use rustls::client::danger::ServerCertVerifier;
954	use rustls::pki_types::ServerName;
955
956	fn self_signed() -> CertificateDer<'static> {
957		let key = rcgen::KeyPair::generate().unwrap();
958		let params = rcgen::CertificateParams::new(vec!["localhost".to_string()]).unwrap();
959		params.self_signed(&key).unwrap().into()
960	}
961
962	#[cfg(any(feature = "quinn", feature = "noq"))]
963	#[test]
964	fn peer_identity_expiry_reads_not_after() {
965		// notAfter at a whole second so the round-trip is exact.
966		let not_after = ::time::OffsetDateTime::from_unix_timestamp(2_000_000_000).unwrap();
967
968		let key = rcgen::KeyPair::generate().unwrap();
969		let mut params = rcgen::CertificateParams::new(vec!["localhost".to_string()]).unwrap();
970		params.not_after = not_after;
971		let cert: CertificateDer<'static> = params.self_signed(&key).unwrap().into();
972
973		// quinn/noq hand back the chain as a boxed Vec<CertificateDer>.
974		let identity: Box<dyn std::any::Any> = Box::new(vec![cert]);
975		let parsed = PeerIdentity::from_any(Some(identity)).expect("chain parsed");
976		let expiry = parsed.expiry().expect("expiry parsed");
977		assert_eq!(
978			expiry.duration_since(std::time::UNIX_EPOCH).unwrap().as_secs(),
979			2_000_000_000
980		);
981	}
982
983	#[cfg(any(feature = "quinn", feature = "noq"))]
984	#[test]
985	fn peer_identity_none_without_chain() {
986		assert!(PeerIdentity::from_any(None).is_none());
987		// A wrong downcast type (not a cert chain) yields None rather than panicking.
988		let bogus: Box<dyn std::any::Any> = Box::new(42u32);
989		assert!(PeerIdentity::from_any(Some(bogus)).is_none());
990	}
991
992	#[test]
993	fn fingerprint_verifier_matches_and_rejects() {
994		let provider = crypto::provider();
995		let cert = self_signed();
996		let fingerprint = crypto::sha256(&provider, cert.as_ref()).as_ref().to_vec();
997
998		let name = ServerName::try_from("localhost").unwrap();
999		let now = UnixTime::now();
1000
1001		let verifier = FingerprintVerifier::new(provider.clone(), vec![fingerprint]);
1002		assert!(verifier.verify_server_cert(&cert, &[], &name, &[], now).is_ok());
1003
1004		// A different leaf certificate must not satisfy the pin.
1005		let other = self_signed();
1006		assert!(verifier.verify_server_cert(&other, &[], &name, &[], now).is_err());
1007	}
1008
1009	#[test]
1010	fn build_installs_fingerprint_verifier() {
1011		let cert = self_signed();
1012		let fingerprint = hex::encode(crypto::sha256(&crypto::provider(), cert.as_ref()));
1013
1014		// A bogus hash still builds; verification happens at handshake time.
1015		let config = Client {
1016			fingerprint: vec![fingerprint],
1017			..Default::default()
1018		};
1019		assert!(config.build().is_ok());
1020	}
1021
1022	#[test]
1023	fn build_rejects_invalid_fingerprint_hex() {
1024		let config = Client {
1025			fingerprint: vec!["not-hex".to_string()],
1026			..Default::default()
1027		};
1028		assert!(matches!(config.build(), Err(Error::Fingerprint(_))));
1029	}
1030
1031	#[test]
1032	fn build_rejects_wrong_length_fingerprint() {
1033		// Valid hex, but only 2 bytes instead of 32.
1034		let config = Client {
1035			fingerprint: vec!["abcd".to_string()],
1036			..Default::default()
1037		};
1038		assert!(matches!(config.build(), Err(Error::FingerprintLength(2))));
1039	}
1040
1041	#[test]
1042	fn build_rejects_no_roots() {
1043		// System roots disabled with no custom root and no alternate verifier:
1044		// nothing could ever verify, so reject up front.
1045		let config = Client {
1046			system_roots: Some(false),
1047			..Default::default()
1048		};
1049		assert!(matches!(config.build(), Err(Error::NoRoots)));
1050	}
1051
1052	#[test]
1053	fn build_allows_no_roots_when_verification_overridden() {
1054		// disable_verify swaps in its own verifier, so an empty store is fine.
1055		let config = Client {
1056			system_roots: Some(false),
1057			disable_verify: Some(true),
1058			..Default::default()
1059		};
1060		assert!(config.build().is_ok());
1061
1062		// Same for fingerprint pinning.
1063		let cert = self_signed();
1064		let fingerprint = hex::encode(crypto::sha256(&crypto::provider(), cert.as_ref()));
1065		let config = Client {
1066			system_roots: Some(false),
1067			fingerprint: vec![fingerprint],
1068			..Default::default()
1069		};
1070		assert!(config.build().is_ok());
1071	}
1072
1073	#[test]
1074	fn build_rejects_fingerprint_with_roots() {
1075		let cert = self_signed();
1076		let fingerprint = hex::encode(crypto::sha256(&crypto::provider(), cert.as_ref()));
1077
1078		// Fingerprint pinning bypasses the CA chain, so combining it with roots
1079		// is rejected rather than silently ignoring one of them.
1080		let with_system = Client {
1081			fingerprint: vec![fingerprint.clone()],
1082			system_roots: Some(true),
1083			..Default::default()
1084		};
1085		assert!(matches!(with_system.build(), Err(Error::FingerprintWithRoots)));
1086
1087		// The conflict is detected before any root file is read, so the path
1088		// need not exist.
1089		let with_custom = Client {
1090			fingerprint: vec![fingerprint],
1091			root: vec![PathBuf::from("/does-not-exist.pem")],
1092			..Default::default()
1093		};
1094		assert!(matches!(with_custom.build(), Err(Error::FingerprintWithRoots)));
1095	}
1096
1097	/// Write a self-signed cert to a temp PEM file, returning the keep-alive
1098	/// handle alongside its path.
1099	fn self_signed_root() -> (tempfile::NamedTempFile, PathBuf) {
1100		use std::io::Write;
1101		let key = rcgen::KeyPair::generate().unwrap();
1102		let params = rcgen::CertificateParams::new(vec!["localhost".to_string()]).unwrap();
1103		let cert = params.self_signed(&key).unwrap();
1104		let mut file = tempfile::NamedTempFile::new().unwrap();
1105		file.write_all(cert.pem().as_bytes()).unwrap();
1106		let path = file.path().to_path_buf();
1107		(file, path)
1108	}
1109
1110	#[test]
1111	fn build_uses_platform_verifier_by_default() {
1112		// No custom roots, system trust on: resolves to the OS platform verifier
1113		// (bundled Mozilla roots on Android) and must build cleanly everywhere.
1114		assert!(Client::default().build().is_ok());
1115	}
1116
1117	#[test]
1118	fn build_with_custom_roots_only() {
1119		// A custom root with system trust left at its default disables the system
1120		// roots, verifying against the custom PEM alone.
1121		let (_keep, path) = self_signed_root();
1122		let config = Client {
1123			root: vec![path],
1124			..Default::default()
1125		};
1126		assert!(config.build().is_ok());
1127	}
1128
1129	#[test]
1130	fn build_with_custom_and_system_roots() {
1131		// Custom roots layered on top of system trust: exercises the platform
1132		// verifier's extra-roots path (or the bundled roots plus custom on Android).
1133		let (_keep, path) = self_signed_root();
1134		let config = Client {
1135			root: vec![path],
1136			system_roots: Some(true),
1137			..Default::default()
1138		};
1139		assert!(config.build().is_ok());
1140	}
1141}
1142
1143// ── ServeCerts ──────────────────────────────────────────────────────
1144
1145#[cfg(any(feature = "quinn", feature = "noq", feature = "quiche"))]
1146#[derive(Debug)]
1147pub(crate) struct ServeCerts {
1148	pub info: Arc<RwLock<Info>>,
1149	provider: crypto::Provider,
1150}
1151
1152#[cfg(any(feature = "quinn", feature = "noq", feature = "quiche"))]
1153impl ServeCerts {
1154	pub fn new(provider: crypto::Provider) -> Self {
1155		Self {
1156			info: Arc::new(RwLock::new(Info::default())),
1157			provider,
1158		}
1159	}
1160
1161	pub fn load_certs(&self, config: &Server) -> Result<()> {
1162		if config.cert.len() != config.key.len() {
1163			return Err(Error::CertKeyCountMismatch);
1164		}
1165		if config.cert.is_empty() && config.generate.is_empty() {
1166			return Err(Error::NoCertSource);
1167		}
1168
1169		let mut certs = Vec::new();
1170
1171		// Load the certificate and key files based on their index.
1172		for (cert, key) in config.cert.iter().zip(config.key.iter()) {
1173			certs.push(Arc::new(self.load(cert, key)?));
1174		}
1175
1176		// Generate a new certificate if requested.
1177		if !config.generate.is_empty() {
1178			certs.push(Arc::new(self.generate(&config.generate)?));
1179		}
1180
1181		self.set_certs(certs);
1182		Ok(())
1183	}
1184
1185	// Load a certificate and corresponding key from a file, but don't add it to the certs
1186	fn load(&self, chain_path: &Path, key_path: &Path) -> Result<rustls::sign::CertifiedKey> {
1187		let chain = read_certs(chain_path)?;
1188		if chain.is_empty() {
1189			return Err(Error::Empty);
1190		}
1191
1192		// Read the PEM private key
1193		let key = PrivateKeyDer::from_pem_file(key_path).map_err(Error::Key)?;
1194		let key = self.provider.key_provider.load_private_key(key)?;
1195
1196		let certified_key = rustls::sign::CertifiedKey::new(chain, key);
1197
1198		certified_key.keys_match().map_err(|source| Error::KeyMismatch {
1199			key: key_path.to_path_buf(),
1200			cert: chain_path.to_path_buf(),
1201			source,
1202		})?;
1203
1204		Ok(certified_key)
1205	}
1206
1207	#[cfg(any(feature = "aws-lc-rs", feature = "ring"))]
1208	fn generate(&self, hostnames: &[String]) -> Result<rustls::sign::CertifiedKey> {
1209		let key_pair = rcgen::KeyPair::generate()?;
1210
1211		let mut params = rcgen::CertificateParams::new(hostnames)?;
1212
1213		// Make the certificate valid for two weeks, starting yesterday (in case of clock drift).
1214		// WebTransport certificates MUST be valid for two weeks at most.
1215		params.not_before = ::time::OffsetDateTime::now_utc() - ::time::Duration::days(1);
1216		params.not_after = params.not_before + ::time::Duration::days(14);
1217
1218		// Generate the certificate
1219		let cert = params.self_signed(&key_pair)?;
1220
1221		// Convert the rcgen type to the rustls type.
1222		let key_der = key_pair.serialized_der().to_vec();
1223		let key_der = PrivatePkcs8KeyDer::from(key_der);
1224		let key = self.provider.key_provider.load_private_key(key_der.into())?;
1225
1226		// Create a rustls::sign::CertifiedKey
1227		Ok(rustls::sign::CertifiedKey::new(vec![cert.into()], key))
1228	}
1229
1230	#[cfg(not(any(feature = "aws-lc-rs", feature = "ring")))]
1231	fn generate(&self, _hostnames: &[String]) -> Result<rustls::sign::CertifiedKey> {
1232		Err(Error::NoCryptoProvider)
1233	}
1234
1235	// Replace the certificates
1236	pub fn set_certs(&self, certs: Vec<Arc<rustls::sign::CertifiedKey>>) {
1237		let fingerprints = certs
1238			.iter()
1239			.map(|ck| {
1240				let fingerprint = crate::crypto::sha256(&self.provider, ck.cert[0].as_ref());
1241				hex::encode(fingerprint)
1242			})
1243			.collect();
1244
1245		let mut info = self.info.write().expect("info write lock poisoned");
1246		info.certs = certs;
1247		info.fingerprints = fingerprints;
1248	}
1249
1250	// Return the best certificate for the given ClientHello.
1251	fn best_certificate(
1252		&self,
1253		client_hello: &rustls::server::ClientHello<'_>,
1254	) -> Option<Arc<rustls::sign::CertifiedKey>> {
1255		let server_name = client_hello.server_name()?;
1256		let dns_name = rustls::pki_types::ServerName::try_from(server_name).ok()?;
1257
1258		for ck in self.info.read().expect("info read lock poisoned").certs.iter() {
1259			let leaf: webpki::EndEntityCert = ck
1260				.end_entity_cert()
1261				.expect("missing certificate")
1262				.try_into()
1263				.expect("failed to parse certificate");
1264
1265			if leaf.verify_is_valid_for_subject_name(&dns_name).is_ok() {
1266				return Some(ck.clone());
1267			}
1268		}
1269
1270		None
1271	}
1272}
1273
1274#[cfg(any(feature = "quinn", feature = "noq", feature = "quiche"))]
1275impl rustls::server::ResolvesServerCert for ServeCerts {
1276	fn resolve(&self, client_hello: rustls::server::ClientHello<'_>) -> Option<Arc<rustls::sign::CertifiedKey>> {
1277		if let Some(cert) = self.best_certificate(&client_hello) {
1278			return Some(cert);
1279		}
1280
1281		// If this happens, it means the client was trying to connect to an unknown hostname.
1282		// We do our best and return the first certificate.
1283		tracing::warn!(server_name = ?client_hello.server_name(), "no SNI certificate found");
1284
1285		self.info
1286			.read()
1287			.expect("info read lock poisoned")
1288			.certs
1289			.first()
1290			.cloned()
1291	}
1292}
1293
1294// ── reload_certs ────────────────────────────────────────────────────
1295
1296/// Watch the on-disk cert/key files and reload them whenever they change.
1297///
1298/// Reacting to the filesystem means cert-manager, Kubernetes secret mounts, and
1299/// `mv`-into-place rotate certs with no external signal. Returns immediately when
1300/// only generated certs are configured: there's nothing on disk to watch.
1301#[cfg(any(feature = "quinn", feature = "noq"))]
1302pub(crate) async fn reload_certs(certs: Arc<ServeCerts>, tls_config: Server) {
1303	let paths: Vec<PathBuf> = tls_config.cert.iter().chain(tls_config.key.iter()).cloned().collect();
1304	if paths.is_empty() {
1305		return;
1306	}
1307
1308	let mut watcher = match crate::watch::FileWatcher::new(&paths) {
1309		Ok(watcher) => watcher,
1310		Err(err) => {
1311			tracing::error!(%err, "failed to watch certificate files; hot reload disabled");
1312			return;
1313		}
1314	};
1315
1316	loop {
1317		watcher.changed().await;
1318		tracing::info!("reloading server certificates");
1319
1320		if let Err(err) = certs.load_certs(&tls_config) {
1321			tracing::warn!(%err, "failed to reload server certificates");
1322		}
1323	}
1324}