@@ -161,19 +161,120 @@ The upload is skipped by using the `--upload=false` flag (default true). To capt
161161$ cosign sign --key key.pem --upload=false --output-signature demo.sig --output-certificate demo.crt user/demo
162162```
163163
164- ## Generate the signature payload (to sign with another tool)
164+ ## Generate the Signature Payload with Cosign (to sign with another tool)
165165
166- The json payload is printed to stdout:
166+ You can also use other tools for signing - not just ` cosign ` . This section will provide examples of how to sign with tools other than ` cosign ` .
167+
168+ ### GCP KMS with ` gcloud `
169+
170+ To sign with ` gcloud kms ` , first use ` cosign generate ` to generate the payload and dump it into a JSON file:
171+
172+ ``` shell
173+ $ cosign generate us-central1-docker.pkg.dev/user/test/taskrun > payload.json
174+ ```
175+
176+ Sign the payload with ` gcloud kms ` :
177+
178+ ``` shell
179+ $ gcloud kms asymmetric-sign \
180+ --digest-algorithm=sha256 \
181+ --input-file=payload.json \
182+ --signature-file=gcpkms.sig \
183+ --key=foo \
184+ --keyring=foo \
185+ --version=1 \
186+ --location=us-central
187+ ```
188+
189+ Base64 encode the signature into a temporary variable and use it to upload with ` cosign ` :
190+
191+ ``` shell
192+ $ BASE64_SIGNATURE=$( cat gcpkms.sig | base64)
193+ $ cosign attach signature --payload payload.json --signature $BASE64_SIGNATURE us-central1-docker.pkg.dev/user/test/taskrun
194+ ```
195+
196+ Now (on another machine) use ` cosign ` to download signature bundle and dump into a JSON file:
197+
198+ ``` shell
199+ $ cosign download signature us-central1-docker.pkg.dev/user/test/taskrun > signatures.json
200+ ```
201+
202+ Extract a payload and signature value and dump into their own respective files:
203+
204+ ``` shell
205+ $ cat signatures.json | tail -1 | jq -r .Payload | base64 -D > payload
206+ $ cat signatures.json | tail -1 | jq -r .Base64Signature | base64 -D > signature
207+ ```
208+
209+ Download (on the same machine as the previous step) the public key:
210+
211+ ``` shell
212+ $ gcloud kms keys versions get-public-key 1 --key=foo --keyring=foo --location=us-central1 > pubkey.pem
213+ ```
214+
215+ Finally, verify the signature with ` openssl ` :
216+
217+ ``` shell
218+ $ openssl dgst -sha256 -verify pubkey.pem -signature gcpkms.sig payload
219+ ```
220+
221+ ### AWS KMS with ` aws `
222+
223+ To use a AWS KMS CMK (Custom Master Key) for signing and verification, first create the CMK (just need to do this once) using the ` aws ` CLI (Version 2):
224+
225+ ``` shell
226+ $ export AWS_CMK_ID=$( aws kms create-key --customer-master-key-spec RSA_4096 \
227+ --key-usage SIGN_VERIFY \
228+ --description " Cosign Signature Key Pair" \
229+ --query KeyMetadata.KeyId --output text)
230+ ```
231+
232+ Use ` cosign ` to generate the payload:
233+
234+ ``` shell
235+ $ cosign generate docker.io/davivcgarcia/hello-world:latest > payload.json
236+ ```
237+
238+ Sign the payload with the AWS KMS CMK we created above:
239+
240+ ``` shell
241+ $ aws kms sign --key-id $AWS_CMK_ID \
242+ --message file://payload.json \
243+ --message-type RAW \
244+ --signing-algorithm RSASSA_PKCS1_V1_5_SHA_256 \
245+ --output text \
246+ --query Signature > payload.sig
247+ ```
248+
249+ Upload the signature with ` cosign ` :
250+
251+ ``` shell
252+ $ cosign attach signature docker.io/davivcgarcia/hello-world:latest --signature $( < payload.sig) --payload payload.json
253+ ```
254+
255+ Now (on another machine) use cosign to download signature bundle and dump into a JSON file:
256+
257+ ``` shell
258+ $ cosign download signature docker.io/davivcgarcia/hello-world:latest > signatures.json
259+ ```
260+
261+ Extract the payload and signature value and dump into their own respective files:
167262
168263``` shell
169- $ cosign generate user/demo
170- { " Critical " :{ " Identity " :{ " docker-reference " : " " }, " Image " :{ " Docker-manifest-digest " : " 87ef60f558bad79beea6425a3b28989f01dd417164150ab3baab98dcbf04def8 " }, " Type " : " cosign container image signature " }, " Optional " :null}
264+ $ cat signatures.json | tail -1 | jq -r .Base64Signature | base64 -D > remote_payload.sig
265+ $ cat signatures.json | tail -1 | jq -r .Payload | base64 -D > remote_payload.json
171266```
172267
173- This can be piped directly into OpenSSL.
268+ Verify with AWS KMS using the CMK key we created in the first step:
174269
175270``` shell
176- $ cosign generate user/demo | openssl...
271+ $ aws kms verify --key-id $AWS_CMK_ID \
272+ --message file://remote_payload.json \
273+ --message-type RAW \
274+ --signing-algorithm RSASSA_PKCS1_V1_5_SHA_256 \
275+ --signature fileb://remote_payload.sig \
276+ --output text \
277+ --query SignatureValid
177278```
178279
179280## Upload a generated signature
0 commit comments