-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign.go
296 lines (267 loc) · 7.25 KB
/
sign.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
package dnsutils
import (
"crypto"
"fmt"
"io"
"time"
"github.com/miekg/dns"
)
type DenialOfExistenceMethod string
const (
DenialOfExistenceMethodNSEC = "NSEC"
DenialOfExistenceMethodNSEC3 = "NSEC3"
)
var (
DefaultBeforeSign time.Duration = time.Hour
DefaultExpiry time.Duration = time.Hour * 24 * 14
)
type SignOption struct {
BeforeSign *time.Duration
Expiry *time.Duration
Inception *uint32
Expiration *uint32
DoEMethod DenialOfExistenceMethod
NSEC3Salt string
NSEC3Iterate uint16
DNSKEYTTL *uint32
ZONEMDEnabled *bool
CDSEnabled *bool
CDNSKEYEnabled *bool
}
func (o *SignOption) GetBeforSign() time.Duration {
if o.BeforeSign == nil {
return DefaultBeforeSign
}
return *o.BeforeSign
}
func (o *SignOption) GetExpiry() time.Duration {
if o.Expiry == nil {
return DefaultExpiry
}
return *o.Expiry
}
func (o *SignOption) GetInception() uint32 {
if o.Inception == nil {
return uint32(time.Now().UTC().Add(-o.GetBeforSign()).Unix())
}
return *o.Inception
}
func (o *SignOption) GetExpiration() uint32 {
if o.Expiration == nil {
return uint32(time.Now().UTC().Add(o.GetExpiry()).Unix())
}
return *o.Expiration
}
func (o *SignOption) GetDNSKEYTTL() uint32 {
if o.DNSKEYTTL == nil {
return uint32(3600)
}
return *o.DNSKEYTTL
}
func (o *SignOption) GetNSEC3Salt() string {
return o.NSEC3Salt
}
func (o *SignOption) GetNSEC3Iterate() uint16 {
return o.NSEC3Iterate
}
func (o *SignOption) GetZONEMDEnabled() bool {
if o.ZONEMDEnabled == nil {
return true
}
return *o.ZONEMDEnabled
}
func (o *SignOption) GetCDSEnabled() bool {
if o.CDSEnabled == nil {
return true
}
return *o.CDSEnabled
}
func (o *SignOption) GetCDNSKEYEnabled() bool {
if o.CDSEnabled == nil {
return true
}
return *o.CDSEnabled
}
type DNSKEY struct {
rr *dns.DNSKEY
signer crypto.Signer
}
func ReadDNSKEY(priv, pub io.Reader) (*DNSKEY, error) {
var dnskey *dns.DNSKEY
zp := dns.NewZoneParser(pub, "", "")
for rr, ok := zp.Next(); ok; rr, ok = zp.Next() {
if rr.Header().Rrtype == dns.TypeDNSKEY {
dnskey, _ = rr.(*dns.DNSKEY)
}
}
if dnskey == nil {
return nil, fmt.Errorf("DNSKEY not found")
}
privateKey, err := dnskey.ReadPrivateKey(priv, "")
if err != nil {
return nil, fmt.Errorf("DNSKEY not found")
}
signer, _ := privateKey.(crypto.Signer)
return &DNSKEY{
rr: dnskey,
signer: signer,
}, nil
}
func (d *DNSKEY) GetSigner() crypto.Signer {
return d.signer
}
func (d *DNSKEY) GetRR() *dns.DNSKEY {
return d.rr
}
func (d *DNSKEY) IsKSK() bool {
return d.rr.Flags == 257
}
func (d *DNSKEY) IsZSK() bool {
return d.rr.Flags == 256
}
func Sign(z ZoneInterface, opt SignOption, dnskeys []*DNSKEY, generator Generator) error {
if err := AddDNSKEY(z, opt, dnskeys, generator); err != nil {
return fmt.Errorf("failed to add DNSKEY: %w", err)
}
if opt.GetZONEMDEnabled() {
if err := AddZONEMDPlaceholder(z, nil, generator); err != nil {
return fmt.Errorf("failed to add ZONEMD: %w", err)
}
}
if err := CreateDoE(z, opt, generator); err != nil {
return fmt.Errorf("failed to add NSEC or NSEC3: %w", err)
}
if err := SignZone(z, opt, dnskeys, generator); err != nil {
return fmt.Errorf("failed to sign zone: %w", err)
}
if opt.GetZONEMDEnabled() {
if err := UpdateZONEMDDigest(z, generator); err != nil {
return fmt.Errorf("failed to update ZONEMD digest: %w", err)
}
if err := SignNode(z.GetRootNode(), opt, dnskeys, generator, true, true); err != nil {
return fmt.Errorf("failed to sign zone apex: %w", err)
}
}
return nil
}
func AddDNSKEY(z ZoneInterface, opt SignOption, dnskeys []*DNSKEY, generator Generator) error {
if len(dnskeys) == 0 {
return fmt.Errorf("empty DNSKEYs")
}
rrset, err := GetRRSetOrCreate(z.GetRootNode(), dns.TypeDNSKEY, opt.GetDNSKEYTTL(), generator)
if err != nil {
return fmt.Errorf("failed to create DNSKEY rrset: %w", err)
}
cdsRRSet, err := GetRRSetOrCreate(z.GetRootNode(), dns.TypeCDS, opt.GetDNSKEYTTL(), generator)
if err != nil {
return fmt.Errorf("failed to create CDS rrset: %w", err)
}
cdnskeyRRset, err := GetRRSetOrCreate(z.GetRootNode(), dns.TypeCDNSKEY, opt.GetDNSKEYTTL(), generator)
if err != nil {
return fmt.Errorf("failed to create CDNSKEY rrset: %w", err)
}
for _, dnskey := range dnskeys {
rr := dnskey.GetRR()
rr.Hdr.Ttl = rrset.GetTTL()
if err := rrset.AddRR(rr); err != nil {
return fmt.Errorf("failed to add DNSKEY RR to rrset: %w", err)
}
if opt.GetCDSEnabled() && dnskey.IsKSK() {
if err := cdsRRSet.AddRR(rr.ToDS(dns.SHA256).ToCDS()); err != nil {
return fmt.Errorf("failed to add CDS RR to rrset: %w", err)
}
}
if opt.GetCDNSKEYEnabled() && dnskey.IsKSK() {
if err := cdnskeyRRset.AddRR(rr.ToCDNSKEY()); err != nil {
return fmt.Errorf("failed to add CDNSKEY RR to rrset: %w", err)
}
}
}
if err := z.GetRootNode().SetRRSet(rrset); err != nil {
return fmt.Errorf("failed to set DNSKEY rrset: %w", err)
}
if opt.GetCDSEnabled() {
if err := z.GetRootNode().SetRRSet(cdsRRSet); err != nil {
return fmt.Errorf("failed to set CDS rrset: %w", err)
}
}
if opt.GetCDNSKEYEnabled() {
if err := z.GetRootNode().SetRRSet(cdnskeyRRset); err != nil {
return fmt.Errorf("failed to set CDNSKEY rrset: %w", err)
}
}
return nil
}
func SignZone(z ZoneInterface, opt SignOption, dnskeys []*DNSKEY, generator Generator) error {
if generator == nil {
generator = &DefaultGenerator{}
}
// Sign
return z.GetRootNode().IterateNameNodeWithValue(func(nni NameNodeInterface, a any) (any, error) {
auth := a.(bool)
if z.GetName() != nni.GetName() {
if nsRRset := nni.GetRRSet(dns.TypeNS); nsRRset != nil {
return false, SignNode(nni, opt, dnskeys, generator, nni == z.GetRootNode(), true)
}
}
return auth, SignNode(nni, opt, dnskeys, generator, nni == z.GetRootNode(), auth)
}, true)
}
func SignNode(nni NameNodeInterface, opt SignOption, dnskeys []*DNSKEY, generator Generator, apex, auth bool) error {
if generator == nil {
generator = &DefaultGenerator{}
}
if !auth {
return nil
}
rrsig, err := generator.NewRRSet(nni.GetName(), 0, nni.GetClass(), dns.TypeRRSIG)
if err != nil {
return err
}
err = nni.IterateNameRRSet(func(ri RRSetInterface) error {
if ri.GetRRtype() == dns.TypeNS && !apex {
return nil
}
if ri.GetRRtype() == dns.TypeRRSIG {
return nil
}
rrsigRRs, err := SignRRSet(ri, opt, dnskeys)
if err != nil {
return err
}
for _, rr := range rrsigRRs {
rrsig.AddRR(rr)
}
return nil
})
if err != nil {
return fmt.Errorf("failed to sign rrset: %w", err)
}
if len(rrsig.GetRRs()) == 0 {
return nil
}
return nni.SetRRSet(rrsig)
}
func SignRRSet(ri RRSetInterface, opt SignOption, dnskeys []*DNSKEY) ([]*dns.RRSIG, error) {
var rrs []*dns.RRSIG
for _, dnskey := range dnskeys {
if (ri.GetRRtype() == dns.TypeDNSKEY && dnskey.IsKSK()) ||
(ri.GetRRtype() != dns.TypeDNSKEY && dnskey.IsZSK()) {
rrsig := &dns.RRSIG{
Hdr: dns.RR_Header{
Ttl: ri.GetTTL(),
},
KeyTag: dnskey.GetRR().KeyTag(),
SignerName: dnskey.GetRR().Header().Name,
Algorithm: dnskey.GetRR().Algorithm,
Inception: opt.GetInception(),
Expiration: opt.GetExpiration(),
}
if err := rrsig.Sign(dnskey.GetSigner(), ri.GetRRs()); err != nil {
return nil, fmt.Errorf("failed to sign: %w", err)
}
rrs = append(rrs, rrsig)
}
}
return rrs, nil
}