forked from sigstore/sigstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.go
273 lines (235 loc) · 8.37 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
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
//
// 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/x509"
"encoding/json"
"flag"
"fmt"
"os"
"github.com/peterbourgon/ff/v3/ffcli"
"github.com/pkg/errors"
"github.com/sigstore/cosign/cmd/cosign/cli/fulcio"
"github.com/sigstore/cosign/cmd/cosign/cli/options"
"github.com/sigstore/cosign/cmd/cosign/cli/sign"
"github.com/sigstore/cosign/pkg/cosign"
"github.com/sigstore/cosign/pkg/cosign/pivkey"
"github.com/sigstore/cosign/pkg/oci"
sigs "github.com/sigstore/cosign/pkg/signature"
"github.com/sigstore/sigstore/pkg/signature"
"github.com/sigstore/sigstore/pkg/signature/payload"
)
// VerifyCommand verifies a signature on a supplied container image
// nolint
type VerifyCommand struct {
options.RegistryOpts
CheckClaims bool
KeyRef string
CertEmail string
Sk bool
Slot string
Output string
RekorURL string
Attachment string
Annotations *map[string]interface{}
}
func ApplyVerifyFlags(cmd *VerifyCommand, flagset *flag.FlagSet) {
annotations := sigs.AnnotationsMap{}
flagset.StringVar(&cmd.KeyRef, "key", "", "path to the public key file, URL, KMS URI or Kubernetes Secret")
flagset.StringVar(&cmd.CertEmail, "cert-email", "", "the email expected in a valid fulcio cert")
flagset.BoolVar(&cmd.Sk, "sk", false, "whether to use a hardware security key")
flagset.StringVar(&cmd.Slot, "slot", "", "security key slot to use for generated key (default: signature) (authentication|signature|card-authentication|key-management)")
flagset.StringVar(&cmd.RekorURL, "rekor-url", "https://rekor.sigstore.dev", "address of rekor STL server")
flagset.BoolVar(&cmd.CheckClaims, "check-claims", true, "whether to check the claims found")
flagset.StringVar(&cmd.Output, "output", "json", "output format for the signing image information (default JSON) (json|text)")
flagset.StringVar(&cmd.Attachment, "attachment", "", "related image attachment to sign (none|sbom), default none")
options.ApplyRegistryFlags(&cmd.RegistryOpts, flagset)
// parse annotations
flagset.Var(&annotations, "a", "extra key=value pairs to sign")
cmd.Annotations = &annotations.Annotations
}
// Verify builds and returns an ffcli command
func Verify() *ffcli.Command {
cmd := VerifyCommand{}
flagset := flag.NewFlagSet("cosign verify", flag.ExitOnError)
ApplyVerifyFlags(&cmd, flagset)
return &ffcli.Command{
Name: "verify",
ShortUsage: "cosign verify -key <key path>|<key url>|<kms uri> <image uri> [<image uri> ...]",
ShortHelp: "Verify a signature on the supplied container image",
LongHelp: `Verify signature and annotations on an image by checking the claims
against the transparency log.
EXAMPLES
# verify cosign claims and signing certificates on the image
cosign verify <IMAGE>
# verify multiple images
cosign verify <IMAGE_1> <IMAGE_2> ...
# additionally verify specified annotations
cosign verify -a key1=val1 -a key2=val2 <IMAGE>
# (experimental) additionally, verify with the transparency log
COSIGN_EXPERIMENTAL=1 cosign verify <IMAGE>
# verify image with public key
cosign verify -key cosign.pub <IMAGE>
# verify image with public key provided by URL
cosign verify -key https://host.for/[FILE] <IMAGE>
# verify image with public key stored in Google Cloud KMS
cosign verify -key gcpkms://projects/[PROJECT]/locations/global/keyRings/[KEYRING]/cryptoKeys/[KEY] <IMAGE>
# verify image with public key stored in Hashicorp Vault
cosign verify -key hashivault://[KEY] <IMAGE>
# verify image with public key stored in a Kubernetes secret
cosign verify -key k8s://[NAMESPACE]/[KEY] <IMAGE>`,
FlagSet: flagset,
Exec: cmd.Exec,
}
}
// Exec runs the verification command
func (c *VerifyCommand) Exec(ctx context.Context, args []string) (err error) {
if len(args) == 0 {
return flag.ErrHelp
}
switch c.Attachment {
case "sbom", "":
break
default:
return flag.ErrHelp
}
if !options.OneOf(c.KeyRef, c.Sk) && !options.EnableExperimental() {
return &options.KeyParseError{}
}
co := &cosign.CheckOpts{
Annotations: *c.Annotations,
RegistryClientOpts: c.RegistryOpts.ClientOpts(ctx),
}
if c.CheckClaims {
co.ClaimVerifier = cosign.SimpleClaimVerifier
}
if options.EnableExperimental() {
co.RekorURL = c.RekorURL
co.RootCerts = fulcio.GetRoots()
}
keyRef := c.KeyRef
// Keys are optional!
var pubKey signature.Verifier
if keyRef != "" {
pubKey, err = sigs.PublicKeyFromKeyRef(ctx, keyRef)
if err != nil {
return errors.Wrap(err, "loading public key")
}
} else if c.Sk {
sk, err := pivkey.GetKeyWithSlot(c.Slot)
if err != nil {
return errors.Wrap(err, "opening piv token")
}
defer sk.Close()
pubKey, err = sk.Verifier()
if err != nil {
return errors.Wrap(err, "initializing piv token verifier")
}
}
co.SigVerifier = pubKey
for _, img := range args {
ref, err := sign.GetAttachedImageRef(img, c.Attachment, c.RegistryOpts.GetRegistryClientOpts(ctx)...)
if err != nil {
return errors.Wrapf(err, "resolving attachment type %s for image %s", c.Attachment, img)
}
verified, bundleVerified, err := cosign.VerifySignatures(ctx, ref, co)
if err != nil {
return err
}
PrintVerificationHeader(ref.Name(), co, bundleVerified)
PrintVerification(ref.Name(), verified, c.Output)
}
return nil
}
func PrintVerificationHeader(imgRef string, co *cosign.CheckOpts, bundleVerified bool) {
fmt.Fprintf(os.Stderr, "\nVerification for %s --\n", imgRef)
fmt.Fprintln(os.Stderr, "The following checks were performed on each of these signatures:")
if co.ClaimVerifier != nil {
if co.Annotations != nil {
fmt.Fprintln(os.Stderr, " - The specified annotations were verified.")
}
fmt.Fprintln(os.Stderr, " - The cosign claims were validated")
}
if bundleVerified {
fmt.Fprintln(os.Stderr, " - Existence of the claims in the transparency log was verified offline")
} else if co.RekorURL != "" {
fmt.Fprintln(os.Stderr, " - The claims were present in the transparency log")
fmt.Fprintln(os.Stderr, " - The signatures were integrated into the transparency log when the certificate was valid")
}
if co.SigVerifier != nil {
fmt.Fprintln(os.Stderr, " - The signatures were verified against the specified public key")
}
fmt.Fprintln(os.Stderr, " - Any certificates were verified against the Fulcio roots.")
}
func certSubject(c *x509.Certificate) string {
switch {
case c.EmailAddresses != nil:
return c.EmailAddresses[0]
case c.URIs != nil:
return c.URIs[0].String()
}
return ""
}
// PrintVerification logs details about the verification to stdout
func PrintVerification(imgRef string, verified []oci.Signature, output string) {
switch output {
case "text":
for _, sig := range verified {
if cert, err := sig.Cert(); err == nil && cert != nil {
fmt.Println("Certificate subject: ", certSubject(cert))
}
p, err := sig.Payload()
if err != nil {
fmt.Fprintf(os.Stderr, "Error fetching payload: %v", err)
return
}
fmt.Println(string(p))
}
default:
var outputKeys []payload.SimpleContainerImage
for _, sig := range verified {
p, err := sig.Payload()
if err != nil {
fmt.Fprintf(os.Stderr, "Error fetching payload: %v", err)
return
}
ss := payload.SimpleContainerImage{}
if err := json.Unmarshal(p, &ss); err != nil {
fmt.Println("error decoding the payload:", err.Error())
return
}
if cert, err := sig.Cert(); err == nil && cert != nil {
if ss.Optional == nil {
ss.Optional = make(map[string]interface{})
}
ss.Optional["Subject"] = certSubject(cert)
}
if bundle, err := sig.Bundle(); err == nil && bundle != nil {
if ss.Optional == nil {
ss.Optional = make(map[string]interface{})
}
ss.Optional["Bundle"] = bundle
}
outputKeys = append(outputKeys, ss)
}
b, err := json.Marshal(outputKeys)
if err != nil {
fmt.Println("error when generating the output:", err.Error())
return
}
fmt.Printf("\n%s\n", string(b))
}
}