| 1 | package cosign |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | |
| 7 | "github.com/opencontainers/go-digest" |
| 8 | "github.com/sassoftware/relic/v7/config" |
| 9 | "github.com/sassoftware/relic/v7/signers" |
| 10 | ) |
| 11 | |
| 12 | const ( |
| 13 | signatureType = "cosign container image signature" |
| 14 | cosignPayloadMediaType = "application/vnd.dev.cosign.simplesigning.v1+json" |
| 15 | cosignArtifactType = "application/vnd.dev.cosign.artifact.sig.v1+json" |
| 16 | |
| 17 | // annotations used to encode the signature |
| 18 | signatureAnnotationKey = "dev.cosignproject.cosign/signature" |
| 19 | certificateAnnotationKey = "dev.sigstore.cosign/certificate" |
| 20 | chainAnnotationKey = "dev.sigstore.cosign/chain" |
| 21 | rfc3161TimestampAnnotationKey = "dev.sigstore.cosign/rfc3161timestamp" |
| 22 | ) |
| 23 | |
| 24 | func newPayload(manifestDigest digest.Digest, opts signers.SignOpts) ([]byte, error) { |
| 25 | payload := simpleContainerImage{ |
| 26 | Critical: critical{ |
| 27 | Image: image{DockerManifestDigest: manifestDigest}, |
| 28 | Type: signatureType, |
| 29 | }, |
| 30 | } |
| 31 | // set optionals |
| 32 | if optional := opts.Flags.GetString("optional"); optional != "" { |
| 33 | if err := json.Unmarshal([]byte(optional), &payload.Optional); err != nil { |
| 34 | return nil, fmt.Errorf("invalid annotations: %w", err) |
| 35 | } |
| 36 | } |
| 37 | if payload.Optional == nil { |
| 38 | payload.Optional = make(map[string]any) |
| 39 | } |
| 40 | payload.Optional["creator"] = config.UserAgent |
| 41 | // record image digest in audit log |
| 42 | opts.Audit.Attributes["cosign.manifest-digest"] = manifestDigest.String() |
| 43 | return json.Marshal(payload) |
| 44 | } |
| 45 | |
| 46 | // SimpleContainerImage describes the structure of a basic container image signature payload, as defined at: |
| 47 | // https://github.com/containers/image/blob/master/docs/containers-signature.5.md#json-data-format |
| 48 | type simpleContainerImage struct { |
| 49 | Critical critical `json:"critical"` // Critical data critical to correctly evaluating the validity of the signature |
| 50 | Optional map[string]any `json:"optional"` // Optional optional metadata about the image |
| 51 | } |
| 52 | |
| 53 | // Critical data critical to correctly evaluating the validity of a signature |
| 54 | type critical struct { |
| 55 | Image image `json:"image"` // Image identifies the container that the signature applies to |
| 56 | Type string `json:"type"` // Type must be 'atomic container signature' |
| 57 | } |
| 58 | |
| 59 | // Image identifies the container image that the signature applies to |
| 60 | type image struct { |
| 61 | DockerManifestDigest digest.Digest `json:"docker-manifest-digest"` // DockerManifestDigest the manifest digest of the signed container image |
| 62 | } |