emersion / go-msgauth

🔏 A Go library and tools for DKIM, DMARC and Authentication-Results
MIT License
162 stars 51 forks source link

DKIM Verify issue in relaxed mode #38

Closed mschneider82 closed 3 years ago

mschneider82 commented 3 years ago

I have a verify issue with mails coming from gmail with attachments, i have cheked the mail with opendkim and other dkim libararys they Passed, but with go-msgauth i get body hash missmatch.

I have debugged it. Sometimes it happens that in the relaxedBodyCanonicalizer produces a \r\r\n which is written to the writer:

00000000: 6b48 4f53 636b 6748 3037 5662 4b4d 6557  kHOSckgH07VbKMeW
00000010: 424a 4841 2b59 6a41 4234 7750 3839 616e  BJHA+YjAB4wP89an
00000020: 4344 4751 7135 4743 5431 344a 347a 3655  CDGQq5GCT14J4z6U
00000030: 7853 356c 6370 7648 7642 3373 6f5a 6944  xS5lcpvHvB3soZiD
00000040: 792f 4848 6334 2b76 5370 5869 0d0d 0a    y/HHc4+vSpXi...
                                        ^--- \r\r \n

When i do a hotfix at the end of the Write() function:

func (c *relaxedBodyCanonicalizer) Write(b []byte) (int, error) {
...
    if bytes.ContainsAny(canonical, "\r\r\n") {
        canonical = bytes.ReplaceAll(canonical, []byte("\r\r\n"), []byte("\r\n"))
    }
    _, err := c.w.Write(canonical)
    return written, err
}

the checksum is Valid! i think the fixCRLF() should be different in relaxed Mode I have attached a PR to fix this issue