From 4e84dd1034a358fdfcda709813661c9a2c6418ac Mon Sep 17 00:00:00 2001 From: John Coffey Date: Wed, 18 Nov 2026 11:34:23 -0700 Subject: [PATCH] TLS: without a default certificate, use the one for the server's hostname With several certificates and no default set, every handshake without SNI or for an unknown name logged tls.multiple-certificates-available at Warn and got whichever certificate the hash map yielded first. Loading the certificates now picks a default when none is set: the one for defaultHostname (exact, then a wildcard on its domain), otherwise the one whose name sorts first. The event is logged once per load, and only when the hostname isn't covered. Fixes #418 --- crates/common/src/config/server/tls.rs | 99 ++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/crates/common/src/config/server/tls.rs b/crates/common/src/config/server/tls.rs index df5660f1..a59189e7 100644 --- a/crates/common/src/config/server/tls.rs +++ b/crates/common/src/config/server/tls.rs @@ -2,6 +2,8 @@ * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC * * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL + * + * Modified by Coffey Labs in 2026 for INBUXA. */ use crate::network::acme::ParsedCert; @@ -194,6 +196,23 @@ pub(crate) async fn parse_certificates( } } + // No default certificate set: pick one now, once per load, so handshakes + // without SNI or for an unknown name get a predictable certificate. + if !certificates.contains_key("*") && certificates.len() > 1 { + let (key, covers_hostname) = + fallback_default_certificate(certificates, &system.default_hostname); + if !covers_hostname { + trc::event!( + Tls(trc::TlsEvent::MultipleCertificatesAvailable), + Total = certificates.len(), + Hostname = system.default_hostname.clone(), + ); + } + if let Some(key) = key { + certificates.insert("*".into(), key); + } + } + // Remove expired certificates if !certs_expired.is_empty() { for (id, sans, object) in certs_expired { @@ -216,6 +235,34 @@ pub(crate) async fn parse_certificates( } } +/// The certificate to use when no default is set: the one for the server's +/// hostname (exact, then a wildcard on its parent domain), otherwise the one +/// whose name sorts first, so the choice doesn't depend on hash-map order. +/// The flag says whether the hostname was covered. +pub(crate) fn fallback_default_certificate( + certificates: &AHashMap, T>, + hostname: &str, +) -> (Option, bool) { + let hostname = hostname.trim_end_matches('.').to_ascii_lowercase(); + if !hostname.is_empty() + && let Some(cert) = certificates.get(hostname.as_str()).or_else(|| { + hostname + .split_once('.') + .and_then(|(_, domain)| certificates.get(domain)) + }) + { + return (Some(cert.clone()), true); + } + ( + certificates + .iter() + .filter(|(name, _)| name.as_ref() != "*") + .min_by(|a, b| a.0.cmp(b.0)) + .map(|(_, cert)| cert.clone()), + false, + ) +} + pub(crate) fn build_certified_key( cert: Vec, pk_bytes: Vec, @@ -276,3 +323,55 @@ pub(crate) fn build_self_signed_cert( signing_key.serialize_pem().into_bytes(), ) } + +#[cfg(test)] +mod tests { + use super::fallback_default_certificate; + use ahash::AHashMap; + + fn certs(names: &[(&str, u32)]) -> AHashMap, u32> { + names + .iter() + .map(|(name, id)| (Box::::from(*name), *id)) + .collect() + } + + #[test] + fn fallback_prefers_the_server_hostname() { + let map = certs(&[("a.example.org", 1), ("mail.example.com", 2), ("z.test", 3)]); + assert_eq!( + fallback_default_certificate(&map, "mail.example.com"), + (Some(2), true) + ); + assert_eq!( + fallback_default_certificate(&map, "MAIL.example.com."), + (Some(2), true) + ); + } + + #[test] + fn fallback_uses_a_wildcard_on_the_hostname_domain() { + // A "*.example.com" SAN is stored under "example.com". + let map = certs(&[("a.example.org", 1), ("example.com", 2)]); + assert_eq!( + fallback_default_certificate(&map, "mail.example.com"), + (Some(2), true) + ); + } + + #[test] + fn fallback_without_a_match_is_the_same_every_time() { + let names: Vec<(String, u32)> = (0..64).map(|i| (format!("d{i:02}.test"), i)).collect(); + for _ in 0..20 { + let map: AHashMap, u32> = names + .iter() + .map(|(n, i)| (Box::::from(n.as_str()), *i)) + .collect(); + assert_eq!( + fallback_default_certificate(&map, "mail.other.net"), + (Some(0), false) + ); + assert_eq!(fallback_default_certificate(&map, ""), (Some(0), false)); + } + } +} -- 2.54.0