MTA-STS
Mail between servers is usually encrypted opportunistically. The sender uses TLS when the receiver offers it, and falls back to plain text otherwise. An attacker on the network path can exploit that fallback, by removing the STARTTLS offer or by presenting a false certificate, as described in Transport security.
MTA Strict Transport Security (MTA-STS, RFC 8461) lets a domain declare that its mail servers accept TLS with certificates from public certificate authorities, and that senders must not deliver to them in any other way. Unlike DANE, it does not depend on DNSSEC: the policy itself is served over HTTPS, and the web server’s certificate protects it.
A domain announces its policy with a TXT record at _mta-sts.<domain>, and serves the policy at https://mta-sts.<domain>/.well-known/mta-sts.txt. The policy holds three things: the MX host names that may receive the domain’s mail, a mode, and how long senders may cache the policy. The mode decides what a sender does when a check fails:
- In
enforcemode, a sender delivers only to a listed host, over TLS with a valid certificate. - In
testingmode, a sender delivers anyway and reports the failures. - The mode
nonewithdraws the policy.
Stalwart takes part on both sides of the protocol, and the two sides are configured separately. As a receiver, it publishes a policy for the domains it hosts, so that other servers deliver to them only over TLS, as described in Publishing a policy. As a sender, it applies the policies of recipient domains when it delivers mail, as described in Applying recipient policies.
Publishing a policy
Section titled “Publishing a policy”A sender that supports MTA-STS looks up the TXT record at _mta-sts.<domain> before delivering, and fetches the policy file when it finds one. The file states the policy’s version (always STSv1), its mode, the mx host names and the max_age in seconds.
Because senders cache the policy for its max_age, they need a way to notice that it has changed. The TXT record carries an id, which senders compare with their cached copy. A new id makes them fetch the policy again.
Stalwart serves the policy file itself, and generates both DNS records for every hosted domain, as long as a policy exists. A policy exists when mode is not disable and the policy has at least one MX host. The records look like this:
mta-sts.example.com. CNAME mail.example.com._mta-sts.example.com. TXT "v=STSv1; id=<policy id>"The id is derived from the policy contents, so it changes whenever the policy changes. The TXT record must then be updated, and how that happens depends on the domain’s DNS management. With automatic DNS management and the mtaSts record type in publishRecords, the records are published, and the TXT record is updated when the records are refreshed. With manual DNS management, copy the records from the domain’s zone file, and update the TXT record after each policy change.
Policy settings
Section titled “Policy settings”One policy applies to all hosted domains. It is configured on the MtaSts singleton (found in the WebUI under Settings › MTA › Inbound › MTA-STS).
The main decision is how strictly senders should apply the policy. It is set in mode, as testing, enforce or disable. The policy is in testing mode until it is changed, so senders report failures but still deliver. With disable, no policy is served, and no MTA-STS records are generated.
How long senders keep a policy is set in maxAge, 7 days unless changed. The policy file states it in seconds. The value is a tradeoff: a long cache protects senders for longer, but it also bounds how quickly a policy change reaches them, as described in Rolling out a policy.
The host names allowed to receive mail are listed in mxHosts. When the list is empty, the hosts of SystemSettings.mailExchangers are used, so most servers need not set it. A name starting with *. matches every host in that domain.
The following policy enforces TLS for two mail exchangers and lets senders cache it for 30 days:
{ "mode": "enforce", "maxAge": 2592000000, "mxHosts": {"mx1.example.com": true, "mx2.example.com": true}}Requirements
Section titled “Requirements”A policy is a promise to senders, and under enforce a broken promise means undelivered mail. Three conditions must hold before a policy is published:
- The policy is served over HTTPS on
mta-sts.<domain>, so port 443 must be reachable and the certificate must covermta-sts.<domain>for every hosted domain. The default ACME certificate of a domain includes that name, as described in Names in a certificate. - Every host in
mxmust present, on port 25, a certificate from a public authority that is valid for its host name. Underenforce, a sender that cannot validate the certificate of any listed host does not deliver the mail at all. - The
mxlist must contain every host that receives the domain’s mail, including backup MX hosts operated elsewhere.
Rolling out a policy
Section titled “Rolling out a policy”Because a mistake under enforce stops delivery, a policy is introduced in stages. Start with testing and a TLS reporting record, so that senders report problems without refusing mail. When the reports show no failures for some weeks, switch to enforce.
The cache matters again when the mail hosts change. Before moving a domain’s mail to other hosts, add them to mxHosts and wait for maxAge to pass, because senders keep applying the cached policy until it expires.
Applying recipient policies
Section titled “Applying recipient policies”Delivery settings
Section titled “Delivery settings”The use of MTA-STS in delivery is set by mtaSts on the TLS strategy (found in the WebUI under Settings › MTA › Outbound › TLS Strategies). The setting decides whether Stalwart fetches the policies of recipient domains and how it treats a domain without one. It is optional by default:
| Value | Effect |
|---|---|
optional | Fetch the policy of each recipient domain that announces one, and apply it. When no policy can be fetched, apply the cached policy if an unexpired one exists, and otherwise deliver with opportunistic TLS. |
require | Deliver only to domains whose policy can be fetched. Delivery to a domain without MTA-STS fails permanently. |
disable | Fetch no policies. |
MTA-STS applies to MX delivery only, not to relay routes. The time allowed to fetch a policy is set by mtaStsTimeout.
Fetching policies
Section titled “Fetching policies”Stalwart fetches a policy from https://mta-sts.<domain>/.well-known/mta-sts.txt. It caches the policy for the max_age that the policy declares, as long as that value lies between one hour and one year. Otherwise it caches the policy for one day.
The fetch is a request to a host that the recipient domain chooses, and the recipient domain may be controlled by an attacker. Stalwart therefore restricts the fetch:
- Only
https://is used, without credentials, and redirections are not followed. - The policy host must be a public domain name, and its addresses must be public. A host that resolves to a private, loopback or link-local address is refused.
- The policy may not exceed 1 MiB.
A cached policy is not kept forever. Stalwart fetches the policy again when the id in the domain’s _mta-sts TXT record differs from the id of the cached policy.
A fetch can fail. When the TXT lookup fails, or the new policy cannot be fetched or parsed, Stalwart applies the unexpired cached policy, as RFC 8461 requires. After a failed fetch, Stalwart waits five minutes before it fetches the policy of that domain again. During that time, deliveries use the cached policy, or, when there is none, they are decided by the recorded failure. When several deliveries to the same domain need its policy at the same time, they share a single fetch.
Failures are logged as events. A refused or failed fetch is logged as mta-sts.policy-fetch-error with the reason, and an unparsable policy as mta-sts.invalid-policy. When no unexpired policy is cached, the failure decides the delivery, as in the following two cases:
- A policy host whose name is not under a public suffix, such as
mta-sts.example.lan, is refused withHost is not a public domain name. Underrequire, the delivery fails permanently withPolicy host rejected. - A policy host that resolves only to private addresses cannot be reached. This happens in laboratory networks and with split-horizon DNS. Under
require, the delivery is deferred and retried.
With mtaSts set to optional, delivery continues with opportunistic TLS in both cases.
Validation under an enforce policy
Section titled “Validation under an enforce policy”When a policy in enforce mode applies to the recipient domain, Stalwart delivers the message only when all of the following conditions hold:
- The MX host name matches one of the
mxpatterns of the policy. - The host offers
STARTTLSand the TLS handshake succeeds. - The host’s certificate chains to a trusted certificate authority and is within its validity period.
- The certificate names the MX host name.
Otherwise the host is not delivered to. Whether Stalwart keeps trying depends on which condition failed:
- An MX host that the policy does not list (
mta-sts.not-authorized), or one that does not offerSTARTTLS, is a permanent failure. When no MX host qualifies, the message is returned to the sender at once. - A failed TLS handshake or certificate check is a temporary failure. It is retried on the recipient’s schedule until the message expires.
Domains that request TLS reports receive the failure in the next report. Policies in testing mode never prevent delivery.
Invalid certificates and enforce policies
Section titled “Invalid certificates and enforce policies”An enforcing policy overrides the TLS strategy’s tolerance of invalid certificates. The allowInvalidCerts setting has no effect on a domain whose policy is in enforce mode, and the domain’s certificates are validated regardless.
This affects the default fallback for TLS errors. The default tls expression of the outbound strategy retries a delivery that failed with a TLS error under the invalid-tls strategy, which accepts invalid certificates. That fallback therefore applies only to domains that have neither an enforcing policy nor DANE.
Sometimes a domain publishes an enforcing policy but its servers’ certificates cannot be fixed, for example a domain under the same administration. To deliver to such a domain, use a TLS strategy with mtaSts set to disable for it. The strategy must be selected with rcpt_domain. The reason is that the MTA-STS setting is taken from the evaluation of the tls expression made for the recipient domain, before the MX hosts are known.
Disabling MTA-STS also removes its requirement of TLS. To keep refusing unencrypted delivery, set startTls to require in the same strategy:
{ "name": "bypass-mta-sts", "mtaSts": "disable", "allowInvalidCerts": true, "startTls": "require"}