Sample code for 30+ languages & platforms
Rust

Add Trusted Certificate to JKS

See more Java KeyStore (JKS) Examples

Adds a trusted certificate to a Java keystore 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 = "secret".to_string();
let jks_path = "/myJksTrustedCerts/cacerts.jks".to_string();

// Load the Java keystore from a file.
if jks.load_file(&jks_password, &jks_path).is_err() {
    println!("{}", jks.last_error_text());
    return;
}

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

// The cert's LoadFrommFile method can load a certificate from
// virtually any format.  It will automatically determine the format
// and load appropriately.
if cert.load_from_file("/certFiles/myNewTrustedCert.pem").is_err() {
    println!("{}", cert.last_error_text());
    return;
}

// The alias can be anything.  It's basically just a label 
// used within the JKS associated with the entry.  It should
// be unique among aliases within the JKS file.
let alias = "habanero".to_string();

if jks.add_trusted_cert(&cert, &alias).is_err() {
    println!("{}", jks.last_error_text());
    return;
}

// Write the JKS containing the new certificate.
if jks.to_file(&jks_password, &jks_path).is_err() {
    println!("{}", jks.last_error_text());
    return;
}

println!("Added a trusted certificate to the JKS.");