forked from in-toto/go-witness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.go
135 lines (114 loc) · 4.24 KB
/
verify.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
// 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 witness
import (
"context"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"github.com/in-toto/go-witness/cryptoutil"
"github.com/in-toto/go-witness/dsse"
"github.com/in-toto/go-witness/policy"
"github.com/in-toto/go-witness/source"
"github.com/in-toto/go-witness/timestamp"
)
func VerifySignature(r io.Reader, verifiers ...cryptoutil.Verifier) (dsse.Envelope, error) {
decoder := json.NewDecoder(r)
envelope := dsse.Envelope{}
if err := decoder.Decode(&envelope); err != nil {
return envelope, fmt.Errorf("failed to parse dsse envelope: %w", err)
}
_, err := envelope.Verify(dsse.VerifyWithVerifiers(verifiers...))
return envelope, err
}
type verifyOptions struct {
policyEnvelope dsse.Envelope
policyVerifiers []cryptoutil.Verifier
collectionSource source.Sourcer
subjectDigests []string
}
type VerifyOption func(*verifyOptions)
func VerifyWithSubjectDigests(subjectDigests []cryptoutil.DigestSet) VerifyOption {
return func(vo *verifyOptions) {
for _, set := range subjectDigests {
for _, digest := range set {
vo.subjectDigests = append(vo.subjectDigests, digest)
}
}
}
}
func VerifyWithCollectionSource(source source.Sourcer) VerifyOption {
return func(vo *verifyOptions) {
vo.collectionSource = source
}
}
// Verify verifies a set of attestations against a provided policy. The set of attestations that satisfy the policy will be returned
// if verifiation is successful.
func Verify(ctx context.Context, policyEnvelope dsse.Envelope, policyVerifiers []cryptoutil.Verifier, opts ...VerifyOption) (map[string][]source.VerifiedCollection, error) {
vo := verifyOptions{
policyEnvelope: policyEnvelope,
policyVerifiers: policyVerifiers,
}
for _, opt := range opts {
opt(&vo)
}
if _, err := vo.policyEnvelope.Verify(dsse.VerifyWithVerifiers(vo.policyVerifiers...)); err != nil {
return nil, fmt.Errorf("could not verify policy: %w", err)
}
pol := policy.Policy{}
if err := json.Unmarshal(vo.policyEnvelope.Payload, &pol); err != nil {
return nil, fmt.Errorf("failed to unmarshal policy from envelope: %w", err)
}
pubKeysById, err := pol.PublicKeyVerifiers()
if err != nil {
return nil, fmt.Errorf("failed to get pulic keys from policy: %w", err)
}
pubkeys := make([]cryptoutil.Verifier, 0)
for _, pubkey := range pubKeysById {
pubkeys = append(pubkeys, pubkey)
}
trustBundlesById, err := pol.TrustBundles()
if err != nil {
return nil, fmt.Errorf("failed to load policy trust bundles: %w", err)
}
roots := make([]*x509.Certificate, 0)
intermediates := make([]*x509.Certificate, 0)
for _, trustBundle := range trustBundlesById {
roots = append(roots, trustBundle.Root)
intermediates = append(intermediates, intermediates...)
}
timestampAuthoritiesById, err := pol.TimestampAuthorityTrustBundles()
if err != nil {
return nil, fmt.Errorf("failed to load policy timestamp authorities: %w", err)
}
timestampVerifiers := make([]dsse.TimestampVerifier, 0)
for _, timestampAuthority := range timestampAuthoritiesById {
certs := []*x509.Certificate{timestampAuthority.Root}
certs = append(certs, timestampAuthority.Intermediates...)
timestampVerifiers = append(timestampVerifiers, timestamp.NewVerifier(timestamp.VerifyWithCerts(certs)))
}
verifiedSource := source.NewVerifiedSource(
vo.collectionSource,
dsse.VerifyWithVerifiers(pubkeys...),
dsse.VerifyWithRoots(roots...),
dsse.VerifyWithIntermediates(intermediates...),
dsse.VerifyWithTimestampVerifiers(timestampVerifiers...),
)
accepted, err := pol.Verify(ctx, policy.WithSubjectDigests(vo.subjectDigests), policy.WithVerifiedSource(verifiedSource))
if err != nil {
return nil, fmt.Errorf("failed to verify policy: %w", err)
}
return accepted, nil
}