Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) XML-DSig Add EncapsulatedTimestamp when SigningSee more XML Digital Signatures ExamplesDemonstrates how to add an EncapsulatedTimestamp at the time of creating an XML signature. Note: This example requires Chilkat v9.5.0.90 or greater.
IncludeFile "CkJsonObject.pb" IncludeFile "CkXmlDSig.pb" IncludeFile "CkXml.pb" IncludeFile "CkXmlDSigGen.pb" IncludeFile "CkStringBuilder.pb" IncludeFile "CkCert.pb" Procedure ChilkatExample() ; This example requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. success.i = 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> xmlToSign.i = CkXml::ckCreate() If xmlToSign.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::setCkTag(xmlToSign, "p:FatturaElettronica") CkXml::ckAddAttribute(xmlToSign,"xmlns:ds","http://www.w3.org/2000/09/xmldsig#") CkXml::ckAddAttribute(xmlToSign,"xmlns:p","http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2") CkXml::ckAddAttribute(xmlToSign,"xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance") CkXml::ckAddAttribute(xmlToSign,"versione","FPR12") CkXml::ckAddAttribute(xmlToSign,"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") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|DatiTrasmissione|IdTrasmittente|IdPaese","IT") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|DatiTrasmissione|IdTrasmittente|IdCodice","01234567890") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|DatiTrasmissione|ProgressivoInvio","00001") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|DatiTrasmissione|FormatoTrasmissione","FPR12") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|DatiTrasmissione|CodiceDestinatario","0000000") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|DatiTrasmissione|PECDestinatario","betagamma@pec.it") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|IdFiscaleIVA|IdPaese","IT") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|IdFiscaleIVA|IdCodice","01234567890") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|Anagrafica|Denominazione","ALPHA SRL") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|RegimeFiscale","RF19") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CedentePrestatore|Sede|Indirizzo","VIALE ROMA 543") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CedentePrestatore|Sede|CAP","07100") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CedentePrestatore|Sede|Comune","SASSARI") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CedentePrestatore|Sede|Provincia","SS") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CedentePrestatore|Sede|Nazione","IT") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CessionarioCommittente|DatiAnagrafici|CodiceFiscale","09876543210") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CessionarioCommittente|DatiAnagrafici|Anagrafica|Denominazione","AMMINISTRAZIONE BETA") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CessionarioCommittente|Sede|Indirizzo","VIA TORINO 38-B") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CessionarioCommittente|Sede|CAP","00145") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CessionarioCommittente|Sede|Comune","ROMA") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CessionarioCommittente|Sede|Provincia","RM") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CessionarioCommittente|Sede|Nazione","IT") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Indirizzo","VIA CASELLE") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|NumeroCivico","4/D") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|CAP","25027") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Comune","QUINZANO D'OGLIO") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Provincia","BS") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Nazione","IT") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CessionarioCommittente|RappresentanteFiscale|IdFiscaleIVA|IdPaese","DE") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CessionarioCommittente|RappresentanteFiscale|IdFiscaleIVA|IdCodice","DE12345") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaHeader|CessionarioCommittente|RappresentanteFiscale|Denominazione","RFCC - DENOMINAZIONE") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|TipoDocumento","TD01") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Divisa","EUR") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Data","2022-03-03") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Numero","123") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Causale","LA FATTURA FA RIFERIMENTO AD UNA OPERAZIONE AAAA") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Causale[1]","SEGUE DESCRIZIONE CAUSALE NEL CASO IN CUI NON SIANO STATI SUFFICIENTI 200 CARATTERI AAAAAAAAAAA BBBBBBBBBBBBBBBBB") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|RiferimentoNumeroLinea","1") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|IdDocumento","66685") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|NumItem","1") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|CodiceCUP","123abc") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|CodiceCIG","456def") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiContratto|RiferimentoNumeroLinea","1") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiContratto|IdDocumento","123") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiContratto|Data","2022-01-01") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiContratto|NumItem","5") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiContratto|CodiceCUP","123abc") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiContratto|CodiceCIG","456def") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|RiferimentoNumeroLinea","1") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|IdDocumento","456") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|NumItem","5") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|CodiceCUP","123abc") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|CodiceCIG","456def") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiRicezione|RiferimentoNumeroLinea","1") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiRicezione|IdDocumento","789") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiRicezione|NumItem","5") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiRicezione|CodiceCUP","123abc") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiRicezione|CodiceCIG","456def") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DatiAnagraficiVettore|IdFiscaleIVA|IdPaese","IT") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DatiAnagraficiVettore|IdFiscaleIVA|IdCodice","24681012141") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DatiAnagraficiVettore|Anagrafica|Denominazione","Trasporto spa") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DataOraConsegna","2022-03-01T14:26:39") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|NumeroLinea","1") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|Descrizione","DESCRIZIONE DELLA FORNITURA") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|Quantita","5.005") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|PrezzoUnitario","1.00") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|PrezzoTotale","5.00") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|AliquotaIVA","22.00") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|AliquotaIVA","22.00") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|ImponibileImporto","5.00") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|Imposta","1.10") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|EsigibilitaIVA","I") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiPagamento|CondizioniPagamento","TP01") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiPagamento|DettaglioPagamento|ModalitaPagamento","MP01") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiPagamento|DettaglioPagamento|DataScadenzaPagamento","2017-02-18") CkXml::ckUpdateChildContent(xmlToSign,"FatturaElettronicaBody|DatiPagamento|DettaglioPagamento|ImportoPagamento","6.10") gen.i = CkXmlDSigGen::ckCreate() If gen.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXmlDSigGen::setCkSigLocation(gen, "p:FatturaElettronica") CkXmlDSigGen::setCkSigLocationMod(gen, 0) CkXmlDSigGen::setCkSigId(gen, "signature-5580-7534-6530-8286") CkXmlDSigGen::ckAddSignatureNamespace(gen,"xadesv1410","http://uri.etsi.org/01903/v1.4.1") CkXmlDSigGen::ckAddSignatureNamespace(gen,"xades","http://uri.etsi.org/01903/v1.3.2#") ; xmlRoot tag = p:FatturaElettronica ; has xmlns:ds! CkXmlDSigGen::setCkSigValueId(gen, "signature-value-5957-3819-1360-0727") CkXmlDSigGen::setCkSignedInfoCanonAlg(gen, "EXCL_C14N") CkXmlDSigGen::setCkSignedInfoDigestMethod(gen, "sha256") ; Set the KeyInfoId before adding references.. CkXmlDSigGen::setCkKeyInfoId(gen, "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.i = CkXml::ckCreate() If object1.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::setCkTag(object1, "xades:QualifyingProperties") CkXml::ckAddAttribute(object1,"Target","#signature-5580-7534-6530-8286") CkXml::ckUpdateAttrAt(object1,"xades:SignedProperties",1,"Id","signed-properties-1545-8800-2160-3000") CkXml::ckUpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime","TO BE GENERATED BY CHILKAT") CkXml::ckUpdateAttrAt(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestMethod",1,"Algorithm","http://www.w3.org/2001/04/xmlenc#sha256") CkXml::ckUpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestValue","TO BE GENERATED BY CHILKAT") CkXml::ckUpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509IssuerName","TO BE GENERATED BY CHILKAT") CkXml::ckUpdateChildContent(object1,"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509SerialNumber","TO BE GENERATED BY CHILKAT") CkXml::ckUpdateAttrAt(object1,"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp",1,"Id","signature-timestamp-5561-8212-3316-5191") CkXml::ckUpdateAttrAt(object1,"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|ds:CanonicalizationMethod",1,"Algorithm","http://www.w3.org/2001/10/xml-exc-c14n#") CkXml::ckUpdateAttrAt(object1,"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp",1,"Encoding","http://uri.etsi.org/01903/v1.2.2#DER") CkXml::ckUpdateChildContent(object1,"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp","TO BE GENERATED BY CHILKAT") CkXmlDSigGen::ckAddObject(gen,"signature-object-8923-2359-1722-2161",CkXml::ckGetXml(object1),"","") ; -------- 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.i = CkXml::ckCreate() If xml1.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::setCkTag(xml1, "ds:Transforms") CkXml::ckUpdateAttrAt(xml1,"ds:Transform",1,"Algorithm","http://www.w3.org/2002/06/xmldsig-filter2") CkXml::ckUpdateAttrAt(xml1,"ds:Transform|dsig-xpath:XPath",1,"xmlns:dsig-xpath","http://www.w3.org/2002/06/xmldsig-filter2") CkXml::ckUpdateAttrAt(xml1,"ds:Transform|dsig-xpath:XPath",1,"Filter","subtract") CkXml::ckUpdateChildContent(xml1,"ds:Transform|dsig-xpath:XPath","/descendant::ds:Signature") CkXml::ckUpdateAttrAt(xml1,"ds:Transform[1]",1,"Algorithm","http://www.w3.org/2001/10/xml-exc-c14n#") CkXmlDSigGen::ckAddSameDocRef2(gen,"","sha256",xml1,"") ; -------- Reference 2 -------- ; <ds:Transforms> ; <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> ; </ds:Transforms> xml2.i = CkXml::ckCreate() If xml2.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::setCkTag(xml2, "ds:Transforms") CkXml::ckUpdateAttrAt(xml2,"ds:Transform",1,"Algorithm","http://www.w3.org/2001/10/xml-exc-c14n#") CkXmlDSigGen::ckAddObjectRef2(gen,"signed-properties-1545-8800-2160-3000","sha256",xml2,"http://uri.etsi.org/01903#SignedProperties") ; Provide a certificate + private key. (PFX password is test123) cert.i = CkCert::ckCreate() If cert.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkCert::ckLoadPfxFile(cert,"qa_data/pfx/cert_test123.pfx","test123") If success <> 1 Debug CkCert::ckLastErrorText(cert) CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkXml::ckDispose(xml1) CkXml::ckDispose(xml2) CkCert::ckDispose(cert) ProcedureReturn EndIf CkXmlDSigGen::ckSetX509Cert(gen,cert,1) CkXmlDSigGen::setCkKeyInfoType(gen, "X509Data+KeyValue") CkXmlDSigGen::setCkX509Type(gen, "Certificate") ; Load XML to be signed... sbXml.i = CkStringBuilder::ckCreate() If sbXml.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::ckGetXmlSb(xmlToSign,sbXml) CkXmlDSigGen::setCkBehaviors(gen, "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.i = CkJsonObject::ckCreate() If jsonTsa.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckUpdateString(jsonTsa,"timestampToken.tsaUrl","http://timestamp.digicert.com") CkJsonObject::ckUpdateBool(jsonTsa,"timestampToken.requestTsaCert",1) CkXmlDSigGen::ckSetTsa(gen,jsonTsa) ; Sign the XML... success = CkXmlDSigGen::ckCreateXmlDSigSb(gen,sbXml) If success <> 1 Debug CkXmlDSigGen::ckLastErrorText(gen) CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkXml::ckDispose(xml1) CkXml::ckDispose(xml2) CkCert::ckDispose(cert) CkStringBuilder::ckDispose(sbXml) CkJsonObject::ckDispose(jsonTsa) ProcedureReturn EndIf ; Save the signed XML to a file. success = CkStringBuilder::ckWriteFile(sbXml,"qa_output/signedXml.xml","utf-8",0) ; ---------------------------------------- ; Verify the signatures we just produced... verifier.i = CkXmlDSig::ckCreate() If verifier.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkXmlDSig::ckLoadSignatureSb(verifier,sbXml) If success <> 1 Debug CkXmlDSig::ckLastErrorText(verifier) CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkXml::ckDispose(xml1) CkXml::ckDispose(xml2) CkCert::ckDispose(cert) CkStringBuilder::ckDispose(sbXml) CkJsonObject::ckDispose(jsonTsa) CkXmlDSig::ckDispose(verifier) ProcedureReturn EndIf ; Add "VerifyEncapsulatedTimeStamp" to the UncommonOptions to also verify any EncapsulatedTimeStamps CkXmlDSig::setCkUncommonOptions(verifier, "VerifyEncapsulatedTimeStamp") numSigs.i = CkXmlDSig::ckNumSignatures(verifier) verifyIdx.i = 0 While verifyIdx < numSigs CkXmlDSig::setCkSelector(verifier, verifyIdx) verified.i = CkXmlDSig::ckVerifySignature(verifier,1) If verified <> 1 Debug CkXmlDSig::ckLastErrorText(verifier) CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkXml::ckDispose(xml1) CkXml::ckDispose(xml2) CkCert::ckDispose(cert) CkStringBuilder::ckDispose(sbXml) CkJsonObject::ckDispose(jsonTsa) CkXmlDSig::ckDispose(verifier) ProcedureReturn EndIf verifyIdx = verifyIdx + 1 Wend Debug "All signatures were successfully verified." CkXml::ckDispose(xmlToSign) CkXmlDSigGen::ckDispose(gen) CkXml::ckDispose(object1) CkXml::ckDispose(xml1) CkXml::ckDispose(xml2) CkCert::ckDispose(cert) CkStringBuilder::ckDispose(sbXml) CkJsonObject::ckDispose(jsonTsa) CkXmlDSig::ckDispose(verifier) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.