Sample code for 30+ languages & platforms
SQL Server

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 SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    SELECT @success = 1

    -- 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>

    DECLARE @xmlToSign int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xmlToSign OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

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

    DECLARE @gen int
    EXEC @hr = sp_OACreate 'Chilkat.XmlDSigGen', @gen OUT

    EXEC sp_OASetProperty @gen, 'SigLocation', 'p:FatturaElettronica'
    EXEC sp_OASetProperty @gen, 'SigLocationMod', 0
    EXEC sp_OASetProperty @gen, 'SigId', 'signature-5580-7534-6530-8286'
    EXEC sp_OAMethod @gen, 'AddSignatureNamespace', @success OUT, 'xadesv1410', 'http://uri.etsi.org/01903/v1.4.1'
    EXEC sp_OAMethod @gen, 'AddSignatureNamespace', @success OUT, 'xades', 'http://uri.etsi.org/01903/v1.3.2#'
    -- xmlRoot tag = p:FatturaElettronica
    -- has xmlns:ds!
    EXEC sp_OASetProperty @gen, 'SigValueId', 'signature-value-5957-3819-1360-0727'
    EXEC sp_OASetProperty @gen, 'SignedInfoCanonAlg', 'EXCL_C14N'
    EXEC sp_OASetProperty @gen, 'SignedInfoDigestMethod', 'sha256'

    -- Set the KeyInfoId before adding references..
    EXEC sp_OASetProperty @gen, 'KeyInfoId', '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.

    DECLARE @object1 int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @object1 OUT

    EXEC sp_OASetProperty @object1, 'Tag', 'xades:QualifyingProperties'
    EXEC sp_OAMethod @object1, 'AddAttribute', @success OUT, 'Target', '#signature-5580-7534-6530-8286'
    EXEC sp_OAMethod @object1, 'UpdateAttrAt', @success OUT, 'xades:SignedProperties', 1, 'Id', 'signed-properties-1545-8800-2160-3000'
    EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime', 'TO BE GENERATED BY CHILKAT'
    EXEC sp_OAMethod @object1, 'UpdateAttrAt', @success OUT, 'xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod', 1, 'Algorithm', 'http://www.w3.org/2001/04/xmlenc#sha256'
    EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue', 'TO BE GENERATED BY CHILKAT'
    EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2', 'TO BE GENERATED BY CHILKAT'
    EXEC sp_OAMethod @object1, 'UpdateAttrAt', @success OUT, 'xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp', 1, 'Id', 'signature-timestamp-5561-8212-3316-5191'
    EXEC sp_OAMethod @object1, 'UpdateAttrAt', @success OUT, 'xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|ds:CanonicalizationMethod', 1, 'Algorithm', 'http://www.w3.org/2001/10/xml-exc-c14n#'
    EXEC sp_OAMethod @object1, 'UpdateAttrAt', @success OUT, 'xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp', 1, 'Encoding', 'http://uri.etsi.org/01903/v1.2.2#DER'
    EXEC sp_OAMethod @object1, 'UpdateChildContent', NULL, 'xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp', 'TO BE GENERATED BY CHILKAT'

    EXEC sp_OAMethod @object1, 'GetXml', @sTmp0 OUT
    EXEC sp_OAMethod @gen, 'AddObject', @success OUT, 'signature-object-8923-2359-1722-2161', @sTmp0, '', ''

    -- -------- 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>

    DECLARE @xml1 int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml1 OUT

    EXEC sp_OASetProperty @xml1, 'Tag', 'ds:Transforms'
    EXEC sp_OAMethod @xml1, 'UpdateAttrAt', @success OUT, 'ds:Transform', 1, 'Algorithm', 'http://www.w3.org/2002/06/xmldsig-filter2'
    EXEC sp_OAMethod @xml1, 'UpdateAttrAt', @success OUT, 'ds:Transform|dsig-xpath:XPath', 1, 'xmlns:dsig-xpath', 'http://www.w3.org/2002/06/xmldsig-filter2'
    EXEC sp_OAMethod @xml1, 'UpdateAttrAt', @success OUT, 'ds:Transform|dsig-xpath:XPath', 1, 'Filter', 'subtract'
    EXEC sp_OAMethod @xml1, 'UpdateChildContent', NULL, 'ds:Transform|dsig-xpath:XPath', '/descendant::ds:Signature'
    EXEC sp_OAMethod @xml1, 'UpdateAttrAt', @success OUT, 'ds:Transform[1]', 1, 'Algorithm', 'http://www.w3.org/2001/10/xml-exc-c14n#'

    EXEC sp_OAMethod @gen, 'AddSameDocRef2', @success OUT, '', 'sha256', @xml1, ''

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

    DECLARE @xml2 int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml2 OUT

    EXEC sp_OASetProperty @xml2, 'Tag', 'ds:Transforms'
    EXEC sp_OAMethod @xml2, 'UpdateAttrAt', @success OUT, 'ds:Transform', 1, 'Algorithm', 'http://www.w3.org/2001/10/xml-exc-c14n#'

    EXEC sp_OAMethod @gen, 'AddObjectRef2', @success OUT, 'signed-properties-1545-8800-2160-3000', 'sha256', @xml2, 'http://uri.etsi.org/01903#SignedProperties'

    -- Provide a certificate + private key. (PFX password is test123)
    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT

    EXEC sp_OAMethod @cert, 'LoadPfxFile', @success OUT, 'qa_data/pfx/cert_test123.pfx', 'test123'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @xmlToSign
        EXEC @hr = sp_OADestroy @gen
        EXEC @hr = sp_OADestroy @object1
        EXEC @hr = sp_OADestroy @xml1
        EXEC @hr = sp_OADestroy @xml2
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END
    EXEC sp_OAMethod @gen, 'SetX509Cert', @success OUT, @cert, 1

    EXEC sp_OASetProperty @gen, 'KeyInfoType', 'X509Data+KeyValue'
    EXEC sp_OASetProperty @gen, 'X509Type', 'Certificate'

    -- Load XML to be signed...
    DECLARE @sbXml int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbXml OUT

    EXEC sp_OAMethod @xmlToSign, 'GetXmlSb', @success OUT, @sbXml

    EXEC sp_OASetProperty @gen, 'Behaviors', '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:
    -- -------------------------------------------------------------------------------------------

    DECLARE @jsonTsa int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonTsa OUT

    EXEC sp_OAMethod @jsonTsa, 'UpdateString', @success OUT, 'timestampToken.tsaUrl', 'http://timestamp.digicert.com'
    EXEC sp_OAMethod @jsonTsa, 'UpdateBool', @success OUT, 'timestampToken.requestTsaCert', 1
    EXEC sp_OAMethod @gen, 'SetTsa', @success OUT, @jsonTsa

    -- Sign the XML...
    EXEC sp_OAMethod @gen, 'CreateXmlDSigSb', @success OUT, @sbXml
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @gen, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @xmlToSign
        EXEC @hr = sp_OADestroy @gen
        EXEC @hr = sp_OADestroy @object1
        EXEC @hr = sp_OADestroy @xml1
        EXEC @hr = sp_OADestroy @xml2
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @sbXml
        EXEC @hr = sp_OADestroy @jsonTsa
        RETURN
      END

    -- Save the signed XML to a file.
    EXEC sp_OAMethod @sbXml, 'WriteFile', @success OUT, 'qa_output/signedXml.xml', 'utf-8', 0

    -- ----------------------------------------
    -- Verify the signatures we just produced...
    DECLARE @verifier int
    EXEC @hr = sp_OACreate 'Chilkat.XmlDSig', @verifier OUT

    EXEC sp_OAMethod @verifier, 'LoadSignatureSb', @success OUT, @sbXml
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @verifier, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @xmlToSign
        EXEC @hr = sp_OADestroy @gen
        EXEC @hr = sp_OADestroy @object1
        EXEC @hr = sp_OADestroy @xml1
        EXEC @hr = sp_OADestroy @xml2
        EXEC @hr = sp_OADestroy @cert
        EXEC @hr = sp_OADestroy @sbXml
        EXEC @hr = sp_OADestroy @jsonTsa
        EXEC @hr = sp_OADestroy @verifier
        RETURN
      END

    -- Add "VerifyEncapsulatedTimeStamp" to the UncommonOptions to also verify any EncapsulatedTimeStamps
    EXEC sp_OASetProperty @verifier, 'UncommonOptions', 'VerifyEncapsulatedTimeStamp'

    DECLARE @numSigs int
    EXEC sp_OAGetProperty @verifier, 'NumSignatures', @numSigs OUT
    DECLARE @verifyIdx int
    SELECT @verifyIdx = 0
    WHILE @verifyIdx < @numSigs
      BEGIN
        EXEC sp_OASetProperty @verifier, 'Selector', @verifyIdx
        DECLARE @verified int
        EXEC sp_OAMethod @verifier, 'VerifySignature', @verified OUT, 1
        IF @verified <> 1
          BEGIN
            EXEC sp_OAGetProperty @verifier, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @xmlToSign
            EXEC @hr = sp_OADestroy @gen
            EXEC @hr = sp_OADestroy @object1
            EXEC @hr = sp_OADestroy @xml1
            EXEC @hr = sp_OADestroy @xml2
            EXEC @hr = sp_OADestroy @cert
            EXEC @hr = sp_OADestroy @sbXml
            EXEC @hr = sp_OADestroy @jsonTsa
            EXEC @hr = sp_OADestroy @verifier
            RETURN
          END
        SELECT @verifyIdx = @verifyIdx + 1
      END

    PRINT 'All signatures were successfully verified.'

    EXEC @hr = sp_OADestroy @xmlToSign
    EXEC @hr = sp_OADestroy @gen
    EXEC @hr = sp_OADestroy @object1
    EXEC @hr = sp_OADestroy @xml1
    EXEC @hr = sp_OADestroy @xml2
    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @sbXml
    EXEC @hr = sp_OADestroy @jsonTsa
    EXEC @hr = sp_OADestroy @verifier


END
GO