10 "github.com/mjl-/mox/dns"
11 "github.com/mjl-/mox/smtp"
14func TestSig(t *testing.T) {
15 test := func(s string, smtputf8 bool, expSig *Sig, expErr error) {
18 isParseErr := func(err error) bool {
19 _, ok := err.(parseErr)
23 sig, _, err := parseSignature([]byte(s), smtputf8)
24 if (err == nil) != (expErr == nil) || err != nil && !errors.Is(err, expErr) && !(isParseErr(err) && isParseErr(expErr)) {
25 t.Fatalf("got err %v, expected %v", err, expErr)
27 if !reflect.DeepEqual(sig, expSig) {
28 t.Fatalf("got sig %#v, expected %#v", sig, expSig)
34 h, err := sig.Header()
36 t.Fatalf("making signature header: %v", err)
38 nsig, _, err := parseSignature([]byte(h), smtputf8)
40 t.Fatalf("parse signature again: %v", err)
42 if !reflect.DeepEqual(nsig, sig) {
43 t.Fatalf("parsed signature again, got %#v, expected %#v", nsig, sig)
47 xbase64 := func(s string) []byte {
49 buf, err := base64.StdEncoding.DecodeString(s)
51 t.Fatalf("parsing base64: %v", err)
56 xdomain := func(s string) dns.Domain {
58 d, err := dns.ParseDomain(s)
60 t.Fatalf("parsing domain: %v", err)
65 var empty smtp.Localpart
68 AlgorithmSign: "ed25519",
69 AlgorithmHash: "sha256",
70 Signature: xbase64("dGVzdAo="),
71 BodyHash: xbase64("LjkN2rUhrS3zKXfH2vNgUzz5ERRJkgP9CURXBX0JP0Q="),
72 Domain: xdomain("mox.example"),
73 SignedHeaders: []string{"from", "to", "cc", "bcc", "date", "subject", "message-id"},
74 Selector: xdomain("test"),
75 Canonicalization: "simple/relaxed",
77 Identity: &Identity{&empty, xdomain("sub.mox.example")},
78 QueryMethods: []string{"dns/txt", "other"},
81 CopiedHeaders: []string{"From:<mjl@mox.example>", "Subject:test | with pipe"},
83 test("dkim-signature: v = 1 ; a=ed25519-sha256; s=test; d=mox.example; h=from:to:cc:bcc:date:subject:message-id; b=dGVzdAo=; bh=LjkN2rUhrS3zKXfH2vNgUzz5ERRJkgP9CURXBX0JP0Q= ; c=simple/relaxed; l=10; i=\"\"@sub.mox.example; q= dns/txt:other; t=10; x=100; z=From:<mjl@mox.example>|Subject:test=20=7C=20with=20pipe; unknown = must be ignored \r\n", true, sig1, nil)
85 ulp := smtp.Localpart("møx")
88 AlgorithmSign: "ed25519",
89 AlgorithmHash: "sha256",
90 Signature: xbase64("dGVzdAo="),
91 BodyHash: xbase64("LjkN2rUhrS3zKXfH2vNgUzz5ERRJkgP9CURXBX0JP0Q="),
92 Domain: xdomain("xn--mx-lka.example"), // møx.example
93 SignedHeaders: []string{"from"},
94 Selector: dns.Domain{ASCII: "xn--tst-bma"},
95 Identity: &Identity{&ulp, xdomain("xn--tst-bma.xn--mx-lka.example")}, // tést.møx.example
96 Canonicalization: "simple/simple",
101 test("dkim-signature: v = 1 ; a=ed25519-sha256; s=xn--tst-bma; d=xn--mx-lka.example; h=from; b=dGVzdAo=; bh=LjkN2rUhrS3zKXfH2vNgUzz5ERRJkgP9CURXBX0JP0Q= ; i=møx@xn--tst-bma.xn--mx-lka.example;\r\n", true, sig2, nil)
102 test("dkim-signature: v = 1 ; a=ed25519-sha256; s=xn--tst-bma; d=xn--mx-lka.example; h=from; b=dGVzdAo=; bh=LjkN2rUhrS3zKXfH2vNgUzz5ERRJkgP9CURXBX0JP0Q= ; i=møx@xn--tst-bma.xn--mx-lka.example;\r\n", false, nil, parseErr("")) // No UTF-8 allowed.
104 multiatom := smtp.Localpart("a.b.c")
107 AlgorithmSign: "ed25519",
108 AlgorithmHash: "sha256",
109 Signature: xbase64("dGVzdAo="),
110 BodyHash: xbase64("LjkN2rUhrS3zKXfH2vNgUzz5ERRJkgP9CURXBX0JP0Q="),
111 Domain: xdomain("mox.example"),
112 SignedHeaders: []string{"from"},
113 Selector: xdomain("test"),
114 Identity: &Identity{&multiatom, xdomain("mox.example")},
115 Canonicalization: "simple/simple",
120 test("dkim-signature: v = 1 ; a=ed25519-sha256; s=test; d=mox.example; h=from; b=dGVzdAo=; bh=LjkN2rUhrS3zKXfH2vNgUzz5ERRJkgP9CURXBX0JP0Q= ; i=a.b.c@mox.example\r\n", true, sig3, nil)
122 quotedlp := smtp.Localpart(`test "\test`)
125 AlgorithmSign: "ed25519",
126 AlgorithmHash: "sha256",
127 Signature: xbase64("dGVzdAo="),
128 BodyHash: xbase64("LjkN2rUhrS3zKXfH2vNgUzz5ERRJkgP9CURXBX0JP0Q="),
129 Domain: xdomain("mox.example"),
130 SignedHeaders: []string{"from"},
131 Selector: xdomain("test"),
132 Identity: &Identity{"edlp, xdomain("mox.example")},
133 Canonicalization: "simple/simple",
138 test("dkim-signature: v = 1 ; a=ed25519-sha256; s=test; d=mox.example; h=from; b=dGVzdAo=; bh=LjkN2rUhrS3zKXfH2vNgUzz5ERRJkgP9CURXBX0JP0Q= ; i=\"test \\\"\\\\test\"@mox.example\r\n", true, sig4, nil)
140 test("", true, nil, errSigMissingCRLF)
141 test("other: ...\r\n", true, nil, errSigHeader)
142 test("dkim-signature: v=2\r\n", true, nil, errSigUnknownVersion)
143 test("dkim-signature: v=1\r\n", true, nil, errSigMissingTag)
144 test("dkim-signature: v=1;v=1\r\n", true, nil, errSigDuplicateTag)
145 test("dkim-signature: v=1; d=mox.example; i=@unrelated.example; s=test; a=ed25519-sha256; h=from; b=dGVzdAo=; bh=LjkN2rUhrS3zKXfH2vNgUzz5ERRJkgP9CURXBX0JP0Q=\r\n", true, nil, errSigIdentityDomain)
146 test("dkim-signature: v=1; t=10; x=9; d=mox.example; s=test; a=ed25519-sha256; h=from; b=dGVzdAo=; bh=LjkN2rUhrS3zKXfH2vNgUzz5ERRJkgP9CURXBX0JP0Q=\r\n", true, nil, errSigExpired)
147 test("dkim-signature: v=1; d=møx.example\r\n", true, nil, parseErr("")) // Unicode domain not allowed.
148 test("dkim-signature: v=1; s=tést\r\n", true, nil, parseErr("")) // Unicode selector not allowed.
149 test("dkim-signature: v=1; ;\r\n", true, nil, parseErr("")) // Empty tag not allowed.
150 test("dkim-signature: v=1; \r\n", true, nil, parseErr("")) // Cannot have whitespace after last colon.
151 test("dkim-signature: v=1; d=mox.example; s=test; a=ed25519-sha256; h=from; b=dGVzdAo=; bh=dGVzdAo=\r\n", true, nil, errSigBodyHash)
152 test("dkim-signature: v=1; d=mox.example; s=test; a=rsa-sha1; h=from; b=dGVzdAo=; bh=dGVzdAo=\r\n", true, nil, errSigBodyHash)
155func TestCopiedHeadersSig(t *testing.T) {
157 sigHeader := strings.ReplaceAll(`DKIM-Signature: v=1; a=rsa-sha256; d=example.net; s=brisbane;
158 c=simple; q=dns/txt; i=@eng.example.net;
159 t=1117574938; x=1118006938;
160 h=from:to:subject:date;
161 z=From:foo@eng.example.net|To:joe@example.com|
162 Subject:demo=20run|Date:July=205,=202005=203:44:08=20PM=20-0700;
163 bh=MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=;
164 b=dzdVyOfAKCdLXdJOc9G2q8LoXSlEniSbav+yuU4zGeeruD00lszZVoG4ZHRNiYzR
167 sig, _, err := parseSignature([]byte(sigHeader), false)
169 t.Fatalf("parsing dkim signature with copied headers: %v", err)
172 "From:foo@eng.example.net",
173 "To:joe@example.com",
175 "Date:July 5, 2005 3:44:08 PM -0700",
177 if !reflect.DeepEqual(sig.CopiedHeaders, exp) {
178 t.Fatalf("copied headers, got %v, expected %v", sig.CopiedHeaders, exp)