Stalwart serves the wrong TLS certificate for mail.example.com

Your question

Stalwart serves the wrong TLS certificate and fails to renew an expired certificate

Environment

  • Stalwart version: 0.16.26
  • Installation: native Linux installation, running as a systemd service (not Docker)
  • Configuration backend: RocksDB
  • ACME provider: Let’s Encrypt
  • ACME certificate and DNS management: enabled

All domain names, IP addresses, and certificate identifiers in this report have been anonymized.

Problem

Stalwart was serving the wrong TLS certificate for mail.example.com, even when the client explicitly sent mail.example.com as the TLS SNI hostname.

The server presented a Let’s Encrypt certificate with:

  • Subject / CN: example.com
  • SAN: DNS:example.com

This certificate does not include mail.example.com.

The same incorrect certificate was served on SMTP ports 25 (STARTTLS), 465 (implicit TLS), and 587 (STARTTLS).

Certificates stored in Stalwart

The certificate list in the Stalwart UI contained two certificates:

  1. A certificate for example.com, valid until 2026-12-13.
  2. A multi-SAN certificate covering:
    • autoconfig.example.com
    • autodiscover.example.com
    • mail.example.com
    • mta-sts.example.com
    • ua-auto-config.example.com

The second certificate expired on 2026-10-14.

ACME configuration

The domain example.com was configured to use ACME TLS certificate management with Let’s Encrypt and automatic DNS management.

The Additional Hostnames field contained only example.com. The configuration had worked for approximately three months before the multi-SAN certificate expired.

When I tried to trigger certificate renewal through the admin interface, I received the following message:

Certificate for domain mail.example.com is still valid; renewal is not due until 2026-11-13T09:21:57Z

However, the multi-SAN certificate covering mail.example.com had already expired on 2026-10-14.

There were also multiple failed ACME renewal and DNS management tasks.

Reproduction

Connect to the server while explicitly specifying mail.example.com as SNI:

openssl s_client \
  -connect mail.example.com:25 \
  -starttls smtp \
  -servername mail.example.com

The server returned the certificate for example.com, which did not contain the requested hostname.

The same result occurred on ports 465 and 587.

DNS A resolution for mail.example.com returned <server-ip>. No AAAA record was configured.

Workaround

I tried several approaches through the admin interface to renew the certificate and configure the correct certificate, but they did not resolve the issue.

The issue was eventually resolved by manually creating an ACME renewal task through the Stalwart CLI:

stalwart-cli create task/AcmeRenewal \
  --url https://mail.example.com \
  --user '<admin-user>' \
  --password '<admin-password>' \
  -k \
  --field domainId=b

After creating this task, Stalwart began serving the correct TLS certificates for all required domains and subdomains. The mail service was working correctly again.

No other changes were required to achieve this result.

Expected behavior

Stalwart should automatically renew ACME-managed certificates when necessary and select the appropriate certificate for each hostname based on the client’s SNI.

If renewal cannot proceed, the system should provide a clear explanation of why it was not attempted, especially when the certificate has already expired.

Questions

  1. Why did the admin interface report that renewal was not due until 2026-11-13 when the multi-SAN certificate had already expired on 2026-10-14?
  2. Why did Stalwart serve the certificate for example.com when the client explicitly sent mail.example.com as SNI, despite a certificate covering mail.example.com being present in its certificate database?
  3. Why did manually creating an AcmeRenewal task resolve the issue for all required domains and subdomains when the normal renewal workflow did not?
  4. Is this expected behavior, a configuration issue, or a bug in ACME renewal scheduling or TLS certificate selection in Stalwart 0.16.26?
  5. Should an administrator ever need to create ACME renewal tasks manually, or should Stalwart handle renewal and certificate selection automatically?
  6. Is there any configuration change recommended to prevent this issue from recurring?

I would appreciate guidance on the underlying cause and the recommended permanent solution. Since this is a production mail server, I would prefer not to rely on manually creating renewal tasks every time certificates expire.

Based on the actual renewal and certificate‑selection code, here is what’s happening:

Why it said “not due” (Q1): The renewal check (acme_certificate_renewal_due) does not look at “the certificate currently covering this domain’s SANs” in a fuzzy way, it looks up a stored certificate whose SAN list, sorted, exactly matches the domain’s currently configured set of hostnames (domain.name + the Additional Hostnames field). If the Additional Hostnames field no longer lists the same set that was used to request the multi-SAN cert (e.g. it was later trimmed down to just example.com), the lookup no longer matches the expired multi‑SAN certificate; it can instead match a different, still‑valid stored certificate object whose SAN set happens to equal the current config. The renewal check then reports “not due” based on that unrelated certificate’s expiry, not the one actually expired. This is why the reported due date (2026-11-13) didn’t correspond to the expired multi‑SAN cert’s dates at all.

Why the wrong cert was served over TLS (Q2): At startup/reload, Stalwart builds an in‑memory map of hostname → certificate from the stored Certificate objects. Any certificate whose not_valid_after <= now is explicitly skipped and excluded from that map entirely (it’s still listed in the admin UI’s certificate store, but it is not loaded for TLS serving). So once the multi‑SAN cert expired, mail.example.com had no entry in the in‑memory map. The SNI resolver (resolve_certificate) falls back from a missing exact hostname match to the parent domain (splitting on the first dot), so a request for mail.example.com fell through to whatever certificate was registered for example.com, the apex cert. This is the expected fallback behavior for unmanaged hostnames, but here it kicked in only because the matching cert had aged out of the live map, not because it was absent.

Why the manual AcmeRenewal task worked (Q3/Q4): It invokes the same renewal function, but by that point the stored certificate really was expired “on the books” too, so the short-circuit (if not_valid_after <= now { return Ok(None) }) forced renewal regardless of the SAN-matching logic that had previously misfired. Practically, this points to a configuration drift issue (the Additional Hostnames list for the domain no longer matching the SAN set that was actually issued) combined with a renewal-lookup that requires an exact SAN-set match rather than a best-effort one, rather than something inherently wrong in the ACME protocol handling itself.

Recommendation (Q5/Q6): Make sure the Additional Hostnames field on the domain’s certificateManagement object always reflects the full, current SAN set you expect the certificate to carry (autoconfig, autodiscover, mail, mta-sts, ua-auto-config, etc.) and don’t change it after the fact without also re-triggering a renewal, since the renewal lookup keys off that exact set. You should not normally need to create AcmeRenewal tasks by hand; doing so is only a workaround here because of the stale SAN-set mismatch.

Reference: Domain certificate management and TLS certificates.

This is an automated reply from the Stalwart Help Bot. Other community members may follow up if this answer is incomplete or wrong.

Fixed in v1.0.0 (expected next week).