| 1 | // |
| 2 | // Copyright 2021 The Sigstore Authors. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | // Package payload defines a container image |
| 17 | package payloadscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/ |
| 18 | |
| 19 | import ( |
| 20 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/" |
| 21 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 22 | |
| 23 | "github.com/google/go-containerregistry/pkg/namescip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/" |
| 24 | ) |
| 25 | |
| 26 | // CosignSignatureType is the value of `critical.type` in a SimpleContainerImage payload. |
| 27 | const CosignSignatureTypescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/CosignSignatureType. = "cosign container image signature" |
| 28 | |
| 29 | // SimpleContainerImage describes the structure of a basic container image signature payload, as defined at: |
| 30 | // https://github.com/containers/image/blob/main/docs/containers-signature.5.md#json-data-format |
| 31 | type SimpleContainerImagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/SimpleContainerImage# struct { |
| 32 | Criticalscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/SimpleContainerImage#Critical. Criticalscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Critical# `json:"critical"` // Critical data critical to correctly evaluating the validity of the signature |
| 33 | Optionalscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/SimpleContainerImage#Optional. map[string]any `json:"optional"` // Optional optional metadata about the image |
| 34 | } |
| 35 | |
| 36 | // Critical data critical to correctly evaluating the validity of a signature |
| 37 | type Criticalscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Critical# struct { |
| 38 | Identityscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Critical#Identity. Identityscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Identity# `json:"identity"` // Identity claimed identity of the image |
| 39 | Imagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Critical#Image. Imagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Image# `json:"image"` // Image identifies the container that the signature applies to |
| 40 | Typescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Critical#Type. string `json:"type"` // Type must be 'atomic container signature' |
| 41 | } |
| 42 | |
| 43 | // Identity is the claimed identity of the image |
| 44 | type Identityscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Identity# struct { |
| 45 | DockerReferencescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Identity#DockerReference. string `json:"docker-reference"` // DockerReference is a reference used to refer to or download the image |
| 46 | } |
| 47 | |
| 48 | // Image identifies the container image that the signature applies to |
| 49 | type Imagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Image# struct { |
| 50 | DockerManifestDigestscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Image#DockerManifestDigest. string `json:"docker-manifest-digest"` // DockerManifestDigest the manifest digest of the signed container image |
| 51 | } |
| 52 | |
| 53 | // Cosign describes a container image signed using Cosign |
| 54 | type Cosignscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign# struct { |
| 55 | Imagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#Image. namescip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/.Digestscip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/Digest# |
| 56 | // ClaimedIdentity is what the signer claims the image to be; usually a registry.com/…/repo:tag, but can also use a digest instead. |
| 57 | // ALMOST ALL consumers MUST verify that ClaimedIdentity in the signature is correct given how user refers to the image; |
| 58 | // e.g. if the user asks to access a signed image example.com/repo/mysql:3.14, |
| 59 | // it is ALMOST ALWAYS necessary to validate that ClaimedIdentity = example.com/repo/mysql:3.14 |
| 60 | // |
| 61 | // Considerations: |
| 62 | // - The user might refer to an image using a digest (example.com/repo/mysql@sha256:…); in that case the registry/…/repo should still match |
| 63 | // - If the image is multi-arch, ClaimedIdentity usually refers to the top-level multi-arch image index also on the per-arch images |
| 64 | // (possibly even if ClaimedIdentity contains a digest!) |
| 65 | // - Older versions of cosign generate signatures where ClaimedIdentity only contains a registry/…/repo ; signature consumers should allow users |
| 66 | // to determine whether such images should be accepted (and, long-term, the default SHOULD be to reject them) |
| 67 | ClaimedIdentityscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#ClaimedIdentity. string |
| 68 | Annotationsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#Annotations. map[string]any |
| 69 | } |
| 70 | |
| 71 | // SimpleContainerImage returns information about a container image in the github.com/containers/image/signature format |
| 72 | func (p Cosignscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#) SimpleContainerImagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#SimpleContainerImage().() SimpleContainerImagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/SimpleContainerImage# { |
| 73 | dockerReference := p.Imagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#Image..Repositoryscip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/Digest#Repository..Namescip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/Repository#Name().() |
| 74 | if p.ClaimedIdentityscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#ClaimedIdentity. != "" { |
| 75 | dockerReference = p.ClaimedIdentityscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#ClaimedIdentity. |
| 76 | } |
| 77 | return SimpleContainerImagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/SimpleContainerImage#{ |
| 78 | Criticalscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/SimpleContainerImage#Critical.: Criticalscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Critical#{ |
| 79 | Identityscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Critical#Identity.: Identityscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Identity#{ |
| 80 | DockerReferencescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Identity#DockerReference.: dockerReference, |
| 81 | }, |
| 82 | Imagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Critical#Image.: Imagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Image#{ |
| 83 | DockerManifestDigestscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Image#DockerManifestDigest.: p.Imagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#Image..DigestStrscip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/Digest#DigestStr().(), |
| 84 | }, |
| 85 | Typescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Critical#Type.: CosignSignatureTypescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/CosignSignatureType., |
| 86 | }, |
| 87 | Optionalscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/SimpleContainerImage#Optional.: p.Annotationsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#Annotations., |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // MarshalJSON marshals the container signature into a []byte of JSON data |
| 92 | func (p Cosignscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#) MarshalJSONscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#MarshalJSON().() ([]byte, error) { |
| 93 | return jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Marshal().(p.SimpleContainerImagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#SimpleContainerImage().()) |
| 94 | } |
| 95 | |
| 96 | var _ jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Marshalerscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Marshaler# = Cosignscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#{} |
| 97 | |
| 98 | // UnmarshalJSON unmarshals []byte of JSON data into a container signature object |
| 99 | func (p *Cosignscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#) UnmarshalJSONscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#UnmarshalJSON().(data []byte) error { |
| 100 | if string(data) == "null" { |
| 101 | // JSON "null" is a no-op by convention |
| 102 | return nil |
| 103 | } |
| 104 | var simple SimpleContainerImagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/SimpleContainerImage# |
| 105 | if err := jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Unmarshal().(data, &simple); err != nil { |
| 106 | return err |
| 107 | } |
| 108 | if simple.Criticalscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/SimpleContainerImage#Critical..Typescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Critical#Type. != CosignSignatureTypescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/CosignSignatureType. { |
| 109 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("Cosign signature payload was of an unknown type: %q", simple.Criticalscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/SimpleContainerImage#Critical..Typescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Critical#Type.) |
| 110 | } |
| 111 | digestStr := simple.Criticalscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/SimpleContainerImage#Critical..Identityscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Critical#Identity..DockerReferencescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Identity#DockerReference. + "@" + simple.Criticalscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/SimpleContainerImage#Critical..Imagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Critical#Image..DockerManifestDigestscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Image#DockerManifestDigest. |
| 112 | digest, err := namescip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/.NewDigestscip-go gomod github.com/google/go-containerregistry v0.21.7 `github.com/google/go-containerregistry/pkg/name`/NewDigest().(digestStr) |
| 113 | if err != nil { |
| 114 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("could not parse image digest string %q: %w", digestStr, err) |
| 115 | } |
| 116 | p.Imagescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#Image. = digest |
| 117 | p.ClaimedIdentityscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#ClaimedIdentity. = simple.Criticalscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/SimpleContainerImage#Critical..Identityscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Critical#Identity..DockerReferencescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Identity#DockerReference. |
| 118 | p.Annotationsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#Annotations. = simple.Optionalscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/SimpleContainerImage#Optional. |
| 119 | return nil |
| 120 | } |
| 121 | |
| 122 | var _ jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Unmarshalerscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Unmarshaler# = (*Cosignscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/payload`/Cosign#)(nil) |