-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrootanchors_test.go
161 lines (133 loc) · 4.36 KB
/
rootanchors_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package rootanchors_test
import (
"fmt"
"strings"
"testing"
"github.com/miekg/dns"
rootanchors "github.com/zmap/go-dns-root-anchors"
)
// TestRootDSValidation checks root KSK against DS records from root trust anchor.
func TestRootDSValidation(t *testing.T) {
dnsKeys, err := queryRootKSK()
if err != nil {
t.Fatalf("Failed to query root DNSKEY records: %v", err)
}
t.Logf("DNSKEY records: %v", dnsKeys)
dsRecords := rootanchors.GetValidDSRecords()
if err != nil {
t.Fatalf("Failed to get DS records from trust anchor: %v", err)
}
t.Logf("DS records: %v", dsRecords)
for _, key := range dnsKeys {
authenticDS, ok := dsRecords[key.KeyTag()]
if !ok {
t.Fatalf("DS record not found for DNSKEY with key tag %d", key.KeyTag())
}
computedDS := key.ToDS(authenticDS.DigestType)
computedDigest := strings.ToUpper(computedDS.Digest)
t.Logf("Authentic digest: %s, computed digest: %s", authenticDS.Digest, computedDigest)
if computedDigest != authenticDS.Digest {
t.Fatalf("DS record mismatch for DNSKEY with key tag %d", key.KeyTag())
}
}
}
// TestRootDNSKEYValidation checks root DNSKEY records against KSKs from root trust anchor.
func TestRootDNSKEYValidation(t *testing.T) {
queriedDNSKEY, err := queryRoot(dns.TypeDNSKEY)
if err != nil {
t.Fatalf("Failed to query root DNSKEY records: %v", err)
}
KSKs := rootanchors.GetValidDNSKEYRecords()
t.Logf("KSKs: %v", KSKs)
var queriedKeys []dns.RR
rrsigs := make(map[uint16]*dns.RRSIG)
for _, rr := range queriedDNSKEY.Answer {
if ns, ok := rr.(*dns.DNSKEY); ok {
queriedKeys = append(queriedKeys, ns)
} else if rrsig, ok := rr.(*dns.RRSIG); ok {
rrsigs[rrsig.TypeCovered] = rrsig
} else {
t.Fatalf("Unexpected RR type: %T", rr)
}
}
rrsig, ok := rrsigs[dns.TypeDNSKEY]
if !ok {
t.Fatalf("RRSIG not found for DNSKEY records")
}
ksk, ok := KSKs[rrsig.KeyTag]
if !ok {
t.Fatalf("KSK not found for RRSIG with key tag %d", rrsig.KeyTag)
}
t.Logf("Verifying RRSet for RRSIG with key %v", ksk)
if err := rrsig.Verify(ksk, queriedKeys); err != nil {
t.Fatalf("Failed to verify RRSet: %v", err)
}
t.Logf("RRSet verified for RRSIG with key tag %d", rrsig.KeyTag)
}
// TestAnchorDNSKEYMatchesAnchorDSes checks that DNSKEY records from root trust anchor match DS records.
func TestAnchorDNSKEYMatchesAnchorDSes(t *testing.T) {
dnsKeys := rootanchors.GetValidDNSKEYRecords()
dsRecords := rootanchors.GetValidDSRecords()
for keyTag, dnsKey := range dnsKeys {
ds, ok := dsRecords[keyTag]
if !ok {
t.Fatalf("DS record not found for DNSKEY with key tag %d", keyTag)
}
computedDS := dnsKey.ToDS(ds.DigestType)
computedDigest := strings.ToUpper(computedDS.Digest)
t.Logf("Authentic digest: %s, computed digest: %s", ds.Digest, computedDigest)
if computedDigest != ds.Digest {
t.Fatalf("DS record mismatch for DNSKEY with key tag %d", keyTag)
}
}
}
// TestKSKAnchorsIncludesOnlineKSKs checks that the root trust anchor includes all online KSKs.
func TestKSKAnchorsIncludesOnlineKSKs(t *testing.T) {
anchors := rootanchors.GetValidDNSKEYRecords()
dnsKeys, err := queryRootKSK()
if err != nil {
t.Fatalf("Failed to query root DNSKEY records: %v", err)
}
for _, key := range dnsKeys {
anchor, ok := anchors[key.KeyTag()]
if !ok {
t.Fatalf("KSK not found for DNSKEY with key tag %d", key.KeyTag())
}
if key.PublicKey != anchor.PublicKey {
t.Fatalf("Public key mismatch for DNSKEY with key tag %d", key.KeyTag())
}
t.Logf("Public key matches for DNSKEY with key tag %d", key.KeyTag())
}
}
// queryRoot queries the root zone (".") for the specified DNS record type.
func queryRoot(recordType uint16) (*dns.Msg, error) {
c := new(dns.Client)
m := new(dns.Msg)
m.SetQuestion(".", recordType)
m.SetEdns0(4096, true)
r, _, err := c.Exchange(m, "198.41.0.4:53")
if err != nil {
return nil, fmt.Errorf("failed to query root: %v", err)
}
if r.Rcode != dns.RcodeSuccess {
return nil, fmt.Errorf("invalid answer for root query: %v", r.Rcode)
}
return r, nil
}
// queryRootKSK queries the KSK records for the root zone (".").
func queryRootKSK() ([]*dns.DNSKEY, error) {
r, err := queryRoot(dns.TypeDNSKEY)
if err != nil {
return nil, err
}
// Collect DNSKEY records from the response.
var dnsKeys []*dns.DNSKEY
for _, rr := range r.Answer {
if key, ok := rr.(*dns.DNSKEY); ok {
if key.Flags == 257 {
dnsKeys = append(dnsKeys, key)
}
}
}
return dnsKeys, nil
}