Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signing only part of xml #106

Open
iasna opened this issue Jul 21, 2020 · 2 comments
Open

Signing only part of xml #106

iasna opened this issue Jul 21, 2020 · 2 comments
Assignees

Comments

@iasna
Copy link

iasna commented Jul 21, 2020

Hello,
This is the output signed xml that is needed

<?xml version="1.0" encoding="UTF-8"?>
<Major xmlns="some:namespace:">
   <Body>
      <header xmlns="another:namespace" xmlns:xsi="yet:another:namesapce">
         <test>some data</test>
         <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="Signature001">.....</ds:Signature>
      </header>
      <Actual xmlns="test:namespace">
         <document>this is to be signed</document>
      </Actual>
   </Body>
</Major>

I only want tag Actual to be signed and it should be uri less, below is my code I tried to get Actual tag from my xml document like

const simpleDoc = xades.Parse(`<Major xmlns="some:namespace:"><Body><header xmlns="another:namespace" xmlns:xsi="yet:another:namesapce"> <test>Some data</test></header><Actual xmlns="test:namespace"><document>this is to be signed</document></Actual></Body></Major>`);
    const signedXml = new xades.SignedXml();
    signedXml.XmlSignature.KeyInfo.Id = "KeyInfo001";
    const keyInfoRef = new xmldsig.Reference("#KeyInfo001");
    keyInfoRef.DigestMethod.Algorithm = xmldsig.SHA1_NAMESPACE;
    signedXml.XmlSignature.SignedInfo.References.Add(keyInfoRef);

var toSign = simpleDoc.getElementsByTagName("Actual");

    var s = new xmldom.XMLSerializer();
    console.log(s.serializeToString(toSign[0]));


     const signature = await signedXml.Sign(alg, // algorithm
        key, // key
        toSign, // document
        {
            keyValue: key.publicKey,
            x509: [x509],

            id: "Signature001",
            references: [
                {  hash: "SHA-256", transforms: ["enveloped"] }
            ]
           
        });

    var testDoc = xmlDoc.getElementsByTagName("header");
    
    testDoc[0].appendChild(signature.GetXml());

But I'm getting error TypeError: data.cloneNode is not a function, however if I pass xmlDoc to Sign function code executes without any errors.

@di-baggio
Copy link

@iasna hey, i don't know if this is the best way but is working, i did that like this:
const xmlDoc = xadesjs.Parse(xml);
const eltExtenxions = xmlDoc.createElement('ext:UBLExtensions');
const eltExtenxion = xmlDoc.createElement('ext:UBLExtension');
const eltContent = xmlDoc.createElement('ext:ExtensionContent');
const extenxions = xmlDoc.documentElement.appendChild(eltExtenxions);
const extenxion = extenxions.appendChild(eltExtenxion);
const content = extenxion.appendChild(eltContent);
content.appendChild(signature.GetXml());
console.log(new XMLSerializer().serializeToString(xmlDoc));

@microshine
Copy link
Contributor

I updated your example. Is it what you want?

TypeScript

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

const simpleDoc = xades.Parse(`<Major xmlns="some:namespace:"><Body><header xmlns="another:namespace" xmlns:xsi="yet:another:namesapce"> <test>Some data</test></header><Actual xmlns="test:namespace"><document>this is to be signed</document></Actual></Body></Major>`);
const signedXml = new xades.SignedXml();
signedXml.XmlSignature.KeyInfo.Id = "KeyInfo001";
const keyInfoRef = new xmldsig.Reference("#KeyInfo001");
keyInfoRef.DigestMethod.Algorithm = xmldsig.SHA1_NAMESPACE;
signedXml.XmlSignature.SignedInfo.References.Add(keyInfoRef);

const xmlActual = simpleDoc.getElementsByTagName("Actual")[0];
const toSign = new xmldom.DOMParser().parseFromString(new xmldom.XMLSerializer().serializeToString(xmlActual), "application/xml");

const signature = await signedXml.Sign(alg, // algorithm
  keys.privateKey, // key
  toSign, // document
  {
    keyValue: keys.publicKey,
    id: "Signature001",
    references: [
      { hash: "SHA-256", transforms: ["enveloped"] }
    ]

  });

const xmlHeader = simpleDoc.getElementsByTagName("header");

xmlHeader[0].appendChild(signature.GetXml());

console.log(new xmldom.XMLSerializer().serializeToString(simpleDoc));

XML (formatted)

<Major xmlns="some:namespace:">
  <Body>
    <header xmlns="another:namespace"
      xmlns:xsi="yet:another:namesapce">
      <test>Some data</test>
      <ds:Signature Id="Signature001"
        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 URI="#KeyInfo001">
            <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
            <ds:DigestValue>x0RFz/zrDbP8QAqLCdhwzbNkXGk=</ds:DigestValue>
          </ds:Reference>
          <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>khBF6jDjYYO30jPqLJPrOOy0fKT6zhyzKZpCw6srtV8=</ds:DigestValue>
          </ds:Reference>
          <ds:Reference URI="#xades-id-bd9ebfc20031" Type="http://uri.etsi.org/01903#SignedProperties">
            <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
            <ds:DigestValue>jCkdQ+kqA3R+DPhBDXEG+AwbX1rUkZc4WNADzBGfSqY=</ds:DigestValue>
          </ds:Reference>
        </ds:SignedInfo>
        <ds:SignatureValue>opdydFpY5+U801VDccEl/wtkFMLwr6q8TGlTJQpOfNOTt0crfB2qECbIEkbt8fcOE/K4Jd+2ZrI3OMUBqoV9fAnmJiRkOonC9LX4B4lx9YiBowIoMmItxAg3KWR1c8yE21oIBM4lKm0skyVkKZhCoghA5yu/nsYNkOQ6kxTZyRzl//iOhxsIxjkS6jVE2MQTEs6vPcsXp+Yt1KDt6mn/4dorO9XyHwDrlHFIiyIF8kFckmwm6T6Za5f9YZy47B9/JVkEvyof3CE+dUhfNAgOOpUUsSB/c+Bme1VpA6cwIR4TPvzpaX7tM1Fup9+JJq9JLWdN8NFGbHcEgegybj5JZA==</ds:SignatureValue>
        <ds:KeyInfo Id="KeyInfo001">
          <ds:KeyValue>
            <ds:RSAKeyValue>
              <ds:Modulus>y7ovWIpVErSvHaCNaVbFSqXuCsNum7FbIMrYy72T0YvdApNV6vHM7cAz9jPpPZseT4R+vOEGWmKtFE/q5a+SNAy11EiqAiqAezE/2bqxxcU03uSvpRuxLjUi2gtmI/c0/g2W9+CUlpldpQNefkz3JINsmN7z/53o7ZlaYtxWfc4F7ZCZXde61mdqcIf1XGNzvQGjFhP3ZZwWVpYud0G911cWw/ewtrrmH8P9a/1TokH/IUvhd3C6BBlTCQLFhi6V3qO3qeSOoLsOQdmGtIe+cBhRKYFoKvjPPsAozwyVWvlfiLbtC8ynPy5W43bIsdUn/t+qVcHYGIEf7ux/Xerg1w==</ds:Modulus>
              <ds:Exponent>AQAB</ds:Exponent>
            </ds:RSAKeyValue>
          </ds:KeyValue>
        </ds:KeyInfo>
        <ds:Object>
          <xades:QualifyingProperties Target="#id-bd9ebfc20031"
            xmlns:xades="http://uri.etsi.org/01903/v1.3.2#">
            <xades:SignedProperties Id="xades-id-bd9ebfc20031">
              <xades:SignedSignatureProperties>
                <xades:SigningTime>2021-07-21T07:51:30.340Z</xades:SigningTime>
              </xades:SignedSignatureProperties>
            </xades:SignedProperties>
          </xades:QualifyingProperties>
        </ds:Object>
      </ds:Signature>
    </header>
    <Actual xmlns="test:namespace">
      <document>this is to be signed</document>
    </Actual>
  </Body>
</Major>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants