From 519243836c04d2f68d94c60e58c43a19689a1da6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Depierrepont=20aka=20Toorop?= Date: Wed, 6 May 2015 15:09:55 +0200 Subject: [PATCH] bugfix add "b=" --- dkim.go | 19 ++++++++----------- dkimHeader.go | 16 +++++++++++++++- dkim_test.go | 16 ++++++++-------- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/dkim.go b/dkim.go index 12d4b88..3c270aa 100644 --- a/dkim.go +++ b/dkim.go @@ -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 diff --git a/dkimHeader.go b/dkimHeader.go index 750ad4d..5908a7c 100644 --- a/dkimHeader.go +++ b/dkimHeader.go @@ -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:: diff --git a/dkim_test.go b/dkim_test.go index 7ae25af..1a0ce21 100644 --- a/dkim_test.go +++ b/dkim_test.go @@ -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?= " + CRLF + "To: =?UTF-8?Q?St=C3=A9phane_Depierrepont?= " + 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?= " + 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)