Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DKIM signing for multipart messages #45

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions dkim/dkim.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"crypto"
"crypto/ed25519"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"errors"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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[:])
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
Loading