diff --git a/dkim.go b/dkim.go index 123c6e0..b0278f0 100644 --- a/dkim.go +++ b/dkim.go @@ -168,6 +168,15 @@ func Sign(email *bytes.Reader, options sigOptions) (*bytes.Reader, error) { h2 = sha256.New() h3 = crypto.SHA256 } + + // if l tag (body length) + if options.BodyLength != 0 { + if uint(len(body)) < options.BodyLength { + return nil, ErrBadDKimTagLBodyTooShort + } + body = body[0:options.BodyLength] + } + h1.Write(body) bodyHash = base64.StdEncoding.EncodeToString(h1.Sum(nil)) diff --git a/dkimHeader.go b/dkimHeader.go index b0d6da7..41613c7 100644 --- a/dkimHeader.go +++ b/dkimHeader.go @@ -199,7 +199,6 @@ func NewDkimHeaderBySigOptions(options sigOptions) *DkimHeader { h.SignatureTimestamp = time.Now() } if options.SignatureExpireIn > 0 { - fmt.Println(options.SignatureExpireIn) h.SignatureExpiration = time.Now().Add(time.Duration(options.SignatureExpireIn) * time.Second) } h.CopiedHeaderFileds = options.CopiedHeaderFileds @@ -253,6 +252,16 @@ func (d *DkimHeader) GetHeaderBase(bodyHash string) string { subh += " x=" + fmt.Sprintf("%d", ts) + ";" } + // body length + if d.BodyLength != 0 { + bodyLengthStr := fmt.Sprintf("%d", d.BodyLength) + if len(subh)+len(bodyLengthStr)+4 > MaxHeaderLineLength { + h += subh + FWS + subh = "" + } + subh += " l=" + bodyLengthStr + ";" + } + // Headers if len(subh)+len(d.Headers)+4 > MaxHeaderLineLength { h += subh + FWS @@ -287,7 +296,6 @@ func (d *DkimHeader) GetHeaderBase(bodyHash string) string { } } h += subh + ";" + FWS + "b=" - return h } diff --git a/dkim_test.go b/dkim_test.go index 3bbdf11..0e89725 100644 --- a/dkim_test.go +++ b/dkim_test.go @@ -174,6 +174,7 @@ func Test_Sign(t *testing.T) { options.Selector = selector options.AddSignatureTimestamp = true options.SignatureExpireIn = 3600 + options.BodyLength = 5 options.Headers = []string{"from"} emailReader, err := Sign(emailReader, options) assert.NoError(t, err) diff --git a/errors.go b/errors.go index c2094c2..0dd2129 100644 --- a/errors.go +++ b/errors.go @@ -28,4 +28,7 @@ var ( // ErrBadMailFormatHeaders ErrBadMailFormatHeaders = errors.New("bad mail format found in headers") + + // ErrBadDKimTagLBodyTooShort + ErrBadDKimTagLBodyTooShort = errors.New("bad tag l or bodyLength option. Body length < l value") )