Day 3 · Sender authentication

DKIM explained: how email signatures work, with a signing lab

lesson 09 / 1919 min1 labpublished
Course contents
On this page
  1. The idea in four steps
  2. The DKIM-Signature header, tag by tag
  3. Canonicalization: surviving harmless changes
  4. The key in DNS
  5. Key rotation and revocation
  6. What breaks DKIM in real life
  7. What DKIM doesn’t do
  8. What the receiver writes
  9. Signing in practice

SPF vouches for a server: this IP may send for this domain. DKIM (DomainKeys Identified Mail, RFC 6376) vouches for a message: the sending domain signs the message with a private key, publishes the public key in DNS, and any receiver can verify that the signed parts arrived unchanged and that the domain took responsibility for them. Because the signature travels inside the message, it survives forwarding, which is exactly where SPF fails. This lesson takes a signature apart, then has you create one, verify it, and break it.

The idea in four steps

  1. The sending server picks some headers (From, To, Subject, Date…) and the body, normalizes them (canonicalization), hashes them, and signs the hash with the domain’s private key.
  2. It adds the result as a DKIM-Signature header, which names the signing domain (d=) and a selector (s=).
  3. The receiver reads d= and s=, and fetches the public key from DNS at <selector>._domainkey.<domain>.
  4. It recomputes the hashes and checks the signature. If anything signed changed on the way, verification fails.

The selector is what lets a domain have many keys at once: one per provider (Google signs with google, Microsoft 365 with selector1 and selector2, your ESP with its own), and a new one each time you rotate.

The DKIM-Signature header, tag by tag

This is a real signature from the lab below:

DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=example.com;
 i=@example.com; q=dns/txt; s=lab2026; t=1793536939; h=from : to :
 subject : date : message-id;
 bh=TZYCZfc1DuFpWN+nQHRaam3xSiDGKAWR+d3V5t6Z1t8=;
 b=QIT6R6JceZ6QV8vkRbWswY3DM0DPbqvfSTpXq/mUhCPA5mjNdTXjPPGiSfBj1QSGAYT1k
 CVtTd31U+MXhABXocWd/4rQHPaiacoZ1ip2KRtHq/QBwZEAzvgXjLE8eabO6OjVJg3mN4RJ
 ...
TagMeaning
v=1Version
a=rsa-sha256Algorithm: RSA with SHA-256. (ed25519-sha256, RFC 8463, also exists; rsa-sha1 is forbidden since RFC 8301)
c=relaxed/relaxedCanonicalization for headers / body (below)
d=example.comThe signing domain: who takes responsibility. This is the domain DMARC compares with From
s=lab2026The selector: the key is at lab2026._domainkey.example.com
h=from : to : subject : date : message-idThe signed headers, in order
bh=The hash of the canonicalized body
b=The signature itself, over the signed headers and this header (with b= empty)
i=The identity (user or subdomain) on whose behalf it was signed. Optional
t=, x=Signing time and optional expiry, as Unix timestamps
l=Body length: only the first l bytes are signed. Don’t use it: anyone can append content after them
q=dns/txtHow to find the key (the only method defined)

Two hashes, two meanings: bh covers the body, and b covers the headers plus bh. If the body changes, the body hash no longer matches; if a signed header changes, the signature no longer matches.

Which headers to sign

From must be signed (it’s required), and you should also sign To, Cc, Subject, Date, Message-ID, Reply-To, MIME-Version, Content-Type and the List-Unsubscribe headers if present. Good signers also oversign important headers: they list From (or Subject) in h= one time more than it appears. The extra entry signs the absence of a second From header, so nobody can add one later and have a client show it instead of the real one.

Canonicalization: surviving harmless changes

Servers on the path sometimes make harmless changes: re-folding long headers, changing the case of header names, changing whitespace. Canonicalization defines how much change is tolerated:

  • simple: (almost) no change allowed. One extra space and the signature fails.
  • relaxed: header names are lowercased, header values have whitespace runs collapsed to one space and folding removed; in the body, runs of spaces and tabs become one space, trailing whitespace on each line is removed, and empty lines at the end are ignored.

Always sign with relaxed/relaxed. It’s what every major provider does.

The key in DNS

The public key is a TXT record. opendkim-genkey (from the course setup) generates a key pair and the record:

opendkim-genkey -b 2048 -d example.com -s lab2026
cat lab2026.txt
lab2026._domainkey	IN	TXT	( "v=DKIM1; h=sha256; k=rsa; "
	  "p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAywhzeHRBVHsPfcQOAmMvIGF9LjtZ83VNLb9MQwncbganlygnxfbK6gXpiBSSzUrxKhxAKb/YWc+ZTV2TEvT8F8btsMkTRq+HBneW0rPjhIhYrJQIby6tjeaJWEHJlW/KU2n8ncbXdloOux0Ki9j4qa0FLpbyguLj/5Ke3S846lbx4lRnvrm9FGAkfoQEwY3CkiJJei/nVLv5pi"
	  "TNQ0s3sD6yGfzG42MNPL3QakWsC3bvJBTDNnBroT9o4RCkuxFgieOHVmsz9SCYZb5CM1/lTyS+qYRY6Flvk3sfwuvHnce6/Aoc4uM95rBQILvyCtoElV2Yb7egJxkJNHQ4d3/qhQIDAQAB" )  ; ----- DKIM key lab2026 for example.com

k=rsa is the key type, p= the public key in base64, h=sha256 restricts the hash algorithm. A 2048-bit key doesn’t fit in one 255-byte string, so the record is split into several strings, which receivers join without spaces. In a web DNS panel, paste the value as one long string; most panels split it for you, and dig lets you check the result.

Key size: RFC 8301 requires at least 1024 bits and recommends 2048. Use 2048-bit RSA. Ed25519 keys are much shorter, but not every receiver verifies them yet, so they’re only useful in addition to an RSA signature.

Delegation with CNAME: Microsoft 365 and many ESPs ask you to publish a CNAME instead of a TXT, pointing your selector to a key they host. That lets the provider rotate keys without asking you to change DNS:

dig +short selector1._domainkey.github.com CNAME
selector1-github-com._domainkey.microsoft.onmicrosoft.com.

Key rotation and revocation

A DKIM private key sits on servers for years, and leaks happen. Rotate keys regularly (every six to twelve months is common practice), and always after a suspected compromise or when a provider is retired:

  1. Generate a new key with a new selector (date-based names such as s2026a make the age obvious) and publish it in DNS.
  2. Once it resolves everywhere, switch signing to the new selector.
  3. Keep the old public key published for a week or so: messages signed with it may still be in queues or be verified late.
  4. Revoke the old key by publishing it with an empty p=. Receivers treat a signature from a revoked key as failed.

Gmail’s retired selectors show exactly that last step:

dig +short 20230601._domainkey.gmail.com TXT
dig +short 20221208._domainkey.gmail.com TXT
"v=DKIM1; k=rsa; p="
"v=DKIM1; k=rsa; p="

Hosted providers rotate their own keys when you delegate with CNAMEs (Microsoft 365 alternates between selector1 and selector2). Google Workspace keys are rotated by you, in the Admin console.

What breaks DKIM in real life

CauseWhyFix
Mailing lists adding [list-name] to the Subject or a footer to the bodySigned content changedARC and From rewriting on the list side (lesson 11)
A disclaimer or signature banner added by a transport rule after signingBody changedMake DKIM signing the last step on the way out
Security gateways that rewrite links or add “External” banners on the outbound pathBody changedSame: sign after the gateway, or have the gateway sign
Re-encoding 8-bit to quoted-printable on a hop without 8BITMIMEBody bytes changedSend 7-bit-safe encodings (quoted-printable or base64) to begin with
Wrong or missing DNS record, typo in the key, panel truncated a long TXTNo key, or a broken keyCheck with dig after publishing; test-send to a Gmail account and read Authentication-Results
Key removed too early during rotationDelayed messages failKeep old keys a week after switching

What DKIM doesn’t do

  • It doesn’t encrypt anything; everything stays readable.
  • It doesn’t prove the message is from who the From header says. The signing domain d= can be any domain: a spammer can sign perfectly with d=spammer.example and put your domain in From. Tying d= to From is, again, DMARC’s job.
  • It doesn’t stop replay: someone who receives one validly signed message from your domain can send the exact same message to a million people, and it still verifies. Defenses: don’t sign anything that can be abused when resent, use x= expiry for bulk mail, oversign headers, and rely on receivers’ replay detection.

What the receiver writes

Authentication-Results: mx.google.com;
       dkim=pass header.i=@example.com header.s=lab2026 header.b=QIT6R6Jc;

header.i and header.s identify the identity and selector that verified; header.b gives the first characters of the signature, to tell several signatures apart. A message can carry several DKIM signatures (yours, your ESP’s, a list’s), and each gets its own result.

Signing in practice

  • Hosted providers: enable DKIM for your domain in the admin console (lesson 7).
  • ESPs: publish the CNAMEs or TXT records they give you, so they sign with your domain, not theirs.
  • Your own Postfix: Rspamd’s dkim_signing module, or OpenDKIM as a milter. Store private keys readable only by the signing daemon.
  • Every system that sends as your domain should sign with your domain. DMARC aggregate reports (next lesson) show you which ones don’t.