Sample code for 30+ languages & platforms
Zig

Create Signed SOAP XML for DIAN Colombia WCF Service

See more XAdES Examples

Demonstrates how to create a signed SOAP XML document for DIAN Colombia.

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 example will produce a signed SOAP XML message that looks like this:

    // <?xml version="1.0" encoding="utf-8"?>
    // <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:wcf="http://wcf.dian.colombia">
    //     <soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
    //         <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">
    //             <wsu:Timestamp wsu:Id="TS-F25839120CBA3ECDAD68754D0443A667636FDA68">
    //                 <wsu:Created>2019-08-23T23:03:01Z</wsu:Created>
    //                 <wsu:Expires>2019-08-24T15:43:01Z</wsu:Expires>
    //             </wsu:Timestamp>
    //             <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#X509v3"
    //             wsu:Id="ABCXYZ-9F0F7E15A59816E680B4735080A789DC1EED6C9C">MIIG8jCCBd ... zLjGQUB6lcz</wsse:BinarySecurityToken>
    //             <ds:Signature Id="SIG-F25839120CBA3ECDAD68754D0443A667636FDA68" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    //                 <ds:SignedInfo>
    //                     <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
    //                         <ec:InclusiveNamespaces PrefixList="wsa soap wcf" xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    //                     </ds:CanonicalizationMethod>
    //                     <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
    //                     <ds:Reference URI="#ID-F25839120CBA3ECDAD68754D0443A667636FDA68">
    //                         <ds:Transforms>
    //                             <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
    //                                 <ec:InclusiveNamespaces PrefixList="soap wcf" xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    //                             </ds:Transform>
    //                         </ds:Transforms>
    //                         <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    //                         <ds:DigestValue>gSIKtjS/BKA2bgecXkM8lYVBDqlXcU3juNYT9a+bSnM=</ds:DigestValue>
    //                     </ds:Reference>
    //                 </ds:SignedInfo>
    //                 <ds:SignatureValue>sL7rOdyfkEnKgJja0eWrv ... YqG0T0pflBsGW9zXkjQ9NvAw==</ds:SignatureValue>
    //                 <ds:KeyInfo Id="KI-F25839120CBA3ECDAD68754D0443A667636FDA68">
    //                     <wsse:SecurityTokenReference wsu:Id="STR-F25839120CBA3ECDAD68754D0443A667636FDA68">
    //                         <wsse:Reference URI="#ABCXYZ-9F0F7E15A59816E680B4735080A789DC1EED6C9C" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"/>
    //                     </wsse:SecurityTokenReference>
    //                 </ds:KeyInfo>
    //             </ds:Signature>
    //         </wsse:Security>
    //         <wsa:Action>http://wcf.dian.colombia/IWcfDianCustomerServices/GetStatus</wsa:Action>
    //         <wsa:To wsu:Id="ID-F25839120CBA3ECDAD68754D0443A667636FDA68" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">https://vpfe-hab.dian.gov.co/WcfDianCustomerServices.svc</wsa:To>
    //     </soap:Header>
    //     <soap:Body>
    //         <wcf:GetStatus>
    //             <wcf:trackId>123456666</wcf:trackId>
    //         </wcf:GetStatus>
    //     </soap:Body>
    // </soap:Envelope>

    // Use this online tool to generate code from sample Signed XML:
    // Generate Code to Create Signed XML

    // Create the XML to be signed...
    const xml_to_sign = try chilkat.Xml.init();
    defer xml_to_sign.deinit();
    xml_to_sign.setTag("soap:Envelope");
    xml_to_sign.addAttribute("xmlns:soap", "http://www.w3.org/2003/05/soap-envelope") catch {};
    xml_to_sign.addAttribute("xmlns:wcf", "http://wcf.dian.colombia") catch {};
    xml_to_sign.updateAttrAt("soap:Header", true, "xmlns:wsa", "http://www.w3.org/2005/08/addressing") catch {};
    xml_to_sign.updateAttrAt("soap: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("soap: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("soap:Header|wsse:Security|wsu:Timestamp", true, "wsu:Id", "TS-F25839120CBA3ECDAD68754D0443A667636FDA68") catch {};
    xml_to_sign.updateChildContent("soap:Header|wsse:Security|wsu:Timestamp|wsu:Created", "2019-08-23T23:03:01Z");
    xml_to_sign.updateChildContent("soap:Header|wsse:Security|wsu:Timestamp|wsu:Expires", "2019-08-24T15:43:01Z");
    xml_to_sign.updateAttrAt("soap: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("soap:Header|wsse:Security|wsse:BinarySecurityToken", true, "ValueType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3") catch {};
    xml_to_sign.updateAttrAt("soap:Header|wsse:Security|wsse:BinarySecurityToken", true, "wsu:Id", "ABCXYZ-9F0F7E15A59816E680B4735080A789DC1EED6C9C") catch {};
    xml_to_sign.updateChildContent("soap:Header|wsse:Security|wsse:BinarySecurityToken", "BinarySecurityToken_Base64Binary_Content");
    xml_to_sign.updateChildContent("soap:Header|wsa:Action", "http://wcf.dian.colombia/IWcfDianCustomerServices/GetStatus");
    xml_to_sign.updateAttrAt("soap:Header|wsa:To", true, "wsu:Id", "ID-F25839120CBA3ECDAD68754D0443A667636FDA68") catch {};
    xml_to_sign.updateAttrAt("soap:Header|wsa:To", true, "xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd") catch {};
    xml_to_sign.updateChildContent("soap:Header|wsa:To", "https://vpfe-hab.dian.gov.co/WcfDianCustomerServices.svc");
    xml_to_sign.updateChildContent("soap:Body|wcf:GetStatus|wcf:trackId", "123456666");

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

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

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

    // -------- Reference 1 --------
    gen.addSameDocRef("ID-F25839120CBA3ECDAD68754D0443A667636FDA68", "sha256", "EXCL_C14N", "soap wcf", "") catch {};

    // 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("wsu:Id", "STR-F25839120CBA3ECDAD68754D0443A667636FDA68") catch {};
    xml_custom_key_info.updateAttrAt("wsse:Reference", true, "URI", "#ABCXYZ-9F0F7E15A59816E680B4735080A789DC1EED6C9C") 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#X509v3") 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 X509 of the signing cert.
    const bd_cert = try chilkat.BinData.init();
    defer bd_cert.deinit();
    cert.exportCertDerBd(bd_cert) catch {};

    _ = sb_xml.replace("BinarySecurityToken_Base64Binary_Content", try bd_cert.getEncoded(alloc, "base64"));

    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", .{});

    // --------------------------------------------------------------------------------
    // Also see Chilkat's Online WSDL Code Generator
    // to generate code and SOAP Request and Response XML for each operation in a WSDL.
    // --------------------------------------------------------------------------------
}