Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Create IRS MeF Login Service Request MessageThis 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.
#include <C_CkPfxW.h> #include <C_CkCertW.h> #include <C_CkBinDataW.h> #include <C_CkStringBuilderW.h> #include <C_CkXmlW.h> #include <C_CkXmlDSigGenW.h> void ChilkatSample(void) { HCkPfxW pfx; BOOL success; HCkCertW signingCert; HCkBinDataW bdCert; HCkStringBuilderW sbCert64; HCkXmlW xmlToSign; HCkXmlDSigGenW gen; HCkCertW cert; HCkXmlW xmlCustomKeyInfo; HCkStringBuilderW sbXml; int nReplaced; // 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..) pfx = CkPfxW_Create(); success = CkPfxW_LoadPfxFile(pfx,L"qa_data/pfx/cert_test123.pfx",L"test123"); if (success != TRUE) { wprintf(L"%s\n",CkPfxW_lastErrorText(pfx)); CkPfxW_Dispose(pfx); 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. signingCert = CkPfxW_GetCert(pfx,0); if (CkPfxW_getLastMethodSuccess(pfx) != TRUE) { wprintf(L"%s\n",CkPfxW_lastErrorText(pfx)); CkPfxW_Dispose(pfx); return; } bdCert = CkBinDataW_Create(); CkCertW_ExportCertDerBd(signingCert,bdCert); sbCert64 = CkStringBuilderW_Create(); CkBinDataW_GetEncodedSb(bdCert,L"base64",sbCert64); // ------------------------------------------------------------------------------------------- // 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... xmlToSign = CkXmlW_Create(); CkXmlW_putTag(xmlToSign,L"SOAP-ENV:Envelope"); CkXmlW_AddAttribute(xmlToSign,L"xmlns:SOAP-ENV",L"http://schemas.xmlsoap.org/soap/envelope/"); CkXmlW_AddAttribute(xmlToSign,L"xmlns:xsd",L"http://www.w3.org/2001/XMLSchema"); CkXmlW_AddAttribute(xmlToSign,L"xmlns:xsi",L"http://www.w3.org/2001/XMLSchema-instance"); CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Header|wsse:Security",TRUE,L"xmlns:wsse",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"); CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,L"EncodingType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"); CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,L"ValueType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"); CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,L"wsu:Id",L"CertId-1673181727"); CkXmlW_UpdateChildContent(xmlToSign,L"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",CkStringBuilderW_getAsString(sbCert64)); CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Header|ns1:MeFHeader",TRUE,L"xmlns:ns1",L"http://www.irs.gov/a2a/mef/MeFHeader.xsd"); CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Header|ns1:MeFHeader",TRUE,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"); CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Header|ns1:MeFHeader",TRUE,L"wsu:Id",L"id-1214941501"); CkXmlW_UpdateChildContent(xmlToSign,L"SOAP-ENV:Header|ns1:MeFHeader|ns1:MessageID",L"12345202018200000234"); CkXmlW_UpdateChildContent(xmlToSign,L"SOAP-ENV:Header|ns1:MeFHeader|ns1:Action",L"Login"); CkXmlW_UpdateChildContent(xmlToSign,L"SOAP-ENV:Header|ns1:MeFHeader|ns1:MessageTs",L"2020-06-30T15:25:42.678Z"); CkXmlW_UpdateChildContentInt(xmlToSign,L"SOAP-ENV:Header|ns1:MeFHeader|ns1:ETIN",12345); CkXmlW_UpdateChildContent(xmlToSign,L"SOAP-ENV:Header|ns1:MeFHeader|ns1:SessionKeyCd",L"Y"); CkXmlW_UpdateChildContent(xmlToSign,L"SOAP-ENV:Header|ns1:MeFHeader|ns1:TestCd",L"P"); CkXmlW_UpdateChildContentInt(xmlToSign,L"SOAP-ENV:Header|ns1:MeFHeader|ns1:AppSysID",65432190); CkXmlW_UpdateChildContent(xmlToSign,L"SOAP-ENV:Header|ns1:MeFHeader|ns1:WSDLVersionNum",L"10.3"); CkXmlW_UpdateChildContent(xmlToSign,L"SOAP-ENV:Header|ns1:MeFHeader|ns1:ClientSoftwareTxt",L"SOATest"); CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body",TRUE,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"); CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body",TRUE,L"wsu:Id",L"id-1871558655"); CkXmlW_UpdateAttrAt(xmlToSign,L"SOAP-ENV:Body|LoginRequest",TRUE,L"xmlns",L"http://www.irs.gov/a2a/mef/MeFMSIServices.xsd"); // ------------------------------------------------------------------------------------------- // Setup the XML DSig generator object to create the desired signature. gen = CkXmlDSigGenW_Create(); CkXmlDSigGenW_putSigLocation(gen,L"SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security"); CkXmlDSigGenW_putSigLocationMod(gen,0); CkXmlDSigGenW_putSigNamespacePrefix(gen,L"ds"); CkXmlDSigGenW_putSigNamespaceUri(gen,L"http://www.w3.org/2000/09/xmldsig#"); CkXmlDSigGenW_putSignedInfoCanonAlg(gen,L"EXCL_C14N"); CkXmlDSigGenW_putSignedInfoDigestMethod(gen,L"sha256"); // Set the KeyInfoId before adding references.. CkXmlDSigGenW_putKeyInfoId(gen,L"KeyId-256137097"); // -------- Reference 1 -------- CkXmlDSigGenW_AddSameDocRef(gen,L"id-1214941501",L"sha256",L"EXCL_C14N",L"",L""); // -------- Reference 2 -------- CkXmlDSigGenW_AddSameDocRef(gen,L"id-1871558655",L"sha256",L"EXCL_C14N",L"",L""); // Provide a certificate + private key. (PFX password is test123) cert = CkCertW_Create(); success = CkCertW_LoadPfxFile(cert,L"qa_data/pfx/cert_test123.pfx",L"test123"); if (success != TRUE) { wprintf(L"%s\n",CkCertW_lastErrorText(cert)); CkPfxW_Dispose(pfx); CkBinDataW_Dispose(bdCert); CkStringBuilderW_Dispose(sbCert64); CkXmlW_Dispose(xmlToSign); CkXmlDSigGenW_Dispose(gen); CkCertW_Dispose(cert); return; } CkXmlDSigGenW_SetX509Cert(gen,cert,TRUE); CkXmlDSigGenW_putKeyInfoType(gen,L"Custom"); // Create the custom KeyInfo XML.. xmlCustomKeyInfo = CkXmlW_Create(); CkXmlW_putTag(xmlCustomKeyInfo,L"wsse:SecurityTokenReference"); CkXmlW_AddAttribute(xmlCustomKeyInfo,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"); CkXmlW_AddAttribute(xmlCustomKeyInfo,L"wsu:Id",L"STRId-1862925355"); CkXmlW_UpdateAttrAt(xmlCustomKeyInfo,L"wsse:Reference",TRUE,L"URI",L"#CertId-1673181727"); CkXmlW_UpdateAttrAt(xmlCustomKeyInfo,L"wsse:Reference",TRUE,L"ValueType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"); CkXmlW_putEmitXmlDecl(xmlCustomKeyInfo,FALSE); CkXmlDSigGenW_putCustomKeyInfoXml(gen,CkXmlW_getXml(xmlCustomKeyInfo)); // ------------------------------------------------------------------------------------------- // Load XML to be signed... sbXml = CkStringBuilderW_Create(); CkXmlW_GetXmlSb(xmlToSign,sbXml); // Update BinarySecurityToken_Base64Binary_Content with the actual X509 of the signing cert. nReplaced = CkStringBuilderW_Replace(sbXml,L"BinarySecurityToken_Base64Binary_Content",CkCertW_getEncoded(cert)); CkXmlDSigGenW_putBehaviors(gen,L"IndentedSignature"); // Sign the XML... success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml); if (success != TRUE) { wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen)); CkPfxW_Dispose(pfx); CkBinDataW_Dispose(bdCert); CkStringBuilderW_Dispose(sbCert64); CkXmlW_Dispose(xmlToSign); CkXmlDSigGenW_Dispose(gen); CkCertW_Dispose(cert); CkXmlW_Dispose(xmlCustomKeyInfo); CkStringBuilderW_Dispose(sbXml); return; } // ----------------------------------------------- // Save the signed XML to a file. success = CkStringBuilderW_WriteFile(sbXml,L"c:/temp/qa_output/signedXml.xml",L"utf-8",FALSE); wprintf(L"%s\n",CkStringBuilderW_getAsString(sbXml)); CkPfxW_Dispose(pfx); CkBinDataW_Dispose(bdCert); CkStringBuilderW_Dispose(sbCert64); CkXmlW_Dispose(xmlToSign); CkXmlDSigGenW_Dispose(gen); CkCertW_Dispose(cert); CkXmlW_Dispose(xmlCustomKeyInfo); CkStringBuilderW_Dispose(sbXml); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.