Sample code for 30+ languages & platforms
PowerBuilder

Create Signed SOAP XML for DIAN Colombia WCF Service

See more XAdES Examples

Demonstrates how to create a signed SOAP XML document for DIAN Colombia.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_XmlToSign
oleobject loo_Gen
oleobject loo_Cert
oleobject loo_XmlCustomKeyInfo
oleobject loo_SbXml
oleobject loo_BdCert
integer li_NReplaced
oleobject loo_Verifier
integer li_NumSigs
integer li_VerifyIdx
integer li_Verified

li_Success = 0

// 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

li_Success = 1
// Create the XML to be signed...
loo_XmlToSign = create oleobject
li_rc = loo_XmlToSign.ConnectToNewObject("Chilkat.Xml")
if li_rc < 0 then
    destroy loo_XmlToSign
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_XmlToSign.Tag = "soap:Envelope"
loo_XmlToSign.AddAttribute("xmlns:soap","http://www.w3.org/2003/05/soap-envelope")
loo_XmlToSign.AddAttribute("xmlns:wcf","http://wcf.dian.colombia")
loo_XmlToSign.UpdateAttrAt("soap:Header",1,"xmlns:wsa","http://www.w3.org/2005/08/addressing")
loo_XmlToSign.UpdateAttrAt("soap:Header|wsse:Security",1,"xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")
loo_XmlToSign.UpdateAttrAt("soap:Header|wsse:Security",1,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
loo_XmlToSign.UpdateAttrAt("soap:Header|wsse:Security|wsu:Timestamp",1,"wsu:Id","TS-F25839120CBA3ECDAD68754D0443A667636FDA68")
loo_XmlToSign.UpdateChildContent("soap:Header|wsse:Security|wsu:Timestamp|wsu:Created","2019-08-23T23:03:01Z")
loo_XmlToSign.UpdateChildContent("soap:Header|wsse:Security|wsu:Timestamp|wsu:Expires","2019-08-24T15:43:01Z")
loo_XmlToSign.UpdateAttrAt("soap:Header|wsse:Security|wsse:BinarySecurityToken",1,"EncodingType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary")
loo_XmlToSign.UpdateAttrAt("soap:Header|wsse:Security|wsse:BinarySecurityToken",1,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3")
loo_XmlToSign.UpdateAttrAt("soap:Header|wsse:Security|wsse:BinarySecurityToken",1,"wsu:Id","ABCXYZ-9F0F7E15A59816E680B4735080A789DC1EED6C9C")
loo_XmlToSign.UpdateChildContent("soap:Header|wsse:Security|wsse:BinarySecurityToken","BinarySecurityToken_Base64Binary_Content")
loo_XmlToSign.UpdateChildContent("soap:Header|wsa:Action","http://wcf.dian.colombia/IWcfDianCustomerServices/GetStatus")
loo_XmlToSign.UpdateAttrAt("soap:Header|wsa:To",1,"wsu:Id","ID-F25839120CBA3ECDAD68754D0443A667636FDA68")
loo_XmlToSign.UpdateAttrAt("soap:Header|wsa:To",1,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
loo_XmlToSign.UpdateChildContent("soap:Header|wsa:To","https://vpfe-hab.dian.gov.co/WcfDianCustomerServices.svc")
loo_XmlToSign.UpdateChildContent("soap:Body|wcf:GetStatus|wcf:trackId","123456666")

loo_Gen = create oleobject
li_rc = loo_Gen.ConnectToNewObject("Chilkat.XmlDSigGen")

loo_Gen.SigLocation = "soap:Envelope|soap:Header|wsse:Security"
loo_Gen.SigLocationMod = 0
loo_Gen.SigId = "SIG-F25839120CBA3ECDAD68754D0443A667636FDA68"
loo_Gen.SigNamespacePrefix = "ds"
loo_Gen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
loo_Gen.SignedInfoCanonAlg = "EXCL_C14N"
loo_Gen.SignedInfoDigestMethod = "sha256"

// Set the KeyInfoId before adding references..
loo_Gen.KeyInfoId = "KI-F25839120CBA3ECDAD68754D0443A667636FDA68"

// -------- Reference 1 --------
loo_Gen.AddSameDocRef("ID-F25839120CBA3ECDAD68754D0443A667636FDA68","sha256","EXCL_C14N","soap wcf","")

// Provide a certificate + private key. (PFX password is test123)
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
if li_Success <> 1 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_XmlToSign
    destroy loo_Gen
    destroy loo_Cert
    return
end if

loo_Gen.SetX509Cert(loo_Cert,1)

loo_Gen.KeyInfoType = "Custom"

// Create the custom KeyInfo XML..
loo_XmlCustomKeyInfo = create oleobject
li_rc = loo_XmlCustomKeyInfo.ConnectToNewObject("Chilkat.Xml")

loo_XmlCustomKeyInfo.Tag = "wsse:SecurityTokenReference"
loo_XmlCustomKeyInfo.AddAttribute("wsu:Id","STR-F25839120CBA3ECDAD68754D0443A667636FDA68")
loo_XmlCustomKeyInfo.UpdateAttrAt("wsse:Reference",1,"URI","#ABCXYZ-9F0F7E15A59816E680B4735080A789DC1EED6C9C")
loo_XmlCustomKeyInfo.UpdateAttrAt("wsse:Reference",1,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3")

loo_XmlCustomKeyInfo.EmitXmlDecl = 0
loo_Gen.CustomKeyInfoXml = loo_XmlCustomKeyInfo.GetXml()

// Load XML to be signed...
loo_SbXml = create oleobject
li_rc = loo_SbXml.ConnectToNewObject("Chilkat.StringBuilder")

loo_XmlToSign.GetXmlSb(loo_SbXml)

// Update BinarySecurityToken_Base64Binary_Content with the actual X509 of the signing cert.
loo_BdCert = create oleobject
li_rc = loo_BdCert.ConnectToNewObject("Chilkat.BinData")

loo_Cert.ExportCertDerBd(loo_BdCert)

li_NReplaced = loo_SbXml.Replace("BinarySecurityToken_Base64Binary_Content",loo_BdCert.GetEncoded("base64"))

loo_Gen.Behaviors = "IndentedSignature"

// Sign the XML...
li_Success = loo_Gen.CreateXmlDSigSb(loo_SbXml)
if li_Success <> 1 then
    Write-Debug loo_Gen.LastErrorText
    destroy loo_XmlToSign
    destroy loo_Gen
    destroy loo_Cert
    destroy loo_XmlCustomKeyInfo
    destroy loo_SbXml
    destroy loo_BdCert
    return
end if

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

// Save the signed XML to a file.
li_Success = loo_SbXml.WriteFile("qa_output/signedXml.xml","utf-8",0)

Write-Debug loo_SbXml.GetAsString()

// ----------------------------------------
// Verify the signatures we just produced...
loo_Verifier = create oleobject
li_rc = loo_Verifier.ConnectToNewObject("Chilkat.XmlDSig")

li_Success = loo_Verifier.LoadSignatureSb(loo_SbXml)
if li_Success <> 1 then
    Write-Debug loo_Verifier.LastErrorText
    destroy loo_XmlToSign
    destroy loo_Gen
    destroy loo_Cert
    destroy loo_XmlCustomKeyInfo
    destroy loo_SbXml
    destroy loo_BdCert
    destroy loo_Verifier
    return
end if

li_NumSigs = loo_Verifier.NumSignatures
li_VerifyIdx = 0
do while li_VerifyIdx < li_NumSigs
    loo_Verifier.Selector = li_VerifyIdx
    li_Verified = loo_Verifier.VerifySignature(1)
    if li_Verified <> 1 then
        Write-Debug loo_Verifier.LastErrorText
        destroy loo_XmlToSign
        destroy loo_Gen
        destroy loo_Cert
        destroy loo_XmlCustomKeyInfo
        destroy loo_SbXml
        destroy loo_BdCert
        destroy loo_Verifier
        return
    end if

    li_VerifyIdx = li_VerifyIdx + 1
loop
Write-Debug "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.
// --------------------------------------------------------------------------------


destroy loo_XmlToSign
destroy loo_Gen
destroy loo_Cert
destroy loo_XmlCustomKeyInfo
destroy loo_SbXml
destroy loo_BdCert
destroy loo_Verifier