diff --git a/dkim/dkim.go b/dkim/dkim.go index 7a71c94..134b23d 100644 --- a/dkim/dkim.go +++ b/dkim/dkim.go @@ -9,6 +9,7 @@ import ( "bytes" "crypto" "crypto/ed25519" + "crypto/rand" "crypto/x509" "encoding/pem" "errors" @@ -75,14 +76,19 @@ func NewFromEd25519Key(k []byte, sc *SignerConfig) (*Middleware, error) { // Handle is the handler method that satisfies the mail.Middleware interface func (d Middleware) Handle(m *mail.Msg) *mail.Msg { - ibuf := bytes.Buffer{} - _, err := m.WriteToSkipMiddleware(&ibuf, Type) + // If no boundary is set for the mail.Msg we need to set our own fixed boundary, otherwise + // a new boundary will bet generated after the middleware has been applied and therfore + // the body hash will be altered + // TODO: Add a GetBoundary() method to go-mail, so we don't override a already set boundary + m.SetBoundary(randomBoundary()) + ibuf := bytes.NewBuffer(nil) + _, err := m.WriteToSkipMiddleware(ibuf, Type) if err != nil { return m } var obuf bytes.Buffer - if err := dkim.Sign(&obuf, &ibuf, d.so); err != nil { + if err := dkim.Sign(&obuf, ibuf, d.so); err != nil { return m } br := bufio.NewReader(&obuf) @@ -157,3 +163,13 @@ func extractDKIMHeader(br *bufio.Reader) (string, error) { } return "", nil } + +// randomBoundary generates boundary in case no boundary is set yet +func randomBoundary() string { + var buf [30]byte + _, err := io.ReadFull(rand.Reader, buf[:]) + if err != nil { + panic(err) + } + return fmt.Sprintf("%x", buf[:]) +} diff --git a/go.mod b/go.mod index b830432..1d19b27 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,6 @@ go 1.16 require ( github.com/ProtonMail/gopenpgp/v2 v2.7.5 github.com/emersion/go-msgauth v0.6.8 - github.com/wneessen/go-mail v0.4.1 + github.com/wneessen/go-mail v0.4.2 golang.org/x/text v0.16.0 ) diff --git a/go.sum b/go.sum index 1f531ac..9bddd4e 100644 --- a/go.sum +++ b/go.sum @@ -27,8 +27,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/wneessen/go-mail v0.4.1 h1:m2rSg/sc8FZQCdtrV5M8ymHYOFrC6KJAQAIcgrXvqoo= -github.com/wneessen/go-mail v0.4.1/go.mod h1:zxOlafWCP/r6FEhAaRgH4IC1vg2YXxO0Nar9u0IScZ8= +github.com/wneessen/go-mail v0.4.2 h1:wISuU9LOGqrA7pxy7OipRtwoExXTzuGKmAjb8gYwc00= +github.com/wneessen/go-mail v0.4.2/go.mod h1:zxOlafWCP/r6FEhAaRgH4IC1vg2YXxO0Nar9u0IScZ8= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=