-
Notifications
You must be signed in to change notification settings - Fork 7
/
dnssec_test.go
359 lines (317 loc) · 8.81 KB
/
dnssec_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package solvere
import (
"context"
"crypto"
"crypto/rsa"
"crypto/sha1"
"fmt"
"net"
"sync"
"testing"
"time"
"github.com/miekg/dns"
"github.com/jmhodges/clock"
)
var eMu = new(sync.Mutex)
var exampleKey = dns.DNSKEY{
Hdr: dns.RR_Header{Name: "example.", Rrtype: dns.TypeDNSKEY, Ttl: 3600},
Algorithm: dns.RSASHA256,
Flags: 256,
Protocol: 3,
}
var examplePrivateKey = new(crypto.PrivateKey)
var exampleKeySig = &dns.RRSIG{}
func init() {
epk, err := exampleKey.Generate(512)
if err != nil {
panic(err)
}
examplePrivateKey = &epk
year68 := int64(1 << 31)
n := time.Now().UTC().Unix()
mod := (n / year68) - 1
if mod < 0 {
mod = 0
}
inception := uint32(n - (mod * year68))
n = time.Now().Add(time.Hour).UTC().Unix()
mod = (n / year68) - 1
if mod < 0 {
mod = 0
}
expiration := uint32(n - (mod * year68))
exampleKeySig = &dns.RRSIG{
Hdr: dns.RR_Header{Ttl: 3600},
Inception: inception,
Expiration: expiration,
KeyTag: exampleKey.KeyTag(),
SignerName: "example.",
Algorithm: dns.RSASHA256,
}
rk := epk.(*rsa.PrivateKey)
err = exampleKeySig.Sign(rk, []dns.RR{&exampleKey})
if err != nil {
panic(err)
}
}
func mockDNSKEYServer(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
m.Rcode = dns.RcodeSuccess
if len(r.Question) != 1 {
m.Rcode = dns.RcodeServerFailure
w.WriteMsg(m)
return
}
eMu.Lock()
defer eMu.Unlock()
switch r.Question[0].Name {
case "example.":
m.Answer = append(m.Answer, &exampleKey, exampleKeySig)
case "bad.":
m.Rcode = dns.RcodeServerFailure
w.WriteMsg(m)
return
case "no-keys-weird.":
m.Answer = append(m.Answer, &dns.SOA{
Hdr: dns.RR_Header{Name: "no-keys-weird.", Rrtype: dns.TypeSOA},
Ns: "ns.no-keys-weird.",
Mbox: "master.no-keys-weird.",
Serial: 1,
Refresh: 1,
Retry: 1,
Expire: 1,
Minttl: 1,
})
case "out-of-bailiwick.":
m.Answer = append(m.Answer, &exampleKey, exampleKeySig)
case "bad-sig.":
badSigRR := dns.Copy(exampleKeySig)
badSig := badSigRR.(*dns.RRSIG)
badSig.Hdr.Name = "bad-sig."
badSig.Signature = ""
keyRR := dns.Copy(&exampleKey)
key := keyRR.(*dns.DNSKEY)
key.Hdr.Name = "bad-sig."
m.Answer = append(m.Answer, key, badSig)
}
w.WriteMsg(m)
return
}
func TestLookupDNSKEY(t *testing.T) {
dnsPort = "9053"
dns.HandleFunc(".", mockDNSKEYServer)
server := &dns.Server{Addr: "127.0.0.1:9053", Net: "udp", ReadTimeout: time.Second, WriteTimeout: time.Second}
go func() {
err := server.ListenAndServe()
if err != nil {
fmt.Printf("DNS test server failed: %s\n", err)
return
}
}()
// wait for things to warm up :/
time.Sleep(time.Millisecond * 500)
stop := make(chan struct{}, 1)
defer func() { stop <- struct{}{} }()
go func() {
<-stop
err := server.Shutdown()
if err != nil {
fmt.Printf("Failed to shutdown DNS test server: %s\n", err)
return
}
}()
rr := RecursiveResolver{useDNSSEC: true, c: new(dns.Client)}
auth := &Nameserver{Zone: "example.", Addr: "127.0.0.1"}
// Valid response
keyMap, _, addToCache, err := rr.lookupDNSKEY(context.Background(), auth)
if err != nil {
t.Fatalf("lookupDNSKEY failed with a valid response with no DS set: %s", err)
}
if len(keyMap) != 1 {
t.Fatal("lookupDNSKEY returned incorrect size keyMap for 'example.'")
}
if k, present := keyMap[exampleKey.KeyTag()]; !present {
t.Fatal("lookupDNSKEY returned keyMap missing expected key for 'example.'")
} else if *k != exampleKey {
t.Fatal("lookupDNSKEY returned keyMap containing wrong key with right key tag for 'example.'")
}
// nothing should happen since cache == nil
addToCache()
// Invalid response, empty answer
_, _, _, err = rr.lookupDNSKEY(context.Background(), &Nameserver{Zone: ".", Addr: "127.0.0.1"})
if err == nil {
t.Fatalf("lookupDNSKEY didn't fail with a empty answer")
}
// Invalid response, bad rcode
_, _, _, err = rr.lookupDNSKEY(context.Background(), &Nameserver{Zone: "bad.", Addr: "127.0.0.1"})
if err == nil {
t.Fatalf("lookupDNSKEY didn't fail with a bad rcode")
}
// Invalid response, wrong types returned
_, _, _, err = rr.lookupDNSKEY(context.Background(), &Nameserver{Zone: "no-keys-weird.", Addr: "127.0.0.1"})
if err == nil {
t.Fatalf("lookupDNSKEY didn't fail with a no keys")
}
// Invalid response, bad rcode
_, _, _, err = rr.lookupDNSKEY(context.Background(), &Nameserver{Zone: "no-keys-weird.", Addr: "127.0.0.1"})
if err == nil {
t.Fatalf("lookupDNSKEY didn't fail with a no keys")
}
// Invalid response, out of bailiwick records
_, _, _, err = rr.lookupDNSKEY(context.Background(), &Nameserver{Zone: "out-of-bailiwick.", Addr: "127.0.0.1"})
if err == nil {
t.Fatalf("lookupDNSKEY didn't fail with out of bailiwick records")
}
// Invalid response, invalid signature
_, _, _, err = rr.lookupDNSKEY(context.Background(), &Nameserver{Zone: "bad-sig.", Addr: "127.0.0.1"})
if err == nil {
t.Fatalf("lookupDNSKEY didn't fail with bad signature")
}
// Cache test
fc := clock.NewFake()
cache := &BasicCache{cache: make(map[[sha1.Size]byte]*cacheEntry), clk: fc}
rr.cache = cache
_, _, addToCache, err = rr.lookupDNSKEY(context.Background(), auth)
if err != nil {
t.Fatalf("lookupDNSKEY failed with a valid response: %s", err)
}
addToCache()
// check key is cached
goodSig := exampleKeySig.Signature
eMu.Lock()
exampleKeySig.Signature = ""
eMu.Unlock()
_, _, _, err = rr.lookupDNSKEY(context.Background(), auth)
eMu.Lock()
exampleKeySig.Signature = goodSig
eMu.Unlock()
if err != nil {
t.Fatalf("lookupDNSKEY failed with a valid response: %s", err)
}
}
func TestCheckDS(t *testing.T) {
k := &dns.DNSKEY{Algorithm: dns.RSASHA256}
_, err := k.Generate(512)
if err != nil {
t.Fatalf("Failed to generate DNSKEY: %s", err)
}
keyMap := map[uint16]*dns.DNSKEY{}
dsSet := []dns.RR{k.ToDS(dns.SHA256)}
err = checkDS(keyMap, dsSet)
if err == nil {
t.Fatal("checkDS did not fail with an empty key map")
}
keyMap[k.KeyTag()] = k
err = checkDS(keyMap, dsSet)
if err != nil {
t.Fatalf("checkDS failed to verify a valid key and DS combination: %s", err)
}
newDS := k.ToDS(dns.SHA256)
newDS.DigestType = dns.SHA1
dsSet = []dns.RR{newDS}
err = checkDS(keyMap, dsSet)
if err == nil {
t.Fatal("checkDS didn't fail with mismatching DS record")
}
k.PublicKey = "broken"
err = checkDS(keyMap, dsSet)
if err == nil {
t.Fatal("checkDS didn't fail with malformed KSK record")
}
}
func TestVerifyRRSIG(t *testing.T) {
k := &dns.DNSKEY{Hdr: dns.RR_Header{Name: "org."}, Algorithm: dns.RSASHA256, Protocol: 3}
pk, err := k.Generate(512)
if err != nil {
t.Fatalf("Failed to generate DNSKEY: %s", err)
}
rk := pk.(*rsa.PrivateKey)
keyMap := map[uint16]*dns.DNSKEY{}
keyMap[k.KeyTag()] = k
n := time.Now().UTC().Unix()
mod := (n / year68) - 1
if mod < 0 {
mod = 0
}
inception := uint32(n - (mod * year68))
n = time.Now().Add(time.Hour).UTC().Unix()
mod = (n / year68) - 1
if mod < 0 {
mod = 0
}
expiration := uint32(n - (mod * year68))
sigA := &dns.RRSIG{
Inception: inception,
Expiration: expiration,
KeyTag: k.KeyTag(),
SignerName: "org.",
Algorithm: dns.RSASHA256,
}
sigB := &dns.RRSIG{
Inception: inception,
Expiration: expiration,
KeyTag: k.KeyTag(),
SignerName: "org.",
Algorithm: dns.RSASHA256,
}
aSet := []dns.RR{
&dns.A{Hdr: dns.RR_Header{Name: "a.com."}, A: net.IP{1, 2, 3, 4}},
&dns.A{Hdr: dns.RR_Header{Name: "a.com."}, A: net.IP{1, 2, 3, 5}},
}
nsSet := []dns.RR{
&dns.NS{Hdr: dns.RR_Header{Name: "c.com."}, Ns: "a.com."},
}
err = sigA.Sign(rk, aSet)
if err != nil {
t.Fatalf("Failed to sign aSet: %s", err)
}
err = sigB.Sign(rk, nsSet)
if err != nil {
t.Fatalf("Failed to sign aSet: %s", err)
}
// Valid signatures
m := &dns.Msg{Answer: append(nsSet, sigB)}
err = verifyRRSIG(m, keyMap)
if err != nil {
t.Fatalf("Failed to verify valid RRSIGs: %s", err)
}
// Missing signatures
m = &dns.Msg{Answer: aSet}
err = verifyRRSIG(m, keyMap)
if err == nil {
t.Fatal("verifyRRSIG didn't fail with missing signatures")
}
// Missing signed records
m = &dns.Msg{Answer: []dns.RR{sigA}}
err = verifyRRSIG(m, keyMap)
if err == nil {
t.Fatal("verifyRRSIG didn't fail with missing signed records")
}
// Missing key
m = &dns.Msg{Answer: append(aSet, sigA)}
err = verifyRRSIG(m, make(map[uint16]*dns.DNSKEY))
if err == nil {
t.Fatal("verifyRRSIG didn't fail with missing DNSKEY")
}
// Invalid signature
sigA.Signature = ""
m = &dns.Msg{Answer: append(aSet, sigA)}
err = verifyRRSIG(m, keyMap)
if err == nil {
t.Fatal("verifyRRSIG didn't fail with invalid signature")
}
// Invalid validity period
sigA.Expiration = inception - 10
err = sigA.Sign(rk, aSet)
if err != nil {
t.Fatalf("Failed to sign aSet: %s", err)
}
m = &dns.Msg{Answer: append(aSet, sigA)}
err = verifyRRSIG(m, keyMap)
if err == nil {
t.Fatal("verifyRRSIG didn't fail with invalid validity period")
}
}
func TestCheckSignatures(t *testing.T) {
}