Sample code for 30+ languages & platforms
Unicode C

Create/Verify XML Signature with External Text Reference

See more XML Digital Signatures Examples

Demonstrates how to create an XML digital signature where the referenced data is external. In this case, the data is a text file located at the following URL: https://www.chilkatsoft.com/helloWorld.txt

Chilkat Unicode C Downloads

Unicode C
#include <C_CkXmlW.h>
#include <C_CkCertW.h>
#include <C_CkXmlDSigGenW.h>
#include <C_CkHttpW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkXmlDSigW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkXmlW xml;
    HCkCertW cert;
    HCkXmlDSigGenW gen;
    HCkHttpW http;
    HCkStringBuilderW sbExternalTxt;
    BOOL bUsePrivateKey;
    HCkStringBuilderW sbXml;
    HCkXmlDSigW verifier;
    HCkStringBuilderW sbExternalData;
    int numRefs;
    int i;
    BOOL bExternal;
    const wchar_t *uri;
    BOOL bVerified;

    success = FALSE;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // This example inserts an XML signature into the following XML:

    // <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    // <abc>
    //   <xyz>
    // 	<name>helloWorld.txt</name>
    // 	<url>https://www.chilkatsoft.com/helloWorld.txt</url>
    //   </xyz>
    // </abc>

    // Build the above XML to be signed.
    xml = CkXmlW_Create();
    CkXmlW_putTag(xml,L"abc");
    CkXmlW_UpdateChildContent(xml,L"xyz|name",L"helloWorld.txt");
    CkXmlW_UpdateChildContent(xml,L"xyz|url",L"https://www.chilkatsoft.com/helloWorld.txt");

    cert = CkCertW_Create();

    success = CkCertW_LoadPfxFile(cert,L"qa_data/pfx/test_secret.pfx",L"secret");
    if (success == FALSE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkXmlW_Dispose(xml);
        CkCertW_Dispose(cert);
        return;
    }

    gen = CkXmlDSigGenW_Create();

    // Indicate the location within the XML the Signature will be inserted.
    CkXmlDSigGenW_putSigLocation(gen,L"abc|xyz");

    // Get the content of the text data to be referenced.
    http = CkHttpW_Create();
    sbExternalTxt = CkStringBuilderW_Create();
    success = CkHttpW_QuickGetSb(http,L"https://www.chilkatsoft.com/helloWorld.txt",sbExternalTxt);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkXmlW_Dispose(xml);
        CkCertW_Dispose(cert);
        CkXmlDSigGenW_Dispose(gen);
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(sbExternalTxt);
        return;
    }

    success = CkXmlDSigGenW_AddExternalTextRef(gen,L"https://www.chilkatsoft.com/helloWorld.txt",sbExternalTxt,L"utf-8",FALSE,L"sha256",L"");
    if (success == FALSE) {
        wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen));
        CkXmlW_Dispose(xml);
        CkCertW_Dispose(cert);
        CkXmlDSigGenW_Dispose(gen);
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(sbExternalTxt);
        return;
    }

    // Provide the private key for signing via the certificate, and indicate that
    // we want the base64 of the certificate embedded in the KeyInfo.
    CkXmlDSigGenW_putKeyInfoType(gen,L"X509Data");
    CkXmlDSigGenW_putX509Type(gen,L"Certificate");

    bUsePrivateKey = TRUE;
    success = CkXmlDSigGenW_SetX509Cert(gen,cert,bUsePrivateKey);
    if (success != TRUE) {
        wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen));
        CkXmlW_Dispose(xml);
        CkCertW_Dispose(cert);
        CkXmlDSigGenW_Dispose(gen);
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(sbExternalTxt);
        return;
    }

    // Indicate we want an indented signature for readability.
    // This can be removed after debugging is finished..
    CkXmlDSigGenW_putBehaviors(gen,L"IndentedSignature");

    // Now create and insert the Signature
    sbXml = CkStringBuilderW_Create();
    CkXmlW_GetXmlSb(xml,sbXml);
    success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml);
    if (success != TRUE) {
        wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen));
        CkXmlW_Dispose(xml);
        CkCertW_Dispose(cert);
        CkXmlDSigGenW_Dispose(gen);
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(sbExternalTxt);
        CkStringBuilderW_Dispose(sbXml);
        return;
    }

    // Examine the XML with the digital signature inserted
    wprintf(L"%s\n",CkStringBuilderW_getAsString(sbXml));

    // Here is the output:

    // <?xml version="1.0" encoding="utf-8"?>
    // <abc>
    //     <xyz>
    //         <name>helloWorld.txt</name>
    //         <url>https://www.chilkatsoft.com/helloWorld.txt</url>
    //     <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    //   <ds:SignedInfo>
    //     <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    //     <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
    //     <ds:Reference URI="https://www.chilkatsoft.com/helloWorld.txt">
    //       <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    //       <ds:DigestValue>f4OxZX/x/FO5LcGBSKHWXfwtSx+j1ncoSt3SABJtkGk=</ds:DigestValue>
    //     </ds:Reference>
    //   </ds:SignedInfo>
    //   <ds:SignatureValue>syFSl...pZDiFQ==</ds:SignatureValue>
    //   <ds:KeyInfo>
    //     <ds:X509Data>
    //       <ds:X509Certificate>MIIHAz...GwnUZWCaDE=</ds:X509Certificate>
    //     </ds:X509Data>
    //   </ds:KeyInfo>
    // </ds:Signature></xyz>
    // </abc>

    // Let's verify the signature...
    verifier = CkXmlDSigW_Create();
    success = CkXmlDSigW_LoadSignatureSb(verifier,sbXml);
    if (success != TRUE) {
        wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier));
        CkXmlW_Dispose(xml);
        CkCertW_Dispose(cert);
        CkXmlDSigGenW_Dispose(gen);
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(sbExternalTxt);
        CkStringBuilderW_Dispose(sbXml);
        CkXmlDSigW_Dispose(verifier);
        return;
    }

    // Let's examine the external references, and fetch the data for each..
    sbExternalData = CkStringBuilderW_Create();
    numRefs = CkXmlDSigW_getNumReferences(verifier);
    i = 0;
    while (i < numRefs) {
        bExternal = CkXmlDSigW_IsReferenceExternal(verifier,i);
        if (bExternal == TRUE) {
            uri = CkXmlDSigW_referenceUri(verifier,i);

            // We're assuming the URI is an https:// or http:// URL...
            // Let's also assume we know that the referenced data is text and we want the utf-8 byte representation.
            CkStringBuilderW_Clear(sbExternalData);
            success = CkHttpW_QuickGetSb(http,uri,sbExternalData);
            if (success == FALSE) {
                wprintf(L"%s\n",CkHttpW_lastErrorText(http));
                CkXmlW_Dispose(xml);
                CkCertW_Dispose(cert);
                CkXmlDSigGenW_Dispose(gen);
                CkHttpW_Dispose(http);
                CkStringBuilderW_Dispose(sbExternalTxt);
                CkStringBuilderW_Dispose(sbXml);
                CkXmlDSigW_Dispose(verifier);
                CkStringBuilderW_Dispose(sbExternalData);
                return;
            }

            success = CkXmlDSigW_SetRefDataSb(verifier,i,sbExternalData,L"utf-8");
            if (success == FALSE) {
                wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier));
                CkXmlW_Dispose(xml);
                CkCertW_Dispose(cert);
                CkXmlDSigGenW_Dispose(gen);
                CkHttpW_Dispose(http);
                CkStringBuilderW_Dispose(sbExternalTxt);
                CkStringBuilderW_Dispose(sbXml);
                CkXmlDSigW_Dispose(verifier);
                CkStringBuilderW_Dispose(sbExternalData);
                return;
            }

        }

        i = i + 1;
    }

    // Now that we have the external data available, we can verify the reference digest(s) and the signature.
    bVerified = CkXmlDSigW_VerifySignature(verifier,TRUE);
    if (bVerified != TRUE) {
        wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier));
        CkXmlW_Dispose(xml);
        CkCertW_Dispose(cert);
        CkXmlDSigGenW_Dispose(gen);
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(sbExternalTxt);
        CkStringBuilderW_Dispose(sbXml);
        CkXmlDSigW_Dispose(verifier);
        CkStringBuilderW_Dispose(sbExternalData);
        return;
    }

    wprintf(L"Signature verified!\n");


    CkXmlW_Dispose(xml);
    CkCertW_Dispose(cert);
    CkXmlDSigGenW_Dispose(gen);
    CkHttpW_Dispose(http);
    CkStringBuilderW_Dispose(sbExternalTxt);
    CkStringBuilderW_Dispose(sbXml);
    CkXmlDSigW_Dispose(verifier);
    CkStringBuilderW_Dispose(sbExternalData);

    }