Sample code for 30+ languages & platforms
Zig

Apple Keychain - Get Certificate by Common Name

See more Apple Keychain Examples

Retrieves the certificate from the Apple Keychain that matches the specified common name (CN).

Note: This example requires Chilkat v10.0.0 or greater.

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();

    // On MacOS or iOS, this searches both the Default keychains and System keychains for the certificate
    // having the specified CN.
    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    cert.loadByCommonName("Certum CA") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{try cert.getSubjectDN(alloc)});

    // Note: In the Apple Keychain, a certificate with its private key represents an identity because it combines the certificate and its private key.
    // When Chilkat loads a certificate from the Apple Keychain, it also gets the associated private key.
    // You can simply use the cert in other places in Chilkat where a cert w/ private key is required.
    const cert2 = try chilkat.Cert.init();
    defer cert2.deinit();
    cert2.loadByCommonName("Chilkat Software, Inc.") catch {
        std.debug.print("{s}\n", .{try cert2.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{try cert2.getSubjectDN(alloc)});
    const has_priv_key = cert2.hasPrivateKey();
    std.debug.print("Has private key: {}\n", .{has_priv_key});

    std.debug.print("Success.\n", .{});
}