|
| 1 | +package signatures |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/google/go-containerregistry/pkg/name" |
| 11 | + v1 "github.com/google/go-containerregistry/pkg/v1" |
| 12 | + "github.com/sigstore/cosign/v3/pkg/cosign" |
| 13 | + "github.com/sigstore/cosign/v3/pkg/oci" |
| 14 | + ociremote "github.com/sigstore/cosign/v3/pkg/oci/remote" |
| 15 | + "github.com/stackrox/rox/generated/storage" |
| 16 | + imgUtils "github.com/stackrox/rox/pkg/images/utils" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + bundleSigArtifactTypePrefix = "application/vnd.dev.sigstore.bundle" |
| 21 | + |
| 22 | + // maxReferrerManifests limits the number of referrer signature manifests processed per image |
| 23 | + // to bound latency and prevent pathological cases from consuming the caller's timeout budget. |
| 24 | + maxReferrerManifests = 50 |
| 25 | +) |
| 26 | + |
| 27 | +// signaturePayload is the internal representation of a fetched cosign signature before |
| 28 | +// it is persisted into the CosignSignature proto. Two formats are supported: |
| 29 | +// |
| 30 | +// - SimpleSigning: the legacy cosign format. The signature, payload, certificate, and |
| 31 | +// rekor bundle are stored as individual fields in cosign.SignedPayload. |
| 32 | +// - Sigstore bundle: the OCI 1.1 bundle format. The raw bundle JSON is stored as-is |
| 33 | +// in sigstoreBundle and verified directly via sigstore-go at verification time. |
| 34 | +// |
| 35 | +// The format is determined by sigstoreBundle: non-empty means sigstore bundle, |
| 36 | +// empty means SimpleSigning. |
| 37 | +type signaturePayload struct { |
| 38 | + cosign.SignedPayload |
| 39 | + sigstoreBundle []byte |
| 40 | +} |
| 41 | + |
| 42 | +var _ oci.SignedEntity = (*tagSignedEntity)(nil) |
| 43 | + |
| 44 | +// tagSignedEntity adapts an image reference for tag-based signature discovery. |
| 45 | +// Signatures() constructs the cosign tag reference (<algo>-<hex>.sig) and fetches |
| 46 | +// the signature manifest from the registry. Digest() returns the image digest so |
| 47 | +// cosign can build the tag. Only these two methods are called by cosign.FetchSignatures. |
| 48 | +// Attestations and Attachment return safe defaults to prevent panics if cosign evolves. |
| 49 | +type tagSignedEntity struct { |
| 50 | + opts []ociremote.Option |
| 51 | + imgRef name.Reference |
| 52 | + imgSHA string |
| 53 | +} |
| 54 | + |
| 55 | +func newTagSignedEntity(img *storage.Image, imgRef name.Reference, opts ...ociremote.Option) *tagSignedEntity { |
| 56 | + return &tagSignedEntity{ |
| 57 | + opts: opts, |
| 58 | + imgRef: imgRef, |
| 59 | + imgSHA: imgUtils.GetSHA(img), |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func (s *tagSignedEntity) Digest() (v1.Hash, error) { |
| 64 | + return v1.NewHash(s.imgSHA) |
| 65 | +} |
| 66 | + |
| 67 | +func (s *tagSignedEntity) Signatures() (oci.Signatures, error) { |
| 68 | + h, err := s.Digest() |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + // Cosign ref: https://github.com/sigstore/cosign/blob/main/pkg/oci/remote/remote.go |
| 73 | + return ociremote.Signatures(s.imgRef.Context().Tag(fmt.Sprint(h.Algorithm, "-", h.Hex, ".sig")), s.opts...) |
| 74 | +} |
| 75 | + |
| 76 | +func (s *tagSignedEntity) Attestations() (oci.Signatures, error) { return nil, nil } |
| 77 | + |
| 78 | +func (s *tagSignedEntity) Attachment(_ string) (oci.File, error) { |
| 79 | + return nil, errors.New("attachments not supported on tag-based signature entity") |
| 80 | +} |
| 81 | + |
| 82 | +// fetchSignaturesByTag discovers SimpleSigning signatures via the cosign tag-based method. |
| 83 | +// Discovery: looks up the tag <algo>-<hex>.sig in the same repository as the image. |
| 84 | +// Format: always SimpleSigning (signature and payload stored in OCI layer annotations). |
| 85 | +func fetchSignaturesByTag(image *storage.Image, imgRef name.Reference, opts []ociremote.Option) ([]signaturePayload, error) { |
| 86 | + se := newTagSignedEntity(image, imgRef, opts...) |
| 87 | + payloads, err := cosign.FetchSignatures(se) |
| 88 | + if err != nil && (isMissingSignatureError(err) || isUnknownMimeTypeError(err)) { |
| 89 | + return nil, nil |
| 90 | + } |
| 91 | + wrapped := make([]signaturePayload, len(payloads)) |
| 92 | + for i, p := range payloads { |
| 93 | + wrapped[i] = signaturePayload{SignedPayload: p} |
| 94 | + } |
| 95 | + return wrapped, err |
| 96 | +} |
| 97 | + |
| 98 | +// fetchSignaturesByReferrer discovers sigstore bundle signatures via the OCI 1.1 Referrers API. |
| 99 | +// Discovery: queries the referrers index for the image digest and filters for sigstore |
| 100 | +// bundle artifact types. Only the sigstore bundle format is supported for referrer-based |
| 101 | +// discovery — cosign v3 exclusively produces bundles for this path. |
| 102 | +func fetchSignaturesByReferrer(ctx context.Context, digestRef name.Digest, repo name.Repository, |
| 103 | + opts []ociremote.Option, |
| 104 | +) ([]signaturePayload, error) { |
| 105 | + index, err := ociremote.Referrers(digestRef, "", opts...) |
| 106 | + if err != nil { |
| 107 | + if checkIfErrorContainsCode(err, http.StatusNotFound) { |
| 108 | + log.Warnf("OCI referrers API not supported for %s (404)", digestRef.String()) |
| 109 | + return nil, nil |
| 110 | + } |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + if index == nil { |
| 114 | + return nil, nil |
| 115 | + } |
| 116 | + |
| 117 | + manifests := index.Manifests |
| 118 | + if len(manifests) > maxReferrerManifests { |
| 119 | + log.Warnf("Image %s has %d referrer manifests, processing only first %d", |
| 120 | + digestRef.String(), len(manifests), maxReferrerManifests) |
| 121 | + manifests = manifests[:maxReferrerManifests] |
| 122 | + } |
| 123 | + |
| 124 | + var payloads []signaturePayload |
| 125 | + for _, desc := range manifests { |
| 126 | + if ctx.Err() != nil { |
| 127 | + break |
| 128 | + } |
| 129 | + if !strings.HasPrefix(desc.ArtifactType, bundleSigArtifactTypePrefix) { |
| 130 | + continue |
| 131 | + } |
| 132 | + p, err := fetchSigstoreBundle(repo.Digest(desc.Digest.String()), opts) |
| 133 | + if err != nil { |
| 134 | + log.Warnf("Failed to fetch sigstore bundle from referrer %s: %v", desc.Digest, err) |
| 135 | + continue |
| 136 | + } |
| 137 | + payloads = append(payloads, p...) |
| 138 | + } |
| 139 | + return payloads, nil |
| 140 | +} |
| 141 | + |
| 142 | +// fetchSigstoreBundle fetches a sigstore bundle referrer and stores the raw JSON. |
| 143 | +// Format: sigstore bundle (DSSE envelope + verification material + tlog entries in one blob). |
| 144 | +// Storage: the raw bundle JSON is stored in SigstoreBundle; no decomposition into individual |
| 145 | +// fields. Verification is deferred to verifySigstoreBundle which uses sigstore-go directly. |
| 146 | +func fetchSigstoreBundle(bundleRef name.Reference, opts []ociremote.Option) ([]signaturePayload, error) { |
| 147 | + b, err := ociremote.Bundle(bundleRef, opts...) |
| 148 | + if err != nil { |
| 149 | + return nil, err |
| 150 | + } |
| 151 | + |
| 152 | + bundleJSON, err := b.MarshalJSON() |
| 153 | + if err != nil { |
| 154 | + return nil, fmt.Errorf("marshalling sigstore bundle: %w", err) |
| 155 | + } |
| 156 | + |
| 157 | + return []signaturePayload{{ |
| 158 | + sigstoreBundle: bundleJSON, |
| 159 | + }}, nil |
| 160 | +} |
0 commit comments