diff --git a/.gitignore b/.gitignore index 77713c2..835326d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ parsimony admin/node_modules internal/adminui/dist/* !internal/adminui/dist/.gitkeep + +# Private keys must never be committed +*.pem diff --git a/internal/cli/cli_test.go b/internal/cli/cli_test.go index dacaa65..d6d0c00 100644 --- a/internal/cli/cli_test.go +++ b/internal/cli/cli_test.go @@ -760,3 +760,19 @@ func TestDKIMCheckEd25519Key(t *testing.T) { t.Fatalf("%d %q %q", code, out, errs) } } + +func TestDKIMDomainIsLowercased(t *testing.T) { + keyFile := filepath.Join(t.TempDir(), "k.pem") + code, out, errs := noAuth(t, "dkim", "keygen", "Lists.Example.ORG", "--out", keyFile) + if code != 0 { + t.Fatalf("%d %s", code, errs) + } + if !strings.Contains(out, "mail._domainkey.lists.example.org.") || !strings.Contains(out, "domain: lists.example.org") { + t.Fatalf("DNS names and the config use lowercase:\n%s", out) + } + asked := withDNS(t, map[string][]string{}) + noAuth(t, "dkim", "check", "Lists.Example.ORG", "--key", keyFile) + if len(*asked) != 1 || (*asked)[0] != "mail._domainkey.lists.example.org" { + t.Fatalf("lookup name: %v", *asked) + } +} diff --git a/internal/dkimsign/dkimsign_test.go b/internal/dkimsign/dkimsign_test.go index c75635a..4f7da3c 100644 --- a/internal/dkimsign/dkimsign_test.go +++ b/internal/dkimsign/dkimsign_test.go @@ -10,6 +10,7 @@ import ( "encoding/base64" "encoding/pem" "errors" + "io" "os" "path/filepath" "regexp" @@ -402,3 +403,42 @@ func TestSplitTXTChunks(t *testing.T) { } } } + +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") + } +}