Sample code for 30+ languages & platforms
Zig

Sign with BinarySecurityToken

See more XML Digital Signatures Examples

Demonstrates creating an XML signature using a BinarySecurityToken.

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.

    // -------------------------------------------------------------------------
    // Load a cert + private key from a PFX.
    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;
    };

    // -------------------------------------------------------------------------
    // Create the XML that is to be signed.
    //
    // The XML we're creating can be found at Sample Pre-Signed XML with BinarySecurityToken
    // The online tool at http://tools.chilkat.io/xmlCreate.cshtml can be used to generate the following XML creation code.
    //

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.setTag("S:Envelope");
    xml.addAttribute("xmlns:S", "http://www.w3.org/2003/05/soap-envelope") catch {};
    xml.addAttribute("xmlns:wsse11", "http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd") catch {};
    xml.addAttribute("xmlns:wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd") catch {};
    xml.addAttribute("xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd") catch {};
    xml.addAttribute("xmlns:xs", "http://www.w3.org/2001/XMLSchema") catch {};
    xml.addAttribute("xmlns:ds", "http://www.w3.org/2000/09/xmldsig#") catch {};
    xml.addAttribute("xmlns:exc14n", "http://www.w3.org/2001/10/xml-exc-c14n#") catch {};
    xml.updateAttrAt("S:Header|To", true, "xmlns", "http://www.w3.org/2005/08/addressing") catch {};
    xml.updateAttrAt("S:Header|To", true, "wsu:Id", "_5002") catch {};
    xml.updateChildContent("S:Header|To", "https://XXXXXXXXX");
    xml.updateAttrAt("S:Header|Action", true, "xmlns", "http://www.w3.org/2005/08/addressing") catch {};
    xml.updateAttrAt("S:Header|Action", true, "xmlns:S", "http://www.w3.org/2003/05/soap-envelope") catch {};
    xml.updateAttrAt("S:Header|Action", true, "S:mustUnderstand", "true") catch {};
    xml.updateChildContent("S:Header|Action", "http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue");
    xml.updateAttrAt("S:Header|ReplyTo", true, "xmlns", "http://www.w3.org/2005/08/addressing") catch {};
    xml.updateChildContent("S:Header|ReplyTo|Address", "http://www.w3.org/2005/08/addressing/anonymous");
    xml.updateAttrAt("S:Header|FaultTo", true, "xmlns", "http://www.w3.org/2005/08/addressing") catch {};
    xml.updateChildContent("S:Header|FaultTo|Address", "http://www.w3.org/2005/08/addressing/anonymous");
    xml.updateAttrAt("S:Header|MessageID", true, "xmlns", "http://www.w3.org/2005/08/addressing") catch {};
    xml.updateChildContent("S:Header|MessageID", "uuid:e9033251-4ff0-4618-8baf-4952ab5fd207");
    xml.updateAttrAt("S:Header|wsse:Security", true, "S:mustUnderstand", "true") catch {};
    xml.updateAttrAt("S:Header|wsse:Security|wsu:Timestamp", true, "xmlns:ns17", "http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512") catch {};
    xml.updateAttrAt("S:Header|wsse:Security|wsu:Timestamp", true, "xmlns:ns16", "http://schemas.xmlsoap.org/soap/envelope/") catch {};
    xml.updateAttrAt("S:Header|wsse:Security|wsu:Timestamp", true, "wsu:Id", "_1") catch {};

    // Get the current date/time in timestamp format, such as "2018-05-23T02:38:27Z"
    const dt = try chilkat.DateTime.init();
    defer dt.deinit();
    dt.setFromCurrentSystemTime() catch {};
    xml.updateChildContent("S:Header|wsse:Security|wsu:Timestamp|wsu:Created", try dt.getAsTimestamp(alloc, false));
    // Add 5 minutes.
    dt.addSeconds(300) catch {};
    xml.updateChildContent("S:Header|wsse:Security|wsu:Timestamp|wsu:Expires", try dt.getAsTimestamp(alloc, false));

    xml.updateAttrAt("S:Header|wsse:Security|wsse:BinarySecurityToken", true, "xmlns:ns17", "http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512") catch {};
    xml.updateAttrAt("S:Header|wsse:Security|wsse:BinarySecurityToken", true, "xmlns:ns16", "http://schemas.xmlsoap.org/soap/envelope/") catch {};
    xml.updateAttrAt("S: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.updateAttrAt("S: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.updateAttrAt("S:Header|wsse:Security|wsse:BinarySecurityToken", true, "wsu:Id", "uuid_43470044-78b4-4b23-926a-b7f590d24cb8") catch {};

    const bd_cert = try chilkat.BinData.init();
    defer bd_cert.deinit();
    cert.exportCertDerBd(bd_cert) catch {};

    // Get the cert as base64 on one line.
    xml.updateChildContent("S:Header|wsse:Security|wsse:BinarySecurityToken", try bd_cert.getEncoded(alloc, "base64"));
    xml.updateAttrAt("S:Body|RequestSecurityToken", true, "xmlns", "http://docs.oasis-open.org/ws-sx/ws-trust/200512") catch {};
    xml.updateChildContent("S:Body|RequestSecurityToken|RequestType", "http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue");
    xml.updateAttrAt("S:Body|RequestSecurityToken|wsp:AppliesTo", true, "xmlns:wsp", "http://schemas.xmlsoap.org/ws/2004/09/policy") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|wsp:AppliesTo|EndpointReference:EndpointReference", true, "xmlns:EndpointReference", "http://www.w3.org/2005/08/addressing") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|wsp:AppliesTo|EndpointReference:EndpointReference", true, "xmlns", "http://www.w3.org/2005/08/addressing") catch {};
    xml.updateChildContent("S:Body|RequestSecurityToken|wsp:AppliesTo|EndpointReference:EndpointReference|Address", "https://XXXXXXXXX/services");
    xml.updateChildContent("S:Body|RequestSecurityToken|TokenType", "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0");
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims", true, "xmlns:i", "http://schemas.xmlsoap.org/ws/2005/05/identity") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims", true, "Dialect", "http://schemas.xmlsoap.org/ws/2005/05/identity") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType", true, "Optional", "false") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType", true, "Uri", "http://XXXXXXXXX/2008/06/identity/claims/abn") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[1]", true, "Optional", "false") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[1]", true, "Uri", "http://XXXXXXXXX/2008/06/identity/claims/commonname") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[2]", true, "Optional", "false") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[2]", true, "Uri", "http://XXXXXXXXX/2008/06/identity/claims/credentialtype") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[3]", true, "Optional", "false") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[3]", true, "Uri", "http://XXXXXXXXX/2008/06/identity/claims/samlsubjectid") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[4]", true, "Optional", "false") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[4]", true, "Uri", "http://XXXXXXXXX/2008/06/identity/claims/fingerprint") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[5]", true, "Optional", "true") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[5]", true, "Uri", "http://XXXXXXXXX/2008/06/identity/claims/sbr_personid") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[6]", true, "Optional", "true") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[6]", true, "Uri", "http://XXXXXXXXX/2008/06/identity/claims/givennames") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[7]", true, "Optional", "true") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[7]", true, "Uri", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[8]", true, "Optional", "true") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[8]", true, "Uri", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[9]", true, "Optional", "true") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[9]", true, "Uri", "http://XXXXXXXXX/2008/06/identity/claims/credentialadministrator") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[10]", true, "Optional", "true") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[10]", true, "Uri", "http://XXXXXXXXX/2008/06/identity/claims/stalecrlminutes") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[11]", true, "Optional", "true") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[11]", true, "Uri", "http://XXXXXXXXX/2008/06/identity/claims/subjectdn") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[12]", true, "Optional", "true") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[12]", true, "Uri", "http://XXXXXXXXX/2008/06/identity/claims/issuerdn") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[13]", true, "Optional", "true") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[13]", true, "Uri", "http://XXXXXXXXX/2008/06/identity/claims/notafterdate") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[14]", true, "Optional", "true") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[14]", true, "Uri", "http://XXXXXXXXX/2008/06/identity/claims/certificateserialnumber") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[15]", true, "Optional", "true") catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Claims|i:ClaimType[15]", true, "Uri", "http://XXXXXXXXX/2008/06/identity/claims/previoussubject") catch {};

    dt.setFromCurrentSystemTime() catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Lifetime|wsu:Created", true, "xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd") catch {};
    xml.updateChildContent("S:Body|RequestSecurityToken|Lifetime|wsu:Created", try dt.getAsTimestamp(alloc, false));

    // Add 40 minutes.
    dt.addSeconds(2400) catch {};
    xml.updateAttrAt("S:Body|RequestSecurityToken|Lifetime|wsu:Expires", true, "xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd") catch {};
    xml.updateChildContent("S:Body|RequestSecurityToken|Lifetime|wsu:Expires", try dt.getAsTimestamp(alloc, false));

    xml.updateChildContent("S:Body|RequestSecurityToken|KeyType", "http://docs.oasis-open.org/ws-sx/ws-trust/200512/SymmetricKey");
    xml.updateChildContent("S:Body|RequestSecurityToken|KeySize", "512");

    // Examine the pre-signed XML
    // println xml.GetXml();

    // Build the custom KeyInfo XML we'll use:
    //
    //         <wsse:SecurityTokenReference>
    //             <wsse:Reference URI="#uuid_43470044-78b4-4b23-926a-b7f590d24cb8" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" /></wsse:SecurityTokenReference>

    const key_info_xml = try chilkat.Xml.init();
    defer key_info_xml.deinit();
    key_info_xml.setTag("wsse:SecurityTokenReference");
    key_info_xml.updateAttrAt("wsse:Reference", true, "URI", "#uuid_43470044-78b4-4b23-926a-b7f590d24cb8") catch {};
    key_info_xml.updateAttrAt("wsse:Reference", true, "ValueType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3") catch {};

    // -------------------------------------------------------------------------
    // Setup the XML Digital Signature Generator and add the XML Signature.
    //
    const gen = try chilkat.XmlDSigGen.init();
    defer gen.deinit();
    gen.setSigLocation("S:Envelope|S:Header|wsse:Security");
    gen.setSignedInfoPrefixList("wsse S");

    gen.addSameDocRef("_1", "sha1", "EXCL_C14N", "wsu wsse S", "") catch {};
    gen.addSameDocRef("_5002", "sha1", "EXCL_C14N", "S", "") catch {};

    gen.setKeyInfoType("Custom");
    key_info_xml.setEmitXmlDecl(false);
    gen.setCustomKeyInfoXml(try key_info_xml.getXml(alloc));

    // Specify the cert for signing (which has a private key because it was loaded from a PFX)
    gen.setX509Cert(cert, true) catch {};

    // Indicated we want an indented signature for easier human reading.
    gen.setBehaviors("IndentedSignature");

    // Sign the XML..
    const sb_xml = try chilkat.StringBuilder.init();
    defer sb_xml.deinit();
    xml.getXmlSb(sb_xml) catch {};

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

    // Examine the signed XML
    std.debug.print("{s}\n", .{try sb_xml.getAsString(alloc)});

    // The resulting signature (extracted from the surrounding XML) looks something like this:

    // <ds:Signature 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 xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="wsse S" />
    //         </ds:CanonicalizationMethod>
    //         <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
    //         <ds:Reference URI="#_1">
    //             <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="wsu wsse S" />
    //                 </ds:Transform>
    //             </ds:Transforms>
    //             <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
    //             <ds:DigestValue>VAJMC/L/BDvml7Qv5CBMePbKDE8=</ds:DigestValue>
    //         </ds:Reference>
    //         <ds:Reference URI="#_5002">
    //             <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="S" />
    //                 </ds:Transform>
    //             </ds:Transforms>
    //             <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
    //             <ds:DigestValue>sW/QFsk6kGv1dzeu0H9Qc/2kvAQ=</ds:DigestValue>
    //         </ds:Reference>
    //     </ds:SignedInfo>
    //     <ds:SignatureValue>....</ds:SignatureValue>
    //     <ds:KeyInfo>
    //         <wsse:SecurityTokenReference>
    //             <wsse:Reference URI="#uuid_43470044-78b4-4b23-926a-b7f590d24cb8" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" />
    //         </wsse:SecurityTokenReference>
    //     </ds:KeyInfo>
    // </ds:Signature>
    //
}