This repository has been archived by the owner on Oct 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathdnssec.go
325 lines (299 loc) · 8.34 KB
/
dnssec.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
// Copyright (c) 2013 Erik St. Martin, Brian Ketelsen. All rights reserved.
// Use of this source code is governed by The MIT License (MIT) that can be
// found in the LICENSE file.
package main
import (
"crypto/sha1"
"log"
"os"
"sync"
"time"
"github.com/miekg/dns"
)
const origTTL uint32 = 60
var (
cache *sigCache = newCache()
inflight *single = new(single)
)
// ParseKeyFile read a DNSSEC keyfile as generated by dnssec-keygen or other
// utilities. It add ".key" for the public key and ".private" for the private key.
func ParseKeyFile(file string) (*dns.DNSKEY, dns.PrivateKey, error) {
f, e := os.Open(file + ".key")
if e != nil {
return nil, nil, e
}
k, e := dns.ReadRR(f, file+".key")
if e != nil {
return nil, nil, e
}
f, e = os.Open(file + ".private")
if e != nil {
return nil, nil, e
}
p, e := k.(*dns.DNSKEY).ReadPrivateKey(f, file+".private")
if e != nil {
return nil, nil, e
}
k.Header().Ttl = origTTL
return k.(*dns.DNSKEY), p, nil
}
// nsec creates (if needed) NSEC records that are included in the reply.
func (s *server) nsec(m *dns.Msg) {
if m.Rcode == dns.RcodeNameError {
// qname nsec
nsec1 := s.newNSEC(m.Question[0].Name)
m.Ns = append(m.Ns, nsec1)
// wildcard nsec
idx := dns.Split(m.Question[0].Name)
wildcard := "*." + m.Question[0].Name[idx[0]:]
nsec2 := s.newNSEC(wildcard)
if nsec1.Hdr.Name != nsec2.Hdr.Name || nsec1.NextDomain != nsec2.NextDomain {
// different NSEC, add it
m.Ns = append(m.Ns, nsec2)
}
}
if m.Rcode == dns.RcodeSuccess && len(m.Ns) == 1 {
if _, ok := m.Ns[0].(*dns.SOA); ok {
m.Ns = append(m.Ns, s.newNSEC(m.Question[0].Name))
}
}
}
// sign signs a message m, it takes care of negative or nodata responses as
// well by synthesising NSEC records. It will also cache the signatures, using
// a hash of the signed data as a key.
// We also fake the origin TTL in the signature, because we don't want to
// throw away signatures when services decide to have longer TTL. So we just
// set the origTTL to 60.
func (s *server) sign(m *dns.Msg, bufsize uint16) {
now := time.Now().UTC()
incep := uint32(now.Add(-2 * time.Hour).Unix()) // 2 hours, be sure to catch daylight saving time and such
expir := uint32(now.Add(7 * 24 * time.Hour).Unix()) // sign for a week
// TODO(miek): repeating this two times?
for _, r := range rrSets(m.Answer) {
if r[0].Header().Rrtype == dns.TypeRRSIG {
continue
}
key := cache.key(r)
if s := cache.search(key); s != nil {
if s.ValidityPeriod(now.Add(-24 * time.Hour)) {
m.Answer = append(m.Answer, s)
continue
}
cache.remove(key)
}
sig, err, shared := inflight.Do(key, func() (*dns.RRSIG, error) {
sig1 := s.newRRSIG(incep, expir)
e := sig1.Sign(s.config.PrivKey, r)
if e != nil {
log.Printf("Failed to sign: %s\n", e.Error())
}
return sig1, e
})
if err != nil {
continue
}
if !shared {
// is it possible to miss this, due the the c.dups > 0 in Do()? TODO(miek)
cache.insert(key, sig)
}
m.Answer = append(m.Answer, dns.Copy(sig).(*dns.RRSIG))
}
for _, r := range rrSets(m.Ns) {
if r[0].Header().Rrtype == dns.TypeRRSIG {
continue
}
key := cache.key(r)
if s := cache.search(key); s != nil {
if s.ValidityPeriod(now.Add(-24 * time.Hour)) {
m.Ns = append(m.Ns, s)
continue
}
cache.remove(key)
}
sig, err, shared := inflight.Do(key, func() (*dns.RRSIG, error) {
sig1 := s.newRRSIG(incep, expir)
e := sig1.Sign(s.config.PrivKey, r)
if e != nil {
log.Printf("Failed to sign: %s\n", e.Error())
}
return sig1, e
})
if err != nil {
continue
}
if !shared {
// is it possible to miss this, due the the c.dups > 0 in Do()? TODO(miek)
cache.insert(key, sig)
}
m.Ns = append(m.Ns, dns.Copy(sig).(*dns.RRSIG))
}
// TODO(miek): Forget the additional section for now
if bufsize >= 512 || bufsize <= 4096 {
m.Truncated = m.Len() > int(bufsize)
}
o := new(dns.OPT)
o.Hdr.Name = "."
o.Hdr.Rrtype = dns.TypeOPT
o.SetDo()
o.SetUDPSize(4096)
m.Extra = append(m.Extra, o)
return
}
func (s *server) newRRSIG(incep, expir uint32) *dns.RRSIG {
sig := new(dns.RRSIG)
sig.Hdr.Rrtype = dns.TypeRRSIG
sig.Hdr.Ttl = origTTL
sig.OrigTtl = origTTL
sig.Algorithm = s.config.PubKey.Algorithm
sig.KeyTag = s.config.KeyTag
sig.Inception = incep
sig.Expiration = expir
sig.SignerName = s.config.PubKey.Hdr.Name
return sig
}
// newNSEC returns the NSEC record need to denial qname, or gives back a NODATA NSEC.
func (s *server) newNSEC(qname string) *dns.NSEC {
qlabels := dns.SplitDomainName(qname)
if len(qlabels) < s.domainLabels {
// TODO(miek): can not happen...?
}
// Strip the last s.domainLabels, return up to 4 before
// that. Four labels is the maximum qname we can handle.
ls := len(qlabels) - s.domainLabels
ls4 := ls - 4
if ls4 < 0 {
ls4 = 0
}
key := qlabels[ls4:ls]
key = key // TODO(miek)
// TODO etcd here
// prev, next := s.registry.GetNSEC(strings.Join(key, "."))
prev, next := "", ""
nsec := &dns.NSEC{Hdr: dns.RR_Header{Name: prev + s.config.Domain + ".", Rrtype: dns.TypeNSEC, Class: dns.ClassINET, Ttl: 60},
NextDomain: next + s.config.Domain + "."}
if prev == "" {
nsec.TypeBitMap = []uint16{dns.TypeA, dns.TypeSOA, dns.TypeNS, dns.TypeAAAA, dns.TypeRRSIG, dns.TypeNSEC, dns.TypeDNSKEY}
} else {
nsec.TypeBitMap = []uint16{dns.TypeA, dns.TypeAAAA, dns.TypeSRV, dns.TypeRRSIG, dns.TypeNSEC}
}
return nsec
}
type rrset struct {
qname string
qtype uint16
}
func rrSets(rrs []dns.RR) map[rrset][]dns.RR {
m := make(map[rrset][]dns.RR)
for _, r := range rrs {
if s, ok := m[rrset{r.Header().Name, r.Header().Rrtype}]; ok {
s = append(s, r)
m[rrset{r.Header().Name, r.Header().Rrtype}] = s
} else {
s := make([]dns.RR, 1, 3)
s[0] = r
m[rrset{r.Header().Name, r.Header().Rrtype}] = s
}
}
if len(m) > 0 {
return m
}
return nil
}
type sigCache struct {
sync.RWMutex
m map[string]*dns.RRSIG
}
func newCache() *sigCache {
c := new(sigCache)
c.m = make(map[string]*dns.RRSIG)
return c
}
func (c *sigCache) remove(s string) {
delete(c.m, s)
}
func (c *sigCache) insert(s string, r *dns.RRSIG) {
c.Lock()
defer c.Unlock()
if _, ok := c.m[s]; !ok {
c.m[s] = r
}
}
func (c *sigCache) search(s string) *dns.RRSIG {
c.RLock()
defer c.RUnlock()
if s, ok := c.m[s]; ok {
// we want to return a copy here, because if we didn't the RRSIG
// could be removed by another goroutine before the packet containing
// this signature is send out.
log.Println("DNS Signature retrieved from cache")
return dns.Copy(s).(*dns.RRSIG)
}
return nil
}
// key uses the name, type and rdata, which is serialized and then hashed as the
// key for the lookup
func (c *sigCache) key(rrs []dns.RR) string {
h := sha1.New()
i := []byte(rrs[0].Header().Name)
i = append(i, packUint16(rrs[0].Header().Rrtype)...)
for _, r := range rrs {
switch t := r.(type) { // we only do a few type, serialize these manually
case *dns.SOA:
i = append(i, packUint32(t.Serial)...)
// we only fiddle with the serial so store that
case *dns.SRV:
i = append(i, packUint16(t.Priority)...)
i = append(i, packUint16(t.Weight)...)
i = append(i, packUint16(t.Weight)...)
i = append(i, []byte(t.Target)...)
case *dns.A:
i = append(i, []byte(t.A)...)
case *dns.AAAA:
i = append(i, []byte(t.AAAA)...)
case *dns.DNSKEY:
// Need nothing more, the rdata stays the same during a run
case *dns.NSEC:
i = append(i, []byte(t.NextDomain)...)
// bitmap does not differentiate
default:
log.Printf("DNS Signature for unhandled type %T seen", t)
}
}
return string(h.Sum(i))
}
// TODO(miek): prolly should use the stdlib ones
func packUint16(i uint16) []byte { return []byte{byte(i >> 8), byte(i)} }
func packUint32(i uint32) []byte { return []byte{byte(i >> 24), byte(i >> 16), byte(i >> 8), byte(i)} }
// Adapted from singleinflight.go from the original Go Code. Copyright 2013 The Go Authors.
type call struct {
wg sync.WaitGroup
val *dns.RRSIG
err error
dups int
}
type single struct {
sync.Mutex
m map[string]*call
}
func (g *single) Do(key string, fn func() (*dns.RRSIG, error)) (*dns.RRSIG, error, bool) {
g.Lock()
if g.m == nil {
g.m = make(map[string]*call)
}
if c, ok := g.m[key]; ok {
c.dups++
g.Unlock()
c.wg.Wait()
return c.val, c.err, true
}
c := new(call)
c.wg.Add(1)
g.m[key] = c
g.Unlock()
c.val, c.err = fn()
c.wg.Done()
g.Lock()
delete(g.m, key)
g.Unlock()
return c.val, c.err, c.dups > 0
}