Sub topic 5
Signing and verifying with a key pair
This chapter shows the basic Cosign workflow: create a key pair, sign an image digest, and verify the signature. Keyless signing, which the next chapters cover, builds on the same registry mechanics. Key-based signing is the simplest way to see them.
You should already be comfortable pushing images to a registry and reading a digest, as covered in the earlier chapters on digests and signature storage. You also need Cosign 2.x installed and write access to a registry. The examples use registry.example.com/team/app, which you should replace with your own repository.
What key-based signing proves
A signature made with a private key proves that someone who held that key approved a specific digest. Verification with the matching public key proves the same thing in reverse: the signature is valid for that digest and was made by the holder of the private key.
It does not prove that the image is free of vulnerabilities. It also does not prove who the key holder is, because that depends on how you distribute the public key and protect the private key. Those two responsibilities are the main operational cost of key-based signing.
Step 1: Resolve the digest
Sign a digest, not a tag. A tag such as 1.0.0 can be moved to different content later. A digest cannot.
# 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>The first command prints the digest as a JSON string, including quotes. Copy the sha256:... value without the quotes into the IMAGE variable. In a script, you can capture the value with --format '{{.Manifest.Digest}}' instead, which prints it without quotes.
If you sign with a tag, Cosign resolves it to a digest at signing time and prints a warning. The signature still binds to that digest. The risk is that your command does not make clear which digest you meant, and a later cosign verify against the tag may resolve to a different digest.
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 to the current directory.
| File | Contents | Handling |
|---|---|---|
| `cosign.key` | Private key, encrypted with your password | Keep secret. Never commit it to Git. |
| `cosign.pub` | Public key in PEM format | Share freely with anyone who verifies your images. |
The private key is stored encrypted. Cosign derives an encryption key from your password and uses it to protect the key file, so a stolen cosign.key is still protected by the password. That protection is only as strong as the password.
For automation, Cosign reads the password from the COSIGN_PASSWORD environment variable. This avoids an interactive prompt, but it moves the problem into your CI secret store. The key file and its password must then both be available to the pipeline, which is the long-lived secret problem that keyless signing and KMS keys avoid.
Step 3: Sign the image
# Sign the image; the signature is pushed to the same repository
cosign sign --key cosign.key "$IMAGE"Cosign does the following:
- It builds a small JSON payload that names the image digest. Cosign calls this the "simple signing" payload.
- It signs that payload with the private key.
- It pushes the signature to the same repository as an OCI artifact. The artifact's tag is derived from the image digest, in the form
sha256-<digest>.sig.
Cosign uses your existing Docker credentials, from ~/.docker/config.json or a credential helper, to authenticate to the registry. If docker push works for the repository, cosign sign normally works too.
You can see the stored signature with cosign tree:
cosign tree "$IMAGE"The output lists the signatures attached to the digest. The image manifest itself is not changed. Signing adds a new artifact beside it, so the image digest stays the same.
Transparency log uploads
In Cosign 2.x, signing also uploads a record of the signature to the public Rekor transparency log by default, even when you use your own key. Only the signature, the public key and the payload hash are uploaded, not your image. If your image name is sensitive, note that the log entry contains the digest. Later chapters cover Rekor in detail.
For a private environment with no access to the public log, disable the upload when signing and the check when verifying:
cosign sign --key cosign.key --tlog-upload=false "$IMAGE"
cosign verify --key cosign.pub --insecure-ignore-tlog=true "$IMAGE"Be aware of what you give up. Without a log entry, you cannot later show when the signature was created.
Step 4: Verify the image
# Verify with the public key
cosign verify --key cosign.pub "$IMAGE"On success, Cosign prints a summary of the checks it performed and then a JSON array of the verified signature payloads. The key field is the digest inside the payload:
[
{
"critical": {
"identity": {
"docker-reference": "registry.example.com/team/app"
},
"image": {
"docker-manifest-digest": "sha256:<digest>"
},
"type": "cosign container image signature"
},
"optional": null
}
]The command exits with status 0 on success and a non-zero status on failure. Scripts should rely on the exit code, not on parsing the output. A verification gate in a script can be as short as this:
set -euo pipefail
if cosign verify --key cosign.pub "$IMAGE" > /dev/null; then
echo "signature valid, safe to deploy"
else
echo "signature check failed, refusing to deploy" >&2
exit 1
fiUse the $IMAGE value that contains the digest, and deploy that same digest. If you verify one reference and deploy a different tag, the check protects nothing.
Adding annotations
You can attach key-value metadata to a signature with -a. Annotations are part of the signed payload, so they cannot be altered without invalidating the signature.
cosign sign --key cosign.key \
-a build=ci-2041 \
-a reviewed=true \
"$IMAGE"
cosign verify --key cosign.pub -a reviewed=true "$IMAGE"When you pass -a to verify, Cosign accepts only signatures that carry that annotation with that value. This lets you separate, for example, images that passed a manual review from images that did not. Do not put secrets in annotations. They are stored in the registry in plain text.
Common errors
| Message or symptom | Likely cause | Fix |
|---|---|---|
| `no signatures found` or `no matching signatures` | The image was never signed, or the signature belongs to a different digest. | Check that `$IMAGE` uses the same digest you signed. Run `cosign tree "$IMAGE"`. |
| `no matching signatures: crypto/ecdsa: verification error` (wording varies by version) | The signature exists, but it was made with a different private key. | Confirm you are using the public key that matches the signer's private key. |
| `decrypt: encrypted: decryption failed` | Wrong password for `cosign.key`. | Re-enter the password, or fix `COSIGN_PASSWORD`. |
| `UNAUTHORIZED` or `DENIED` while signing | Your credentials cannot write to the repository. | Run `docker login`, and check that the account has push permission. |
| Verification succeeds for a tag but the wrong content runs | The tag was moved after verification. | Deploy by digest, using the exact reference you verified. |
| Tlog-related failure in an offline environment | Cosign cannot reach the Rekor log. | Use the tlog flags shown earlier, and accept the trade-off. |
Limits of key-based signing
Key-based signing is simple and works offline, but it has costs you should plan for.
- Key custody. Anyone who gets the private key and password can sign images that every verifier will accept. You must store the key securely and limit who can use it.
- Distribution. Verifiers need the correct public key through a trusted channel. If an attacker can replace
cosign.pubon the verifier's side, verification no longer protects you. - Rotation and revocation. Cosign has no built-in way to revoke a leaked key. You replace the key, re-sign the images you still trust, and update every verifier. The final chapter covers this in more detail.
- Identity. A signature says "someone with this key". It does not say which person or pipeline. Keyless signing, covered next, addresses that by tying the signature to an identity.
Later chapters show how KMS-backed keys reduce the custody risk while keeping the same verification commands, with a --key value that points at a KMS URI instead of a file.
Summary of the workflow
- Resolve the image digest after you push.
- Run
cosign generate-key-paironce, and storecosign.keyand its password securely. - Sign with
cosign sign --key cosign.key "$IMAGE". - Verify with
cosign verify --key cosign.pub "$IMAGE", and deploy the same digest.
Exercise
You run cosign sign --key cosign.key registry.example.com/team/app:1.0.0. A week later someone pushes a different image to the 1.0.0 tag. A colleague runs cosign verify --key cosign.pub registry.example.com/team/app@sha256:<old-digest>. What happens, and what would happen if they verified the tag 1.0.0 instead?
Show answer
The digest-based verification succeeds. Cosign resolved the tag to the old digest at signing time and signed that digest, so a signature exists for <old-digest>, and verifying that digest finds it. Verifying the tag 1.0.0 now resolves it to the new digest, which has no signature, so Cosign reports no signatures found and exits non-zero. This is why you should sign and verify by digest. The tag only tells you what the repository currently points at.
Exercise
Your CI pipeline signs images with a key stored in the secret COSIGN_KEY, and the password is in COSIGN_PASSWORD. Name two risks of this setup and one way to reduce them.
Show answer
First, anyone who can read both secrets, such as a compromised workflow or a maintainer with secret access, can sign any image that verifiers will trust. Second, the key cannot be revoked, so rotating it means updating every verifier. To reduce the risk, keep the private key in a KMS or hardware-backed store so the key material is never exposed to the pipeline, or move to keyless signing so that no long-lived signing secret exists at all.