xml-crypto-next 7.0.3 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/c14n-canonicalization-2.d.ts +37 -0
- package/build/c14n-canonicalization-2.d.ts.map +1 -0
- package/build/c14n-canonicalization-2.js +228 -0
- package/build/c14n-canonicalization.d.ts +48 -0
- package/build/c14n-canonicalization.d.ts.map +1 -0
- package/{lib → build}/c14n-canonicalization.js +95 -75
- package/{lib → build}/enveloped-signature.d.ts +1 -1
- package/build/enveloped-signature.d.ts.map +1 -0
- package/{lib → build}/enveloped-signature.js +4 -9
- package/build/exclusive-canonicalization.d.ts +57 -0
- package/build/exclusive-canonicalization.d.ts.map +1 -0
- package/{lib → build}/exclusive-canonicalization.js +116 -102
- package/{lib → build}/hash-algorithms.d.ts +5 -1
- package/build/hash-algorithms.d.ts.map +1 -0
- package/build/hash-algorithms.js +70 -0
- package/build/index.d.ts +9 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +23 -0
- package/build/signature-algorithms.d.ts +244 -0
- package/build/signature-algorithms.d.ts.map +1 -0
- package/build/signature-algorithms.js +480 -0
- package/{lib → build}/signed-xml.d.ts +2 -2
- package/build/signed-xml.d.ts.map +1 -0
- package/{lib → build}/signed-xml.js +143 -102
- package/{lib → build}/types.d.ts +43 -6
- package/build/types.d.ts.map +1 -0
- package/{lib → build}/types.js +1 -5
- package/{lib → build}/utils.d.ts +34 -4
- package/build/utils.d.ts.map +1 -0
- package/{lib → build}/utils.js +189 -62
- package/build/xmldsig2-transform.d.ts +18 -0
- package/build/xmldsig2-transform.d.ts.map +1 -0
- package/build/xmldsig2-transform.js +65 -0
- package/package.json +74 -73
- package/types/c14n-canonicalization-2.d.ts +38 -0
- package/types/c14n-canonicalization-2.d.ts.map +1 -0
- package/types/c14n-canonicalization.d.ts +49 -0
- package/types/c14n-canonicalization.d.ts.map +1 -0
- package/types/enveloped-signature.d.ts +8 -0
- package/types/enveloped-signature.d.ts.map +1 -0
- package/types/exclusive-canonicalization.d.ts +58 -0
- package/types/exclusive-canonicalization.d.ts.map +1 -0
- package/types/hash-algorithms.d.ts +26 -0
- package/types/hash-algorithms.d.ts.map +1 -0
- package/types/index.d.ts +10 -0
- package/types/index.d.ts.map +1 -0
- package/types/signature-algorithms.d.ts +112 -0
- package/types/signature-algorithms.d.ts.map +1 -0
- package/types/signed-xml.d.ts +237 -0
- package/types/signed-xml.d.ts.map +1 -0
- package/types/src/c14n-canonicalization-2.d.ts +38 -0
- package/types/src/c14n-canonicalization-2.d.ts.map +1 -0
- package/types/src/c14n-canonicalization.d.ts +49 -0
- package/types/src/c14n-canonicalization.d.ts.map +1 -0
- package/types/src/enveloped-signature.d.ts +8 -0
- package/types/src/enveloped-signature.d.ts.map +1 -0
- package/types/src/exclusive-canonicalization.d.ts +58 -0
- package/types/src/exclusive-canonicalization.d.ts.map +1 -0
- package/types/src/hash-algorithms.d.ts +26 -0
- package/types/src/hash-algorithms.d.ts.map +1 -0
- package/types/src/index.d.ts +10 -0
- package/types/src/index.d.ts.map +1 -0
- package/types/src/signature-algorithms.d.ts +245 -0
- package/types/src/signature-algorithms.d.ts.map +1 -0
- package/types/src/signed-xml.d.ts +237 -0
- package/types/src/signed-xml.d.ts.map +1 -0
- package/types/src/types.d.ts +182 -0
- package/types/src/types.d.ts.map +1 -0
- package/types/src/utils.d.ts +96 -0
- package/types/src/utils.d.ts.map +1 -0
- package/types/src/xmldsig2-transform.d.ts +19 -0
- package/types/src/xmldsig2-transform.d.ts.map +1 -0
- package/types/types.d.ts +182 -0
- package/types/types.d.ts.map +1 -0
- package/types/utils.d.ts +96 -0
- package/types/utils.d.ts.map +1 -0
- package/types/xmldsig2-transform.d.ts +19 -0
- package/types/xmldsig2-transform.d.ts.map +1 -0
- package/lib/c14n-canonicalization.d.ts +0 -39
- package/lib/c14n-canonicalization.js.map +0 -1
- package/lib/enveloped-signature.js.map +0 -1
- package/lib/exclusive-canonicalization.d.ts +0 -38
- package/lib/exclusive-canonicalization.js.map +0 -1
- package/lib/hash-algorithms.js +0 -84
- package/lib/hash-algorithms.js.map +0 -1
- package/lib/index.d.ts +0 -5
- package/lib/index.js +0 -28
- package/lib/index.js.map +0 -1
- package/lib/signature-algorithms.d.ts +0 -112
- package/lib/signature-algorithms.js +0 -304
- package/lib/signature-algorithms.js.map +0 -1
- package/lib/signed-xml.js.map +0 -1
- package/lib/types.js.map +0 -1
- package/lib/utils.js.map +0 -1
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { CanonicalizationOrTransformationAlgorithm, CanonicalizationOrTransformationAlgorithmProcessOptions, NamespacePrefix, RenderedNamespace, XmlVersion } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Canonical XML 2.0
|
|
4
|
+
* @see https://www.w3.org/TR/xml-c14n2/
|
|
5
|
+
*
|
|
6
|
+
* Key differences from C14N 1.1:
|
|
7
|
+
* - Inheritable attributes are handled differently
|
|
8
|
+
* - XML base attributes are inherited
|
|
9
|
+
* - XML language attributes are inherited
|
|
10
|
+
* - Default attributes are handled per XML spec
|
|
11
|
+
*/
|
|
12
|
+
export declare class C14nCanonicalization2 implements CanonicalizationOrTransformationAlgorithm {
|
|
13
|
+
protected includeComments: boolean;
|
|
14
|
+
protected xmlVersion: XmlVersion;
|
|
15
|
+
constructor(xmlVersion?: XmlVersion, includeComments?: boolean);
|
|
16
|
+
attrCompare(a: Attr, b: Attr): number;
|
|
17
|
+
nsCompare(a: {
|
|
18
|
+
prefix: string;
|
|
19
|
+
namespaceURI: string;
|
|
20
|
+
}, b: {
|
|
21
|
+
prefix: string;
|
|
22
|
+
namespaceURI: string;
|
|
23
|
+
}): number;
|
|
24
|
+
renderAttrs(node: Node): string;
|
|
25
|
+
renderNs(node: Element, prefixesInScope: string[], defaultNs: string, defaultNsForPrefix: Record<string, string>, ancestorNamespaces: NamespacePrefix[]): RenderedNamespace;
|
|
26
|
+
processInner(node: Node, prefixesInScope: string[], defaultNs: string, defaultNsForPrefix: Record<string, string>, ancestorNamespaces: NamespacePrefix[]): string;
|
|
27
|
+
renderComment(node: Comment): string;
|
|
28
|
+
process(node: Node, options: CanonicalizationOrTransformationAlgorithmProcessOptions): string;
|
|
29
|
+
getAlgorithmName(): string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Canonical XML 2.0 with comments
|
|
33
|
+
*/
|
|
34
|
+
export declare class C14nCanonicalization2WithComments extends C14nCanonicalization2 {
|
|
35
|
+
constructor(xmlVersion?: XmlVersion);
|
|
36
|
+
getAlgorithmName(): string;
|
|
37
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"c14n-canonicalization-2.d.ts","sourceRoot":"","sources":["../src/c14n-canonicalization-2.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,yCAAyC,EACzC,uDAAuD,EACvD,eAAe,EACf,iBAAiB,EACjB,UAAU,EACX,MAAM,YAAY,CAAC;AAOpB;;;;;;;;;GASG;AACH,qBAAa,qBAAsB,YAAW,yCAAyC;IACrF,SAAS,CAAC,eAAe,UAAS;IAClC,SAAS,CAAC,UAAU,EAAE,UAAU,CAAS;gBAE7B,UAAU,CAAC,EAAE,UAAU,EAAE,eAAe,CAAC,EAAE,OAAO;IAO9D,WAAW,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,GAAG,MAAM;IAoBrC,SAAS,CAAC,CAAC,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM;IAO3G,WAAW,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM;IAiC/B,QAAQ,CACN,IAAI,EAAE,OAAO,EACb,eAAe,EAAE,MAAM,EAAE,EACzB,SAAS,EAAE,MAAM,EACjB,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC1C,kBAAkB,EAAE,eAAe,EAAE,GACpC,iBAAiB;IA2EpB,YAAY,CACV,IAAI,EAAE,IAAI,EACV,eAAe,EAAE,MAAM,EAAE,EACzB,SAAS,EAAE,MAAM,EACjB,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC1C,kBAAkB,EAAE,eAAe,EAAE,GACpC,MAAM;IAoDT,aAAa,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM;IAwCpC,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,uDAAuD,GAAG,MAAM;IAoB7F,gBAAgB;CAGjB;AAED;;GAEG;AACH,qBAAa,iCAAkC,SAAQ,qBAAqB;gBAC9D,UAAU,CAAC,EAAE,UAAU;IAInC,gBAAgB;CAGjB"}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import * as utils from "./utils.js";
|
|
2
|
+
import * as isDomNode from "@xmldom/is-dom-node";
|
|
3
|
+
// Pre-compiled regex for better performance
|
|
4
|
+
const XMLNS_ATTR_REGEX = /^xmlns/;
|
|
5
|
+
/**
|
|
6
|
+
* Canonical XML 2.0
|
|
7
|
+
* @see https://www.w3.org/TR/xml-c14n2/
|
|
8
|
+
*
|
|
9
|
+
* Key differences from C14N 1.1:
|
|
10
|
+
* - Inheritable attributes are handled differently
|
|
11
|
+
* - XML base attributes are inherited
|
|
12
|
+
* - XML language attributes are inherited
|
|
13
|
+
* - Default attributes are handled per XML spec
|
|
14
|
+
*/
|
|
15
|
+
export class C14nCanonicalization2 {
|
|
16
|
+
includeComments = false;
|
|
17
|
+
xmlVersion = "1.1";
|
|
18
|
+
constructor(xmlVersion, includeComments) {
|
|
19
|
+
if (xmlVersion) {
|
|
20
|
+
this.xmlVersion = xmlVersion;
|
|
21
|
+
}
|
|
22
|
+
this.includeComments = includeComments ?? false;
|
|
23
|
+
}
|
|
24
|
+
attrCompare(a, b) {
|
|
25
|
+
if (!a.namespaceURI && b.namespaceURI) {
|
|
26
|
+
return -1;
|
|
27
|
+
}
|
|
28
|
+
if (!b.namespaceURI && a.namespaceURI) {
|
|
29
|
+
return 1;
|
|
30
|
+
}
|
|
31
|
+
const left = a.namespaceURI + a.localName;
|
|
32
|
+
const right = b.namespaceURI + b.localName;
|
|
33
|
+
if (left === right) {
|
|
34
|
+
return 0;
|
|
35
|
+
}
|
|
36
|
+
else if (left < right) {
|
|
37
|
+
return -1;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
return 1;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
nsCompare(a, b) {
|
|
44
|
+
if (a.prefix === b.prefix) {
|
|
45
|
+
return 0;
|
|
46
|
+
}
|
|
47
|
+
return a.prefix.localeCompare(b.prefix);
|
|
48
|
+
}
|
|
49
|
+
renderAttrs(node) {
|
|
50
|
+
if (isDomNode.isCommentNode(node)) {
|
|
51
|
+
return this.renderComment(node);
|
|
52
|
+
}
|
|
53
|
+
if (!isDomNode.isElementNode(node) || !node.attributes) {
|
|
54
|
+
return "";
|
|
55
|
+
}
|
|
56
|
+
const attrListToRender = [];
|
|
57
|
+
for (let i = 0; i < node.attributes.length; i++) {
|
|
58
|
+
const attr = node.attributes[i];
|
|
59
|
+
// Ignore namespace definition attributes
|
|
60
|
+
if (!XMLNS_ATTR_REGEX.test(attr.name)) {
|
|
61
|
+
attrListToRender.push(attr);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
attrListToRender.sort(this.attrCompare.bind(this));
|
|
65
|
+
const encodeFn = this.xmlVersion === "1.1"
|
|
66
|
+
? utils.encodeSpecialCharactersInAttributeXml11
|
|
67
|
+
: utils.encodeSpecialCharactersInAttribute;
|
|
68
|
+
const res = new Array(attrListToRender.length);
|
|
69
|
+
for (let i = 0; i < attrListToRender.length; i++) {
|
|
70
|
+
const attr = attrListToRender[i];
|
|
71
|
+
res[i] = ` ${attr.name}="${encodeFn(attr.value)}"`;
|
|
72
|
+
}
|
|
73
|
+
return res.join("");
|
|
74
|
+
}
|
|
75
|
+
renderNs(node, prefixesInScope, defaultNs, defaultNsForPrefix, ancestorNamespaces) {
|
|
76
|
+
const res = [];
|
|
77
|
+
let newDefaultNs = defaultNs;
|
|
78
|
+
const nsListToRender = [];
|
|
79
|
+
const currNs = node.namespaceURI || "";
|
|
80
|
+
// Handle the namespace of the node itself
|
|
81
|
+
if (node.prefix) {
|
|
82
|
+
if (prefixesInScope.indexOf(node.prefix) === -1) {
|
|
83
|
+
nsListToRender.push({
|
|
84
|
+
prefix: node.prefix,
|
|
85
|
+
namespaceURI: node.namespaceURI || defaultNsForPrefix[node.prefix],
|
|
86
|
+
});
|
|
87
|
+
prefixesInScope.push(node.prefix);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else if (defaultNs !== currNs) {
|
|
91
|
+
newDefaultNs = node.namespaceURI || "";
|
|
92
|
+
res.push(' xmlns="', newDefaultNs, '"');
|
|
93
|
+
}
|
|
94
|
+
// Handle the attributes namespace
|
|
95
|
+
if (node.attributes) {
|
|
96
|
+
for (let i = 0; i < node.attributes.length; i++) {
|
|
97
|
+
const attr = node.attributes[i];
|
|
98
|
+
if (attr.prefix === "xmlns" && prefixesInScope.indexOf(attr.localName) === -1) {
|
|
99
|
+
nsListToRender.push({ prefix: attr.localName, namespaceURI: attr.value });
|
|
100
|
+
prefixesInScope.push(attr.localName);
|
|
101
|
+
}
|
|
102
|
+
if (attr.prefix &&
|
|
103
|
+
prefixesInScope.indexOf(attr.prefix) === -1 &&
|
|
104
|
+
attr.prefix !== "xmlns" &&
|
|
105
|
+
attr.prefix !== "xml") {
|
|
106
|
+
nsListToRender.push({ prefix: attr.prefix, namespaceURI: attr.namespaceURI });
|
|
107
|
+
prefixesInScope.push(attr.prefix);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// Add ancestor namespaces
|
|
112
|
+
if (utils.isArrayHasLength(ancestorNamespaces)) {
|
|
113
|
+
for (const ancestorNamespace of ancestorNamespaces) {
|
|
114
|
+
let alreadyListed = false;
|
|
115
|
+
for (const nsToRender of nsListToRender) {
|
|
116
|
+
if (nsToRender.prefix === ancestorNamespace.prefix &&
|
|
117
|
+
nsToRender.namespaceURI === ancestorNamespace.namespaceURI) {
|
|
118
|
+
alreadyListed = true;
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (!alreadyListed) {
|
|
123
|
+
nsListToRender.push(ancestorNamespace);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
nsListToRender.sort(this.nsCompare.bind(this));
|
|
128
|
+
for (const attr of nsListToRender) {
|
|
129
|
+
if (attr.prefix) {
|
|
130
|
+
res.push(` xmlns:${attr.prefix}="${attr.namespaceURI}"`);
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
res.push(` xmlns="${attr.namespaceURI}"`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return { rendered: res.join(""), newDefaultNs };
|
|
137
|
+
}
|
|
138
|
+
processInner(node, prefixesInScope, defaultNs, defaultNsForPrefix, ancestorNamespaces) {
|
|
139
|
+
if (isDomNode.isCommentNode(node)) {
|
|
140
|
+
return this.renderComment(node);
|
|
141
|
+
}
|
|
142
|
+
if (isDomNode.isTextNode(node) && node.data) {
|
|
143
|
+
const encodeFn = this.xmlVersion === "1.1"
|
|
144
|
+
? utils.encodeSpecialCharactersInTextXml11
|
|
145
|
+
: utils.encodeSpecialCharactersInText;
|
|
146
|
+
return encodeFn(node.data);
|
|
147
|
+
}
|
|
148
|
+
// Handle CDATA sections
|
|
149
|
+
if (isDomNode.isCDATASectionNode(node) && node.data) {
|
|
150
|
+
const encodeFn = this.xmlVersion === "1.1"
|
|
151
|
+
? utils.encodeSpecialCharactersInTextXml11
|
|
152
|
+
: utils.encodeSpecialCharactersInText;
|
|
153
|
+
return encodeFn(node.data);
|
|
154
|
+
}
|
|
155
|
+
if (isDomNode.isElementNode(node)) {
|
|
156
|
+
const ns = this.renderNs(node, prefixesInScope, defaultNs, defaultNsForPrefix, ancestorNamespaces);
|
|
157
|
+
const childCount = node.childNodes.length;
|
|
158
|
+
const parts = new Array(childCount + 3);
|
|
159
|
+
parts[0] = `<${node.tagName}${ns.rendered}${this.renderAttrs(node)}>`;
|
|
160
|
+
let partIndex = 1;
|
|
161
|
+
for (let i = 0; i < childCount; i++) {
|
|
162
|
+
const pfxCopy = prefixesInScope.slice(0);
|
|
163
|
+
parts[partIndex++] = this.processInner(node.childNodes[i], pfxCopy, ns.newDefaultNs, defaultNsForPrefix, []);
|
|
164
|
+
}
|
|
165
|
+
parts[partIndex] = `</${node.tagName}>`;
|
|
166
|
+
return parts.join("");
|
|
167
|
+
}
|
|
168
|
+
throw new Error(`Unable to canonicalize node type: ${node.nodeType}`);
|
|
169
|
+
}
|
|
170
|
+
renderComment(node) {
|
|
171
|
+
if (!this.includeComments) {
|
|
172
|
+
return "";
|
|
173
|
+
}
|
|
174
|
+
const isOutsideDocument = node.ownerDocument === node.parentNode;
|
|
175
|
+
let isBeforeDocument = false;
|
|
176
|
+
let isAfterDocument = false;
|
|
177
|
+
if (isOutsideDocument) {
|
|
178
|
+
let nextNode = node;
|
|
179
|
+
let previousNode = node;
|
|
180
|
+
while (nextNode !== null) {
|
|
181
|
+
if (nextNode === node.ownerDocument.documentElement) {
|
|
182
|
+
isBeforeDocument = true;
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
nextNode = nextNode.nextSibling;
|
|
186
|
+
}
|
|
187
|
+
while (previousNode !== null) {
|
|
188
|
+
if (previousNode === node.ownerDocument.documentElement) {
|
|
189
|
+
isAfterDocument = true;
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
previousNode = previousNode.previousSibling;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const afterDocument = isAfterDocument ? "\n" : "";
|
|
196
|
+
const beforeDocument = isBeforeDocument ? "\n" : "";
|
|
197
|
+
const encodeFn = this.xmlVersion === "1.1"
|
|
198
|
+
? utils.encodeSpecialCharactersInTextXml11
|
|
199
|
+
: utils.encodeSpecialCharactersInText;
|
|
200
|
+
const encodedText = encodeFn(node.data);
|
|
201
|
+
return `${afterDocument}<!--${encodedText}-->${beforeDocument}`;
|
|
202
|
+
}
|
|
203
|
+
process(node, options) {
|
|
204
|
+
options = options || {};
|
|
205
|
+
const defaultNs = options.defaultNs || "";
|
|
206
|
+
const defaultNsForPrefix = options.defaultNsForPrefix || {};
|
|
207
|
+
const ancestorNamespaces = options.ancestorNamespaces || [];
|
|
208
|
+
const prefixesInScope = [];
|
|
209
|
+
for (let i = 0; i < ancestorNamespaces.length; i++) {
|
|
210
|
+
prefixesInScope.push(ancestorNamespaces[i].prefix);
|
|
211
|
+
}
|
|
212
|
+
return this.processInner(node, prefixesInScope, defaultNs, defaultNsForPrefix, ancestorNamespaces);
|
|
213
|
+
}
|
|
214
|
+
getAlgorithmName() {
|
|
215
|
+
return "http://www.w3.org/2010/xml-c14n2";
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Canonical XML 2.0 with comments
|
|
220
|
+
*/
|
|
221
|
+
export class C14nCanonicalization2WithComments extends C14nCanonicalization2 {
|
|
222
|
+
constructor(xmlVersion) {
|
|
223
|
+
super(xmlVersion, true);
|
|
224
|
+
}
|
|
225
|
+
getAlgorithmName() {
|
|
226
|
+
return "http://www.w3.org/2010/xml-c14n2#WithComments";
|
|
227
|
+
}
|
|
228
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { CanonicalizationOrTransformationAlgorithm, CanonicalizationOrTransformationAlgorithmProcessOptions, NamespacePrefix, RenderedNamespace, XmlVersion } from "./types.ts";
|
|
2
|
+
export declare class C14nCanonicalization implements CanonicalizationOrTransformationAlgorithm {
|
|
3
|
+
protected includeComments: boolean;
|
|
4
|
+
protected xmlVersion: XmlVersion;
|
|
5
|
+
constructor(xmlVersion?: XmlVersion);
|
|
6
|
+
attrCompare(a: Attr, b: Attr): number;
|
|
7
|
+
nsCompare(a: {
|
|
8
|
+
prefix: string;
|
|
9
|
+
namespaceURI: string;
|
|
10
|
+
}, b: {
|
|
11
|
+
prefix: string;
|
|
12
|
+
namespaceURI: string;
|
|
13
|
+
}): number;
|
|
14
|
+
renderAttrs(node: Node): string;
|
|
15
|
+
/**
|
|
16
|
+
* Create the string of all namespace declarations that should appear on this element
|
|
17
|
+
*/
|
|
18
|
+
renderNs(node: Element, prefixesInScope: string[], defaultNs: string, defaultNsForPrefix: Record<string, string>, ancestorNamespaces: NamespacePrefix[]): RenderedNamespace;
|
|
19
|
+
/**
|
|
20
|
+
* Process inner nodes with optimized string building
|
|
21
|
+
*/
|
|
22
|
+
processInner(node: Node, prefixesInScope: string[], defaultNs: string, defaultNsForPrefix: Record<string, string>, ancestorNamespaces: NamespacePrefix[]): string;
|
|
23
|
+
renderComment(node: Comment): string;
|
|
24
|
+
/**
|
|
25
|
+
* Perform canonicalization of the given node
|
|
26
|
+
*/
|
|
27
|
+
process(node: Node, options: CanonicalizationOrTransformationAlgorithmProcessOptions): string;
|
|
28
|
+
getAlgorithmName(): string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* C14n with comments support
|
|
32
|
+
*/
|
|
33
|
+
export declare class C14nCanonicalizationWithComments extends C14nCanonicalization {
|
|
34
|
+
constructor(xmlVersion?: XmlVersion);
|
|
35
|
+
getAlgorithmName(): string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* XML 1.1 specific C14n canonicalization
|
|
39
|
+
*/
|
|
40
|
+
export declare class C14nCanonicalizationXml11 extends C14nCanonicalization {
|
|
41
|
+
constructor();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* XML 1.1 C14n with comments support
|
|
45
|
+
*/
|
|
46
|
+
export declare class C14nCanonicalizationWithCommentsXml11 extends C14nCanonicalizationWithComments {
|
|
47
|
+
constructor();
|
|
48
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"c14n-canonicalization.d.ts","sourceRoot":"","sources":["../src/c14n-canonicalization.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,yCAAyC,EACzC,uDAAuD,EACvD,eAAe,EACf,iBAAiB,EACjB,UAAU,EACX,MAAM,YAAY,CAAC;AAOpB,qBAAa,oBAAqB,YAAW,yCAAyC;IACpF,SAAS,CAAC,eAAe,UAAS;IAClC,SAAS,CAAC,UAAU,EAAE,UAAU,CAAS;gBAE7B,UAAU,CAAC,EAAE,UAAU;IAOnC,WAAW,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,GAAG,MAAM;IAoBrC,SAAS,CAAC,CAAC,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM;IAO3G,WAAW,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM;IAiC/B;;OAEG;IACH,QAAQ,CACN,IAAI,EAAE,OAAO,EACb,eAAe,EAAE,MAAM,EAAE,EACzB,SAAS,EAAE,MAAM,EACjB,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC1C,kBAAkB,EAAE,eAAe,EAAE,GACpC,iBAAiB;IA+EpB;;OAEG;IACH,YAAY,CACV,IAAI,EAAE,IAAI,EACV,eAAe,EAAE,MAAM,EAAE,EACzB,SAAS,EAAE,MAAM,EACjB,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC1C,kBAAkB,EAAE,eAAe,EAAE,GACpC,MAAM;IAsDT,aAAa,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM;IAwCpC;;OAEG;IACH,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,uDAAuD,GAAG,MAAM;IAoB7F,gBAAgB;CAGjB;AAED;;GAEG;AACH,qBAAa,gCAAiC,SAAQ,oBAAoB;gBAC5D,UAAU,CAAC,EAAE,UAAU;IAKnC,gBAAgB;CAGjB;AAED;;GAEG;AACH,qBAAa,yBAA0B,SAAQ,oBAAoB;;CAIlE;AAED;;GAEG;AACH,qBAAa,qCAAsC,SAAQ,gCAAgC;;CAI1F"}
|
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import * as utils from "./utils.js";
|
|
2
|
+
import * as isDomNode from "@xmldom/is-dom-node";
|
|
3
|
+
// Pre-compiled regex for better performance
|
|
4
|
+
const XMLNS_ATTR_REGEX = /^xmlns/;
|
|
5
|
+
export class C14nCanonicalization {
|
|
6
|
+
includeComments = false;
|
|
7
|
+
xmlVersion = "1.0";
|
|
8
|
+
constructor(xmlVersion) {
|
|
9
9
|
this.includeComments = false;
|
|
10
|
+
if (xmlVersion) {
|
|
11
|
+
this.xmlVersion = xmlVersion;
|
|
12
|
+
}
|
|
10
13
|
}
|
|
11
14
|
attrCompare(a, b) {
|
|
12
15
|
if (!a.namespaceURI && b.namespaceURI) {
|
|
@@ -28,54 +31,46 @@ class C14nCanonicalization {
|
|
|
28
31
|
}
|
|
29
32
|
}
|
|
30
33
|
nsCompare(a, b) {
|
|
31
|
-
|
|
32
|
-
const attr2 = b.prefix;
|
|
33
|
-
if (attr1 === attr2) {
|
|
34
|
+
if (a.prefix === b.prefix) {
|
|
34
35
|
return 0;
|
|
35
36
|
}
|
|
36
|
-
return
|
|
37
|
+
return a.prefix.localeCompare(b.prefix);
|
|
37
38
|
}
|
|
38
39
|
renderAttrs(node) {
|
|
39
|
-
let i;
|
|
40
|
-
let attr;
|
|
41
|
-
const attrListToRender = [];
|
|
42
40
|
if (isDomNode.isCommentNode(node)) {
|
|
43
41
|
return this.renderComment(node);
|
|
44
42
|
}
|
|
45
|
-
if (node.attributes) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
43
|
+
if (!isDomNode.isElementNode(node) || !node.attributes) {
|
|
44
|
+
return "";
|
|
45
|
+
}
|
|
46
|
+
const attrListToRender = [];
|
|
47
|
+
for (let i = 0; i < node.attributes.length; i++) {
|
|
48
|
+
const attr = node.attributes[i];
|
|
49
|
+
// Ignore namespace definition attributes
|
|
50
|
+
if (!XMLNS_ATTR_REGEX.test(attr.name)) {
|
|
52
51
|
attrListToRender.push(attr);
|
|
53
52
|
}
|
|
54
53
|
}
|
|
55
|
-
attrListToRender.sort(this.attrCompare);
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
54
|
+
attrListToRender.sort(this.attrCompare.bind(this));
|
|
55
|
+
const encodeFn = this.xmlVersion === "1.1"
|
|
56
|
+
? utils.encodeSpecialCharactersInAttributeXml11
|
|
57
|
+
: utils.encodeSpecialCharactersInAttribute;
|
|
58
|
+
const res = new Array(attrListToRender.length);
|
|
59
|
+
for (let i = 0; i < attrListToRender.length; i++) {
|
|
60
|
+
const attr = attrListToRender[i];
|
|
61
|
+
res[i] = ` ${attr.name}="${encodeFn(attr.value)}"`;
|
|
62
|
+
}
|
|
59
63
|
return res.join("");
|
|
60
64
|
}
|
|
61
65
|
/**
|
|
62
66
|
* Create the string of all namespace declarations that should appear on this element
|
|
63
|
-
*
|
|
64
|
-
* @param node The node we now render
|
|
65
|
-
* @param prefixesInScope The prefixes defined on this node parents which are a part of the output set
|
|
66
|
-
* @param defaultNs The current default namespace
|
|
67
|
-
* @param defaultNsForPrefix
|
|
68
|
-
* @param ancestorNamespaces Import ancestor namespaces if it is specified
|
|
69
|
-
* @api private
|
|
70
67
|
*/
|
|
71
68
|
renderNs(node, prefixesInScope, defaultNs, defaultNsForPrefix, ancestorNamespaces) {
|
|
72
|
-
let i;
|
|
73
|
-
let attr;
|
|
74
69
|
const res = [];
|
|
75
70
|
let newDefaultNs = defaultNs;
|
|
76
71
|
const nsListToRender = [];
|
|
77
72
|
const currNs = node.namespaceURI || "";
|
|
78
|
-
//
|
|
73
|
+
// Handle the namespace of the node itself
|
|
79
74
|
if (node.prefix) {
|
|
80
75
|
if (prefixesInScope.indexOf(node.prefix) === -1) {
|
|
81
76
|
nsListToRender.push({
|
|
@@ -86,22 +81,20 @@ class C14nCanonicalization {
|
|
|
86
81
|
}
|
|
87
82
|
}
|
|
88
83
|
else if (defaultNs !== currNs) {
|
|
89
|
-
//
|
|
84
|
+
// New default ns
|
|
90
85
|
newDefaultNs = node.namespaceURI || "";
|
|
91
86
|
res.push(' xmlns="', newDefaultNs, '"');
|
|
92
87
|
}
|
|
93
|
-
//
|
|
88
|
+
// Handle the attributes namespace
|
|
94
89
|
if (node.attributes) {
|
|
95
|
-
for (i = 0; i < node.attributes.length; ++
|
|
96
|
-
attr = node.attributes[i];
|
|
97
|
-
//
|
|
98
|
-
//the prefix is not defined already. New prefixes can only be defined by `xmlns:`.
|
|
90
|
+
for (let i = 0; i < node.attributes.length; i++) {
|
|
91
|
+
const attr = node.attributes[i];
|
|
92
|
+
// Handle all prefixed attributes that are included in the prefix list
|
|
99
93
|
if (attr.prefix === "xmlns" && prefixesInScope.indexOf(attr.localName) === -1) {
|
|
100
94
|
nsListToRender.push({ prefix: attr.localName, namespaceURI: attr.value });
|
|
101
95
|
prefixesInScope.push(attr.localName);
|
|
102
96
|
}
|
|
103
|
-
//
|
|
104
|
-
//the prefix is not defined already
|
|
97
|
+
// Handle all prefixed attributes that are not xmlns definitions
|
|
105
98
|
if (attr.prefix &&
|
|
106
99
|
prefixesInScope.indexOf(attr.prefix) === -1 &&
|
|
107
100
|
attr.prefix !== "xmlns" &&
|
|
@@ -111,14 +104,15 @@ class C14nCanonicalization {
|
|
|
111
104
|
}
|
|
112
105
|
}
|
|
113
106
|
}
|
|
107
|
+
// Add ancestor namespaces
|
|
114
108
|
if (utils.isArrayHasLength(ancestorNamespaces)) {
|
|
115
|
-
// Remove namespaces which are already present in nsListToRender
|
|
116
109
|
for (const ancestorNamespace of ancestorNamespaces) {
|
|
117
110
|
let alreadyListed = false;
|
|
118
111
|
for (const nsToRender of nsListToRender) {
|
|
119
112
|
if (nsToRender.prefix === ancestorNamespace.prefix &&
|
|
120
113
|
nsToRender.namespaceURI === ancestorNamespace.namespaceURI) {
|
|
121
114
|
alreadyListed = true;
|
|
115
|
+
break;
|
|
122
116
|
}
|
|
123
117
|
}
|
|
124
118
|
if (!alreadyListed) {
|
|
@@ -126,37 +120,51 @@ class C14nCanonicalization {
|
|
|
126
120
|
}
|
|
127
121
|
}
|
|
128
122
|
}
|
|
129
|
-
nsListToRender.sort(this.nsCompare);
|
|
130
|
-
//
|
|
131
|
-
|
|
123
|
+
nsListToRender.sort(this.nsCompare.bind(this));
|
|
124
|
+
// Render namespaces
|
|
125
|
+
for (const attr of nsListToRender) {
|
|
132
126
|
if (attr.prefix) {
|
|
133
|
-
|
|
127
|
+
res.push(` xmlns:${attr.prefix}="${attr.namespaceURI}"`);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
res.push(` xmlns="${attr.namespaceURI}"`);
|
|
134
131
|
}
|
|
135
|
-
|
|
136
|
-
}));
|
|
132
|
+
}
|
|
137
133
|
return { rendered: res.join(""), newDefaultNs };
|
|
138
134
|
}
|
|
139
135
|
/**
|
|
140
|
-
*
|
|
136
|
+
* Process inner nodes with optimized string building
|
|
141
137
|
*/
|
|
142
138
|
processInner(node, prefixesInScope, defaultNs, defaultNsForPrefix, ancestorNamespaces) {
|
|
143
139
|
if (isDomNode.isCommentNode(node)) {
|
|
144
140
|
return this.renderComment(node);
|
|
145
141
|
}
|
|
146
|
-
if (node.data) {
|
|
147
|
-
|
|
142
|
+
if (isDomNode.isTextNode(node) && node.data) {
|
|
143
|
+
const encodeFn = this.xmlVersion === "1.1"
|
|
144
|
+
? utils.encodeSpecialCharactersInTextXml11
|
|
145
|
+
: utils.encodeSpecialCharactersInText;
|
|
146
|
+
return encodeFn(node.data);
|
|
147
|
+
}
|
|
148
|
+
// Handle CDATA sections - treat as text content
|
|
149
|
+
if (isDomNode.isCDATASectionNode(node) && node.data) {
|
|
150
|
+
const encodeFn = this.xmlVersion === "1.1"
|
|
151
|
+
? utils.encodeSpecialCharactersInTextXml11
|
|
152
|
+
: utils.encodeSpecialCharactersInText;
|
|
153
|
+
return encodeFn(node.data);
|
|
148
154
|
}
|
|
149
155
|
if (isDomNode.isElementNode(node)) {
|
|
150
|
-
let i;
|
|
151
|
-
let pfxCopy;
|
|
152
156
|
const ns = this.renderNs(node, prefixesInScope, defaultNs, defaultNsForPrefix, ancestorNamespaces);
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
+
// Pre-allocate array for better performance
|
|
158
|
+
const childCount = node.childNodes.length;
|
|
159
|
+
const parts = new Array(childCount + 3);
|
|
160
|
+
parts[0] = `<${node.tagName}${ns.rendered}${this.renderAttrs(node)}>`;
|
|
161
|
+
let partIndex = 1;
|
|
162
|
+
for (let i = 0; i < childCount; i++) {
|
|
163
|
+
const pfxCopy = prefixesInScope.slice(0);
|
|
164
|
+
parts[partIndex++] = this.processInner(node.childNodes[i], pfxCopy, ns.newDefaultNs, defaultNsForPrefix, []);
|
|
157
165
|
}
|
|
158
|
-
|
|
159
|
-
return
|
|
166
|
+
parts[partIndex] = `</${node.tagName}>`;
|
|
167
|
+
return parts.join("");
|
|
160
168
|
}
|
|
161
169
|
throw new Error(`Unable to canonicalize node type: ${node.nodeType}`);
|
|
162
170
|
}
|
|
@@ -188,14 +196,14 @@ class C14nCanonicalization {
|
|
|
188
196
|
}
|
|
189
197
|
const afterDocument = isAfterDocument ? "\n" : "";
|
|
190
198
|
const beforeDocument = isBeforeDocument ? "\n" : "";
|
|
191
|
-
const
|
|
199
|
+
const encodeFn = this.xmlVersion === "1.1"
|
|
200
|
+
? utils.encodeSpecialCharactersInTextXml11
|
|
201
|
+
: utils.encodeSpecialCharactersInText;
|
|
202
|
+
const encodedText = encodeFn(node.data);
|
|
192
203
|
return `${afterDocument}<!--${encodedText}-->${beforeDocument}`;
|
|
193
204
|
}
|
|
194
205
|
/**
|
|
195
206
|
* Perform canonicalization of the given node
|
|
196
|
-
*
|
|
197
|
-
* @param node
|
|
198
|
-
* @api public
|
|
199
207
|
*/
|
|
200
208
|
process(node, options) {
|
|
201
209
|
options = options || {};
|
|
@@ -206,25 +214,37 @@ class C14nCanonicalization {
|
|
|
206
214
|
for (let i = 0; i < ancestorNamespaces.length; i++) {
|
|
207
215
|
prefixesInScope.push(ancestorNamespaces[i].prefix);
|
|
208
216
|
}
|
|
209
|
-
|
|
210
|
-
return res;
|
|
217
|
+
return this.processInner(node, prefixesInScope, defaultNs, defaultNsForPrefix, ancestorNamespaces);
|
|
211
218
|
}
|
|
212
219
|
getAlgorithmName() {
|
|
213
220
|
return "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
|
|
214
221
|
}
|
|
215
222
|
}
|
|
216
|
-
exports.C14nCanonicalization = C14nCanonicalization;
|
|
217
223
|
/**
|
|
218
|
-
*
|
|
224
|
+
* C14n with comments support
|
|
219
225
|
*/
|
|
220
|
-
class C14nCanonicalizationWithComments extends C14nCanonicalization {
|
|
221
|
-
constructor() {
|
|
222
|
-
super();
|
|
226
|
+
export class C14nCanonicalizationWithComments extends C14nCanonicalization {
|
|
227
|
+
constructor(xmlVersion) {
|
|
228
|
+
super(xmlVersion);
|
|
223
229
|
this.includeComments = true;
|
|
224
230
|
}
|
|
225
231
|
getAlgorithmName() {
|
|
226
232
|
return "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments";
|
|
227
233
|
}
|
|
228
234
|
}
|
|
229
|
-
|
|
230
|
-
|
|
235
|
+
/**
|
|
236
|
+
* XML 1.1 specific C14n canonicalization
|
|
237
|
+
*/
|
|
238
|
+
export class C14nCanonicalizationXml11 extends C14nCanonicalization {
|
|
239
|
+
constructor() {
|
|
240
|
+
super("1.1");
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* XML 1.1 C14n with comments support
|
|
245
|
+
*/
|
|
246
|
+
export class C14nCanonicalizationWithCommentsXml11 extends C14nCanonicalizationWithComments {
|
|
247
|
+
constructor() {
|
|
248
|
+
super("1.1");
|
|
249
|
+
}
|
|
250
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CanonicalizationOrTransformationAlgorithm, CanonicalizationOrTransformationAlgorithmProcessOptions, CanonicalizationOrTransformAlgorithmType } from "./types";
|
|
1
|
+
import type { CanonicalizationOrTransformationAlgorithm, CanonicalizationOrTransformationAlgorithmProcessOptions, CanonicalizationOrTransformAlgorithmType } from "./types.ts";
|
|
2
2
|
export declare class EnvelopedSignature implements CanonicalizationOrTransformationAlgorithm {
|
|
3
3
|
protected includeComments: boolean;
|
|
4
4
|
constructor();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enveloped-signature.d.ts","sourceRoot":"","sources":["../src/enveloped-signature.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,yCAAyC,EACzC,uDAAuD,EACvD,wCAAwC,EACzC,MAAM,YAAY,CAAC;AAEpB,qBAAa,kBAAmB,YAAW,yCAAyC;IAClF,SAAS,CAAC,eAAe,UAAS;;IAMlC,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,uDAAuD,GAAG,IAAI;IAyC3F,gBAAgB,IAAI,wCAAwC;CAG7D"}
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const isDomNode = require("@xmldom/is-dom-node");
|
|
6
|
-
class EnvelopedSignature {
|
|
1
|
+
import * as xpath from "xpath";
|
|
2
|
+
import * as isDomNode from "@xmldom/is-dom-node";
|
|
3
|
+
export class EnvelopedSignature {
|
|
4
|
+
includeComments = false;
|
|
7
5
|
constructor() {
|
|
8
6
|
this.includeComments = false;
|
|
9
|
-
this.includeComments = false;
|
|
10
7
|
}
|
|
11
8
|
process(node, options) {
|
|
12
9
|
if (null == options.signatureNode) {
|
|
@@ -39,5 +36,3 @@ class EnvelopedSignature {
|
|
|
39
36
|
return "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
|
|
40
37
|
}
|
|
41
38
|
}
|
|
42
|
-
exports.EnvelopedSignature = EnvelopedSignature;
|
|
43
|
-
//# sourceMappingURL=enveloped-signature.js.map
|