Sample code for 30+ languages & platforms
Zig

Subject Alternative Name for ICP Brasil Certs

See more Certificates Examples

Demonstrates how to access the multiple names contained in the Subject Alternative Name of a certificate. This example is for an ICP Brasil certificate.

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.loadFromFile("qa_data/certs/testIcpBrasil.cer") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    // If the cert's Subject Alternative Name contains multiple values, each specified by an OID,
    // then the Rfc822Name property returns XML.
    const subject_alt_name_xml = try cert.getRfc822Name(alloc);

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

    // Here's a sample of the subjectAltNameXml:

    // <SubjectAltName>
    //     <name type="oid" oid="2.16.76.1.3.4">290999653223955019700000000000000000000150424SRJUXPTS</name>
    //     <name type="oid" oid="2.16.76.1.3.2">JOAO SILVA</name>
    //     <name type="oid" oid="2.16.76.1.3.3">02408939000115</name>
    //     <name type="oid" oid="2.16.76.1.3.7">000000000000</name>
    // </SubjectAltName>

    // The XML can be parsed like this:

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.loadXml(subject_alt_name_xml) catch {};

    var oid: [:0]const u8 = "";
    var name: [:0]const u8 = "";

    var i: i32 = 0;
    const count_i = xml.numChildrenHavingTag("name");
    while (i < count_i) {
        xml.setI(i);
        oid = try xml.chilkatPath(alloc, "name[i]|(oid)");
        name = try xml.getChildContent(alloc, "name[i]");
        std.debug.print("{s}: {s}\n", .{ oid, name });
        i = i + 1;
    }
}