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

Basic xades support #444

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
31 changes: 25 additions & 6 deletions src/signed-xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
keyInfoAttributes: { [attrName: string]: string } = {};
getKeyInfoContent = SignedXml.getKeyInfoContent;
getCertFromKeyInfo = SignedXml.getCertFromKeyInfo;
//xadesQualifyingProperties?: null;
xadesQualifyingProperties: (() => string) | null;

// Internal state
private id = 0;
Expand Down Expand Up @@ -151,6 +153,7 @@
this.keyInfoAttributes = keyInfoAttributes ?? this.keyInfoAttributes;
this.getKeyInfoContent = getKeyInfoContent ?? this.getKeyInfoContent;
this.getCertFromKeyInfo = getCertFromKeyInfo ?? SignedXml.noop;
this.xadesQualifyingProperties = null;
this.CanonicalizationAlgorithms;
this.HashAlgorithms;
this.SignatureAlgorithms;
Expand Down Expand Up @@ -802,6 +805,9 @@

signatureXml += this.createSignedInfo(doc, prefix);
signatureXml += this.getKeyInfo(prefix);
if (this.xadesQualifyingProperties != null) {
signatureXml += `<${currentPrefix}Object>${this.xadesQualifyingProperties()}</${currentPrefix}Object>`;

Check warning on line 809 in src/signed-xml.ts

View check run for this annotation

Codecov / codecov/patch

src/signed-xml.ts#L809

Added line #L809 was not covered by tests
}
signatureXml += `</${currentPrefix}Signature>`;

this.originalXmlWithIds = doc.toString();
Expand Down Expand Up @@ -918,21 +924,34 @@
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(

Check warning on line 931 in src/signed-xml.ts

View check run for this annotation

Codecov / codecov/patch

src/signed-xml.ts#L931

Added line #L931 was not covered by tests
ref.xpath ?? "",
new xmldom.DOMParser().parseFromString(this.xadesQualifyingProperties()),
this.namespaceResolver,
);
}
if (!utils.isArrayHasLength(nodes)) {
throw new Error(

Check warning on line 938 in src/signed-xml.ts

View check run for this annotation

Codecov / codecov/patch

src/signed-xml.ts#L938

Added line #L938 was not covered by tests
`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"';

Check warning on line 947 in src/signed-xml.ts

View check run for this annotation

Codecov / codecov/patch

src/signed-xml.ts#L947

Added line #L947 was not covered by tests
}
if (ref.isEmptyUri) {
res += `<${prefix}Reference URI="">`;
res += `<${prefix}Reference URI=""${addattr}>`;
} else {
const id = this.ensureHasId(node);
ref.uri = id;
res += `<${prefix}Reference URI="#${id}">`;
res += `<${prefix}Reference URI="#${id}"${addattr}>`;
}
res += `<${prefix}Transforms>`;
for (const trans of ref.transforms || []) {
Expand Down
Loading