Skip to main content

hyprstream_core/server/
tls.rs

1//! Shared TLS materials and HTTPS serving helpers for HTTP services.
2//!
3//! Provides:
4//! - `TlsMaterials`: DER-encoded cert + key pair
5//! - `get_or_init_tls_materials()`: OnceLock-based lazy generation (self-signed or file-loaded)
6//! - `resolve_rustls_config()`: Resolves per-service or shared TLS config
7//! - `serve_app()`: Serves an Axum router over HTTPS or HTTP with graceful shutdown
8//!
9//! TLS modes (set via `[tls] mode`):
10//!  - `self-signed` (default) — rcgen-generated, dev/air-gapped only
11//!  - `acme` — automatic via RFC 8555 (Let's Encrypt or self-hosted step-ca / Pebble)
12//!  - `files` — operator-supplied PEM paths
13
14use crate::account::AccountZoneConfig;
15use crate::config::{TlsConfig, TlsMode};
16use hyprstream_rpc::error::RpcError;
17use std::net::SocketAddr;
18use std::path::PathBuf;
19use std::sync::{Arc, OnceLock};
20use std::time::Duration;
21use tokio::sync::Notify;
22use tracing::info;
23use zeroize::Zeroizing;
24
25/// DER-encoded TLS certificate and private key.
26///
27/// Key material is wrapped in `Zeroizing` to ensure it is zeroed on drop,
28/// preventing private key bytes from lingering in freed heap memory.
29#[derive(Clone)]
30pub struct TlsMaterials {
31    pub cert_der: Vec<u8>,
32    pub key_der: Zeroizing<Vec<u8>>,
33}
34
35/// Shared self-signed TLS materials (generated once, reused across all services).
36///
37/// Stored behind an `Arc` so callers share the same allocation rather than
38/// cloning key bytes on every call.
39static SHARED_TLS: OnceLock<Arc<TlsMaterials>> = OnceLock::new();
40
41/// Get or initialize the shared TLS materials.
42///
43/// - If already initialized, returns a clone of the `Arc` (no key bytes copied).
44/// - If both `cert_path`/`key_path` are set in config, loads from PEM files.
45/// - Otherwise, loads persisted materials from `secrets.path` (generating on first run).
46///   The key is stable across restarts; certs are renewed when approaching expiry.
47pub fn get_or_init_tls_materials(config: &TlsConfig) -> anyhow::Result<Arc<TlsMaterials>> {
48    if let Some(existing) = SHARED_TLS.get() {
49        return Ok(Arc::clone(existing));
50    }
51
52    let materials = if config.use_self_signed() {
53        let secrets_dir = crate::config::HyprConfig::resolve_secrets_dir()?;
54        let server_name = &config.server_name;
55        info!(
56            "Loading/generating TLS materials (365-day validity) from '{}'",
57            secrets_dir.display()
58        );
59        crate::auth::identity_store::load_or_generate_tls_materials(&secrets_dir, server_name, 365)?
60    } else {
61        // Both paths guaranteed Some when use_self_signed() returns false
62        let cert_path = config
63            .cert_path
64            .as_ref()
65            .ok_or_else(|| anyhow::anyhow!("tls.cert_path must be set when tls.key_path is set"))?;
66        let key_path = config
67            .key_path
68            .as_ref()
69            .ok_or_else(|| anyhow::anyhow!("tls.key_path must be set when tls.cert_path is set"))?;
70
71        let cert_pem = std::fs::read(cert_path).map_err(|e| {
72            anyhow::anyhow!(
73                "failed to read tls.cert_path '{}': {}",
74                cert_path.display(),
75                e
76            )
77        })?;
78        let key_pem = std::fs::read(key_path).map_err(|e| {
79            anyhow::anyhow!(
80                "failed to read tls.key_path '{}': {}",
81                key_path.display(),
82                e
83            )
84        })?;
85
86        let cert_der = rustls_pemfile::certs(&mut &cert_pem[..])
87            .next()
88            .ok_or_else(|| anyhow::anyhow!("no certificate found in {}", cert_path.display()))?
89            .map_err(|e| anyhow::anyhow!("invalid certificate PEM: {}", e))?
90            .to_vec();
91
92        let key_der = Zeroizing::new(
93            rustls_pemfile::private_key(&mut &key_pem[..])
94                .map_err(|e| anyhow::anyhow!("invalid key PEM: {}", e))?
95                .ok_or_else(|| anyhow::anyhow!("no private key found in {}", key_path.display()))?
96                .secret_der()
97                .to_vec(),
98        );
99
100        TlsMaterials { cert_der, key_der }
101    };
102
103    // If another thread beat us, use theirs (Arc::clone, no key bytes copied)
104    Ok(Arc::clone(SHARED_TLS.get_or_init(|| Arc::new(materials))))
105}
106
107/// Resolve the rustls configuration for a service.
108///
109pub struct ResolvedTlsConfig {
110    pub rustls: axum_server::tls_rustls::RustlsConfig,
111    /// Present only when the rustls-acme listener must inspect and handle
112    /// TLS-ALPN-01 connections before the ordinary Axum TLS handshake.
113    pub acme_acceptor: Option<rustls_acme::axum::AxumAcceptor>,
114}
115
116impl ResolvedTlsConfig {
117    pub(crate) fn ordinary(rustls: axum_server::tls_rustls::RustlsConfig) -> Self {
118        Self {
119            rustls,
120            acme_acceptor: None,
121        }
122    }
123}
124
125/// Resolve HTTP TLS configuration and any protocol-specific acceptor.
126///
127/// Resolution order:
128/// 1. `tls_config.enabled == false` → `None` (plain HTTP)
129/// 2. Both `service_cert` + `service_key` set → per-service PEM files (overrides mode)
130/// 3. `mode = "acme"` → obtain/renew via ACME and use its challenge-aware acceptor
131/// 4. Otherwise → shared self-signed / `files` mode materials
132pub async fn resolve_rustls_config(
133    tls_config: &TlsConfig,
134    account_cfg: &AccountZoneConfig,
135    service_cert: Option<&PathBuf>,
136    service_key: Option<&PathBuf>,
137) -> anyhow::Result<Option<ResolvedTlsConfig>> {
138    if !tls_config.enabled {
139        return Ok(None);
140    }
141
142    // Ensure the PQ-hybrid (aws-lc-rs, X25519MLKEM768) crypto provider is
143    // installed for rustls (#557 / S6). Services that don't use QUIC (which
144    // installs it via quinn) need this before any RustlsConfig can be created.
145    // Idempotent, but fail closed if another provider was installed first.
146    hyprstream_rpc::transport::install_pq_crypto_provider()?;
147
148    // Per-service override: both cert and key must be set
149    if let (Some(cert), Some(key)) = (service_cert, service_key) {
150        let rustls_config = axum_server::tls_rustls::RustlsConfig::from_pem_file(cert, key)
151            .await
152            .map_err(|e| {
153                anyhow::anyhow!(
154                    "failed to load per-service TLS from {:?}/{:?}: {}",
155                    cert,
156                    key,
157                    e
158                )
159            })?;
160        return Ok(Some(ResolvedTlsConfig::ordinary(rustls_config)));
161    }
162
163    // Warn if only one of cert/key is set at the per-service level
164    if service_cert.is_some() != service_key.is_some() {
165        tracing::warn!(
166            "Per-service TLS requires both tls_cert and tls_key — falling back to shared TLS config"
167        );
168    }
169
170    match tls_config.effective_mode() {
171        TlsMode::Acme => {
172            let config = init_acme_rustls_config(tls_config).await?;
173            Ok(Some(config))
174        }
175        TlsMode::AcmeDns01 => {
176            // Account-zone DNS-01 wildcard issuance (epic #1158, A3 / #1162).
177            // The concrete ACME-DNS-01 client and DNS backend are product-layer
178            // seams (`hyprstream::account::{WildcardCertIssuer, DnsProvider}`);
179            // here we only sanity-check provisioning. When the deployment
180            // configured no account zone or DNS-01 credential, this mode
181            // **degrades sanely** to the existing shared self-signed materials
182            // — no panic, no synthesized zone name (one-way door #1).
183            match crate::account::tls::require_provisioned(account_cfg) {
184                Ok(zone) => {
185                    // NOTE (#1182 review): this branch validates that an account
186                    // zone + zone-scoped DNS-01 credential are *configured*. It
187                    // does NOT yet invoke `WildcardCertIssuer` or bind a
188                    // `CertHandle` to this listener — those are product-layer /
189                    // B3 (#1165) seams. Until that integration lands, the
190                    // account zone's wildcard cert is NOT what this service
191                    // serves; we fall through to shared self-signed materials so
192                    // the deployment keeps booting. Do not read the log below as
193                    // "the wildcard cert is live"; it means "config is
194                    // well-formed and provisioning is detectable".
195                    tracing::info!(
196                        apex = %zone.apex(),
197                        wildcard = %zone.wildcard_domain(),
198                        "TLS mode acme-dns01: account-zone config is provisioned (zone + \
199                         zone-scoped DNS-01 credential present). Wildcard issuance + rustls \
200                         binding are product-layer/B3 (#1165) seams not yet wired here — \
201                         serving shared self-signed TLS until then.",
202                    );
203                    let materials = get_or_init_tls_materials(tls_config)?;
204                    let rustls_config = axum_server::tls_rustls::RustlsConfig::from_der(
205                        vec![materials.cert_der.clone()],
206                        (*materials.key_der).clone(),
207                    )
208                    .await
209                    .map_err(|e| anyhow::anyhow!("failed to build RustlsConfig from DER: {}", e))?;
210                    Ok(Some(ResolvedTlsConfig::ordinary(rustls_config)))
211                }
212                Err(e) => {
213                    // Sane-degrade: clear error in the log, fall back to shared
214                    // self-signed materials. We do NOT bail: account minting is
215                    // disabled, but the rest of the deployment (OAI/OAuth/MCP
216                    // faces) must still boot.
217                    tracing::warn!(
218                        "TLS mode acme-dns01 requested but account zone is not provisioned \
219                         — did:web minting disabled, serving with shared self-signed TLS: {e}"
220                    );
221                    let materials = get_or_init_tls_materials(tls_config)?;
222                    let rustls_config = axum_server::tls_rustls::RustlsConfig::from_der(
223                        vec![materials.cert_der.clone()],
224                        (*materials.key_der).clone(),
225                    )
226                    .await
227                    .map_err(|e| anyhow::anyhow!("failed to build RustlsConfig from DER: {}", e))?;
228                    Ok(Some(ResolvedTlsConfig::ordinary(rustls_config)))
229                }
230            }
231        }
232        _ => {
233            // Shared materials (self-signed or Files mode)
234            let materials = get_or_init_tls_materials(tls_config)?;
235            let rustls_config = axum_server::tls_rustls::RustlsConfig::from_der(
236                vec![materials.cert_der.clone()],
237                (*materials.key_der).clone(),
238            )
239            .await
240            .map_err(|e| anyhow::anyhow!("failed to build RustlsConfig from DER: {}", e))?;
241            Ok(Some(ResolvedTlsConfig::ordinary(rustls_config)))
242        }
243    }
244}
245
246/// Initialize an ACME-managed TLS config and spawn the renewal event-loop task.
247///
248/// Uses `rustls-acme` which handles ACME-01 TLS-ALPN challenges internally and
249/// updates the `Arc<rustls::ServerConfig>` in-place on each renewal — no explicit
250/// hot-swap needed.
251///
252/// For self-hosted ACME, point `acme_directory` at a step-ca or Pebble URL.
253/// See `docs/tls.md` for operator guidance.
254async fn init_acme_rustls_config(tls_config: &TlsConfig) -> anyhow::Result<ResolvedTlsConfig> {
255    use rustls_acme::AcmeConfig;
256
257    let domain = tls_config
258        .acme_domain
259        .as_deref()
260        .ok_or_else(|| anyhow::anyhow!("[tls] mode = \"acme\" requires acme_domain to be set"))?;
261    let contact = tls_config.acme_contact.as_deref().unwrap_or("");
262    let cache_dir = tls_config.acme_cache_dir.clone().unwrap_or_else(|| {
263        dirs::cache_dir()
264            .unwrap_or_else(|| std::path::PathBuf::from("/var/cache"))
265            .join("hyprstream")
266            .join("acme")
267    });
268
269    std::fs::create_dir_all(&cache_dir)
270        .map_err(|e| anyhow::anyhow!("cannot create ACME cache dir {:?}: {e}", cache_dir))?;
271
272    let mut acme = AcmeConfig::new([domain])
273        .directory_lets_encrypt(true)
274        .contact_push(contact)
275        .cache_option(Some(rustls_acme::caches::DirCache::new(cache_dir)));
276
277    if let Some(dir_url) = tls_config.acme_directory.as_deref() {
278        acme = acme.directory(dir_url);
279    }
280
281    info!(
282        "ACME TLS: obtaining certificate for domain '{}' from {}",
283        domain,
284        tls_config
285            .acme_directory
286            .as_deref()
287            .unwrap_or("Let's Encrypt")
288    );
289
290    let mut state = acme.state();
291    let rustls_server_config = acme_https_config(state.resolver());
292    let acme_acceptor = state.axum_acceptor(Arc::clone(&rustls_server_config));
293
294    // Spawn the ACME event loop — handles challenge responses and renewals.
295    tokio::spawn(async move {
296        use tokio_stream::StreamExt;
297        loop {
298            match state.next().await {
299                Some(Ok(event)) => {
300                    info!("ACME event: {event:?}");
301                }
302                Some(Err(e)) => {
303                    tracing::warn!("ACME error (will retry): {e}");
304                }
305                None => break,
306            }
307        }
308    });
309
310    // Keep the resolver shared between ordinary TLS handshakes and the
311    // AxumAcceptor's dedicated TLS-ALPN-01 handshake path.
312    let rustls_config = axum_server::tls_rustls::RustlsConfig::from_config(rustls_server_config);
313    Ok(ResolvedTlsConfig {
314        rustls: rustls_config,
315        acme_acceptor: Some(acme_acceptor),
316    })
317}
318
319/// A bound HTTP(S) listener retained between the bind and serve phases.
320///
321/// Bind early (before readiness signals); serve later, consuming the handle.
322/// No second bind occurs: the TLS arm keeps the already-bound std listener
323/// and hands it to the pinned `axum-server` prebound constructor.
324pub enum BoundHttpListener {
325    /// Prebound nonblocking listener adopted by tokio.
326    Http(tokio::net::TcpListener),
327    /// Prebound std listener + TLS config, served either via
328    /// `axum_server::from_tcp_rustls` or the ACME-aware acceptor.
329    Https(std::net::TcpListener, ResolvedTlsConfig),
330}
331
332/// Bind phase: a real `std::net::TcpListener::bind` plus `set_nonblocking`.
333///
334/// Call this BEFORE any readiness signal so a bind failure (e.g. an occupied
335/// port) surfaces before the launcher is told the service is ready.
336pub fn bind_listener(
337    addr: SocketAddr,
338    rustls_config: Option<ResolvedTlsConfig>,
339    service_name: &str,
340) -> Result<BoundHttpListener, RpcError> {
341    let std_listener = std::net::TcpListener::bind(addr)
342        .map_err(|e| RpcError::SpawnFailed(format!("{service_name} HTTP(S) bind failed: {e}")))?;
343    std_listener.set_nonblocking(true).map_err(|e| {
344        RpcError::SpawnFailed(format!(
345            "{service_name} HTTP(S) set_nonblocking failed: {e}"
346        ))
347    })?;
348    match rustls_config {
349        Some(tls) => Ok(BoundHttpListener::Https(std_listener, tls)),
350        None => Ok(BoundHttpListener::Http(
351            tokio::net::TcpListener::from_std(std_listener).map_err(|e| {
352                RpcError::SpawnFailed(format!("{service_name} HTTP listener adoption failed: {e}"))
353            })?,
354        )),
355    }
356}
357
358/// Serve phase: consume a [`BoundHttpListener`] with graceful shutdown.
359///
360/// TLS uses the pinned `axum-server` prebound constructor on the already-bound
361/// std listener; ACME mode installs rustls-acme's challenge-aware acceptor.
362pub async fn serve_bound(
363    bound: BoundHttpListener,
364    app: axum::Router,
365    shutdown: Arc<Notify>,
366    service_name: &str,
367) -> Result<(), RpcError> {
368    match bound {
369        BoundHttpListener::Https(std_listener, tls) => {
370            let handle = axum_server::Handle::new();
371            let shutdown_handle = handle.clone();
372            let name = service_name.to_owned();
373
374            // Spawn shutdown listener
375            tokio::spawn(async move {
376                shutdown.notified().await;
377                info!("{name} received shutdown signal");
378                shutdown_handle.graceful_shutdown(Some(Duration::from_secs(30)));
379            });
380
381            if let Some(acme_acceptor) = tls.acme_acceptor {
382                axum_server::from_tcp(std_listener)
383                    .acceptor(acme_acceptor)
384                    .handle(handle)
385                    .serve(app.into_make_service())
386                    .await
387                    .map_err(|e| {
388                        RpcError::SpawnFailed(format!("{service_name} HTTPS server error: {e}"))
389                    })?;
390            } else {
391                axum_server::from_tcp_rustls(std_listener, tls.rustls)
392                    .handle(handle)
393                    .serve(app.into_make_service())
394                    .await
395                    .map_err(|e| {
396                        RpcError::SpawnFailed(format!("{service_name} HTTPS server error: {e}"))
397                    })?;
398            }
399        }
400        BoundHttpListener::Http(listener) => {
401            let shutdown_clone = shutdown.clone();
402            let name = service_name.to_owned();
403            axum::serve(listener, app)
404                .with_graceful_shutdown(async move {
405                    shutdown_clone.notified().await;
406                    info!("{name} received shutdown signal");
407                })
408                .await
409                .map_err(|e| {
410                    RpcError::SpawnFailed(format!("{service_name} HTTP server error: {e}"))
411                })?;
412        }
413    }
414
415    info!("{service_name} stopped");
416    Ok(())
417}
418
419/// Serve an Axum router over HTTPS (if rustls_config is Some) or plain HTTP.
420///
421/// Uses `axum_server` for HTTPS with `Handle`-based graceful shutdown,
422/// or standard `axum::serve` for HTTP. Bind-then-serve wrapper over
423/// [`bind_listener`] + [`serve_bound`]; existing callers are unchanged.
424pub async fn serve_app(
425    addr: SocketAddr,
426    app: axum::Router,
427    rustls_config: Option<ResolvedTlsConfig>,
428    shutdown: Arc<Notify>,
429    service_name: &str,
430) -> Result<(), RpcError> {
431    let scheme = if rustls_config.is_some() {
432        "https"
433    } else {
434        "http"
435    };
436    info!("{service_name} listening on {scheme}://{addr}");
437
438    let bound = bind_listener(addr, rustls_config, service_name)?;
439    serve_bound(bound, app, shutdown, service_name).await
440}
441
442/// Configure normal HTTP protocols on the certificate resolver. The
443/// rustls-acme AxumAcceptor handles TLS-ALPN-01 ClientHello routing before
444/// handing ordinary connections to this server config.
445fn acme_https_config(
446    resolver: Arc<dyn rustls::server::ResolvesServerCert>,
447) -> Arc<rustls::ServerConfig> {
448    let mut config = rustls::ServerConfig::builder()
449        .with_no_client_auth()
450        .with_cert_resolver(resolver);
451    config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
452    Arc::new(config)
453}
454
455#[cfg(test)]
456mod acme_tests {
457    use super::*;
458    use rustls::pki_types::{PrivateKeyDer, PrivatePkcs8KeyDer, ServerName};
459    use std::io::Cursor;
460    use std::sync::atomic::{AtomicBool, Ordering};
461
462    #[derive(Debug)]
463    struct FixtureResolver {
464        key: Arc<rustls::sign::CertifiedKey>,
465        challenge_seen: AtomicBool,
466    }
467
468    impl rustls::server::ResolvesServerCert for FixtureResolver {
469        fn resolve(
470            &self,
471            hello: rustls::server::ClientHello<'_>,
472        ) -> Option<Arc<rustls::sign::CertifiedKey>> {
473            self.challenge_seen
474                .store(rustls_acme::is_tls_alpn_challenge(&hello), Ordering::SeqCst);
475            Some(self.key.clone())
476        }
477    }
478
479    /// Exercise the ordinary HTTP side of the production config with real
480    /// ClientHello negotiation. TLS-ALPN challenge handshakes are routed by
481    /// rustls-acme's AxumAcceptor before this config is used.
482    #[test]
483    fn acme_https_negotiates_http_protocols() -> anyhow::Result<()> {
484        hyprstream_rpc::transport::install_pq_crypto_provider()?;
485        let generated = rcgen::generate_simple_self_signed(vec!["localhost".to_owned()])?;
486        let cert = generated.cert.der().clone();
487        let private =
488            PrivateKeyDer::Pkcs8(PrivatePkcs8KeyDer::from(generated.key_pair.serialize_der()));
489        let provider = rustls::crypto::CryptoProvider::get_default()
490            .ok_or_else(|| anyhow::anyhow!("crypto provider missing"))?;
491        let key = Arc::new(rustls::sign::CertifiedKey::from_der(
492            vec![cert.clone()],
493            private,
494            provider,
495        )?);
496        for protocol in [b"h2".as_slice(), b"http/1.1"] {
497            let resolver = Arc::new(FixtureResolver {
498                key: key.clone(),
499                challenge_seen: AtomicBool::new(false),
500            });
501            let mut roots = rustls::RootCertStore::empty();
502            roots.add(cert.clone())?;
503            let mut client_config = rustls::ClientConfig::builder()
504                .with_root_certificates(roots)
505                .with_no_client_auth();
506            client_config.alpn_protocols = vec![protocol.to_vec()];
507            let mut client = rustls::ClientConnection::new(
508                Arc::new(client_config),
509                ServerName::try_from("localhost")?,
510            )?;
511            let mut server = rustls::ServerConnection::new(acme_https_config(resolver.clone()))?;
512            for _ in 0..16 {
513                let mut bytes = Vec::new();
514                client.write_tls(&mut bytes)?;
515                server.read_tls(&mut Cursor::new(bytes))?;
516                server.process_new_packets()?;
517                let mut bytes = Vec::new();
518                server.write_tls(&mut bytes)?;
519                client.read_tls(&mut Cursor::new(bytes))?;
520                client.process_new_packets()?;
521                if !client.is_handshaking() && !server.is_handshaking() {
522                    break;
523                }
524            }
525            assert!(!client.is_handshaking() && !server.is_handshaking());
526            assert_eq!(client.alpn_protocol(), Some(protocol));
527            assert!(!resolver.challenge_seen.load(Ordering::SeqCst));
528        }
529        Ok(())
530    }
531}
532
533#[cfg(test)]
534mod bound_serve_tests {
535    #![allow(clippy::expect_used, clippy::unwrap_used)]
536
537    use super::*;
538    use axum::routing::get;
539    use rustls::pki_types::ServerName;
540
541    /// Positive HTTPS regression through the production bind/serve seam:
542    /// `bind_listener` retains the real socket, `serve_bound` hands it to the
543    /// pinned `axum_server::from_tcp_rustls` constructor, a client that
544    /// TRUSTS the served certificate and verifies the hostname normally (no
545    /// verification bypass) completes a real request, and shutdown is
546    /// bounded. Same helper OAuth and OAI call before their readiness
547    /// signals.
548    #[tokio::test]
549    async fn bound_https_serves_verified_client_and_shuts_down_bounded() -> anyhow::Result<()> {
550        hyprstream_rpc::transport::install_pq_crypto_provider()?;
551        let generated = rcgen::generate_simple_self_signed(vec!["localhost".to_owned()])?;
552        let rustls_config = axum_server::tls_rustls::RustlsConfig::from_pem(
553            generated.cert.pem().into_bytes(),
554            generated.key_pair.serialize_pem().into_bytes(),
555        )
556        .await?;
557
558        // Reserve, then bind through the production seam (no second bind).
559        let probe = std::net::TcpListener::bind("127.0.0.1:0")?;
560        let addr = probe.local_addr()?;
561        drop(probe);
562        let bound = bind_listener(
563            addr,
564            Some(ResolvedTlsConfig::ordinary(rustls_config)),
565            "BoundServeTest",
566        )?;
567
568        let app =
569            axum::Router::new().route("/bound-serve-probe", get(|| async { "bound-serve-ok" }));
570        let shutdown = Arc::new(Notify::new());
571        let shutdown_signal = Arc::clone(&shutdown);
572        let server = tokio::spawn(serve_bound(
573            bound,
574            app,
575            Arc::clone(&shutdown),
576            "BoundServeTest",
577        ));
578
579        // Client trusts the served certificate and verifies "localhost"
580        // normally; no dangerous verification-disabling configuration.
581        let mut roots = rustls::RootCertStore::empty();
582        roots.add(generated.cert.der().clone())?;
583        let client_config = rustls::ClientConfig::builder()
584            .with_root_certificates(roots)
585            .with_no_client_auth();
586        // The client runs on a blocking thread over a real socket. `StreamOwned`
587        // implements blocking Read/Write that drives rustls IO internally (the
588        // raw ClientConnection reader only drains buffered plaintext); hostname
589        // and certificate verification stay fully enabled.
590        let client = std::thread::spawn(move || -> anyhow::Result<String> {
591            use std::io::{Read, Write};
592            let tcp = std::net::TcpStream::connect(addr)?;
593            tcp.set_read_timeout(Some(Duration::from_secs(10)))?;
594            let tls = rustls::ClientConnection::new(
595                Arc::new(client_config),
596                ServerName::try_from("localhost")?,
597            )?;
598            let mut stream = rustls::StreamOwned::new(tls, tcp);
599            stream.write_all(
600                b"GET /bound-serve-probe HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n",
601            )?;
602            let mut response = Vec::new();
603            stream.read_to_end(&mut response)?;
604            Ok(String::from_utf8_lossy(&response).into_owned())
605        });
606
607        let response = tokio::task::spawn_blocking(move || client.join())
608            .await
609            .expect("client joiner must not fail")
610            .expect("client thread must not panic")?;
611        assert!(
612            response.starts_with("HTTP/1.1 200"),
613            "verified-TLS client must receive HTTP 200: got {response:?}"
614        );
615        assert!(
616            response.contains("bound-serve-ok"),
617            "exact probe body must be served through the TLS path: got {response:?}"
618        );
619
620        // Bounded shutdown: the retained handle's server ends cleanly.
621        shutdown_signal.notify_one();
622        tokio::time::timeout(Duration::from_secs(30), server)
623            .await
624            .expect("serve_bound must terminate within the shutdown budget")?
625            .expect("graceful shutdown must complete Ok");
626        Ok(())
627    }
628
629    #[tokio::test]
630    async fn bound_https_acme_acceptor_serves_ordinary_tls() -> anyhow::Result<()> {
631        hyprstream_rpc::transport::install_pq_crypto_provider()?;
632        let generated = rcgen::generate_simple_self_signed(vec!["localhost".to_owned()])?;
633        let cert = generated.cert.der().clone();
634        let key = rustls::pki_types::PrivateKeyDer::Pkcs8(
635            rustls::pki_types::PrivatePkcs8KeyDer::from(generated.key_pair.serialize_der()),
636        );
637        let mut server_config = rustls::ServerConfig::builder()
638            .with_no_client_auth()
639            .with_single_cert(vec![cert.clone()], key)?;
640        server_config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
641        let server_config = Arc::new(server_config);
642
643        // No ACME event loop is needed for this regression: the supplied
644        // certificate serves ordinary HTTPS while the acceptor owns the
645        // separate TLS-ALPN challenge handshake path.
646        let acme_state = rustls_acme::AcmeConfig::new(["localhost"]).state();
647        let acme_acceptor = acme_state.axum_acceptor(Arc::clone(&server_config));
648        let tls = ResolvedTlsConfig {
649            rustls: axum_server::tls_rustls::RustlsConfig::from_config(server_config),
650            acme_acceptor: Some(acme_acceptor),
651        };
652
653        let probe = std::net::TcpListener::bind("127.0.0.1:0")?;
654        let addr = probe.local_addr()?;
655        drop(probe);
656        let bound = bind_listener(addr, Some(tls), "BoundAcmeServeTest")?;
657        let app = axum::Router::new().route("/bound-acme-probe", get(|| async { "bound-acme-ok" }));
658        let shutdown = Arc::new(Notify::new());
659        let shutdown_signal = Arc::clone(&shutdown);
660        let server = tokio::spawn(serve_bound(
661            bound,
662            app,
663            Arc::clone(&shutdown),
664            "BoundAcmeServeTest",
665        ));
666
667        let mut roots = rustls::RootCertStore::empty();
668        roots.add(cert)?;
669        let client_config = rustls::ClientConfig::builder()
670            .with_root_certificates(roots)
671            .with_no_client_auth();
672        let client = std::thread::spawn(move || -> anyhow::Result<String> {
673            use std::io::{Read, Write};
674            let tcp = std::net::TcpStream::connect(addr)?;
675            tcp.set_read_timeout(Some(Duration::from_secs(10)))?;
676            let tls = rustls::ClientConnection::new(
677                Arc::new(client_config),
678                ServerName::try_from("localhost")?,
679            )?;
680            let mut stream = rustls::StreamOwned::new(tls, tcp);
681            stream.write_all(
682                b"GET /bound-acme-probe HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n",
683            )?;
684            let mut response = Vec::new();
685            stream.read_to_end(&mut response)?;
686            Ok(String::from_utf8_lossy(&response).into_owned())
687        });
688        let response = tokio::task::spawn_blocking(move || client.join())
689            .await
690            .expect("client joiner must not fail")
691            .expect("client thread must not panic")?;
692        assert!(response.starts_with("HTTP/1.1 200"), "{response:?}");
693        assert!(response.contains("bound-acme-ok"), "{response:?}");
694
695        shutdown_signal.notify_one();
696        tokio::time::timeout(Duration::from_secs(30), server)
697            .await
698            .expect("serve_bound must terminate within the shutdown budget")?
699            .expect("graceful shutdown must complete Ok");
700        Ok(())
701    }
702}