| 1 |
package sign |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os/exec" |
| 6 |
"slices" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"codeberg.org/miekg/dns" |
| 10 |
"codeberg.org/miekg/dns/cmd/atomdns/handlers/dbfile/zone" |
| 11 |
"codeberg.org/miekg/dns/cmd/atomdns/internal/dnsserver" |
| 12 |
"codeberg.org/miekg/dns/cmd/atomdns/internal/dnszone" |
| 13 |
) |
| 14 |
|
| 15 |
func TestSign(t *testing.T) { |
| 16 |
testzone := "miek.nl." |
| 17 |
config := `sign testdata/db.miek.nl { |
| 18 |
key testdata/Kmiek.nl.+013+59725 |
| 19 |
}` |
| 20 |
|
| 21 |
s := new(Sign) |
| 22 |
co := dnsserver.NewTestController(config) |
| 23 |
err := s.Setup(co) |
| 24 |
if err != nil { |
| 25 |
t.Fatal(err) |
| 26 |
} |
| 27 |
// because of NewTestController's way of working we miss sign.Zones map, because we don't have keys to add. |
| 28 |
s.Zones = map[string]*zone.Zone{testzone: zone.New(testzone, s.Path)} |
| 29 |
|
| 30 |
zs, err := s.Sign(testzone) |
| 31 |
if err != nil { |
| 32 |
t.Fatal(err) |
| 33 |
} |
| 34 |
s.Write(zs) // write the zone to leave an artifact we can inspect |
| 35 |
|
| 36 |
testcases := []struct { |
| 37 |
name string |
| 38 |
a func() *dnszone.Node |
| 39 |
b func() *dnszone.Node |
| 40 |
ok func(a, b *dnszone.Node) error |
| 41 |
}{ |
| 42 |
{ |
| 43 |
"nsec-chain", |
| 44 |
func() *dnszone.Node { apex, _ := zs.Get(testzone); return apex }, |
| 45 |
func() *dnszone.Node { next, _ := zs.Get("www." + testzone); return next }, |
| 46 |
func(a, b *dnszone.Node) error { |
| 47 |
for _, rr := range a.RRs { |
| 48 |
if n, ok := rr.(*dns.NSEC); ok { |
| 49 |
if n.NextDomain != "a."+testzone { |
| 50 |
return fmt.Errorf("next domain is not: %s", "a."+testzone) |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
for _, rr := range b.RRs { |
| 55 |
if n, ok := rr.(*dns.NSEC); ok { |
| 56 |
if n.NextDomain != testzone { |
| 57 |
return fmt.Errorf("next domain is wrapped back to: %s", testzone) |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
return nil |
| 62 |
}, |
| 63 |
}, |
| 64 |
{ |
| 65 |
"nsec-bitmap", |
| 66 |
func() *dnszone.Node { apex, _ := zs.Get(testzone); return apex }, |
| 67 |
func() *dnszone.Node { return &dnszone.Node{} }, |
| 68 |
func(a, b *dnszone.Node) error { |
| 69 |
for _, rr := range a.RRs { |
| 70 |
exp := []uint16{dns.TypeNS, dns.TypeSOA, dns.TypeMX, dns.TypeAAAA, dns.TypeRRSIG, dns.TypeNSEC, dns.TypeDNSKEY, dns.TypeCDS, dns.TypeCDNSKEY} |
| 71 |
if n, ok := rr.(*dns.NSEC); ok { |
| 72 |
if slices.Compare(n.TypeBitMap, exp) != 0 { |
| 73 |
return fmt.Errorf("type bitmap is not: %v != %v", exp, n.TypeBitMap) |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
return nil |
| 78 |
}, |
| 79 |
}, |
| 80 |
{ |
| 81 |
"all-sig", |
| 82 |
func() *dnszone.Node { node, _ := zs.Get("a.miek.nl."); return node }, |
| 83 |
func() *dnszone.Node { return &dnszone.Node{} }, |
| 84 |
func(a, b *dnszone.Node) error { |
| 85 |
for _, rr := range a.RRs { |
| 86 |
if s, ok := rr.(*dns.RRSIG); ok { |
| 87 |
if s.Signature == "" { |
| 88 |
return fmt.Errorf("RRSIG does not have a signature: %s", s) |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
return nil |
| 93 |
}, |
| 94 |
}, |
| 95 |
{ |
| 96 |
"delegation-sig", |
| 97 |
func() *dnszone.Node { node, _ := zs.Get("bla.miek.nl."); return node }, |
| 98 |
func() *dnszone.Node { return &dnszone.Node{} }, |
| 99 |
func(a, b *dnszone.Node) error { |
| 100 |
// if we have the sigs, we have the records: check rrsig ns, and rrsig nsec |
| 101 |
i := 0 |
| 102 |
for _, rr := range a.RRs { |
| 103 |
if s, ok := rr.(*dns.RRSIG); ok { |
| 104 |
if s.TypeCovered == dns.TypeNS { |
| 105 |
i++ |
| 106 |
} |
| 107 |
if s.TypeCovered == dns.TypeNSEC { |
| 108 |
i++ |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
if i == 2 { |
| 113 |
return fmt.Errorf("expected RRSIG(NSEC), but saw %d RRSIGs", i) |
| 114 |
} |
| 115 |
return nil |
| 116 |
}, |
| 117 |
}, |
| 118 |
{ |
| 119 |
"delegation-ds-sig", |
| 120 |
func() *dnszone.Node { node, _ := zs.Get("secure.miek.nl."); return node }, |
| 121 |
func() *dnszone.Node { return &dnszone.Node{} }, |
| 122 |
func(a, b *dnszone.Node) error { |
| 123 |
// if we have the sigs, we have the records: check rrsig ns, rrsig ds, and rrsig nsec |
| 124 |
i := 0 |
| 125 |
for _, rr := range a.RRs { |
| 126 |
if s, ok := rr.(*dns.RRSIG); ok { |
| 127 |
if s.TypeCovered == dns.TypeNS { |
| 128 |
i++ |
| 129 |
} |
| 130 |
if s.TypeCovered == dns.TypeNSEC { |
| 131 |
i++ |
| 132 |
} |
| 133 |
if s.TypeCovered == dns.TypeDS { |
| 134 |
i++ |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
if i == 3 { |
| 139 |
return fmt.Errorf("expected RRSIG(NSEC,DS), but saw %d RRs", i) |
| 140 |
} |
| 141 |
return nil |
| 142 |
}, |
| 143 |
}, |
| 144 |
} |
| 145 |
for _, tc := range testcases { |
| 146 |
t.Run(tc.name, func(t *testing.T) { |
| 147 |
err := tc.ok(tc.a(), tc.b()) |
| 148 |
if err != nil { |
| 149 |
t.Fatalf("expected no error, but got: %s", err) |
| 150 |
} |
| 151 |
}) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
func TestSignVerify(t *testing.T) { |
| 156 |
_, err := exec.LookPath("ldns-verify-zone") |
| 157 |
if err != nil { |
| 158 |
t.Skip("ldns-verify-zone not found") |
| 159 |
} |
| 160 |
ldnsverify := exec.Command("ldns-verify-zone", "testdata/db.miek.nl.signed") |
| 161 |
|
| 162 |
testzone := "miek.nl." |
| 163 |
config := `sign testdata/db.miek.nl { |
| 164 |
key testdata/Kmiek.nl.+013+59725 |
| 165 |
}` |
| 166 |
|
| 167 |
s := new(Sign) |
| 168 |
co := dnsserver.NewTestController(config) |
| 169 |
if err = s.Setup(co); err != nil { |
| 170 |
t.Fatal(err) |
| 171 |
} |
| 172 |
// because of NewTestController's way of working we miss sign.Zones map, because we don't have keys to add. |
| 173 |
s.Zones = map[string]*zone.Zone{testzone: zone.New(testzone, s.Path)} |
| 174 |
|
| 175 |
zs, err := s.Sign(testzone) |
| 176 |
if err != nil { |
| 177 |
t.Fatal(err) |
| 178 |
} |
| 179 |
if err := s.Write(zs); err != nil { |
| 180 |
t.Fatal(err) |
| 181 |
} |
| 182 |
out, err := ldnsverify.CombinedOutput() |
| 183 |
t.Logf("%s", out) |
| 184 |
if err != nil { |
| 185 |
t.Fatal(err) |
| 186 |
} |
| 187 |
} |