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

Convert Java KeyStore to PEM

See more Java KeyStore (JKS) Examples

Loads a Java keystore file and saves the trusted certificate entries to a PEM file.

Chilkat Rust Downloads

Rust

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

let jks = chilkat::JavaKeyStore::new();

let jks_password = "myJksPassword".to_string();

// Load the Java keystore from a file.  The JKS file password is used
// to verify the keyed digest that is found at the very end of the keystore.
// It verifies that the keystore has not been modified.
if jks.load_file(&jks_password, "/someDir/keyStore.jks").is_err() {
    println!("{}", jks.last_error_text());
    return;
}

// Open/create the output PEM file. 
// This example uses Chilkat's file access class for writing the output file.
// You may replace the file I/O lines of code with whatever is most convenient for you.
let fac = chilkat::FileAccess::new();
if fac.open_for_write("/pemFiles/caCerts.pem").is_err() {
    println!("{}", fac.last_error_text());
    return;
}

let num_certs = jks.num_trusted_certs();

let cert = chilkat::Cert::new();
let mut pem = String::new();

// Iterate over the trusted certs, get the PEM for each,
// and append it to the output file.
let mut i = 0;
while i < num_certs {
    let _ = jks.trusted_cert_at(i, &cert);

    // Get the certificate in PEM format.  
    pem = cert.export_cert_pem().unwrap_or_default();

    // Append the PEM string to the open file.
    if fac.append_text(&pem, "utf-8").is_err() {
        println!("{}", fac.last_error_text());
        return;
    }

    i = i + 1;
}

// Close the output file.
fac.file_close();

println!("Trusted certificates saved to PEM.");