Why must a DKIM signer hash CRLF line endings and the final transfer encoding, even when it signs with c=relaxed/relaxed?
answer
- network normal form assumed
- bare LF is local form
- sign what the verifier receives
- hash after quoted-printable or base64
- before dot-stuffing
basics
~20 sDKIM canonicalization assumes the message is already in network form: CRLF line endings and the transfer encoding it travels in. Relaxed neither converts bare LF nor undoes re-encoding, so a signer hashing a local form produces hashes verifiers cannot reproduce.
solid answer
~40 sRFC 6376 section 3.4 assumes the message is in "network normal" format, with lines separated by CRLF, before either algorithm runs. Relaxed only normalises whitespace and header folding; it does not turn bare LF into CRLF. Section 5.3 therefore says bare CR or LF used as a local line convention MUST be converted to CRLF before signing, and the signer MUST sign the message as the verifier is expected to receive it. Transfer encoding is the same story: section 3.7 hashes the body after quoted-printable or base64 encoding, so a relay converting 8-bit content to 7-bit changes the hashed bytes. Section 5.3 says signers SHOULD convert to a 7-bit MIME encoding before signing. SMTP dot-stuffing, by contrast, is transport-level and excluded from the hash.
go deeper
Recall that DKIM assumes CRLF line endings and hashes the encoded body, and that relaxed only deals with whitespace and folding.
Explain section 5.3's MUST on converting bare LF to CRLF before signing, and why the hash is taken after quoted-printable or base64 but before dot-stuffing.
When every receiver reports body-hash failures, even on direct delivery, inspect the signer's line endings and pre-signing encoding before blaming intermediaries.
Require that signing happens on the final wire form at one point in the outbound path, so no later component re-encodes or reformats signed mail.
## What canonicalization assumes before it starts Both DKIM canonicalization algorithms in RFC 6376 (September 2011) operate on a message that is already in what section 3.4 calls **"network normal" format**: ASCII text, lines separated by CRLF. They were designed to absorb the cosmetic changes relays make, such as whitespace and header folding, not to translate between a host's storage format and the wire format. That gap is where signers that look correctly configured still produce signatures that fail everywhere. ## Line endings Many systems store mail with a bare LF at the end of each line and add the CR only when the message goes onto the wire. If the signing component hashes the stored form, its body hash is computed over `line<LF>` while every verifier sees `line<CRLF>`. No `c=` value bridges that difference: - `simple` hashes the line exactly as it is. - `relaxed` reduces spaces and tabs, but CR and LF are not whitespace characters it touches; it explicitly keeps the CRLF at the end of each line. RFC 6376 section 5.3 closes the gap at the signer: bare CR or LF characters used as a local line separator **MUST** be converted to the SMTP-standard CRLF sequence before the message is signed, and that conversion SHOULD be applied to the message actually sent, not just to the copy presented to the signing algorithm. The general rule in the same section is that the signer MUST sign the message **as it is expected to be received by the verifier**, not in some local or internal form. Whether a receiving server accepts mail with non-standard line endings at all is a separate question about the SMTP dialog and the end of data, covered with SMTP. ## Transfer encoding DKIM treats the body as a string of octets; it has no notion of decoded content. Section 3.7 is specific: 1. When the message will be transmitted using **base64 or quoted-printable**, the signer MUST compute the hash **after** the encoding, and the verifier MUST hash the encoded text before decoding it. 2. The hash MUST be computed **before transport-level encodings** such as SMTP dot-stuffing, the doubling of a leading `.` that keeps a line from being read as the end of the message. The first rule means a relay that re-encodes a body changes the hash input. Section 5.3 names the common case: messages with 8-bit characters can be converted to 7-bit form in transit, and such conversions will break DKIM signatures. The remedy it gives is that signers SHOULD convert the message to a suitable MIME content-transfer encoding such as quoted-printable or base64 before signing, and that the conversion SHOULD be done by the mail user agent or the submission server before the message reaches the DKIM algorithm. The second rule is why dot-stuffing is harmless: the sending server adds it and the receiving server removes it as part of SMTP, so neither end hashes it. ## How the pieces line up | Change between signer and verifier | Absorbed by `relaxed`? | Where it is handled | |---|---|---| | Spaces or tabs adjusted, headers re-folded | yes | canonicalization | | Bare LF on the signer, CRLF on the wire | no | signer converts to CRLF first (section 5.3, MUST) | | 8-bit body converted to 7-bit by a relay | no | signer encodes to quoted-printable or base64 first (SHOULD) | | Body re-encoded from one transfer encoding to another | no | avoid re-encoding relays; nothing in DKIM undoes it | | SMTP dot-stuffing | not needed | excluded from the hash (section 3.7) | ## Diagnosing it on a domain you run - **Every** receiver reports failures, typically on the body hash, including for mail that went straight from your server to the receiver: suspect the signer's line endings before suspecting any intermediary. - Failures appear only for messages containing 8-bit characters: check whether the body is encoded before signing and whether a relay on the path downgrades it. - Compare the signed message as your outbound server transmits it with the copy the receiver stored; differences in line endings or `Content-Transfer-Encoding` show which case you have. The fix in each case is on the signing side, ahead of canonicalization. Changing `c=` from `simple` to `relaxed` will not help, because neither algorithm was ever meant to normalise line endings or encodings.
- Where in the sending path should the 7-bit conversion for DKIM happen?Before signing. RFC 6376 section 5.3 says the message SHOULD be converted to 7-bit MIME by the mail user agent or the submission server before it is presented to the DKIM algorithm, and the converted form should be what is actually sent, so later relays have nothing to downgrade.
- Why does SMTP dot-stuffing not break DKIM signatures?RFC 6376 section 3.7 requires the hash to be computed before transport-level encodings such as dot-stuffing. The sending server adds the extra dot during the SMTP transfer and the receiving server removes it, so neither the signer nor the verifier ever hashes the stuffed form.
- c=relaxed/relaxed normalises LF and CRLF, so line format never matters.
- DKIM hashes the decoded body, so re-encoding quoted-printable as base64 is harmless.
- The DKIM body hash includes the dots added by SMTP dot-stuffing.
- Relaxed body canonicalization absorbs an 8-bit to 7-bit conversion by a relay.
- Signing the stored copy is fine as long as the content is identical.