Unicode C++
Unicode C++
Create XAdES with ec:InclusiveNamespaces within the Transforms
See more XML Digital Signatures Examples
Demonstrates how to generate XAdES that has a Reference that has Transforms that include an ec:InclusiveNamespaces.Chilkat Unicode C++ Downloads
#include <CkXmlW.h>
#include <CkXmlDSigGenW.h>
#include <CkCertW.h>
#include <CkStringBuilderW.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 is the XML we'll be signing:
// <soapenv:Envelope xmlns:obs="http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
// <soapenv:Header>
// <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" soapenv:mustUnderstand="1">
// <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#X509PKIPathv1"
// wsu:Id="X509-02BF0107214FC61449FD0013DF68F0359">MII...</wsse:BinarySecurityToken>
// </wsse:Security>
// </soapenv:Header>
// <soapenv:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
// wsu:Id="id-396BB6026342EB5C0E1EA73593B3CC098">
// <obs:zapiszKomunikatOS>
// <komunikatOS>
// <idPodmiotuRaportujacego>
// <idBiznesowy>000000011986</idBiznesowy>
// <rodzajPodmiotuRaportujacego>PA</rodzajPodmiotuRaportujacego>
// </idPodmiotuRaportujacego>
// </komunikatOS>
// </obs:zapiszKomunikatOS>
// </soapenv:Body>
// </soapenv:Envelope>
//
// ----------------------------------------------------------------------
// IMPORTANT: This generated example requires Chilkat v9.5.0.77 or later.
// ----------------------------------------------------------------------
success = true;
// Create the above XML to be signed...
CkXmlW xmlToSign;
xmlToSign.put_Tag(L"soapenv:Envelope");
xmlToSign.AddAttribute(L"xmlns:obs",L"http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/");
xmlToSign.AddAttribute(L"xmlns:soapenv",L"http://schemas.xmlsoap.org/soap/envelope/");
xmlToSign.UpdateAttrAt(L"soapenv: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"soapenv: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"soapenv:Header|wsse:Security",true,L"soapenv:mustUnderstand",L"1");
xmlToSign.UpdateAttrAt(L"soapenv: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"soapenv: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#X509PKIPathv1");
xmlToSign.UpdateAttrAt(L"soapenv:Header|wsse:Security|wsse:BinarySecurityToken",true,L"wsu:Id",L"X509-02BF0107214FC61449FD0013DF68F0359");
// Note: The content of this XML node is a placeholder that will be updated below with the X509PKIPathv1 for the signing certificate.
xmlToSign.UpdateChildContent(L"soapenv:Header|wsse:Security|wsse:BinarySecurityToken",L"BinarySecurityToken_Base64Binary_Content");
xmlToSign.UpdateAttrAt(L"soapenv:Body",true,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
xmlToSign.UpdateAttrAt(L"soapenv:Body",true,L"wsu:Id",L"id-396BB6026342EB5C0E1EA73593B3CC098");
xmlToSign.UpdateChildContent(L"soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|idBiznesowy",L"000000011986");
xmlToSign.UpdateChildContent(L"soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|rodzajPodmiotuRaportujacego",L"PA");
CkXmlDSigGenW gen;
gen.put_SigLocation(L"soapenv:Envelope|soapenv:Header|wsse:Security");
gen.put_SigLocationMod(0);
gen.put_SigId(L"SIG-BB965DFC3C8AAF87903C0ED898B8D2A8D");
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"sha1");
// Set the KeyInfoId before adding references..
gen.put_KeyInfoId(L"KI-9D95C38916099AD2EE87DDAC1A76E97E4");
// -------- Reference 1 --------
gen.AddSameDocRef(L"id-396BB6026342EB5C0E1EA73593B3CC098",L"sha1",L"EXCL_C14N",L"obs",L"");
// The reference to be produced in the Signature should look like this:
// <ds:Reference URI="#id-396BB6026342EB5C0E1EA73593B3CC098">
// <ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
// <ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="obs"></ec:InclusiveNamespaces>
// </ds:Transform></ds:Transforms>
// <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></ds:DigestMethod>
// <ds:DigestValue>2e9hZYj/CN2nPsgQqUraU43k3ds=</ds:DigestValue>
// </ds:Reference>
//
// 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"wsse11:TokenType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1");
xmlCustomKeyInfo.AddAttribute(L"xmlns:wsse11",L"http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd");
xmlCustomKeyInfo.AddAttribute(L"wsu:Id",L"STR-FF238E7C061332C5B19752C2FBC8CDEF2");
xmlCustomKeyInfo.UpdateAttrAt(L"wsse:Reference",true,L"URI",L"#X509-02BF0107214FC61449FD0013DF68F0359");
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#X509PKIPathv1");
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 X509PKIPathv1 of the signing cert.
int nReplaced = sbXml.Replace(L"BinarySecurityToken_Base64Binary_Content",cert.x509PKIPathv1());
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");
}