implement validation for ML-DSA-44

This commit is contained in:
Jonas Leder 2026-11-16 11:06:25 +01:00
commit 0d7eec9746
4 changed files with 159 additions and 3 deletions

View file

@ -106,7 +106,7 @@ dashboard filters on a `zone` label, which the chart's ServiceMonitor adds; with
## Limitations / roadmap
* DNSSEC: wildcard-denial parts of NXDOMAIN proofs are not verified (covering/closest-encloser records are); algorithms
beyond RSA/ECDSA/Ed25519 are treated as insecure.
beyond RSA/ECDSA/Ed25519/ML-DSA-44 are treated as insecure.
* No DoQ listener (DoT/DoH listeners and DoT upstream are supported), no per-client groups, no query log / top-domain lists
(counters only, to keep Prometheus cardinality bounded), no QNAME minimisation, no hot-reload of the YAML
(pods roll on change; API changes are live).

109
internal/dnssec/mldsa.go Normal file
View file

@ -0,0 +1,109 @@
package dnssec
import (
"bytes"
"crypto/mldsa"
"encoding/base64"
"errors"
"sort"
"strings"
"github.com/miekg/dns"
)
// algMLDSA44 is ML-DSA-44 per draft-westerbaan-dnssec-mldsa (number pending IANA);
// miekg/dns does not know it, so it is verified here.
const algMLDSA44 uint8 = 18
// verifyMLDSA verifies sig over rrset with k (pure ML-DSA-44, empty context).
// shortcut: canonical lowercasing covers NS/CNAME/PTR/DNAME/MX/SRV/SOA only; add types if signed data mismatches.
func verifyMLDSA(sig *dns.RRSIG, k *dns.DNSKEY, rrset []dns.RR) error {
if len(rrset) == 0 || k.Protocol != 3 || k.Flags&dns.ZONE == 0 || sig.Algorithm != algMLDSA44 || k.Algorithm != algMLDSA44 {
return dns.ErrKey
}
h0 := rrset[0].Header()
if h0.Rrtype != sig.TypeCovered || h0.Class != sig.Hdr.Class || !dns.IsRRset(rrset) {
return dns.ErrRRset
}
keyb, err := base64.StdEncoding.DecodeString(k.PublicKey)
if err != nil {
return err
}
sigb, err := base64.StdEncoding.DecodeString(sig.Signature)
if err != nil {
return err
}
pub, err := mldsa.NewPublicKey(mldsa.MLDSA44(), keyb)
if err != nil {
return err
}
data, err := mldsaSignedData(sig, rrset)
if err != nil {
return err
}
if len(sigb) != mldsa.MLDSA44().SignatureSize() {
return errors.New("bad ML-DSA-44 signature length")
}
return mldsa.Verify(pub, data, sigb, nil)
}
// mldsaSignedData builds the RFC 4035 §5.3.2 signed data for sig over rrset.
func mldsaSignedData(sig *dns.RRSIG, rrset []dns.RR) ([]byte, error) {
// RRSIG RDATA without signature: pack a header-less (root-owner) RRSIG, drop the 11-byte header.
hdr := *sig
hdr.Hdr = dns.RR_Header{Name: ".", Rrtype: dns.TypeRRSIG, Class: dns.ClassINET}
hdr.SignerName = dns.CanonicalName(sig.SignerName)
hdr.Signature = ""
buf := make([]byte, 512)
n, err := dns.PackRR(&hdr, buf, 0, nil, false)
if err != nil {
return nil, err
}
data := append([]byte(nil), buf[11:n]...)
wires := make([][]byte, len(rrset))
for i, r := range rrset {
c := dns.Copy(r)
h := c.Header()
h.Ttl = sig.OrigTtl
if labels := dns.SplitDomainName(h.Name); len(labels) > int(sig.Labels) {
h.Name = "*." + strings.Join(labels[len(labels)-int(sig.Labels):], ".") + "."
}
h.Name = dns.CanonicalName(h.Name)
switch x := c.(type) {
case *dns.NS:
x.Ns = dns.CanonicalName(x.Ns)
case *dns.CNAME:
x.Target = dns.CanonicalName(x.Target)
case *dns.PTR:
x.Ptr = dns.CanonicalName(x.Ptr)
case *dns.DNAME:
x.Target = dns.CanonicalName(x.Target)
case *dns.MX:
x.Mx = dns.CanonicalName(x.Mx)
case *dns.SRV:
x.Target = dns.CanonicalName(x.Target)
case *dns.SOA:
x.Ns, x.Mbox = dns.CanonicalName(x.Ns), dns.CanonicalName(x.Mbox)
}
w := make([]byte, dns.Len(c)+1)
off, err := dns.PackRR(c, w, 0, nil, false)
if err != nil {
return nil, err
}
wires[i] = w[:off]
}
rdata := func(w []byte) []byte { // skip owner name + type/class/ttl/rdlen
_, off, _ := dns.UnpackDomainName(w, 0)
return w[off+10:]
}
sort.Slice(wires, func(i, j int) bool { return bytes.Compare(rdata(wires[i]), rdata(wires[j])) < 0 })
for i, w := range wires {
if i == 0 || !bytes.Equal(w, wires[i-1]) {
data = append(data, w...)
}
}
return data, nil
}

View file

@ -0,0 +1,43 @@
package dnssec
import (
"crypto/mldsa"
"encoding/base64"
"testing"
"time"
"github.com/miekg/dns"
)
func TestVerifyMLDSA(t *testing.T) {
sk, err := mldsa.GenerateKey(mldsa.MLDSA44())
if err != nil {
t.Fatal(err)
}
k := &dns.DNSKEY{Hdr: dns.RR_Header{Name: "example.", Rrtype: dns.TypeDNSKEY, Class: dns.ClassINET, Ttl: 300},
Flags: 257, Protocol: 3, Algorithm: algMLDSA44, PublicKey: base64.StdEncoding.EncodeToString(sk.PublicKey().Bytes())}
rrset := []dns.RR{
&dns.NS{Hdr: dns.RR_Header{Name: "Example.", Rrtype: dns.TypeNS, Class: dns.ClassINET, Ttl: 300}, Ns: "B.example."},
&dns.NS{Hdr: dns.RR_Header{Name: "Example.", Rrtype: dns.TypeNS, Class: dns.ClassINET, Ttl: 300}, Ns: "a.example."},
}
now := uint32(time.Now().Unix())
sig := &dns.RRSIG{Hdr: dns.RR_Header{Name: "example.", Rrtype: dns.TypeRRSIG, Class: dns.ClassINET, Ttl: 300},
TypeCovered: dns.TypeNS, Algorithm: algMLDSA44, Labels: 1, OrigTtl: 300, Expiration: now + 3600, Inception: now - 3600,
KeyTag: k.KeyTag(), SignerName: "example."}
data, err := mldsaSignedData(sig, rrset)
if err != nil {
t.Fatal(err)
}
raw, err := sk.Sign(nil, data, &mldsa.Options{})
if err != nil {
t.Fatal(err)
}
sig.Signature = base64.StdEncoding.EncodeToString(raw)
if err := verifyMLDSA(sig, k, rrset); err != nil {
t.Fatal(err)
}
rrset[0].(*dns.NS).Ns = "evil.example."
if verifyMLDSA(sig, k, rrset) == nil {
t.Fatal("tampered rrset accepted")
}
}

View file

@ -95,7 +95,7 @@ func New(fetch Fetcher, anchors []string) (*Validator, error) {
var supportedAlg = map[uint8]bool{
dns.RSASHA1: true, dns.RSASHA1NSEC3SHA1: true, dns.RSASHA256: true, dns.RSASHA512: true,
dns.ECDSAP256SHA256: true, dns.ECDSAP384SHA384: true, dns.ED25519: true,
dns.ECDSAP256SHA256: true, dns.ECDSAP384SHA384: true, dns.ED25519: true, algMLDSA44: true,
}
func parentOf(name string) string {
@ -312,7 +312,11 @@ func (v *Validator) verifyRRset(rrset []dns.RR, sigs []*dns.RRSIG, z *zoneInfo)
if k.KeyTag() != sig.KeyTag || k.Algorithm != sig.Algorithm {
continue
}
if err := sig.Verify(k, rrset); err != nil {
verify := sig.Verify
if sig.Algorithm == algMLDSA44 {
verify = func(k *dns.DNSKEY, rrs []dns.RR) error { return verifyMLDSA(sig, k, rrs) }
}
if err := verify(k, rrset); err != nil {
last = err
continue
}