add body length tag
This commit is contained in:
9
dkim.go
9
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))
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user