Rust
Rust
Add Private Key and Certificate to a PEM
See more PEM Examples
Demonstrates how to add certificates and private keys to a PEM.Chilkat Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The Chilkat PEM class was introduced in v9.5.0.49.
// It requires the bundle to be unlocked, as shown above.
let pem = chilkat::Pem::new();
// Add the private key found in alice.key to this PEM.
//
let priv_key = chilkat::PrivateKey::new();
if priv_key.load_any_format_file("qa_data/alice.key", "").is_err() {
println!("{}", priv_key.last_error_text());
return;
}
// Add it to the PEM:
let _ = pem.add_private_key(&priv_key).is_ok();
// Add the certificate found in alice.crt to this PEM.
//
let cert = chilkat::Cert::new();
if cert.load_from_file("qa_data/alice.crt").is_err() {
println!("{}", cert.last_error_text());
return;
}
// Add it to the PEM:
let include_cert_chain = false;
let _ = pem.add_cert(&cert, include_cert_chain).is_ok();
// Write the PEM containing the private key and certificate.
// The private key will be output in PKCS8 encrypted form.
// Certificates are never encrypted.
// This is the password that will be required to open and access the private key
// from the PEM we're about to write..
let password = "secret".to_string();
let extended_attrs = false;
let no_keys = false;
let no_certs = false;
let no_ca_certs = false;
let encrypt_alg = "aes128".to_string();
let pem_str = pem.to_pem_ex(extended_attrs, no_keys, no_certs, no_ca_certs, &encrypt_alg, &password).unwrap_or_default();
println!("{}", pem_str);