skip to content

Message Signing

The signer lists headers in h=, hashes the body into bh=, and signs with rsa-sha256 or ed25519-sha256. Interviewers ask which headers to sign and why over-signing From blocks a forged copy.

on this pageshow

explore

questions

6

When a sending mail server DKIM-signs a message, what exactly does it hash, what does it sign, and where does each result go?

level: juniorimportance: must knowfreq 48%

answer

  1. two hashes, not one
  2. body digest lands in bh=
  3. headers named in h=, in order
  4. the signature field itself, b= blanked

basics

~20 s

A DKIM signer hashes the body into bh=, then hashes the header fields listed in h= plus its own DKIM-Signature field with b= left empty, signs that hash with the domain's private key, and writes the result into b=.

solid answer

~40 s

DKIM signing is two hashes. First the body, after the canonicalization named in `c=`, is hashed with the hash in `a=` and the base64 digest goes into `bh=`. Second, the signer feeds the header fields named in `h=`, in that order, followed by the `DKIM-Signature` field it is building with the `b=` value treated as empty, so `bh=`, `d=`, `s=` and every other tag are covered too. That header hash is signed with the private key whose public half is published under selector `s=` at domain `d=`, and the result goes into `b=`. The field is then normally prepended to the message. Header fields not listed in `h=`, the SMTP envelope, and fields added after signing are not covered.

code

pseudocode · 14 lines
pseudocode
# DKIM signing, after RFC 6376 Section 3.7
body      = canonicalize_body(message.body, c_body)
bh        = base64(hash(a_hash, body))          # written to bh=

sig_field = "DKIM-Signature: v=1; a=rsa-sha256; d=example.com; "
          + "s=s2026a; h=from:to:subject:date; bh=" + bh + "; b="

data = ""
for name in h_list:                             # order of h=, not of the message
    data += canonicalize_header(select(message, name)) + CRLF
data += canonicalize_header(sig_field)          # b= empty, no trailing CRLF

b = base64(sign(private_key_for("s2026a", "example.com"), hash(a_hash, data)))
prepend(message, sig_field + b)

go deeper

for a junior

Recall the two tags that carry results: bh= holds the body hash, b= holds the signature over the listed headers and the signature field itself.

for a middle

Walk through the hash order: body into bh=, then h= fields in h= order, then the DKIM-Signature field with b= blanked, and say why that blanking is needed.

for a senior

Use the coverage boundary to explain real breakage: which in-transit edits leave the signature intact, which break it, and why encoding happens before hashing but dot-stuffing after.

for a principal

Frame coverage as a design choice: what the organisation wants a passing signature to vouch for, and how the h= list encodes that promise to receivers.

## Two hashes, one signature A DKIM signer (RFC 6376, September 2011, Section 3.7) computes **two hashes** and signs only one of them directly: 1. **The body hash.** The message body is canonicalized with the body algorithm named in `c=` (the normalisation rules are covered with DKIM canonicalization), hashed with the hash function the `a=` tag names (SHA-256 for both `rsa-sha256` and `ed25519-sha256`), base64-encoded and written into the `bh=` tag. 2. **The header hash.** The signer then hashes, in this order, the header fields listed in `h=` (in the order `h=` gives them, each canonicalized and ending in CRLF), and finally the `DKIM-Signature` field it is creating, with the value of `b=` treated as an empty string and no trailing CRLF. 3. **The signature.** That header hash is signed with the private key that matches the selector `s=` under the signing domain `d=`, and the base64 result becomes the value of `b=`. Because the body hash sits inside the `DKIM-Signature` field as `bh=`, and that field is part of the header hash, the one signature in `b=` ends up protecting the body too. The RFC's own pseudocode reads `body-hash = hash-alg(canon-body, l-param)`, then `data-hash = hash-alg(h-headers, D-SIG, body-hash)`, then `signature = sig-alg(d-domain, selector, data-hash)`. ## What the DKIM-Signature field contributes The field being created is **always** included in the header hash, after the listed headers, but it is **never** named in its own `h=`: RFC 6376 says `h=` MUST NOT include the `DKIM-Signature` field being created. Including it means that every tag is protected: - `d=` and `s=` cannot be swapped to point a verifier at a different key without breaking `b=`. - `h=` cannot be shortened to drop a header from coverage. - `bh=` cannot be replaced to match a rewritten body. - `t=` and `x=`, when present, cannot be moved. - Tags a verifier does not recognise are still hashed; RFC 6376 says they MUST be included in the calculation and otherwise ignored. Only the value of `b=` is blanked, because it cannot contain itself. ## Which bytes, exactly RFC 6376 pins down the byte stream so that signer and verifier hash the same thing: | Step | Rule | |---|---| | Content transfer encoding | Hash **after** base64 or quoted-printable encoding of the body | | SMTP dot-stuffing | Hash **before** it; dot-stuffing is a transport detail | | Attachments | MIME parts are part of the body and MUST be included | | Body length | The whole body, unless the `l=` tag limits it (covered with canonicalization) | | Header order | The order of names in `h=`, not the order in the message | If a header field named in `h=` does not exist in the message, it contributes nothing to the hash; that rule is what lets a signer assert that a field was absent. ## What the signature does not cover A DKIM signature protects exactly the bytes above and nothing else: - **Unlisted header fields.** A relay can add or change `Received`, or any field missing from `h=`, and the signature still verifies. - **The SMTP envelope.** `MAIL FROM` and `RCPT TO` are never hashed; SPF is the check that looks at the envelope. - **Fields added after signing.** They were not part of what was signed, and an extra copy of a signed field placed above the original can go unnoticed unless the signer listed its name once more than it occurred, the over-signing technique. - **The meaning of the domain.** A pass says the holder of the key for `d=` signed these bytes; whether `d=` matches the visible From domain is a separate DMARC alignment question. ## Where the field goes RFC 6376 says the `DKIM-Signature` field SHOULD be treated like a trace field and SHOULD be prepended to the message, and it MUST be inserted before any existing `DKIM-Signature` fields. A message that passes through several signers therefore normally carries its signatures newest first, each one independent, although RFC 6376 warns that some intermediaries reorder them. ## Why interviewers ask Candidates who say DKIM "signs the whole message" or "encrypts the email" cannot explain why a relay can add `Received` lines freely while editing the `Subject` breaks the signature. Knowing the two-hash structure answers both: the body is covered through `bh=`, the headers only as far as `h=` lists them.

  • If h= can never name the DKIM-Signature field being created, how is that field protected?
    It is hashed implicitly, after all the fields listed in `h=`, with the value of `b=` treated as empty and no trailing CRLF. So every tag in it, including `d=`, `s=`, `h=` and `bh=`, is covered by the signature in `b=`; changing any of them breaks verification.
  • May a DKIM signer list a header name in h= that the message does not contain?
    Yes. RFC 6376 allows it, and a missing field is treated as a null input, contributing nothing to the hash. If a field of that name is added later, the verifier now finds something to hash and the signature fails. That is how a signer asserts a field was absent.
  • Why does a relay that rewrites Received lines not break a typical DKIM signature?
    Because `Received` is normally left out of `h=`; RFC 6376 lists it among fields to exclude since relays add instances in transit. A field the signer did not list is outside the header hash, so changing or adding it leaves `b=` valid.

A DKIM signature is a wax seal pressed over a bundle of chosen pages plus a cover note listing which pages are in the bundle: it survives being handed from courier to courier, a rewritten sealed page breaks it, and a page that was never on the list was never protected.

  • DKIM signs every header field of the message automatically.
  • bh= is the signature itself and b= is only a checksum.
  • The DKIM signature also covers the MAIL FROM envelope address.
  • The signer encrypts the message with its private key.
  • The DKIM-Signature field is left out of its own signature.
open as a page

Which tags must every DKIM-Signature header field carry under RFC 6376, and which header field must its h= list always include?

level: middleimportance: must knowfreq 38%

basics

~10 s

RFC 6376 requires v=1, a=, b=, bh=, d=, h= and s= on every DKIM-Signature, and h= must list From. A verifier returns PERMFAIL for a signature missing a required tag or leaving From unsigned.

open as a page

Which a= algorithms may a DKIM signer use today, and why is rsa-sha1 no longer acceptable for signing or for verifying?

level: middleimportance: should knowfreq 30%

basics

~10 s

DKIM signers use rsa-sha256, which RFC 8301 (January 2018) made mandatory, and may add ed25519-sha256 from RFC 8463 (September 2018). RFC 8301 made rsa-sha1 historic: it MUST NOT be used for signing or verifying.

open as a page

Why do DKIM signers list From twice in h=, as in h=from:from:..., when the message carries only one From header field?

level: seniorimportance: should knowfreq 24%

basics

~20 s

Listing From one more time than it occurs makes the extra h= entry hash an absent field as nothing; if another From is added later, that entry now picks it up, the header hash changes and the DKIM signature fails.

open as a page

Your team is adding DKIM signing to a self-hosted MTA for transactional mail; how do you decide which header fields go in h= and which to over-sign?

level: principalimportance: should knowfreq 16%

basics

~20 s

Sign From (mandatory) and every field a reader sees or acts on, such as Subject, Date, To, Reply-To and MIME fields; omit fields relays add or change, like Received and Return-Path; over-sign fields nothing on your paths adds.

open as a page

Your outbound DKIM signer sets x= one hour after t= and puts each sender's full address in i=; what do those tags assert, and what can go wrong?

level: seniorimportance: nice to knowfreq 12%

basics

~20 s

DKIM's t= is the signing time, x= an absolute expiry above t=, and i= the user d= signs for, within d= or a subdomain. A one-hour x= can expire legitimate mail delayed in retry queues.

open as a page