Sample code for 30+ languages & platforms
Unicode C

XML Signature Multiple External References

See more XML Digital Signatures Examples

Demonstrates how to add multiple external references to an XML digital signature.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkXmlDSigGenW gen;
    HCkXmlW object1;
    HCkHttpW http;
    HCkBinDataW bd;
    const wchar_t *url;
    HCkCertW cert;
    HCkStringBuilderW sbXml;
    HCkXmlDSigW verifier;
    BOOL verified;

    success = FALSE;

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

    // This example creates the following signed XML with multiple external references:

    // <?xml version="1.0" encoding="UTF-8"?>
    // <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4">
    //     <ds:SignedInfo>
    //         <ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
    //         <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
    //         <ds:Reference Id="xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref0" URI="https://isus.ezdrowie.gov.pl/fhir/Patient/123/_history/456">
    //             <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    //             <ds:DigestValue>...</ds:DigestValue>
    //         </ds:Reference>
    //         <ds:Reference Id="xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref1" URI="https://isus.ezdrowie.gov.pl/fhir/Encounter/124/_history/22">
    //             <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    //             <ds:DigestValue>...</ds:DigestValue>
    //         </ds:Reference>
    //         <ds:Reference Id="xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref2" URI="https://isus.ezdrowie.gov.pl/fhir/Condition/125/_history/1">
    //             <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    //             <ds:DigestValue>...</ds:DigestValue>
    //         </ds:Reference>
    //         <ds:Reference Id="xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref3" URI="https://isus.ezdrowie.gov.pl/fhir/Procedure/126/_history/1">
    //             <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    //             <ds:DigestValue>...</ds:DigestValue>
    //         </ds:Reference>
    //         <ds:Reference Type="http://uri.etsi.org/01903#SignedProperties" URI="#xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-signedprops">
    //             <ds:Transforms>
    //                 <ds:Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
    //             </ds:Transforms>
    //             <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    //             <ds:DigestValue>...</ds:DigestValue>
    //         </ds:Reference>
    //     </ds:SignedInfo>
    //     <ds:SignatureValue Id="xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-sigvalue">...</ds:SignatureValue>
    //     <ds:KeyInfo>
    //         <ds:X509Data>
    //             <ds:X509Certificate>...</ds:X509Certificate>
    //             <ds:X509SubjectName>...</ds:X509SubjectName>
    //             <ds:X509IssuerSerial>
    //                 <ds:X509IssuerName>...</ds:X509IssuerName>
    //                 <ds:X509SerialNumber>...</ds:X509SerialNumber>
    //             </ds:X509IssuerSerial>
    //         </ds:X509Data>
    //     </ds:KeyInfo>
    //     <ds:Object>
    //         <xades:QualifyingProperties xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:xades141="http://uri.etsi.org/01903/v1.4.1#" Target="#xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4">
    //             <xades:SignedProperties Id="xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-signedprops">
    //                 <xades:SignedSignatureProperties>
    //                     <xades:SigningTime>2021-06-02T07:10:13.306+02:00</xades:SigningTime>
    //                     <xades:SigningCertificate>
    //                         <xades:Cert>
    //                             <xades:CertDigest>
    //                                 <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    //                                 <ds:DigestValue>...</ds:DigestValue>
    //                             </xades:CertDigest>
    //                             <xades:IssuerSerial>
    //                                 <ds:X509IssuerName>...</ds:X509IssuerName>
    //                                 <ds:X509SerialNumber>...</ds:X509SerialNumber>
    //                             </xades:IssuerSerial>
    //                         </xades:Cert>
    //                         <xades:Cert>
    //                             <xades:CertDigest>
    //                                 <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    //                                 <ds:DigestValue>...</ds:DigestValue>
    //                             </xades:CertDigest>
    //                             <xades:IssuerSerial>
    //                                 <ds:X509IssuerName>...</ds:X509IssuerName>
    //                                 <ds:X509SerialNumber>...</ds:X509SerialNumber>
    //                             </xades:IssuerSerial>
    //                         </xades:Cert>
    //                         <xades:Cert>
    //                             <xades:CertDigest>
    //                                 <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    //                                 <ds:DigestValue>...</ds:DigestValue>
    //                             </xades:CertDigest>
    //                             <xades:IssuerSerial>
    //                                 <ds:X509IssuerName>...</ds:X509IssuerName>
    //                                 <ds:X509SerialNumber>...</ds:X509SerialNumber>
    //                             </xades:IssuerSerial>
    //                         </xades:Cert>
    //                     </xades:SigningCertificate>
    //                 </xades:SignedSignatureProperties>
    //             </xades:SignedProperties>
    //         </xades:QualifyingProperties>
    //     </ds:Object>
    // </ds:Signature>
    // 

    success = TRUE;
    gen = CkXmlDSigGenW_Create();

    CkXmlDSigGenW_putSigLocation(gen,L"");
    CkXmlDSigGenW_putSigLocationMod(gen,0);
    // Note: ID's simply need to be unique values in the signed XML document.  They don't necessarily need to be GUID/UUID formatted.
    CkXmlDSigGenW_putSigId(gen,L"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4");
    CkXmlDSigGenW_putSigNamespacePrefix(gen,L"ds");
    CkXmlDSigGenW_putSigNamespaceUri(gen,L"http://www.w3.org/2000/09/xmldsig#");
    CkXmlDSigGenW_putSigValueId(gen,L"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-sigvalue");
    CkXmlDSigGenW_putSignedInfoCanonAlg(gen,L"C14N");
    CkXmlDSigGenW_putSignedInfoDigestMethod(gen,L"sha256");

    // Create an Object to be added to the Signature.
    object1 = CkXmlW_Create();
    CkXmlW_putTag(object1,L"xades:QualifyingProperties");
    CkXmlW_AddAttribute(object1,L"xmlns:xades",L"http://uri.etsi.org/01903/v1.3.2#");
    CkXmlW_AddAttribute(object1,L"xmlns:xades141",L"http://uri.etsi.org/01903/v1.4.1#");
    CkXmlW_AddAttribute(object1,L"Target",L"#xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4");
    CkXmlW_UpdateAttrAt(object1,L"xades:SignedProperties",TRUE,L"Id",L"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-signedprops");
    CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime",L"TO BE GENERATED BY CHILKAT");
    // This assumes there are 3 certs in the certificate chain:  The signing certificate, an intermediate root, and the root CA.
    // Remove the lines with "Cert[2]" if there is no intermediate root...
    CkXmlW_UpdateAttrAt(object1,L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod",TRUE,L"Algorithm",L"http://www.w3.org/2001/04/xmlenc#sha256");
    CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue",L"TO BE GENERATED BY CHILKAT");
    CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2",L"TO BE GENERATED BY CHILKAT");

    CkXmlDSigGenW_AddObject(gen,L"",CkXmlW_getXml(object1),L"",L"");

    http = CkHttpW_Create();
    bd = CkBinDataW_Create();

    url = L"https://isus.ezdrowie.gov.pl/fhir/Patient/123/_history/456";
    success = CkHttpW_QuickGetBd(http,url,bd);
    if (success != TRUE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkXmlDSigGenW_Dispose(gen);
        CkXmlW_Dispose(object1);
        CkHttpW_Dispose(http);
        CkBinDataW_Dispose(bd);
        return;
    }

    // -------- Reference 1 --------
    CkXmlDSigGenW_AddExternalBinaryRef(gen,url,bd,L"sha256",L"");
    CkXmlDSigGenW_SetRefIdAttr(gen,url,L"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref0");

    // -------- Reference 2 --------
    CkBinDataW_Clear(bd);
    url = L"https://isus.ezdrowie.gov.pl/fhir/Encounter/789/_history/1";
    success = CkHttpW_QuickGetBd(http,url,bd);
    if (success != TRUE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkXmlDSigGenW_Dispose(gen);
        CkXmlW_Dispose(object1);
        CkHttpW_Dispose(http);
        CkBinDataW_Dispose(bd);
        return;
    }

    CkXmlDSigGenW_AddExternalBinaryRef(gen,url,bd,L"sha256",L"");
    CkXmlDSigGenW_SetRefIdAttr(gen,url,L"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref1");

    // -------- Reference 3 --------
    CkBinDataW_Clear(bd);
    url = L"https://isus.ezdrowie.gov.pl/fhir/Condition/123456/_history/1";
    success = CkHttpW_QuickGetBd(http,url,bd);
    if (success != TRUE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkXmlDSigGenW_Dispose(gen);
        CkXmlW_Dispose(object1);
        CkHttpW_Dispose(http);
        CkBinDataW_Dispose(bd);
        return;
    }

    CkXmlDSigGenW_AddExternalBinaryRef(gen,url,bd,L"sha256",L"");
    CkXmlDSigGenW_SetRefIdAttr(gen,url,L"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref2");

    // -------- Reference 4 --------
    CkBinDataW_Clear(bd);
    url = L"https://isus.ezdrowie.gov.pl/fhir/Procedure/123456/_history/1";
    success = CkHttpW_QuickGetBd(http,url,bd);
    if (success != TRUE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkXmlDSigGenW_Dispose(gen);
        CkXmlW_Dispose(object1);
        CkHttpW_Dispose(http);
        CkBinDataW_Dispose(bd);
        return;
    }

    CkXmlDSigGenW_AddExternalBinaryRef(gen,url,bd,L"sha256",L"");
    CkXmlDSigGenW_SetRefIdAttr(gen,url,L"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-ref3");

    // -------- Reference 5 --------
    CkXmlDSigGenW_AddObjectRef(gen,L"xmldsig-2dde88a8-def0-43e4-8570-b031015a46b4-signedprops",L"sha256",L"C14N",L"",L"http://uri.etsi.org/01903#SignedProperties");

    // 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));
        CkXmlDSigGenW_Dispose(gen);
        CkXmlW_Dispose(object1);
        CkHttpW_Dispose(http);
        CkBinDataW_Dispose(bd);
        CkCertW_Dispose(cert);
        return;
    }

    CkXmlDSigGenW_SetX509Cert(gen,cert,TRUE);

    CkXmlDSigGenW_putKeyInfoType(gen,L"X509Data");
    CkXmlDSigGenW_putX509Type(gen,L"IssuerSerial,SubjectName,Certificate");

    // This will be an enveloping signature where the Signature element
    // is the XML document root, the signed data is contained within Object
    // tag(s) within the Signature.
    // Therefore, pass an empty sbXml to CreateXmlDsigSb.
    sbXml = CkStringBuilderW_Create();

    CkXmlDSigGenW_putBehaviors(gen,L"IndentedSignature");

    // Sign the XML...
    success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml);
    if (success != TRUE) {
        wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen));
        CkXmlDSigGenW_Dispose(gen);
        CkXmlW_Dispose(object1);
        CkHttpW_Dispose(http);
        CkBinDataW_Dispose(bd);
        CkCertW_Dispose(cert);
        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 signature we just produced...
    verifier = CkXmlDSigW_Create();
    success = CkXmlDSigW_LoadSignatureSb(verifier,sbXml);
    if (success != TRUE) {
        wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier));
        CkXmlDSigGenW_Dispose(gen);
        CkXmlW_Dispose(object1);
        CkHttpW_Dispose(http);
        CkBinDataW_Dispose(bd);
        CkCertW_Dispose(cert);
        CkStringBuilderW_Dispose(sbXml);
        CkXmlDSigW_Dispose(verifier);
        return;
    }

    verified = CkXmlDSigW_VerifySignature(verifier,TRUE);
    if (verified != TRUE) {
        wprintf(L"%s\n",CkXmlDSigW_lastErrorText(verifier));
        CkXmlDSigGenW_Dispose(gen);
        CkXmlW_Dispose(object1);
        CkHttpW_Dispose(http);
        CkBinDataW_Dispose(bd);
        CkCertW_Dispose(cert);
        CkStringBuilderW_Dispose(sbXml);
        CkXmlDSigW_Dispose(verifier);
        return;
    }

    wprintf(L"This signature was successfully verified.\n");


    CkXmlDSigGenW_Dispose(gen);
    CkXmlW_Dispose(object1);
    CkHttpW_Dispose(http);
    CkBinDataW_Dispose(bd);
    CkCertW_Dispose(cert);
    CkStringBuilderW_Dispose(sbXml);
    CkXmlDSigW_Dispose(verifier);

    }