|
| 1 | +// Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS |
| 2 | + |
| 3 | +use std::path::PathBuf; |
| 4 | + |
| 5 | +use anyhow::Context; |
| 6 | +use clap::Parser; |
| 7 | +use libparsec_platform_pki::{ |
| 8 | + get_der_encoded_certificate, verify_message, Certificate, CertificateHash, SignatureAlgorithm, |
| 9 | + SignedMessage, |
| 10 | +}; |
| 11 | +use sha2::Digest; |
| 12 | + |
| 13 | +mod utils; |
| 14 | + |
| 15 | +#[derive(Debug, Parser)] |
| 16 | +struct Args { |
| 17 | + #[command(flatten)] |
| 18 | + cert: CertificateOrRef, |
| 19 | + #[command(flatten)] |
| 20 | + content: utils::ContentOpts, |
| 21 | + signature_header: SignatureAlgorithm, |
| 22 | + /// Signature in base64 |
| 23 | + signature: String, |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Debug, Clone, clap::Args)] |
| 27 | +#[group(required = true, multiple = false)] |
| 28 | +struct CertificateOrRef { |
| 29 | + /// Hash of the certificate from the store to use to verify the signature. |
| 30 | + #[arg(value_parser = utils::CertificateSRIHashParser)] |
| 31 | + certificate_hash: Option<CertificateHash>, |
| 32 | + /// Path to a file containing the certificate in DER format. |
| 33 | + der_file: Option<PathBuf>, |
| 34 | + /// Path to a file containing the certificate in PEM format. |
| 35 | + pem_file: Option<PathBuf>, |
| 36 | + /// Certificate in PEM format but without headers. |
| 37 | + pem: Option<String>, |
| 38 | +} |
| 39 | + |
| 40 | +fn main() -> anyhow::Result<()> { |
| 41 | + let args = Args::parse(); |
| 42 | + println!("args={args:?}"); |
| 43 | + |
| 44 | + let data = args.content.into_bytes()?; |
| 45 | + |
| 46 | + let signature = data_encoding::BASE64 |
| 47 | + .decode(args.signature.as_bytes()) |
| 48 | + .context("Invalid signature format")?; |
| 49 | + |
| 50 | + let cert = if let Some(hash) = args.cert.certificate_hash { |
| 51 | + let res = |
| 52 | + get_der_encoded_certificate(&libparsec_platform_pki::CertificateReference::Hash(hash))?; |
| 53 | + println!( |
| 54 | + "Will verify signature using cert with id {{{}}}", |
| 55 | + data_encoding::BASE64.encode_display(&res.cert_ref.id) |
| 56 | + ); |
| 57 | + Certificate::from_der_owned(res.der_content.into()) |
| 58 | + } else if let Some(der_file) = args.cert.der_file { |
| 59 | + let raw = std::fs::read(der_file).context("Failed to read file")?; |
| 60 | + Certificate::from_der_owned(raw) |
| 61 | + } else if let Some(pem_file) = args.cert.pem_file { |
| 62 | + let raw = std::fs::read(pem_file).context("Failed to read file")?; |
| 63 | + Certificate::try_from_pem(&r#raw)?.into_owned() |
| 64 | + } else if let Some(pem) = args.cert.pem { |
| 65 | + let raw = data_encoding::BASE64 |
| 66 | + .decode(pem.as_bytes()) |
| 67 | + .context("Invalid pem base64")?; |
| 68 | + Certificate::from_der_owned(raw) |
| 69 | + } else { |
| 70 | + unreachable!("Should be handle by clap") |
| 71 | + }; |
| 72 | + |
| 73 | + #[cfg(feature = "hash-sri-display")] |
| 74 | + { |
| 75 | + let fingerprint = |
| 76 | + CertificateHash::SHA256(Box::new(sha2::Sha256::digest(cert.as_ref()).into())); |
| 77 | + println!("Certificate fingerprint: {fingerprint}"); |
| 78 | + } |
| 79 | + |
| 80 | + let signed_message = SignedMessage { |
| 81 | + algo: args.signature_header, |
| 82 | + signature, |
| 83 | + message: data, |
| 84 | + }; |
| 85 | + |
| 86 | + match verify_message(&signed_message, cert) { |
| 87 | + Ok(_) => { |
| 88 | + println!("The message as a correct signature") |
| 89 | + } |
| 90 | + Err(e) => println!("The message as an incorrect signature: {e}"), |
| 91 | + } |
| 92 | + |
| 93 | + Ok(()) |
| 94 | +} |
0 commit comments