Skip to content

Commit 8b965f4

Browse files
authored
Merge pull request #403 from jku/verify_blob-change-cert-arg
cosign: Make verify-blob compatible with sigstore-python
2 parents 170a765 + 2ca912d commit 8b965f4

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

examples/cosign/verify-blob/main.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
extern crate clap;
1717
extern crate sigstore;
18+
use base64::{engine::general_purpose::STANDARD as BASE64_STD_ENGINE, Engine as _};
1819
use clap::Parser;
1920
use sigstore::cosign::client::Client;
2021
use sigstore::cosign::CosignCapabilities;
@@ -56,11 +57,17 @@ pub async fn main() {
5657
.with(fmt::layer().with_writer(std::io::stderr))
5758
.init();
5859

59-
let certificate = fs::read_to_string(&cli.certificate).expect("error reading certificate");
60+
// certificate may be PEM or "double base64 encoded PEM" (cosign).
61+
let cert_input = fs::read_to_string(&cli.certificate).expect("error reading certificate");
62+
let certificate = match BASE64_STD_ENGINE.decode(cert_input.clone()) {
63+
Ok(res) => String::from_utf8(res).expect("error stringifying PEM certificate"),
64+
Err(_) => cert_input,
65+
};
66+
6067
let signature = fs::read_to_string(&cli.signature).expect("error reading signature");
6168
let blob = fs::read(cli.blob.as_str()).expect("error reading blob file");
6269

63-
match Client::verify_blob(&certificate, &signature, &blob) {
70+
match Client::verify_blob(&certificate, signature.trim(), &blob) {
6471
Ok(_) => println!("Verification succeeded"),
6572
Err(e) => eprintln!("Verification failed {:?}", e),
6673
}

examples/cosign/verify-bundle/main.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16+
use base64::{engine::general_purpose::STANDARD as BASE64_STD_ENGINE, Engine as _};
1617
use clap::Parser;
1718
use sigstore::cosign::bundle::SignedArtifactBundle;
1819
use sigstore::cosign::client::Client;
@@ -62,7 +63,14 @@ pub async fn main() {
6263
let blob = fs::read(cli.blob.as_str()).expect("error reading blob file");
6364

6465
let bundle = SignedArtifactBundle::new_verified(&bundle_json, &rekor_pub_key).unwrap();
65-
match Client::verify_blob(&bundle.cert, &bundle.base64_signature, &blob) {
66+
67+
// certificate in bundle is double base64 encoded, remove one layer:
68+
let cert_data = BASE64_STD_ENGINE
69+
.decode(bundle.cert)
70+
.expect("Error decoding base64 certificate");
71+
let cert = String::from_utf8(cert_data).expect("error stringifying PEM certificate");
72+
73+
match Client::verify_blob(&cert, &bundle.base64_signature, &blob) {
6674
Ok(_) => println!("Verification succeeded"),
6775
Err(e) => eprintln!("Verification failed: {}", e),
6876
}

src/cosign/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ use crate::registry::{Auth, PushResponse};
4646

4747
use crate::crypto::{CosignVerificationKey, Signature};
4848
use crate::errors::SigstoreError;
49-
use base64::{engine::general_purpose::STANDARD as BASE64_STD_ENGINE, Engine as _};
5049
use pkcs8::der::Decode;
5150
use x509_cert::Certificate;
5251

@@ -155,13 +154,13 @@ pub trait CosignCapabilities {
155154
/// Verifies the signature produced by cosign when signing the given blob via the `cosign sign-blob` command
156155
///
157156
/// The parameters:
158-
/// * `cert`: a PEM encoded x509 certificate that contains the public key used to verify the signature
157+
/// * `cert`: a PEM encoded x509 certificate that contains the public key used to verify the signature.
158+
/// Note that cert is not double-base64-encoded like the output of sigstore/cosign is.
159159
/// * `signature`: the base64 encoded signature of the blob that has to be verified
160160
/// * `blob`: the contents of the blob
161161
///
162162
/// This function returns `Ok())` when the given signature has been verified, otherwise returns an `Err`.
163163
fn verify_blob(cert: &str, signature: &str, blob: &[u8]) -> Result<()> {
164-
let cert = BASE64_STD_ENGINE.decode(cert)?;
165164
let pem = pem::parse(cert)?;
166165
let cert = Certificate::from_der(pem.contents()).map_err(|e| {
167166
SigstoreError::PKCS8SpkiError(format!("parse der into cert failed: {e}"))

0 commit comments

Comments
 (0)