Signing documents according to Spanish Factura format (government invoices)

Here you will find the sample code, which signs "factura" electronic XML documents (Spanish government's invoices), as described in this document.

The resulting signature is the enveloped signature, which contains a reference to the Document element, signed KeyInfo element and the XAdES-EPES form.

The signed XML looks like the sample below:

  1. <span class="tag">&lt;<span class="title">fe:Facturae</span> <span class="attribute">xmlns:ds</span>=<span class="value">"http://www.w3.org/2000/09/xmldsig#"</span> <span class="attribute">xmlns:fe</span>=<span class="value">"http://www.facturae.es/Facturae/2014/v3.2.1/Facturae"</span>&gt;</span>
  2. ...
  3. <span class="tag">&lt;<span class="title">ds:Signature</span> <span class="attribute">xmlns:ds</span>=<span class="value">"http://www.w3.org/2000/09/xmldsig#"</span> <span class="attribute">Id</span>=<span class="value">"Signature-998668816"</span>&gt;</span>
  4. <span class="tag">&lt;<span class="title">ds:SignedInfo</span>&gt;</span>
  5. <span class="tag">&lt;<span class="title">ds:CanonicalizationMethod</span> <span class="attribute">Algorithm</span>=<span class="value">"http://www.w3.org/TR/2001/REC-xml-c14n-20010315"</span>/&gt;</span>
  6. <span class="tag">&lt;<span class="title">ds:SignatureMethod</span> <span class="attribute">Algorithm</span>=<span class="value">"http://www.w3.org/2000/09/xmldsig#rsa-sha1"</span>/&gt;</span>
  7. <span class="tag">&lt;<span class="title">ds:Reference</span> <span class="attribute">Id</span>=<span class="value">"Ref1"</span> <span class="attribute">URI</span>=<span class="value">""</span>&gt;</span>
  8. <span class="tag">&lt;<span class="title">ds:Transforms</span>&gt;</span>
  9. <span class="tag">&lt;<span class="title">ds:Transform</span> <span class="attribute">Algorithm</span>=<span class="value">"http://www.w3.org/2000/09/xmldsig#enveloped-signature"</span>/&gt;</span>
  10. <span class="tag">&lt;/<span class="title">ds:Transforms</span>&gt;</span>
  11. <span class="tag">&lt;<span class="title">ds:DigestMethod</span> <span class="attribute">Algorithm</span>=<span class="value">"http://www.w3.org/2000/09/xmldsig#sha1"</span>/&gt;</span>
  12. <span class="tag">&lt;<span class="title">ds:DigestValue</span>&gt;</span>rKxz...<span class="tag">&lt;/<span class="title">ds:DigestValue</span>&gt;</span>
  13. <span class="tag">&lt;/<span class="title">ds:Reference</span>&gt;</span>
  14. <span class="tag">&lt;<span class="title">ds:Reference</span> <span class="attribute">URI</span>=<span class="value">"#Certificate1"</span>&gt;</span>
  15. <span class="tag">&lt;<span class="title">ds:DigestMethod</span> <span class="attribute">Algorithm</span>=<span class="value">"http://www.w3.org/2000/09/xmldsig#sha1"</span>/&gt;</span>
  16. <span class="tag">&lt;<span class="title">ds:DigestValue</span>&gt;</span>pLvS...<span class="tag">&lt;/<span class="title">ds:DigestValue</span>&gt;</span>
  17. <span class="tag">&lt;/<span class="title">ds:Reference</span>&gt;</span>
  18. <span class="tag">&lt;<span class="title">ds:Reference</span> <span class="attribute">Type</span>=<span class="value">"http://uri.etsi.org/01903#SignedProperties"</span> <span class="attribute">URI</span>=<span class="value">"#SignedProperties-292532795"</span>&gt;</span>
  19. <span class="tag">&lt;<span class="title">ds:DigestMethod</span> <span class="attribute">Algorithm</span>=<span class="value">"http://www.w3.org/2000/09/xmldsig#sha1"</span>/&gt;</span>
  20. <span class="tag">&lt;<span class="title">ds:DigestValue</span>&gt;</span>6tQA...<span class="tag">&lt;/<span class="title">ds:DigestValue</span>&gt;</span>
  21. <span class="tag">&lt;/<span class="title">ds:Reference</span>&gt;</span>
  22. <span class="tag">&lt;/<span class="title">ds:SignedInfo</span>&gt;</span>
  23. <span class="tag">&lt;<span class="title">ds:SignatureValue</span>&gt;</span>PVoD...<span class="tag">&lt;/<span class="title">ds:SignatureValue</span>&gt;</span>
  24. <span class="tag">&lt;<span class="title">ds:KeyInfo</span> <span class="attribute">Id</span>=<span class="value">"Certificate1"</span>&gt;</span>
  25. <span class="tag">&lt;<span class="title">ds:KeyValue</span>&gt;</span>
  26. <span class="tag">&lt;<span class="title">ds:RSAKeyValue</span>&gt;</span>
  27. <span class="tag">&lt;<span class="title">ds:Modulus</span>&gt;</span>We0Z...<span class="tag">&lt;/<span class="title">ds:Modulus</span>&gt;</span>
  28. <span class="tag">&lt;<span class="title">ds:Exponent</span>&gt;</span>AQAB<span class="tag">&lt;/<span class="title">ds:Exponent</span>&gt;</span>
  29. <span class="tag">&lt;/<span class="title">ds:RSAKeyValue</span>&gt;</span>
  30. <span class="tag">&lt;/<span class="title">ds:KeyValue</span>&gt;</span>
  31. <span class="tag">&lt;<span class="title">ds:X509Data</span>&gt;</span>
  32. <span class="tag">&lt;<span class="title">ds:X509Certificate</span>&gt;</span>MIIC...<span class="tag">&lt;/<span class="title">ds:X509Certificate</span>&gt;</span>
  33. <span class="tag">&lt;/<span class="title">ds:X509Data</span>&gt;</span>
  34. <span class="tag">&lt;/<span class="title">ds:KeyInfo</span>&gt;</span>
  35. <span class="tag">&lt;<span class="title">ds:Object</span>&gt;</span>
  36. <span class="tag">&lt;<span class="title">etsi:QualifyingProperties</span> <span class="attribute">xmlns:etsi</span>=<span class="value">"http://uri.etsi.org/01903/v1.3.2#"</span> <span class="attribute">Target</span>=<span class="value">"#Signature-998668816"</span>&gt;</span>
  37. <span class="tag">&lt;<span class="title">etsi:SignedProperties</span> <span class="attribute">Id</span>=<span class="value">"SignedProperties-292532795"</span>&gt;</span>
  38. <span class="tag">&lt;<span class="title">etsi:SignedSignatureProperties</span>&gt;</span>
  39. <span class="tag">&lt;<span class="title">etsi:SigningTime</span>&gt;</span>2016-01-01T00:00:00.000Z<span class="tag">&lt;/<span class="title">etsi:SigningTime</span>&gt;</span>
  40. <span class="tag">&lt;<span class="title">etsi:SigningCertificate</span>&gt;</span>
  41. <span class="tag">&lt;<span class="title">etsi:Cert</span>&gt;</span>
  42. <span class="tag">&lt;<span class="title">etsi:CertDigest</span>&gt;</span>
  43. <span class="tag">&lt;<span class="title">ds:DigestMethod</span> <span class="attribute">Algorithm</span>=<span class="value">"http://www.w3.org/2000/09/xmldsig#sha1"</span>/&gt;</span>
  44. <span class="tag">&lt;<span class="title">ds:DigestValue</span>&gt;</span>Qs3C...<span class="tag">&lt;/<span class="title">ds:DigestValue</span>&gt;</span>
  45. <span class="tag">&lt;/<span class="title">etsi:CertDigest</span>&gt;</span>
  46. <span class="tag">&lt;<span class="title">etsi:IssuerSerial</span>&gt;</span>
  47. <span class="tag">&lt;<span class="title">ds:X509IssuerName</span>&gt;</span>CN=...<span class="tag">&lt;/<span class="title">ds:X509IssuerName</span>&gt;</span>
  48. <span class="tag">&lt;<span class="title">ds:X509SerialNumber</span>&gt;</span>26...<span class="tag">&lt;/<span class="title">ds:X509SerialNumber</span>&gt;</span>
  49. <span class="tag">&lt;/<span class="title">etsi:IssuerSerial</span>&gt;</span>
  50. <span class="tag">&lt;/<span class="title">etsi:Cert</span>&gt;</span>
  51. <span class="tag">&lt;/<span class="title">etsi:SigningCertificate</span>&gt;</span>
  52. <span class="tag">&lt;<span class="title">etsi:SignaturePolicyIdentifier</span>&gt;</span>
  53. <span class="tag">&lt;<span class="title">etsi:SignaturePolicyId</span>&gt;</span>
  54. <span class="tag">&lt;<span class="title">etsi:SigPolicyId</span>&gt;</span>
  55. <span class="tag">&lt;<span class="title">etsi:Identifier</span>&gt;</span>http://www.facturae.es/politica_de_firma_formato_facturae/politica_de_firma_formato_facturae_v3_1.pdf<span class="tag">&lt;/<span class="title">etsi:Identifier</span>&gt;</span>
  56. <span class="tag">&lt;<span class="title">etsi:Description</span>&gt;</span>Política de Firma FacturaE v3.1<span class="tag">&lt;/<span class="title">etsi:Description</span>&gt;</span>
  57. <span class="tag">&lt;/<span class="title">etsi:SigPolicyId</span>&gt;</span>
  58. <span class="tag">&lt;<span class="title">etsi:SigPolicyHash</span>&gt;</span>
  59. <span class="tag">&lt;<span class="title">ds:DigestMethod</span> <span class="attribute">Algorithm</span>=<span class="value">"http://www.w3.org/2000/09/xmldsig#sha1"</span>/&gt;</span>
  60. <span class="tag">&lt;<span class="title">ds:DigestValue</span>&gt;</span>Ohixl6upD6av8N7pEvDABhEL6hM=<span class="tag">&lt;/<span class="title">ds:DigestValue</span>&gt;</span>
  61. <span class="tag">&lt;/<span class="title">etsi:SigPolicyHash</span>&gt;</span>
  62. <span class="tag">&lt;/<span class="title">etsi:SignaturePolicyId</span>&gt;</span>
  63. <span class="tag">&lt;/<span class="title">etsi:SignaturePolicyIdentifier</span>&gt;</span>
  64. <span class="tag">&lt;<span class="title">etsi:SignerRole</span>&gt;</span>
  65. <span class="tag">&lt;<span class="title">etsi:ClaimedRoles</span>&gt;</span>
  66. <span class="tag">&lt;<span class="title">etsi:ClaimedRole</span>&gt;</span>emisor<span class="tag">&lt;/<span class="title">etsi:ClaimedRole</span>&gt;</span>
  67. <span class="tag">&lt;/<span class="title">etsi:ClaimedRoles</span>&gt;</span>
  68. <span class="tag">&lt;/<span class="title">etsi:SignerRole</span>&gt;</span>
  69. <span class="tag">&lt;/<span class="title">etsi:SignedSignatureProperties</span>&gt;</span>
  70. <span class="tag">&lt;<span class="title">etsi:SignedDataObjectProperties</span>&gt;</span>
  71. <span class="tag">&lt;<span class="title">etsi:DataObjectFormat</span> <span class="attribute">ObjectReference</span>=<span class="value">"#Ref1"</span>&gt;</span>
  72. <span class="tag">&lt;<span class="title">etsi:Description</span>&gt;</span>Factura electrónica<span class="tag">&lt;/<span class="title">etsi:Description</span>&gt;</span>
  73. <span class="tag">&lt;<span class="title">etsi:MimeType</span>&gt;</span>text/xml<span class="tag">&lt;/<span class="title">etsi:MimeType</span>&gt;</span>
  74. <span class="tag">&lt;/<span class="title">etsi:DataObjectFormat</span>&gt;</span>
  75. <span class="tag">&lt;/<span class="title">etsi:SignedDataObjectProperties</span>&gt;</span>
  76. <span class="tag">&lt;/<span class="title">etsi:SignedProperties</span>&gt;</span>
  77. <span class="tag">&lt;/<span class="title">etsi:QualifyingProperties</span>&gt;</span>
  78. <span class="tag">&lt;/<span class="title">ds:Object</span>&gt;</span>
  79. <span class="tag">&lt;/<span class="title">ds:Signature</span>&gt;</span>
  80. <span class="tag">&lt;/<span class="title">fe:Facturae</span>&gt;</span>

Code samples in various languages follow:

C#:

  1. <span class="keyword">void</span> SignFactura(TElXMLDOMDocument xmlDocument, TElX509Certificate cert)
  2. {
  3. TElXMLSigner Signer = <span class="keyword">new</span> TElXMLSigner();
  4. TElXAdESSigner XAdESSigner = <span class="keyword">new</span> TElXAdESSigner();
  5. TElXMLKeyInfoX509Data X509KeyInfoData = <span class="keyword">new</span> TElXMLKeyInfoX509Data(<span class="keyword">false</span>);
  6. <span class="keyword">try</span>
  7. {
  8. Signer.XAdESProcessor = XAdESSigner;
  9. Signer.SignatureMethodType = SBXMLSec.Unit.xmtSig;
  10. Signer.SignatureMethod = SBXMLSec.Unit.xsmRSA_SHA1;
  11. Signer.CanonicalizationMethod = SBXMLDefs.Unit.xcmCanon;
  12. Signer.IncludeKey = <span class="keyword">true</span>;
  13. &nbsp;
  14. <span class="keyword">int</span> k = Signer.References.Add();
  15. TElXMLReference Ref = Signer.References[k];
  16. Ref.DigestMethod = SBXMLSec.Unit.xdmSHA1;
  17. Ref.ID = <span class="string">"Ref1"</span>;
  18. Ref.URI = <span class="string">""</span>;
  19. Ref.URINode = xmlDocument.DocumentElement;
  20. Ref.TransformChain.AddEnvelopedSignatureTransform();
  21. &nbsp;
  22. Signer.UpdateReferencesDigest();
  23. &nbsp;
  24. k = Signer.References.Add();
  25. Ref = Signer.References[k];
  26. Ref.DigestMethod = SBXMLSec.Unit.xdmSHA1;
  27. Ref.URI = <span class="string">"#Certificate1"</span>;
  28. &nbsp;
  29. XAdESSigner.XAdESVersion = SBXMLAdES.Unit.XAdES_v1_3_2;
  30. XAdESSigner.Included = SBXMLAdESIntf.Unit.xipSignerRole;
  31. XAdESSigner.SigningTime = DateTime.UtcNow;
  32. XAdESSigner.SignerRole.ClaimedRoles.AddText(XAdESSigner.XAdESVersion, xmlDocument, <span class="string">"emisor"</span>);
  33. &nbsp;
  34. <span class="keyword">string</span> URL = <span class="string">"http://www.facturae.es/politica_de_firma_formato_facturae/politica_de_firma_formato_facturae_v3_1.pdf"</span>;
  35. XAdESSigner.PolicyId.SigPolicyId.Identifier = URL;
  36. XAdESSigner.PolicyId.SigPolicyId.IdentifierQualifier = SBXMLAdES.Unit.xqtNone;
  37. XAdESSigner.PolicyId.SigPolicyId.Description = <span class="string">"Política de Firma FacturaE v3.1"</span>;
  38. XAdESSigner.PolicyId.SigPolicyHash.DigestMethod = SBXMLSec.Unit.DigestMethodToURI(SBXMLSec.Unit.xdmSHA1);
  39. <span class="comment">// uncomment to calculate a digest value or use precalculated value</span>
  40. <span class="comment">//Buf := DownloadData(URL);</span>
  41. <span class="comment">//XAdESSigner.PolicyId.SigPolicyHash.DigestValue := CalculateDigest(@Buf[0], Length(Buf), xdmSHA1);</span>
  42. XAdESSigner.PolicyId.SigPolicyHash.DigestValue = SBXMLUtils.Unit.ConvertFromBase64String(<span class="string">"Ohixl6upD6av8N7pEvDABhEL6hM="</span>);
  43. &nbsp;
  44. XAdESSigner.SigningCertificates = <span class="keyword">new</span> TElMemoryCertStorage();
  45. XAdESSigner.OwnSigningCertificates = <span class="keyword">true</span>;
  46. XAdESSigner.SigningCertificates.Add(cert);
  47. &nbsp;
  48. XAdESSigner.Generate(SBXMLAdES.Unit.XAdES_EPES);
  49. &nbsp;
  50. XAdESSigner.QualifyingProperties.XAdESPrefix = <span class="string">"etsi"</span>;
  51. &nbsp;
  52. TElXMLDataObjectFormat DataFormat = <span class="keyword">new</span> TElXMLDataObjectFormat(XAdESSigner.XAdESVersion);
  53. DataFormat.Description = <span class="string">"Factura electrónica"</span>;
  54. DataFormat.MimeType = <span class="string">"text/xml"</span>;
  55. DataFormat.ObjectReference = <span class="string">"#Ref1"</span>;
  56. XAdESSigner.QualifyingProperties.SignedProperties.SignedDataObjectProperties.DataObjectFormats.Add(DataFormat);
  57. &nbsp;
  58. X509KeyInfoData.IncludeKeyValue = <span class="keyword">true</span>;
  59. X509KeyInfoData.IncludeDataParams = SBXMLSec.Unit.xkidX509Certificate;
  60. X509KeyInfoData.Certificate = cert;
  61. Signer.KeyData = X509KeyInfoData;
  62. &nbsp;
  63. Signer.GenerateSignature();
  64. Signer.Signature.KeyInfo.ID = <span class="string">"Certificate1"</span>;
  65. &nbsp;
  66. Signer.SaveEnveloped(xmlDocument.DocumentElement);
  67. }
  68. <span class="keyword">finally</span>
  69. {
  70. X509KeyInfoData.Dispose();
  71. Signer.Dispose();
  72. XAdESSigner.Dispose();
  73. }
  74. }

PHP:

  1. function SignFactura($xmlDocument, $cert)
  2. {
  3. $Signer = <span class="keyword">new</span> TElXMLSigner(NULL);
  4. $XAdESSigner = <span class="keyword">new</span> TElXAdESSigner(NULL);
  5. $X509KeyInfoData = <span class="keyword">new</span> TElXMLKeyInfoX509Data(<span class="keyword">false</span>);
  6. $SigningCertificates = <span class="keyword">new</span> TElMemoryCertStorage(NULL);
  7. $Signer-&gt;XAdESProcessor = $XAdESSigner;
  8. $Signer-&gt;SignatureMethodType = TElXMLSigMethodType::xmtSig;
  9. $Signer-&gt;SignatureMethod = TElXMLSignatureMethod::xsmRSA_SHA1;
  10. $Signer-&gt;CanonicalizationMethod = TElXMLCanonicalizationMethod::xcmCanon;
  11. $Signer-&gt;IncludeKey = <span class="keyword">true</span>;
  12. $k = $Signer-&gt;References-&gt;Add();
  13. $Ref = $Signer-&gt;References-&gt;get_Reference($k);
  14. $Ref-&gt;DigestMethod = TElXMLDigestMethod::xdmSHA1;
  15. $Ref-&gt;ID = <span class="string">'Ref1'</span>;
  16. $Ref-&gt;URI = <span class="string">''</span>;
  17. $Ref-&gt;URINode = $xmlDocument-&gt;DocumentElement;
  18. $Ref-&gt;TransformChain-&gt;AddEnvelopedSignatureTransform();
  19. $Signer-&gt;UpdateReferencesDigest();
  20. $k = $Signer-&gt;References-&gt;Add();
  21. $Ref = $Signer-&gt;References-&gt;get_Reference($k);
  22. $Ref-&gt;DigestMethod = TElXMLDigestMethod::xdmSHA1;
  23. $Ref-&gt;URI = <span class="string">'#Certificate1'</span>;
  24. $XAdESSigner-&gt;XAdESVersion = TSBXAdESVersion::XAdES_v1_3_2;
  25. $XAdESSigner-&gt;Included = TElXAdESIncludedProperties::xipSignerRole;
  26. $XAdESSigner-&gt;SigningTime = SBUtils\UTCNow();
  27. $XAdESSigner-&gt;SignerRole-&gt;ClaimedRoles-&gt;AddText($XAdESSigner-&gt;XAdESVersion, $xmlDocument, <span class="string">'emisor'</span>);
  28. $URL = <span class="string">'http://www.facturae.es/politica_de_firma_formato_facturae/politica_de_firma_formato_facturae_v3_1.pdf'</span>;
  29. $XAdESSigner-&gt;PolicyId-&gt;SigPolicyId-&gt;Identifier = $URL;
  30. $XAdESSigner-&gt;PolicyId-&gt;SigPolicyId-&gt;IdentifierQualifier = TSBXAdESQualifierType::xqtNone;
  31. $XAdESSigner-&gt;PolicyId-&gt;SigPolicyId-&gt;Description = <span class="string">'Politica de Firma FacturaE v3.1'</span>;
  32. $XAdESSigner-&gt;PolicyId-&gt;SigPolicyHash-&gt;DigestMethod = SBXMLSec\DigestMethodToURI(TElXMLDigestMethod::xdmSHA1);
  33. <span class="comment">// uncomment to calculate a digest value or use precalculated value</span>
  34. <span class="comment">//$Buf = DownloadData($URL);</span>
  35. <span class="comment">//$XAdESSigner-&gt;PolicyId-&gt;SigPolicyHash-&gt;DigestValue = SBXMLSec\CalculateDigest(Buf, Length(Buf), TElXMLDigestMethod::xdmSHA1);</span>
  36. $XAdESSigner-&gt;PolicyId-&gt;SigPolicyHash-&gt;DigestValue = SBXMLUtils\ConvertFromBase64String(<span class="string">'Ohixl6upD6av8N7pEvDABhEL6hM='</span>);
  37. $SigningCertificates-&gt;Add($cert, <span class="keyword">false</span>);
  38. $XAdESSigner-&gt;SigningCertificates = $SigningCertificates;
  39. $XAdESSigner-&gt;Generate(TSBXAdESForm::XAdES_EPES);
  40. $XAdESSigner-&gt;QualifyingProperties-&gt;XAdESPrefix = <span class="string">'etsi'</span>;
  41. $DataFormat = <span class="keyword">new</span> TElXMLDataObjectFormat($XAdESSigner-&gt;XAdESVersion);
  42. $DataFormat-&gt;Description = <span class="string">'Factura electronica'</span>;
  43. $DataFormat-&gt;MimeType = <span class="string">'text/xml'</span>;
  44. $DataFormat-&gt;ObjectReference = <span class="string">'#Ref1'</span>;
  45. $XAdESSigner-&gt;QualifyingProperties-&gt;SignedProperties-&gt;SignedDataObjectProperties-&gt;DataObjectFormats-&gt;Add($DataFormat);
  46. $DataFormat-&gt;detachHandle();
  47. $X509KeyInfoData-&gt;IncludeKeyValue = <span class="keyword">true</span>;
  48. $X509KeyInfoData-&gt;IncludeDataParams = TElXMLKeyInfoX509DataParams::xkidX509Certificate;
  49. $X509KeyInfoData-&gt;Certificate = $cert;
  50. $Signer-&gt;KeyData = $X509KeyInfoData;
  51. $Signer-&gt;GenerateSignature();
  52. $Signer-&gt;Signature-&gt;KeyInfo-&gt;ID = <span class="string">'Certificate1'</span>;
  53. $Signer-&gt;SaveEnveloped($xmlDocument-&gt;DocumentElement);
  54. }

Delphi:

  1. <span class="function"><span class="keyword">procedure</span> <span class="title">SignFactura</span><span class="params">(XMLDocument : TElXMLDOMDocument; Cert : TElX509Certificate)</span>;</span>
  2. <span class="keyword">var</span>
  3. Signer : TElXMLSigner;
  4. XAdESSigner: TElXAdESSigner;
  5. X509KeyInfoData: TElXMLKeyInfoX509Data;
  6. DataFormat : TElXMLDataObjectFormat;
  7. Ref : TElXMLReference;
  8. URL : <span class="keyword">string</span>;
  9. Buf : ByteArray;
  10. k : Integer;
  11. <span class="keyword">begin</span>
  12. Signer := TElXMLSigner.Create(<span class="keyword">nil</span>);
  13. XAdESSigner := TElXAdESSigner.Create(<span class="keyword">nil</span>);
  14. X509KeyInfoData := TElXMLKeyInfoX509Data.Create(false);
  15. <span class="keyword">try</span>
  16. Signer.XAdESProcessor := XAdESSigner;
  17. Signer.SignatureMethodType := xmtSig;
  18. Signer.SignatureMethod := xsmRSA_SHA1;
  19. Signer.CanonicalizationMethod := xcmCanon;
  20. Signer.IncludeKey := true;
  21. &nbsp;
  22. k := Signer.References.Add;
  23. Ref := Signer.References[k];
  24. Ref.DigestMethod := xdmSHA1;
  25. Ref.ID := <span class="string">'Ref1'</span>;
  26. Ref.URI := <span class="string">''</span>;
  27. Ref.URINode := XMLDocument.DocumentElement;
  28. Ref.TransformChain.AddEnvelopedSignatureTransform();
  29. &nbsp;
  30. Signer.UpdateReferencesDigest();
  31. &nbsp;
  32. k := Signer.References.Add;
  33. Ref := Signer.References[k];
  34. Ref.DigestMethod := xdmSHA1;
  35. Ref.URI := <span class="string">'#Certificate1'</span>;
  36. &nbsp;
  37. XAdESSigner.XAdESVersion := XAdES_v1_3_2;
  38. XAdESSigner.Included := [xipSignerRole];
  39. XAdESSigner.SigningTime := UTCNow;
  40. XAdESSigner.SignerRole.ClaimedRoles.AddText(XAdESSigner.XAdESVersion, XMLDocument, <span class="string">'emisor'</span>);
  41. &nbsp;
  42. URL := <span class="string">'http://www.facturae.es/politica_de_firma_formato_facturae/politica_de_firma_formato_facturae_v3_1.pdf'</span>;
  43. XAdESSigner.PolicyId.SigPolicyId.Identifier := URL;
  44. XAdESSigner.PolicyId.SigPolicyId.IdentifierQualifier := xqtNone;
  45. XAdESSigner.PolicyId.SigPolicyId.Description := <span class="string">'Política de Firma FacturaE v3.1'</span>;
  46. XAdESSigner.PolicyId.SigPolicyHash.DigestMethod := DigestMethodToURI(xdmSHA1);
  47. <span class="comment">// uncomment to calculate a digest value or use precalculated value</span>
  48. <span class="comment">//Buf := DownloadData(URL);</span>
  49. <span class="comment">//XAdESSigner.PolicyId.SigPolicyHash.DigestValue := CalculateDigest(@Buf[0], Length(Buf), xdmSHA1);</span>
  50. XAdESSigner.PolicyId.SigPolicyHash.DigestValue := ConvertFromBase64String(<span class="string">'Ohixl6upD6av8N7pEvDABhEL6hM='</span>);
  51. &nbsp;
  52. XAdESSigner.SigningCertificates := TElMemoryCertStorage.Create(<span class="keyword">nil</span>);
  53. XAdESSigner.OwnSigningCertificates := true;
  54. XAdESSigner.SigningCertificates.Add(Cert);
  55. &nbsp;
  56. XAdESSigner.Generate(XAdES_EPES);
  57. &nbsp;
  58. XAdESSigner.QualifyingProperties.XAdESPrefix := <span class="string">'etsi'</span>;
  59. &nbsp;
  60. DataFormat := TElXMLDataObjectFormat.Create(XAdESSigner.XAdESVersion);
  61. DataFormat.Description := <span class="string">'Factura electrónica'</span>;
  62. DataFormat.MimeType := <span class="string">'text/xml'</span>;
  63. DataFormat.ObjectReference := <span class="string">'#Ref1'</span>;
  64. XAdESSigner.QualifyingProperties.SignedProperties.SignedDataObjectProperties.DataObjectFormats.Add(DataFormat);
  65. &nbsp;
  66. X509KeyInfoData.IncludeKeyValue := true;
  67. X509KeyInfoData.IncludeDataParams := [xkidX509Certificate];
  68. X509KeyInfoData.Certificate := Cert;
  69. Signer.KeyData := X509KeyInfoData;
  70. &nbsp;
  71. Signer.GenerateSignature;
  72. Signer.Signature.KeyInfo.ID := <span class="string">'Certificate1'</span>;
  73. &nbsp;
  74. Signer.SaveEnveloped(XMLDocument.DocumentElement);
  75. <span class="keyword">finally</span>
  76. FreeAndNil(X509KeyInfoData);
  77. FreeAndNil(Signer);
  78. FreeAndNil(XAdESSigner);
  79. <span class="keyword">end</span>;
  80. <span class="keyword">end</span>;

Ready to get started?

Learn more about SecureBlackbox or download a free trial.

Download Now