Sample code for 30+ languages & platforms
Unicode C

XML-DSig Add EncapsulatedTimestamp when Signing

See more XML Digital Signatures Examples

Demonstrates how to add an EncapsulatedTimestamp at the time of creating an XML signature.

Note: This example requires Chilkat v9.5.0.90 or greater.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkXmlW xmlToSign;
    HCkXmlDSigGenW gen;
    HCkXmlW object1;
    HCkXmlW xml1;
    HCkXmlW xml2;
    HCkCertW cert;
    HCkStringBuilderW sbXml;
    HCkJsonObjectW jsonTsa;
    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.

    success = TRUE;

    // Create the folllowing XML to be signed...

    // Use this online tool to generate code from sample XML: 
    // Generate Code to Create XML

    // <p:FatturaElettronica xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" versione="FPR12" xsi:schemaLocation="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2 http://www.fatturapa.gov.it/export/fatturazione/sdi/fatturapa/v1.2/Schema_del_file_xml_FatturaPA_versione_1.2.xsd">
    //     <FatturaElettronicaHeader>
    //         <DatiTrasmissione>
    //             <IdTrasmittente>
    //                 <IdPaese>IT</IdPaese>
    //                 <IdCodice>01234567890</IdCodice>
    //             </IdTrasmittente>
    //             <ProgressivoInvio>00001</ProgressivoInvio>
    //             <FormatoTrasmissione>FPR12</FormatoTrasmissione>
    //             <CodiceDestinatario>0000000</CodiceDestinatario>
    //             <PECDestinatario>betagamma@pec.it</PECDestinatario>
    //         </DatiTrasmissione>
    //         <CedentePrestatore>
    //             <DatiAnagrafici>
    //                 <IdFiscaleIVA>
    //                     <IdPaese>IT</IdPaese>
    //                     <IdCodice>01234567890</IdCodice>
    //                 </IdFiscaleIVA>
    //                 <Anagrafica>
    //                     <Denominazione>ALPHA SRL</Denominazione>
    //                 </Anagrafica>
    //                 <RegimeFiscale>RF19</RegimeFiscale>
    //             </DatiAnagrafici>
    //             <Sede>
    //                 <Indirizzo>VIALE ROMA 543</Indirizzo>
    //                 <CAP>07100</CAP>
    //                 <Comune>SASSARI</Comune>
    //                 <Provincia>SS</Provincia>
    //                 <Nazione>IT</Nazione>
    //             </Sede>
    //         </CedentePrestatore>
    //         <CessionarioCommittente>
    //             <DatiAnagrafici>
    //                 <CodiceFiscale>09876543210</CodiceFiscale>
    //                 <Anagrafica>
    //                     <Denominazione>AMMINISTRAZIONE BETA</Denominazione>
    //                 </Anagrafica>
    //             </DatiAnagrafici>
    //             <Sede>
    //                 <Indirizzo>VIA TORINO 38-B</Indirizzo>
    //                 <CAP>00145</CAP>
    // 
    //                 <Comune>ROMA</Comune>
    //                 <Provincia>RM</Provincia>
    //                 <Nazione>IT</Nazione>
    //             </Sede>
    //             <StabileOrganizzazione>
    //                 <Indirizzo>VIA CASELLE</Indirizzo>
    //                 <NumeroCivico>4/D</NumeroCivico>
    //                 <CAP>25027</CAP>
    //                 <Comune>QUINZANO D'OGLIO</Comune>
    //                 <Provincia>BS</Provincia>
    //                 <Nazione>IT</Nazione>
    //             </StabileOrganizzazione>
    //             <RappresentanteFiscale>
    //                 <IdFiscaleIVA>
    //                     <IdPaese>DE</IdPaese>
    //                     <IdCodice>DE12345</IdCodice>
    //                 </IdFiscaleIVA>
    //                 <Denominazione>RFCC - DENOMINAZIONE</Denominazione>
    //             </RappresentanteFiscale>
    //         </CessionarioCommittente>
    //     </FatturaElettronicaHeader>
    //     <FatturaElettronicaBody>
    //         <DatiGenerali>
    //             <DatiGeneraliDocumento>
    //                 <TipoDocumento>TD01</TipoDocumento>
    //                 <Divisa>EUR</Divisa>
    //                 <Data>2022-03-03</Data>
    //                 <Numero>123</Numero>
    //                 <Causale>LA FATTURA FA RIFERIMENTO AD UNA OPERAZIONE AAAA</Causale>
    //                 <Causale>SEGUE DESCRIZIONE CAUSALE NEL CASO IN CUI NON SIANO STATI SUFFICIENTI 200 CARATTERI AAAAAAAAAAA BBBBBBBBBBBBBBBBB</Causale>
    //             </DatiGeneraliDocumento>
    //             <DatiOrdineAcquisto>
    //                 <RiferimentoNumeroLinea>1</RiferimentoNumeroLinea>
    //                 <IdDocumento>66685</IdDocumento>
    //                 <NumItem>1</NumItem>
    //                 <CodiceCUP>123abc</CodiceCUP>
    //                 <CodiceCIG>456def</CodiceCIG>
    //             </DatiOrdineAcquisto>
    //             <DatiContratto>
    //                 <RiferimentoNumeroLinea>1</RiferimentoNumeroLinea>
    //                 <IdDocumento>123</IdDocumento>
    //                 <Data>2022-01-01</Data>
    //                 <NumItem>5</NumItem>
    //                 <CodiceCUP>123abc</CodiceCUP>
    //                 <CodiceCIG>456def</CodiceCIG>
    //             </DatiContratto>
    //             <DatiConvenzione>
    //                 <RiferimentoNumeroLinea>1</RiferimentoNumeroLinea>
    //                 <IdDocumento>456</IdDocumento>
    //                 <NumItem>5</NumItem>
    //                 <CodiceCUP>123abc</CodiceCUP>
    //                 <CodiceCIG>456def</CodiceCIG>
    //             </DatiConvenzione>
    //             <DatiRicezione>
    //                 <RiferimentoNumeroLinea>1</RiferimentoNumeroLinea>
    //                 <IdDocumento>789</IdDocumento>
    //                 <NumItem>5</NumItem>
    //                 <CodiceCUP>123abc</CodiceCUP>
    //                 <CodiceCIG>456def</CodiceCIG>
    //             </DatiRicezione>
    //             <DatiTrasporto>
    //                 <DatiAnagraficiVettore>
    //                     <IdFiscaleIVA>
    //                         <IdPaese>IT</IdPaese>
    //                         <IdCodice>24681012141</IdCodice>
    //                     </IdFiscaleIVA>
    //                     <Anagrafica>
    //                         <Denominazione>Trasporto spa</Denominazione>
    //                     </Anagrafica>
    //                 </DatiAnagraficiVettore>
    //                 <DataOraConsegna>2022-03-01T14:26:39</DataOraConsegna>
    //             </DatiTrasporto>
    //         </DatiGenerali>
    //         <DatiBeniServizi>
    //             <DettaglioLinee>
    //                 <NumeroLinea>1</NumeroLinea>
    //                 <Descrizione>DESCRIZIONE DELLA FORNITURA</Descrizione>
    //                 <Quantita>5.005</Quantita>
    //                 <PrezzoUnitario>1.00</PrezzoUnitario>
    //                 <PrezzoTotale>5.00</PrezzoTotale>
    //                 <AliquotaIVA>22.00</AliquotaIVA>
    //             </DettaglioLinee>
    //             <DatiRiepilogo>
    //                 <AliquotaIVA>22.00</AliquotaIVA>
    //                 <ImponibileImporto>5.00</ImponibileImporto>
    //                 <Imposta>1.10</Imposta>
    //                 <EsigibilitaIVA>I</EsigibilitaIVA>
    //             </DatiRiepilogo>
    //         </DatiBeniServizi>
    //         <DatiPagamento>
    //             <CondizioniPagamento>TP01</CondizioniPagamento>
    //             <DettaglioPagamento>
    //                 <ModalitaPagamento>MP01</ModalitaPagamento>
    //                 <DataScadenzaPagamento>2017-02-18</DataScadenzaPagamento>
    //                 <ImportoPagamento>6.10</ImportoPagamento>
    //             </DettaglioPagamento>
    //         </DatiPagamento>
    //     </FatturaElettronicaBody>
    // </p:FatturaElettronica>

    xmlToSign = CkXmlW_Create();
    CkXmlW_putTag(xmlToSign,L"p:FatturaElettronica");
    CkXmlW_AddAttribute(xmlToSign,L"xmlns:ds",L"http://www.w3.org/2000/09/xmldsig#");
    CkXmlW_AddAttribute(xmlToSign,L"xmlns:p",L"http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2");
    CkXmlW_AddAttribute(xmlToSign,L"xmlns:xsi",L"http://www.w3.org/2001/XMLSchema-instance");
    CkXmlW_AddAttribute(xmlToSign,L"versione",L"FPR12");
    CkXmlW_AddAttribute(xmlToSign,L"xsi:schemaLocation",L"http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2 http://www.fatturapa.gov.it/export/fatturazione/sdi/fatturapa/v1.2/Schema_del_file_xml_FatturaPA_versione_1.2.xsd");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|DatiTrasmissione|IdTrasmittente|IdPaese",L"IT");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|DatiTrasmissione|IdTrasmittente|IdCodice",L"01234567890");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|DatiTrasmissione|ProgressivoInvio",L"00001");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|DatiTrasmissione|FormatoTrasmissione",L"FPR12");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|DatiTrasmissione|CodiceDestinatario",L"0000000");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|DatiTrasmissione|PECDestinatario",L"betagamma@pec.it");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|IdFiscaleIVA|IdPaese",L"IT");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|IdFiscaleIVA|IdCodice",L"01234567890");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|Anagrafica|Denominazione",L"ALPHA SRL");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|RegimeFiscale",L"RF19");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CedentePrestatore|Sede|Indirizzo",L"VIALE ROMA 543");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CedentePrestatore|Sede|CAP",L"07100");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CedentePrestatore|Sede|Comune",L"SASSARI");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CedentePrestatore|Sede|Provincia",L"SS");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CedentePrestatore|Sede|Nazione",L"IT");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CessionarioCommittente|DatiAnagrafici|CodiceFiscale",L"09876543210");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CessionarioCommittente|DatiAnagrafici|Anagrafica|Denominazione",L"AMMINISTRAZIONE BETA");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CessionarioCommittente|Sede|Indirizzo",L"VIA TORINO 38-B");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CessionarioCommittente|Sede|CAP",L"00145");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CessionarioCommittente|Sede|Comune",L"ROMA");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CessionarioCommittente|Sede|Provincia",L"RM");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CessionarioCommittente|Sede|Nazione",L"IT");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Indirizzo",L"VIA CASELLE");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|NumeroCivico",L"4/D");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|CAP",L"25027");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Comune",L"QUINZANO D'OGLIO");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Provincia",L"BS");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Nazione",L"IT");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CessionarioCommittente|RappresentanteFiscale|IdFiscaleIVA|IdPaese",L"DE");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CessionarioCommittente|RappresentanteFiscale|IdFiscaleIVA|IdCodice",L"DE12345");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaHeader|CessionarioCommittente|RappresentanteFiscale|Denominazione",L"RFCC - DENOMINAZIONE");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|TipoDocumento",L"TD01");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Divisa",L"EUR");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Data",L"2022-03-03");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Numero",L"123");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Causale",L"LA FATTURA FA RIFERIMENTO AD UNA OPERAZIONE AAAA");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Causale[1]",L"SEGUE DESCRIZIONE CAUSALE NEL CASO IN CUI NON SIANO STATI SUFFICIENTI 200 CARATTERI AAAAAAAAAAA BBBBBBBBBBBBBBBBB");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|RiferimentoNumeroLinea",L"1");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|IdDocumento",L"66685");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|NumItem",L"1");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|CodiceCUP",L"123abc");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|CodiceCIG",L"456def");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiContratto|RiferimentoNumeroLinea",L"1");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiContratto|IdDocumento",L"123");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiContratto|Data",L"2022-01-01");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiContratto|NumItem",L"5");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiContratto|CodiceCUP",L"123abc");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiContratto|CodiceCIG",L"456def");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|RiferimentoNumeroLinea",L"1");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|IdDocumento",L"456");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|NumItem",L"5");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|CodiceCUP",L"123abc");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|CodiceCIG",L"456def");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiRicezione|RiferimentoNumeroLinea",L"1");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiRicezione|IdDocumento",L"789");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiRicezione|NumItem",L"5");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiRicezione|CodiceCUP",L"123abc");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiRicezione|CodiceCIG",L"456def");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DatiAnagraficiVettore|IdFiscaleIVA|IdPaese",L"IT");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DatiAnagraficiVettore|IdFiscaleIVA|IdCodice",L"24681012141");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DatiAnagraficiVettore|Anagrafica|Denominazione",L"Trasporto spa");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DataOraConsegna",L"2022-03-01T14:26:39");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|NumeroLinea",L"1");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|Descrizione",L"DESCRIZIONE DELLA FORNITURA");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|Quantita",L"5.005");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|PrezzoUnitario",L"1.00");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|PrezzoTotale",L"5.00");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|AliquotaIVA",L"22.00");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|AliquotaIVA",L"22.00");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|ImponibileImporto",L"5.00");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|Imposta",L"1.10");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|EsigibilitaIVA",L"I");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiPagamento|CondizioniPagamento",L"TP01");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiPagamento|DettaglioPagamento|ModalitaPagamento",L"MP01");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiPagamento|DettaglioPagamento|DataScadenzaPagamento",L"2017-02-18");
    CkXmlW_UpdateChildContent(xmlToSign,L"FatturaElettronicaBody|DatiPagamento|DettaglioPagamento|ImportoPagamento",L"6.10");

    gen = CkXmlDSigGenW_Create();

    CkXmlDSigGenW_putSigLocation(gen,L"p:FatturaElettronica");
    CkXmlDSigGenW_putSigLocationMod(gen,0);
    CkXmlDSigGenW_putSigId(gen,L"signature-5580-7534-6530-8286");
    CkXmlDSigGenW_AddSignatureNamespace(gen,L"xadesv1410",L"http://uri.etsi.org/01903/v1.4.1");
    CkXmlDSigGenW_AddSignatureNamespace(gen,L"xades",L"http://uri.etsi.org/01903/v1.3.2#");
    // xmlRoot tag = p:FatturaElettronica
    // has xmlns:ds!
    CkXmlDSigGenW_putSigValueId(gen,L"signature-value-5957-3819-1360-0727");
    CkXmlDSigGenW_putSignedInfoCanonAlg(gen,L"EXCL_C14N");
    CkXmlDSigGenW_putSignedInfoDigestMethod(gen,L"sha256");

    // Set the KeyInfoId before adding references..
    CkXmlDSigGenW_putKeyInfoId(gen,L"key-info-7422-1087-7530-8569");

    // Create the following signed properties object to be added to the signature:

    // Use this online tool to generate code from sample XML: 
    // Generate Code to Create XML

    // <xades:QualifyingProperties Target="#signature-5580-7534-6530-8286">
    //     <xades:SignedProperties Id="signed-properties-1545-8800-2160-3000">
    //         <xades:SignedSignatureProperties>
    //             <xades:SigningTime>TO BE GENERATED BY CHILKAT</xades:SigningTime>
    //             <xades:SigningCertificate>
    //                 <xades:Cert>
    //                     <xades:CertDigest>
    //                         <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    //                         <ds:DigestValue>TO BE GENERATED BY CHILKAT</ds:DigestValue>
    // 
    //                     </xades:CertDigest>
    //                     <xades:IssuerSerial>
    //                         <ds:X509IssuerName>TO BE GENERATED BY CHILKAT</ds:X509IssuerName>
    //                         <ds:X509SerialNumber>TO BE GENERATED BY CHILKAT</ds:X509SerialNumber>
    //                     </xades:IssuerSerial>
    //                 </xades:Cert>
    //             </xades:SigningCertificate>
    //         </xades:SignedSignatureProperties>
    //     </xades:SignedProperties>
    //     <xades:UnsignedProperties>
    //         <xades:UnsignedSignatureProperties>
    //             <xades:SignatureTimeStamp Id="signature-timestamp-5561-8212-3316-5191">
    //                 <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    //                 <xades:EncapsulatedTimeStamp Encoding="http://uri.etsi.org/01903/v1.2.2#DER">TO BE GENERATED BY CHILKAT</xades:EncapsulatedTimeStamp>
    //             </xades:SignatureTimeStamp>
    //         </xades:UnsignedSignatureProperties>
    //     </xades:UnsignedProperties>
    // </xades:QualifyingProperties>

    // Note: Chilkat will automatically fill in the values marked as "TO BE GENERATED BY CHILKAT" at the time of signing.
    // The EncapsulatedTimestamp will be automatically generated.

    object1 = CkXmlW_Create();
    CkXmlW_putTag(object1,L"xades:QualifyingProperties");
    CkXmlW_AddAttribute(object1,L"Target",L"#signature-5580-7534-6530-8286");
    CkXmlW_UpdateAttrAt(object1,L"xades:SignedProperties",TRUE,L"Id",L"signed-properties-1545-8800-2160-3000");
    CkXmlW_UpdateChildContent(object1,L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime",L"TO BE GENERATED BY CHILKAT");
    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");
    CkXmlW_UpdateAttrAt(object1,L"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp",TRUE,L"Id",L"signature-timestamp-5561-8212-3316-5191");
    CkXmlW_UpdateAttrAt(object1,L"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|ds:CanonicalizationMethod",TRUE,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#");
    CkXmlW_UpdateAttrAt(object1,L"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp",TRUE,L"Encoding",L"http://uri.etsi.org/01903/v1.2.2#DER");
    CkXmlW_UpdateChildContent(object1,L"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp",L"TO BE GENERATED BY CHILKAT");

    CkXmlDSigGenW_AddObject(gen,L"signature-object-8923-2359-1722-2161",CkXmlW_getXml(object1),L"",L"");

    // -------- Reference 1 --------
    // <ds:Transforms>
    //     <ds:Transform Algorithm="http://www.w3.org/2002/06/xmldsig-filter2">
    //         <dsig-xpath:XPath xmlns:dsig-xpath="http://www.w3.org/2002/06/xmldsig-filter2" Filter="subtract">/descendant::ds:Signature</dsig-xpath:XPath>
    //     </ds:Transform>
    //     <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    // </ds:Transforms>

    xml1 = CkXmlW_Create();
    CkXmlW_putTag(xml1,L"ds:Transforms");
    CkXmlW_UpdateAttrAt(xml1,L"ds:Transform",TRUE,L"Algorithm",L"http://www.w3.org/2002/06/xmldsig-filter2");
    CkXmlW_UpdateAttrAt(xml1,L"ds:Transform|dsig-xpath:XPath",TRUE,L"xmlns:dsig-xpath",L"http://www.w3.org/2002/06/xmldsig-filter2");
    CkXmlW_UpdateAttrAt(xml1,L"ds:Transform|dsig-xpath:XPath",TRUE,L"Filter",L"subtract");
    CkXmlW_UpdateChildContent(xml1,L"ds:Transform|dsig-xpath:XPath",L"/descendant::ds:Signature");
    CkXmlW_UpdateAttrAt(xml1,L"ds:Transform[1]",TRUE,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#");

    CkXmlDSigGenW_AddSameDocRef2(gen,L"",L"sha256",xml1,L"");

    // -------- Reference 2 --------
    // <ds:Transforms>
    //     <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    // </ds:Transforms>

    xml2 = CkXmlW_Create();
    CkXmlW_putTag(xml2,L"ds:Transforms");
    CkXmlW_UpdateAttrAt(xml2,L"ds:Transform",TRUE,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#");

    CkXmlDSigGenW_AddObjectRef2(gen,L"signed-properties-1545-8800-2160-3000",L"sha256",xml2,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));
        CkXmlW_Dispose(xmlToSign);
        CkXmlDSigGenW_Dispose(gen);
        CkXmlW_Dispose(object1);
        CkXmlW_Dispose(xml1);
        CkXmlW_Dispose(xml2);
        CkCertW_Dispose(cert);
        return;
    }

    CkXmlDSigGenW_SetX509Cert(gen,cert,TRUE);

    CkXmlDSigGenW_putKeyInfoType(gen,L"X509Data+KeyValue");
    CkXmlDSigGenW_putX509Type(gen,L"Certificate");

    // Load XML to be signed...
    sbXml = CkStringBuilderW_Create();
    CkXmlW_GetXmlSb(xmlToSign,sbXml);

    CkXmlDSigGenW_putBehaviors(gen,L"IndentedSignature,OmitAlreadyDefinedSigNamespace");

    // -------------------------------------------------------------------------------------------
    // To have the EncapsulatedTimeStamp automatically added, we only need to do 2 things.
    // 1) Add the <xades:EncapsulatedTimeStamp Encoding="http://uri.etsi.org/01903/v1.2.2#DER">TO BE GENERATED BY CHILKAT</xades:EncapsulatedTimeStamp>
    //    to the unsigned properties.
    // 2) Specify the TSA URL (Timestamping Authority URL).
    //    Here we specify the TSA URL:
    // -------------------------------------------------------------------------------------------

    jsonTsa = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(jsonTsa,L"timestampToken.tsaUrl",L"http://timestamp.digicert.com");
    CkJsonObjectW_UpdateBool(jsonTsa,L"timestampToken.requestTsaCert",TRUE);
    CkXmlDSigGenW_SetTsa(gen,jsonTsa);

    // Sign the XML...
    success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml);
    if (success != TRUE) {
        wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen));
        CkXmlW_Dispose(xmlToSign);
        CkXmlDSigGenW_Dispose(gen);
        CkXmlW_Dispose(object1);
        CkXmlW_Dispose(xml1);
        CkXmlW_Dispose(xml2);
        CkCertW_Dispose(cert);
        CkStringBuilderW_Dispose(sbXml);
        CkJsonObjectW_Dispose(jsonTsa);
        return;
    }

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

    // ----------------------------------------
    // 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);
        CkXmlW_Dispose(object1);
        CkXmlW_Dispose(xml1);
        CkXmlW_Dispose(xml2);
        CkCertW_Dispose(cert);
        CkStringBuilderW_Dispose(sbXml);
        CkJsonObjectW_Dispose(jsonTsa);
        CkXmlDSigW_Dispose(verifier);
        return;
    }

    // Add "VerifyEncapsulatedTimeStamp" to the UncommonOptions to also verify any EncapsulatedTimeStamps
    CkXmlDSigW_putUncommonOptions(verifier,L"VerifyEncapsulatedTimeStamp");

    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);
            CkXmlW_Dispose(object1);
            CkXmlW_Dispose(xml1);
            CkXmlW_Dispose(xml2);
            CkCertW_Dispose(cert);
            CkStringBuilderW_Dispose(sbXml);
            CkJsonObjectW_Dispose(jsonTsa);
            CkXmlDSigW_Dispose(verifier);
            return;
        }

        verifyIdx = verifyIdx + 1;
    }

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


    CkXmlW_Dispose(xmlToSign);
    CkXmlDSigGenW_Dispose(gen);
    CkXmlW_Dispose(object1);
    CkXmlW_Dispose(xml1);
    CkXmlW_Dispose(xml2);
    CkCertW_Dispose(cert);
    CkStringBuilderW_Dispose(sbXml);
    CkJsonObjectW_Dispose(jsonTsa);
    CkXmlDSigW_Dispose(verifier);

    }