Sample code for 30+ languages & platforms
Zig

Create XAdES with ec:InclusiveNamespaces within the Transforms

See more XML Digital Signatures Examples

Demonstrates how to generate XAdES that has a Reference that has Transforms that include an ec:InclusiveNamespaces.

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.

    // This is the XML we'll be signing:

    // <soapenv:Envelope xmlns:obs="http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    //     <soapenv:Header>
    //         <wsse:Security
    //             xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
    //             xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" soapenv:mustUnderstand="1">
    //             <wsse:BinarySecurityToken
    //                 EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"
    //                 ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1"
    //                 wsu:Id="X509-02BF0107214FC61449FD0013DF68F0359">MII...</wsse:BinarySecurityToken>
    //         </wsse:Security>
    //     </soapenv:Header>
    //     <soapenv:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
    //         wsu:Id="id-396BB6026342EB5C0E1EA73593B3CC098">
    //         <obs:zapiszKomunikatOS>
    //             <komunikatOS>
    //                 <idPodmiotuRaportujacego>
    //                     <idBiznesowy>000000011986</idBiznesowy>
    //                     <rodzajPodmiotuRaportujacego>PA</rodzajPodmiotuRaportujacego>
    //                 </idPodmiotuRaportujacego>
    //             </komunikatOS>
    //         </obs:zapiszKomunikatOS>
    //     </soapenv:Body>
    // </soapenv:Envelope>
    //

    // ----------------------------------------------------------------------
    // IMPORTANT: This generated example requires Chilkat v9.5.0.77 or later.
    // ----------------------------------------------------------------------
    // Create the above XML to be signed...
    const xml_to_sign = try chilkat.Xml.init();
    defer xml_to_sign.deinit();
    xml_to_sign.setTag("soapenv:Envelope");
    xml_to_sign.addAttribute("xmlns:obs", "http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/") catch {};
    xml_to_sign.addAttribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/") catch {};
    xml_to_sign.updateAttrAt("soapenv:Header|wsse:Security", true, "xmlns:wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd") catch {};
    xml_to_sign.updateAttrAt("soapenv:Header|wsse:Security", true, "xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd") catch {};
    xml_to_sign.updateAttrAt("soapenv:Header|wsse:Security", true, "soapenv:mustUnderstand", "1") catch {};
    xml_to_sign.updateAttrAt("soapenv:Header|wsse:Security|wsse:BinarySecurityToken", true, "EncodingType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary") catch {};
    xml_to_sign.updateAttrAt("soapenv:Header|wsse:Security|wsse:BinarySecurityToken", true, "ValueType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1") catch {};
    xml_to_sign.updateAttrAt("soapenv:Header|wsse:Security|wsse:BinarySecurityToken", true, "wsu:Id", "X509-02BF0107214FC61449FD0013DF68F0359") catch {};
    // Note: The content of this XML node is a placeholder that will be updated below with the X509PKIPathv1 for the signing certificate.
    xml_to_sign.updateChildContent("soapenv:Header|wsse:Security|wsse:BinarySecurityToken", "BinarySecurityToken_Base64Binary_Content");
    xml_to_sign.updateAttrAt("soapenv:Body", true, "xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd") catch {};
    xml_to_sign.updateAttrAt("soapenv:Body", true, "wsu:Id", "id-396BB6026342EB5C0E1EA73593B3CC098") catch {};
    xml_to_sign.updateChildContent("soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|idBiznesowy", "000000011986");
    xml_to_sign.updateChildContent("soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|rodzajPodmiotuRaportujacego", "PA");

    const gen = try chilkat.XmlDSigGen.init();
    defer gen.deinit();

    gen.setSigLocation("soapenv:Envelope|soapenv:Header|wsse:Security");
    gen.setSigLocationMod(0);
    gen.setSigId("SIG-BB965DFC3C8AAF87903C0ED898B8D2A8D");
    gen.setSigNamespacePrefix("ds");
    gen.setSigNamespaceUri("http://www.w3.org/2000/09/xmldsig#");
    gen.setSignedInfoCanonAlg("EXCL_C14N");
    gen.setSignedInfoDigestMethod("sha1");

    // Set the KeyInfoId before adding references..
    gen.setKeyInfoId("KI-9D95C38916099AD2EE87DDAC1A76E97E4");

    // -------- Reference 1 --------
    gen.addSameDocRef("id-396BB6026342EB5C0E1EA73593B3CC098", "sha1", "EXCL_C14N", "obs", "") catch {};

    // The reference to be produced in the Signature should look like this:

    // <ds:Reference URI="#id-396BB6026342EB5C0E1EA73593B3CC098">
    //     <ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
    //         <ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="obs"></ec:InclusiveNamespaces>
    //     </ds:Transform></ds:Transforms>
    //     <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></ds:DigestMethod>
    //     <ds:DigestValue>2e9hZYj/CN2nPsgQqUraU43k3ds=</ds:DigestValue>
    // </ds:Reference>
    //

    // Provide a certificate + private key. (PFX password is test123)
    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    cert.loadPfxFile("qa_data/pfx/cert_test123.pfx", "test123") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    gen.setX509Cert(cert, true) catch {};

    gen.setKeyInfoType("Custom");

    // Create the custom KeyInfo XML..
    const xml_custom_key_info = try chilkat.Xml.init();
    defer xml_custom_key_info.deinit();
    xml_custom_key_info.setTag("wsse:SecurityTokenReference");
    xml_custom_key_info.addAttribute("wsse11:TokenType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1") catch {};
    xml_custom_key_info.addAttribute("xmlns:wsse11", "http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd") catch {};
    xml_custom_key_info.addAttribute("wsu:Id", "STR-FF238E7C061332C5B19752C2FBC8CDEF2") catch {};
    xml_custom_key_info.updateAttrAt("wsse:Reference", true, "URI", "#X509-02BF0107214FC61449FD0013DF68F0359") catch {};
    xml_custom_key_info.updateAttrAt("wsse:Reference", true, "ValueType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1") catch {};

    xml_custom_key_info.setEmitXmlDecl(false);
    gen.setCustomKeyInfoXml(try xml_custom_key_info.getXml(alloc));

    // Load XML to be signed...
    const sb_xml = try chilkat.StringBuilder.init();
    defer sb_xml.deinit();
    xml_to_sign.getXmlSb(sb_xml) catch {};

    // Update BinarySecurityToken_Base64Binary_Content with the actual X509PKIPathv1 of the signing cert.
    _ = sb_xml.replace("BinarySecurityToken_Base64Binary_Content", try cert.x509PKIPathv1(alloc));

    gen.setBehaviors("IndentedSignature");

    // Sign the XML...
    gen.createXmlDSigSb(sb_xml) catch {
        std.debug.print("{s}\n", .{try gen.getLastErrorText(alloc)});
        return;
    };

    // -----------------------------------------------

    // Save the signed XML to a file.
    try sb_xml.writeFile("qa_output/signedXml.xml", "utf-8", false);

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

    // ----------------------------------------
    // Verify the signatures we just produced...
    const verifier = try chilkat.XmlDSig.init();
    defer verifier.deinit();
    verifier.loadSignatureSb(sb_xml) catch {
        std.debug.print("{s}\n", .{try verifier.getLastErrorText(alloc)});
        return;
    };

    const num_sigs = verifier.getNumSignatures();
    var verify_idx: i32 = 0;
    while (verify_idx < num_sigs) {
        verifier.setSelector(verify_idx);
        verifier.verifySignature(true) catch {
            std.debug.print("{s}\n", .{try verifier.getLastErrorText(alloc)});
            return;
        };

        verify_idx = verify_idx + 1;
    }

    std.debug.print("All signatures were successfully verified.\n", .{});
}