Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkXml.h>
#include <C_CkXmlDSigGen.h>
#include <C_CkCert.h>
#include <C_CkStringBuilder.h>
#include <C_CkXmlDSig.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkXml xmlToSign;
    HCkXmlDSigGen gen;
    HCkCert cert;
    HCkXml xmlCustomKeyInfo;
    HCkStringBuilder sbXml;
    int nReplaced;
    HCkXmlDSig 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 = CkXml_Create();
    CkXml_putTag(xmlToSign,"soapenv:Envelope");
    CkXml_AddAttribute(xmlToSign,"xmlns:obs","http://csioz.gov.pl/zsmopl/ws/obslugakomunikatow/");
    CkXml_AddAttribute(xmlToSign,"xmlns:soapenv","http://schemas.xmlsoap.org/soap/envelope/");
    CkXml_UpdateAttrAt(xmlToSign,"soapenv:Header|wsse:Security",TRUE,"xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
    CkXml_UpdateAttrAt(xmlToSign,"soapenv:Header|wsse:Security",TRUE,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
    CkXml_UpdateAttrAt(xmlToSign,"soapenv:Header|wsse:Security",TRUE,"soapenv:mustUnderstand","1");
    CkXml_UpdateAttrAt(xmlToSign,"soapenv:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,"EncodingType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
    CkXml_UpdateAttrAt(xmlToSign,"soapenv:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1");
    CkXml_UpdateAttrAt(xmlToSign,"soapenv:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,"wsu:Id","X509-02BF0107214FC61449FD0013DF68F0359");
    // Note: The content of this XML node is a placeholder that will be updated below with the X509PKIPathv1 for the signing certificate.
    CkXml_UpdateChildContent(xmlToSign,"soapenv:Header|wsse:Security|wsse:BinarySecurityToken","BinarySecurityToken_Base64Binary_Content");
    CkXml_UpdateAttrAt(xmlToSign,"soapenv:Body",TRUE,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
    CkXml_UpdateAttrAt(xmlToSign,"soapenv:Body",TRUE,"wsu:Id","id-396BB6026342EB5C0E1EA73593B3CC098");
    CkXml_UpdateChildContent(xmlToSign,"soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|idBiznesowy","000000011986");
    CkXml_UpdateChildContent(xmlToSign,"soapenv:Body|obs:zapiszKomunikatOS|komunikatOS|idPodmiotuRaportujacego|rodzajPodmiotuRaportujacego","PA");

    gen = CkXmlDSigGen_Create();

    CkXmlDSigGen_putSigLocation(gen,"soapenv:Envelope|soapenv:Header|wsse:Security");
    CkXmlDSigGen_putSigLocationMod(gen,0);
    CkXmlDSigGen_putSigId(gen,"SIG-BB965DFC3C8AAF87903C0ED898B8D2A8D");
    CkXmlDSigGen_putSigNamespacePrefix(gen,"ds");
    CkXmlDSigGen_putSigNamespaceUri(gen,"http://www.w3.org/2000/09/xmldsig#");
    CkXmlDSigGen_putSignedInfoCanonAlg(gen,"EXCL_C14N");
    CkXmlDSigGen_putSignedInfoDigestMethod(gen,"sha1");

    // Set the KeyInfoId before adding references..
    CkXmlDSigGen_putKeyInfoId(gen,"KI-9D95C38916099AD2EE87DDAC1A76E97E4");

    // -------- Reference 1 --------
    CkXmlDSigGen_AddSameDocRef(gen,"id-396BB6026342EB5C0E1EA73593B3CC098","sha1","EXCL_C14N","obs","");

    // 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 = CkCert_Create();
    success = CkCert_LoadPfxFile(cert,"qa_data/pfx/cert_test123.pfx","test123");
    if (success != TRUE) {
        printf("%s\n",CkCert_lastErrorText(cert));
        CkXml_Dispose(xmlToSign);
        CkXmlDSigGen_Dispose(gen);
        CkCert_Dispose(cert);
        return;
    }

    CkXmlDSigGen_SetX509Cert(gen,cert,TRUE);

    CkXmlDSigGen_putKeyInfoType(gen,"Custom");

    // Create the custom KeyInfo XML..
    xmlCustomKeyInfo = CkXml_Create();
    CkXml_putTag(xmlCustomKeyInfo,"wsse:SecurityTokenReference");
    CkXml_AddAttribute(xmlCustomKeyInfo,"wsse11:TokenType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1");
    CkXml_AddAttribute(xmlCustomKeyInfo,"xmlns:wsse11","http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd");
    CkXml_AddAttribute(xmlCustomKeyInfo,"wsu:Id","STR-FF238E7C061332C5B19752C2FBC8CDEF2");
    CkXml_UpdateAttrAt(xmlCustomKeyInfo,"wsse:Reference",TRUE,"URI","#X509-02BF0107214FC61449FD0013DF68F0359");
    CkXml_UpdateAttrAt(xmlCustomKeyInfo,"wsse:Reference",TRUE,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1");

    CkXml_putEmitXmlDecl(xmlCustomKeyInfo,FALSE);
    CkXmlDSigGen_putCustomKeyInfoXml(gen,CkXml_getXml(xmlCustomKeyInfo));

    // Load XML to be signed...
    sbXml = CkStringBuilder_Create();
    CkXml_GetXmlSb(xmlToSign,sbXml);

    // Update BinarySecurityToken_Base64Binary_Content with the actual X509PKIPathv1 of the signing cert.
    nReplaced = CkStringBuilder_Replace(sbXml,"BinarySecurityToken_Base64Binary_Content",CkCert_x509PKIPathv1(cert));

    CkXmlDSigGen_putBehaviors(gen,"IndentedSignature");

    // Sign the XML...
    success = CkXmlDSigGen_CreateXmlDSigSb(gen,sbXml);
    if (success != TRUE) {
        printf("%s\n",CkXmlDSigGen_lastErrorText(gen));
        CkXml_Dispose(xmlToSign);
        CkXmlDSigGen_Dispose(gen);
        CkCert_Dispose(cert);
        CkXml_Dispose(xmlCustomKeyInfo);
        CkStringBuilder_Dispose(sbXml);
        return;
    }

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

    // Save the signed XML to a file.
    success = CkStringBuilder_WriteFile(sbXml,"qa_output/signedXml.xml","utf-8",FALSE);

    printf("%s\n",CkStringBuilder_getAsString(sbXml));

    // ----------------------------------------
    // Verify the signatures we just produced...
    verifier = CkXmlDSig_Create();
    success = CkXmlDSig_LoadSignatureSb(verifier,sbXml);
    if (success != TRUE) {
        printf("%s\n",CkXmlDSig_lastErrorText(verifier));
        CkXml_Dispose(xmlToSign);
        CkXmlDSigGen_Dispose(gen);
        CkCert_Dispose(cert);
        CkXml_Dispose(xmlCustomKeyInfo);
        CkStringBuilder_Dispose(sbXml);
        CkXmlDSig_Dispose(verifier);
        return;
    }

    numSigs = CkXmlDSig_getNumSignatures(verifier);
    verifyIdx = 0;
    while (verifyIdx < numSigs) {
        CkXmlDSig_putSelector(verifier,verifyIdx);
        verified = CkXmlDSig_VerifySignature(verifier,TRUE);
        if (verified != TRUE) {
            printf("%s\n",CkXmlDSig_lastErrorText(verifier));
            CkXml_Dispose(xmlToSign);
            CkXmlDSigGen_Dispose(gen);
            CkCert_Dispose(cert);
            CkXml_Dispose(xmlCustomKeyInfo);
            CkStringBuilder_Dispose(sbXml);
            CkXmlDSig_Dispose(verifier);
            return;
        }

        verifyIdx = verifyIdx + 1;
    }

    printf("All signatures were successfully verified.\n");


    CkXml_Dispose(xmlToSign);
    CkXmlDSigGen_Dispose(gen);
    CkCert_Dispose(cert);
    CkXml_Dispose(xmlCustomKeyInfo);
    CkStringBuilder_Dispose(sbXml);
    CkXmlDSig_Dispose(verifier);

    }