bugfix add "b="

This commit is contained in:
Stéphane Depierrepont aka Toorop
2015-05-06 15:09:55 +02:00
parent c27b209a79
commit 519243836c
3 changed files with 31 additions and 20 deletions

19
dkim.go
View File

@@ -11,6 +11,7 @@ import (
"crypto/x509"
"encoding/base64"
"encoding/pem"
//"fmt"
"hash"
"io/ioutil"
"regexp"
@@ -20,7 +21,7 @@ import (
const (
CRLF = "\r\n"
TAB = "\t"
TAB = " "
FWS = CRLF + TAB
MaxHeaderLineLength = 70
)
@@ -167,7 +168,8 @@ func Sign(email *bytes.Reader, options sigOptions) (*bytes.Reader, error) {
h2 = sha256.New()
h3 = crypto.SHA256
}
bodyHash = base64.StdEncoding.EncodeToString(h1.Sum(body))
h1.Write(body)
bodyHash = base64.StdEncoding.EncodeToString(h1.Sum(nil))
// Get dkim header base
dkimHeader := NewDkimHeaderBySigOptions(options)
@@ -179,6 +181,7 @@ func Sign(email *bytes.Reader, options sigOptions) (*bytes.Reader, error) {
return nil, err
}
headers = append(headers, []byte(dHeaderCanonicalized)...)
headers = bytes.TrimRight(headers, " \r\n")
// sign
h2.Write(headers)
@@ -189,9 +192,7 @@ func Sign(email *bytes.Reader, options sigOptions) (*bytes.Reader, error) {
sig64 := base64.StdEncoding.EncodeToString(sig)
// add to DKIM-Header
dHeader += ";" + FWS
subh := "b="
subh := ""
l := len(subh)
for _, c := range sig64 {
subh += string(c)
@@ -227,6 +228,8 @@ func canonicalize(emailReader *bytes.Reader, options sigOptions) (headers, body
return
}
//fmt.Println(email)
// todo \n -> \r\n
parts := bytes.SplitN(email, []byte{13, 10, 13, 10}, 2)
if len(parts) != 2 {
@@ -326,12 +329,6 @@ func canonicalize(emailReader *bytes.Reader, options sigOptions) (headers, body
}
}
return
/*println(string(parts[0]))
println("\r\n")
println(string(parts[1]))
println(string(body))*/
return
}
// canonicalizeHeader returns canonicalized version of header

View File

@@ -216,6 +216,18 @@ func (d *DkimHeader) GetHeaderBase(bodyHash string) string {
}
subh += " d=" + d.Domain + ";"
// Auid
if len(d.Auid) != 0 {
if len(subh)+len(d.Auid)+4 > MaxHeaderLineLength {
h += subh + FWS
subh = ""
}
subh += " i=" + d.Auid + ";"
}
/*h := "DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tmail.io; i=@tmail.io;" + FWS
subh := "q=dns/txt; s=test;"*/
// signature timestamp
if !d.SignatureTimestamp.IsZero() {
ts := d.SignatureTimestamp.Unix()
@@ -274,7 +286,9 @@ func (d *DkimHeader) GetHeaderBase(bodyHash string) string {
l = 0
}
}
h += subh
h += subh + ";" + FWS + "b="
return h
}
//'test._domainkey.tmail.io:v=DKIM1;k=rsa;s=email;h=sha256;t=y;p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNUXO+Qsl1tw+GjrqFajz0ERSEUs1FHSL/+udZRWn1Atw8gz0+tcGqhWChBDeU9gY5sKLEAZnX3FjC/T/IbqeiSM68kS5vLkzRI84eiJrm3+IieUqIIicsO+WYxQs+JgVx5XhpPjX4SQjHtwEC2xKkWnEv+VPgO1JWdooURcSC6QIDAQAB':300::

View File

@@ -47,11 +47,11 @@ var email = "Received: (qmail 28277 invoked from network); 1 May 2015 09:43:37 -
"From: =?UTF-8?Q?St=C3=A9phane_Depierrepont?= <toorop@tmail.io>" + CRLF +
"To: =?UTF-8?Q?St=C3=A9phane_Depierrepont?= <toorop@toorop.fr>" + CRLF +
"Content-Type: text/plain; charset=UTF-8" + CRLF + CRLF +
"Hello world" + CRLF +
"line with trailing space " + CRLF +
"line with space " + CRLF +
"-- " + CRLF +
"Toorop " + CRLF + CRLF + CRLF + CRLF + CRLF + CRLF
"Hello world" + CRLF //+
//"line with trailing space " + CRLF +
//"line with space " + CRLF +
//"-- " + CRLF +
//"Toorop" // + CRLF + CRLF + CRLF + CRLF + CRLF + CRLF
var headerSimple = "From: =?UTF-8?Q?St=C3=A9phane_Depierrepont?= <toorop@tmail.io>" + CRLF +
"Date: Fri, 1 May 2015 11:48:37 +0200" + CRLF +
@@ -172,9 +172,9 @@ func Test_Sign(t *testing.T) {
options.Canonicalization = "relaxed/relaxed"
options.Domain = domain
options.Selector = selector
options.AddSignatureTimestamp = true
options.SignatureExpireIn = 3600
options.Headers = []string{"from", "date", "mime-version", "received", "received", "In-Reply-To"}
//options.AddSignatureTimestamp = true
//options.SignatureExpireIn = 3600
options.Headers = []string{"from"}
emailReader, err := Sign(emailReader, options)
assert.NoError(t, err)
raw, _ := ioutil.ReadAll(emailReader)