git » chasquid » next » tree

[next] / internal / dkim / sign_test.go

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package dkim

import (
	"context"
	"crypto/ecdsa"
	"crypto/ed25519"
	"crypto/elliptic"
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/base64"
	"errors"
	"regexp"
	"strings"
	"testing"
)

var basicMessage = toCRLF(
	`Received: from client1.football.example.com  [192.0.2.1]
      by submitserver.example.com with SUBMISSION;
      Fri, 11 Jul 2003 21:01:54 -0700 (PDT)
From: Joe SixPack <joe@football.example.com>
To: Suzie Q <suzie@shopping.example.net>
Subject: Is dinner ready?
Date: Fri, 11 Jul 2003 21:00:37 -0700 (PDT)
Message-ID: <20030712040037.46341.5F8J@football.example.com>

Hi.

We lost the game. Are you hungry yet?

Joe.
`)

func TestSignRSA(t *testing.T) {
	ctx := t.Context()
	ctx = WithTraceFunc(ctx, t.Logf)

	// Generate a new key pair.
	priv, err := rsa.GenerateKey(rand.Reader, 2048)
	if err != nil {
		t.Fatalf("rsa.GenerateKey: %v", err)
	}
	pub, err := x509.MarshalPKIXPublicKey(priv.Public())
	if err != nil {
		t.Fatalf("MarshalPKIXPublicKey: %v", err)
	}

	ctx = WithLookupTXTFunc(ctx, makeLookupTXT(map[string][]string{
		"test._domainkey.example.com": {
			"v=DKIM1; p=" + base64.StdEncoding.EncodeToString(pub),
		},
	}))

	s := &Signer{
		Domain:   "example.com",
		Selector: "test",
		Signer:   priv,
	}

	sig, err := s.Sign(ctx, basicMessage)
	if err != nil {
		t.Fatalf("Sign: %v", err)
	}

	// Verify the signature.
	res, err := VerifyMessage(ctx, addSig(sig, basicMessage))
	if err != nil || res.Valid != 1 {
		t.Errorf("VerifyMessage: wanted 1 valid / nil; got %v / %v", res, err)
	}

	// Compare the reproducible parts against a known-good header.
	want := regexp.MustCompile(
		"v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n" +
			"d=example.com; s=test; t=\\d+;\r\n" +
			"h=from:subject:date:to:message-id:from:subject:date:to:cc:message-id;\r\n" +
			"bh=[A-Za-z0-9+/]+=*;\r\n" +
			"b=[A-Za-z0-9+/ \r\n]+=*;")
	if !want.MatchString(sig) {
		t.Errorf("Unexpected signature:")
		t.Errorf("  Want: %q (regexp)", want)
		t.Errorf("  Got:  %q", sig)
	}
}

func TestSignEd25519(t *testing.T) {
	ctx := t.Context()
	ctx = WithTraceFunc(ctx, t.Logf)

	// Generate a new key pair.
	pub, priv, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		t.Fatalf("ed25519.GenerateKey: %v", err)
	}

	ctx = WithLookupTXTFunc(ctx, makeLookupTXT(map[string][]string{
		"test._domainkey.example.com": {
			"v=DKIM1; k=ed25519; p=" + base64.StdEncoding.EncodeToString(pub),
		},
	}))

	s := &Signer{
		Domain:   "example.com",
		Selector: "test",
		Signer:   priv,
	}

	sig, err := s.Sign(ctx, basicMessage)
	if err != nil {
		t.Fatalf("Sign: %v", err)
	}

	// Verify the signature.
	res, err := VerifyMessage(ctx, addSig(sig, basicMessage))
	if err != nil || res.Valid != 1 {
		t.Errorf("VerifyMessage: wanted 1 valid / nil; got %v / %v", res, err)
	}

	// Compare the reproducible parts against a known-good header.
	want := regexp.MustCompile(
		"v=1; a=ed25519-sha256; c=relaxed/relaxed;\r\n" +
			"d=example.com; s=test; t=\\d+;\r\n" +
			"h=from:subject:date:to:message-id:from:subject:date:to:cc:message-id;\r\n" +
			"bh=[A-Za-z0-9+/]+=*;\r\n" +
			"b=[A-Za-z0-9+/ \r\n]+=*;")
	if !want.MatchString(sig) {
		t.Errorf("Unexpected signature:")
		t.Errorf("  Want: %q (regexp)", want)
		t.Errorf("  Got:  %q", sig)
	}
}

func TestSignManyHeaders(t *testing.T) {
	// Regression test for signing being quadratic on the number of instances
	// of a header. This takes milliseconds, while the quadratic
	// implementation took minutes with this input. We don't check the
	// timing, so a regression will show up as this test being very slow.
	const n = 50_000

	_, priv, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		t.Fatalf("ed25519.GenerateKey: %v", err)
	}
	s := &Signer{
		Domain:   "example.com",
		Selector: "test",
		Signer:   priv,
	}

	message := "From: from@example.com\r\n" +
		strings.Repeat("To: to@example.com\r\n", n) +
		"\r\nHi.\r\n"

	sig, err := s.Sign(t.Context(), message)
	if err != nil {
		t.Fatalf("Sign: %v", err)
	}

	// All instances must be in h=, plus the extra one we always add.
	hTag := regexp.MustCompile(`h=([^;]*);`).FindStringSubmatch(sig)
	if hTag == nil {
		t.Fatalf("h= not found in signature: %q", sig)
	}
	if got := strings.Count(hTag[1], "to"); got != n+1 {
		t.Errorf("wanted %d instances of 'to' in h=, got %d", n+1, got)
	}
}

func TestSignRepeatedHeaders(t *testing.T) {
	// When a signed header appears multiple times (with different values),
	// they must be signed from bottom to top, otherwise the signature will
	// not verify.
	ctx := t.Context()
	ctx = WithTraceFunc(ctx, t.Logf)

	pub, priv, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		t.Fatalf("ed25519.GenerateKey: %v", err)
	}

	ctx = WithLookupTXTFunc(ctx, makeLookupTXT(map[string][]string{
		"test._domainkey.example.com": {
			"v=DKIM1; k=ed25519; p=" + base64.StdEncoding.EncodeToString(pub),
		},
	}))

	s := &Signer{
		Domain:   "example.com",
		Selector: "test",
		Signer:   priv,
	}

	message := toCRLF(`From: from@example.com
To: to1@example.com
Subject: Subject 1
To: to2@example.com
Subject: Subject 2
To: to3@example.com

Hi.
`)

	sig, err := s.Sign(ctx, message)
	if err != nil {
		t.Fatalf("Sign: %v", err)
	}

	res, err := VerifyMessage(ctx, addSig(sig, message))
	if err != nil || res.Valid != 1 {
		t.Errorf("VerifyMessage: wanted 1 valid / nil; got %v / %v", res, err)
	}
}

func TestSignNonASCIIHeaderNames(t *testing.T) {
	// Header names with non-ASCII characters are invalid, but we must still
	// sign them consistently with how we verify them.
	// Here, "\u017fubject" (U+017F is a long s) is not the same as
	// "Subject", and should not be signed as such.
	ctx := t.Context()
	ctx = WithTraceFunc(ctx, t.Logf)

	pub, priv, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		t.Fatalf("ed25519.GenerateKey: %v", err)
	}

	ctx = WithLookupTXTFunc(ctx, makeLookupTXT(map[string][]string{
		"test._domainkey.example.com": {
			"v=DKIM1; k=ed25519; p=" + base64.StdEncoding.EncodeToString(pub),
		},
	}))

	s := &Signer{
		Domain:   "example.com",
		Selector: "test",
		Signer:   priv,
	}

	message := toCRLF("From: from@example.com\n" +
		"\u017fubject: Not a subject\n" +
		"\n" +
		"Hi.\n")

	sig, err := s.Sign(ctx, message)
	if err != nil {
		t.Fatalf("Sign: %v", err)
	}

	res, err := VerifyMessage(ctx, addSig(sig, message))
	if err != nil || res.Valid != 1 {
		t.Errorf("VerifyMessage: wanted 1 valid / nil; got %v / %v", res, err)
	}
}

func addSig(sig, message string) string {
	return "DKIM-Signature: " +
		strings.ReplaceAll(sig, "\r\n", "\r\n\t") +
		"\r\n" + message
}

func TestSignBadMessage(t *testing.T) {
	s := &Signer{
		Domain:   "example.com",
		Selector: "test",
	}
	_, err := s.Sign(t.Context(), "Bad message")
	if err == nil {
		t.Errorf("Sign: wanted error; got nil")
	}
}

func TestSignBadAlgorithm(t *testing.T) {
	s := &Signer{
		Domain:   "example.com",
		Selector: "test",
	}

	priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
	if err != nil {
		t.Fatalf("ecdsa.GenerateKey: %v", err)
	}
	s.Signer = priv

	_, err = s.Sign(t.Context(), basicMessage)
	if !errors.Is(err, errUnsupportedKeyType) {
		t.Errorf("Sign: wanted unsupported key type; got %v", err)
	}
}

func TestBreakLongLines(t *testing.T) {
	cases := []struct {
		in   string
		want string
	}{
		{"1234567890", "1234567890"},
		{
			"xxxxxxxx10xxxxxxxx20xxxxxxxx30xxxxxxxx40" +
				"xxxxxxxx50xxxxxxxx60xxxxxxxx70",
			"xxxxxxxx10xxxxxxxx20xxxxxxxx30xxxxxxxx40" +
				"xxxxxxxx50xxxxxxxx60xxxxxxxx70",
		},
		{
			"xxxxxxxx10xxxxxxxx20xxxxxxxx30xxxxxxxx40" +
				"xxxxxxxx50xxxxxxxx60xxxxxxxx70123",
			"xxxxxxxx10xxxxxxxx20xxxxxxxx30xxxxxxxx40" +
				"xxxxxxxx50xxxxxxxx60xxxxxxxx70\r\n  123",
		},
		{
			"xxxxxxxx10xxxxxxxx20xxxxxxxx30xxxxxxxx40" +
				"xxxxxxxx50xxxxxxxx60xxxxxxxx70xxxxxxxx80" +
				"xxxxxxxx90xxxxxxx100xxxxxxx110xxxxxxx120" +
				"xxxxxxx130xxxxxxx140xxxxxxx150xxxxxxx160",
			"xxxxxxxx10xxxxxxxx20xxxxxxxx30xxxxxxxx40" +
				"xxxxxxxx50xxxxxxxx60xxxxxxxx70\r\n  " +
				"xxxxxxxx80xxxxxxxx90xxxxxxx100xxxxxxx110" +
				"xxxxxxx120xxxxxxx130xxxxxxx140\r\n  " +
				"xxxxxxx150xxxxxxx160",
		},
	}

	for i, c := range cases {
		got := breakLongLines(c.in)
		if got != c.want {
			t.Errorf("%d: breakLongLines(%q):", i, c.in)
			t.Errorf("      want %q", c.want)
			t.Errorf("      got  %q", got)
		}
	}
}

func TestFormatHeaders(t *testing.T) {
	cases := []struct {
		in   []string
		want string
	}{
		{[]string{"From"}, "from"},
		{
			[]string{"From", "Subject", "Date"},
			"from:subject:date",
		},
		{
			[]string{"from", "subject", "date", "to", "message-id",
				"from", "subject", "date", "to", "cc", "in-reply-to",
				"message-id"},
			"from:subject:date:to:message-id:" +
				"from:subject:date:to:cc:in-reply-to\r\n" +
				"  :message-id",
		},
		{
			[]string{"from", "subject", "date", "to", "message-id",
				"from", "subject", "date", "to", "cc", "xxxxxxxxxxxx70"},
			"from:subject:date:to:message-id:" +
				"from:subject:date:to:cc:xxxxxxxxxxxx70",
		},
		{
			[]string{"from", "subject", "date", "to", "message-id",
				"from", "subject", "date", "to", "cc", "xxxxxxxxxxxx701"},
			"from:subject:date:to:message-id:from:subject:date:to:cc\r\n" +
				"  :xxxxxxxxxxxx701",
		},
		{
			[]string{"from", "subject", "date", "to", "message-id",
				"from", "subject", "date", "to", "cc", "xxxxxxxxxxxx70",
				"1"},
			"from:subject:date:to:message-id:" +
				"from:subject:date:to:cc:xxxxxxxxxxxx70\r\n" +
				"  :1",
		},
	}

	for i, c := range cases {
		got := formatHeaders(c.in)
		if got != c.want {
			t.Errorf("%d: formatHeaders(%q):", i, c.in)
			t.Errorf("      want %q", c.want)
			t.Errorf("      got  %q", got)
		}
	}
}

func FuzzSignAndVerify(f *testing.F) {
	f.Add(basicMessage)
	f.Add("From: a@b\r\n\r\n")
	f.Add("")

	// Repeated headers with different values.
	f.Add("From: a@b\r\nTo: c@d\r\n e@f\r\nTo: g@h\r\n\r\nbody\r\n")

	// Non-ASCII header name (U+017F is a long s).
	f.Add("From: a@b\r\nſubject: x\r\n\r\nbody\r\n")

	// Messages from the file-based tests, which have more realistic headers,
	// including existing DKIM signatures.
	addTestdataCorpus(f)

	// Use a fixed key, as generating one for each run would be too slow.
	priv := ed25519.NewKeyFromSeed(make([]byte, ed25519.SeedSize))
	pub := priv.Public().(ed25519.PublicKey)
	ctx := WithLookupTXTFunc(context.Background(),
		makeLookupTXT(map[string][]string{
			"test._domainkey.example.com": {
				"v=DKIM1; k=ed25519; p=" +
					base64.StdEncoding.EncodeToString(pub),
			},
		}))

	s := &Signer{
		Domain:   "example.com",
		Selector: "test",
		Signer:   priv,
	}

	f.Fuzz(func(t *testing.T, msg string) {
		sig, err := s.Sign(ctx, msg)
		if err != nil {
			return
		}

		// Any message we can sign, we must be able to verify.
		// Our signature is the first one, so it's always verified even if
		// the message has other signatures.
		res, err := VerifyMessage(ctx, addSig(sig, msg))
		if err != nil {
			t.Fatalf("VerifyMessage error: %v", err)
		}
		if len(res.Results) == 0 {
			t.Fatalf("signature not found when verifying")
		}
		if r := res.Results[0]; r.State != SUCCESS {
			t.Errorf("signature did not verify: %v / %v", r.State, r.Error)
		}
	})
}