-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtrustanchor.go
More file actions
83 lines (69 loc) · 2.47 KB
/
trustanchor.go
File metadata and controls
83 lines (69 loc) · 2.47 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
package goresolver
import (
"errors"
"github.com/miekg/dns"
)
// ErrRootZoneNotTrusted is returned when the root zone DNSKEY does not
// match any configured trust anchor
var ErrRootZoneNotTrusted = errors.New("root zone DNSKEY does not match trust anchor")
// TrustAnchor represents a DNSSEC trust anchor for the root zone
type TrustAnchor struct {
dnskeys []*dns.DNSKEY
}
// defaultRootTrustAnchors contains the official IANA root zone trust anchors
// These are the KSK (Key Signing Key) records for the root zone
//
// KSK-2017 (Key Tag 20326) - Currently active
// Reference: https://www.iana.org/dnssec/files
var defaultRootTrustAnchors = []string{
// KSK-2017 (Key Tag 20326)
". IN DNSKEY 257 3 8 AwEAAaz/tAm8yTn4Mfeh5eyI96WSVexTBAvkMgJzkKTOiW1vkIbzxeF3+/4RgWOq7HrxRixHlFlExOLAJr5emLvN7SWXgnLh4+B5xQlNVz8Og8kvArMtNROxVQuCaSnIDdD5LKyWbRd2n9WGe2R8PzgCmr3EgVLrjyBxWezF0jLHwVN8efS3rCj/EWgvIWgb9tarpVUDK/b58Da+sqqls3eNbuv7pr+eoZG+SrDK6nWeL3c6H5Apxz7LjVc1uTIdsIXxuOLYA4/ilBmSVIzuDWfdRUfhHdY6+cn8HFRm+2hM8AnXGXws9555KrUB5qihylGa8subX2Nn6UwNR1AkUTV74bU=",
}
// NewTrustAnchor creates a new TrustAnchor with the default root zone
// trust anchors
func NewTrustAnchor() (*TrustAnchor, error) {
ta := &TrustAnchor{
dnskeys: make([]*dns.DNSKEY, 0, len(defaultRootTrustAnchors)),
}
for _, anchor := range defaultRootTrustAnchors {
rr, err := dns.NewRR(anchor)
if err != nil {
return nil, err
}
dnskey, ok := rr.(*dns.DNSKEY)
if !ok {
return nil, errors.New("trust anchor is not a DNSKEY record")
}
ta.dnskeys = append(ta.dnskeys, dnskey)
}
return ta, nil
}
// VerifyRootZone validates that the root zone DNSKEY matches one of the
// configured trust anchors. It returns nil if validation succeeds.
func (ta *TrustAnchor) VerifyRootZone(rootZone SignedZone) error {
if rootZone.zone != "." {
return errors.New("not a root zone")
}
// Check that at least one of the KSKs in the root zone matches
// a trust anchor
for _, trustAnchor := range ta.dnskeys {
trustAnchorKeyTag := trustAnchor.KeyTag()
// Look up the key in the root zone by key tag
rootKey := rootZone.lookupPubKey(trustAnchorKeyTag)
if rootKey == nil {
continue
}
// Compare the DNSKEY records
if keysMatch(trustAnchor, rootKey) {
return nil
}
}
return ErrRootZoneNotTrusted
}
// keysMatch compares two DNSKEY records for equality
func keysMatch(a, b *dns.DNSKEY) bool {
return a.Flags == b.Flags &&
a.Protocol == b.Protocol &&
a.Algorithm == b.Algorithm &&
a.PublicKey == b.PublicKey
}