Sample code for 30+ languages & platforms
Zig

Get Certificate Subject Part by Name or OID

Demonstrates the GetSubjectPart method where a name or OID can be passed.

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

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

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

    // This example demonstrates getting the SERIALNUMBER part of the subject,
    // if one such part exists in the cert's subject.
    // Not too many certicates include the serial number in the Subject.
    // (Chilean issued certificates for SII is one such case where the SERIALNUMBER part exists within the Subject.)
    var serial_num: [:0]const u8 = cert.getSubjectPart(alloc, "SERIALNUMBER") catch {
        std.debug.print("No SERIALNUMBER part exists.\n", .{});
        return;
    };

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

    // We could also get the part by the OID.
    // See https://oidref.com/2.5.4.5 for a list of OID's in the 2.5.4.* range..
    // The OID for SERIALNUMBER is "2.5.4.5"
    serial_num = cert.getSubjectPart(alloc, "2.5.4.5") catch {
        std.debug.print("No SERIALNUMBER part exists.\n", .{});
        return;
    };

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