-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathdnssec.go
More file actions
443 lines (392 loc) · 10.9 KB
/
dnssec.go
File metadata and controls
443 lines (392 loc) · 10.9 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package dns
import (
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rand"
"crypto/rsa"
_ "crypto/sha1" // need its init function
_ "crypto/sha256" // need its init function
_ "crypto/sha512" // need its init function
"encoding/asn1"
"encoding/binary"
"encoding/hex"
"hash"
"math/big"
"strings"
"time"
"codeberg.org/miekg/dns/internal/pack"
"codeberg.org/miekg/dns/internal/unpack"
"codeberg.org/miekg/dns/pkg/pool"
)
// DNSSEC encryption algorithm codes.
const (
_ uint8 = iota
RSAMD5
DH
DSA
_ // Skip 4, RFC 6725, section 2.1
RSASHA1
DSANSEC3SHA1
RSASHA1NSEC3SHA1
RSASHA256
_ // Skip 9, RFC 6725, section 2.1
RSASHA512
_ // Skip 11, RFC 6725, section 2.1
ECCGOST
ECDSAP256SHA256
ECDSAP384SHA384
ED25519
ED448
INDIRECT uint8 = 252
PRIVATEDNS uint8 = 253 // Private (experimental keys).
PRIVATEOID uint8 = 254
)
// AlgorithmToString is a map of algorithm IDs to algorithm names.
var AlgorithmToString = map[uint8]string{
RSAMD5: "RSAMD5",
DH: "DH",
DSA: "DSA",
RSASHA1: "RSASHA1",
DSANSEC3SHA1: "DSA-NSEC3-SHA1",
RSASHA1NSEC3SHA1: "RSASHA1-NSEC3-SHA1",
RSASHA256: "RSASHA256",
RSASHA512: "RSASHA512",
ECCGOST: "ECC-GOST",
ECDSAP256SHA256: "ECDSAP256SHA256",
ECDSAP384SHA384: "ECDSAP384SHA384",
ED25519: "ED25519",
ED448: "ED448",
INDIRECT: "INDIRECT",
PRIVATEDNS: "PRIVATEDNS",
PRIVATEOID: "PRIVATEOID",
}
// AlgorithmToHash is a map of algorithm crypto hash IDs to crypto.Hash's.
// Newer algorithm that do their own hashing (i.e. ED25519) are not present here.
var AlgorithmToHash = map[uint8]crypto.Hash{
RSAMD5: crypto.MD5, // Deprecated in RFC 6725.
DSA: crypto.SHA1,
RSASHA1: crypto.SHA1,
RSASHA1NSEC3SHA1: crypto.SHA1,
RSASHA256: crypto.SHA256,
ECDSAP256SHA256: crypto.SHA256,
ECDSAP384SHA384: crypto.SHA384,
RSASHA512: crypto.SHA512,
}
// DNSSEC hashing algorithm codes.
const (
_ uint8 = iota
SHA1 // RFC 4034.
SHA256 // RFC 4509.
GOST94 // RFC 5933.
SHA384 // Experimental.
SHA512 // Experimental.
)
// HashToString is a map of hash IDs to names.
var HashToString = map[uint8]string{
SHA1: "SHA1",
SHA256: "SHA256",
GOST94: "GOST94",
SHA384: "SHA384",
SHA512: "SHA512",
}
// DNSKEY flag values.
const (
FlagSEP = 1
FlagREVOKE = 1 << 7
FlagZONE = 1 << 8
FlagDELEG = 1 << 14
)
// KeyTag returns the keytag (or key-id) of the DNSKEY. If k.Tag is not zero, that value is returned instead,
// otherwise the tag is calculated, set and returned.
func (k *DNSKEY) KeyTag() uint16 {
if k.Tag > 0 {
return k.Tag
}
keytag := 0
switch k.Algorithm {
case RSAMD5:
// This algorithm has been deprecated, but keep this key-tag calculation.
// Look at the bottom two bytes of the modules, which the last item in the pubkey.
// See https://www.rfc-editor.org/errata/eid193 .
modulus, _ := pack.Base64([]byte(k.PublicKey))
if len(modulus) > 1 {
x := binary.BigEndian.Uint16(modulus[len(modulus)-3:])
keytag = int(x)
}
default:
wire := make([]byte, defaultBufSize/2)
n, err := k.pack(wire, 0, nil)
if err != nil {
return 0
}
wire = wire[:n]
for i, v := range wire {
if i&1 != 0 {
keytag += int(v) // must be larger than uint32
} else {
keytag += int(v) << 8
}
}
keytag += keytag >> 16 & 0xFFFF
keytag &= 0xFFFF
}
k.Tag = uint16(keytag)
return k.Tag
}
// ToDS converts a DNSKEY record to a DS record.
func (k *DNSKEY) ToDS(h uint8) *DS {
if k == nil {
return nil
}
ds := new(DS)
ds.Hdr.Name = k.Hdr.Name
ds.Hdr.Class = k.Hdr.Class
ds.Hdr.TTL = k.Hdr.TTL
ds.Algorithm = k.Algorithm
ds.DigestType = h
ds.KeyTag = k.KeyTag()
wire := make([]byte, defaultBufSize/2)
n, err := k.pack(wire, 0, nil)
if err != nil {
return nil
}
wire = wire[:n]
owner := make([]byte, len(k.Hdr.Name)+1)
off, err1 := pack.Name(dnsutilCanonical(k.Hdr.Name), owner, 0, nil, false)
if err1 != nil {
return nil
}
owner = owner[:off]
// RFC4034:
// digest = digest_algorithm( DNSKEY owner name | DNSKEY RDATA);
// "|" denotes concatenation
// DNSKEY RDATA = Flags | Protocol | Algorithm | Public Key.
var hash crypto.Hash
switch h {
case SHA1:
hash = crypto.SHA1
case SHA256:
hash = crypto.SHA256
case SHA384:
hash = crypto.SHA384
case SHA512:
hash = crypto.SHA512
default:
return nil
}
s := hash.New()
s.Write(owner)
s.Write(wire)
ds.Digest = hex.EncodeToString(s.Sum(nil))
return ds
}
// ToCDNSKEY converts a DNSKEY record to a CDNSKEY record.
func (k *DNSKEY) ToCDNSKEY() *CDNSKEY {
c := &CDNSKEY{DNSKEY: *k}
c.Hdr = k.Hdr
return c
}
// ToCDS converts a DS record to a CDS record.
func (d *DS) ToCDS() *CDS {
c := &CDS{DS: *d}
c.Hdr = d.Hdr
return c
}
// Sign signs an RRset. The signature needs to be filled in with the values:
// Inception, Expiration, KeyTag, SignerName and Algorithm. See [NewRRSIG], the rest is copied
// from the RRset. Sign returns a non-nill error when the signing went OK.
// There is no check if RRSet is a proper (RFC 2181) RRSet.
// Sign expect RRSIG to be initialized with [NewRRSIG]. Sign will skip RRSIG records, and return nil in that case.
func (rr *RRSIG) Sign(k crypto.Signer, rrset []RR, options *SignOption) error {
// s.Inception and s.Expiration may be 0 (rollover etc.), the rest must be set
if rr.KeyTag == 0 || len(rr.SignerName) == 0 || rr.Algorithm == 0 {
return ErrKey
}
if options.Pooler == nil {
options.Pooler = pool.NewNoop(defaultBufSize)
}
h0 := rrset[0].Header()
t0 := RRToType(rrset[0])
if t0 == TypeRRSIG {
return nil
}
rr.Hdr.Name = h0.Name
rr.Hdr.TTL = h0.TTL
rr.Hdr.Class = h0.Class
rr.OrigTTL = h0.TTL
rr.TypeCovered = t0
rr.Labels = uint8(dnsutilLabels(h0.Name))
if strings.HasPrefix(h0.Name, "*.") {
rr.Labels-- // wildcard, remove from label count
}
rr.Signature = ""
// Create the desired binary blob
signdata := options.Get()
defer options.Put(signdata)
n, err := rr.pack(signdata, 0, nil)
if err != nil {
return err
}
m := rawSignatureData(signdata[n:], rrset, rr)
signdata = signdata[:m+n]
var h hash.Hash
hash, ok := AlgorithmToHash[rr.Algorithm]
if !ok && rr.Algorithm != ED25519 {
return ErrAlg
}
switch rr.Algorithm {
case RSAMD5, DSA, DSANSEC3SHA1:
// See RFC 6944.
return ErrAlg
case ED25519:
signature, err := sign(k, signdata, hash, rr.Algorithm)
if err != nil {
return err
}
rr.Signature = unpack.Base64(signature)
return nil
default:
h = hash.New()
h.Write(signdata)
signature, err := sign(k, h.Sum(nil), hash, rr.Algorithm)
if err != nil {
return err
}
rr.Signature = unpack.Base64(signature)
return nil
}
}
func sign(k crypto.Signer, hashed []byte, hash crypto.Hash, alg uint8) ([]byte, error) {
signature, err := k.Sign(rand.Reader, hashed, hash)
if err != nil {
return nil, err
}
switch alg {
case RSASHA1, RSASHA1NSEC3SHA1, RSASHA256, RSASHA512, ED25519:
return signature, nil
case ECDSAP256SHA256, ECDSAP384SHA384:
ecdsaSignature := &struct {
R, S *big.Int
}{}
if _, err := asn1.Unmarshal(signature, ecdsaSignature); err != nil {
return nil, err
}
var intlen int
switch alg {
case ECDSAP256SHA256:
intlen = 32
case ECDSAP384SHA384:
intlen = 48
}
signature := intToBytes(ecdsaSignature.R, intlen)
signature = append(signature, intToBytes(ecdsaSignature.S, intlen)...)
return signature, nil
default:
return nil, ErrAlg
}
}
// Verify validates an RRSet with the signature and key. This is only the cryptographic test, the signature
// validity period must be checked separately. The rrset is not checked for actually being an rrset. See
// [codeberg.org/miekg/dns/dnsutil.IsRRset], and neither is checked if the RRSIG's TypeCovered matches the
// type in rrset.
//
// This function copies the rdata of some RRs (to lowercase domain names) for the validation to work.
// It also checks that the Zone Key bit (RFC 4034 2.1.1) is set on the DNSKEY
// and that the Protocol field is set to 3 (RFC 4034 2.1.2). Options can not be nil
func (rr *RRSIG) Verify(k *DNSKEY, rrset []RR, options *SignOption) error {
if rr.KeyTag != k.KeyTag() || rr.Hdr.Class != k.Hdr.Class || rr.Algorithm != k.Algorithm {
return ErrKey
}
// RFC 4034 2.1.1 If bit 7 has value 0, then the DNSKEY record holds some
// other type of DNS public key and MUST NOT be used to verify RRSIGs that
// cover RRsets.
if k.Flags&FlagZONE == 0 {
return ErrKey
}
if k.Protocol != 3 || !EqualName(rr.SignerName, k.Hdr.Name) {
return ErrKey
}
if options.Pooler == nil {
options.Pooler = pool.NewNoop(defaultBufSize)
}
rr.Hdr.Name = rrset[0].Header().Name
signeddata := options.Get()
defer options.Put(signeddata)
// RFC 4035 5.3.2. Reconstructing the Signed Data
// Remove signature to get correct wiredata, and then set it again
signature := rr.Signature
rr.Signature = ""
n, err := rr.pack(signeddata, 0, nil)
if err != nil {
return err
}
rr.Signature = signature
m := rawSignatureData(signeddata[n:], rrset, rr)
signeddata = signeddata[:m+n]
sigbuf, _ := pack.Base64([]byte(rr.Signature))
var h hash.Hash
hash, ok := AlgorithmToHash[rr.Algorithm]
if !ok && rr.Algorithm != ED25519 {
return ErrAlg
}
switch rr.Algorithm {
case RSASHA1, RSASHA1NSEC3SHA1, RSASHA256, RSASHA512:
pubkey := k.publicKeyRSA()
if pubkey == nil {
return ErrKey
}
h = hash.New()
h.Write(signeddata)
return rsa.VerifyPKCS1v15(pubkey, hash, h.Sum(nil), sigbuf)
case ECDSAP256SHA256, ECDSAP384SHA384:
pubkey := k.publicKeyECDSA()
if pubkey == nil {
return ErrKey
}
// Split sigbuf into the r and s coordinates
r := new(big.Int).SetBytes(sigbuf[:len(sigbuf)/2])
s := new(big.Int).SetBytes(sigbuf[len(sigbuf)/2:])
h = hash.New()
h.Write(signeddata)
if ecdsa.Verify(pubkey, h.Sum(nil), r, s) {
return nil
}
return ErrSig
case ED25519:
pubkey := k.publicKeyED25519()
if pubkey == nil {
return ErrKey
}
if ed25519.Verify(pubkey, signeddata, sigbuf) {
return nil
}
return ErrSig
default:
return ErrAlg
}
}
// ValidPeriod uses RFC1982 serial arithmetic to calculate
// if a signature period is valid. If t is the zero time, the
// current time is taken other t is. Returns true if the signature
// is valid at the given time, otherwise returns false.
func (rr *RRSIG) ValidPeriod(t time.Time) bool {
var utc int64
if t.IsZero() {
utc = time.Now().UTC().Unix()
} else {
utc = t.UTC().Unix()
}
modi := (int64(rr.Inception) - utc) / MaxSerialIncrement
mode := (int64(rr.Expiration) - utc) / MaxSerialIncrement
ti := int64(rr.Inception) + modi*MaxSerialIncrement
te := int64(rr.Expiration) + mode*MaxSerialIncrement
return ti <= utc && utc <= te
}
// SignOption are options that are given to the signer and verifier.
type SignOption struct {
// If Pooler is set is will be used for all memory allocations. If nil the default pooler will be used and
// the buffers size used will be defaultBufSize
pool.Pooler
}
const defaultBufSize = 8192