Objective-C
Objective-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 Objective-C Downloads
#import <CkoXml.h>
#import <CkoXmlDSigGen.h>
#import <CkoCert.h>
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>
#import <CkoXmlDSig.h>
BOOL success = NO;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
success = YES;
// 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>
CkoXml *xmlToSign = [[CkoXml alloc] init];
xmlToSign.Tag = @"p:FatturaElettronica";
[xmlToSign AddAttribute: @"xmlns:ds" value: @"http://www.w3.org/2000/09/xmldsig#"];
[xmlToSign AddAttribute: @"xmlns:p" value: @"http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2"];
[xmlToSign AddAttribute: @"xmlns:xsi" value: @"http://www.w3.org/2001/XMLSchema-instance"];
[xmlToSign AddAttribute: @"versione" value: @"FPR12"];
[xmlToSign AddAttribute: @"xsi:schemaLocation" value: @"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"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|DatiTrasmissione|IdTrasmittente|IdPaese" value: @"IT"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|DatiTrasmissione|IdTrasmittente|IdCodice" value: @"01234567890"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|DatiTrasmissione|ProgressivoInvio" value: @"00001"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|DatiTrasmissione|FormatoTrasmissione" value: @"FPR12"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|DatiTrasmissione|CodiceDestinatario" value: @"0000000"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|DatiTrasmissione|PECDestinatario" value: @"betagamma@pec.it"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|IdFiscaleIVA|IdPaese" value: @"IT"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|IdFiscaleIVA|IdCodice" value: @"01234567890"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|Anagrafica|Denominazione" value: @"ALPHA SRL"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|RegimeFiscale" value: @"RF19"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CedentePrestatore|Sede|Indirizzo" value: @"VIALE ROMA 543"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CedentePrestatore|Sede|CAP" value: @"07100"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CedentePrestatore|Sede|Comune" value: @"SASSARI"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CedentePrestatore|Sede|Provincia" value: @"SS"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CedentePrestatore|Sede|Nazione" value: @"IT"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CessionarioCommittente|DatiAnagrafici|CodiceFiscale" value: @"09876543210"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CessionarioCommittente|DatiAnagrafici|Anagrafica|Denominazione" value: @"AMMINISTRAZIONE BETA"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CessionarioCommittente|Sede|Indirizzo" value: @"VIA TORINO 38-B"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CessionarioCommittente|Sede|CAP" value: @"00145"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CessionarioCommittente|Sede|Comune" value: @"ROMA"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CessionarioCommittente|Sede|Provincia" value: @"RM"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CessionarioCommittente|Sede|Nazione" value: @"IT"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Indirizzo" value: @"VIA CASELLE"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|NumeroCivico" value: @"4/D"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|CAP" value: @"25027"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Comune" value: @"QUINZANO D'OGLIO"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Provincia" value: @"BS"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Nazione" value: @"IT"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CessionarioCommittente|RappresentanteFiscale|IdFiscaleIVA|IdPaese" value: @"DE"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CessionarioCommittente|RappresentanteFiscale|IdFiscaleIVA|IdCodice" value: @"DE12345"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaHeader|CessionarioCommittente|RappresentanteFiscale|Denominazione" value: @"RFCC - DENOMINAZIONE"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|TipoDocumento" value: @"TD01"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Divisa" value: @"EUR"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Data" value: @"2022-03-03"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Numero" value: @"123"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Causale" value: @"LA FATTURA FA RIFERIMENTO AD UNA OPERAZIONE AAAA"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Causale[1]" value: @"SEGUE DESCRIZIONE CAUSALE NEL CASO IN CUI NON SIANO STATI SUFFICIENTI 200 CARATTERI AAAAAAAAAAA BBBBBBBBBBBBBBBBB"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|RiferimentoNumeroLinea" value: @"1"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|IdDocumento" value: @"66685"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|NumItem" value: @"1"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|CodiceCUP" value: @"123abc"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|CodiceCIG" value: @"456def"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiContratto|RiferimentoNumeroLinea" value: @"1"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiContratto|IdDocumento" value: @"123"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiContratto|Data" value: @"2022-01-01"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiContratto|NumItem" value: @"5"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiContratto|CodiceCUP" value: @"123abc"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiContratto|CodiceCIG" value: @"456def"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|RiferimentoNumeroLinea" value: @"1"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|IdDocumento" value: @"456"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|NumItem" value: @"5"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|CodiceCUP" value: @"123abc"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|CodiceCIG" value: @"456def"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiRicezione|RiferimentoNumeroLinea" value: @"1"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiRicezione|IdDocumento" value: @"789"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiRicezione|NumItem" value: @"5"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiRicezione|CodiceCUP" value: @"123abc"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiRicezione|CodiceCIG" value: @"456def"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DatiAnagraficiVettore|IdFiscaleIVA|IdPaese" value: @"IT"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DatiAnagraficiVettore|IdFiscaleIVA|IdCodice" value: @"24681012141"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DatiAnagraficiVettore|Anagrafica|Denominazione" value: @"Trasporto spa"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DataOraConsegna" value: @"2022-03-01T14:26:39"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|NumeroLinea" value: @"1"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|Descrizione" value: @"DESCRIZIONE DELLA FORNITURA"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|Quantita" value: @"5.005"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|PrezzoUnitario" value: @"1.00"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|PrezzoTotale" value: @"5.00"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|AliquotaIVA" value: @"22.00"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|AliquotaIVA" value: @"22.00"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|ImponibileImporto" value: @"5.00"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|Imposta" value: @"1.10"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|EsigibilitaIVA" value: @"I"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiPagamento|CondizioniPagamento" value: @"TP01"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiPagamento|DettaglioPagamento|ModalitaPagamento" value: @"MP01"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiPagamento|DettaglioPagamento|DataScadenzaPagamento" value: @"2017-02-18"];
[xmlToSign UpdateChildContent: @"FatturaElettronicaBody|DatiPagamento|DettaglioPagamento|ImportoPagamento" value: @"6.10"];
CkoXmlDSigGen *gen = [[CkoXmlDSigGen alloc] init];
gen.SigLocation = @"p:FatturaElettronica";
gen.SigLocationMod = [NSNumber numberWithInt:0];
gen.SigId = @"signature-5580-7534-6530-8286";
[gen AddSignatureNamespace: @"xadesv1410" nsUri: @"http://uri.etsi.org/01903/v1.4.1"];
[gen AddSignatureNamespace: @"xades" nsUri: @"http://uri.etsi.org/01903/v1.3.2#"];
// xmlRoot tag = p:FatturaElettronica
// has xmlns:ds!
gen.SigValueId = @"signature-value-5957-3819-1360-0727";
gen.SignedInfoCanonAlg = @"EXCL_C14N";
gen.SignedInfoDigestMethod = @"sha256";
// Set the KeyInfoId before adding references..
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.
CkoXml *object1 = [[CkoXml alloc] init];
object1.Tag = @"xades:QualifyingProperties";
[object1 AddAttribute: @"Target" value: @"#signature-5580-7534-6530-8286"];
[object1 UpdateAttrAt: @"xades:SignedProperties" autoCreate: YES attrName: @"Id" attrValue: @"signed-properties-1545-8800-2160-3000"];
[object1 UpdateChildContent: @"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime" value: @"TO BE GENERATED BY CHILKAT"];
[object1 UpdateAttrAt: @"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod" autoCreate: YES attrName: @"Algorithm" attrValue: @"http://www.w3.org/2001/04/xmlenc#sha256"];
[object1 UpdateChildContent: @"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue" value: @"TO BE GENERATED BY CHILKAT"];
[object1 UpdateChildContent: @"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2" value: @"TO BE GENERATED BY CHILKAT"];
[object1 UpdateAttrAt: @"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp" autoCreate: YES attrName: @"Id" attrValue: @"signature-timestamp-5561-8212-3316-5191"];
[object1 UpdateAttrAt: @"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|ds:CanonicalizationMethod" autoCreate: YES attrName: @"Algorithm" attrValue: @"http://www.w3.org/2001/10/xml-exc-c14n#"];
[object1 UpdateAttrAt: @"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp" autoCreate: YES attrName: @"Encoding" attrValue: @"http://uri.etsi.org/01903/v1.2.2#DER"];
[object1 UpdateChildContent: @"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp" value: @"TO BE GENERATED BY CHILKAT"];
[gen AddObject: @"signature-object-8923-2359-1722-2161" content: [object1 GetXml] mimeType: @"" encoding: @""];
// -------- 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>
CkoXml *xml1 = [[CkoXml alloc] init];
xml1.Tag = @"ds:Transforms";
[xml1 UpdateAttrAt: @"ds:Transform" autoCreate: YES attrName: @"Algorithm" attrValue: @"http://www.w3.org/2002/06/xmldsig-filter2"];
[xml1 UpdateAttrAt: @"ds:Transform|dsig-xpath:XPath" autoCreate: YES attrName: @"xmlns:dsig-xpath" attrValue: @"http://www.w3.org/2002/06/xmldsig-filter2"];
[xml1 UpdateAttrAt: @"ds:Transform|dsig-xpath:XPath" autoCreate: YES attrName: @"Filter" attrValue: @"subtract"];
[xml1 UpdateChildContent: @"ds:Transform|dsig-xpath:XPath" value: @"/descendant::ds:Signature"];
[xml1 UpdateAttrAt: @"ds:Transform[1]" autoCreate: YES attrName: @"Algorithm" attrValue: @"http://www.w3.org/2001/10/xml-exc-c14n#"];
[gen AddSameDocRef2: @"" digestMethod: @"sha256" transforms: xml1 refType: @""];
// -------- Reference 2 --------
// <ds:Transforms>
// <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
// </ds:Transforms>
CkoXml *xml2 = [[CkoXml alloc] init];
xml2.Tag = @"ds:Transforms";
[xml2 UpdateAttrAt: @"ds:Transform" autoCreate: YES attrName: @"Algorithm" attrValue: @"http://www.w3.org/2001/10/xml-exc-c14n#"];
[gen AddObjectRef2: @"signed-properties-1545-8800-2160-3000" digestMethod: @"sha256" transforms: xml2 refType: @"http://uri.etsi.org/01903#SignedProperties"];
// Provide a certificate + private key. (PFX password is test123)
CkoCert *cert = [[CkoCert alloc] init];
success = [cert LoadPfxFile: @"qa_data/pfx/cert_test123.pfx" password: @"test123"];
if (success != YES) {
NSLog(@"%@",cert.LastErrorText);
return;
}
[gen SetX509Cert: cert usePrivateKey: YES];
gen.KeyInfoType = @"X509Data+KeyValue";
gen.X509Type = @"Certificate";
// Load XML to be signed...
CkoStringBuilder *sbXml = [[CkoStringBuilder alloc] init];
[xmlToSign GetXmlSb: sbXml];
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:
// -------------------------------------------------------------------------------------------
CkoJsonObject *jsonTsa = [[CkoJsonObject alloc] init];
[jsonTsa UpdateString: @"timestampToken.tsaUrl" value: @"http://timestamp.digicert.com"];
[jsonTsa UpdateBool: @"timestampToken.requestTsaCert" value: YES];
[gen SetTsa: jsonTsa];
// Sign the XML...
success = [gen CreateXmlDSigSb: sbXml];
if (success != YES) {
NSLog(@"%@",gen.LastErrorText);
return;
}
// Save the signed XML to a file.
success = [sbXml WriteFile: @"qa_output/signedXml.xml" charset: @"utf-8" emitBom: NO];
// ----------------------------------------
// Verify the signatures we just produced...
CkoXmlDSig *verifier = [[CkoXmlDSig alloc] init];
success = [verifier LoadSignatureSb: sbXml];
if (success != YES) {
NSLog(@"%@",verifier.LastErrorText);
return;
}
// Add "VerifyEncapsulatedTimeStamp" to the UncommonOptions to also verify any EncapsulatedTimeStamps
verifier.UncommonOptions = @"VerifyEncapsulatedTimeStamp";
int numSigs = [verifier.NumSignatures intValue];
int verifyIdx = 0;
while (verifyIdx < numSigs) {
verifier.Selector = [NSNumber numberWithInt: verifyIdx];
BOOL verified = [verifier VerifySignature: YES];
if (verified != YES) {
NSLog(@"%@",verifier.LastErrorText);
return;
}
verifyIdx = verifyIdx + 1;
}
NSLog(@"%@",@"All signatures were successfully verified.");