add body length tag

This commit is contained in:
Stéphane Depierrepont aka Toorop
2015-05-06 15:41:32 +02:00
parent 6b2f38361d
commit d33d1a64e8
4 changed files with 23 additions and 2 deletions

View File

@@ -168,6 +168,15 @@ func Sign(email *bytes.Reader, options sigOptions) (*bytes.Reader, error) {
h2 = sha256.New() h2 = sha256.New()
h3 = crypto.SHA256 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) h1.Write(body)
bodyHash = base64.StdEncoding.EncodeToString(h1.Sum(nil)) bodyHash = base64.StdEncoding.EncodeToString(h1.Sum(nil))

View File

@@ -199,7 +199,6 @@ func NewDkimHeaderBySigOptions(options sigOptions) *DkimHeader {
h.SignatureTimestamp = time.Now() h.SignatureTimestamp = time.Now()
} }
if options.SignatureExpireIn > 0 { if options.SignatureExpireIn > 0 {
fmt.Println(options.SignatureExpireIn)
h.SignatureExpiration = time.Now().Add(time.Duration(options.SignatureExpireIn) * time.Second) h.SignatureExpiration = time.Now().Add(time.Duration(options.SignatureExpireIn) * time.Second)
} }
h.CopiedHeaderFileds = options.CopiedHeaderFileds h.CopiedHeaderFileds = options.CopiedHeaderFileds
@@ -253,6 +252,16 @@ func (d *DkimHeader) GetHeaderBase(bodyHash string) string {
subh += " x=" + fmt.Sprintf("%d", ts) + ";" 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 // Headers
if len(subh)+len(d.Headers)+4 > MaxHeaderLineLength { if len(subh)+len(d.Headers)+4 > MaxHeaderLineLength {
h += subh + FWS h += subh + FWS
@@ -287,7 +296,6 @@ func (d *DkimHeader) GetHeaderBase(bodyHash string) string {
} }
} }
h += subh + ";" + FWS + "b=" h += subh + ";" + FWS + "b="
return h return h
} }

View File

@@ -174,6 +174,7 @@ func Test_Sign(t *testing.T) {
options.Selector = selector options.Selector = selector
options.AddSignatureTimestamp = true options.AddSignatureTimestamp = true
options.SignatureExpireIn = 3600 options.SignatureExpireIn = 3600
options.BodyLength = 5
options.Headers = []string{"from"} options.Headers = []string{"from"}
emailReader, err := Sign(emailReader, options) emailReader, err := Sign(emailReader, options)
assert.NoError(t, err) assert.NoError(t, err)

View File

@@ -28,4 +28,7 @@ var (
// ErrBadMailFormatHeaders // ErrBadMailFormatHeaders
ErrBadMailFormatHeaders = errors.New("bad mail format found in headers") ErrBadMailFormatHeaders = errors.New("bad mail format found in headers")
// ErrBadDKimTagLBodyTooShort
ErrBadDKimTagLBodyTooShort = errors.New("bad tag l or bodyLength option. Body length < l value")
) )