- Cover headers outside the signed list changing in transit, quoted local parts with @, signing errors and lowercase domains - Ignore *.pem so a private key is never committed
444 lines
15 KiB
Go
444 lines
15 KiB
Go
package dkimsign
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto"
|
|
"crypto/ed25519"
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
"encoding/base64"
|
|
"encoding/pem"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/emersion/go-msgauth/dkim"
|
|
)
|
|
|
|
func rsaKey(t *testing.T, bits int) *rsa.PrivateKey {
|
|
t.Helper()
|
|
k, err := rsa.GenerateKey(rand.Reader, bits)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return k
|
|
}
|
|
|
|
// dnsRecord is the TXT record a receiver would find for a key.
|
|
func dnsRecord(t *testing.T, pub crypto.PublicKey) string {
|
|
t.Helper()
|
|
switch k := pub.(type) {
|
|
case *rsa.PublicKey:
|
|
der, _ := x509.MarshalPKIXPublicKey(k)
|
|
return "v=DKIM1; k=rsa; p=" + base64.StdEncoding.EncodeToString(der)
|
|
case ed25519.PublicKey:
|
|
return "v=DKIM1; k=ed25519; p=" + base64.StdEncoding.EncodeToString(k)
|
|
}
|
|
t.Fatal("unsupported key")
|
|
return ""
|
|
}
|
|
|
|
// verify checks every DKIM signature of msg against a fake DNS with a single record.
|
|
func verify(t *testing.T, msg []byte, selector, domain string, pub crypto.PublicKey) []*dkim.Verification {
|
|
t.Helper()
|
|
vs, err := dkim.VerifyWithOptions(bytes.NewReader(msg), &dkim.VerifyOptions{
|
|
LookupTXT: func(name string) ([]string, error) {
|
|
if name == selector+"._domainkey."+domain {
|
|
return []string{dnsRecord(t, pub)}, nil
|
|
}
|
|
return nil, errors.New("no such record: " + name)
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("verify: %v", err)
|
|
}
|
|
return vs
|
|
}
|
|
|
|
func mustVerify(t *testing.T, msg []byte, selector, domain string, pub crypto.PublicKey) {
|
|
t.Helper()
|
|
vs := verify(t, msg, selector, domain, pub)
|
|
if len(vs) != 1 || vs[0].Err != nil || vs[0].Domain != domain {
|
|
t.Fatalf("verification: %+v", vs)
|
|
}
|
|
}
|
|
|
|
func message(from string) []byte {
|
|
return []byte("From: " + from + "\r\nTo: dev@lists.example.org\r\nSubject: Hello list\r\nDate: Mon, 01 Jan 2024 10:00:00 +0000\r\n" +
|
|
"Message-ID: <1@x>\r\nList-Id: Dev <dev.lists.example.org>\r\nList-Unsubscribe: <mailto:dev+leave@lists.example.org>\r\n" +
|
|
"Content-Type: text/plain; charset=utf-8\r\n\r\nthe body\r\nsecond line\r\n")
|
|
}
|
|
|
|
func newSigner(t *testing.T, keys ...Key) *Signer {
|
|
t.Helper()
|
|
s, err := New(keys)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return s
|
|
}
|
|
|
|
func TestSignRSAAndVerify(t *testing.T) {
|
|
k := rsaKey(t, 2048)
|
|
s := newSigner(t, Key{Domain: "lists.example.org", Selector: "mail", Signer: k})
|
|
in := message("Alice via Dev <dev@lists.example.org>")
|
|
out, err := s.Sign(in)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !bytes.HasPrefix(out, []byte("DKIM-Signature:")) || !bytes.HasSuffix(out, in) {
|
|
t.Fatalf("the signature header goes first and the original follows untouched:\n%s", out)
|
|
}
|
|
mustVerify(t, out, "mail", "lists.example.org", &k.PublicKey)
|
|
sig := string(out[:bytes.Index(out, []byte("\r\n\r\n"))])
|
|
for _, want := range []string{"a=rsa-sha256", "c=relaxed/relaxed", "d=lists.example.org", "s=mail"} {
|
|
if !strings.Contains(sig, want) {
|
|
t.Errorf("signature lacks %q:\n%s", want, sig)
|
|
}
|
|
}
|
|
if strings.Contains(sig, "l=") {
|
|
t.Error("a body length limit lets attackers append text: never use l=")
|
|
}
|
|
}
|
|
|
|
func TestSignEd25519AndVerify(t *testing.T) {
|
|
pub, priv, _ := ed25519.GenerateKey(rand.Reader)
|
|
s := newSigner(t, Key{Domain: "lists.example.org", Selector: "ed", Signer: priv})
|
|
out, err := s.Sign(message("dev@lists.example.org"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mustVerify(t, out, "ed", "lists.example.org", pub)
|
|
}
|
|
|
|
func TestSignedHeadersCoverTheListHeaders(t *testing.T) {
|
|
k := rsaKey(t, 2048)
|
|
s := newSigner(t, Key{Domain: "lists.example.org", Selector: "mail", Signer: k})
|
|
out, _ := s.Sign(message("dev@lists.example.org"))
|
|
sig := strings.ToLower(string(out[:bytes.Index(out, []byte("\r\n\r\n"))]))
|
|
for _, h := range []string{"from", "to", "subject", "date", "message-id", "list-id", "list-unsubscribe", "content-type"} {
|
|
if !regexpHas(sig, h) {
|
|
t.Errorf("%s must be signed: %s", h, sig)
|
|
}
|
|
}
|
|
}
|
|
|
|
var hTag = regexp.MustCompile(`(?m)(?:^|[;\s])h=([^;]*)`)
|
|
|
|
func regexpHas(sig, header string) bool {
|
|
m := hTag.FindStringSubmatch(sig)
|
|
if m == nil {
|
|
return false
|
|
}
|
|
for _, h := range strings.Split(strings.Join(strings.Fields(m[1]), ""), ":") {
|
|
if h == header {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func TestTamperingBreaksTheSignature(t *testing.T) {
|
|
k := rsaKey(t, 2048)
|
|
s := newSigner(t, Key{Domain: "lists.example.org", Selector: "mail", Signer: k})
|
|
out, _ := s.Sign(message("dev@lists.example.org"))
|
|
for name, tampered := range map[string][]byte{
|
|
"body": bytes.Replace(out, []byte("the body"), []byte("the BODY!"), 1),
|
|
"subject": bytes.Replace(out, []byte("Subject: Hello list"), []byte("Subject: Hello you"), 1),
|
|
"list-unsubscribe": bytes.Replace(out, []byte("List-Unsubscribe: <mailto:dev+leave@lists.example.org>\r\n"), nil, 1),
|
|
"appended text": append(append([]byte{}, out...), []byte("extra line\r\n")...),
|
|
} {
|
|
vs := verify(t, tampered, "mail", "lists.example.org", &k.PublicKey)
|
|
if len(vs) != 1 || vs[0].Err == nil {
|
|
t.Errorf("%s: a modified message must not verify: %+v", name, vs)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestUnsignedWhenThereIsNoKeyOrNoUsableFrom(t *testing.T) {
|
|
k := rsaKey(t, 2048)
|
|
s := newSigner(t, Key{Domain: "lists.example.org", Selector: "mail", Signer: k})
|
|
for name, in := range map[string][]byte{
|
|
"other domain": message("someone@elsewhere.test"),
|
|
"no From header": []byte("Subject: x\r\n\r\nbody\r\n"),
|
|
"unparsable From": []byte("From: not an address\r\nSubject: x\r\n\r\nbody\r\n"),
|
|
"empty message": {},
|
|
"not a message": []byte("garbage"),
|
|
} {
|
|
out, err := s.Sign(in)
|
|
if err != nil || !bytes.Equal(out, in) {
|
|
t.Errorf("%s: want the message back untouched, got %q %v", name, out, err)
|
|
}
|
|
}
|
|
// No keys at all.
|
|
empty := newSigner(t)
|
|
in := message("dev@lists.example.org")
|
|
if out, err := empty.Sign(in); err != nil || !bytes.Equal(out, in) {
|
|
t.Fatalf("no keys: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSigningDomainSelection(t *testing.T) {
|
|
parent, sub := rsaKey(t, 2048), rsaKey(t, 2048)
|
|
s := newSigner(t,
|
|
Key{Domain: "example.org", Selector: "p", Signer: parent},
|
|
Key{Domain: "lists.example.org", Selector: "s", Signer: sub},
|
|
)
|
|
// Exact match wins over the parent.
|
|
out, _ := s.Sign(message("dev@lists.example.org"))
|
|
mustVerify(t, out, "s", "lists.example.org", &sub.PublicKey)
|
|
// A subdomain without its own key is signed with the parent (relaxed alignment).
|
|
out, _ = s.Sign(message("dev@news.lists.example.org"))
|
|
mustVerify(t, out, "s", "lists.example.org", &sub.PublicKey)
|
|
out, _ = s.Sign(message("dev@other.example.org"))
|
|
mustVerify(t, out, "p", "example.org", &parent.PublicKey)
|
|
// A different domain with a similar suffix is not a subdomain.
|
|
if out, _ := s.Sign(message("dev@notexample.org")); bytes.HasPrefix(out, []byte("DKIM-Signature")) {
|
|
t.Fatal("notexample.org is not a subdomain of example.org")
|
|
}
|
|
}
|
|
|
|
func TestDomainMatchingIgnoresCaseAndDisplayNames(t *testing.T) {
|
|
k := rsaKey(t, 2048)
|
|
s := newSigner(t, Key{Domain: "Lists.Example.ORG", Selector: "mail", Signer: k})
|
|
out, _ := s.Sign(message(`"someone@elsewhere.test via Dev" <DEV@LISTS.example.org>`))
|
|
mustVerify(t, out, "mail", "lists.example.org", &k.PublicKey)
|
|
// A key domain named only in the display name must not be used.
|
|
out, _ = s.Sign(message(`"dev@lists.example.org" <x@elsewhere.test>`))
|
|
if bytes.HasPrefix(out, []byte("DKIM-Signature")) {
|
|
t.Fatal("the domain comes from the address, not the display name")
|
|
}
|
|
}
|
|
|
|
func TestNewRejectsBadKeySets(t *testing.T) {
|
|
k := rsaKey(t, 2048)
|
|
for name, keys := range map[string][]Key{
|
|
"duplicate domain": {{Domain: "a.org", Selector: "x", Signer: k}, {Domain: "A.org", Selector: "y", Signer: k}},
|
|
"empty domain": {{Domain: "", Selector: "x", Signer: k}},
|
|
"empty selector": {{Domain: "a.org", Selector: "", Signer: k}},
|
|
"no signer": {{Domain: "a.org", Selector: "x"}},
|
|
"selector with a space": {{Domain: "a.org", Selector: "my sel", Signer: k}},
|
|
} {
|
|
if _, err := New(keys); err == nil {
|
|
t.Errorf("%s: want an error", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSignIsSafeForConcurrentUse(t *testing.T) {
|
|
k := rsaKey(t, 2048)
|
|
s := newSigner(t, Key{Domain: "lists.example.org", Selector: "mail", Signer: k})
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 16; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
out, err := s.Sign(message("dev@lists.example.org"))
|
|
if err != nil || !bytes.HasPrefix(out, []byte("DKIM-Signature")) {
|
|
t.Errorf("concurrent sign: %v", err)
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
}
|
|
|
|
func TestBigMessagesAreSigned(t *testing.T) {
|
|
k := rsaKey(t, 2048)
|
|
s := newSigner(t, Key{Domain: "lists.example.org", Selector: "mail", Signer: k})
|
|
in := append(message("dev@lists.example.org"), bytes.Repeat([]byte("0123456789abcdef\r\n"), 300_000)...)
|
|
out, err := s.Sign(in)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mustVerify(t, out, "mail", "lists.example.org", &k.PublicKey)
|
|
}
|
|
|
|
func writePEM(t *testing.T, typ string, der []byte) string {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), "key.pem")
|
|
if err := os.WriteFile(path, pem.EncodeToMemory(&pem.Block{Type: typ, Bytes: der}), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
func TestLoadKeyFormats(t *testing.T) {
|
|
k := rsaKey(t, 2048)
|
|
pkcs8, _ := x509.MarshalPKCS8PrivateKey(k)
|
|
_, ed, _ := ed25519.GenerateKey(rand.Reader)
|
|
edDER, _ := x509.MarshalPKCS8PrivateKey(ed)
|
|
for name, path := range map[string]string{
|
|
"rsa pkcs1": writePEM(t, "RSA PRIVATE KEY", x509.MarshalPKCS1PrivateKey(k)),
|
|
"rsa pkcs8": writePEM(t, "PRIVATE KEY", pkcs8),
|
|
"ed25519 p8": writePEM(t, "PRIVATE KEY", edDER),
|
|
} {
|
|
key, err := LoadKey("lists.example.org", "mail", path)
|
|
if err != nil {
|
|
t.Errorf("%s: %v", name, err)
|
|
continue
|
|
}
|
|
s := newSigner(t, key)
|
|
out, err := s.Sign(message("dev@lists.example.org"))
|
|
if err != nil || !bytes.HasPrefix(out, []byte("DKIM-Signature")) {
|
|
t.Errorf("%s: loaded key does not sign: %v", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLoadKeyRejectsUnusableFiles(t *testing.T) {
|
|
cases := map[string]string{
|
|
"garbage": func() string {
|
|
p := filepath.Join(t.TempDir(), "k")
|
|
os.WriteFile(p, []byte("not a pem"), 0o600)
|
|
return p
|
|
}(),
|
|
"wrong type": writePEM(t, "CERTIFICATE", []byte("x")),
|
|
"truncated der": writePEM(t, "RSA PRIVATE KEY", []byte{1, 2, 3}),
|
|
"missing file": "/nonexistent/key.pem",
|
|
}
|
|
for name, path := range cases {
|
|
if _, err := LoadKey("a.org", "mail", path); err == nil {
|
|
t.Errorf("%s: want an error", name)
|
|
}
|
|
}
|
|
// The error never contains key material.
|
|
_, err := LoadKey("a.org", "mail", writePEM(t, "RSA PRIVATE KEY", []byte{1, 2, 3}))
|
|
if err != nil && strings.Contains(err.Error(), "AQID") {
|
|
t.Fatal("do not echo key bytes in errors")
|
|
}
|
|
}
|
|
|
|
func TestGenerateKeyPEMProducesAUsableKey(t *testing.T) {
|
|
pemBytes, err := GenerateKeyPEM(2048)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !bytes.HasPrefix(pemBytes, []byte("-----BEGIN PRIVATE KEY-----")) {
|
|
t.Fatalf("PKCS#8 expected:\n%s", pemBytes)
|
|
}
|
|
path := filepath.Join(t.TempDir(), "k.pem")
|
|
os.WriteFile(path, pemBytes, 0o600)
|
|
key, err := LoadKey("lists.example.org", "mail", path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out, err := newSigner(t, key).Sign(message("dev@lists.example.org"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rec, err := Record(key.Signer.Public())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
vs, err := dkim.VerifyWithOptions(bytes.NewReader(out), &dkim.VerifyOptions{
|
|
LookupTXT: func(string) ([]string, error) { return []string{rec}, nil },
|
|
})
|
|
if err != nil || len(vs) != 1 || vs[0].Err != nil {
|
|
t.Fatalf("the generated key and its DNS record must work together: %v %+v", err, vs)
|
|
}
|
|
if _, err := GenerateKeyPEM(512); err == nil {
|
|
t.Fatal("keys that are too small must be refused")
|
|
}
|
|
}
|
|
|
|
func TestRecordFormat(t *testing.T) {
|
|
k := rsaKey(t, 2048)
|
|
rec, err := Record(&k.PublicKey)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
der, _ := x509.MarshalPKIXPublicKey(&k.PublicKey)
|
|
if rec != "v=DKIM1; k=rsa; p="+base64.StdEncoding.EncodeToString(der) {
|
|
t.Fatalf("rsa record: %s", rec)
|
|
}
|
|
pub, _, _ := ed25519.GenerateKey(rand.Reader)
|
|
rec, _ = Record(pub)
|
|
if rec != "v=DKIM1; k=ed25519; p="+base64.StdEncoding.EncodeToString(pub) {
|
|
t.Fatalf("ed25519 record: %s", rec)
|
|
}
|
|
if _, err := Record("not a key"); err == nil {
|
|
t.Fatal("unsupported key type")
|
|
}
|
|
}
|
|
|
|
func TestRecordKeyParsing(t *testing.T) {
|
|
for in, want := range map[string]string{
|
|
"v=DKIM1; k=rsa; p=AAAA": "AAAA",
|
|
"v=DKIM1;k=rsa;p=AA AA": "AAAA",
|
|
"v=DKIM1; p=AAAA; t=y": "AAAA",
|
|
"p=AAAA": "AAAA",
|
|
"v=DKIM1; k=rsa; p=AAAA BBBB\tCCCC": "AAAABBBBCCCC",
|
|
} {
|
|
if got, ok := RecordKey(in); !ok || got != want {
|
|
t.Errorf("RecordKey(%q) = %q %v, want %q", in, got, ok, want)
|
|
}
|
|
}
|
|
for _, in := range []string{"", "v=spf1 -all", "v=DKIM1; k=rsa", "v=DKIM1; p="} {
|
|
if _, ok := RecordKey(in); ok {
|
|
t.Errorf("RecordKey(%q) must fail", in)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSplitTXTChunks(t *testing.T) {
|
|
for _, n := range []int{0, 1, 254, 255, 256, 400, 510, 511, 1000} {
|
|
v := strings.Repeat("a", n)
|
|
parts := SplitTXT(v)
|
|
if strings.Join(parts, "") != v {
|
|
t.Errorf("n=%d: the parts must rebuild the value", n)
|
|
}
|
|
for _, p := range parts {
|
|
if len(p) > 255 {
|
|
t.Errorf("n=%d: chunk of %d bytes exceeds the DNS limit", n, len(p))
|
|
}
|
|
}
|
|
if want := (n + 254) / 255; n > 0 && len(parts) != want {
|
|
t.Errorf("n=%d: %d chunks, want %d", n, len(parts), want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHeadersOutsideTheSignedListMayChangeInTransit(t *testing.T) {
|
|
k := rsaKey(t, 2048)
|
|
s := newSigner(t, Key{Domain: "lists.example.org", Selector: "mail", Signer: k})
|
|
in := append([]byte("X-Transit: original\r\n"), message("dev@lists.example.org")...)
|
|
out, _ := s.Sign(in)
|
|
changed := bytes.Replace(out, []byte("X-Transit: original"), []byte("X-Transit: rewritten by a relay"), 1)
|
|
mustVerify(t, changed, "mail", "lists.example.org", &k.PublicKey)
|
|
withReceived := append([]byte("Received: from relay by mx; Mon, 01 Jan 2024 10:00:01 +0000\r\n"), out...)
|
|
mustVerify(t, withReceived, "mail", "lists.example.org", &k.PublicKey)
|
|
}
|
|
|
|
func TestFromAddressWithAnAtSignInTheLocalPart(t *testing.T) {
|
|
k := rsaKey(t, 2048)
|
|
s := newSigner(t, Key{Domain: "lists.example.org", Selector: "mail", Signer: k})
|
|
out, _ := s.Sign(message(`"odd@name"@lists.example.org`))
|
|
mustVerify(t, out, "mail", "lists.example.org", &k.PublicKey)
|
|
}
|
|
|
|
// brokenSigner has an RSA public key but cannot sign.
|
|
type brokenSigner struct{ pub crypto.PublicKey }
|
|
|
|
func (b brokenSigner) Public() crypto.PublicKey { return b.pub }
|
|
func (brokenSigner) Sign(io.Reader, []byte, crypto.SignerOpts) ([]byte, error) {
|
|
return nil, errors.New("hsm unavailable")
|
|
}
|
|
|
|
func TestASigningErrorReturnsTheOriginalMessageAndTheError(t *testing.T) {
|
|
k := rsaKey(t, 2048)
|
|
s := newSigner(t, Key{Domain: "lists.example.org", Selector: "mail", Signer: brokenSigner{pub: &k.PublicKey}})
|
|
in := message("dev@lists.example.org")
|
|
out, err := s.Sign(in)
|
|
if err == nil || !strings.Contains(err.Error(), "lists.example.org") {
|
|
t.Fatalf("the error names the domain: %v", err)
|
|
}
|
|
if !bytes.Equal(out, in) {
|
|
t.Fatal("on error the caller still gets the original message to send unsigned")
|
|
}
|
|
}
|