DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoXmlToSign
Handle hoGen
Handle hoObject1
Variant vXml1
Handle hoXml1
Variant vXml2
Handle hoXml2
Variant vCert
Handle hoCert
Variant vSbXml
Handle hoSbXml
Variant vJsonTsa
Handle hoJsonTsa
Handle hoVerifier
Integer iNumSigs
Integer iVerifyIdx
Boolean iVerified
String sTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Move True To iSuccess
// 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>
Get Create (RefClass(cComChilkatXml)) To hoXmlToSign
If (Not(IsComObjectCreated(hoXmlToSign))) Begin
Send CreateComObject of hoXmlToSign
End
Set ComTag Of hoXmlToSign To "p:FatturaElettronica"
Get ComAddAttribute Of hoXmlToSign "xmlns:ds" "http://www.w3.org/2000/09/xmldsig#" To iSuccess
Get ComAddAttribute Of hoXmlToSign "xmlns:p" "http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" To iSuccess
Get ComAddAttribute Of hoXmlToSign "xmlns:xsi" "http://www.w3.org/2001/XMLSchema-instance" To iSuccess
Get ComAddAttribute Of hoXmlToSign "versione" "FPR12" To iSuccess
Get ComAddAttribute Of hoXmlToSign "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" To iSuccess
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|DatiTrasmissione|IdTrasmittente|IdPaese" "IT"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|DatiTrasmissione|IdTrasmittente|IdCodice" "01234567890"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|DatiTrasmissione|ProgressivoInvio" "00001"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|DatiTrasmissione|FormatoTrasmissione" "FPR12"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|DatiTrasmissione|CodiceDestinatario" "0000000"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|DatiTrasmissione|PECDestinatario" "betagamma@pec.it"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|IdFiscaleIVA|IdPaese" "IT"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|IdFiscaleIVA|IdCodice" "01234567890"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|Anagrafica|Denominazione" "ALPHA SRL"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|RegimeFiscale" "RF19"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CedentePrestatore|Sede|Indirizzo" "VIALE ROMA 543"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CedentePrestatore|Sede|CAP" "07100"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CedentePrestatore|Sede|Comune" "SASSARI"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CedentePrestatore|Sede|Provincia" "SS"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CedentePrestatore|Sede|Nazione" "IT"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CessionarioCommittente|DatiAnagrafici|CodiceFiscale" "09876543210"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CessionarioCommittente|DatiAnagrafici|Anagrafica|Denominazione" "AMMINISTRAZIONE BETA"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CessionarioCommittente|Sede|Indirizzo" "VIA TORINO 38-B"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CessionarioCommittente|Sede|CAP" "00145"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CessionarioCommittente|Sede|Comune" "ROMA"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CessionarioCommittente|Sede|Provincia" "RM"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CessionarioCommittente|Sede|Nazione" "IT"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Indirizzo" "VIA CASELLE"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|NumeroCivico" "4/D"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|CAP" "25027"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Comune" "QUINZANO D'OGLIO"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Provincia" "BS"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Nazione" "IT"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CessionarioCommittente|RappresentanteFiscale|IdFiscaleIVA|IdPaese" "DE"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CessionarioCommittente|RappresentanteFiscale|IdFiscaleIVA|IdCodice" "DE12345"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaHeader|CessionarioCommittente|RappresentanteFiscale|Denominazione" "RFCC - DENOMINAZIONE"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|TipoDocumento" "TD01"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Divisa" "EUR"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Data" "2022-03-03"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Numero" "123"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Causale" "LA FATTURA FA RIFERIMENTO AD UNA OPERAZIONE AAAA"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Causale[1]" "SEGUE DESCRIZIONE CAUSALE NEL CASO IN CUI NON SIANO STATI SUFFICIENTI 200 CARATTERI AAAAAAAAAAA BBBBBBBBBBBBBBBBB"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|RiferimentoNumeroLinea" "1"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|IdDocumento" "66685"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|NumItem" "1"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|CodiceCUP" "123abc"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|CodiceCIG" "456def"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiContratto|RiferimentoNumeroLinea" "1"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiContratto|IdDocumento" "123"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiContratto|Data" "2022-01-01"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiContratto|NumItem" "5"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiContratto|CodiceCUP" "123abc"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiContratto|CodiceCIG" "456def"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiConvenzione|RiferimentoNumeroLinea" "1"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiConvenzione|IdDocumento" "456"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiConvenzione|NumItem" "5"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiConvenzione|CodiceCUP" "123abc"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiConvenzione|CodiceCIG" "456def"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiRicezione|RiferimentoNumeroLinea" "1"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiRicezione|IdDocumento" "789"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiRicezione|NumItem" "5"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiRicezione|CodiceCUP" "123abc"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiRicezione|CodiceCIG" "456def"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DatiAnagraficiVettore|IdFiscaleIVA|IdPaese" "IT"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DatiAnagraficiVettore|IdFiscaleIVA|IdCodice" "24681012141"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DatiAnagraficiVettore|Anagrafica|Denominazione" "Trasporto spa"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DataOraConsegna" "2022-03-01T14:26:39"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|NumeroLinea" "1"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|Descrizione" "DESCRIZIONE DELLA FORNITURA"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|Quantita" "5.005"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|PrezzoUnitario" "1.00"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|PrezzoTotale" "5.00"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|AliquotaIVA" "22.00"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|AliquotaIVA" "22.00"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|ImponibileImporto" "5.00"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|Imposta" "1.10"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|EsigibilitaIVA" "I"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiPagamento|CondizioniPagamento" "TP01"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiPagamento|DettaglioPagamento|ModalitaPagamento" "MP01"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiPagamento|DettaglioPagamento|DataScadenzaPagamento" "2017-02-18"
Send ComUpdateChildContent To hoXmlToSign "FatturaElettronicaBody|DatiPagamento|DettaglioPagamento|ImportoPagamento" "6.10"
Get Create (RefClass(cComChilkatXmlDSigGen)) To hoGen
If (Not(IsComObjectCreated(hoGen))) Begin
Send CreateComObject of hoGen
End
Set ComSigLocation Of hoGen To "p:FatturaElettronica"
Set ComSigLocationMod Of hoGen To 0
Set ComSigId Of hoGen To "signature-5580-7534-6530-8286"
Get ComAddSignatureNamespace Of hoGen "xadesv1410" "http://uri.etsi.org/01903/v1.4.1" To iSuccess
Get ComAddSignatureNamespace Of hoGen "xades" "http://uri.etsi.org/01903/v1.3.2#" To iSuccess
// xmlRoot tag = p:FatturaElettronica
// has xmlns:ds!
Set ComSigValueId Of hoGen To "signature-value-5957-3819-1360-0727"
Set ComSignedInfoCanonAlg Of hoGen To "EXCL_C14N"
Set ComSignedInfoDigestMethod Of hoGen To "sha256"
// Set the KeyInfoId before adding references..
Set ComKeyInfoId Of hoGen To "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.
Get Create (RefClass(cComChilkatXml)) To hoObject1
If (Not(IsComObjectCreated(hoObject1))) Begin
Send CreateComObject of hoObject1
End
Set ComTag Of hoObject1 To "xades:QualifyingProperties"
Get ComAddAttribute Of hoObject1 "Target" "#signature-5580-7534-6530-8286" To iSuccess
Get ComUpdateAttrAt Of hoObject1 "xades:SignedProperties" True "Id" "signed-properties-1545-8800-2160-3000" To iSuccess
Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime" "TO BE GENERATED BY CHILKAT"
Get ComUpdateAttrAt Of hoObject1 "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod" True "Algorithm" "http://www.w3.org/2001/04/xmlenc#sha256" To iSuccess
Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue" "TO BE GENERATED BY CHILKAT"
Send ComUpdateChildContent To hoObject1 "xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2" "TO BE GENERATED BY CHILKAT"
Get ComUpdateAttrAt Of hoObject1 "xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp" True "Id" "signature-timestamp-5561-8212-3316-5191" To iSuccess
Get ComUpdateAttrAt Of hoObject1 "xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|ds:CanonicalizationMethod" True "Algorithm" "http://www.w3.org/2001/10/xml-exc-c14n#" To iSuccess
Get ComUpdateAttrAt Of hoObject1 "xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp" True "Encoding" "http://uri.etsi.org/01903/v1.2.2#DER" To iSuccess
Send ComUpdateChildContent To hoObject1 "xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp" "TO BE GENERATED BY CHILKAT"
Get ComGetXml Of hoObject1 To sTemp1
Get ComAddObject Of hoGen "signature-object-8923-2359-1722-2161" sTemp1 "" "" To iSuccess
// -------- 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>
Get Create (RefClass(cComChilkatXml)) To hoXml1
If (Not(IsComObjectCreated(hoXml1))) Begin
Send CreateComObject of hoXml1
End
Set ComTag Of hoXml1 To "ds:Transforms"
Get ComUpdateAttrAt Of hoXml1 "ds:Transform" True "Algorithm" "http://www.w3.org/2002/06/xmldsig-filter2" To iSuccess
Get ComUpdateAttrAt Of hoXml1 "ds:Transform|dsig-xpath:XPath" True "xmlns:dsig-xpath" "http://www.w3.org/2002/06/xmldsig-filter2" To iSuccess
Get ComUpdateAttrAt Of hoXml1 "ds:Transform|dsig-xpath:XPath" True "Filter" "subtract" To iSuccess
Send ComUpdateChildContent To hoXml1 "ds:Transform|dsig-xpath:XPath" "/descendant::ds:Signature"
Get ComUpdateAttrAt Of hoXml1 "ds:Transform[1]" True "Algorithm" "http://www.w3.org/2001/10/xml-exc-c14n#" To iSuccess
Get pvComObject of hoXml1 to vXml1
Get ComAddSameDocRef2 Of hoGen "" "sha256" vXml1 "" To iSuccess
// -------- Reference 2 --------
// <ds:Transforms>
// <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
// </ds:Transforms>
Get Create (RefClass(cComChilkatXml)) To hoXml2
If (Not(IsComObjectCreated(hoXml2))) Begin
Send CreateComObject of hoXml2
End
Set ComTag Of hoXml2 To "ds:Transforms"
Get ComUpdateAttrAt Of hoXml2 "ds:Transform" True "Algorithm" "http://www.w3.org/2001/10/xml-exc-c14n#" To iSuccess
Get pvComObject of hoXml2 to vXml2
Get ComAddObjectRef2 Of hoGen "signed-properties-1545-8800-2160-3000" "sha256" vXml2 "http://uri.etsi.org/01903#SignedProperties" To iSuccess
// Provide a certificate + private key. (PFX password is test123)
Get Create (RefClass(cComChilkatCert)) To hoCert
If (Not(IsComObjectCreated(hoCert))) Begin
Send CreateComObject of hoCert
End
Get ComLoadPfxFile Of hoCert "qa_data/pfx/cert_test123.pfx" "test123" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoCert To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoCert to vCert
Get ComSetX509Cert Of hoGen vCert True To iSuccess
Set ComKeyInfoType Of hoGen To "X509Data+KeyValue"
Set ComX509Type Of hoGen To "Certificate"
// Load XML to be signed...
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbXml
If (Not(IsComObjectCreated(hoSbXml))) Begin
Send CreateComObject of hoSbXml
End
Get pvComObject of hoSbXml to vSbXml
Get ComGetXmlSb Of hoXmlToSign vSbXml To iSuccess
Set ComBehaviors Of hoGen To "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:
// -------------------------------------------------------------------------------------------
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonTsa
If (Not(IsComObjectCreated(hoJsonTsa))) Begin
Send CreateComObject of hoJsonTsa
End
Get ComUpdateString Of hoJsonTsa "timestampToken.tsaUrl" "http://timestamp.digicert.com" To iSuccess
Get ComUpdateBool Of hoJsonTsa "timestampToken.requestTsaCert" True To iSuccess
Get pvComObject of hoJsonTsa to vJsonTsa
Get ComSetTsa Of hoGen vJsonTsa To iSuccess
// Sign the XML...
Get pvComObject of hoSbXml to vSbXml
Get ComCreateXmlDSigSb Of hoGen vSbXml To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoGen To sTemp1
Showln sTemp1
Procedure_Return
End
// Save the signed XML to a file.
Get ComWriteFile Of hoSbXml "qa_output/signedXml.xml" "utf-8" False To iSuccess
// ----------------------------------------
// Verify the signatures we just produced...
Get Create (RefClass(cComChilkatXmlDSig)) To hoVerifier
If (Not(IsComObjectCreated(hoVerifier))) Begin
Send CreateComObject of hoVerifier
End
Get pvComObject of hoSbXml to vSbXml
Get ComLoadSignatureSb Of hoVerifier vSbXml To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoVerifier To sTemp1
Showln sTemp1
Procedure_Return
End
// Add "VerifyEncapsulatedTimeStamp" to the UncommonOptions to also verify any EncapsulatedTimeStamps
Set ComUncommonOptions Of hoVerifier To "VerifyEncapsulatedTimeStamp"
Get ComNumSignatures Of hoVerifier To iNumSigs
Move 0 To iVerifyIdx
While (iVerifyIdx < iNumSigs)
Set ComSelector Of hoVerifier To iVerifyIdx
Get ComVerifySignature Of hoVerifier True To iVerified
If (iVerified <> True) Begin
Get ComLastErrorText Of hoVerifier To sTemp1
Showln sTemp1
Procedure_Return
End
Move (iVerifyIdx + 1) To iVerifyIdx
Loop
Showln "All signatures were successfully verified."
End_Procedure