Outgoing connections
Stalwart opens TLS connections of its own to several kinds of servers: the SMTP relays that its routes deliver through, the PostgreSQL and MySQL databases that hold its stores, and the LDAP directories that hold its accounts. By default, it verifies each server’s certificate against the operating system’s trust store and the name it connects to, and presents no certificate of its own. That suits servers with publicly trusted certificates, but not every deployment has them. Databases and directories often use certificates from a private CA, some servers are reached by an address that their certificate does not name, and some require the client to authenticate with a certificate (mutual TLS). A Microsoft 365 connector, for example, can identify the sending server by its certificate.
The tlsSettings field covers these cases. It is available on the following objects:
- The
Relayvariant of MtaRoute, described in Routing. - The
PostgreSqlandMySqlvariants of the store objects, such as DataStore, BlobStore and SearchStore, described in PostgreSQL and MySQL. - The
Ldapvariant of Directory, described in LDAP.
TLS for deliveries to the recipients’ MX hosts is not configured here: it follows the TLS strategy, DANE and MTA-STS, as described in Outbound TLS.
TLS settings
Section titled “TLS settings”tlsSettings has two variants. Default keeps the behaviour described above and is the setting of every object unless changed. Custom takes the following decisions, each of them optional:
- Which certificate authorities to trust, and which certificates they have revoked.
- Whether the server’s certificate must be valid for the name the connection uses.
- Which client certificate to present to the server.
The settings only apply when the connection uses TLS. Each object keeps its own switch for that: useTls on a PostgreSQL or MySQL store, an ldaps:// URL or useTls on an LDAP directory, and implicit TLS or STARTTLS on a relay route. With TLS off, the custom settings have no effect and no certificate is presented.
Trusted certificate authorities
Section titled “Trusted certificate authorities”caCertificates holds one or more CA certificates in PEM format. When it is set, it replaces the operating system’s trust store for this connection: only certificates that chain to these authorities are accepted, and the public authorities are no longer trusted. This pins the connection to a private CA, so that a certificate obtained from any public authority for the same name is not enough to impersonate the server.
revocationLists adds one or more certificate revocation lists in PEM format. A certificate listed in one of them is rejected, while a certificate from an authority without a matching list is accepted. Revocation lists are checked against the authorities in caCertificates, so they require that field. Stalwart does not download lists from distribution points.
Hostname verification
Section titled “Hostname verification”verifyHostname decides whether the server’s certificate must be valid for the host name or address that the connection uses. It is enabled unless changed. When it is disabled, the certificate still has to chain to one of the trusted authorities and be otherwise valid, but its names are not compared with the host. This helps when a database is reached by an IP address or an internal name that its certificate does not list, and it matches the verify-ca mode of PostgreSQL clients.
Disabling hostname verification requires caCertificates. Without a private CA, any certificate from a public authority would be accepted for any server, which would make the check meaningless.
Client certificate
Section titled “Client certificate”certificate and privateKey set the certificate that Stalwart presents when the server asks for one. certificate holds the chain in PEM format, with the leaf certificate first, followed by any intermediate certificates the server needs to build the chain. privateKey holds the matching private key in PEM format. The two must be given together.
Invalid certificates
Section titled “Invalid certificates”Each of these objects also has an allowInvalidCerts field, which keeps its meaning with custom settings: when it is enabled, the server’s certificate is not verified at all, so caCertificates, revocationLists and verifyHostname are ignored. The client certificate is still presented. allowInvalidCerts is meant for testing; a private CA in caCertificates is the way to connect to a server with a certificate that public authorities did not issue.
Example
Section titled “Example”All certificate fields take their value inline (Text), from an environment variable (EnvironmentVariable) or from a file (File), and privateKey is stored as a secret. The following settings trust only an internal CA, check its revocation list and present a client certificate, all read from files:
{ "tlsSettings": { "@type": "Custom", "caCertificates": {"@type": "File", "filePath": "/etc/stalwart/tls/internal-ca.pem"}, "revocationLists": {"@type": "File", "filePath": "/etc/stalwart/tls/internal-ca.crl"}, "certificate": {"@type": "File", "filePath": "/etc/stalwart/tls/client.pem"}, "privateKey": {"@type": "File", "filePath": "/etc/stalwart/tls/client.key"} }}Errors
Section titled “Errors”Relay routes and LDAP directories are checked when they are saved, and invalid TLS settings are refused with an error on tlsSettings that describes the problem. The most common errors are:
Revocation lists require trusted CA certificates:revocationListsis set withoutcaCertificates.Disabling hostname verification requires trusted CA certificates:verifyHostnameis disabled withoutcaCertificates.A client certificate requires a private keyandA private key requires a client certificate: only one half of the client certificate is set.No certificates foundandNo revocation lists found: a field holds no PEM block of the expected type, often because a file contains something else or the wrong field was used.Invalid client certificate or private key: <error>: the key does not match the certificate or cannot be used.
PostgreSQL and MySQL stores are not checked when they are saved. An error in their TLS settings appears when the store is opened, at the next start, and the store fails to open with one of the errors above.
A relay route whose TLS settings cannot be loaded when the settings are read, for example because a file has gone missing, is logged as a registry.build-error event with the reason Invalid TLS settings: <error>. Deliveries through that route are deferred with a TLS error and retried later, rather than sent without the client certificate or the configured trust.