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

Load PKCS12 / PFX and Access Contents

See more PFX/P12 Examples

Loads a PKCS12 / PFX file and iterates over the contents which include private keys and certificates.

Chilkat Rust Downloads

Rust

let pfx = chilkat::Pfx::new();

// Load the PKCS12 from a file
if pfx.load_pfx_file("/someDir/my.p12", "pfxFilePassword").is_err() {
    println!("{}", pfx.last_error_text());
    return;
}

let num_private_keys = pfx.num_private_keys();

let priv_key = chilkat::PrivateKey::new();

println!("Private Keys:");

let mut i = 0;
while i < num_private_keys {
    let _ = pfx.private_key_at(i, &priv_key);

    // Do something with the private key ...

    i = i + 1;
}

let cert = chilkat::Cert::new();

let num_certs = pfx.num_certs();

println!("Certs:");
i = 0;
while i < num_certs {
    let _ = pfx.cert_at(i, &cert);
    println!("{}", cert.subject_dn());

    // If the certificate has a private key (one of the private keys within the PFX)
    // then it can also be obtained via the certificate object:
    if cert.has_private_key() {

        println!("Has private key!");

        let _ = cert.get_private_key(&priv_key).is_ok();
        // ...

    }

    i = i + 1;
}