From 17c628c0bc401c3c80d5129fd7f6757dc0834621 Mon Sep 17 00:00:00 2001 From: GauriSpears Date: Wed, 17 Jan 2024 13:59:02 +0500 Subject: [PATCH] Basic xades support --- src/signed-xml.ts | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/signed-xml.ts b/src/signed-xml.ts index ca19db7..38e04b9 100644 --- a/src/signed-xml.ts +++ b/src/signed-xml.ts @@ -802,6 +802,9 @@ export class SignedXml { signatureXml += this.createSignedInfo(doc, prefix); signatureXml += this.getKeyInfo(prefix); + if (this.xadesQualifyingProperties != null) { + signatureXml += `<${currentPrefix}Object>${this.xadesQualifyingProperties()}`; + } signatureXml += ``; this.originalXmlWithIds = doc.toString(); @@ -918,21 +921,29 @@ export class SignedXml { prefix = prefix ? `${prefix}:` : prefix; for (const ref of this.getReferences()) { - const nodes = xpath.selectWithResolver(ref.xpath ?? "", doc, this.namespaceResolver); + let nodes = xpath.selectWithResolver(ref.xpath ?? "", doc, this.namespaceResolver); if (!utils.isArrayHasLength(nodes)) { - throw new Error( - `the following xpath cannot be signed because it was not found: ${ref.xpath}`, - ); + if (this.xadesQualifyingProperties != null){ + nodes = xpath.selectWithResolver(ref.xpath ?? "", new xmldom.DOMParser().parseFromString(this.xadesQualifyingProperties()), this.namespaceResolver); + } + if (!utils.isArrayHasLength(nodes)) { + throw new Error( + `the following xpath cannot be signed because it was not found: ${ref.xpath}`, + ); + } } for (const node of nodes) { + let addattr=""; + if (node.localName=="SignedProperties") addattr=' Type="http://uri.etsi.org/01903#SignedProperties"'; if (ref.isEmptyUri) { - res += `<${prefix}Reference URI="">`; - } else { - const id = this.ensureHasId(node); - ref.uri = id; - res += `<${prefix}Reference URI="#${id}">`; + res += `<${prefix}Reference URI=""${addattr}>`; + } + else { + const id = this.ensureHasId(node); + ref.uri = id; + res += `<${prefix}Reference URI="#${id}"${addattr}>`; } res += `<${prefix}Transforms>`; for (const trans of ref.transforms || []) {