Unicode C++
Unicode C++
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 Unicode C++ Downloads
#include <CkXmlW.h>
#include <CkXmlDSigGenW.h>
#include <CkCertW.h>
#include <CkStringBuilderW.h>
#include <CkBinDataW.h>
#include <CkXmlDSigW.h>
void ChilkatSample(void)
{
bool success = false;
// 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...
CkXmlW xmlToSign;
xmlToSign.put_Tag(L"soap:Envelope");
xmlToSign.AddAttribute(L"xmlns:soap",L"http://www.w3.org/2003/05/soap-envelope");
xmlToSign.AddAttribute(L"xmlns:wcf",L"http://wcf.dian.colombia");
xmlToSign.UpdateAttrAt(L"soap:Header",true,L"xmlns:wsa",L"http://www.w3.org/2005/08/addressing");
xmlToSign.UpdateAttrAt(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");
xmlToSign.UpdateAttrAt(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");
xmlToSign.UpdateAttrAt(L"soap:Header|wsse:Security|wsu:Timestamp",true,L"wsu:Id",L"TS-F25839120CBA3ECDAD68754D0443A667636FDA68");
xmlToSign.UpdateChildContent(L"soap:Header|wsse:Security|wsu:Timestamp|wsu:Created",L"2019-08-23T23:03:01Z");
xmlToSign.UpdateChildContent(L"soap:Header|wsse:Security|wsu:Timestamp|wsu:Expires",L"2019-08-24T15:43:01Z");
xmlToSign.UpdateAttrAt(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");
xmlToSign.UpdateAttrAt(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");
xmlToSign.UpdateAttrAt(L"soap:Header|wsse:Security|wsse:BinarySecurityToken",true,L"wsu:Id",L"ABCXYZ-9F0F7E15A59816E680B4735080A789DC1EED6C9C");
xmlToSign.UpdateChildContent(L"soap:Header|wsse:Security|wsse:BinarySecurityToken",L"BinarySecurityToken_Base64Binary_Content");
xmlToSign.UpdateChildContent(L"soap:Header|wsa:Action",L"http://wcf.dian.colombia/IWcfDianCustomerServices/GetStatus");
xmlToSign.UpdateAttrAt(L"soap:Header|wsa:To",true,L"wsu:Id",L"ID-F25839120CBA3ECDAD68754D0443A667636FDA68");
xmlToSign.UpdateAttrAt(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");
xmlToSign.UpdateChildContent(L"soap:Header|wsa:To",L"https://vpfe-hab.dian.gov.co/WcfDianCustomerServices.svc");
xmlToSign.UpdateChildContent(L"soap:Body|wcf:GetStatus|wcf:trackId",L"123456666");
CkXmlDSigGenW gen;
gen.put_SigLocation(L"soap:Envelope|soap:Header|wsse:Security");
gen.put_SigLocationMod(0);
gen.put_SigId(L"SIG-F25839120CBA3ECDAD68754D0443A667636FDA68");
gen.put_SigNamespacePrefix(L"ds");
gen.put_SigNamespaceUri(L"http://www.w3.org/2000/09/xmldsig#");
gen.put_SignedInfoCanonAlg(L"EXCL_C14N");
gen.put_SignedInfoDigestMethod(L"sha256");
// Set the KeyInfoId before adding references..
gen.put_KeyInfoId(L"KI-F25839120CBA3ECDAD68754D0443A667636FDA68");
// -------- Reference 1 --------
gen.AddSameDocRef(L"ID-F25839120CBA3ECDAD68754D0443A667636FDA68",L"sha256",L"EXCL_C14N",L"soap wcf",L"");
// Provide a certificate + private key. (PFX password is test123)
CkCertW cert;
success = cert.LoadPfxFile(L"qa_data/pfx/cert_test123.pfx",L"test123");
if (success != true) {
wprintf(L"%s\n",cert.lastErrorText());
return;
}
gen.SetX509Cert(cert,true);
gen.put_KeyInfoType(L"Custom");
// Create the custom KeyInfo XML..
CkXmlW xmlCustomKeyInfo;
xmlCustomKeyInfo.put_Tag(L"wsse:SecurityTokenReference");
xmlCustomKeyInfo.AddAttribute(L"wsu:Id",L"STR-F25839120CBA3ECDAD68754D0443A667636FDA68");
xmlCustomKeyInfo.UpdateAttrAt(L"wsse:Reference",true,L"URI",L"#ABCXYZ-9F0F7E15A59816E680B4735080A789DC1EED6C9C");
xmlCustomKeyInfo.UpdateAttrAt(L"wsse:Reference",true,L"ValueType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");
xmlCustomKeyInfo.put_EmitXmlDecl(false);
gen.put_CustomKeyInfoXml(xmlCustomKeyInfo.getXml());
// Load XML to be signed...
CkStringBuilderW sbXml;
xmlToSign.GetXmlSb(sbXml);
// Update BinarySecurityToken_Base64Binary_Content with the actual X509 of the signing cert.
CkBinDataW bdCert;
cert.ExportCertDerBd(bdCert);
int nReplaced = sbXml.Replace(L"BinarySecurityToken_Base64Binary_Content",bdCert.getEncoded(L"base64"));
gen.put_Behaviors(L"IndentedSignature");
// Sign the XML...
success = gen.CreateXmlDSigSb(sbXml);
if (success != true) {
wprintf(L"%s\n",gen.lastErrorText());
return;
}
// -----------------------------------------------
// Save the signed XML to a file.
success = sbXml.WriteFile(L"qa_output/signedXml.xml",L"utf-8",false);
wprintf(L"%s\n",sbXml.getAsString());
// ----------------------------------------
// Verify the signatures we just produced...
CkXmlDSigW verifier;
success = verifier.LoadSignatureSb(sbXml);
if (success != true) {
wprintf(L"%s\n",verifier.lastErrorText());
return;
}
int numSigs = verifier.get_NumSignatures();
int verifyIdx = 0;
while (verifyIdx < numSigs) {
verifier.put_Selector(verifyIdx);
bool verified = verifier.VerifySignature(true);
if (verified != true) {
wprintf(L"%s\n",verifier.lastErrorText());
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.
// --------------------------------------------------------------------------------
}