Sample code for 30+ languages & platforms
Swift

Add Trusted Certificate to JKS

See more Java KeyStore (JKS) Examples

Adds a trusted certificate to a Java keystore file.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let jks = CkoJavaKeyStore()!

    var jksPassword: String? = "secret"
    var jksPath: String? = "/myJksTrustedCerts/cacerts.jks"

    // Load the Java keystore from a file.
    success = jks.loadFile(password: jksPassword, path: jksPath)
    if success != true {
        print("\(jks.lastErrorText!)")
        return
    }

    let cert = CkoCert()!

    // The cert's LoadFrommFile method can load a certificate from
    // virtually any format.  It will automatically determine the format
    // and load appropriately.
    success = cert.load(fromFile: "/certFiles/myNewTrustedCert.pem")
    if success != true {
        print("\(cert.lastErrorText!)")
        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.
    var alias: String? = "habanero"

    success = jks.addTrustedCert(cert: cert, alias: alias)
    if success != true {
        print("\(jks.lastErrorText!)")
        return
    }

    // Write the JKS containing the new certificate.
    success = jks.toFile(password: jksPassword, path: jksPath)
    if success != true {
        print("\(jks.lastErrorText!)")
        return
    }

    print("Added a trusted certificate to the JKS.")

}