Merge pull request 'DKIM signing of outgoing mail' (#25) from feat/dkim into main

This commit is contained in:
squidd 2026-11-09 20:06:42 +01:00
commit 9d31549f50
17 changed files with 1485 additions and 3 deletions

3
.gitignore vendored
View file

@ -4,3 +4,6 @@ parsimony
admin/node_modules admin/node_modules
internal/adminui/dist/* internal/adminui/dist/*
!internal/adminui/dist/.gitkeep !internal/adminui/dist/.gitkeep
# Private keys must never be committed
*.pem

View file

@ -5,6 +5,7 @@ A modern, single-binary mailing list manager: an evolution of Mailman.
- Postfix delivers to parsimony over LMTP; parsimony relays through SMTP. - Postfix delivers to parsimony over LMTP; parsimony relays through SMTP.
- Discussion lists with member-only posting, `List-*` headers, one-click unsubscribe - Discussion lists with member-only posting, `List-*` headers, one-click unsubscribe
and DMARC-safe `From` rewriting. and DMARC-safe `From` rewriting.
- DKIM signing of outgoing mail with one key per list domain, so DMARC holds after the From rewrite.
- Durable outbound queue: a post is accepted once it is stored, one delivery per recipient, - Durable outbound queue: a post is accepted once it is stored, one delivery per recipient,
and a background worker sends it with retries (1m, 5m, 15m, 1h, 3h, then every 6h, for up to and a background worker sends it with retries (1m, 5m, 15m, 1h, 3h, then every 6h, for up to
3 days). Relay outages and restarts lose nothing. 3 days). Relay outages and restarts lose nothing.
@ -89,6 +90,39 @@ of the messages. Messages that cannot be read are listed and counted, and the ex
It works whatever the list's archive policy is (the policy only governs live mail). The admin UI has the same import on It works whatever the list's archive policy is (the policy only governs live mail). The admin UI has the same import on
the Archive tab. Search is accent-insensitive: `caffe` finds "Caffè". the Archive tab. Search is accent-insensitive: `caffe` finds "Caffè".
## Outgoing mail: DKIM, SPF and DMARC
Parsimony rewrites `From` to the list address, so a signature of the original author's domain is of no use and
receivers that enforce DMARC need a signature of the **list domain**.
```
parsimony dkim keygen lists.example.org --selector mail --out /etc/parsimony/dkim-lists.pem
```
prints the DNS TXT record to publish and the config to add (the private key is written with mode 0600 and the
command never overwrites an existing key without `--force`):
```yaml
dkim:
- domain: lists.example.org
selector: mail
key_file: /etc/parsimony/dkim-lists.pem
```
Once the record is in DNS, `parsimony dkim check lists.example.org --key FILE --selector mail` looks it up and compares it
with the key. Both commands work offline of the API and need no token.
- A message is signed with the key of the domain in its From address (exact match, else a parent domain), at send time, so
the per-recipient `List-Unsubscribe` headers are covered. Relaxed/relaxed, SHA-256, no body length limit.
Signed: From, To, Cc, Subject, Date, Message-ID, In-Reply-To, References, Reply-To, MIME-Version, Content-Type,
Content-Transfer-Encoding and the `List-*` headers. A relay may add `Received` or rewrite other headers.
- A list on a domain without a key is sent unsigned. A broken key file stops the daemon at startup. If signing fails at
send time the error is logged and the message goes out unsigned: not delivering is worse than a missed signature.
- RSA (2048 bits recommended; PKCS#1 or PKCS#8 PEM) and Ed25519 keys are supported. Some receivers do not verify Ed25519 yet.
- Also publish SPF for the machine that relays your mail (for example `v=spf1 ip4:203.0.113.10 -all` on the list domain)
and a DMARC record (start with `_dmarc.lists.example.org TXT "v=DMARC1; p=none; rua=mailto:postmaster@example.org"`,
then tighten the policy once the reports look clean).
## Docker ## Docker
The image runs as an unprivileged user (uid 65532) and works with a read-only root filesystem. The image runs as an unprivileged user (uid 65532) and works with a read-only root filesystem.

View file

@ -11,7 +11,12 @@
package e2e package e2e
import ( import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/json" "encoding/json"
"encoding/pem"
"fmt" "fmt"
"io" "io"
"net" "net"
@ -24,6 +29,8 @@ import (
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/emersion/go-msgauth/dkim"
) )
const ( const (
@ -37,6 +44,7 @@ var (
postfixC = "pm-e2e-postfix-" + suffix postfixC = "pm-e2e-postfix-" + suffix
parsimonyC = "pm-e2e-parsimony-" + suffix parsimonyC = "pm-e2e-parsimony-" + suffix
smtpAddr string smtpAddr string
dkimPub *rsa.PublicKey // the list domain's signing key
apiBase string apiBase string
webBase string webBase string
) )
@ -84,6 +92,16 @@ func TestMain(m *testing.M) {
apiBase = fmt.Sprintf("http://127.0.0.1:%d", apiPort) apiBase = fmt.Sprintf("http://127.0.0.1:%d", apiPort)
webBase = fmt.Sprintf("http://127.0.0.1:%d", webPort) webBase = fmt.Sprintf("http://127.0.0.1:%d", webPort)
// A DKIM key for the list domain, mounted read-only into the container.
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
panic(err)
}
dkimPub = &key.PublicKey
keyFile := filepath.Join(os.TempDir(), "pm-e2e-dkim-"+suffix+".pem")
os.WriteFile(keyFile, pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}), 0o644)
defer os.Remove(keyFile)
cfg := filepath.Join(os.TempDir(), "pm-e2e-"+suffix+".yaml") cfg := filepath.Join(os.TempDir(), "pm-e2e-"+suffix+".yaml")
os.WriteFile(cfg, []byte(fmt.Sprintf(`db: /data/parsimony.db os.WriteFile(cfg, []byte(fmt.Sprintf(`db: /data/parsimony.db
base_url: %s base_url: %s
@ -97,6 +115,10 @@ api:
listen: 0.0.0.0:8080 listen: 0.0.0.0:8080
web: web:
listen: 0.0.0.0:8081 listen: 0.0.0.0:8081
dkim:
- domain: lists.example.test
selector: e2e
key_file: /etc/parsimony/dkim.pem
`, webBase)), 0o644) `, webBase)), 0o644)
defer os.Remove(cfg) defer os.Remove(cfg)
@ -104,7 +126,7 @@ web:
"-p", "127.0.0.1::25", "parsimony-e2e-postfix") "-p", "127.0.0.1::25", "parsimony-e2e-postfix")
mustDocker("run", "-d", "--name", parsimonyC, "--network", network, "--network-alias", "parsimony", mustDocker("run", "-d", "--name", parsimonyC, "--network", network, "--network-alias", "parsimony",
"--read-only", "--cap-drop", "ALL", "-e", "PARSIMONY_API_TOKEN="+token, "--read-only", "--cap-drop", "ALL", "-e", "PARSIMONY_API_TOKEN="+token,
"-v", cfg+":/etc/parsimony/parsimony.yaml:ro", "-v", cfg+":/etc/parsimony/parsimony.yaml:ro", "-v", keyFile+":/etc/parsimony/dkim.pem:ro",
"-p", fmt.Sprintf("127.0.0.1:%d:8080", apiPort), "-p", fmt.Sprintf("127.0.0.1:%d:8081", webPort), image) "-p", fmt.Sprintf("127.0.0.1:%d:8080", apiPort), "-p", fmt.Sprintf("127.0.0.1:%d:8081", webPort), image)
smtpAddr = mustDocker("port", postfixC, "25/tcp") smtpAddr = mustDocker("port", postfixC, "25/tcp")
smtpAddr = strings.Split(strings.Split(smtpAddr, "\n")[0], " ")[0] // "127.0.0.1:49153" smtpAddr = strings.Split(strings.Split(smtpAddr, "\n")[0], " ")[0] // "127.0.0.1:49153"
@ -263,6 +285,24 @@ func waitMail(t *testing.T, user, contains string) string {
return found return found
} }
// verifyDKIM checks the signatures of a delivered message against the test key.
func verifyDKIM(t *testing.T, raw string) []*dkim.Verification {
t.Helper()
der, _ := x509.MarshalPKIXPublicKey(dkimPub)
vs, err := dkim.VerifyWithOptions(strings.NewReader(raw), &dkim.VerifyOptions{
LookupTXT: func(name string) ([]string, error) {
if name == "e2e._domainkey."+domain {
return []string{"v=DKIM1; k=rsa; p=" + base64.StdEncoding.EncodeToString(der)}, nil
}
return nil, fmt.Errorf("no record %s", name)
},
})
if err != nil {
t.Fatalf("verify: %v", err)
}
return vs
}
func header(msg, name string) string { func header(msg, name string) string {
re := regexp.MustCompile(`(?im)^` + regexp.QuoteMeta(name) + `:[ \t]*(.*(?:\r?\n[ \t]+.*)*)`) re := regexp.MustCompile(`(?im)^` + regexp.QuoteMeta(name) + `:[ \t]*(.*(?:\r?\n[ \t]+.*)*)`)
if m := re.FindStringSubmatch(msg); m != nil { if m := re.FindStringSubmatch(msg); m != nil {
@ -608,6 +648,44 @@ func TestWholePath(t *testing.T) {
} }
}) })
t.Run("everything the list delivered is DKIM-signed and still verifies after Postfix", func(t *testing.T) {
checked := 0
for _, user := range []string{"alice", "bob", "carol"} {
for _, m := range mailbox(user) {
vs := verifyDKIM(t, m)
if len(vs) != 1 || vs[0].Err != nil || vs[0].Domain != domain {
t.Fatalf("%s: message %q is not validly signed by %s: %+v", user, header(m, "Subject"), domain, vs)
}
if from := header(m, "From"); !strings.Contains(from, "@"+domain) {
t.Fatalf("a signature for the list domain only makes sense when From is on it: %q", from)
}
checked++
}
}
if checked < 8 {
t.Fatalf("expected posts, confirmations and approved mail, checked only %d", checked)
}
// A copy changed after delivery must fail verification.
m := mailbox("bob")[0]
tampered := strings.Replace(m, "Subject: ", "Subject: [edited] ", 1)
if vs := verifyDKIM(t, tampered); len(vs) != 1 || vs[0].Err == nil {
t.Fatalf("a modified message must not verify: %+v", vs)
}
})
t.Run("dkim keygen works in the read-only container", func(t *testing.T) {
out, err := docker("exec", parsimonyC, "/parsimony", "dkim", "keygen", "keys.example.test", "--out", "/data/e2e-key.pem")
if err != nil || !strings.Contains(out, "mail._domainkey.keys.example.test.") || !strings.Contains(out, "v=DKIM1; k=rsa; p=") {
t.Fatalf("keygen: %v\n%s", err, out)
}
if out, err := docker("exec", parsimonyC, "/parsimony", "dkim", "keygen", "keys.example.test", "--out", "/data/e2e-key.pem"); err == nil || !strings.Contains(out, "already exists") {
t.Fatalf("must not overwrite: %v %s", err, out)
}
if out, err := docker("exec", parsimonyC, "/parsimony", "dkim", "record", "keys.example.test", "--key", "/data/e2e-key.pem"); err != nil || !strings.Contains(out, "v=DKIM1") {
t.Fatalf("record: %v %s", err, out)
}
})
t.Run("the queue ends empty", func(t *testing.T) { t.Run("the queue ends empty", func(t *testing.T) {
waitQueueEmpty(t) waitQueueEmpty(t)
}) })

4
go.mod
View file

@ -4,6 +4,7 @@ go 1.27.1
require ( require (
github.com/emersion/go-message v0.18.2 github.com/emersion/go-message v0.18.2
github.com/emersion/go-msgauth v0.7.0
github.com/emersion/go-smtp v0.25.1 github.com/emersion/go-smtp v0.25.1
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
modernc.org/sqlite v1.62.0 modernc.org/sqlite v1.62.0
@ -16,8 +17,9 @@ require (
github.com/mattn/go-isatty v0.0.24 // indirect github.com/mattn/go-isatty v0.0.24 // indirect
github.com/ncruces/go-strftime v1.1.0 // indirect github.com/ncruces/go-strftime v1.1.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/sys v0.49.0 // indirect golang.org/x/sys v0.49.0 // indirect
golang.org/x/text v0.14.0 // indirect golang.org/x/text v0.21.0 // indirect
modernc.org/libc v1.80.1 // indirect modernc.org/libc v1.80.1 // indirect
modernc.org/mathutil v1.7.1 // indirect modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.12.1 // indirect modernc.org/memory v1.12.1 // indirect

7
go.sum
View file

@ -2,6 +2,8 @@ github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2
github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI= github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI=
github.com/emersion/go-message v0.18.2 h1:rl55SQdjd9oJcIoQNhubD2Acs1E6IzlZISRTK7x/Lpg= github.com/emersion/go-message v0.18.2 h1:rl55SQdjd9oJcIoQNhubD2Acs1E6IzlZISRTK7x/Lpg=
github.com/emersion/go-message v0.18.2/go.mod h1:XpJyL70LwRvq2a8rVbHXikPgKj8+aI0kGdHlg16ibYA= github.com/emersion/go-message v0.18.2/go.mod h1:XpJyL70LwRvq2a8rVbHXikPgKj8+aI0kGdHlg16ibYA=
github.com/emersion/go-msgauth v0.7.0 h1:vj2hMn6KhFtW41kshIBTXvp6KgYSqpA/ZN9Pv4g1INc=
github.com/emersion/go-msgauth v0.7.0/go.mod h1:mmS9I6HkSovrNgq0HNXTeu8l3sRAAuQ9RMvbM4KU7Ck=
github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6 h1:oP4q0fw+fOSWn3DfFi4EXdT+B+gTtzx8GC9xsc26Znk= github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6 h1:oP4q0fw+fOSWn3DfFi4EXdT+B+gTtzx8GC9xsc26Znk=
github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ= github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-smtp v0.25.1 h1:y6nzefT6PtFeMItFw97GX/ZCkVbh0rqSXG8b3ang8/w= github.com/emersion/go-smtp v0.25.1 h1:y6nzefT6PtFeMItFw97GX/ZCkVbh0rqSXG8b3ang8/w=
@ -21,6 +23,8 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qq
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.42.0 h1:ICeL3t2L30ljSPHHxDnR4fqVcBt/zR2zJAb9juavmQg= golang.org/x/mod v0.42.0 h1:ICeL3t2L30ljSPHHxDnR4fqVcBt/zR2zJAb9juavmQg=
@ -49,8 +53,9 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=

View file

@ -17,6 +17,7 @@ import (
"codeberg.org/squidd/parsimony/internal/adminui" "codeberg.org/squidd/parsimony/internal/adminui"
"codeberg.org/squidd/parsimony/internal/api" "codeberg.org/squidd/parsimony/internal/api"
"codeberg.org/squidd/parsimony/internal/config" "codeberg.org/squidd/parsimony/internal/config"
"codeberg.org/squidd/parsimony/internal/dkimsign"
"codeberg.org/squidd/parsimony/internal/ingest" "codeberg.org/squidd/parsimony/internal/ingest"
"codeberg.org/squidd/parsimony/internal/outbound" "codeberg.org/squidd/parsimony/internal/outbound"
"codeberg.org/squidd/parsimony/internal/queue" "codeberg.org/squidd/parsimony/internal/queue"
@ -54,6 +55,24 @@ func (r releaser) Release(_ context.Context, id int64) error { return r.rt.Relea
// Start opens the database and begins serving. The REST API runs only when // Start opens the database and begins serving. The REST API runs only when
// apiToken is not empty. // apiToken is not empty.
func Start(cfg config.Config, apiToken string) (*App, error) { func Start(cfg config.Config, apiToken string) (*App, error) {
// Load the signing keys first: a broken key stops the daemon now, not at the first send.
var sign func([]byte) ([]byte, error)
if len(cfg.DKIM) > 0 {
keys := make([]dkimsign.Key, 0, len(cfg.DKIM))
for _, k := range cfg.DKIM {
key, err := dkimsign.LoadKey(k.Domain, k.Selector, k.KeyFile)
if err != nil {
return nil, fmt.Errorf("dkim key for %s: %w", k.Domain, err)
}
keys = append(keys, key)
}
signer, err := dkimsign.New(keys)
if err != nil {
return nil, err
}
sign = signer.Sign
}
st, err := store.Open(cfg.DB) st, err := store.Open(cfg.DB)
if err != nil { if err != nil {
return nil, fmt.Errorf("store: %w", err) return nil, fmt.Errorf("store: %w", err)
@ -67,6 +86,7 @@ func Start(cfg config.Config, apiToken string) (*App, error) {
Store: st, Store: st,
Sender: &outbound.SMTPRelay{Addr: cfg.Relay.Addr}, Sender: &outbound.SMTPRelay{Addr: cfg.Relay.Addr},
Concurrency: cfg.Relay.Concurrency, Concurrency: cfg.Relay.Concurrency,
Sign: sign,
} }
rt.OnEnqueue = worker.Wake rt.OnEnqueue = worker.Wake
a.lmtp = ingest.NewServer(rt) a.lmtp = ingest.NewServer(rt)

View file

@ -1,15 +1,24 @@
package app package app
import ( import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"io" "io"
"net" "net"
"net/http" "net/http"
"os"
"path/filepath"
"strings" "strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
"testing" "testing"
"time" "time"
"github.com/emersion/go-msgauth/dkim"
"github.com/emersion/go-smtp" "github.com/emersion/go-smtp"
"codeberg.org/squidd/parsimony/internal/config" "codeberg.org/squidd/parsimony/internal/config"
@ -521,3 +530,110 @@ func TestCloseInterruptsASlowSendWithoutLosingIt(t *testing.T) {
t.Fatalf("the interrupted delivery must stay queued: %+v", st) t.Fatalf("the interrupted delivery must stay queued: %+v", st)
} }
} }
// dkimKey writes a fresh RSA key as PKCS#1 PEM and returns the file and the public key.
func dkimKey(t *testing.T) (string, *rsa.PublicKey) {
t.Helper()
k, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
path := filepath.Join(t.TempDir(), "dkim.pem")
if err := os.WriteFile(path, pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(k)}), 0o600); err != nil {
t.Fatal(err)
}
return path, &k.PublicKey
}
func verifyDKIM(t *testing.T, raw string, selector, domain string, pub *rsa.PublicKey) []*dkim.Verification {
t.Helper()
der, _ := x509.MarshalPKIXPublicKey(pub)
vs, err := dkim.VerifyWithOptions(strings.NewReader(raw), &dkim.VerifyOptions{
LookupTXT: func(name string) ([]string, error) {
if name == selector+"._domainkey."+domain {
return []string{"v=DKIM1; k=rsa; p=" + base64.StdEncoding.EncodeToString(der)}, nil
}
return nil, errors.New("no record " + name)
},
})
if err != nil {
t.Fatalf("verify: %v", err)
}
return vs
}
func TestOutgoingMailIsSignedWhenAKeyIsConfigured(t *testing.T) {
relayAddr, relay := startSink(t)
keyFile, pub := dkimKey(t)
cfg := testConfig(t, relayAddr)
cfg.DKIM = []config.DKIMKey{{Domain: "example.org", Selector: "mail", KeyFile: keyFile}}
a, err := Start(cfg, "tok")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { a.Close() })
call(t, a, "POST", "/api/v1/lists", `{"name":"dev","domain":"example.org","title":"Dev"}`, "tok")
call(t, a, "POST", "/api/v1/lists/1/members", `{"address":"alice@example.com"}`, "tok")
// A post.
msg := "From: Alice <alice@example.com>\r\nSubject: signed post\r\nMessage-ID: <s1@x>\r\n\r\nbody\r\n"
if err := lmtpSend(t, a.LMTPAddr, "alice@example.com", "dev@example.org", msg); err != nil {
t.Fatal(err)
}
// A confirmation mail (system mail) from the same list domain.
if err := lmtpSend(t, a.LMTPAddr, "carol@x.com", "dev+join@example.org", ""); err != nil {
t.Fatal(err)
}
relay.waitCount(t, 2)
signed := 0
for _, m := range relay.all() {
vs := verifyDKIM(t, m.data, "mail", "example.org", pub)
if len(vs) != 1 || vs[0].Err != nil || vs[0].Domain != "example.org" {
t.Fatalf("every outgoing message is signed with the list domain: %+v\n%.300s", vs, m.data)
}
signed++
}
if signed != 2 {
t.Fatalf("signed %d", signed)
}
}
func TestMailIsUnsignedWithoutKeysAndForOtherDomains(t *testing.T) {
relayAddr, relay := startSink(t)
keyFile, _ := dkimKey(t)
cfg := testConfig(t, relayAddr)
cfg.DKIM = []config.DKIMKey{{Domain: "another.test", Selector: "mail", KeyFile: keyFile}}
a, err := Start(cfg, "tok")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { a.Close() })
call(t, a, "POST", "/api/v1/lists", `{"name":"dev","domain":"example.org"}`, "tok")
call(t, a, "POST", "/api/v1/lists/1/members", `{"address":"alice@example.com"}`, "tok")
msg := "From: Alice <alice@example.com>\r\nSubject: x\r\nMessage-ID: <u1@x>\r\n\r\nbody\r\n"
if err := lmtpSend(t, a.LMTPAddr, "alice@example.com", "dev@example.org", msg); err != nil {
t.Fatal(err)
}
relay.waitCount(t, 1)
if strings.Contains(relay.all()[0].data, "DKIM-Signature") {
t.Fatal("a list on a domain without a key is sent unsigned")
}
}
func TestStartFailsOnAnUnusableDKIMKey(t *testing.T) {
relayAddr, _ := startSink(t)
bad := filepath.Join(t.TempDir(), "bad.pem")
os.WriteFile(bad, []byte("not a key"), 0o600)
for name, keyFile := range map[string]string{"missing file": "/nonexistent/dkim.pem", "garbage": bad} {
cfg := testConfig(t, relayAddr)
cfg.DKIM = []config.DKIMKey{{Domain: "example.org", Selector: "mail", KeyFile: keyFile}}
a, err := Start(cfg, "tok")
if err == nil {
a.Close()
t.Fatalf("%s: a broken key must stop the daemon at startup, not later at send time", name)
}
if !strings.Contains(err.Error(), "dkim") || !strings.Contains(err.Error(), "example.org") {
t.Errorf("%s: the error names the key: %v", name, err)
}
}
}

View file

@ -51,6 +51,14 @@ Administration (talks to the REST API):
queue ls outbound queue queue ls outbound queue
queue rm <id> discard one queued delivery queue rm <id> discard one queued delivery
DKIM (works offline, needs no token):
dkim keygen <domain> [--selector mail] [--out FILE] [--force]
create a signing key and print the DNS record and config
dkim record <domain> --key FILE [--selector mail]
print the DNS record of an existing key
dkim check <domain> --key FILE [--selector mail]
look the record up in DNS and compare it with the key
Daemon: Daemon:
serve [-config FILE] run the mailing list server serve [-config FILE] run the mailing list server
@ -94,6 +102,7 @@ func Run(args []string, io IO, getenv func(string) string) int {
"held": {"ls": c.heldLs, "approve": c.heldApprove, "discard": c.heldDiscard}, "held": {"ls": c.heldLs, "approve": c.heldApprove, "discard": c.heldDiscard},
"archive": {"ls": c.archiveLs, "show": c.archiveShow, "rm": c.archiveRm, "import": c.archiveImport}, "archive": {"ls": c.archiveLs, "show": c.archiveShow, "rm": c.archiveRm, "import": c.archiveImport},
"queue": {"ls": c.queueLs, "rm": c.queueRm}, "queue": {"ls": c.queueLs, "rm": c.queueRm},
"dkim": {"keygen": c.dkimKeygen, "record": c.dkimRecord, "check": c.dkimCheck},
} }
group, ok := commands[rest[0]] group, ok := commands[rest[0]]
if !ok { if !ok {

View file

@ -3,17 +3,25 @@ package cli
import ( import (
"bytes" "bytes"
"context" "context"
"crypto/ed25519"
"crypto/rand"
"crypto/x509"
"encoding/json" "encoding/json"
"encoding/pem"
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
"path/filepath"
"regexp"
"strings" "strings"
"testing" "testing"
"time" "time"
"codeberg.org/squidd/parsimony/internal/api" "codeberg.org/squidd/parsimony/internal/api"
"codeberg.org/squidd/parsimony/internal/dkimsign"
"codeberg.org/squidd/parsimony/internal/store" "codeberg.org/squidd/parsimony/internal/store"
) )
@ -552,3 +560,219 @@ func TestArchiveImportSendsTheFileAsAnMbox(t *testing.T) {
t.Fatalf("content type %q, body unchanged: %v", contentType, body == cliMbox) t.Fatalf("content type %q, body unchanged: %v", contentType, body == cliMbox)
} }
} }
// --- dkim ----------------------------------------------------------------------
// noAuth runs a command that must not need the API token or URL.
func noAuth(t *testing.T, args ...string) (int, string, string) {
t.Helper()
var out, errb bytes.Buffer
code := Run(args, IO{In: strings.NewReader(""), Out: &out, Err: &errb}, func(string) string { return "" })
return code, out.String(), errb.String()
}
func TestDKIMKeygen(t *testing.T) {
dir := t.TempDir()
keyFile := filepath.Join(dir, "dkim.pem")
code, out, errs := noAuth(t, "dkim", "keygen", "example.org", "--out", keyFile)
if code != 0 {
t.Fatalf("exit %d: %s", code, errs)
}
info, err := os.Stat(keyFile)
if err != nil || info.Mode().Perm() != 0o600 {
t.Fatalf("the private key must be created with mode 0600: %v %v", err, info)
}
key, err := dkimsign.LoadKey("example.org", "mail", keyFile)
if err != nil {
t.Fatal(err)
}
want, _ := dkimsign.Record(key.Signer.Public())
for _, s := range []string{
"mail._domainkey.example.org.", "IN TXT", "Wrote private key to " + keyFile,
"dkim:", "domain: example.org", "selector: mail", "key_file: " + keyFile, want,
"parsimony dkim check example.org",
} {
if !strings.Contains(out, s) {
t.Errorf("output lacks %q:\n%s", s, out)
}
}
// The zone-file form is split in strings of at most 255 bytes.
for _, line := range strings.Split(out, "\n") {
if strings.Contains(line, "IN TXT") {
for _, q := range regexp.MustCompile(`"([^"]*)"`).FindAllStringSubmatch(line, -1) {
if len(q[1]) > 255 {
t.Fatalf("a TXT string of %d bytes exceeds the DNS limit", len(q[1]))
}
}
}
}
if strings.Contains(out, "PRIVATE KEY") {
t.Fatal("never print the private key")
}
}
func TestDKIMKeygenOptions(t *testing.T) {
dir := t.TempDir()
keyFile := filepath.Join(dir, "k.pem")
code, out, _ := noAuth(t, "dkim", "keygen", "lists.example.org", "--selector", "s2024", "--out", keyFile)
if code != 0 || !strings.Contains(out, "s2024._domainkey.lists.example.org.") || !strings.Contains(out, "selector: s2024") {
t.Fatalf("selector: %d %s", code, out)
}
first, _ := os.ReadFile(keyFile)
code, _, errs := noAuth(t, "dkim", "keygen", "lists.example.org", "--out", keyFile)
if code != 1 || !strings.Contains(errs, "already exists") {
t.Fatalf("an existing key must not be overwritten: %d %q", code, errs)
}
if again, _ := os.ReadFile(keyFile); !bytes.Equal(first, again) {
t.Fatal("the existing key was modified")
}
if code, _, errs := noAuth(t, "dkim", "keygen", "lists.example.org", "--out", keyFile, "--force"); code != 0 {
t.Fatalf("--force: %d %s", code, errs)
}
if replaced, _ := os.ReadFile(keyFile); bytes.Equal(first, replaced) {
t.Fatal("--force must replace the key")
}
if info, _ := os.Stat(keyFile); info.Mode().Perm() != 0o600 {
t.Fatalf("mode after --force: %v", info.Mode().Perm())
}
}
func TestDKIMKeygenDefaultFileName(t *testing.T) {
dir := t.TempDir()
old, _ := os.Getwd()
os.Chdir(dir)
defer os.Chdir(old)
if code, _, errs := noAuth(t, "dkim", "keygen", "example.org"); code != 0 {
t.Fatalf("%d %s", code, errs)
}
if _, err := os.Stat(filepath.Join(dir, "dkim-example.org.pem")); err != nil {
t.Fatalf("default file name: %v", err)
}
}
func TestDKIMUsageErrors(t *testing.T) {
for name, args := range map[string][]string{
"no domain": {"dkim", "keygen"},
"domain with @": {"dkim", "keygen", "a@example.org"},
"domain with space": {"dkim", "keygen", "my domain.org"},
"no dot": {"dkim", "keygen", "localhost"},
"bad selector": {"dkim", "keygen", "example.org", "--selector", "my sel"},
"bits not a number": {"dkim", "keygen", "example.org", "--bits", "abc"},
"record no key": {"dkim", "record", "example.org"},
"check no key": {"dkim", "check", "example.org"},
"unknown": {"dkim", "frobnicate"},
} {
if code, _, errs := noAuth(t, args...); code != 2 {
t.Errorf("%s: exit %d, want 2 (%q)", name, code, errs)
}
}
if code, _, errs := noAuth(t, "dkim", "keygen", "example.org", "--bits", "512", "--out", filepath.Join(t.TempDir(), "k")); code != 1 || !strings.Contains(errs, "1024") {
t.Fatalf("a tiny key is refused: %d %q", code, errs)
}
}
func writeKey(t *testing.T) (string, string) {
t.Helper()
dir := t.TempDir()
keyFile := filepath.Join(dir, "k.pem")
if code, _, errs := noAuth(t, "dkim", "keygen", "example.org", "--out", keyFile); code != 0 {
t.Fatalf("%d %s", code, errs)
}
key, _ := dkimsign.LoadKey("example.org", "mail", keyFile)
rec, _ := dkimsign.Record(key.Signer.Public())
return keyFile, rec
}
func TestDKIMRecord(t *testing.T) {
keyFile, rec := writeKey(t)
code, out, errs := noAuth(t, "dkim", "record", "example.org", "--key", keyFile)
if code != 0 || !strings.Contains(out, "mail._domainkey.example.org.") || !strings.Contains(out, rec) {
t.Fatalf("record: %d %q %q", code, out, errs)
}
code, out, _ = noAuth(t, "dkim", "record", "example.org", "--key", keyFile, "--selector", "x1")
if !strings.Contains(out, "x1._domainkey.example.org.") {
t.Fatalf("selector: %q", out)
}
if code, _, errs := noAuth(t, "dkim", "record", "example.org", "--key", "/nonexistent.pem"); code != 1 || errs == "" {
t.Fatalf("missing key: %d %q", code, errs)
}
}
func withDNS(t *testing.T, records map[string][]string) *[]string {
t.Helper()
var asked []string
old := lookupTXT
lookupTXT = func(name string) ([]string, error) {
asked = append(asked, name)
if r, ok := records[name]; ok {
return r, nil
}
return nil, errors.New("no such host")
}
t.Cleanup(func() { lookupTXT = old })
return &asked
}
func TestDKIMCheck(t *testing.T) {
keyFile, rec := writeKey(t)
asked := withDNS(t, map[string][]string{"mail._domainkey.example.org": {"v=spf1 -all", rec}})
code, out, errs := noAuth(t, "dkim", "check", "example.org", "--key", keyFile)
if code != 0 || !strings.Contains(out, "OK") || len(*asked) != 1 || (*asked)[0] != "mail._domainkey.example.org" {
t.Fatalf("matching record: %d %q %q asked=%v", code, out, errs, *asked)
}
withDNS(t, map[string][]string{"mail._domainkey.example.org": {"v=DKIM1; k=rsa; p=AAAAAAAA"}})
code, _, errs = noAuth(t, "dkim", "check", "example.org", "--key", keyFile)
if code != 1 || !strings.Contains(errs, "different key") {
t.Fatalf("mismatch: %d %q", code, errs)
}
withDNS(t, map[string][]string{})
code, _, errs = noAuth(t, "dkim", "check", "example.org", "--key", keyFile)
if code != 1 || !strings.Contains(errs, "no DKIM record") || !strings.Contains(errs, "mail._domainkey.example.org") {
t.Fatalf("missing: %d %q", code, errs)
}
withDNS(t, map[string][]string{"mail._domainkey.example.org": {"v=spf1 -all"}})
if code, _, errs = noAuth(t, "dkim", "check", "example.org", "--key", keyFile); code != 1 || !strings.Contains(errs, "no DKIM record") {
t.Fatalf("only unrelated TXT records: %d %q", code, errs)
}
asked = withDNS(t, map[string][]string{"s9._domainkey.example.org": {rec}})
if code, _, errs = noAuth(t, "dkim", "check", "example.org", "--key", keyFile, "--selector", "s9"); code != 0 {
t.Fatalf("selector: %d %q asked=%v", code, errs, *asked)
}
if code, _, errs = noAuth(t, "dkim", "check", "example.org", "--key", "/nonexistent.pem"); code != 1 {
t.Fatalf("missing key file: %d %q", code, errs)
}
}
func TestDKIMCheckEd25519Key(t *testing.T) {
pub, priv, _ := ed25519.GenerateKey(rand.Reader)
der, _ := x509.MarshalPKCS8PrivateKey(priv)
keyFile := filepath.Join(t.TempDir(), "ed.pem")
os.WriteFile(keyFile, pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: der}), 0o600)
rec, _ := dkimsign.Record(pub)
withDNS(t, map[string][]string{"mail._domainkey.example.org": {rec}})
if code, out, errs := noAuth(t, "dkim", "check", "example.org", "--key", keyFile); code != 0 || !strings.Contains(out, "OK") {
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)
}
}

185
internal/cli/dkim.go Normal file
View file

@ -0,0 +1,185 @@
package cli
import (
"errors"
"fmt"
"net"
"os"
"path/filepath"
"regexp"
"strings"
"codeberg.org/squidd/parsimony/internal/dkimsign"
)
// lookupTXT resolves DNS TXT records; tests replace it.
var lookupTXT = net.LookupTXT
var (
domainRe = regexp.MustCompile(`^[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?)+$`)
selectorRe = regexp.MustCompile(`^[A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?$`)
)
// dkimTarget parses "<domain> --selector S ..." and validates domain and selector.
type dkimTarget struct {
domain, selector string
}
func (t dkimTarget) recordName() string { return t.selector + "._domainkey." + t.domain }
// zoneLine is the TXT record as it is written in a DNS zone file.
func (t dkimTarget) zoneLine(value string) string {
quoted := make([]string, 0, 2)
for _, p := range dkimsign.SplitTXT(value) {
quoted = append(quoted, `"`+p+`"`)
}
return fmt.Sprintf("%s. IN TXT %s", t.recordName(), strings.Join(quoted, " "))
}
func (c *cmd) dkimKeygen(args []string) int {
fs := newFlagSet("dkim keygen")
selector := fs.String("selector", "mail", "")
out := fs.String("out", "", "")
bits := fs.Int("bits", 2048, "")
force := fs.Bool("force", false, "")
pos, err := parseArgs(fs, args)
if err != nil {
return c.usageError("%v", err)
}
t, code := c.dkimDomain("dkim keygen <domain> [--selector S] [--out FILE] [--bits N] [--force]", pos, *selector)
if code != 0 {
return code
}
path := *out
if path == "" {
path = "dkim-" + t.domain + ".pem"
}
if abs, err := filepath.Abs(path); err == nil {
path = abs
}
pemBytes, err := dkimsign.GenerateKeyPEM(*bits)
if err != nil {
return c.fail(err)
}
flags := os.O_WRONLY | os.O_CREATE | os.O_EXCL
if *force {
flags = os.O_WRONLY | os.O_CREATE | os.O_TRUNC
}
f, err := os.OpenFile(path, flags, 0o600)
if errors.Is(err, os.ErrExist) {
return c.fail(fmt.Errorf("%s already exists (use --force to replace it)", path))
}
if err != nil {
return c.fail(err)
}
if _, err := f.Write(pemBytes); err != nil {
f.Close()
return c.fail(err)
}
if err := f.Close(); err != nil {
return c.fail(err)
}
// An existing file keeps its old mode when truncated: make it private.
if err := os.Chmod(path, 0o600); err != nil {
return c.fail(err)
}
key, err := dkimsign.LoadKey(t.domain, t.selector, path)
if err != nil {
return c.fail(err)
}
record, err := dkimsign.Record(key.Signer.Public())
if err != nil {
return c.fail(err)
}
fmt.Fprintf(c.io.Out, "Wrote private key to %s (mode 0600)\n\n", path)
fmt.Fprintf(c.io.Out, "Publish this DNS TXT record:\n\n%s\n\n", t.zoneLine(record))
fmt.Fprintf(c.io.Out, "Value, for a DNS control panel that wants one string:\n\n%s\n\n", record)
fmt.Fprintf(c.io.Out, "Then add this to the Parsimony config and restart it:\n\ndkim:\n - domain: %s\n selector: %s\n key_file: %s\n\n", t.domain, t.selector, path)
fmt.Fprintf(c.io.Out, "Check the record once it is published:\n\n parsimony dkim check %s --key %s --selector %s\n", t.domain, path, t.selector)
return 0
}
// dkimKeyArgs parses "<domain> --key FILE [--selector S]" and loads the key.
func (c *cmd) dkimKeyArgs(name string, args []string) (dkimTarget, dkimsign.Key, int) {
fs := newFlagSet(name)
selector := fs.String("selector", "mail", "")
keyFile := fs.String("key", "", "")
pos, err := parseArgs(fs, args)
if err != nil {
return dkimTarget{}, dkimsign.Key{}, c.usageError("%v", err)
}
t, code := c.dkimDomain(name+" <domain> --key FILE [--selector S]", pos, *selector)
if code != 0 {
return t, dkimsign.Key{}, code
}
if *keyFile == "" {
return t, dkimsign.Key{}, c.usageError("usage: parsimony %s <domain> --key FILE [--selector S]", name)
}
key, err := dkimsign.LoadKey(t.domain, t.selector, *keyFile)
if err != nil {
return t, dkimsign.Key{}, c.fail(err)
}
return t, key, 0
}
// dkimDomain validates the positional domain and the selector.
func (c *cmd) dkimDomain(usage string, pos []string, selector string) (dkimTarget, int) {
if len(pos) != 1 {
return dkimTarget{}, c.usageError("usage: parsimony %s", usage)
}
if !domainRe.MatchString(pos[0]) {
return dkimTarget{}, c.usageError("%q is not a valid domain name", pos[0])
}
if !selectorRe.MatchString(selector) {
return dkimTarget{}, c.usageError("%q is not a valid selector", selector)
}
return dkimTarget{domain: strings.ToLower(pos[0]), selector: selector}, 0
}
func (c *cmd) dkimRecord(args []string) int {
t, key, code := c.dkimKeyArgs("dkim record", args)
if code != 0 {
return code
}
record, err := dkimsign.Record(key.Signer.Public())
if err != nil {
return c.fail(err)
}
fmt.Fprintf(c.io.Out, "%s\n\nValue:\n\n%s\n", t.zoneLine(record), record)
return 0
}
func (c *cmd) dkimCheck(args []string) int {
t, key, code := c.dkimKeyArgs("dkim check", args)
if code != 0 {
return code
}
want, err := dkimsign.Record(key.Signer.Public())
if err != nil {
return c.fail(err)
}
wantKey, _ := dkimsign.RecordKey(want)
txts, err := lookupTXT(t.recordName())
found := false
for _, txt := range txts {
got, ok := dkimsign.RecordKey(txt)
if !ok {
continue // not a DKIM record (no p= tag), for instance an SPF record
}
found = true
if got == wantKey {
fmt.Fprintf(c.io.Out, "OK: %s publishes the public key of this private key\n", t.recordName())
return 0
}
}
if !found {
reason := ""
if err != nil {
reason = fmt.Sprintf(" (%v)", err)
}
return c.fail(fmt.Errorf("no DKIM record at %s%s; publish the one from `parsimony dkim record`", t.recordName(), reason))
}
return c.fail(fmt.Errorf("%s publishes a different key than this private key: the record is stale or belongs to another key", t.recordName()))
}

View file

@ -5,6 +5,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"os" "os"
"strings"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -23,12 +24,41 @@ type Config struct {
Web struct { Web struct {
Listen string `yaml:"listen"` // public archive and unsubscribe pages; empty disables them Listen string `yaml:"listen"` // public archive and unsubscribe pages; empty disables them
} `yaml:"web"` } `yaml:"web"`
// DKIM lists the signing keys, one per domain. The private keys stay in files.
DKIM []DKIMKey `yaml:"dkim"`
Relay struct { Relay struct {
Addr string `yaml:"addr"` // host:port of the outgoing SMTP relay Addr string `yaml:"addr"` // host:port of the outgoing SMTP relay
Concurrency int `yaml:"concurrency"` // simultaneous deliveries Concurrency int `yaml:"concurrency"` // simultaneous deliveries
} `yaml:"relay"` } `yaml:"relay"`
} }
// DKIMKey is the signing key of one domain.
type DKIMKey struct {
Domain string `yaml:"domain"`
Selector string `yaml:"selector"`
KeyFile string `yaml:"key_file"` // PEM private key
}
func (c Config) validate() error {
seen := map[string]bool{}
for i, k := range c.DKIM {
switch {
case strings.TrimSpace(k.Domain) == "":
return fmt.Errorf("dkim[%d]: domain is required", i)
case strings.TrimSpace(k.Selector) == "":
return fmt.Errorf("dkim[%d] (%s): selector is required", i, k.Domain)
case strings.TrimSpace(k.KeyFile) == "":
return fmt.Errorf("dkim[%d] (%s): key_file is required", i, k.Domain)
}
d := strings.ToLower(k.Domain)
if seen[d] {
return fmt.Errorf("dkim[%d]: more than one key for %s", i, k.Domain)
}
seen[d] = true
}
return nil
}
// Load reads path and applies defaults. // Load reads path and applies defaults.
func Load(path string) (Config, error) { func Load(path string) (Config, error) {
var c Config var c Config
@ -49,5 +79,8 @@ func Load(path string) (Config, error) {
if err := dec.Decode(&c); err != nil { if err := dec.Decode(&c); err != nil {
return c, fmt.Errorf("parse %s: %w", path, err) return c, fmt.Errorf("parse %s: %w", path, err)
} }
if err := c.validate(); err != nil {
return c, fmt.Errorf("%s: %w", path, err)
}
return c, nil return c, nil
} }

View file

@ -3,6 +3,7 @@ package config
import ( import (
"os" "os"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
) )
@ -56,3 +57,35 @@ func TestWebCanBeDisabled(t *testing.T) {
t.Fatalf("an empty listen address disables the web: %q", c.Web.Listen) t.Fatalf("an empty listen address disables the web: %q", c.Web.Listen)
} }
} }
func TestLoadDKIMKeys(t *testing.T) {
c, err := Load(write(t, "dkim:\n - domain: lists.example.org\n selector: mail\n key_file: /run/secrets/dkim.pem\n - domain: news.example.org\n selector: s2\n key_file: keys/news.pem\n"))
if err != nil {
t.Fatal(err)
}
if len(c.DKIM) != 2 || c.DKIM[0].Domain != "lists.example.org" || c.DKIM[0].Selector != "mail" || c.DKIM[0].KeyFile != "/run/secrets/dkim.pem" ||
c.DKIM[1].KeyFile != "keys/news.pem" {
t.Fatalf("dkim: %+v", c.DKIM)
}
if c, _ := Load(write(t, "{}\n")); len(c.DKIM) != 0 {
t.Fatalf("no keys by default: %+v", c.DKIM)
}
}
func TestLoadRejectsIncompleteOrDuplicateDKIMKeys(t *testing.T) {
for name, y := range map[string]string{
"missing domain": "dkim:\n - selector: mail\n key_file: k.pem\n",
"missing selector": "dkim:\n - domain: a.org\n key_file: k.pem\n",
"missing key file": "dkim:\n - domain: a.org\n selector: mail\n",
"duplicate domain": "dkim:\n - {domain: a.org, selector: x, key_file: k1.pem}\n - {domain: A.ORG, selector: y, key_file: k2.pem}\n",
"inline key": "dkim:\n - {domain: a.org, selector: x, key_file: k.pem, private_key: SECRET}\n",
} {
if _, err := Load(write(t, y)); err == nil {
t.Errorf("%s: want an error", name)
}
}
_, err := Load(write(t, "dkim:\n - selector: mail\n key_file: k.pem\n"))
if err == nil || !strings.Contains(err.Error(), "dkim[0]") {
t.Fatalf("the error names the entry: %v", err)
}
}

View file

@ -0,0 +1,228 @@
// Package dkimsign signs outgoing messages with DKIM.
//
// The signing domain comes from the address in the message's From header: a key
// for that exact domain, else a key for one of its parent domains (relaxed
// alignment). A message from a domain without a key is left unsigned.
package dkimsign
import (
"bytes"
"crypto"
"crypto/ed25519"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"net/mail"
"os"
"regexp"
"strings"
"github.com/emersion/go-msgauth/dkim"
)
// headerKeys are the header fields covered by the signature. The list headers
// are included so a recipient can trust the unsubscribe links. Received and
// Return-Path are left out: MTAs add them on the way.
var headerKeys = []string{
"From", "To", "Cc", "Subject", "Date", "Message-ID", "In-Reply-To", "References", "Reply-To",
"MIME-Version", "Content-Type", "Content-Transfer-Encoding",
"List-Id", "List-Post", "List-Unsubscribe", "List-Unsubscribe-Post",
}
var selectorRe = regexp.MustCompile(`^[A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?$`)
// Key is a signing key for one domain.
type Key struct {
Domain string
Selector string
Signer crypto.Signer // *rsa.PrivateKey or ed25519.PrivateKey
}
// Signer signs messages with the key that matches their From domain.
type Signer struct {
keys map[string]Key // by lowercase domain
}
// New builds a Signer. Domains must be unique.
func New(keys []Key) (*Signer, error) {
s := &Signer{keys: map[string]Key{}}
for _, k := range keys {
k.Domain = strings.ToLower(strings.TrimSuffix(strings.TrimSpace(k.Domain), "."))
switch {
case k.Domain == "":
return nil, errors.New("dkim: a key needs a domain")
case !selectorRe.MatchString(k.Selector):
return nil, fmt.Errorf("dkim: invalid selector %q for %s", k.Selector, k.Domain)
case k.Signer == nil:
return nil, fmt.Errorf("dkim: no private key for %s", k.Domain)
}
switch k.Signer.Public().(type) {
case *rsa.PublicKey, ed25519.PublicKey:
default:
return nil, fmt.Errorf("dkim: unsupported key type for %s", k.Domain)
}
if _, dup := s.keys[k.Domain]; dup {
return nil, fmt.Errorf("dkim: more than one key for %s", k.Domain)
}
s.keys[k.Domain] = k
}
return s, nil
}
// LoadKey reads a PEM private key file (PKCS#1 or PKCS#8; RSA or Ed25519).
func LoadKey(domain, selector, path string) (Key, error) {
raw, err := os.ReadFile(path)
if err != nil {
return Key{}, err
}
block, _ := pem.Decode(raw)
if block == nil {
return Key{}, fmt.Errorf("%s: not a PEM file", path)
}
var priv crypto.Signer
switch block.Type {
case "RSA PRIVATE KEY":
k, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return Key{}, fmt.Errorf("%s: invalid RSA private key", path)
}
priv = k
case "PRIVATE KEY":
k, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return Key{}, fmt.Errorf("%s: invalid PKCS#8 private key", path)
}
switch k := k.(type) {
case *rsa.PrivateKey:
priv = k
case ed25519.PrivateKey:
priv = k
default:
return Key{}, fmt.Errorf("%s: unsupported key type %T (use RSA or Ed25519)", path, k)
}
default:
return Key{}, fmt.Errorf("%s: unsupported PEM type %q", path, block.Type)
}
return Key{Domain: domain, Selector: selector, Signer: priv}, nil
}
// fromDomain returns the lowercase domain of the first address in From.
func fromDomain(msg []byte) (string, bool) {
m, err := mail.ReadMessage(bytes.NewReader(msg))
if err != nil {
return "", false
}
addrs, err := m.Header.AddressList("From")
if err != nil || len(addrs) == 0 {
return "", false
}
at := strings.LastIndex(addrs[0].Address, "@")
if at < 0 || at == len(addrs[0].Address)-1 {
return "", false
}
return strings.ToLower(addrs[0].Address[at+1:]), true
}
func (s *Signer) keyFor(domain string) (Key, bool) {
for {
if k, ok := s.keys[domain]; ok {
return k, true
}
i := strings.Index(domain, ".")
if i < 0 {
return Key{}, false
}
domain = domain[i+1:]
}
}
// Sign returns msg with a DKIM-Signature header in front. When no key applies
// (or the message has no usable From) it returns msg unchanged and no error.
func (s *Signer) Sign(msg []byte) ([]byte, error) {
domain, ok := fromDomain(msg)
if !ok {
return msg, nil
}
key, ok := s.keyFor(domain)
if !ok {
return msg, nil
}
var out bytes.Buffer
err := dkim.Sign(&out, bytes.NewReader(msg), &dkim.SignOptions{
Domain: key.Domain,
Selector: key.Selector,
Signer: key.Signer,
Hash: crypto.SHA256,
HeaderCanonicalization: dkim.CanonicalizationRelaxed,
BodyCanonicalization: dkim.CanonicalizationRelaxed,
HeaderKeys: headerKeys,
})
if err != nil {
return msg, fmt.Errorf("dkim sign for %s: %w", key.Domain, err)
}
return out.Bytes(), nil
}
// GenerateKeyPEM creates an RSA private key and returns it as PKCS#8 PEM. Use
// 2048 bits: every receiver verifies it and it fits a DNS record.
func GenerateKeyPEM(bits int) ([]byte, error) {
if bits < 1024 {
return nil, fmt.Errorf("RSA keys under 1024 bits are not accepted (use 2048)")
}
k, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, err
}
der, err := x509.MarshalPKCS8PrivateKey(k)
if err != nil {
return nil, err
}
return pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: der}), nil
}
// Record returns the DKIM DNS TXT value that publishes a public key.
func Record(pub crypto.PublicKey) (string, error) {
switch k := pub.(type) {
case *rsa.PublicKey:
der, err := x509.MarshalPKIXPublicKey(k)
if err != nil {
return "", err
}
return "v=DKIM1; k=rsa; p=" + base64.StdEncoding.EncodeToString(der), nil
case ed25519.PublicKey:
return "v=DKIM1; k=ed25519; p=" + base64.StdEncoding.EncodeToString(k), nil
}
return "", fmt.Errorf("unsupported public key type %T", pub)
}
// RecordKey extracts the base64 public key (the p= tag, whitespace removed)
// from a DKIM TXT value.
func RecordKey(txt string) (string, bool) {
for _, tag := range strings.Split(txt, ";") {
name, value, ok := strings.Cut(strings.TrimSpace(tag), "=")
if ok && strings.TrimSpace(name) == "p" {
key := strings.Join(strings.Fields(value), "")
return key, key != ""
}
}
return "", false
}
// SplitTXT cuts a TXT value into the strings of at most 255 bytes that a DNS
// record is made of.
func SplitTXT(value string) []string {
const max = 255
var parts []string
for len(value) > max {
parts = append(parts, value[:max])
value = value[max:]
}
if value != "" {
parts = append(parts, value)
}
return parts
}

View file

@ -0,0 +1,444 @@
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")
}
}

View file

@ -52,6 +52,9 @@ type Worker struct {
Batch int // deliveries taken per run Batch int // deliveries taken per run
Timeout time.Duration // per send Timeout time.Duration // per send
Interval time.Duration // idle polling, to pick up retries that have become due Interval time.Duration // idle polling, to pick up retries that have become due
// Sign, when set, signs the final message (DKIM). On error the message is sent unsigned:
// not delivering is worse than a missed signature.
Sign func(msg []byte) ([]byte, error)
wakeOnce sync.Once wakeOnce sync.Once
wake chan struct{} wake chan struct{}
@ -165,6 +168,13 @@ func (w *Worker) deliver(ctx context.Context, it store.OutboxItem) Stats {
} }
msg := make([]byte, 0, len(it.Prefix)+len(data)) msg := make([]byte, 0, len(it.Prefix)+len(data))
msg = append(append(msg, it.Prefix...), data...) msg = append(append(msg, it.Prefix...), data...)
if w.Sign != nil {
if signed, err := w.Sign(msg); err != nil {
log.Printf("queue: sign message for %s: %v (sending unsigned)", it.Rcpt, err)
} else {
msg = signed
}
}
timeout := w.Timeout timeout := w.Timeout
if timeout <= 0 { if timeout <= 0 {

View file

@ -447,3 +447,55 @@ func TestIdleRunPollsForDueRetries(t *testing.T) {
e.clk.set(t0.Add(2 * time.Minute)) // the backoff has passed; no Wake needed e.clk.set(t0.Add(2 * time.Minute)) // the backoff has passed; no Wake needed
waitFor(t, "retry by polling", func() bool { return e.snd.count() == 1 }) waitFor(t, "retry by polling", func() bool { return e.snd.count() == 1 })
} }
func TestSignHookSeesTheFinalMessageIncludingThePerRecipientHeaders(t *testing.T) {
e := setup(t)
e.enqueue(t, 1)
var seen []byte
e.w.Sign = func(msg []byte) ([]byte, error) {
seen = append([]byte(nil), msg...)
return append([]byte("X-Signed: yes\r\n"), msg...), nil
}
if st, _ := e.w.RunOnce(context.Background()); st.Sent != 1 {
t.Fatalf("%+v", st)
}
if string(seen) != "X-Rcpt: a\r\nSubject: hi\r\n\r\nbody\r\n" {
t.Fatalf("the signer must see prefix + message, so List-Unsubscribe is covered: %q", seen)
}
if string(e.snd.sent[0].Data) != "X-Signed: yes\r\nX-Rcpt: a\r\nSubject: hi\r\n\r\nbody\r\n" {
t.Fatalf("the relay must get the signed message: %q", e.snd.sent[0].Data)
}
}
func TestSigningFailureSendsTheMessageUnsigned(t *testing.T) {
e := setup(t)
e.enqueue(t, 1)
e.w.Sign = func(msg []byte) ([]byte, error) { return nil, errors.New("hsm unavailable") }
st, _ := e.w.RunOnce(context.Background())
if st.Sent != 1 || e.pending(t) != 0 {
t.Fatalf("a signing error must not stop delivery: %+v", st)
}
if string(e.snd.sent[0].Data) != "X-Rcpt: a\r\nSubject: hi\r\n\r\nbody\r\n" {
t.Fatalf("unsigned message expected: %q", e.snd.sent[0].Data)
}
}
func TestEachAttemptIsSignedAgain(t *testing.T) {
e := setup(t)
e.enqueue(t, 1)
calls := 0
e.w.Sign = func(msg []byte) ([]byte, error) { calls++; return msg, nil }
failures := int32(1)
e.snd.fn = func(context.Context, string, []string) error {
if atomic.AddInt32(&failures, -1) >= 0 {
return errors.New("down")
}
return nil
}
e.w.RunOnce(context.Background())
e.clk.set(t0.Add(2 * time.Minute))
e.w.RunOnce(context.Background())
if calls != 2 {
t.Fatalf("a retry is signed afresh (new timestamp), signing calls = %d", calls)
}
}

View file

@ -11,3 +11,9 @@ web:
api: api:
listen: 0.0.0.0:8080 listen: 0.0.0.0:8080
# The API token is read from the PARSIMONY_API_TOKEN environment variable. # The API token is read from the PARSIMONY_API_TOKEN environment variable.
# Sign outgoing mail with DKIM, one key per domain. The private key stays in a file
# (create it with: parsimony dkim keygen lists.example.org --selector mail).
# dkim:
# - domain: lists.example.org
# selector: mail
# key_file: /etc/parsimony/dkim-lists.pem