Sample code for 30+ languages & platforms
Zig

Create IRS MeF Login Service Request Message

See more XML Digital Signatures Examples

This example demonstrates how to create a digitally signed Login Service Request Message.

This example used the documentation at https://www.irs.gov/pub/irs-utl/mef-doc-stp_ref_guide.pdf as a guide.

It creates signed XML as specified in section 4.1.1 found in Section 4: Example A2A Web Service Messages.

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.

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

    // The goal of this example is to create signed SOAP XML such as the following:

    // <?xml version="1.0" encoding="UTF-8"?>
    // <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    //     <SOAP-ENV:Header>
    //         <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    //             <wsse:BinarySecurityToken
    //         xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
    //         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="CertId-1673181727">MIIHab...n71P</wsse:BinarySecurityToken>
    //             <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#"/>
    //                     <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
    //                     <ds:Reference URI="#id-1214941501">
    //                         <ds:Transforms>
    //                             <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    //                         </ds:Transforms>
    //                         <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    //                         <ds:DigestValue>abcdefghijkLMNOPQRSTUVwxyz012345Em0o3VEOTck=</ds:DigestValue>
    //                     </ds:Reference>
    //                     <ds:Reference URI="#id-1871558655">
    //                         <ds:Transforms>
    //                             <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    //                         </ds:Transforms>
    //                         <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    //                         <ds:DigestValue>abcdefghijkLMNOPQRSTUVwxyz012345Xm5ViA+royg=</ds:DigestValue>
    //                     </ds:Reference>
    //                 </ds:SignedInfo>
    //                 <ds:SignatureValue>abcd...5Nbw==</ds:SignatureValue>
    //                 <ds:KeyInfo Id="KeyId-256137097">
    //                     <wsse:SecurityTokenReference xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="STRId-1862925355">
    //                         <wsse:Reference URI="#CertId-1673181727" 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>
    //         <ns1:MeFHeader xmlns:ns1="http://www.irs.gov/a2a/mef/MeFHeader.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-1214941501">
    //             <ns1:MessageID>12345202018200000234</ns1:MessageID>
    //             <ns1:Action>Login</ns1:Action>
    //             <ns1:MessageTs>2020-06-30T15:25:42.678Z</ns1:MessageTs>
    //             <ns1:ETIN>12345</ns1:ETIN>
    //             <ns1:SessionKeyCd>Y</ns1:SessionKeyCd>
    //             <ns1:TestCd>P</ns1:TestCd>
    //             <ns1:AppSysID>65432190</ns1:AppSysID>
    //             <ns1:WSDLVersionNum>10.3</ns1:WSDLVersionNum>
    //             <ns1:ClientSoftwareTxt>SOATest</ns1:ClientSoftwareTxt>
    //         </ns1:MeFHeader>
    //     </SOAP-ENV:Header>
    //     <SOAP-ENV:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-1871558655">
    //         <LoginRequest xmlns="http://www.irs.gov/a2a/mef/MeFMSIServices.xsd"/>
    //     </SOAP-ENV:Body>
    // </SOAP-ENV:Envelope>

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

    // First, let's load the certificate + private key to be used for signing (from a PFX).
    // (It is also possible to use certificates installed on a Windows system, or from other file formats..)
    const pfx = try chilkat.Pfx.init();
    defer pfx.deinit();
    pfx.loadPfxFile("qa_data/pfx/cert_test123.pfx", "test123") catch {
        std.debug.print("{s}\n", .{try pfx.getLastErrorText(alloc)});
        return;
    };

    // We'll be needing the X.509 signing cert as base64 for the BinarySecurityToken, so let's get it now..
    // The certificate having the private key should be the 1st in the PFX.

    const signing_cert = try chilkat.Cert.init();
    defer signing_cert.deinit();
    pfx.certAt(0, signing_cert) catch {
        std.debug.print("{s}\n", .{try pfx.getLastErrorText(alloc)});
        return;
    };

    const bd_cert = try chilkat.BinData.init();
    defer bd_cert.deinit();
    signing_cert.exportCertDerBd(bd_cert) catch {};
    const sb_cert64 = try chilkat.StringBuilder.init();
    defer sb_cert64.deinit();
    bd_cert.getEncodedSb("base64", sb_cert64) catch {};

    // -------------------------------------------------------------------------------------------
    // The XML before signing would look like this:

    // <?xml version="1.0" encoding="UTF-8"?>
    // <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    //     <SOAP-ENV:Header>
    //         <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    //             <wsse:BinarySecurityToken
    //         xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
    //         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="CertId-1673181727">MIIHab...n71P</wsse:BinarySecurityToken>
    //         </wsse:Security>
    //         <ns1:MeFHeader xmlns:ns1="http://www.irs.gov/a2a/mef/MeFHeader.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-1214941501">
    //             <ns1:MessageID>12345202018200000234</ns1:MessageID>
    //             <ns1:Action>Login</ns1:Action>
    //             <ns1:MessageTs>2020-06-30T15:25:42.678Z</ns1:MessageTs>
    //             <ns1:ETIN>12345</ns1:ETIN>
    //             <ns1:SessionKeyCd>Y</ns1:SessionKeyCd>
    //             <ns1:TestCd>P</ns1:TestCd>
    //             <ns1:AppSysID>65432190</ns1:AppSysID>
    //             <ns1:WSDLVersionNum>10.3</ns1:WSDLVersionNum>
    //             <ns1:ClientSoftwareTxt>SOATest</ns1:ClientSoftwareTxt>
    //         </ns1:MeFHeader>
    //     </SOAP-ENV:Header>
    //     <SOAP-ENV:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-1871558655">
    //         <LoginRequest xmlns="http://www.irs.gov/a2a/mef/MeFMSIServices.xsd"/>
    //     </SOAP-ENV:Body>
    // </SOAP-ENV:Envelope>

    // You can use the online XML code generation tool at http://tools.chilkat.io/xmlCreate.cshtml
    // to generate the following XML creation source code:

    // Create the XML to be signed...
    const xml_to_sign = try chilkat.Xml.init();
    defer xml_to_sign.deinit();
    xml_to_sign.setTag("SOAP-ENV:Envelope");
    xml_to_sign.addAttribute("xmlns:SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/") catch {};
    xml_to_sign.addAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema") catch {};
    xml_to_sign.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV: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-ENV:Header|wsse:Security|wsse:BinarySecurityToken", 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-ENV: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-ENV: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-ENV:Header|wsse:Security|wsse:BinarySecurityToken", true, "wsu:Id", "CertId-1673181727") catch {};
    xml_to_sign.updateChildContent("SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken", try sb_cert64.getAsString(alloc));
    xml_to_sign.updateAttrAt("SOAP-ENV:Header|ns1:MeFHeader", true, "xmlns:ns1", "http://www.irs.gov/a2a/mef/MeFHeader.xsd") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Header|ns1:MeFHeader", 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-ENV:Header|ns1:MeFHeader", true, "wsu:Id", "id-1214941501") catch {};
    xml_to_sign.updateChildContent("SOAP-ENV:Header|ns1:MeFHeader|ns1:MessageID", "12345202018200000234");
    xml_to_sign.updateChildContent("SOAP-ENV:Header|ns1:MeFHeader|ns1:Action", "Login");
    xml_to_sign.updateChildContent("SOAP-ENV:Header|ns1:MeFHeader|ns1:MessageTs", "2020-06-30T15:25:42.678Z");
    xml_to_sign.updateChildContentInt("SOAP-ENV:Header|ns1:MeFHeader|ns1:ETIN", 12345);
    xml_to_sign.updateChildContent("SOAP-ENV:Header|ns1:MeFHeader|ns1:SessionKeyCd", "Y");
    xml_to_sign.updateChildContent("SOAP-ENV:Header|ns1:MeFHeader|ns1:TestCd", "P");
    xml_to_sign.updateChildContentInt("SOAP-ENV:Header|ns1:MeFHeader|ns1:AppSysID", 65432190);
    xml_to_sign.updateChildContent("SOAP-ENV:Header|ns1:MeFHeader|ns1:WSDLVersionNum", "10.3");
    xml_to_sign.updateChildContent("SOAP-ENV:Header|ns1:MeFHeader|ns1:ClientSoftwareTxt", "SOATest");
    xml_to_sign.updateAttrAt("SOAP-ENV: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("SOAP-ENV:Body", true, "wsu:Id", "id-1871558655") catch {};
    xml_to_sign.updateAttrAt("SOAP-ENV:Body|LoginRequest", true, "xmlns", "http://www.irs.gov/a2a/mef/MeFMSIServices.xsd") catch {};

    // -------------------------------------------------------------------------------------------
    // Setup the XML DSig generator object to create the desired signature.

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

    gen.setSigLocation("SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security");
    gen.setSigLocationMod(0);
    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("KeyId-256137097");

    // -------- Reference 1 --------
    gen.addSameDocRef("id-1214941501", "sha256", "EXCL_C14N", "", "") catch {};

    // -------- Reference 2 --------
    gen.addSameDocRef("id-1871558655", "sha256", "EXCL_C14N", "", "") 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("xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd") catch {};
    xml_custom_key_info.addAttribute("wsu:Id", "STRId-1862925355") catch {};
    xml_custom_key_info.updateAttrAt("wsse:Reference", true, "URI", "#CertId-1673181727") 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.
    _ = sb_xml.replace("BinarySecurityToken_Base64Binary_Content", try cert.getEncoded(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("c:/temp/qa_output/signedXml.xml", "utf-8", false);

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