Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

Verify Opaque Signature and Retrieve Signing Certificates

See more Digital Signatures Examples

Demonstrates how to verify a PCKS7 opaque digital signature (signed data), extract the original file/data, and then extract the certificate(s) that were used to sign.

Chilkat Rust Downloads

Rust

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let crypt = chilkat::Crypt2::new();

// Verify a PKCS7 signed-data (opaque signature) file and extract the original content to a file.
if crypt.verify_p7_m("qa_data/p7m/opaqueSig.p7", "qa_output/originalData.dat").is_err() {
    println!("{}", crypt.last_error_text());
    return;
}

// Alternatively, we can do it in memory...
let bin_data = chilkat::BinData::new();
let _ = bin_data.load_file("qa_data/p7m/opaqueSig.p7").is_ok();
// Your app should check for success, but we'll skip the check for brevity..

// If verified, the signature is unwrapped and binData is replaced with the original data that was signed.
if crypt.opaque_verify_bd(&bin_data).is_err() {
    println!("{}", crypt.last_error_text());
    return;
}

// For our testing, we signed some text, so we can get it from the binData..
println!("Original Data:");
println!("{}", bin_data.get_string("utf-8").unwrap_or_default());

// After any method call that verifies a signature, the crypt object will contain the certificate(s)
// that were used for signing (assuming the X.509 certs were available in the signature, which is typically the case).

// Get each signing certificate, and build the certificate chain for each.
let cert = chilkat::Cert::new();
let cert_chain = chilkat::CertChain::new();
let num_certs = crypt.num_signer_certs();
let mut i = 0;
while i < num_certs {
    let _ = crypt.last_signer_cert(i, &cert);
    println!("{}", cert.subject_dn());

    if cert.build_cert_chain(&cert_chain).is_err() {
        println!("{}", cert.last_error_text());
        return;
    }

    i = i + 1;
}