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

Load Certificate from PFX (PKCS#12)

See more Certificates Examples

Loads a digital certificate (and private key, if available) from a PFX file.(also known as PKCS#12)

Chilkat Rust Downloads

Rust

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

// Load from the PFX file
let pfx_filename = "/Users/chilkat/testData/pfx/chilkat_ssl_pwd_is_test.pfx".to_string();
let pfx_password = "test".to_string();

// A PFX typically contains certificates in the chain of authentication.
// The Chilkat cert object will choose the certificate w/
// private key farthest from the root authority cert.
// To access all the certificates in a PFX, use the 
// Chilkat certificate store object instead.
if cert.load_pfx_file(&pfx_filename, &pfx_password).is_err() {
    println!("{}", cert.last_error_text());
    return;
}

// Get some information about the digital certificate, 
// then get the private key...

// DN = "Distinguished Name"
println!("SubjectDN:{}", cert.subject_dn());

println!("Common Name:{}", cert.subject_cn());
println!("Issuer Common Name:{}", cert.issuer_cn());

println!("Serial Number:{}", cert.serial_number());

let priv_key = chilkat::PrivateKey::new();
if cert.get_private_key(&priv_key).is_err() {
    println!("{}", cert.last_error_text());
    return;
}

// The private key object may be used in any Chilkat methods
// (in other objects/classes) that expect a private key argument.

// In this case, save the private key to a PKCS8 Encrypted PEM format file:
let pem_password = "secret".to_string();
let pem_path = "/Users/chilkat/testData/pem/chilkat_privKey.pem".to_string();
if priv_key.save_pkcs8_encrypted_pem_file(&pem_password, &pem_path).is_err() {
    println!("{}", priv_key.last_error_text());
    return;
}

println!("Private key saved to PKCS8 Encrypted PEM...");