Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/enveloped-signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class EnvelopedSignature implements CanonicalizationOrTransformationAlgor
process(node: Node, options: CanonicalizationOrTransformationAlgorithmProcessOptions): Node {
if (null == options.signatureNode) {
const signature = xpath.select1(
"./*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']",
".//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']",
node,
);
if (isDomNode.isNodeLike(signature) && signature.parentNode) {
Expand Down
33 changes: 33 additions & 0 deletions test/signature-integration-tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,37 @@ describe("Signature integration tests", function () {
"<library> should have two child nodes : <book> and <Signature>",
).to.equal(2);
});

it("should create valid signature when signature location is nested in child element", function () {
const xml = "<root><child/></root>";

const sig = new SignedXml();
sig.privateKey = fs.readFileSync("./test/static/client.pem");
sig.addReference({
xpath: "/*",
transforms: [
"http://www.w3.org/2000/09/xmldsig#enveloped-signature",
"http://www.w3.org/2001/10/xml-exc-c14n#",
],
digestAlgorithm: "http://www.w3.org/2001/04/xmlenc#sha256",
});
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";

sig.computeSignature(xml, {
location: { action: "append", reference: "//*[local-name()='child']" },
});

const signedXml = sig.getSignedXml();

const doc = new xmldom.DOMParser().parseFromString(signedXml);
const signatureNode = xpath.select1("//*[local-name(.)='Signature']", doc);
isDomNode.assertIsNodeLike(signatureNode);

const verifier = new SignedXml();
verifier.publicCert = fs.readFileSync("./test/static/client_public.pem");
verifier.loadSignature(signatureNode);

expect(verifier.checkSignature(signedXml)).to.be.true;
});
});