-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
354 lines (323 loc) · 12.4 KB
/
main.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
package main
import (
"bytes"
"context"
"crypto"
"crypto/x509"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"os"
"strings"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/static"
"github.com/sigstore/cosign/v2/pkg/cosign"
"github.com/sigstore/cosign/v2/pkg/oci"
"github.com/sigstore/cosign/v2/pkg/oci/signature"
sig "github.com/sigstore/cosign/v2/pkg/signature"
rekor "github.com/sigstore/rekor/pkg/client"
"github.com/sigstore/sigstore/pkg/cryptoutils"
sigtuf "github.com/sigstore/sigstore/pkg/tuf"
)
const DEFAULT_REKOR_URL string = "https://rekor.sigstore.dev"
type Configuration struct {
ImageDigest string `json:"ImageDigest"`
RootsOfTrust []RootOfTrust `json:"RootsOfTrust"`
SignatureData SignatureData `json:"SignatureData"`
}
type RootOfTrust struct {
Name string `json:"Name"`
RootlessKeypairsOnly bool `json:"RootlessKeypairsOnly"`
RekorPublicKey string `json:"RekorPublicKey"`
RootCert string `json:"RootCert"`
SCTPublicKey string `json:"SCTPublicKey"`
Verifiers []Verifier `json:"Verifiers"`
}
func (r *RootOfTrust) IsPublic() bool {
return r.RekorPublicKey == "" && r.RootCert == "" && r.SCTPublicKey == ""
}
type Verifier struct {
Name string `json:"Name"`
Type string `json:"Type"`
KeyPairOptions VerifierKeyPairOptions `json:"KeyPairOptions"`
KeylessOptions VerifierKeylessOptions `json:"KeylessOptions"`
}
type VerifierKeyPairOptions struct {
PublicKey string `json:"PublicKey"`
}
type VerifierKeylessOptions struct {
CertIssuer string `json:"CertIssuer"`
CertSubject string `json:"CertSubject"`
}
type SignatureData struct {
Manifest string `json:"Manifest"`
Payloads map[string]string `json:"Payloads"`
}
var configFilePath = flag.String("config-file", "", "path to the config file with target image digest, root of trust, signature, and verifier data")
var proxyURL = flag.String("proxy-url", "", "")
var proxyHasCredentials = flag.Bool("proxy-has-credentials", false, "")
func getProxyDetails() (Proxy, error) {
if *proxyURL == "" {
return Proxy{}, nil
}
proxy := Proxy{URL: *proxyURL}
if *proxyHasCredentials {
stdinCredentials, err := os.ReadFile(os.Stdin.Name())
if err != nil {
return Proxy{}, fmt.Errorf("error when reading credentials, could not read stdin: %s", err.Error())
}
if len(stdinCredentials) == 0 {
return Proxy{}, fmt.Errorf("expecting credentials but received empty string")
}
separatorIndex := strings.Index(string(stdinCredentials), ":")
if separatorIndex == 0 {
return Proxy{}, fmt.Errorf("proxy credentials argument cannot start with colon, expecting USERNAME:PASSWORD")
}
splitCredentials := strings.Split(string(stdinCredentials), ":")
proxy.Username = splitCredentials[0]
proxy.Password = strings.Join(splitCredentials[1:], ":")
}
return proxy, nil
}
func main() {
flag.Parse()
config, err := loadConfiguration()
if err != nil {
log.Fatalf("ERROR: error loading config: %s", err.Error())
}
proxy, err := getProxyDetails()
if err != nil {
log.Fatalf("ERROR: error when getting proxy details: %s", err.Error())
}
imageDigestHash, err := v1.NewHash(config.ImageDigest)
if err != nil {
log.Fatalf("ERROR: error hashing image digest: %s", err.Error())
}
signatures, err := generateCosignSignatureObjects(config.SignatureData)
if err != nil {
log.Fatalf("ERROR: error generating objects for signature data: %s", err.Error())
}
allSatisfiedVerifiers := []string{}
for _, rootOfTrust := range config.RootsOfTrust {
fmt.Printf("\n>>>> checking root of trust: %s\n", rootOfTrust.Name)
satisfiedVerifiers, err := verify(imageDigestHash, rootOfTrust, signatures, proxy)
if err != nil {
// line with prefix "ERROR: " is recognized by scanner for error encounted when verifying against a verifier
fmt.Printf("ERROR: %s\n", err.Error())
} else if len(satisfiedVerifiers) > 0 {
allSatisfiedVerifiers = append(allSatisfiedVerifiers, satisfiedVerifiers...)
}
}
// line with prefix "Satisfied verifiers: " is recognized by scanner for all the satisfied verifiers separated by ", "
fmt.Printf("Satisfied verifiers: %s\n", strings.Join(allSatisfiedVerifiers, ", "))
}
func loadConfiguration() (config Configuration, err error) {
if *configFilePath == "" {
return config, errors.New("must provide --config-file flag")
}
configFile, err := os.ReadFile(*configFilePath)
if err != nil {
return config, fmt.Errorf("could not read config file: %s", err.Error())
}
err = json.Unmarshal(configFile, &config)
if err != nil {
return config, fmt.Errorf("could not unmarshal config file: %s", err.Error())
}
return config, nil
}
func generateCosignSignatureObjects(sigData SignatureData) ([]oci.Signature, error) {
parsedManifest, err := v1.ParseManifest(bytes.NewReader([]byte(sigData.Manifest)))
if err != nil {
return nil, fmt.Errorf("could not parse manifest from signatures data: %s", err.Error())
}
signatures := []oci.Signature{}
for _, manifestLayer := range parsedManifest.Layers {
layerDigest := manifestLayer.Digest.String()
payloadLayer := static.NewLayer([]byte(sigData.Payloads[layerDigest]), parsedManifest.MediaType)
signatures = append(signatures, signature.New(payloadLayer, manifestLayer))
}
return signatures, nil
}
func printWarningLine(message string) {
fmt.Printf("\033[33m%s\033[0m\n", message)
}
func verify(imgDigest v1.Hash, rootOfTrust RootOfTrust, sigs []oci.Signature, proxy Proxy) (satisfiedVerifiers []string, err error) {
ctx := context.Background()
cosignOptions := cosign.CheckOpts{ClaimVerifier: cosign.SimpleClaimVerifier}
err = setRootOfTrustCosignOptions(&cosignOptions, rootOfTrust, proxy, ctx)
if err != nil {
return satisfiedVerifiers, fmt.Errorf("could not set root of trust %s cosign check options: %s", rootOfTrust.Name, err.Error())
}
for _, verifier := range rootOfTrust.Verifiers {
cosignOptions.SigVerifier = nil
cosignOptions.Identities = nil
fmt.Printf(">> checking verifier %s\n", verifier.Name)
err = setVerifierCosignOptions(&cosignOptions, verifier, rootOfTrust, ctx)
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
fmt.Println("could not create valid cosign options for verifier, skipping verifier")
continue
}
for i, signature := range sigs {
bundle, err := signature.Bundle()
if err != nil {
fmt.Printf("error when retrieving bundle for signature, skipping signature: %s\n", err.Error())
continue
}
if bundle == nil {
printWarningLine("no bundle found, any tlog verification must happen through network")
} else {
fmt.Printf("signature bundle: %s\n", bundle.Payload.LogID)
}
fmt.Printf("verifying signature %d\n", i)
_, err = cosign.VerifyImageSignature(ctx, signature, imgDigest, &cosignOptions)
if err != nil {
// the image is not signed by this verifier
fmt.Printf("signature not verified: %s\n", err.Error())
} else {
fmt.Printf("signature %d satisfies verifier %s\n", i, verifier.Name)
satisfiedVerifiers = append(satisfiedVerifiers, fmt.Sprintf("%s/%s", rootOfTrust.Name, verifier.Name))
break
}
}
}
return satisfiedVerifiers, nil
}
func setRootOfTrustCosignOptions(cosignOptions *cosign.CheckOpts, rootOfTrust RootOfTrust, proxy Proxy, ctx context.Context) (err error) {
if rootOfTrust.RootlessKeypairsOnly {
return nil
}
// rekor public keys
rekorKeyCollection := cosign.NewTrustedTransparencyLogPubKeys()
if rootOfTrust.IsPublic() {
rekorKeyTargets, err := GetSigstorePublicTufTargets(sigtuf.Rekor, proxy)
if err != nil {
return fmt.Errorf("could not retrieve rekor tuf targets: %s", err.Error())
}
for _, rekorKeyTarget := range rekorKeyTargets {
if err := rekorKeyCollection.AddTransparencyLogPubKey(rekorKeyTarget.Target, rekorKeyTarget.Status); err != nil {
return fmt.Errorf("could not add public root of trust rekor public key to collection: %w", err)
}
}
} else if rootOfTrust.RekorPublicKey != "" {
if err := rekorKeyCollection.AddTransparencyLogPubKey([]byte(rootOfTrust.RekorPublicKey), sigtuf.Active); err != nil {
return fmt.Errorf("could not add custom root of trust rekor public key to collection: %w", err)
}
}
cosignOptions.RekorPubKeys = &rekorKeyCollection
// root & intermediate certificates
selfSigned := func(cert *x509.Certificate) bool {
return bytes.Equal(cert.RawSubject, cert.RawIssuer)
}
if rootOfTrust.RootCert != "" {
rootPool := x509.NewCertPool()
var intermediatePool *x509.CertPool // should be nil if no intermediate certs are found
certs, err := cryptoutils.UnmarshalCertificatesFromPEM([]byte(rootOfTrust.RootCert))
if err != nil {
return fmt.Errorf("error unmarshalling provided root certificate(s): %w", err)
}
for _, cert := range certs {
if selfSigned(cert) {
rootPool.AddCert(cert)
} else {
if intermediatePool == nil {
intermediatePool = x509.NewCertPool()
}
intermediatePool.AddCert(cert)
}
}
cosignOptions.RootCerts = rootPool
cosignOptions.IntermediateCerts = intermediatePool
} else if rootOfTrust.IsPublic() {
targetCertificates, err := GetSigstorePublicTufTargets(sigtuf.Fulcio, proxy)
// certificates, err := GetPublicRootOfTrustFulcioCertificates(proxy)
if err != nil {
return fmt.Errorf("could not retrieve public root of trust fulcio certificates: %s", err.Error())
}
rootPool := x509.NewCertPool()
var intermediatePool *x509.CertPool // should be nil if no intermediate certs are found
for _, targetCertificate := range targetCertificates {
certs, err := cryptoutils.UnmarshalCertificatesFromPEM(targetCertificate.Target)
if err != nil {
continue
}
for _, cert := range certs {
if selfSigned(cert) {
rootPool.AddCert(cert)
} else {
if intermediatePool == nil {
intermediatePool = x509.NewCertPool()
}
intermediatePool.AddCert(cert)
}
}
}
cosignOptions.RootCerts = rootPool
cosignOptions.IntermediateCerts = intermediatePool
}
// sct public keys
sctKeyCollection := cosign.NewTrustedTransparencyLogPubKeys()
if rootOfTrust.IsPublic() {
sctKeyTargets, err := GetSigstorePublicTufTargets(sigtuf.CTFE, proxy)
if err != nil {
return fmt.Errorf("could not retrieve ctfe tuf targets: %s", err.Error())
}
for _, sctKeyTarget := range sctKeyTargets {
if err := sctKeyCollection.AddTransparencyLogPubKey(sctKeyTarget.Target, sctKeyTarget.Status); err != nil {
return fmt.Errorf("could not add public root of trust sct public key to collection: %w", err)
}
}
} else if rootOfTrust.SCTPublicKey != "" {
if err := sctKeyCollection.AddTransparencyLogPubKey([]byte(rootOfTrust.SCTPublicKey), sigtuf.Active); err != nil {
return fmt.Errorf("could not add custom root of trust sct public key to collection: %w", err)
}
}
cosignOptions.CTLogPubKeys = &sctKeyCollection
return nil
}
func setVerifierCosignOptions(cosignOptions *cosign.CheckOpts, verifier Verifier, rootOfTrust RootOfTrust, ctx context.Context) (err error) {
switch verifier.Type {
case "keypair":
cosignOptions.SigVerifier, err = sig.LoadPublicKeyRaw([]byte(verifier.KeyPairOptions.PublicKey), crypto.SHA256)
if err != nil {
return fmt.Errorf("could not load PEM encoded public key of verifier %s under %s: %s", verifier.Name, rootOfTrust.Name, err.Error())
}
case "keyless":
if rootOfTrust.RootlessKeypairsOnly {
return fmt.Errorf("cannot use keyless verifier for root of trust with field RootlessKeypairsOnly set to true")
}
if rootOfTrust.RootCert == "" && !rootOfTrust.IsPublic() {
return fmt.Errorf("cannot use keyless verifier %s with private root of trust without root cert", verifier.Name)
}
cosignOptions.Identities = []cosign.Identity{
{
Issuer: verifier.KeylessOptions.CertIssuer,
Subject: verifier.KeylessOptions.CertSubject,
},
}
default:
// verifier.Type must be "keypair" or "keyless"
return fmt.Errorf("invalid verification type in config file: %s", verifier.Type)
}
if !rootOfTrust.IsPublic() {
if rootOfTrust.RekorPublicKey == "" {
cosignOptions.IgnoreTlog = true
}
if rootOfTrust.SCTPublicKey == "" {
cosignOptions.IgnoreSCT = true
}
} else {
rekorClient, err := rekor.GetRekorClient(DEFAULT_REKOR_URL)
if err != nil {
return fmt.Errorf("could not get rekor client for online tlog validation: %s", err.Error())
}
cosignOptions.RekorClient = rekorClient
}
if rootOfTrust.RootlessKeypairsOnly {
cosignOptions.IgnoreSCT = true
cosignOptions.IgnoreTlog = true
}
return nil
}