Sub topic 5
Signing and verifying with a key pair
This chapter assumes you can build, tag and push an image to a registry, that you know what a public/private key pair is, and that you have Cosign installed and are logged in to your registry. Key-based signing is the simplest Cosign workflow. It is also the model that keyless signing builds on later.
The idea
You keep a private key and publish a public key. The private key signs an image digest. Anyone with the public key can check that the signature is valid and that the digest has not changed. Cosign stores the signature in the same registry as the image, so no extra infrastructure is needed.
Step 1: Resolve the digest
A tag such as 1.0.0 can be moved to different content, so sign the digest instead. Resolve it after pushing:
# Resolve the digest of the pushed image
docker buildx imagetools inspect registry.example.com/team/app:1.0.0 --format '{{json .Manifest.Digest}}'
# Always sign and verify by digest, not by tag
IMAGE=registry.example.com/team/app@sha256:<digest>Replace <digest> with the value printed by the first command.
Step 2: Generate a key pair
# Create cosign.key (encrypted private key) and cosign.pub
cosign generate-key-pairCosign asks for a password and writes two files. cosign.key is the private key, encrypted with that password. cosign.pub is the public key, which you can share freely.
Step 3: Sign and verify
# Sign the image; the signature is pushed to the same repository
cosign sign --key cosign.key "$IMAGE"
# Verify with the public key
cosign verify --key cosign.pub "$IMAGE"cosign sign prompts for the key password. In automation, supply it through the COSIGN_PASSWORD environment variable, taken from your CI secret store.
On success, cosign verify prints the checks it performed and the signature payload as JSON, and exits with status 0. On failure it exits non-zero, so you can use it directly as a gate in a script.
What the signature covers
The signed payload is a small JSON document. It contains the image digest and the repository name. Cosign signs that document, not the image layers. Because the digest is a hash of the manifest, a matching digest means the manifest and all the layers it references are unchanged.
You can add your own metadata with annotations:
cosign sign --key cosign.key -a build=ci-4821 -a team=payments "$IMAGE"Annotations are part of the signed payload. cosign verify prints them, and you can require them with -a build=ci-4821.
Common errors
| Symptom | Likely cause |
|---|---|
| `no signatures found` | The image was signed under a different digest, or in a different repository. |
| `invalid signature when validating ASN.1 encoded signature` | The public key does not match the private key that signed. |
| Verification passes for a tag but not after a re-push | The tag now points to a new digest with no signature. |
| `getting private key ... decrypt` fails | Wrong password in the prompt or in `COSIGN_PASSWORD`. |
Limits of this approach
- You must distribute
cosign.pubto every verifier and trust that the copy is authentic. - Rotating a key means re-signing images and updating every verifier.
- Any CI job that needs to sign needs access to the private key.
These costs are the reason keyless signing exists.
Exercise
You sign registry.example.com/team/app:1.0.0 by tag. A week later someone pushes a different image to the same tag. Why is the setup unsafe, and what should you have done?
Show answer
Cosign resolves the tag to a digest when it signs, so the signature belongs to the old digest. But humans and scripts who verify or deploy by tag cannot tell which content they are getting, and they may be checking a different digest than the one you signed. You should resolve the digest after pushing and run both cosign sign and cosign verify against registry.example.com/team/app@sha256:<digest>, so the signature is bound to specific content.
Exercise
Which file do you give to consumers who need to verify your images, and which command would you run in a script to fail the build if verification does not pass?
Show answer
Give them cosign.pub, the public key. They run cosign verify --key cosign.pub "$IMAGE". It exits non-zero when verification fails, so the script stops.