summaryrefslogtreecommitdiff
path: root/tools/network/dnssec/trustchain.go
blob: 15f69aea1dc381f6437d006eae4eacca76035c77 (plain)
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
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package dnssec

import (
	"context"
	"fmt"
	"strings"
	"time"

	"github.com/algorand/go-deadlock"
	"github.com/miekg/dns"
)

// TrustQuerier wraps Querier and trusted root anchor retrieval for better testability
type TrustQuerier interface {
	Querier
	GetRootAnchorDS() ([]dns.DS, error)
}

type trustChain struct {
	client       TrustQuerier
	trustedZones map[string]trustedZone
	mu           deadlock.RWMutex
}

func makeTrustChain(c TrustQuerier) *trustChain {
	return &trustChain{
		client:       c,
		trustedZones: make(map[string]trustedZone),
	}
}

// QueryWrapper implements TrustQuerier
// GetRootAnchor is forwared to MakeRootTrustAnchor
type QueryWrapper struct {
	Querier
}

// GetRootAnchorDS returns DS from a real trust anchor
func (qw QueryWrapper) GetRootAnchorDS() (dss []dns.DS, err error) {
	a, err := MakeRootTrustAnchor()
	if err != nil {
		return
	}
	return a.ToDS(), nil
}

// must be called with the lock taken
func (t *trustChain) removeSelfAndChildren(zone string) {
	for k := range t.trustedZones {
		if strings.HasSuffix(k, zone) {
			delete(t.trustedZones, k)
		}
	}
}

// ensure checks that all zones from root till fqZoneName are valid and places them into a cache.
// It also performs cache invalidation: if child-parent authentication fails because of keys mismatch
// then parent zone is updated from the network and the process repeats.
// For example, example.com. is represented by 3 trusted zones: [".", "com.", "example.com."]
func (t *trustChain) ensure(ctx context.Context, fqZoneName string, keytags []uint16) error {
	// get zones from . to fqZoneName
	zones, err := splitToZones(fqZoneName)
	if err != nil {
		return err
	}

	zoneIdx := 0
	refreshedZones := make([]bool, len(zones)) // indexes of refreshed zones during the loop iterations
	// the loop goes over zones and cached trust chain and creates new entries if needed.
	// cache invalidation happens in these three cases:
	// 1. no ZSK in parent zone to check newly obtained DS
	// Explanation: DS is stored in parent and signed with its ZSK. If no such keys in the cache then ZSK rotation happened.
	// 2. DNSKEY signature (RRSIG) expired
	// 3. no ZSK in the last zone in cache
	// Explanation: server rotated ZSK and the cache does not have key to authenticate response.
	//
	// worst case: the last zone fails to find ZSK, it triggers refreshing back to root
	// causing 2 * len(zones) iterations
	// makeTrustedZone does not return cacheOutdated and refreshedZones has indication that the root was already updated
	// and the second fallback to the root zone will fail

	// this would not survive after granular locks and concurrent underlying zones removal
	t.mu.Lock()
	defer t.mu.Unlock()
	for {
		zone := zones[zoneIdx]
		tz, ok := t.trustedZones[zone]
		if !ok {
			if refreshedZones[zoneIdx] {
				return fmt.Errorf("cache outdated for already updated zone %s", zone)
			}
			var cacheOutdated bool
			parentZone := trustedZone{}
			if zoneIdx > 0 {
				parentZone = t.trustedZones[zones[zoneIdx-1]]
			}
			if tz, cacheOutdated, err = makeTrustedZone(ctx, zone, parentZone, t.client, time.Now()); err != nil {
				return err
			}
			if cacheOutdated {
				// Failed to validate DS using cached parent's keys
				// restart loop from parent
				zoneIdx--
				if zoneIdx < 0 {
					return fmt.Errorf("logic error: cache outdated for root zone")
				}
				parentZoneName := zones[zoneIdx]
				t.removeSelfAndChildren(parentZoneName)
				continue
			}
			// successfully created a new zone, record it
			t.trustedZones[zone] = tz
			refreshedZones[zoneIdx] = true
		}
		if tz.isExpired(time.Now()) {
			// remove current and all child zones and restart with the same zone
			t.removeSelfAndChildren(zone)
			continue
		}
		if zoneIdx == len(zones)-1 {
			// for the last zone also ensure that ZSK used to sign user-requested RR are also in place
			if !tz.checkKeys(keytags) {
				if refreshedZones[zoneIdx] {
					return fmt.Errorf("ZSK %v not found in zone %s", keytags, fqZoneName)
				}
				// seems like the latest zone
				t.removeSelfAndChildren(zone)
				continue
			}
		}

		zoneIdx++
		if zoneIdx >= len(zones) {
			break
		}
	}
	return nil
}

func (t *trustChain) getDNSKey(fqZoneName string, keyTag uint16) (key *dns.DNSKEY, found bool) {
	t.mu.RLock()
	trustedZone, ok := t.trustedZones[fqZoneName]
	t.mu.RUnlock()
	if !ok {
		return
	}
	k, found := trustedZone.zsk[keyTag]
	return &k, found
}

// Authenticate verifies a given rrset and its signatures validity down to the trusted root.
// Following steps are done:
// 1. Ensure the trust chain is valid. This requires keys' signature check back to the root if not cached
// 2. Check the signature using authenticated DNSKEY
func (t *trustChain) Authenticate(ctx context.Context, rrSet []dns.RR, rrSig []dns.RRSIG) (err error) {
	if len(rrSig) == 0 {
		return fmt.Errorf("empty RRSIG")
	}

	signer := rrSig[0].SignerName
	// sanity check: ensure all RRSIG contain the same signer, it must be the parent zone
	for i := 1; i < len(rrSig); i++ {
		if signer != rrSig[i].SignerName {
			return fmt.Errorf("signer name mismatch: %s vs %s", signer, rrSig[i].SignerName)
		}
	}

	fqdn := dns.Fqdn(signer)

	keytags := make([]uint16, 0, len(rrSig))
	for _, sig := range rrSig {
		keytags = append(keytags, sig.KeyTag)
	}

	// 1. ensure trust from the root to the signer
	// 2. check the keys are in place
	err = t.ensure(ctx, fqdn, keytags)
	if err != nil {
		return err
	}

	for _, sig := range rrSig {
		// get already authenticated ZSK
		key, ok := t.getDNSKey(fqdn, sig.KeyTag)
		if !ok {
			// skip, trustChain.ensure checks that at least one keytag is available
			continue
		}
		if err = sig.Verify(key, rrSet); err == nil {
			return nil
		}
	}
	return err
}