-
Notifications
You must be signed in to change notification settings - Fork 16
/
policy.go
384 lines (320 loc) · 11 KB
/
policy.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
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
// Copyright 2021 The Witness Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package policy
import (
"bytes"
"context"
"crypto/x509"
"errors"
"fmt"
"strings"
"time"
"github.com/in-toto/go-witness/attestation"
"github.com/in-toto/go-witness/cryptoutil"
"github.com/in-toto/go-witness/signer/kms"
"github.com/in-toto/go-witness/source"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const PolicyPredicate = "https://witness.testifysec.com/policy/v0.1"
// +kubebuilder:object:generate=true
type Policy struct {
Expires metav1.Time `json:"expires"`
Roots map[string]Root `json:"roots,omitempty"`
TimestampAuthorities map[string]Root `json:"timestampauthorities,omitempty"`
PublicKeys map[string]PublicKey `json:"publickeys,omitempty"`
Steps map[string]Step `json:"steps"`
}
// +kubebuilder:object:generate=true
type Root struct {
Certificate []byte `json:"certificate"`
Intermediates [][]byte `json:"intermediates,omitempty"`
}
// +kubebuilder:object:generate=true
type PublicKey struct {
KeyID string `json:"keyid"`
Key []byte `json:"key"`
}
// PublicKeyVerifiers returns verifiers for each of the policy's embedded public keys grouped by the key's ID
func (p Policy) PublicKeyVerifiers() (map[string]cryptoutil.Verifier, error) {
verifiers := make(map[string]cryptoutil.Verifier)
var err error
for _, key := range p.PublicKeys {
var verifier cryptoutil.Verifier
for _, prefix := range kms.SupportedProviders() {
if strings.HasPrefix(key.KeyID, prefix) {
verifier, err = kms.New(kms.WithRef(key.KeyID), kms.WithHash("SHA256")).Verifier(context.TODO())
if err != nil {
return nil, fmt.Errorf("KMS Key ID recognized but not valid: %w", err)
}
}
}
if verifier == nil {
verifier, err = cryptoutil.NewVerifierFromReader(bytes.NewReader(key.Key))
if err != nil {
return nil, err
}
}
keyID, err := verifier.KeyID()
if err != nil {
return nil, err
}
if keyID != key.KeyID {
return nil, ErrKeyIDMismatch{
Expected: key.KeyID,
Actual: keyID,
}
}
verifiers[keyID] = verifier
}
return verifiers, nil
}
type TrustBundle struct {
Root *x509.Certificate
Intermediates []*x509.Certificate
}
// TrustBundles returns the policy's x509 roots and intermediates grouped by the root's ID
func (p Policy) TrustBundles() (map[string]TrustBundle, error) {
return trustBundlesFromRoots(p.Roots)
}
func (p Policy) TimestampAuthorityTrustBundles() (map[string]TrustBundle, error) {
return trustBundlesFromRoots(p.TimestampAuthorities)
}
func trustBundlesFromRoots(roots map[string]Root) (map[string]TrustBundle, error) {
bundles := make(map[string]TrustBundle)
for id, root := range roots {
bundle := TrustBundle{}
var err error
bundle.Root, err = cryptoutil.TryParseCertificate(root.Certificate)
if err != nil {
return bundles, err
}
for _, intBytes := range root.Intermediates {
cert, err := cryptoutil.TryParseCertificate(intBytes)
if err != nil {
return bundles, err
}
bundle.Intermediates = append(bundle.Intermediates, cert)
}
bundles[id] = bundle
}
return bundles, nil
}
type VerifyOption func(*verifyOptions)
type verifyOptions struct {
verifiedSource source.VerifiedSourcer
subjectDigests []string
searchDepth int
}
func WithVerifiedSource(verifiedSource source.VerifiedSourcer) VerifyOption {
return func(vo *verifyOptions) {
vo.verifiedSource = verifiedSource
}
}
func WithSubjectDigests(subjectDigests []string) VerifyOption {
return func(vo *verifyOptions) {
vo.subjectDigests = subjectDigests
}
}
func WithSearchDepth(depth int) VerifyOption {
return func(vo *verifyOptions) {
vo.searchDepth = depth
}
}
func checkVerifyOpts(vo *verifyOptions) error {
if vo.verifiedSource == nil {
return ErrInvalidOption{
Option: "verified source",
Reason: "a verified attestation source is required",
}
}
if len(vo.subjectDigests) == 0 {
return ErrInvalidOption{
Option: "subject digests",
Reason: "at least one subject digest is required",
}
}
if vo.searchDepth < 1 {
return ErrInvalidOption{
Option: "search depth",
Reason: "search depth must be at least 1",
}
}
return nil
}
func (p Policy) Verify(ctx context.Context, opts ...VerifyOption) (bool, map[string]StepResult, error) {
vo := &verifyOptions{
searchDepth: 3,
}
for _, opt := range opts {
opt(vo)
}
if err := checkVerifyOpts(vo); err != nil {
return false, nil, err
}
if time.Now().After(p.Expires.Time) {
return false, nil, ErrPolicyExpired(p.Expires.Time)
}
trustBundles, err := p.TrustBundles()
if err != nil {
return false, nil, err
}
attestationsByStep := make(map[string][]string)
for name, step := range p.Steps {
for _, attestation := range step.Attestations {
attestationsByStep[name] = append(attestationsByStep[name], attestation.Type)
}
}
resultsByStep := make(map[string]StepResult)
for depth := 0; depth < vo.searchDepth; depth++ {
for stepName, step := range p.Steps {
// Use search to get all the attestations that match the supplied step name and subjects
collections, err := vo.verifiedSource.Search(ctx, stepName, vo.subjectDigests, attestationsByStep[stepName])
if err != nil {
return false, nil, err
}
if len(collections) == 0 {
collections = append(collections, source.CollectionVerificationResult{Errors: []error{ErrNoCollections{Step: stepName}}})
}
// Verify the functionaries
collections = step.checkFunctionaries(collections, trustBundles)
stepResult := step.validateAttestations(collections)
// We perform many searches against the same step, so we need to merge the relevant fields
if resultsByStep[stepName].Step == "" {
resultsByStep[stepName] = stepResult
} else {
if result, ok := resultsByStep[stepName]; ok {
result.Passed = append(result.Passed, stepResult.Passed...)
result.Rejected = append(result.Rejected, stepResult.Rejected...)
resultsByStep[stepName] = result
}
}
for _, coll := range stepResult.Passed {
for _, digestSet := range coll.Collection.BackRefs() {
for _, digest := range digestSet {
vo.subjectDigests = append(vo.subjectDigests, digest)
}
}
}
}
}
resultsByStep, err = p.verifyArtifacts(resultsByStep)
if err != nil {
return false, nil, fmt.Errorf("failed to verify artifacts: %w", err)
}
pass := true
for _, result := range resultsByStep {
p := result.Analyze()
if !p {
pass = false
}
}
return pass, resultsByStep, nil
}
// checkFunctionaries checks to make sure the signature on each statement corresponds to a trusted functionary for
// the step the statement corresponds to
func (step Step) checkFunctionaries(statements []source.CollectionVerificationResult, trustBundles map[string]TrustBundle) []source.CollectionVerificationResult {
for i, statement := range statements {
// Check that the statement contains a predicate type that we accept
if statement.Statement.PredicateType != attestation.CollectionType {
statements[i].Errors = append(statement.Errors, fmt.Errorf("predicate type %v is not a collection predicate type", statement.Statement.PredicateType))
}
if len(statement.Verifiers) > 0 {
for _, verifier := range statement.Verifiers {
for _, functionary := range step.Functionaries {
if err := functionary.Validate(verifier, trustBundles); err != nil {
statements[i].Warnings = append(statement.Warnings, fmt.Sprintf("failed to validate functionary of KeyID %s in step %s: %s", functionary.PublicKeyID, step.Name, err.Error()))
continue
} else {
statements[i].ValidFunctionaries = append(statement.ValidFunctionaries, verifier)
}
}
}
} else {
statements[i].Errors = append(statement.Errors, fmt.Errorf("no verifiers present to validate against collection verifiers"))
}
}
return statements
}
// verifyArtifacts will check the artifacts (materials+products) of the step referred to by `ArtifactsFrom` against the
// materials of the original step. This ensures file integrity between each step.
func (p Policy) verifyArtifacts(resultsByStep map[string]StepResult) (map[string]StepResult, error) {
for _, step := range p.Steps {
accepted := false
if len(resultsByStep[step.Name].Passed) == 0 {
if result, ok := resultsByStep[step.Name]; ok {
result.Rejected = append(result.Rejected, RejectedCollection{Reason: fmt.Errorf("failed to verify artifacts for step %s: no passed collections present", step.Name)})
resultsByStep[step.Name] = result
} else {
return nil, fmt.Errorf("failed to find step %s in step results map", step.Name)
}
continue
}
reasons := []error{}
for _, collection := range resultsByStep[step.Name].Passed {
if err := verifyCollectionArtifacts(step, collection, resultsByStep); err == nil {
accepted = true
} else {
reasons = append(reasons, err)
}
}
if !accepted {
// can't address the map fields directly so have to make a copy and overwrite
if result, ok := resultsByStep[step.Name]; ok {
reject := RejectedCollection{Reason: fmt.Errorf("failed to verify artifacts for step %s: ", step.Name)}
for _, reason := range reasons {
reject.Reason = errors.Join(reject.Reason, reason)
}
result.Rejected = append(result.Rejected, reject)
result.Passed = []source.CollectionVerificationResult{}
resultsByStep[step.Name] = result
}
}
}
return resultsByStep, nil
}
func verifyCollectionArtifacts(step Step, collection source.CollectionVerificationResult, collectionsByStep map[string]StepResult) error {
mats := collection.Collection.Materials()
reasons := []string{}
for _, artifactsFrom := range step.ArtifactsFrom {
accepted := make([]source.CollectionVerificationResult, 0)
for _, testCollection := range collectionsByStep[artifactsFrom].Passed {
if err := compareArtifacts(mats, testCollection.Collection.Artifacts()); err != nil {
collection.Warnings = append(collection.Warnings, fmt.Sprintf("failed to verify artifacts for step %s: %v", step.Name, err))
reasons = append(reasons, err.Error())
break
}
accepted = append(accepted, testCollection)
}
if len(accepted) <= 0 {
return ErrVerifyArtifactsFailed{Reasons: reasons}
}
}
return nil
}
func compareArtifacts(mats map[string]cryptoutil.DigestSet, arts map[string]cryptoutil.DigestSet) error {
for path, mat := range mats {
art, ok := arts[path]
if !ok {
continue
}
if !mat.Equal(art) {
return ErrMismatchArtifact{
Artifact: art,
Material: mat,
Path: path,
}
}
}
return nil
}