Sign responses to TSIG-signed UPDATEs (RFC 8945 §5.4.2)

When a request arrives with TSIG, attach a TSIG record to the response
so dns.ResponseWriter computes the MAC at write time using the secret
in TsigSecret. Without this, BIND nsupdate complains "expected a TSIG
or SIG(0)" on every UPDATE, even when the update applies successfully.

Two response paths fixed:
  - handleUpdate success/per-rcode replies (update.go)
  - ServeDNS rejection when TSIG verification fails (plugin.go)

The new helper in tsig.go is a no-op for unsigned requests. Unknown
keys still silently skip signing — we can't authenticate to a peer we
don't share a key with.

Tests verify both branches: signed request → response carries matching
TSIG (key name + algorithm); unsigned request → response stays plain.
This commit is contained in:
Ryan Malloy 2026-05-22 09:24:12 -06:00
parent 1fe95e3f6c
commit 6268e6eafd
4 changed files with 91 additions and 0 deletions

27
tsig.go
View file

@ -3,10 +3,37 @@ package rfc2136
import (
"fmt"
"strings"
"time"
"github.com/miekg/dns"
)
// tsigResponseFudge is the time tolerance (seconds) embedded in
// responses we sign. RFC 8945 §10 suggests 300s; we mirror that.
const tsigResponseFudge = 300
// signResponseIfSigned attaches a TSIG record to resp using the
// request's key name and algorithm. This causes the downstream
// dns.ResponseWriter to compute and serialize the MAC at WriteMsg
// time (using the secret from the server's TsigSecret map, which
// setup.go populated). Per RFC 8945 §5.4.2, the response to a
// TSIG-signed message MUST itself be signed if the server knows the
// key — otherwise the client cannot authenticate the answer and
// rejects it with "expected a TSIG or SIG(0)" (BIND nsupdate's exact
// complaint).
//
// If the request was not TSIG-signed, this is a no-op. If the key is
// not in the server's TsigSecret map (e.g. unknown key), miekg/dns
// will skip signing at write time and the response goes back
// unsigned — that's the correct shape for "I don't have your key."
func signResponseIfSigned(resp, req *dns.Msg) {
tsig := req.IsTsig()
if tsig == nil {
return
}
resp.SetTsig(tsig.Hdr.Name, tsig.Algorithm, tsigResponseFudge, time.Now().Unix())
}
// checkTSIG verifies that the incoming UPDATE message is properly signed
// with a TSIG key we know about. The actual signature math has already
// been done by the underlying dns.Server (because setup.go registered