Sample code for 30+ languages & platforms
Zig

Get Certificates within XML Signature

See more XML Digital Signatures Examples

Demonstrates how to get the certificates contained within an XML signature.

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 example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const sb_xml = try chilkat.StringBuilder.init();
    defer sb_xml.deinit();

    // Load XML containing one or more signatures.
    sb_xml.loadFile("qa_data/xml_dsig_valid_samples/multipleSigners/sp.pdf.XAdES.xml", "utf-8") catch {
        std.debug.print("Failed to load the XML file..\n", .{});
        return;
    };

    const dsig = try chilkat.XmlDSig.init();
    defer dsig.deinit();

    // First load the XML containing the signatures to be verified.
    // Note that this particular Signature already contains the RSA public key that will be used
    // for verification.
    dsig.loadSignatureSb(sb_xml) catch {
        std.debug.print("{s}\n", .{try dsig.getLastErrorText(alloc)});
        return;
    };

    // For each signature, verify and also get the certificate(s) contained within each Signature.
    var i: i32 = 0;
    const sa_certs = try chilkat.StringArray.init();
    defer sa_certs.deinit();
    const cert = try chilkat.Cert.init();
    defer cert.deinit();

    std.debug.print("numSignatures = {d}\n", .{dsig.getNumSignatures()});

    while (i < dsig.getNumSignatures()) {
        // Select the Nth signature by setting the Selector property.
        dsig.setSelector(i);

        const b_verify_reference_digests = true;
        const b_verified = if (dsig.verifySignature(b_verify_reference_digests)) true else |_| false;
        std.debug.print("Signature {d} verified = {}\n", .{ i + 1, b_verified });

        // Get the certificates embedded in this signature.
        sa_certs.clear();
        if (dsig.getCerts(sa_certs)) {
            var j: i32 = 0;
            while (j < sa_certs.getCount()) {
                if (cert.loadFromBase64(try sa_certs.getString(alloc, j))) {
                    std.debug.print("    {s}\n", .{try cert.getSubjectDN(alloc)});
                } else |_| {}

                j = j + 1;
            }
        } else |_| {}

        i = i + 1;
    }
}