Sub topic 5
DKIM: signing messages with your domain
DKIM (DomainKeys Identified Mail) lets a sending domain attach a cryptographic signature to each message. Receivers check the signature against a public key published in DNS. This chapter assumes you know what a TXT record is, can read raw email headers, and have read the SPF chapter.
How DKIM works
The sending system holds a private key. It hashes selected headers and the body, then signs the hash. The result goes into a DKIM-Signature header. The matching public key sits in DNS, and the receiver fetches it and verifies the signature.
A signature proves two things: the signing domain approved the message, and the signed parts were not changed in transit. Unlike SPF, DKIM usually survives forwarding, because the signature travels with the message.
The tags that matter
A DKIM-Signature header contains these tags:
| Tag | Meaning |
|---|---|
| `d=` | Signing domain |
| `s=` | Selector, which names the key |
| `h=` | Signed header fields |
| `bh=` | Hash of the body |
| `b=` | The signature itself |
The receiver builds the DNS name from s= and d=: <selector>._domainkey.<domain>. Selectors let you publish several keys at once. Use a separate selector for each sending platform, and for each key rotation.
Publishing the key
Your email service provider usually generates the key pair and gives you the public key to publish.
mail1._domainkey.example.com. TXT "v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A...(public key from your sender)..."Some providers ask for a CNAME to a record they host. That lets them rotate keys without asking you to edit DNS. Use whichever form the provider gives you.
Check the record with dig:
dig +short TXT mail1._domainkey.example.comSigning the right domain
Gmail and Yahoo require bulk senders to authenticate with DKIM, and the signing domain should match your visible From domain. Many providers sign by default with their own domain, such as d=esp.example.net. That signature verifies, but it does not align with [email protected], so it cannot help you pass DMARC. Configure custom (branded) DKIM so that d= is your own domain. The alignment chapter covers the matching rules.
Sign From, To, Subject, Date, MIME-Version, and Content-Type. For one-click unsubscribe, also sign List-Unsubscribe and List-Unsubscribe-Post. Without that, an intermediary could change them without breaking the signature.
Reading the result
The receiver records the outcome in Authentication-Results:
Authentication-Results: mx.example.net;
spf=pass smtp.mailfrom=bounce.example.com;
dkim=pass header.d=example.com header.s=mail1;
dmarc=pass (p=NONE) header.from=example.comHere header.d is the signing domain and header.s is the selector. DKIM passes and matches the From domain, so DMARC passes even though the SPF domain differs.
Common errors
dkim=fail (body hash did not verify): something changed the body after signing. Typical causes are a footer added by a gateway, link rewriting, or a mailing list.dkim=neutralordkim=permerror: the key record is missing, malformed, or published under the wrong selector. Re-run the dig check and look for pasted line breaks or stray spaces.dkim=passbut DMARC fails: thed=domain does not match the From domain.- Old selector removed too early: delete a retired key only after mail signed with it has stopped arriving.
Exercise
A message shows dkim=pass header.d=esp.example.net header.s=s1 and the From address is [email protected]. Does DKIM help this message pass DMARC? What would you change?
Show answer
No. The signature is valid, but the signing domain esp.example.net does not match the From domain example.com. Configure custom DKIM in the sending platform so the message is signed with d=example.com, and publish the public key at the selector it gives you under _domainkey.example.com.