From 6f73f1f45e84ec24a444ec6ca6ef69ae36743f1b Mon Sep 17 00:00:00 2001 From: Frando Date: Fri, 12 Jun 2026 12:36:09 +0200 Subject: [PATCH] feat(iroh-relay): trust a custom CA for the ACME server TLS Add an `IROH_RELAY_ACME_CA` environment variable to the relay binary and a `tls_config` option on `AcmeConfig` that feeds a `CaTlsConfig` into the rustls-acme client. This lets the relay reach an ACME directory whose certificate is not signed by a publicly trusted CA, such as a local pebble server used for testing. The default stays the embedded webpki roots, so existing deployments are unaffected. --- iroh-relay/src/main.rs | 14 +++++++++++++- iroh-relay/src/server.rs | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/iroh-relay/src/main.rs b/iroh-relay/src/main.rs index 828e040c61..73396a731b 100644 --- a/iroh-relay/src/main.rs +++ b/iroh-relay/src/main.rs @@ -21,6 +21,7 @@ use iroh_relay::{ self as relay, Access, AccessControl, AcmeConfig, ClientRateLimit, ClientRequest, DEFAULT_CERT_RELOAD_INTERVAL, QuicConfig, reloading_resolver, }, + tls::CaTlsConfig, }; use n0_error::{AnyError, Result, StdResultExt, bail_any}; use serde::{Deserialize, Serialize}; @@ -645,10 +646,21 @@ async fn load_cert_config(tls: &TlsConfig) -> Result { } else { AcmeConfig::letsencrypt(tls.prod_tls) }; - let acme_config = acme_config + let mut acme_config = acme_config .domains(domains) .contact(vec![format!("mailto:{contact}")]) .cache_path(tls.cert_dir()); + // Trust an additional CA for the ACME server's TLS certificate. Useful for testing + // against a local ACME server such as pebble, whose certificate is not signed by a + // publicly trusted CA. + if let Ok(ca_path) = std::env::var("IROH_RELAY_ACME_CA") { + let extra_roots = CertificateDer::pem_file_iter(&ca_path) + .std_context("failed to read IROH_RELAY_ACME_CA")? + .collect::, _>>() + .std_context("failed to parse IROH_RELAY_ACME_CA")?; + acme_config = + acme_config.tls_config(CaTlsConfig::default().with_extra_roots(extra_roots)); + } relay::CertConfig::LetsEncrypt { acme_config, server_config_builder: server_config, diff --git a/iroh-relay/src/server.rs b/iroh-relay/src/server.rs index 07fb48cacc..8e607271a5 100644 --- a/iroh-relay/src/server.rs +++ b/iroh-relay/src/server.rs @@ -58,6 +58,7 @@ use crate::{ defaults::DEFAULT_KEY_CACHE_CAPACITY, http::{AUTH_TOKEN_URL_QUERY_PARAM, ProtocolVersion, RELAY_PROBE_PATH}, quic::server::{QuicServer, QuicSpawnError, ServerHandle as QuicServerHandle}, + tls::CaTlsConfig, }; pub mod client; @@ -549,6 +550,7 @@ pub struct AcmeConfig { pub(crate) domains: Vec, pub(crate) contact: Vec, pub(crate) cache_path: Option, + pub(crate) tls_config: CaTlsConfig, } impl AcmeConfig { @@ -559,6 +561,7 @@ impl AcmeConfig { domains: Vec::new(), contact: Vec::new(), cache_path: None, + tls_config: CaTlsConfig::default(), } } @@ -593,6 +596,16 @@ impl AcmeConfig { self.cache_path = Some(path); self } + + /// Sets the [`CaTlsConfig`] used to verify the ACME server's TLS certificate. + /// + /// Defaults to [`CaTlsConfig::embedded`]. Set a config with extra roots when targeting + /// an ACME server whose certificate is not signed by a publicly trusted CA, such as a + /// local test server. + pub fn tls_config(mut self, tls_config: CaTlsConfig) -> Self { + self.tls_config = tls_config; + self + } } /// A running Relay + QAD server. @@ -636,6 +649,11 @@ pub enum SpawnError { TlsHeaderParse { source: InvalidHeaderValue }, #[error("Failed to bind TcpListener")] BindTlsListener { source: std::io::Error }, + #[error("Failed to build ACME client TLS config")] + AcmeClientTlsConfig { + #[error(std_err)] + source: std::io::Error, + }, #[error("No local address")] NoLocalAddr { source: std::io::Error }, #[error("Failed to bind server socket to {addr}")] @@ -731,11 +749,20 @@ impl Server { server_config_builder, } => { let cache = acme_config.cache_path.map(DirCache::new); + let crypto_provider = + server_config_builder.crypto_provider().clone(); + let client_tls_config = acme_config + .tls_config + .client_config(crypto_provider) + .map_err(|err| e!(SpawnError::AcmeClientTlsConfig, err))?; let config = - tokio_rustls_acme::AcmeConfig::new(acme_config.domains) - .contact(acme_config.contact) - .directory(acme_config.directory_url) - .cache_option(cache); + tokio_rustls_acme::AcmeConfig::new_with_client_tls_config( + acme_config.domains, + Arc::new(client_tls_config), + ) + .contact(acme_config.contact) + .directory(acme_config.directory_url) + .cache_option(cache); let mut state = config.state(); let resolver = state.resolver().clone(); let server_config =