-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
tuf.go
233 lines (202 loc) · 5.57 KB
/
tuf.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2022-present Datadog, Inc.
package state
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strconv"
"strings"
"github.com/DataDog/go-tuf/client"
"github.com/DataDog/go-tuf/data"
"github.com/DataDog/go-tuf/util"
"github.com/DataDog/go-tuf/verify"
)
type tufRootsClient struct {
rootClient *client.Client
rootLocalStore client.LocalStore
rootRemoteStore *rootClientRemoteStore
}
func newTufRootsClient(root []byte) (*tufRootsClient, error) {
rootLocalStore := client.MemoryLocalStore()
rootRemoteStore := &rootClientRemoteStore{}
rootClient := client.NewClient(rootLocalStore, rootRemoteStore)
err := rootClient.InitLocal(root)
if err != nil {
return nil, err
}
return &tufRootsClient{
rootClient: rootClient,
rootLocalStore: rootLocalStore,
rootRemoteStore: rootRemoteStore,
}, nil
}
func (trc *tufRootsClient) clone() (*tufRootsClient, error) {
root, err := trc.latestRootRaw()
if err != nil {
return nil, err
}
return newTufRootsClient(root)
}
func (trc *tufRootsClient) updateRoots(newRoots [][]byte) error {
if len(newRoots) == 0 {
return nil
}
trc.rootRemoteStore.roots = append(trc.rootRemoteStore.roots, newRoots...)
return trc.rootClient.UpdateRoots()
}
func (trc *tufRootsClient) latestRoot() (*data.Root, error) {
raw, err := trc.latestRootRaw()
if err != nil {
return nil, err
}
return unsafeUnmarshalRoot(raw)
}
func (trc *tufRootsClient) latestRootRaw() ([]byte, error) {
metas, err := trc.rootLocalStore.GetMeta()
if err != nil {
return nil, err
}
rawRoot := metas["root.json"]
return rawRoot, nil
}
func (trc *tufRootsClient) validateTargets(rawTargets []byte) (*data.Targets, error) {
root, err := trc.latestRoot()
if err != nil {
return nil, err
}
db := verify.NewDB()
for _, key := range root.Keys {
for _, id := range key.IDs() {
if err := db.AddKey(id, key); err != nil {
return nil, err
}
}
}
targetsRole, hasRoleTargets := root.Roles["targets"]
if !hasRoleTargets {
return nil, fmt.Errorf("root is missing a targets role")
}
role := &data.Role{Threshold: targetsRole.Threshold, KeyIDs: targetsRole.KeyIDs}
if err := db.AddRole("targets", role); err != nil {
return nil, fmt.Errorf("could not add targets role to db: %v", err)
}
var targets data.Targets
err = db.Unmarshal(rawTargets, &targets, "targets", 0)
if err != nil {
return nil, err
}
return &targets, nil
}
type rootClientRemoteStore struct {
roots [][]byte
}
func (s *rootClientRemoteStore) GetMeta(name string) (stream io.ReadCloser, size int64, err error) {
metaPath, err := parseMetaPath(name)
if err != nil {
return nil, 0, err
}
if metaPath.role != roleRoot || !metaPath.versionSet {
return nil, 0, client.ErrNotFound{File: name}
}
for _, root := range s.roots {
parsedRoot, err := unsafeUnmarshalRoot(root)
if err != nil {
return nil, 0, err
}
if parsedRoot.Version == metaPath.version {
return io.NopCloser(bytes.NewReader(root)), int64(len(root)), nil
}
}
return nil, 0, client.ErrNotFound{File: name}
}
func (s *rootClientRemoteStore) GetTarget(path string) (stream io.ReadCloser, size int64, err error) {
return nil, 0, client.ErrNotFound{File: path}
}
type role string
const (
roleRoot role = "root"
)
type metaPath struct {
role role
version int64
versionSet bool
}
func parseMetaPath(rawMetaPath string) (metaPath, error) {
splitRawMetaPath := strings.SplitN(rawMetaPath, ".", 3)
if len(splitRawMetaPath) != 2 && len(splitRawMetaPath) != 3 {
return metaPath{}, fmt.Errorf("invalid metadata path '%s'", rawMetaPath)
}
suffix := splitRawMetaPath[len(splitRawMetaPath)-1]
if suffix != "json" {
return metaPath{}, fmt.Errorf("invalid metadata path (suffix) '%s'", rawMetaPath)
}
rawRole := splitRawMetaPath[len(splitRawMetaPath)-2]
if rawRole == "" {
return metaPath{}, fmt.Errorf("invalid metadata path (role) '%s'", rawMetaPath)
}
if len(splitRawMetaPath) == 2 {
return metaPath{
role: role(rawRole),
}, nil
}
rawVersion, err := strconv.ParseInt(splitRawMetaPath[0], 10, 64)
if err != nil {
return metaPath{}, fmt.Errorf("invalid metadata path (version) '%s': %w", rawMetaPath, err)
}
return metaPath{
role: role(rawRole),
version: rawVersion,
versionSet: true,
}, nil
}
func validateTargetFileHash(targetMeta data.TargetFileMeta, targetFile []byte) error {
if len(targetMeta.HashAlgorithms()) == 0 {
return fmt.Errorf("target file has no hash")
}
generatedMeta, err := util.GenerateFileMeta(bytes.NewBuffer(targetFile), targetMeta.HashAlgorithms()...)
if err != nil {
return err
}
err = util.FileMetaEqual(targetMeta.FileMeta, generatedMeta)
if err != nil {
return err
}
return nil
}
func unsafeUnmarshalRoot(raw []byte) (*data.Root, error) {
var signedRoot data.Signed
err := json.Unmarshal(raw, &signedRoot)
if err != nil {
return nil, err
}
var root data.Root
err = json.Unmarshal(signedRoot.Signed, &root)
if err != nil {
return nil, err
}
return &root, err
}
func unsafeUnmarshalTargets(raw []byte) (*data.Targets, error) {
var signedTargets data.Signed
err := json.Unmarshal(raw, &signedTargets)
if err != nil {
return nil, err
}
var targets data.Targets
err = json.Unmarshal(signedTargets.Signed, &targets)
if err != nil {
return nil, err
}
return &targets, err
}
func extractRootVersion(raw []byte) (int64, error) {
root, err := unsafeUnmarshalRoot(raw)
if err != nil {
return 0, err
}
return root.Version, nil
}