File: policy_eval_sigstore.go

package info (click to toggle)
golang-github-containers-image 5.36.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,152 kB
  • sloc: sh: 267; makefile: 100
file content (435 lines) | stat: -rw-r--r-- 16,803 bytes parent folder | download | duplicates (2)
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Policy evaluation for prSigstoreSigned.

package signature

import (
	"context"
	"crypto"
	"crypto/ecdsa"
	"crypto/x509"
	"errors"
	"fmt"
	"os"
	"strings"

	"github.com/containers/image/v5/internal/multierr"
	"github.com/containers/image/v5/internal/private"
	"github.com/containers/image/v5/internal/signature"
	"github.com/containers/image/v5/manifest"
	"github.com/containers/image/v5/signature/internal"
	digest "github.com/opencontainers/go-digest"
	"github.com/sigstore/sigstore/pkg/cryptoutils"
)

// configBytesSources contains configuration fields which may result in one or more []byte values
type configBytesSources struct {
	inconsistencyErrorMessage string   // Error to return if more than one source is set
	path                      string   // …Path: a path to a file containing the data, or ""
	paths                     []string // …Paths: paths to files containing the data, or nil
	data                      []byte   // …Data: a single instance ofhe raw data, or nil
	datas                     [][]byte // …Datas: the raw data, or nil // codespell:ignore datas
}

// loadBytesFromConfigSources ensures at most one of the sources in src is set,
// and returns the referenced data, or nil if neither is set.
func loadBytesFromConfigSources(src configBytesSources) ([][]byte, error) {
	sources := 0
	var data [][]byte // = nil
	if src.path != "" {
		sources++
		d, err := os.ReadFile(src.path)
		if err != nil {
			return nil, err
		}
		data = [][]byte{d}
	}
	if src.paths != nil {
		sources++
		data = [][]byte{}
		for _, path := range src.paths {
			d, err := os.ReadFile(path)
			if err != nil {
				return nil, err
			}
			data = append(data, d)
		}
	}
	if src.data != nil {
		sources++
		data = [][]byte{src.data}
	}
	if src.datas != nil { // codespell:ignore datas
		sources++
		data = src.datas // codespell:ignore datas
	}
	if sources > 1 {
		return nil, errors.New(src.inconsistencyErrorMessage)
	}
	return data, nil
}

// prepareTrustRoot creates a fulcioTrustRoot from the input data.
// (This also prevents external implementations of this interface, ensuring that prSigstoreSignedFulcio is the only one.)
func (f *prSigstoreSignedFulcio) prepareTrustRoot() (*fulcioTrustRoot, error) {
	caCertPEMs, err := loadBytesFromConfigSources(configBytesSources{
		inconsistencyErrorMessage: `Internal inconsistency: both "caPath" and "caData" specified`,
		path:                      f.CAPath,
		data:                      f.CAData,
	})
	if err != nil {
		return nil, err
	}
	if len(caCertPEMs) != 1 {
		return nil, errors.New(`Internal inconsistency: Fulcio specified with not exactly one of "caPath" nor "caData"`)
	}
	certs := x509.NewCertPool()
	if ok := certs.AppendCertsFromPEM(caCertPEMs[0]); !ok {
		return nil, errors.New("error loading Fulcio CA certificates")
	}
	fulcio := fulcioTrustRoot{
		caCertificates: certs,
		oidcIssuer:     f.OIDCIssuer,
		subjectEmail:   f.SubjectEmail,
	}
	if err := fulcio.validate(); err != nil {
		return nil, err
	}
	return &fulcio, nil
}

// prepareTrustRoot creates a pkiTrustRoot from the input data.
// (This also prevents external implementations of this interface, ensuring that prSigstoreSignedPKI is the only one.)
func (p *prSigstoreSignedPKI) prepareTrustRoot() (*pkiTrustRoot, error) {
	caRootsCertPEMs, err := loadBytesFromConfigSources(configBytesSources{
		inconsistencyErrorMessage: `Internal inconsistency: both "caRootsPath" and "caRootsData" specified`,
		path:                      p.CARootsPath,
		data:                      p.CARootsData,
	})
	if err != nil {
		return nil, err
	}
	if len(caRootsCertPEMs) != 1 {
		return nil, errors.New(`Internal inconsistency: PKI specified with not exactly one of "caRootsPath" nor "caRootsData"`)
	}
	rootsCerts := x509.NewCertPool()
	if ok := rootsCerts.AppendCertsFromPEM(caRootsCertPEMs[0]); !ok {
		return nil, errors.New("error loading PKI CA Roots certificates")
	}
	pki := pkiTrustRoot{
		caRootsCertificates: rootsCerts,
		subjectEmail:        p.SubjectEmail,
		subjectHostname:     p.SubjectHostname,
	}
	caIntermediateCertPEMs, err := loadBytesFromConfigSources(configBytesSources{
		inconsistencyErrorMessage: `Internal inconsistency: both "caIntermediatesPath" and "caIntermediatesData" specified`,
		path:                      p.CAIntermediatesPath,
		data:                      p.CAIntermediatesData,
	})
	if err != nil {
		return nil, err
	}
	if caIntermediateCertPEMs != nil {
		if len(caIntermediateCertPEMs) != 1 {
			return nil, errors.New(`Internal inconsistency: PKI specified with invalid value from "caIntermediatesPath" or "caIntermediatesData"`)
		}
		intermediatePool := x509.NewCertPool()
		trustedIntermediates, err := cryptoutils.UnmarshalCertificatesFromPEM(caIntermediateCertPEMs[0])
		if err != nil {
			return nil, internal.NewInvalidSignatureError(fmt.Sprintf("loading trusted intermediate certificates: %v", err))
		}
		for _, trustedIntermediateCert := range trustedIntermediates {
			intermediatePool.AddCert(trustedIntermediateCert)
		}
		pki.caIntermediateCertificates = intermediatePool
	}

	if err := pki.validate(); err != nil {
		return nil, err
	}
	return &pki, nil
}

// sigstoreSignedTrustRoot contains an already parsed version of the prSigstoreSigned policy
type sigstoreSignedTrustRoot struct {
	publicKeys      []crypto.PublicKey
	fulcio          *fulcioTrustRoot
	rekorPublicKeys []*ecdsa.PublicKey
	pki             *pkiTrustRoot
}

func (pr *prSigstoreSigned) prepareTrustRoot() (*sigstoreSignedTrustRoot, error) {
	res := sigstoreSignedTrustRoot{}

	publicKeyPEMs, err := loadBytesFromConfigSources(configBytesSources{
		inconsistencyErrorMessage: `Internal inconsistency: more than one of "keyPath", "keyPaths", "keyData", "keyDatas" specified`,
		path:                      pr.KeyPath,
		paths:                     pr.KeyPaths,
		data:                      pr.KeyData,
		datas:                     pr.KeyDatas, // codespell:ignore datas
	})
	if err != nil {
		return nil, err
	}
	if publicKeyPEMs != nil {
		for index, keyData := range publicKeyPEMs {
			pk, err := cryptoutils.UnmarshalPEMToPublicKey(keyData)
			if err != nil {
				return nil, fmt.Errorf("parsing public key %d: %w", index+1, err)
			}
			res.publicKeys = append(res.publicKeys, pk)
		}
		if len(res.publicKeys) == 0 {
			return nil, errors.New(`Internal inconsistency: "keyPath", "keyPaths", "keyData" and "keyDatas" produced no public keys`)
		}
	}

	if pr.Fulcio != nil {
		f, err := pr.Fulcio.prepareTrustRoot()
		if err != nil {
			return nil, err
		}
		res.fulcio = f
	}

	rekorPublicKeyPEMs, err := loadBytesFromConfigSources(configBytesSources{
		inconsistencyErrorMessage: `Internal inconsistency: both "rekorPublicKeyPath" and "rekorPublicKeyData" specified`,
		path:                      pr.RekorPublicKeyPath,
		paths:                     pr.RekorPublicKeyPaths,
		data:                      pr.RekorPublicKeyData,
		datas:                     pr.RekorPublicKeyDatas, // codespell:ignore datas
	})
	if err != nil {
		return nil, err
	}
	if rekorPublicKeyPEMs != nil {
		for index, pem := range rekorPublicKeyPEMs {
			pk, err := cryptoutils.UnmarshalPEMToPublicKey(pem)
			if err != nil {
				return nil, fmt.Errorf("parsing Rekor public key %d: %w", index+1, err)
			}
			pkECDSA, ok := pk.(*ecdsa.PublicKey)
			if !ok {
				return nil, fmt.Errorf("Rekor public key %d is not using ECDSA", index+1)

			}
			res.rekorPublicKeys = append(res.rekorPublicKeys, pkECDSA)
		}
		if len(res.rekorPublicKeys) == 0 {
			return nil, errors.New(`Internal inconsistency: "rekorPublicKeyPath", "rekorPublicKeyPaths", "rekorPublicKeyData" and "rekorPublicKeyDatas" produced no public keys`)
		}
	}

	if pr.PKI != nil {
		p, err := pr.PKI.prepareTrustRoot()
		if err != nil {
			return nil, err
		}
		res.pki = p
	}

	return &res, nil
}

func (pr *prSigstoreSigned) isSignatureAuthorAccepted(ctx context.Context, image private.UnparsedImage, sig []byte) (signatureAcceptanceResult, *Signature, error) {
	// We don’t know of a single user of this API, and we might return unexpected values in Signature.
	// For now, just punt.
	return sarRejected, nil, errors.New("isSignatureAuthorAccepted is not implemented for sigstore")
}

func (pr *prSigstoreSigned) isSignatureAccepted(ctx context.Context, image private.UnparsedImage, sig signature.Sigstore) (signatureAcceptanceResult, error) {
	// FIXME: move this to per-context initialization
	trustRoot, err := pr.prepareTrustRoot()
	if err != nil {
		return sarRejected, err
	}

	untrustedAnnotations := sig.UntrustedAnnotations()
	untrustedBase64Signature, ok := untrustedAnnotations[signature.SigstoreSignatureAnnotationKey]
	if !ok {
		return sarRejected, fmt.Errorf("missing %s annotation", signature.SigstoreSignatureAnnotationKey)
	}
	untrustedPayload := sig.UntrustedPayload()

	keySources := 0
	if trustRoot.publicKeys != nil {
		keySources++
	}
	if trustRoot.fulcio != nil {
		keySources++
	}
	if trustRoot.pki != nil {
		keySources++
	}

	var publicKeys []crypto.PublicKey
	switch {
	case keySources > 1: // newPRSigstoreSigned rejects more than one key sources.
		return sarRejected, errors.New("Internal inconsistency: More than one of public key, Fulcio, or PKI specified")
	case keySources == 0: // newPRSigstoreSigned rejects empty key sources.
		return sarRejected, errors.New("Internal inconsistency: A public key, Fulcio, or PKI must be specified.")
	case trustRoot.publicKeys != nil:
		if trustRoot.rekorPublicKeys != nil {
			untrustedSET, ok := untrustedAnnotations[signature.SigstoreSETAnnotationKey]
			if !ok { // For user convenience; passing an empty []byte to VerifyRekorSet should work.
				return sarRejected, fmt.Errorf("missing %s annotation", signature.SigstoreSETAnnotationKey)
			}

			var rekorFailures []string
			for _, candidatePublicKey := range trustRoot.publicKeys {
				// We could use publicKeyPEM directly, but let’s re-marshal to avoid inconsistencies.
				// FIXME: We could just generate DER instead of the full PEM text
				recreatedPublicKeyPEM, err := cryptoutils.MarshalPublicKeyToPEM(candidatePublicKey)
				if err != nil {
					// Coverage: The key was loaded from a PEM format, so it’s unclear how this could fail.
					// (PEM is not essential, MarshalPublicKeyToPEM can only fail if marshaling to ASN1.DER fails.)
					return sarRejected, fmt.Errorf("re-marshaling public key to PEM: %w", err)
				}
				// We don’t care about the Rekor timestamp, just about log presence.
				_, err = internal.VerifyRekorSET(trustRoot.rekorPublicKeys, []byte(untrustedSET), recreatedPublicKeyPEM, untrustedBase64Signature, untrustedPayload)
				if err == nil {
					publicKeys = append(publicKeys, candidatePublicKey)
					break // The SET can only accept one public key entry, so if we found one, the rest either doesn’t match or is a duplicate
				}
				rekorFailures = append(rekorFailures, err.Error())
			}
			if len(publicKeys) == 0 {
				if len(rekorFailures) == 0 {
					// Coverage: We have ensured that len(trustRoot.publicKeys) != 0, when nothing succeeds, there must be at least one failure.
					return sarRejected, errors.New(`Internal inconsistency: Rekor SET did not match any key but we have no failures.`)
				}
				return sarRejected, internal.NewInvalidSignatureError(fmt.Sprintf("No public key verified against the RekorSET: %s", strings.Join(rekorFailures, ", ")))
			}
		} else {
			publicKeys = trustRoot.publicKeys
		}

	case trustRoot.fulcio != nil:
		if trustRoot.rekorPublicKeys == nil { // newPRSigstoreSigned rejects such combinations.
			return sarRejected, errors.New("Internal inconsistency: Fulcio CA specified without a Rekor public key")
		}
		untrustedSET, ok := untrustedAnnotations[signature.SigstoreSETAnnotationKey]
		if !ok { // For user convenience; passing an empty []byte to VerifyRekorSet should correctly reject it anyway.
			return sarRejected, fmt.Errorf("missing %s annotation", signature.SigstoreSETAnnotationKey)
		}
		untrustedCert, ok := untrustedAnnotations[signature.SigstoreCertificateAnnotationKey]
		if !ok { // For user convenience; passing an empty []byte to VerifyRekorSet should correctly reject it anyway.
			return sarRejected, fmt.Errorf("missing %s annotation", signature.SigstoreCertificateAnnotationKey)
		}
		var untrustedIntermediateChainBytes []byte
		if untrustedIntermediateChain, ok := untrustedAnnotations[signature.SigstoreIntermediateCertificateChainAnnotationKey]; ok {
			untrustedIntermediateChainBytes = []byte(untrustedIntermediateChain)
		}
		pk, err := verifyRekorFulcio(trustRoot.rekorPublicKeys, trustRoot.fulcio,
			[]byte(untrustedSET), []byte(untrustedCert), untrustedIntermediateChainBytes, untrustedBase64Signature, untrustedPayload)
		if err != nil {
			return sarRejected, err
		}
		publicKeys = []crypto.PublicKey{pk}

	case trustRoot.pki != nil:
		if trustRoot.rekorPublicKeys != nil { // newPRSigstoreSigned rejects such combinations.
			return sarRejected, errors.New("Internal inconsistency: PKI specified with a Rekor public key")
		}
		untrustedCert, ok := untrustedAnnotations[signature.SigstoreCertificateAnnotationKey]
		if !ok {
			return sarRejected, fmt.Errorf("missing %s annotation", signature.SigstoreCertificateAnnotationKey)
		}
		var untrustedIntermediateChainBytes []byte
		if untrustedIntermediateChain, ok := untrustedAnnotations[signature.SigstoreIntermediateCertificateChainAnnotationKey]; ok {
			untrustedIntermediateChainBytes = []byte(untrustedIntermediateChain)
		}
		pk, err := verifyPKI(trustRoot.pki, []byte(untrustedCert), untrustedIntermediateChainBytes)
		if err != nil {
			return sarRejected, err
		}
		publicKeys = []crypto.PublicKey{pk}
	}

	if len(publicKeys) == 0 {
		// Coverage: This should never happen, we ensured that trustRoot.publicKeys is non-empty if set,
		// and we have already excluded the possibility in the switch above.
		return sarRejected, fmt.Errorf("Internal inconsistency: publicKey not set before verifying sigstore payload")
	}
	signature, err := internal.VerifySigstorePayload(publicKeys, untrustedPayload, untrustedBase64Signature, internal.SigstorePayloadAcceptanceRules{
		ValidateSignedDockerReference: func(ref string) error {
			if !pr.SignedIdentity.matchesDockerReference(image, ref) {
				return PolicyRequirementError(fmt.Sprintf("Signature for identity %q is not accepted", ref))
			}
			return nil
		},
		ValidateSignedDockerManifestDigest: func(digest digest.Digest) error {
			m, _, err := image.Manifest(ctx)
			if err != nil {
				return err
			}
			digestMatches, err := manifest.MatchesDigest(m, digest)
			if err != nil {
				return err
			}
			if !digestMatches {
				return PolicyRequirementError(fmt.Sprintf("Signature for digest %s does not match", digest))
			}
			return nil
		},
	})
	if err != nil {
		return sarRejected, err
	}
	if signature == nil { // A paranoid sanity check that VerifySigstorePayload has returned consistent values
		return sarRejected, errors.New("internal error: VerifySigstorePayload succeeded but returned no data") // Coverage: This should never happen.
	}

	return sarAccepted, nil
}

func (pr *prSigstoreSigned) isRunningImageAllowed(ctx context.Context, image private.UnparsedImage) (bool, error) {
	sigs, err := image.UntrustedSignatures(ctx)
	if err != nil {
		return false, err
	}
	var rejections []error
	foundNonSigstoreSignatures := 0
	foundSigstoreNonAttachments := 0
	for _, s := range sigs {
		sigstoreSig, ok := s.(signature.Sigstore)
		if !ok {
			foundNonSigstoreSignatures++
			continue
		}
		if sigstoreSig.UntrustedMIMEType() != signature.SigstoreSignatureMIMEType {
			foundSigstoreNonAttachments++
			continue
		}

		var reason error
		switch res, err := pr.isSignatureAccepted(ctx, image, sigstoreSig); res {
		case sarAccepted:
			// One accepted signature is enough.
			return true, nil
		case sarRejected:
			reason = err
		case sarUnknown:
			// Huh?! This should not happen at all; treat it as any other invalid value.
			fallthrough
		default:
			reason = fmt.Errorf(`Internal error: Unexpected signature verification result %q`, string(res))
		}
		rejections = append(rejections, reason)
	}
	var summary error
	switch len(rejections) {
	case 0:
		if foundNonSigstoreSignatures == 0 && foundSigstoreNonAttachments == 0 {
			// A nice message for the most common case.
			summary = PolicyRequirementError("A signature was required, but no signature exists")
		} else {
			summary = PolicyRequirementError(fmt.Sprintf("A signature was required, but no signature exists (%d non-sigstore signatures, %d sigstore non-signature attachments)",
				foundNonSigstoreSignatures, foundSigstoreNonAttachments))
		}
	case 1:
		summary = rejections[0]
	default:
		summary = PolicyRequirementError(multierr.Format("None of the signatures were accepted, reasons: ", "; ", "", rejections).Error())
	}
	return false, summary
}