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 Signed SOAP XML for DIAN Colombia WCF ServiceDemonstrates how to create a signed SOAP XML document for DIAN Colombia.
#include <C_CkXmlW.h> #include <C_CkXmlDSigGenW.h> #include <C_CkCertW.h> #include <C_CkStringBuilderW.h> #include <C_CkBinDataW.h> #include <C_CkXmlDSigW.h> void ChilkatSample(void) { BOOL success; HCkXmlW xmlToSign; HCkXmlDSigGenW gen; HCkCertW cert; HCkXmlW xmlCustomKeyInfo; HCkStringBuilderW sbXml; HCkBinDataW bdCert; int nReplaced; HCkXmlDSigW verifier; int numSigs; int verifyIdx; BOOL verified; // 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 success = TRUE; // Create the XML to be signed... xmlToSign = CkXmlW_Create(); CkXmlW_putTag(xmlToSign,L"soap:Envelope"); CkXmlW_AddAttribute(xmlToSign,L"xmlns:soap",L"http://www.w3.org/2003/05/soap-envelope"); CkXmlW_AddAttribute(xmlToSign,L"xmlns:wcf",L"http://wcf.dian.colombia"); CkXmlW_UpdateAttrAt(xmlToSign,L"soap:Header",TRUE,L"xmlns:wsa",L"http://www.w3.org/2005/08/addressing"); CkXmlW_UpdateAttrAt(xmlToSign,L"soap: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:Header|wsse:Security",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:Header|wsse:Security|wsu:Timestamp",TRUE,L"wsu:Id",L"TS-F25839120CBA3ECDAD68754D0443A667636FDA68"); CkXmlW_UpdateChildContent(xmlToSign,L"soap:Header|wsse:Security|wsu:Timestamp|wsu:Created",L"2019-08-23T23:03:01Z"); CkXmlW_UpdateChildContent(xmlToSign,L"soap:Header|wsse:Security|wsu:Timestamp|wsu:Expires",L"2019-08-24T15:43:01Z"); CkXmlW_UpdateAttrAt(xmlToSign,L"soap: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: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:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,L"wsu:Id",L"ABCXYZ-9F0F7E15A59816E680B4735080A789DC1EED6C9C"); CkXmlW_UpdateChildContent(xmlToSign,L"soap:Header|wsse:Security|wsse:BinarySecurityToken",L"BinarySecurityToken_Base64Binary_Content"); CkXmlW_UpdateChildContent(xmlToSign,L"soap:Header|wsa:Action",L"http://wcf.dian.colombia/IWcfDianCustomerServices/GetStatus"); CkXmlW_UpdateAttrAt(xmlToSign,L"soap:Header|wsa:To",TRUE,L"wsu:Id",L"ID-F25839120CBA3ECDAD68754D0443A667636FDA68"); CkXmlW_UpdateAttrAt(xmlToSign,L"soap:Header|wsa:To",TRUE,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"); CkXmlW_UpdateChildContent(xmlToSign,L"soap:Header|wsa:To",L"https://vpfe-hab.dian.gov.co/WcfDianCustomerServices.svc"); CkXmlW_UpdateChildContent(xmlToSign,L"soap:Body|wcf:GetStatus|wcf:trackId",L"123456666"); gen = CkXmlDSigGenW_Create(); CkXmlDSigGenW_putSigLocation(gen,L"soap:Envelope|soap:Header|wsse:Security"); CkXmlDSigGenW_putSigLocationMod(gen,0); CkXmlDSigGenW_putSigId(gen,L"SIG-F25839120CBA3ECDAD68754D0443A667636FDA68"); 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"KI-F25839120CBA3ECDAD68754D0443A667636FDA68"); // -------- Reference 1 -------- CkXmlDSigGenW_AddSameDocRef(gen,L"ID-F25839120CBA3ECDAD68754D0443A667636FDA68",L"sha256",L"EXCL_C14N",L"soap wcf",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)); 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"wsu:Id",L"STR-F25839120CBA3ECDAD68754D0443A667636FDA68"); CkXmlW_UpdateAttrAt(xmlCustomKeyInfo,L"wsse:Reference",TRUE,L"URI",L"#ABCXYZ-9F0F7E15A59816E680B4735080A789DC1EED6C9C"); 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. bdCert = CkBinDataW_Create(); CkCertW_ExportCertDerBd(cert,bdCert); nReplaced = CkStringBuilderW_Replace(sbXml,L"BinarySecurityToken_Base64Binary_Content",CkBinDataW_getEncoded(bdCert,L"base64")); CkXmlDSigGenW_putBehaviors(gen,L"IndentedSignature"); // Sign the XML... success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml); if (success != TRUE) { wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen)); CkXmlW_Dispose(xmlToSign); CkXmlDSigGenW_Dispose(gen); CkCertW_Dispose(cert); CkXmlW_Dispose(xmlCustomKeyInfo); CkStringBuilderW_Dispose(sbXml); CkBinDataW_Dispose(bdCert); return; } // ----------------------------------------------- // Save the signed XML to a file. success = CkStringBuilderW_WriteFile(sbXml,L"qa_output/signedXml.xml",L"utf-8",FALSE); wprintf(L"%s\n",CkStringBuilderW_getAsString(sbXml)); // ---------------------------------------- // Verify the signatures we just produced... verifier = CkXmlDSigW_Create(); success = CkXmlDSigW_LoadSignatureSb(verifier,sbXml); if (success != TRUE) { wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier)); CkXmlW_Dispose(xmlToSign); CkXmlDSigGenW_Dispose(gen); CkCertW_Dispose(cert); CkXmlW_Dispose(xmlCustomKeyInfo); CkStringBuilderW_Dispose(sbXml); CkBinDataW_Dispose(bdCert); CkXmlDSigW_Dispose(verifier); return; } numSigs = CkXmlDSigW_getNumSignatures(verifier); verifyIdx = 0; while (verifyIdx < numSigs) { CkXmlDSigW_putSelector(verifier,verifyIdx); verified = CkXmlDSigW_VerifySignature(verifier,TRUE); if (verified != TRUE) { wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier)); CkXmlW_Dispose(xmlToSign); CkXmlDSigGenW_Dispose(gen); CkCertW_Dispose(cert); CkXmlW_Dispose(xmlCustomKeyInfo); CkStringBuilderW_Dispose(sbXml); CkBinDataW_Dispose(bdCert); CkXmlDSigW_Dispose(verifier); return; } verifyIdx = verifyIdx + 1; } wprintf(L"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. // -------------------------------------------------------------------------------- CkXmlW_Dispose(xmlToSign); CkXmlDSigGenW_Dispose(gen); CkCertW_Dispose(cert); CkXmlW_Dispose(xmlCustomKeyInfo); CkStringBuilderW_Dispose(sbXml); CkBinDataW_Dispose(bdCert); CkXmlDSigW_Dispose(verifier); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.