Sample code for 30+ languages & platforms
Zig

Add Trusted Certificate to JKS

See more Java KeyStore (JKS) Examples

Adds a trusted certificate to a Java keystore file.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const jks = try chilkat.JavaKeyStore.init();
    defer jks.deinit();

    const jks_password = "secret";
    const jks_path = "/myJksTrustedCerts/cacerts.jks";

    // Load the Java keystore from a file.
    jks.loadFile(jks_password, jks_path) catch {
        std.debug.print("{s}\n", .{try jks.getLastErrorText(alloc)});
        return;
    };

    const cert = try chilkat.Cert.init();
    defer cert.deinit();

    // The cert's LoadFrommFile method can load a certificate from
    // virtually any format.  It will automatically determine the format
    // and load appropriately.
    cert.loadFromFile("/certFiles/myNewTrustedCert.pem") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        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.
    const alias = "habanero";

    jks.addTrustedCert(cert, alias) catch {
        std.debug.print("{s}\n", .{try jks.getLastErrorText(alloc)});
        return;
    };

    // Write the JKS containing the new certificate.
    jks.toFile(jks_password, jks_path) catch {
        std.debug.print("{s}\n", .{try jks.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Added a trusted certificate to the JKS.\n", .{});
}