TExcelTAD
Home › Software Supply Chain Security: SBOM, SLSA & Sigstore › cosign: Signing with a Key Pair

cosign: Signing with a Key Pair

cosign is Sigstore's signing tool for container images and other artifacts. It has two modes: key pair signing, where you manage a private key like any other secret, and keyless signing, where a short-lived certificate ties each signature to a CI identity (Keyless Signing). This lesson uses key pairs, which work fully offline.

About the flags in this course

The sessions run without network access, which changes two things compared to a default cosign 3 setup:

Flag Why it's here
--signing-config /opt/sigstore/offline-signing-config.json (sign, attest) cosign 3 normally downloads a signing config from Sigstore's TUF repository to learn which transparency log and timestamp authority to use. This local config lists none, so nothing is uploaded.
--insecure-ignore-tlog (verify) normally cosign also checks that the signature was recorded in the Rekor transparency log. There is no log here, so verification skips that check and prints a warning saying so.

In production you'd keep the defaults: signatures recorded in Rekor, verification requiring the log entry. Everything else in these sessions is what you'd type for real.

Generate a key pair

bash · try it yourself
export COSIGN_PASSWORD='correct horse battery staple'
cosign generate-key-pair
ls
head -1 cosign.key
head -1 cosign.pub
cosign public-key --key cosign.key | cmp - cosign.pub && echo "public key derived from the private key matches cosign.pub"
Private key written to cosign.key
Public key written to cosign.pub
cosign.key
cosign.pub
-----BEGIN ENCRYPTED SIGSTORE PRIVATE KEY-----
-----BEGIN PUBLIC KEY-----
public key derived from the private key matches cosign.pub

cosign.key is an ECDSA P-256 private key, encrypted with the password (scrypt + secretbox); cosign.pub is a plain PEM public key that you publish. In CI the password comes from a secret, and better still the key never leaves a KMS: cosign generate-key-pair --kms awskms://alias/release-signing (also gcpkms://, azurekms://, hashivault://), after which --key awskms://alias/release-signing signs without the key ever being on the runner.

Sign an image by digest

The course image provides a key pair in /opt/keys (password learner-pass, already in COSIGN_PASSWORD). Push an image, resolve its digest, sign:

bash · try it yourself
crane push /opt/samples/images/alpine-3.18.0.tar localhost:5000/shop/app:1.4.2 > /dev/null 2>&1
IMG=localhost:5000/shop/app@$(crane digest localhost:5000/shop/app:1.4.2)
cosign sign --key /opt/keys/cosign.key --signing-config /opt/sigstore/offline-signing-config.json "$IMG"
echo "exit $?"
cosign verify --key /opt/keys/cosign.pub --insecure-ignore-tlog "$IMG" > verified.json
echo "exit $?"
jq '.[0].critical' verified.json
Signing artifact...
Pushing signature to: localhost:5000/shop/app
exit 0
WARNING: Skipping tlog verification is an insecure practice that lacks transparency and auditability verification for the signature.

Verification for localhost:5000/shop/app@sha256:f04ffda0b1406b530a735ae8676c9d6fa527618e842cc66b1bc09ae4dbd44e0b --
The following checks were performed on each of these signatures:
  - The cosign claims were validated
  - Existence of the claims in the transparency log was verified offline
  - The signatures were verified against the specified public key
exit 0
{
  "identity": {
    "docker-reference": "localhost:5000/shop/app@sha256:f04ffda0b1406b530a735ae8676c9d6fa527618e842cc66b1bc09ae4dbd44e0b"
  },
  "image": {
    "docker-manifest-digest": "sha256:f04ffda0b1406b530a735ae8676c9d6fa527618e842cc66b1bc09ae4dbd44e0b"
  },
  "type": "https://sigstore.dev/cosign/sign/v1"
}

The verified payload is cosign's "simple signing" format: the signature covers a JSON document whose critical.image.docker-manifest-digest is the image digest, exactly the payload-contains-digest structure from What a Signature Proves. cosign checks both the signature and that the digest matches the image it resolved.

Note: cosign verify prints "Existence of the claims in the transparency log was verified offline" even with --insecure-ignore-tlog; that list is cosign's standard summary. The WARNING line above it is the accurate part: no log was checked.

Sign by tag? Don't

bash · try it yourself
crane push /opt/samples/images/alpine-3.18.0.tar localhost:5000/shop/app:1.4.2 > /dev/null 2>&1
cosign sign --key /opt/keys/cosign.key --signing-config /opt/sigstore/offline-signing-config.json localhost:5000/shop/app:1.4.2 2>&1 | head -6
WARNING: Image reference localhost:5000/shop/app:1.4.2 uses a tag, not a digest, to identify the image to sign.
    This can lead you to sign a different image than the intended one. Please use a
    digest (example.com/ubuntu@sha256:abc123...) rather than tag
    (example.com/ubuntu:latest) for the input to cosign. The ability to refer to
    images by tag will be removed in a future release.

cosign resolves the tag and signs the digest it found, but between your build and that resolution someone could have moved the tag, and you'd sign their image. Capture the digest from the push that you did (crane push and docker buildx build --push --metadata-file both report it) and sign that.

Annotations

-a key=value adds claims to the signed payload, such as the commit the image was built from. A verifier can then require them:

bash · try it yourself
crane push /opt/samples/images/alpine-3.18.0.tar localhost:5000/shop/app:1.4.2 > /dev/null 2>&1
IMG=localhost:5000/shop/app@$(crane digest localhost:5000/shop/app:1.4.2)
cosign sign --key /opt/keys/cosign.key --signing-config /opt/sigstore/offline-signing-config.json \
  -a git_commit=3f2a9c1 -a pipeline=release "$IMG" > /dev/null 2>&1
cosign verify --key /opt/keys/cosign.pub --insecure-ignore-tlog -a git_commit=3f2a9c1 "$IMG" 2> /dev/null | jq -c '.[0].optional'
cosign verify --key /opt/keys/cosign.pub --insecure-ignore-tlog -a git_commit=0000000 "$IMG" 2>&1 | grep '^Error'
{"git_commit":"3f2a9c1","pipeline":"release"}
Error: no matching attestations: missing or incorrect annotation

Annotations are useful but unstructured; for real build metadata use an attestation with a defined predicate such as SLSA provenance.

Verification failures

bash · try it yourself
crane push /opt/samples/images/alpine-3.18.0.tar localhost:5000/shop/app:1.4.2 > /dev/null 2>&1
IMG=localhost:5000/shop/app@$(crane digest localhost:5000/shop/app:1.4.2)
cosign sign --key /opt/keys/cosign.key --signing-config /opt/sigstore/offline-signing-config.json "$IMG" > /dev/null 2>&1
COSIGN_PASSWORD=other cosign generate-key-pair --output-key-prefix attacker > /dev/null 2>&1
echo "--- wrong key"
cosign verify --key attacker.pub --insecure-ignore-tlog "$IMG" 2>&1 | grep '^Error'
echo "--- an unsigned image"
crane push /opt/samples/images/alpine-3.18.0.tar localhost:5000/shop/other:1 > /dev/null 2>&1
cosign verify --key /opt/keys/cosign.pub --insecure-ignore-tlog localhost:5000/shop/other:1 2>&1 | grep '^Error'
--- wrong key
Error: no matching attestations: failed to verify signature: could not verify envelope: accepted signatures do not match threshold, Found: 0, Expected 1
--- an unsigned image
Error: no signatures found

Why is shop/other:1 unsigned, when it is the same alpine digest we just signed? It lives in a different repository: cosign stores signatures in the repository of the image you sign (next lesson), so shop/other has no signature for that digest. When you promote between repositories, copy the referrers along with the image (oras cp -r copies an image and the artifacts attached to it), or sign again in the target repository.

Test yourself

Why does cosign warn when you sign app:1.4.2 instead of app@sha256:…?

  1. Tags can't be signed
  2. The tag may point to a different image than the one you built by the time cosign resolves it
  3. Tags are slower
  4. The signature would expire

What does cosign verify check about the signed payload besides the signature?

  1. Only the timestamp
  2. That the docker-manifest-digest in the payload equals the digest of the image being verified
  3. The tag name
  4. Nothing

What is the advantage of --key awskms://… over a cosign.key file in CI?

  1. Faster signing
  2. The private key never leaves the KMS, so the runner can't leak it
  3. No password needed for verification
  4. It skips Rekor