How does a receiver verify a DKIM-Signature header field, in order, from parsing its tags to a pass result?
answer
- cheap checks before DNS
- selector under _domainkey of d=
- body hash before header signature
- b= value emptied, bh= signed
basics
~20 sThe verifier validates the DKIM-Signature tags, fetches the public key from the TXT record at s=._domainkey.d=, recomputes the body hash and compares it with bh=, then checks the b= signature over the h= header fields and the signature field itself.
solid answer
~50 sRFC 6376 section 6 requires a result equivalent to these steps in order. First, **validate the field**: `v=1`, every required tag present (`v`, `a`, `b`, `bh`, `d`, `h`, `s`), `From` listed in `h=`, any `i=` inside `d=`; a verifier MAY also drop a signature whose `x=` time has passed. A problem returns PERMFAIL. Second, **fetch the key**: a TXT query for `s2026a._domainkey.example.com`. No answer may give TEMPFAIL; no record gives PERMFAIL, as do a malformed record, an empty `p=` (revoked) or a key that does not suit `a=`. Third, normalise the body as `c=` says, hash it and compare with `bh=`. Fourth, hash the `h=` header fields in the listed order plus the `DKIM-Signature` field with its `b=` value emptied, and verify `b=` with the key. Because `bh=` sits inside that signed field, `b=` covers the body too.
code
pseudocode · 14 linesverify(sig, msg):
if not valid_tags(sig) or "from" not in sig.h: return PERMFAIL
if sig.x and verification_time > sig.x: return PERMFAIL # optional (MAY)
if sig.a == "rsa-sha1": return PERMFAIL # RFC 8301
rec = dns_txt(sig.s + "._domainkey." + sig.d)
if rec == NO_RESPONSE: return TEMPFAIL
if rec == NO_RECORD: return PERMFAIL
if malformed(rec) or rec.p == "": return PERMFAIL # empty p = revoked
if not key_fits(rec, sig.a) or rsa_bits(rec) < 1024: return PERMFAIL
body = canon_body(msg.body, sig.c, sig.l)
if base64(hash(sig.a, body)) != sig.bh: return PERMFAIL
hdrs = canon_headers(msg, sig.h, sig.c) + canon(sig_field_with_empty_b(sig))
if not check_signature(rec.p, sig.a, hdrs, sig.b): return PERMFAIL
return SUCCESS(sig.d)go deeper
Recall the four moves: check the tags, fetch the key at the selector name under _domainkey of the d= domain, compare the body hash, then verify the signature.
Explain what goes into each hash, the h= header fields in listed order plus the signature field with b= emptied, and why a signed bh= ties the body to b=.
Map each failure point to PERMFAIL or TEMPFAIL, and use the verifier's logged reason to tell a body edit from a header edit or a key problem.
Weigh what a receiver enforces beyond the minimum: honouring x=, refusing signatures that skip essential header fields, and capping how many signatures one message may cost.
## The rule: semantically in order A **DKIM verifier** is the receiving system that checks a `DKIM-Signature` header field. RFC 6376 (September 2011) section 6 says verifiers MUST produce a result semantically equivalent to running its steps in order. An implementation may run some steps in parallel, starting the DNS query while it hashes the body, for instance, but the verdict must be the one the ordered procedure would give. Each step can end the evaluation of that signature with **PERMFAIL** (permanent) or **TEMPFAIL** (transient); a signature that clears every step is **SUCCESS**. ## Step 1: validate the header field The verifier parses the tag list and checks it strictly; RFC 6376 calls being 'liberal in what you accept' a bad strategy in this security context. It returns PERMFAIL when: - `v=` is not `1` (incompatible version); - a required tag is missing; the required tags are `v`, `a`, `b`, `bh`, `d`, `h` and `s`; - `h=` does not include `From`; - `i=` is present and its domain is neither the `d=` domain nor a subdomain of it; - any value is malformed. Unknown tags are explicitly permitted and do not invalidate the field. Some checks are optional: a verifier MAY return PERMFAIL when the signature's `x=` expiry time has passed, MAY ignore signatures whose `d=` is not a plausible signing entity (a bare `com`, say), and MAY refuse signatures that leave out header fields it considers essential. ## Step 2: fetch and validate the key Only a fully parsed signature earns a DNS query. With `q=` defaulting to `dns/txt`, the verifier asks for the TXT record at the selector, the `_domainkey` label and the `d=` domain: for `s=s2026a` and `d=example.com`, the name `s2026a._domainkey.example.com`. Then: 1. If the query gets **no response**, the verifier MAY return TEMPFAIL (key unavailable) and try later. 2. If the record **does not exist**, it MUST return PERMFAIL (no key for signature). 3. If the record is **malformed**, or carries a `v=` the verifier does not implement, PERMFAIL (key syntax error). 4. If the record's `h=` lists acceptable hashes and the one implied by `a=` is not among them, PERMFAIL. 5. If `p=` is **empty**, the key has been revoked: PERMFAIL (key revoked). 6. If the key does not suit the algorithm and key type in `a=` and `k=`, PERMFAIL. Since RFC 8301 (January 2018), `rsa-sha1` signatures and RSA keys shorter than 1024 bits are no longer acceptable at all. What each key-record tag means is covered with DKIM key records. ## Step 3: the body hash The verifier normalises the body with the body half of `c=`, truncates it to `l=` octets if that tag is present, hashes it with the algorithm in `a=`, base64-encodes the result and compares it with `bh=`. A mismatch is PERMFAIL (body hash did not verify): the body is not what the signer hashed. ## Step 4: the header signature The verifier then hashes, in this order: 1. the header fields named in `h=`, in the order listed, each normalised by the header half of `c=`, with names matched case-insensitively; 2. the `DKIM-Signature` field itself, with the **value of `b=` deleted** and no trailing CRLF. It verifies the `b=` signature against that hash with the public key. If it does not validate, PERMFAIL (signature did not verify). Otherwise the signature has verified, and DKIM reports SUCCESS together with the `d=` domain. The design point to remember: every tag of the `DKIM-Signature` field, **including `bh=`**, goes into the header hash. So the signature in `b=` binds the body hash, and through it the body. Matching `bh=` alone proves nothing, because anyone can compute a body hash, and a body-hash mismatch fails the whole signature even when an implementation checks `b=` first. ## Where each failure lands | Failure point | RFC 6376 status | |---|---| | Missing required tag, `From` not in `h=` | PERMFAIL | | DNS query gets no response | TEMPFAIL | | No key record | PERMFAIL (no key for signature) | | Empty `p=` | PERMFAIL (key revoked) | | Body hash differs from `bh=` | PERMFAIL | | `b=` does not verify | PERMFAIL | A PERMFAIL or TEMPFAIL ends **that signature** only. The verifier SHOULD move on to the next `DKIM-Signature` field, if there is one, and a signature that verifies gives the message a valid signature for its `d=` domain. ## Why the order matters in practice - Cheap syntax checks run before DNS, so malformed signatures cost no lookups. - Key problems are reported as key problems: a missing record is PERMFAIL (no key for signature), never mistaken for a hash mismatch. - The logged reason tells an operator where to look. A body-hash failure points at something that edited the body; a `b=` failure with a matching body hash points at a changed signed header field or a key that does not match the signer's.
- Why does the verifier delete the b= value before hashing the DKIM-Signature field?`b=` holds the signature being checked, so it cannot be part of its own input. RFC 6376 treats the `b=` value as the empty string and hashes every other tag of the field, unknown ones included. That way `d=`, `s=`, `h=` and `bh=` cannot be altered without breaking `b=`.
- May a DKIM verifier check b= before it checks bh=?Yes. RFC 6376 allows any order or parallelism as long as the result is semantically the same, and its implementer's note says a verifier may check the header signature first. But if the body hash does not match, the entire signature must still be considered failed: a valid `b=` over a stale `bh=` proves nothing about this body.
- What does a DKIM verifier do with an x= time that has passed?RFC 6376 allows, but does not require, it to ignore the signature and return PERMFAIL (signature expired). The verification time should be when the message first reached the verifier's administrative domain, if that is reliably known, so mail delayed in the receiver's own queues is not penalised. `x=` MUST be greater than `t=` when both are present.
- Matching bh= alone proves the signing domain sent this body.
- The key is looked up under _domainkey of the From domain.
- A missing key record is a temporary error worth retrying.
- Unknown tags in DKIM-Signature make the signature invalid.
- Header fields are hashed in message order, not in h= order.