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
(DataFlex) Create Signed SOAP XML for DIAN Colombia WCF ServiceDemonstrates how to create a signed SOAP XML document for DIAN Colombia.
Use ChilkatAx-win32.pkg Procedure Test Boolean iSuccess Handle hoXmlToSign Handle hoGen Variant vCert Handle hoCert Handle hoXmlCustomKeyInfo Variant vSbXml Handle hoSbXml Variant vBdCert Handle hoBdCert Integer iNReplaced Handle hoVerifier Integer iNumSigs Integer iVerifyIdx Boolean iVerified String sTemp1 // 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 Move True To iSuccess // Create the XML to be signed... Get Create (RefClass(cComChilkatXml)) To hoXmlToSign If (Not(IsComObjectCreated(hoXmlToSign))) Begin Send CreateComObject of hoXmlToSign End Set ComTag Of hoXmlToSign To "soap:Envelope" Get ComAddAttribute Of hoXmlToSign "xmlns:soap" "http://www.w3.org/2003/05/soap-envelope" To iSuccess Get ComAddAttribute Of hoXmlToSign "xmlns:wcf" "http://wcf.dian.colombia" To iSuccess Get ComUpdateAttrAt Of hoXmlToSign "soap:Header" True "xmlns:wsa" "http://www.w3.org/2005/08/addressing" To iSuccess Get ComUpdateAttrAt Of hoXmlToSign "soap:Header|wsse:Security" True "xmlns:wsse" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" To iSuccess Get ComUpdateAttrAt Of hoXmlToSign "soap:Header|wsse:Security" True "xmlns:wsu" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" To iSuccess Get ComUpdateAttrAt Of hoXmlToSign "soap:Header|wsse:Security|wsu:Timestamp" True "wsu:Id" "TS-F25839120CBA3ECDAD68754D0443A667636FDA68" To iSuccess Send ComUpdateChildContent To hoXmlToSign "soap:Header|wsse:Security|wsu:Timestamp|wsu:Created" "2019-08-23T23:03:01Z" Send ComUpdateChildContent To hoXmlToSign "soap:Header|wsse:Security|wsu:Timestamp|wsu:Expires" "2019-08-24T15:43:01Z" Get ComUpdateAttrAt Of hoXmlToSign "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" To iSuccess Get ComUpdateAttrAt Of hoXmlToSign "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" To iSuccess Get ComUpdateAttrAt Of hoXmlToSign "soap:Header|wsse:Security|wsse:BinarySecurityToken" True "wsu:Id" "ABCXYZ-9F0F7E15A59816E680B4735080A789DC1EED6C9C" To iSuccess Send ComUpdateChildContent To hoXmlToSign "soap:Header|wsse:Security|wsse:BinarySecurityToken" "BinarySecurityToken_Base64Binary_Content" Send ComUpdateChildContent To hoXmlToSign "soap:Header|wsa:Action" "http://wcf.dian.colombia/IWcfDianCustomerServices/GetStatus" Get ComUpdateAttrAt Of hoXmlToSign "soap:Header|wsa:To" True "wsu:Id" "ID-F25839120CBA3ECDAD68754D0443A667636FDA68" To iSuccess Get ComUpdateAttrAt Of hoXmlToSign "soap:Header|wsa:To" True "xmlns:wsu" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" To iSuccess Send ComUpdateChildContent To hoXmlToSign "soap:Header|wsa:To" "https://vpfe-hab.dian.gov.co/WcfDianCustomerServices.svc" Send ComUpdateChildContent To hoXmlToSign "soap:Body|wcf:GetStatus|wcf:trackId" "123456666" Get Create (RefClass(cComChilkatXmlDSigGen)) To hoGen If (Not(IsComObjectCreated(hoGen))) Begin Send CreateComObject of hoGen End Set ComSigLocation Of hoGen To "soap:Envelope|soap:Header|wsse:Security" Set ComSigLocationMod Of hoGen To 0 Set ComSigId Of hoGen To "SIG-F25839120CBA3ECDAD68754D0443A667636FDA68" Set ComSigNamespacePrefix Of hoGen To "ds" Set ComSigNamespaceUri Of hoGen To "http://www.w3.org/2000/09/xmldsig#" Set ComSignedInfoCanonAlg Of hoGen To "EXCL_C14N" Set ComSignedInfoDigestMethod Of hoGen To "sha256" // Set the KeyInfoId before adding references.. Set ComKeyInfoId Of hoGen To "KI-F25839120CBA3ECDAD68754D0443A667636FDA68" // -------- Reference 1 -------- Get ComAddSameDocRef Of hoGen "ID-F25839120CBA3ECDAD68754D0443A667636FDA68" "sha256" "EXCL_C14N" "soap wcf" "" To iSuccess // Provide a certificate + private key. (PFX password is test123) Get Create (RefClass(cComChilkatCert)) To hoCert If (Not(IsComObjectCreated(hoCert))) Begin Send CreateComObject of hoCert End Get ComLoadPfxFile Of hoCert "qa_data/pfx/cert_test123.pfx" "test123" To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoCert To sTemp1 Showln sTemp1 Procedure_Return End Get pvComObject of hoCert to vCert Get ComSetX509Cert Of hoGen vCert True To iSuccess Set ComKeyInfoType Of hoGen To "Custom" // Create the custom KeyInfo XML.. Get Create (RefClass(cComChilkatXml)) To hoXmlCustomKeyInfo If (Not(IsComObjectCreated(hoXmlCustomKeyInfo))) Begin Send CreateComObject of hoXmlCustomKeyInfo End Set ComTag Of hoXmlCustomKeyInfo To "wsse:SecurityTokenReference" Get ComAddAttribute Of hoXmlCustomKeyInfo "wsu:Id" "STR-F25839120CBA3ECDAD68754D0443A667636FDA68" To iSuccess Get ComUpdateAttrAt Of hoXmlCustomKeyInfo "wsse:Reference" True "URI" "#ABCXYZ-9F0F7E15A59816E680B4735080A789DC1EED6C9C" To iSuccess Get ComUpdateAttrAt Of hoXmlCustomKeyInfo "wsse:Reference" True "ValueType" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" To iSuccess Set ComEmitXmlDecl Of hoXmlCustomKeyInfo To False Get ComGetXml Of hoXmlCustomKeyInfo To sTemp1 Set ComCustomKeyInfoXml Of hoGen To sTemp1 // Load XML to be signed... Get Create (RefClass(cComChilkatStringBuilder)) To hoSbXml If (Not(IsComObjectCreated(hoSbXml))) Begin Send CreateComObject of hoSbXml End Get pvComObject of hoSbXml to vSbXml Get ComGetXmlSb Of hoXmlToSign vSbXml To iSuccess // Update BinarySecurityToken_Base64Binary_Content with the actual X509 of the signing cert. Get Create (RefClass(cComChilkatBinData)) To hoBdCert If (Not(IsComObjectCreated(hoBdCert))) Begin Send CreateComObject of hoBdCert End Get pvComObject of hoBdCert to vBdCert Get ComExportCertDerBd Of hoCert vBdCert To iSuccess Get ComGetEncoded Of hoBdCert "base64" To sTemp1 Get ComReplace Of hoSbXml "BinarySecurityToken_Base64Binary_Content" sTemp1 To iNReplaced Set ComBehaviors Of hoGen To "IndentedSignature" // Sign the XML... Get pvComObject of hoSbXml to vSbXml Get ComCreateXmlDSigSb Of hoGen vSbXml To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoGen To sTemp1 Showln sTemp1 Procedure_Return End // ----------------------------------------------- // Save the signed XML to a file. Get ComWriteFile Of hoSbXml "qa_output/signedXml.xml" "utf-8" False To iSuccess Get ComGetAsString Of hoSbXml To sTemp1 Showln sTemp1 // ---------------------------------------- // Verify the signatures we just produced... Get Create (RefClass(cComChilkatXmlDSig)) To hoVerifier If (Not(IsComObjectCreated(hoVerifier))) Begin Send CreateComObject of hoVerifier End Get pvComObject of hoSbXml to vSbXml Get ComLoadSignatureSb Of hoVerifier vSbXml To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoVerifier To sTemp1 Showln sTemp1 Procedure_Return End Get ComNumSignatures Of hoVerifier To iNumSigs Move 0 To iVerifyIdx While (iVerifyIdx < iNumSigs) Set ComSelector Of hoVerifier To iVerifyIdx Get ComVerifySignature Of hoVerifier True To iVerified If (iVerified <> True) Begin Get ComLastErrorText Of hoVerifier To sTemp1 Showln sTemp1 Procedure_Return End Move (iVerifyIdx + 1) To iVerifyIdx Loop Showln "All signatures were successfully verified." // -------------------------------------------------------------------------------- // Also see Chilkat's Online WSDL Code Generator // to generate code and SOAP Request and Response XML for each operation in a WSDL. // -------------------------------------------------------------------------------- End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.