Sample code for 30+ languages & platforms
Tcl

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 Tcl Downloads

Tcl

load ./chilkat.dll

set 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

set success 1
# Create the XML to be signed...
set xmlToSign [new_CkXml]

CkXml_put_Tag $xmlToSign "soap:Envelope"
CkXml_AddAttribute $xmlToSign "xmlns:soap" "http://www.w3.org/2003/05/soap-envelope"
CkXml_AddAttribute $xmlToSign "xmlns:wcf" "http://wcf.dian.colombia"
CkXml_UpdateAttrAt $xmlToSign "soap:Header" 1 "xmlns:wsa" "http://www.w3.org/2005/08/addressing"
CkXml_UpdateAttrAt $xmlToSign "soap:Header|wsse:Security" 1 "xmlns:wsse" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
CkXml_UpdateAttrAt $xmlToSign "soap:Header|wsse:Security" 1 "xmlns:wsu" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
CkXml_UpdateAttrAt $xmlToSign "soap:Header|wsse:Security|wsu:Timestamp" 1 "wsu:Id" "TS-F25839120CBA3ECDAD68754D0443A667636FDA68"
CkXml_UpdateChildContent $xmlToSign "soap:Header|wsse:Security|wsu:Timestamp|wsu:Created" "2019-08-23T23:03:01Z"
CkXml_UpdateChildContent $xmlToSign "soap:Header|wsse:Security|wsu:Timestamp|wsu:Expires" "2019-08-24T15:43:01Z"
CkXml_UpdateAttrAt $xmlToSign "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"
CkXml_UpdateAttrAt $xmlToSign "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"
CkXml_UpdateAttrAt $xmlToSign "soap:Header|wsse:Security|wsse:BinarySecurityToken" 1 "wsu:Id" "ABCXYZ-9F0F7E15A59816E680B4735080A789DC1EED6C9C"
CkXml_UpdateChildContent $xmlToSign "soap:Header|wsse:Security|wsse:BinarySecurityToken" "BinarySecurityToken_Base64Binary_Content"
CkXml_UpdateChildContent $xmlToSign "soap:Header|wsa:Action" "http://wcf.dian.colombia/IWcfDianCustomerServices/GetStatus"
CkXml_UpdateAttrAt $xmlToSign "soap:Header|wsa:To" 1 "wsu:Id" "ID-F25839120CBA3ECDAD68754D0443A667636FDA68"
CkXml_UpdateAttrAt $xmlToSign "soap:Header|wsa:To" 1 "xmlns:wsu" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
CkXml_UpdateChildContent $xmlToSign "soap:Header|wsa:To" "https://vpfe-hab.dian.gov.co/WcfDianCustomerServices.svc"
CkXml_UpdateChildContent $xmlToSign "soap:Body|wcf:GetStatus|wcf:trackId" "123456666"

set gen [new_CkXmlDSigGen]

CkXmlDSigGen_put_SigLocation $gen "soap:Envelope|soap:Header|wsse:Security"
CkXmlDSigGen_put_SigLocationMod $gen 0
CkXmlDSigGen_put_SigId $gen "SIG-F25839120CBA3ECDAD68754D0443A667636FDA68"
CkXmlDSigGen_put_SigNamespacePrefix $gen "ds"
CkXmlDSigGen_put_SigNamespaceUri $gen "http://www.w3.org/2000/09/xmldsig#"
CkXmlDSigGen_put_SignedInfoCanonAlg $gen "EXCL_C14N"
CkXmlDSigGen_put_SignedInfoDigestMethod $gen "sha256"

# Set the KeyInfoId before adding references..
CkXmlDSigGen_put_KeyInfoId $gen "KI-F25839120CBA3ECDAD68754D0443A667636FDA68"

# -------- Reference 1 --------
CkXmlDSigGen_AddSameDocRef $gen "ID-F25839120CBA3ECDAD68754D0443A667636FDA68" "sha256" "EXCL_C14N" "soap wcf" ""

# Provide a certificate + private key. (PFX password is test123)
set cert [new_CkCert]

set success [CkCert_LoadPfxFile $cert "qa_data/pfx/cert_test123.pfx" "test123"]
if {$success != 1} then {
    puts [CkCert_lastErrorText $cert]
    delete_CkXml $xmlToSign
    delete_CkXmlDSigGen $gen
    delete_CkCert $cert
    exit
}

CkXmlDSigGen_SetX509Cert $gen $cert 1

CkXmlDSigGen_put_KeyInfoType $gen "Custom"

# Create the custom KeyInfo XML..
set xmlCustomKeyInfo [new_CkXml]

CkXml_put_Tag $xmlCustomKeyInfo "wsse:SecurityTokenReference"
CkXml_AddAttribute $xmlCustomKeyInfo "wsu:Id" "STR-F25839120CBA3ECDAD68754D0443A667636FDA68"
CkXml_UpdateAttrAt $xmlCustomKeyInfo "wsse:Reference" 1 "URI" "#ABCXYZ-9F0F7E15A59816E680B4735080A789DC1EED6C9C"
CkXml_UpdateAttrAt $xmlCustomKeyInfo "wsse:Reference" 1 "ValueType" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"

CkXml_put_EmitXmlDecl $xmlCustomKeyInfo 0
CkXmlDSigGen_put_CustomKeyInfoXml $gen [CkXml_getXml $xmlCustomKeyInfo]

# Load XML to be signed...
set sbXml [new_CkStringBuilder]

CkXml_GetXmlSb $xmlToSign $sbXml

# Update BinarySecurityToken_Base64Binary_Content with the actual X509 of the signing cert.
set bdCert [new_CkBinData]

CkCert_ExportCertDerBd $cert $bdCert

set nReplaced [CkStringBuilder_Replace $sbXml "BinarySecurityToken_Base64Binary_Content" [CkBinData_getEncoded $bdCert "base64"]]

CkXmlDSigGen_put_Behaviors $gen "IndentedSignature"

# Sign the XML...
set success [CkXmlDSigGen_CreateXmlDSigSb $gen $sbXml]
if {$success != 1} then {
    puts [CkXmlDSigGen_lastErrorText $gen]
    delete_CkXml $xmlToSign
    delete_CkXmlDSigGen $gen
    delete_CkCert $cert
    delete_CkXml $xmlCustomKeyInfo
    delete_CkStringBuilder $sbXml
    delete_CkBinData $bdCert
    exit
}

# -----------------------------------------------

# Save the signed XML to a file.
set success [CkStringBuilder_WriteFile $sbXml "qa_output/signedXml.xml" "utf-8" 0]

puts [CkStringBuilder_getAsString $sbXml]

# ----------------------------------------
# Verify the signatures we just produced...
set verifier [new_CkXmlDSig]

set success [CkXmlDSig_LoadSignatureSb $verifier $sbXml]
if {$success != 1} then {
    puts [CkXmlDSig_lastErrorText $verifier]
    delete_CkXml $xmlToSign
    delete_CkXmlDSigGen $gen
    delete_CkCert $cert
    delete_CkXml $xmlCustomKeyInfo
    delete_CkStringBuilder $sbXml
    delete_CkBinData $bdCert
    delete_CkXmlDSig $verifier
    exit
}

set numSigs [CkXmlDSig_get_NumSignatures $verifier]
set verifyIdx 0
while {$verifyIdx < $numSigs} {
    CkXmlDSig_put_Selector $verifier $verifyIdx
    set verified [CkXmlDSig_VerifySignature $verifier 1]
    if {$verified != 1} then {
        puts [CkXmlDSig_lastErrorText $verifier]
        delete_CkXml $xmlToSign
        delete_CkXmlDSigGen $gen
        delete_CkCert $cert
        delete_CkXml $xmlCustomKeyInfo
        delete_CkStringBuilder $sbXml
        delete_CkBinData $bdCert
        delete_CkXmlDSig $verifier
        exit
    }

    set verifyIdx [expr $verifyIdx + 1]
}
puts "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.
# --------------------------------------------------------------------------------

delete_CkXml $xmlToSign
delete_CkXmlDSigGen $gen
delete_CkCert $cert
delete_CkXml $xmlCustomKeyInfo
delete_CkStringBuilder $sbXml
delete_CkBinData $bdCert
delete_CkXmlDSig $verifier