Skip to content

Client certificates

Some services should only be reachable from known devices or systems: an IMAP or JMAP endpoint for managed laptops, a submission port for internal applications, or an HTTPS listener used only by an administration network. A listener can enforce this at the TLS layer by requiring every client to present a certificate issued by a certificate authority that the administrator trusts. A client without such a certificate never gets past the TLS handshake, so it cannot reach the protocol at all, and in particular cannot try passwords against it.

The certificate only gates the connection. It does not identify the user: once the handshake succeeds, the client authenticates as usual, with OAuth, a passkey or a password. Stalwart does not offer SASL EXTERNAL and does not map certificates to accounts, so a valid certificate grants no access to any mailbox by itself.

Client certificates are set per listener, in the tlsClientAuth field of the NetworkListener object (found in the WebUI under Settings › Network › Listeners). The field has two variants:

  • Disabled: the listener does not request a client certificate. This is the setting of every listener unless changed.
  • Required: every client must present a valid certificate issued by one of the certificate authorities in caCertificates, otherwise the handshake fails.

caCertificates holds one or more CA certificates in PEM format. Any certificate that chains to one of them is accepted, whatever its subject, so the authorities listed here should issue certificates only to the clients that may connect. A dedicated internal CA serves this purpose better than a public one. When a client certificate carries an extended key usage extension, the extension must include TLS client authentication.

Both certificate fields take their value inline (Text), from an environment variable (EnvironmentVariable) or from a file (File). The following listener accepts IMAP with implicit TLS on port 993 only from clients that hold a certificate from an internal CA, and reads both the CA certificate and the revocation list from files:

{
"name": "imaps",
"protocol": "imap",
"bind": {"[::]:993": true},
"useTls": true,
"implicitTls": true,
"tlsClientAuth": {
"@type": "Required",
"caCertificates": {"@type": "File", "filePath": "/etc/stalwart/client-ca.pem"},
"revocationLists": {"@type": "File", "filePath": "/etc/stalwart/client-ca.crl"}
}
}

The requirement applies to every client of the listener. On an HTTPS listener this includes web browsers opening the WebUI and every JMAP, CalDAV and CardDAV client, so a service meant for a restricted group usually gets a listener of its own, while the general listeners keep tlsClientAuth disabled. ACME TLS-ALPN-01 challenges are still answered on a listener that requires client certificates, because the server handles them before the client certificate check.

revocationLists takes one or more certificate revocation lists (CRLs) in PEM format. A client certificate listed in one of them is rejected. A certificate issued by an authority for which no list is supplied is accepted, so a CRL only needs to be supplied for the authorities that actually revoke certificates.

The server reads the lists when the listener starts and does not fetch them from distribution points. A newly published list therefore takes effect only after it has been placed in the setting, or in the file it is read from, and the server has restarted.

Client certificates require a listener with both useTls and implicitTls enabled, which means a port where the connection starts with the TLS handshake, as RFC 8314 describes for ports 465, 993 and 995. On a STARTTLS port the connection starts in plain text, and a client that never sends STARTTLS would reach the protocol without presenting any certificate, so the requirement could be bypassed.

The server refuses this combination. Saving a listener that requires client certificates without implicit TLS fails with the error Client certificates require TLS with implicit TLS enabled. A listener that reaches startup with this combination anyway is not started, and the server logs a registry.build-error event for it with the reason Client certificates require implicit TLS, otherwise clients can skip the handshake.

Listeners explains the two TLS modes.

Changes to tlsClientAuth, including a new revocation list, take effect only when the server restarts, like other listener changes. Reloading the settings or the TLS certificates does not rebuild the client certificate check.

The CA certificates and revocation lists are checked when the listener is saved, and an invalid value is refused with an error that names the problem. When they cannot be loaded at startup, for example because a file is missing or no longer parses, the listener is not started, and the server logs a registry.build-error event with the reason Invalid client certificate settings: <error>. The listener is never started without TLS or without the client certificate check.

For each connection on an implicit-TLS listener that presented a client certificate, the event that opens the session, such as imap.connection-start or http.connection-start, carries two extra keys: subject, the subject name of the client certificate, and fingerprint, its SHA-256 fingerprint in hexadecimal. These keys identify which device or system opened a session. The connection start events are logged at the debug level, so the tracer must include that level for them to appear.

A client that presents no certificate, or a certificate that does not validate, fails the handshake. The server logs a tls.handshake-error event with the reason given by the TLS library, for example:

  • peer sent no certificates: the client did not present a certificate, usually because none is configured for it.
  • invalid peer certificate: UnknownIssuer: the certificate does not chain to any of the certificate authorities in caCertificates.
  • invalid peer certificate: Revoked: the certificate is listed in one of the revocation lists.

Expired certificates and certificates without the TLS client authentication usage are rejected in the same way, with a reason that says so.