PeculiarVentures/xadesjs

How to set a value for SigningTime in YYYY-MM-DDThh: mm: ss-05: 00 format?

Jairos2015 opened this issue · 4 comments

How to set a value for SigningTime in YYYY-MM-DDThh: mm: ss-05: 00 format?. The signature has the SigningTime element in format YY-MM-DDThh: mm: ssxxxz but the tax entity requires the YYYY-MM-DDThh: mm: ss-05: 00 format. Please, how to set?.

XAdES uses dateFormat function to serialize XadesDateTime element.

I've updated the XAdES module to v2.0.15 and added the SigningTime options to Sign method.

Example

const { Crypto } = require("@peculiar/webcrypto");
const xades = require("xadesjs");

async function main() {
  const crypto = new Crypto();

  // Set crypto engine for XAdES
  xades.Application.setEngine("NodeJS", crypto);

  // Generate RSA keys
  const algorithm = {
    name: "RSASSA-PKCS1-v1_5",
    hash: "SHA-256",
    publicExponent: new Uint8Array([1, 0, 1]),
    modulusLength: 2048,
  };
  const keys = await crypto.subtle.generateKey(algorithm, false, ["sign", "verify"]);

  // Sign XML
  const xmlDoc = xades.Parse(`<root><child attr="Some"/></root>`);
  const signedXml = new xades.SignedXml();

  const signature = await signedXml.Sign(
    algorithm,                              // algorithm
    keys.privateKey,                        // key
    xmlDoc,                                 // document
    {                                       // options
      keyValue: keys.publicKey,
      references: [
        { hash: "SHA-256", transforms: ["enveloped"] }
      ],
      productionPlace: {
        country: "Country",
        state: "State",
        city: "City",
        code: "Code",
      },
      signingTime: {
        format: "isoDateTime"
      }
    });

  console.log(signature.toString());
}

main()
  .catch(e => console.error(e));

Formatted Signature

<ds:Signature Id="id-59e84a77cae4"
    xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    <ds:SignedInfo>
        <ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
        <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
        <ds:Reference>
            <ds:Transforms>
                <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
            </ds:Transforms>
            <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
            <ds:DigestValue>0Ul7cHlzBRgEcKcYhAO7u3oWL55aFk1y4mKYnzbFljs=</ds:DigestValue>
        </ds:Reference>
        <ds:Reference URI="#xades-id-59e84a77cae4" Type="http://uri.etsi.org/01903#SignedProperties">
            <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
            <ds:DigestValue>TWcSAViLpXepPjYVAd777xHDa/Mu4JFSaItZd8Iau2c=</ds:DigestValue>
        </ds:Reference>
    </ds:SignedInfo>
    <ds:SignatureValue>eyqqsHSyVxUwrLHKI4OCqSzU2GOiFuHzQg2CGOo9d6MkCXjHN8OJbudqF/qKapZfZKhLpYtbzq+YeTf7IQtDGqC/f1rklyP6WSq5pzWTt3MegParQFXDvVdbO0XhqMhoEuFa+mEaZSA/o6Ty1FGapvTNyj3+8qjBFBJOXXEfOTa6KCukxaLrjYZFECrGCbXI2eh32TekyAx/ReeUGVqUyQFnJS4McUOYmVeD7alwPSy/RA95Yk/Ze/RWoc9EckV0a/70kg25PBn7r1YLG0qQ+oww+/Z1kSVNo2/4lqMiZNbdjraa7jI6sE+jHfTGpriJwsTiAB3XfnQI8LsjqJ65sA==</ds:SignatureValue>
    <ds:KeyInfo>
        <ds:KeyValue>
            <ds:RSAKeyValue>
                <ds:Modulus>uyH0yr/KX8TjcOOoYdzudsZT3dHlMJPAh+opsWsJyx5sGOminir+vrMqy2cekmDwUxMpU2PtgnAL+vu9seBpPITiAVjIhzNIoJDGEXIoAmvvoKTC394eSh2xTInwNm5xX1PIzyHe9DX7BnT9Qaf9CNKp+1LzKTY/ayIPSO1tSV698pE+a/oJVHQnqwlHGLIG4uegqceZdtKM3X0IOKphPq1dpVKeQC3OSNDuMXOE0cARLQ/MYa5Z/rjI8yuSkKkUtus609slG6ZzNlF4xmSUcr42+gS+BjbVhhSu8rCNFIkTn9b15nlUTEV8c0H/XO1SqI9zudnxSbsdIvepe4B8RQ==</ds:Modulus>
                <ds:Exponent>AQAB</ds:Exponent>
            </ds:RSAKeyValue>
        </ds:KeyValue>
    </ds:KeyInfo>
    <ds:Object>
        <xades:QualifyingProperties Target="#id-59e84a77cae4"
            xmlns:xades="http://uri.etsi.org/01903/v1.3.2#">
            <xades:SignedProperties Id="xades-id-59e84a77cae4">
                <xades:SignedSignatureProperties>
                    <xades:SigningTime>2019-04-26T14:05:34+0300</xades:SigningTime>
                    <xades:SignatureProductionPlace>
                        <xades:City>City</xades:City>
                        <xades:StateOrProvince>State</xades:StateOrProvince>
                        <xades:PostalCode>Code</xades:PostalCode>
                        <xades:CountryName>Country</xades:CountryName>
                    </xades:SignatureProductionPlace>
                </xades:SignedSignatureProperties>
            </xades:SignedProperties>
        </xades:QualifyingProperties>
    </ds:Object>
</ds:Signature>

Update xadesjs from v2.0.14 -> v2.0.15 and it works for me. Thank you.

Would love to hear how your using xadesjs.

I look on the file dateFormat but I can't find the xmlDateTime,

The problem is isoDateTime dont put the char : in timezone, and it is invalid for some xml schemas.

Example: 2020-07-13T08:17:03-05:00

image