forked from sigstore/cosign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify_attestation.go
227 lines (206 loc) · 6.68 KB
/
verify_attestation.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
//
// Copyright 2021 The Sigstore Authors.
//
// 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 verify
import (
"context"
"crypto"
"flag"
"fmt"
"os"
"path/filepath"
"github.com/google/go-containerregistry/pkg/name"
"github.com/pkg/errors"
"github.com/sigstore/cosign/pkg/cosign/pkcs11key"
"github.com/sigstore/cosign/pkg/cosign/rego"
"github.com/sigstore/cosign/pkg/oci"
"github.com/sigstore/sigstore/pkg/signature"
"github.com/sigstore/cosign/cmd/cosign/cli/fulcio"
"github.com/sigstore/cosign/cmd/cosign/cli/options"
"github.com/sigstore/cosign/cmd/cosign/cli/rekor"
"github.com/sigstore/cosign/pkg/cosign"
"github.com/sigstore/cosign/pkg/cosign/cue"
"github.com/sigstore/cosign/pkg/cosign/pivkey"
"github.com/sigstore/cosign/pkg/policy"
sigs "github.com/sigstore/cosign/pkg/signature"
)
// VerifyAttestationCommand verifies a signature on a supplied container image
// nolint
type VerifyAttestationCommand struct {
options.RegistryOptions
CheckClaims bool
KeyRef string
CertRef string
CertEmail string
CertOidcIssuer string
CertChain string
EnforceSCT bool
Sk bool
Slot string
Output string
RekorURL string
PredicateType string
Policies []string
LocalImage bool
}
// Exec runs the verification command
func (c *VerifyAttestationCommand) Exec(ctx context.Context, images []string) (err error) {
if len(images) == 0 {
return flag.ErrHelp
}
if !options.OneOf(c.KeyRef, c.Sk, c.CertRef) && !options.EnableExperimental() {
return &options.PubKeyParseError{}
}
ociremoteOpts, err := c.ClientOpts(ctx)
if err != nil {
return errors.Wrap(err, "constructing client options")
}
co := &cosign.CheckOpts{
RegistryClientOpts: ociremoteOpts,
CertEmail: c.CertEmail,
CertOidcIssuer: c.CertOidcIssuer,
EnforceSCT: c.EnforceSCT,
}
if c.CheckClaims {
co.ClaimVerifier = cosign.IntotoSubjectClaimVerifier
}
if options.EnableExperimental() {
if c.RekorURL != "" {
rekorClient, err := rekor.NewClient(c.RekorURL)
if err != nil {
return errors.Wrap(err, "creating Rekor client")
}
co.RekorClient = rekorClient
}
co.RootCerts = fulcio.GetRoots()
co.IntermediateCerts = fulcio.GetIntermediates()
}
keyRef := c.KeyRef
// Keys are optional!
switch {
case keyRef != "":
co.SigVerifier, err = sigs.PublicKeyFromKeyRef(ctx, keyRef)
if err != nil {
return errors.Wrap(err, "loading public key")
}
pkcs11Key, ok := co.SigVerifier.(*pkcs11key.Key)
if ok {
defer pkcs11Key.Close()
}
case c.Sk:
sk, err := pivkey.GetKeyWithSlot(c.Slot)
if err != nil {
return errors.Wrap(err, "opening piv token")
}
defer sk.Close()
co.SigVerifier, err = sk.Verifier()
if err != nil {
return errors.Wrap(err, "initializing piv token verifier")
}
case c.CertRef != "":
cert, err := loadCertFromFileOrURL(c.CertRef)
if err != nil {
return errors.Wrap(err, "loading certificate from reference")
}
if c.CertChain == "" {
co.SigVerifier, err = signature.LoadVerifier(cert.PublicKey, crypto.SHA256)
if err != nil {
return errors.Wrap(err, "creating certificate verifier")
}
} else {
// Verify certificate with chain
chain, err := loadCertChainFromFileOrURL(c.CertChain)
if err != nil {
return err
}
co.SigVerifier, err = cosign.ValidateAndUnpackCertWithChain(cert, chain, co)
if err != nil {
return errors.Wrap(err, "creating certificate verifier")
}
}
}
// NB: There are only 2 kinds of verification right now:
// 1. You gave us the public key explicitly to verify against so co.SigVerifier is non-nil or,
// 2. We're going to find an x509 certificate on the signature and verify against Fulcio root trust
// TODO(nsmith5): Refactor this verification logic to pass back _how_ verification
// was performed so we don't need to use this fragile logic here.
fulcioVerified := (co.SigVerifier == nil)
for _, imageRef := range images {
var verified []oci.Signature
var bundleVerified bool
if c.LocalImage {
verified, bundleVerified, err = cosign.VerifyLocalImageAttestations(ctx, imageRef, co)
if err != nil {
return err
}
} else {
ref, err := name.ParseReference(imageRef)
if err != nil {
return err
}
verified, bundleVerified, err = cosign.VerifyImageAttestations(ctx, ref, co)
if err != nil {
return err
}
}
var cuePolicies, regoPolicies []string
for _, policy := range c.Policies {
switch filepath.Ext(policy) {
case ".rego":
regoPolicies = append(regoPolicies, policy)
case ".cue":
cuePolicies = append(cuePolicies, policy)
default:
return errors.New("invalid policy format, expected .cue or .rego")
}
}
var validationErrors []error
for _, vp := range verified {
payload, err := policy.AttestationToPayloadJSON(ctx, c.PredicateType, vp)
if err != nil {
return errors.Wrap(err, "converting to consumable policy validation")
}
if len(payload) == 0 {
// This is not the predicate type we're looking for.
continue
}
if len(cuePolicies) > 0 {
fmt.Fprintf(os.Stderr, "will be validating against CUE policies: %v\n", cuePolicies)
cueValidationErr := cue.ValidateJSON(payload, cuePolicies)
if cueValidationErr != nil {
validationErrors = append(validationErrors, cueValidationErr)
}
}
if len(regoPolicies) > 0 {
fmt.Fprintf(os.Stderr, "will be validating against Rego policies: %v\n", regoPolicies)
regoValidationErrs := rego.ValidateJSON(payload, regoPolicies)
if len(regoValidationErrs) > 0 {
validationErrors = append(validationErrors, regoValidationErrs...)
}
}
}
if len(validationErrors) > 0 {
fmt.Fprintf(os.Stderr, "There are %d number of errors occurred during the validation:\n", len(validationErrors))
for _, v := range validationErrors {
_, _ = fmt.Fprintf(os.Stderr, "- %v\n", v)
}
return fmt.Errorf("%d validation errors occurred", len(validationErrors))
}
// TODO: add CUE validation report to `PrintVerificationHeader`.
PrintVerificationHeader(imageRef, co, bundleVerified, fulcioVerified)
// The attestations are always JSON, so use the raw "text" mode for outputting them instead of conversion
PrintVerification(imageRef, verified, "text")
}
return nil
}