Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve folding signature algorithm #29

Merged
merged 5 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions dkim/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,37 +92,58 @@ func parseHeaderParams(s string) (map[string]string, error) {
return params, nil
}

func formatHeaderParams(params map[string]string) string {
keys := make([]string, 0, len(params))
found := false
for k := range params {
if k == "b" {
found = true
} else {
keys = append(keys, k)
}
}
sort.Strings(keys)
if found {
keys = append(keys, "b")
}
func formatHeaderParams(headerFieldName string, params map[string]string) string {
keys, bvalue, found := sortParams(params)

var s string
var line string
first := true

for _, k := range keys {
v := params[k]

if first {
first = false
line += fmt.Sprintf("%v: %v=%v;", headerFieldName, k, v)
continue
} else {
s += " "
nextLength := 3 + len(line) + len(v) + len(k)
if nextLength > 75 {
s += line + crlf
line = ""
}
line = fmt.Sprintf("%v %v=%v;", line, k, v)
}
}

s += k + "=" + v + ";"
if line != "" {
s += line
}

if found {
bfiled := foldHeaderField(" b=" + bvalue)
s += crlf + bfiled
}

return s
}

func sortParams(params map[string]string) ([]string, string, bool) {
keys := make([]string, 0, len(params))
found := false
var bvalue string
for k := range params {
if k == "b" {
bvalue = params["b"]
found = true
} else {
keys = append(keys, k)
}
}
sort.Strings(keys)
return keys, bvalue, found
}

type headerPicker struct {
h header
picked map[string]int
Expand Down
38 changes: 36 additions & 2 deletions dkim/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,48 @@ func TestFormatHeaderParams(t *testing.T) {
"d": "example.org",
}

expected := "a=rsa-sha256; d=example.org; v=1;"
expected := "DKIM-Signature: a=rsa-sha256; d=example.org; v=1;"

s := formatHeaderParams(params)
s := formatHeaderParams("DKIM-Signature", params)
if s != expected {
t.Errorf("Expected formatted params to be %q, but got %q", expected, s)
}
}

func TestLongHeaderFolding(t *testing.T) {
// see #29 and #27

params := map[string]string{
"v": "1",
"a": "rsa-sha256",
"d": "example.org",
"h": "From:To:Subject:Date:Message-ID:Long-Header-Name",
}

expected := "DKIM-Signature: a=rsa-sha256; d=example.org;\r\n h=From:To:Subject:Date:Message-ID:Long-Header-Name; v=1;"

s := formatHeaderParams("DKIM-Signature", params)
if s != expected {
t.Errorf("Expected formatted params to be\n\n %q\n\n, but got\n\n %q", expected, s)
}
}

func TestSignedHeaderFolding(t *testing.T) {
hValue := "From:To:Subject:Date:Message-ID:Long-Header-Name:Another-Long-Header-Name"

params := map[string]string{
"v": "1",
"a": "rsa-sha256",
"d": "example.org",
"h": hValue,
}

s := formatHeaderParams("DKIM-Signature", params)
if !strings.Contains(s, hValue) {
t.Errorf("Signed Headers names (%v) are not well folded in the signed header %q", hValue, s)
}
}

func TestParseHeaderParams_malformed(t *testing.T) {
_, err := parseHeaderParams("abc; def")
if err == nil {
Expand Down
4 changes: 2 additions & 2 deletions dkim/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ func Sign(w io.Writer, r io.Reader, options *SignOptions) error {
}

func formatSignature(params map[string]string) string {
sig := headerFieldName + ": " + formatHeaderParams(params)
return foldHeaderField(sig)
sig := formatHeaderParams(headerFieldName, params)
return sig
}

func formatTagList(l []string) string {
Expand Down
10 changes: 6 additions & 4 deletions dkim/sign_ed25519_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"testing"
)

const signedEd25519MailString = "DKIM-Signature: a=ed25519-sha256; bh=2jUSOH9NhtVGCQWNr9BrIAPreKQjO6Sn7XIkfJ" + "\r\n" +
" " + "VOzv8=; c=simple/simple; d=football.example.com; h=From:To:Subject:Date:Mes" + "\r\n" +
" " + "sage-ID; s=brisbane; t=424242; v=1; b=ZduPZq83AOTqjhScIfHll6W90tMG1nf34a34Q" + "\r\n" +
" " + "XKat3iFtP7NQE/3AwnHOrcsR2r5nVNoW+LeZURpT2obCthPCw==;" + "\r\n" +
const signedEd25519MailString = "DKIM-Signature: a=ed25519-sha256;" + "\r\n" +
" " + "bh=2jUSOH9NhtVGCQWNr9BrIAPreKQjO6Sn7XIkfJVOzv8=; c=simple/simple;" + "\r\n" +
" " + "d=football.example.com; h=From:To:Subject:Date:Message-ID; s=brisbane;" + "\r\n" +
" " + "t=424242; v=1;" + "\r\n" +
" " + "b=iv+zd7iDETgqtf1BSyL++WgzTLNmG2msLIb5HupDqb9ynsNKWld1ApDJ0lsIxTBIyq/mKmSw" + "\r\n" +
" " + "+EnfA3YRP/CHBw==" + "\r\n" +
mailHeaderString +
"\r\n" +
mailBodyString
Expand Down
12 changes: 7 additions & 5 deletions dkim/sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ const mailBodyString = "Hi.\r\n" +

const mailString = mailHeaderString + "\r\n" + mailBodyString

const signedMailString = "DKIM-Signature: a=rsa-sha256; bh=2jUSOH9NhtVGCQWNr9BrIAPreKQjO6Sn7XIkfJVOzv" + "\r\n" +
" " + "8=; c=simple/simple; d=example.org; h=From:To:Subject:Date:Message-ID; s=br" + "\r\n" +
" " + "isbane; t=424242; v=1; b=bXtqB8uOEvtd1Xv/DHatdjb9onP0+vnzdYBbPMZm1qrRmhSuFH" + "\r\n" +
" " + "WsbkETafswNvJ4VqNX0gMoaYvzcmoMkUhW9m4pgZqR5y+62yA+B7WJCd6mz82UVkS1qEJeGjMxX" + "\r\n" +
" " + "mmPDkmLDA5HHL5LLTc3DLrxkwWMLzwrhQL48WhNFD1d6L4=;" + "\r\n" +
const signedMailString = "DKIM-Signature: a=rsa-sha256;" + "\r\n" +
" " + "bh=2jUSOH9NhtVGCQWNr9BrIAPreKQjO6Sn7XIkfJVOzv8=; c=simple/simple;" + "\r\n" +
" " + "d=example.org; h=From:To:Subject:Date:Message-ID; s=brisbane; t=424242;" + "\r\n" +
" " + "v=1;" + "\r\n" +
" " + "b=0SgMief3lYbcE4Ke+LxAOWwGD+wTYWvTMqHUY7QQtC05EcTIyHn3vwFUxESzq6l/zx5GY68C" + "\r\n" +
" " + "nkVuMmZ2JkxMtHffdpIAY0w7bkj7EsrJJVmpjk9GANXWPThJCbnVVBZXMViQoQ6hXfzNOunV4+C" + "\r\n" +
" " + "n2QWkBhpjPEJeDBSZ279qFnA=" + "\r\n" +
mailHeaderString +
"\r\n" +
mailBodyString
Expand Down