Concepts / Trustworthy DNS and mail

Trustworthy DNS and mail

SPF, DKIM and DMARC

Also called SPF, DKIM, DMARC, email authentication, Authentication-Results, DKIM selector, alignment, email spoofing

Three DNS-published policies that let a receiving mail server check that a message really comes from the domain it claims.

Three checks, three DNS records
Three checks, three DNS records

SPF checks the sending address, DKIM checks a signature over the message, and DMARC requires one of them to pass for the domain in the From: line the reader sees. Without DMARC, SPF and DKIM can pass for a different domain.

SPF, DKIM and DMARC are three DNS records that let a receiving mail server check a message comes from the domain it claims. SPF lists the addresses allowed to send for the envelope sender's domain. DKIM signs headers and body; the public key is at <selector>._domainkey.<domain>. DMARC (_dmarc.<domain>) counts a pass only for a domain aligned with the From: line, and says what to do otherwise: none, quarantine or reject.

How SPF, DKIM and DMARC work, step by step

In the SPF, DKIM and DMARC lab a rowing club's captain mails from clubMail (10.25.1.25, rowclub.lab) to Lotte at a big provider, postboxMail (10.25.2.25), and a forger at 10.25.2.66 writes in the captain's name. The email lab shows the plain relay; here three checks are added on top of SMTP:

  1. The sender publishes policies in DNS, as TXT records: SPF at rowclub.lab (v=spf1 ip4:10.25.1.25 -all), the DKIM public key at mail._domainkey.rowclub.lab (selector mail), and DMARC at _dmarc.rowclub.lab (v=DMARC1; p=reject).
  2. The sending server signs (DKIM, RFC 6376). It hashes the body (bh=), then signs that hash with the listed headers (h=From:To:Subject) using its private key, after canonicalization (c=relaxed/simple: header case and spacing normalized, body as sent). The result goes on top of the message as a DKIM-Signature: header with d=rowclub.lab; s=mail.
  3. The receiver checks SPF (RFC 7208) during the SMTP conversation. It takes the domain of the envelope sender (MAIL FROM), reads its SPF record, and tests the connecting address against each mechanism in the order the record lists them (ip4, a, mx, include, usually ending in all); the first match decides. -all means fail, ~all softfail. At most 10 DNS lookups are allowed; more is a permanent error.
  4. The receiver checks DKIM. It fetches the key at <s>._domainkey.<d>, recomputes the body hash and verifies the signature. Any change to the body or a signed header fails it.
  5. DMARC decides (RFC 7489). It takes the domain in the visible From: line and asks: did SPF pass for an aligned envelope domain, or DKIM for an aligned d=? Relaxed alignment (r, the default) accepts the same organizational domain, strict (s) only the exact name. One aligned pass is enough. Otherwise the policy applies: none, quarantine or reject.
  6. The verdict is written down in Authentication-Results: headers (RFC 8601), and reports go to the addresses in the DMARC record's rua= tag, if any.

What it looks like on a Linux machine

The policies are ordinary DNS records. The 2048-bit key is too long for one 255-character TXT string, so it comes as two, which readers join:

$ dig +short TXT rowclub.lab
"v=spf1 ip4:10.25.1.25 -all"
$ dig +short TXT mail._domainkey.rowclub.lab
"v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuD/7NiZx…" "…"

Postfix hands each message to milters: opendkim signs on clubMail and checks signatures on postboxMail, and opendmarc on postboxMail checks SPF itself, then DMARC. The captain's real message reaches Lotte with the verdicts on top:

Received: from clubMail.lab (clubMail [10.25.1.25])
Authentication-Results: postboxMail.lab; dmarc=pass (p=reject dis=none) header.from=rowclub.lab
Authentication-Results: postboxMail.lab; spf=pass smtp.mailfrom=rowclub.lab
Authentication-Results: postboxMail.lab;
	dkim=pass (2048-bit key; unprotected) header.d=rowclub.lab header.i=@rowclub.lab header.a=rsa-sha256 header.s=mail …
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=rowclub.lab; s=mail;

The forger sends "Club fees: new bank account" from its own domain's envelope, billing@quickparcel.lab, with From: captain@rowclub.lab. SPF passes for quickparcel.lab, nothing passes for rowclub.lab, and postboxMail refuses it at the end of the SMTP conversation:

$ grep reject /var/log/mail.log
… milter-reject: END-OF-MESSAGE from forger[10.25.2.66]: 5.7.1 rejected by DMARC policy for rowclub.lab; from=<billing@quickparcel.lab> to=<lotte@postbox.lab>

curl, the sender here, reports it as curl: (8) Weird server reply.

What goes wrong, and how you see it

A real sender is missing. A newsletter service or a new server mails as the domain from an address SPF does not list, and does not sign with the domain's key: spf=fail, no aligned DKIM, and p=reject bounces every copy. Add it to SPF (services hand out an include:) and publish its DKIM key under its own selector.

SPF passes for the wrong domain. The forger uses its own envelope sender, whose SPF lists its own server: spf=pass smtp.mailfrom=quickparcel.lab. DMARC still fails, because quickparcel.lab is not aligned with From: rowclub.lab. Alignment is the point of DMARC.

The DMARC record is at the wrong name. Receivers ask only for _dmarc.<domain>. A record typed as dmarc is served happily at the wrong name, and every receiver writes dmarc=none: no policy at all.

The body changed in transit. A footer added by a mailing list, or an edit by an attacker, breaks the body hash and DKIM fails. Forwarding also breaks SPF, since the forwarder's address is not listed; ARC (RFC 8617) exists to carry the original results across such hops.

A signed message is replayed. Someone with one real message sends it again, unchanged, from their own server. DKIM still passes, so DMARC passes, and it lands in the inbox. The newest Received: line shows where this copy really came from.

The policy only watches. With p=none a failing forgery is delivered. Move to quarantine, then reject, once reports show every real sender passing.

Questions people ask

Do I need all three? Yes. SPF alone breaks on forwarding, DKIM alone does not say what to do with a failure, and neither checks the From: line the reader sees. DMARC needs at least one of them to pass, aligned.

What does "dmarc=fail" with "spf=pass" mean? SPF passed for the envelope domain, but that domain does not match the visible From domain. Typical of a forger using its own domain, or a third-party sender not set up to sign as you.

Where are SPF, DKIM and DMARC stored? All three are DNS TXT records: SPF on the domain itself, DKIM at <selector>._domainkey.<domain>, DMARC at _dmarc.<domain>.

See it on real machines

Read next

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab