Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(Excel) Create Signed SOAP XML for DIAN Colombia WCF ServiceDemonstrates how to create a signed SOAP XML document for DIAN Colombia.
' 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... Dim xmlToSign As Chilkat.Xml Set xmlToSign = Chilkat.NewXml xmlToSign.Tag = "soap:Envelope" Dim success As Boolean success = xmlToSign.AddAttribute("xmlns:soap","http://www.w3.org/2003/05/soap-envelope") success = xmlToSign.AddAttribute("xmlns:wcf","http://wcf.dian.colombia") success = xmlToSign.UpdateAttrAt("soap:Header",True,"xmlns:wsa","http://www.w3.org/2005/08/addressing") success = xmlToSign.UpdateAttrAt("soap:Header|wsse:Security",True,"xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd") success = xmlToSign.UpdateAttrAt("soap:Header|wsse:Security",True,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd") success = xmlToSign.UpdateAttrAt("soap:Header|wsse:Security|wsu:Timestamp",True,"wsu:Id","TS-F25839120CBA3ECDAD68754D0443A667636FDA68") xmlToSign.UpdateChildContent "soap:Header|wsse:Security|wsu:Timestamp|wsu:Created","2019-08-23T23:03:01Z" xmlToSign.UpdateChildContent "soap:Header|wsse:Security|wsu:Timestamp|wsu:Expires","2019-08-24T15:43:01Z" success = xmlToSign.UpdateAttrAt("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") success = xmlToSign.UpdateAttrAt("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") success = xmlToSign.UpdateAttrAt("soap:Header|wsse:Security|wsse:BinarySecurityToken",True,"wsu:Id","ABCXYZ-9F0F7E15A59816E680B4735080A789DC1EED6C9C") xmlToSign.UpdateChildContent "soap:Header|wsse:Security|wsse:BinarySecurityToken","BinarySecurityToken_Base64Binary_Content" xmlToSign.UpdateChildContent "soap:Header|wsa:Action","http://wcf.dian.colombia/IWcfDianCustomerServices/GetStatus" success = xmlToSign.UpdateAttrAt("soap:Header|wsa:To",True,"wsu:Id","ID-F25839120CBA3ECDAD68754D0443A667636FDA68") success = xmlToSign.UpdateAttrAt("soap:Header|wsa:To",True,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd") xmlToSign.UpdateChildContent "soap:Header|wsa:To","https://vpfe-hab.dian.gov.co/WcfDianCustomerServices.svc" xmlToSign.UpdateChildContent "soap:Body|wcf:GetStatus|wcf:trackId","123456666" Dim gen As Chilkat.XmlDSigGen Set gen = Chilkat.NewXmlDSigGen gen.SigLocation = "soap:Envelope|soap:Header|wsse:Security" gen.SigLocationMod = 0 gen.SigId = "SIG-F25839120CBA3ECDAD68754D0443A667636FDA68" gen.SigNamespacePrefix = "ds" gen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#" gen.SignedInfoCanonAlg = "EXCL_C14N" gen.SignedInfoDigestMethod = "sha256" ' Set the KeyInfoId before adding references.. gen.KeyInfoId = "KI-F25839120CBA3ECDAD68754D0443A667636FDA68" ' -------- Reference 1 -------- success = gen.AddSameDocRef("ID-F25839120CBA3ECDAD68754D0443A667636FDA68","sha256","EXCL_C14N","soap wcf","") ' Provide a certificate + private key. (PFX password is test123) Dim cert As Chilkat.Cert Set cert = Chilkat.NewCert success = cert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123") If (success <> True) Then Debug.Print cert.LastErrorText Exit Sub End If success = gen.SetX509Cert(cert,True) gen.KeyInfoType = "Custom" ' Create the custom KeyInfo XML.. Dim xmlCustomKeyInfo As Chilkat.Xml Set xmlCustomKeyInfo = Chilkat.NewXml xmlCustomKeyInfo.Tag = "wsse:SecurityTokenReference" success = xmlCustomKeyInfo.AddAttribute("wsu:Id","STR-F25839120CBA3ECDAD68754D0443A667636FDA68") success = xmlCustomKeyInfo.UpdateAttrAt("wsse:Reference",True,"URI","#ABCXYZ-9F0F7E15A59816E680B4735080A789DC1EED6C9C") success = xmlCustomKeyInfo.UpdateAttrAt("wsse:Reference",True,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3") xmlCustomKeyInfo.EmitXmlDecl = False gen.CustomKeyInfoXml = xmlCustomKeyInfo.GetXml() ' Load XML to be signed... Dim sbXml As Chilkat.StringBuilder Set sbXml = Chilkat.NewStringBuilder success = xmlToSign.GetXmlSb(sbXml) ' Update BinarySecurityToken_Base64Binary_Content with the actual X509 of the signing cert. Dim bdCert As Chilkat.BinData Set bdCert = Chilkat.NewBinData success = cert.ExportCertDerBd(bdCert) nReplaced = sbXml.Replace("BinarySecurityToken_Base64Binary_Content",bdCert.GetEncoded("base64")) gen.Behaviors = "IndentedSignature" ' Sign the XML... success = gen.CreateXmlDSigSb(sbXml) If (success <> True) Then Debug.Print gen.LastErrorText Exit Sub End If ' ----------------------------------------------- ' Save the signed XML to a file. success = sbXml.WriteFile("qa_output/signedXml.xml","utf-8",False) Debug.Print sbXml.GetAsString() ' ---------------------------------------- ' Verify the signatures we just produced... Dim verifier As Chilkat.XmlDSig Set verifier = Chilkat.NewXmlDSig success = verifier.LoadSignatureSb(sbXml) If (success <> True) Then Debug.Print verifier.LastErrorText Exit Sub End If numSigs = verifier.NumSignatures verifyIdx = 0 Do While verifyIdx < numSigs verifier.Selector = verifyIdx verified = verifier.VerifySignature(True) If (verified <> True) Then Debug.Print verifier.LastErrorText Exit Sub End If verifyIdx = verifyIdx + 1 Loop Debug.Print "All signatures were successfully verified." |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.