Skip to content

Commit 8f682f0

Browse files
committed
Basic xades support
1 parent 5a0d317 commit 8f682f0

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/signed-xml.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ export class SignedXml {
5555
keyInfoAttributes: { [attrName: string]: string } = {};
5656
getKeyInfoContent = SignedXml.getKeyInfoContent;
5757
getCertFromKeyInfo = SignedXml.getCertFromKeyInfo;
58+
//xadesQualifyingProperties?: null;
59+
xadesQualifyingProperties: (() => string) | null;
5860

5961
// Internal state
6062
private id = 0;
@@ -151,6 +153,7 @@ export class SignedXml {
151153
this.keyInfoAttributes = keyInfoAttributes ?? this.keyInfoAttributes;
152154
this.getKeyInfoContent = getKeyInfoContent ?? SignedXml.noop;
153155
this.getCertFromKeyInfo = getCertFromKeyInfo ?? this.getCertFromKeyInfo;
156+
this.xadesQualifyingProperties = null;
154157
this.CanonicalizationAlgorithms;
155158
this.HashAlgorithms;
156159
this.SignatureAlgorithms;
@@ -802,6 +805,9 @@ export class SignedXml {
802805

803806
signatureXml += this.createSignedInfo(doc, prefix);
804807
signatureXml += this.getKeyInfo(prefix);
808+
if (this.xadesQualifyingProperties != null) {
809+
signatureXml += `<${currentPrefix}Object>${this.xadesQualifyingProperties()}</${currentPrefix}Object>`;
810+
}
805811
signatureXml += `</${currentPrefix}Signature>`;
806812

807813
this.originalXmlWithIds = doc.toString();
@@ -918,21 +924,34 @@ export class SignedXml {
918924
prefix = prefix ? `${prefix}:` : prefix;
919925

920926
for (const ref of this.getReferences()) {
921-
const nodes = xpath.selectWithResolver(ref.xpath ?? "", doc, this.namespaceResolver);
927+
let nodes = xpath.selectWithResolver(ref.xpath ?? "", doc, this.namespaceResolver);
922928

923929
if (!utils.isArrayHasLength(nodes)) {
924-
throw new Error(
925-
`the following xpath cannot be signed because it was not found: ${ref.xpath}`,
926-
);
930+
if (this.xadesQualifyingProperties != null) {
931+
nodes = xpath.selectWithResolver(
932+
ref.xpath ?? "",
933+
new xmldom.DOMParser().parseFromString(this.xadesQualifyingProperties()),
934+
this.namespaceResolver,
935+
);
936+
}
937+
if (!utils.isArrayHasLength(nodes)) {
938+
throw new Error(
939+
`the following xpath cannot be signed because it was not found: ${ref.xpath}`,
940+
);
941+
}
927942
}
928943

929944
for (const node of nodes) {
945+
let addattr = "";
946+
if (node["localName"] === "SignedProperties") {
947+
addattr = ' Type="http://uri.etsi.org/01903#SignedProperties"';
948+
}
930949
if (ref.isEmptyUri) {
931-
res += `<${prefix}Reference URI="">`;
950+
res += `<${prefix}Reference URI=""${addattr}>`;
932951
} else {
933952
const id = this.ensureHasId(node);
934953
ref.uri = id;
935-
res += `<${prefix}Reference URI="#${id}">`;
954+
res += `<${prefix}Reference URI="#${id}"${addattr}>`;
936955
}
937956
res += `<${prefix}Transforms>`;
938957
for (const trans of ref.transforms || []) {

0 commit comments

Comments
 (0)