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 <C_CkXmlW.h>
#include <C_CkXmlDSigGenW.h>
#include <C_CkCertW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkXmlDSigW.h>
void ChilkatSample(void)
{
BOOL success;
HCkXmlW xmlToSign;
HCkXmlDSigGenW gen;
HCkCertW cert;
HCkXmlW xmlCustomKeyInfo;
HCkStringBuilderW sbXml;
int nReplaced;
HCkXmlDSigW verifier;
int numSigs;
int verifyIdx;
BOOL verified;
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...
xmlToSign = CkXmlW_Create();
CkXmlW_putTag(xmlToSign,L"soapenv:Envelope");
CkXmlW_AddAttribute(xmlToSign,L"xmlns:obs",L"http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/");
CkXmlW_AddAttribute(xmlToSign,L"xmlns:soapenv",L"http://schemas.xmlsoap.org/soap/envelope/");
CkXmlW_UpdateAttrAt(xmlToSign,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");
CkXmlW_UpdateAttrAt(xmlToSign,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");
CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Header|wsse:Security",TRUE,L"soapenv:mustUnderstand",L"1");
CkXmlW_UpdateAttrAt(xmlToSign,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");
CkXmlW_UpdateAttrAt(xmlToSign,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");
CkXmlW_UpdateAttrAt(xmlToSign,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.
CkXmlW_UpdateChildContent(xmlToSign,L"soapenv:Header|wsse:Security|wsse:BinarySecurityToken",L"BinarySecurityToken_Base64Binary_Content");
CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Body",TRUE,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
CkXmlW_UpdateAttrAt(xmlToSign,L"soapenv:Body",TRUE,L"wsu:Id",L"id-396BB6026342EB5C0E1EA73593B3CC098");
CkXmlW_UpdateChildContent(xmlToSign,L"soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|idBiznesowy",L"000000011986");
CkXmlW_UpdateChildContent(xmlToSign,L"soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|rodzajPodmiotuRaportujacego",L"PA");
gen = CkXmlDSigGenW_Create();
CkXmlDSigGenW_putSigLocation(gen,L"soapenv:Envelope|soapenv:Header|wsse:Security");
CkXmlDSigGenW_putSigLocationMod(gen,0);
CkXmlDSigGenW_putSigId(gen,L"SIG-BB965DFC3C8AAF87903C0ED898B8D2A8D");
CkXmlDSigGenW_putSigNamespacePrefix(gen,L"ds");
CkXmlDSigGenW_putSigNamespaceUri(gen,L"http://www.w3.org/2000/09/xmldsig#");
CkXmlDSigGenW_putSignedInfoCanonAlg(gen,L"EXCL_C14N");
CkXmlDSigGenW_putSignedInfoDigestMethod(gen,L"sha1");
// Set the KeyInfoId before adding references..
CkXmlDSigGenW_putKeyInfoId(gen,L"KI-9D95C38916099AD2EE87DDAC1A76E97E4");
// -------- Reference 1 --------
CkXmlDSigGenW_AddSameDocRef(gen,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)
cert = CkCertW_Create();
success = CkCertW_LoadPfxFile(cert,L"qa_data/pfx/cert_test123.pfx",L"test123");
if (success != TRUE) {
wprintf(L"%s\n",CkCertW_lastErrorText(cert));
CkXmlW_Dispose(xmlToSign);
CkXmlDSigGenW_Dispose(gen);
CkCertW_Dispose(cert);
return;
}
CkXmlDSigGenW_SetX509Cert(gen,cert,TRUE);
CkXmlDSigGenW_putKeyInfoType(gen,L"Custom");
// Create the custom KeyInfo XML..
xmlCustomKeyInfo = CkXmlW_Create();
CkXmlW_putTag(xmlCustomKeyInfo,L"wsse:SecurityTokenReference");
CkXmlW_AddAttribute(xmlCustomKeyInfo,L"wsse11:TokenType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1");
CkXmlW_AddAttribute(xmlCustomKeyInfo,L"xmlns:wsse11",L"http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd");
CkXmlW_AddAttribute(xmlCustomKeyInfo,L"wsu:Id",L"STR-FF238E7C061332C5B19752C2FBC8CDEF2");
CkXmlW_UpdateAttrAt(xmlCustomKeyInfo,L"wsse:Reference",TRUE,L"URI",L"#X509-02BF0107214FC61449FD0013DF68F0359");
CkXmlW_UpdateAttrAt(xmlCustomKeyInfo,L"wsse:Reference",TRUE,L"ValueType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1");
CkXmlW_putEmitXmlDecl(xmlCustomKeyInfo,FALSE);
CkXmlDSigGenW_putCustomKeyInfoXml(gen,CkXmlW_getXml(xmlCustomKeyInfo));
// Load XML to be signed...
sbXml = CkStringBuilderW_Create();
CkXmlW_GetXmlSb(xmlToSign,sbXml);
// Update BinarySecurityToken_Base64Binary_Content with the actual X509PKIPathv1 of the signing cert.
nReplaced = CkStringBuilderW_Replace(sbXml,L"BinarySecurityToken_Base64Binary_Content",CkCertW_x509PKIPathv1(cert));
CkXmlDSigGenW_putBehaviors(gen,L"IndentedSignature");
// Sign the XML...
success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml);
if (success != TRUE) {
wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen));
CkXmlW_Dispose(xmlToSign);
CkXmlDSigGenW_Dispose(gen);
CkCertW_Dispose(cert);
CkXmlW_Dispose(xmlCustomKeyInfo);
CkStringBuilderW_Dispose(sbXml);
return;
}
// -----------------------------------------------
// Save the signed XML to a file.
success = CkStringBuilderW_WriteFile(sbXml,L"qa_output/signedXml.xml",L"utf-8",FALSE);
wprintf(L"%s\n",CkStringBuilderW_getAsString(sbXml));
// ----------------------------------------
// Verify the signatures we just produced...
verifier = CkXmlDSigW_Create();
success = CkXmlDSigW_LoadSignatureSb(verifier,sbXml);
if (success != TRUE) {
wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier));
CkXmlW_Dispose(xmlToSign);
CkXmlDSigGenW_Dispose(gen);
CkCertW_Dispose(cert);
CkXmlW_Dispose(xmlCustomKeyInfo);
CkStringBuilderW_Dispose(sbXml);
CkXmlDSigW_Dispose(verifier);
return;
}
numSigs = CkXmlDSigW_getNumSignatures(verifier);
verifyIdx = 0;
while (verifyIdx < numSigs) {
CkXmlDSigW_putSelector(verifier,verifyIdx);
verified = CkXmlDSigW_VerifySignature(verifier,TRUE);
if (verified != TRUE) {
wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier));
CkXmlW_Dispose(xmlToSign);
CkXmlDSigGenW_Dispose(gen);
CkCertW_Dispose(cert);
CkXmlW_Dispose(xmlCustomKeyInfo);
CkStringBuilderW_Dispose(sbXml);
CkXmlDSigW_Dispose(verifier);
return;
}
verifyIdx = verifyIdx + 1;
}
wprintf(L"All signatures were successfully verified.\n");
CkXmlW_Dispose(xmlToSign);
CkXmlDSigGenW_Dispose(gen);
CkCertW_Dispose(cert);
CkXmlW_Dispose(xmlCustomKeyInfo);
CkStringBuilderW_Dispose(sbXml);
CkXmlDSigW_Dispose(verifier);
}