Sample code for 30+ languages & platforms
Rust

Get Certificates within XML Signature

See more XML Digital Signatures Examples

Demonstrates how to get the certificates contained within an XML signature.

Chilkat Rust Downloads

Rust

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

let sb_xml = chilkat::StringBuilder::new();

// Load XML containing one or more signatures.
if sb_xml.load_file("qa_data/xml_dsig_valid_samples/multipleSigners/sp.pdf.XAdES.xml", "utf-8").is_err() {
    println!("Failed to load the XML file..");
    return;
}

let dsig = chilkat::XmlDSig::new();

// First load the XML containing the signatures to be verified.
// Note that this particular Signature already contains the RSA public key that will be used
// for verification.
if dsig.load_signature_sb(&sb_xml).is_err() {
    println!("{}", dsig.last_error_text());
    return;
}

// For each signature, verify and also get the certificate(s) contained within each Signature.
let mut i = 0;
let sa_certs = chilkat::StringArray::new();
let cert = chilkat::Cert::new();

println!("numSignatures = {}", dsig.num_signatures());

while i < dsig.num_signatures() {
    // Select the Nth signature by setting the Selector property.
    dsig.set_selector(i);

    let b_verify_reference_digests = true;
    let b_verified = dsig.verify_signature(b_verify_reference_digests).is_ok();
    println!("Signature {} verified = {}", i + 1, b_verified);

    // Get the certificates embedded in this signature.
    sa_certs.clear();
    if dsig.get_certs(&sa_certs).is_ok() {
        let mut j = 0;
        while j < sa_certs.count() {
            if cert.load_from_base64(&sa_certs.get_string(j).unwrap_or_default()).is_ok() {
                println!("    {}", cert.subject_dn());
            }

            j = j + 1;
        }

    }

    i = i + 1;
}