-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsec.go
229 lines (212 loc) · 6.04 KB
/
nsec.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
package dnsutils
import (
"errors"
"fmt"
"sort"
"strings"
"github.com/miekg/dns"
)
func CreateDoE(z ZoneInterface, opt SignOption, generator Generator) error {
if generator == nil {
generator = &DefaultGenerator{}
}
switch opt.DoEMethod {
case DenialOfExistenceMethodNSEC, "":
return createNSEC(z, generator)
case DenialOfExistenceMethodNSEC3:
return createNSEC3(z, opt, generator)
}
return fmt.Errorf("not support: %s", opt.DoEMethod)
}
func createNSEC(z ZoneInterface, generator RRSetGenerator) error {
var nodes = map[string]NameNodeInterface{}
var names []string
if generator == nil {
generator = &DefaultGenerator{}
}
soa, err := GetSOA(z)
if err != nil {
return ErrBadZone
}
zoneCuts, _, err := GetZoneCuts(z.GetRootNode())
if err != nil {
return ErrBadZone
}
// get next domain names
z.GetRootNode().IterateNameNode(func(nni NameNodeInterface) error {
// Blocks with no types present MUST NOT be included
if nni.RRSetLen() == 0 {
return nil
}
// A zone MUST NOT include an NSEC RR for any domain name that only holds glue records
parent, strict := zoneCuts.GetNameNode(nni.GetName())
if parent.GetName() != z.GetName() {
if !strict && parent.GetRRSet(dns.TypeNS) != nil {
return nil
}
}
nodes[nni.GetName()] = nni
names = append(names, nni.GetName())
return nil
})
SortNames(names)
for i, name := range names {
nsec := &dns.NSEC{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeNSEC,
Class: dns.ClassINET,
// The NSEC RR SHOULD have the same TTL value as the SOA minimum TTL field.
// This is in the spirit of negative caching ([RFC2308]).
Ttl: soa.Minttl,
},
TypeBitMap: []uint16{dns.TypeRRSIG, dns.TypeNSEC},
}
if i+1 < len(names) {
nsec.NextDomain = names[i+1]
} else {
nsec.NextDomain = names[0]
}
rresetMap := nodes[name].CopyRRSetMap()
for rtype := range rresetMap {
switch rtype {
case dns.TypeRRSIG:
case dns.TypeNSEC:
default:
nsec.TypeBitMap = append(nsec.TypeBitMap, rtype)
}
}
sort.SliceStable(nsec.TypeBitMap, func(i, j int) bool { return nsec.TypeBitMap[i] < nsec.TypeBitMap[j] })
set, err := generator.NewRRSet(name, soa.Minttl, dns.ClassINET, dns.TypeNSEC)
if err != nil {
return err
}
if err := set.AddRR(nsec); err != nil {
return err
}
if err := nodes[name].SetRRSet(set); err != nil {
return err
}
}
return nil
}
var (
ErrCollision = fmt.Errorf("hash collision detected")
)
func createNSEC3(z ZoneInterface, opt SignOption, generator Generator) error {
var nodes = map[string]NameNodeInterface{}
var hashCheckName = map[string]struct{}{}
var names []string
soa, err := GetSOA(z)
if err != nil {
return ErrBadZone
}
zoneCuts, _, err := GetZoneCuts(z.GetRootNode())
if err != nil {
return ErrBadZone
}
nsec3param := &dns.NSEC3PARAM{
Hdr: dns.RR_Header{
Name: soa.Hdr.Name,
Rrtype: dns.TypeNSEC3PARAM,
Class: dns.ClassINET,
Ttl: 0,
},
Hash: dns.SHA1,
Iterations: opt.GetNSEC3Iterate(),
Salt: opt.GetNSEC3Salt(),
}
nsec3ParamRRRet, err := NewRRSetFromRRWithGenerator(nsec3param, generator)
if err != nil {
return fmt.Errorf("failed to create nsec3param")
}
if err := z.GetRootNode().SetRRSet(nsec3ParamRRRet); err != nil {
return fmt.Errorf("failed to set nsec3param")
}
// get next domain names
err = z.GetRootNode().IterateNameNode(func(nni NameNodeInterface) error {
parent, static := zoneCuts.GetNameNode(nni.GetName())
if parent.GetName() != z.GetName() {
if !static && parent.GetRRSet(dns.TypeNS) != nil {
return nil
}
}
nodes[nni.GetName()] = nni
names = append(names, nni.GetName())
hashCheckName[nni.GetName()] = struct{}{}
labels := dns.SplitDomainName(nni.GetName())
if len(labels) > 0 && labels[0] != "*" {
hashCheckName["*."+nni.GetName()] = struct{}{}
}
return nil
})
if err != nil {
return fmt.Errorf("failed to create name list: %w", err)
}
// collision check and make hash owner name
hashMap := map[string]string{}
hashCheck := map[string]string{}
for name := range hashCheckName {
hashName := dns.HashName(name, dns.SHA1, opt.GetNSEC3Iterate(), opt.GetNSEC3Salt())
hashMap[name] = hashName
if _, exist := hashCheck[hashName]; exist {
return errors.Join(ErrCollision, fmt.Errorf("collision %s %s", hashCheck[hashName], name))
} else {
hashCheck[hashName] = name
}
}
sort.Slice(names, func(i, j int) bool {
cmp, _ := CompareName(hashMap[names[i]], hashMap[names[j]])
return cmp < 0
})
for i, name := range names {
nsec3 := &dns.NSEC3{
Hdr: dns.RR_Header{
Name: dns.CanonicalName(hashMap[name] + "." + z.GetName()),
Rrtype: dns.TypeNSEC3,
Class: dns.ClassINET,
// The NSEC RR SHOULD have the same TTL value as the SOA minimum TTL field.
// This is in the spirit of negative caching ([RFC2308]).
Ttl: soa.Minttl,
},
Hash: dns.SHA1,
Iterations: opt.GetNSEC3Iterate(),
Salt: opt.GetNSEC3Salt(),
SaltLength: uint8(len(opt.GetNSEC3Salt()) / 2),
HashLength: 20, // SHA-1
}
if i+1 < len(names) {
nsec3.NextDomain = strings.ToLower(hashMap[names[i+1]])
} else {
nsec3.NextDomain = strings.ToLower(hashMap[names[0]])
}
rresetMap := nodes[name].CopyRRSetMap()
var (
isZoneCust, haveDS bool
)
for rtype := range rresetMap {
switch rtype {
case dns.TypeRRSIG:
case dns.TypeNSEC:
case dns.TypeDS:
nsec3.TypeBitMap = append(nsec3.TypeBitMap, rtype)
haveDS = true
case dns.TypeNS:
nsec3.TypeBitMap = append(nsec3.TypeBitMap, rtype)
if z.GetName() != name {
isZoneCust = true
}
default:
nsec3.TypeBitMap = append(nsec3.TypeBitMap, rtype)
}
}
if !IsENT(nodes[name]) && (!isZoneCust || isZoneCust && haveDS) {
nsec3.TypeBitMap = append(nsec3.TypeBitMap, dns.TypeRRSIG)
}
sort.SliceStable(nsec3.TypeBitMap, func(i, j int) bool { return nsec3.TypeBitMap[i] < nsec3.TypeBitMap[j] })
if err := CreateOrReplaceRRSetFromRRs(z.GetRootNode(), []dns.RR{nsec3}, generator); err != nil {
return fmt.Errorf("failed to create NSEC3 %s cover %s : %w", nsec3.Header().Name, name, err)
}
}
return nil
}