Skip to content

Commit

Permalink
Introduce SignedArtifactBundle
Browse files Browse the repository at this point in the history
This commit adds a new struct named SignedArtifactBundle which
represents a signed artifact bundle as produced when signing with cosign
and specifying the --bundle option.

The motivation for change comes from trying to implement an example
that verifies a blob using a bundle. For example, first a blob is
signed using the following command:

cosign sign-blob --bundle=artifact.bundle artifact.txt

The `artifact.bundle` file generated by the above command will look
something like this (shortened to fit the commit message format):

{
  "base64Signature": "...",
  "cert": "...",
  "rekorBundle": {
    "SignedEntryTimestamp": "...",
    "Payload": {
      "body": "...",
      "integratedTime": 1669361833,
      "logIndex": 7810348,
      "logID": "..."
    }
  }
}

Currently, to create Bundle from this, one would have to parse the
string as json, and then access the `rekorBundle` element, and then
serialize it so that it can be passed to `Bundle::new_verified`.

With the changes in this commit it will be possible to call
`SignedArtifactBundle::new_verified` and pass in the contents for the
bundle file directly.

Refs: sigstore#117

Signed-off-by: Daniel Bevenius <[email protected]>
  • Loading branch information
danbev committed Dec 8, 2022
1 parent bd2a406 commit 3cec130
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/cosign/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ use std::cmp::PartialEq;
use crate::crypto::{CosignVerificationKey, Signature};
use crate::errors::{Result, SigstoreError};

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct SignedArtifactBundle {
pub base64_signature: String,
pub cert: String,
pub rekor_bundle: Bundle,
}

impl SignedArtifactBundle {
/// Create a new verified `SignedArtifactBundle`.
///
/// **Note well:** The bundle will be returned only if it can be verified
/// using the supplied `rekor_pub_key` public key.
#[allow(dead_code)]
pub(crate) fn new_verified(raw: &str, rekor_pub_key: &CosignVerificationKey) -> Result<Self> {
let bundle: SignedArtifactBundle = serde_json::from_str(raw).map_err(|e| {
SigstoreError::UnexpectedError(format!("Cannot parse bundle |{}|: {:?}", raw, e))
})?;
Bundle::verify_bundle(&bundle.rekor_bundle, rekor_pub_key).map(|_| bundle)
}
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "PascalCase")]
pub struct Bundle {
Expand All @@ -36,7 +58,17 @@ impl Bundle {
let bundle: Bundle = serde_json::from_str(raw).map_err(|e| {
SigstoreError::UnexpectedError(format!("Cannot parse bundle |{}|: {:?}", raw, e))
})?;
Self::verify_bundle(&bundle, rekor_pub_key).map(|_| bundle)
}

/// Verify a `Bundle`.
///
/// **Note well:** The bundle will be returned only if it can be verified
/// using the supplied `rekor_pub_key` public key.
pub(crate) fn verify_bundle(
bundle: &Bundle,
rekor_pub_key: &CosignVerificationKey,
) -> Result<()> {
let mut buf = Vec::new();
let mut ser = serde_json::Serializer::with_formatter(&mut buf, CanonicalFormatter::new());
bundle.payload.serialize(&mut ser).map_err(|e| {
Expand All @@ -50,7 +82,7 @@ impl Bundle {
Signature::Base64Encoded(bundle.signed_entry_timestamp.as_bytes()),
&buf,
)?;
Ok(bundle)
Ok(())
}
}

Expand Down Expand Up @@ -110,4 +142,19 @@ OSWS1X9vPavpiQOoTTGC0xX57OojUadxF1cdQmrsiReWg2Wn4FneJfa8xw==

assert!(bundle.is_err());
}

#[test]
fn signedartifactbundle_new_verified_success() {
// Bundle as generated by running the following command, and taking the
// content from the generated 'artifact.bundle` file:
// cosign sign-blob --bundle=artifact.bundle artifact.txt
let bundle_raw = r#"
{"base64Signature":"MEQCIGp1XZP5zaImosrBhDPCdXn3f8xI9FHGLsGVx6UeRPCgAiAt5GrsdQhOKnZcA3EWecvgJSHzCIjWifFBQkD7Hdsymg==","cert":"LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUNxRENDQWkrZ0F3SUJBZ0lVVFBXVGZPLzFOUmFTRmRlY2FBUS9wQkRHSnA4d0NnWUlLb1pJemowRUF3TXcKTnpFVk1CTUdBMVVFQ2hNTWMybG5jM1J2Y21VdVpHVjJNUjR3SEFZRFZRUURFeFZ6YVdkemRHOXl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phR1ZzTTFCcGFraFJiSGxLTVZFMVN6WTBjREI0Y1VSa2J6ZFhOR1o0Wm05QlV6bDRjbEFLY3pKUVMxRmpaRzlFT1dKWWQyZEpkMWcyZWt4cWVXSmFhMDVJVURWNGRFSndOM1pMTWtaWlpWcDBNRTlYVEZKc1ZXeHNZMVZFVEROVUx6ZEtVV1ozWXdwSFUzRTJkbFpDVG5kS01EQjNPVWhTQ2kwdExTMHRSVTVFSUVORlVsUkpSa2xEUVZSRkxTMHRMUzBLIn19fX0=","integratedTime":1669361833,"logIndex":7810348,"logID":"c0d23d6ad406973f9559f3ba2d1ca01f84147d8ffc5b8445c224f98b9591801d"}}}
"#;
let rekor_pub_key = get_rekor_public_key();
let result = SignedArtifactBundle::new_verified(&bundle_raw, &rekor_pub_key);
assert!(result.is_ok());
let bundle = result.unwrap();
assert_eq!(bundle.rekor_bundle.payload.log_index, 7810348);
}
}

0 comments on commit 3cec130

Please sign in to comment.