Sample code for 30+ languages & platforms
Rust

Duplicate openssl pkcs12 –export –in certfile.cer –inkey certfile.key –out certfile.pfx

See more OpenSSL Examples

How to create a PKCS12 (.p12 or .pfx) from a certificate file and private key file: Demonstrates how to duplicate this OpenSSL command:
Duplicate openssl pkcs12 –export –in certfile.cer –inkey certfile.key –out certfile.pfx

Chilkat Rust Downloads

Rust

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

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

// Load the private key from the file.
if pkey.load_any_format_file("certFile.key", "").is_err() {
    println!("{}", pkey.last_error_text());
    return;
}

let cert = chilkat::Cert::new();
// The LoadFromFile method auto-recognizes the format...
if cert.load_from_file("certfile.cer").is_err() {
    println!("{}", cert.last_error_text());
    return;
}

// We'll need a cert chain object to create the PKCS12, so get it
// from the cert.  
let Ok(cert_chain) = cert.get_cert_chain() else {
    println!("{}", cert.last_error_text());
    return;
};

// Create the PFX object, add the cert and private key, and write to a .pfx file.
let pfx = chilkat::Pfx::new();

// The cert(s) are automatically added in the call to AddPrivateKey
if pfx.add_private_key(&pkey, &cert_chain).is_err() {
    println!("{}", pfx.last_error_text());
    return;
}

// Write the .pfx to a file.
let password = "myPassword".to_string();
if pfx.to_file(&password, "certfile.pfx").is_err() {
    println!("{}", pfx.last_error_text());
    return;
}

println!("Success.");