Skip to content

Commit e9a1db7

Browse files
committed
refactor: change ...AlgorithmName to ...AlgorithmURI to more accurately reflect the contents, change Digest... back to Hash... to remain consistent with the existing library, consistently use XMLDSIG_URIS in the library instead of hardcoded strings
1 parent 56c48fd commit e9a1db7

22 files changed

+380
-398
lines changed

src/c14n-canonicalization.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {
2-
CanonicalizationAlgorithmName,
2+
CanonicalizationAlgorithmURI,
33
CanonicalizationAlgorithm,
44
TransformAlgorithmOptions,
55
NamespacePrefix,
@@ -278,7 +278,7 @@ export class C14nCanonicalization implements CanonicalizationAlgorithm {
278278
return res;
279279
}
280280

281-
getAlgorithmName(): CanonicalizationAlgorithmName {
281+
getAlgorithmName(): CanonicalizationAlgorithmURI {
282282
return XMLDSIG_URIS.CANONICALIZATION_ALGORITHMS.C14N;
283283
}
284284
}
@@ -292,7 +292,7 @@ export class C14nCanonicalizationWithComments extends C14nCanonicalization {
292292
this.includeComments = true;
293293
}
294294

295-
getAlgorithmName(): CanonicalizationAlgorithmName {
295+
getAlgorithmName(): CanonicalizationAlgorithmURI {
296296
return XMLDSIG_URIS.CANONICALIZATION_ALGORITHMS.C14N_WITH_COMMENTS;
297297
}
298298
}

src/enveloped-signature.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import * as xpath from "xpath";
22
import * as isDomNode from "@xmldom/is-dom-node";
3-
4-
import type {
5-
TransformAlgorithmOptions,
6-
TransformAlgorithm,
7-
TransformAlgorithmName,
8-
} from "./types";
3+
import { XMLDSIG_URIS } from "./xmldsig-uris";
4+
import type { TransformAlgorithmOptions, TransformAlgorithm, TransformAlgorithmURI } from "./types";
95

106
export class EnvelopedSignature implements TransformAlgorithm {
117
protected includeComments = false;
@@ -17,7 +13,7 @@ export class EnvelopedSignature implements TransformAlgorithm {
1713
process(node: Node, options: TransformAlgorithmOptions): Node {
1814
if (null == options.signatureNode) {
1915
const signature = xpath.select1(
20-
"./*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']",
16+
`./*[local-name(.)='Signature' and namespace-uri(.)='${XMLDSIG_URIS.NAMESPACES.ds}']`,
2117
node,
2218
);
2319
if (isDomNode.isNodeLike(signature) && signature.parentNode) {
@@ -34,7 +30,7 @@ export class EnvelopedSignature implements TransformAlgorithm {
3430
const expectedSignatureValueData = expectedSignatureValue.data;
3531

3632
const signatures = xpath.select(
37-
".//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']",
33+
`.//*[local-name(.)='Signature' and namespace-uri(.)='${XMLDSIG_URIS.NAMESPACES.ds}']`,
3834
node,
3935
);
4036
for (const nodeSignature of Array.isArray(signatures) ? signatures : []) {
@@ -55,7 +51,7 @@ export class EnvelopedSignature implements TransformAlgorithm {
5551
return node;
5652
}
5753

58-
getAlgorithmName(): TransformAlgorithmName {
59-
return "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
54+
getAlgorithmName(): TransformAlgorithmURI {
55+
return XMLDSIG_URIS.TRANSFORM_ALGORITHMS.ENVELOPED_SIGNATURE;
6056
}
6157
}

src/exclusive-canonicalization.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {
2-
CanonicalizationAlgorithmName,
2+
CanonicalizationAlgorithmURI,
33
CanonicalizationAlgorithm,
44
TransformAlgorithmOptions,
55
NamespacePrefix,
@@ -301,7 +301,7 @@ export class ExclusiveCanonicalization implements CanonicalizationAlgorithm {
301301
ancestorNamespaces.forEach(function (ancestorNamespace) {
302302
if (prefix === ancestorNamespace.prefix) {
303303
elem.setAttributeNS(
304-
"http://www.w3.org/2000/xmlns/",
304+
XMLDSIG_URIS.NAMESPACES.xmlns,
305305
`xmlns:${prefix}`,
306306
ancestorNamespace.namespaceURI,
307307
);
@@ -321,7 +321,7 @@ export class ExclusiveCanonicalization implements CanonicalizationAlgorithm {
321321
return res;
322322
}
323323

324-
getAlgorithmName(): CanonicalizationAlgorithmName {
324+
getAlgorithmName(): CanonicalizationAlgorithmURI {
325325
return XMLDSIG_URIS.CANONICALIZATION_ALGORITHMS.EXCLUSIVE_C14N;
326326
}
327327
}
@@ -332,7 +332,7 @@ export class ExclusiveCanonicalizationWithComments extends ExclusiveCanonicaliza
332332
this.includeComments = true;
333333
}
334334

335-
getAlgorithmName(): CanonicalizationAlgorithmName {
335+
getAlgorithmName(): CanonicalizationAlgorithmURI {
336336
return XMLDSIG_URIS.CANONICALIZATION_ALGORITHMS.EXCLUSIVE_C14N_WITH_COMMENTS;
337337
}
338338
}

src/hash-algorithms.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as crypto from "crypto";
22
import type { HashAlgorithm } from "./types";
3+
import { XMLDSIG_URIS } from "./xmldsig-uris";
34

45
export class Sha1 implements HashAlgorithm {
56
getHash = function (xml) {
@@ -10,7 +11,7 @@ export class Sha1 implements HashAlgorithm {
1011
};
1112

1213
getAlgorithmName = function () {
13-
return "http://www.w3.org/2000/09/xmldsig#sha1";
14+
return XMLDSIG_URIS.HASH_ALGORITHMS.SHA1;
1415
};
1516
}
1617

@@ -23,7 +24,7 @@ export class Sha256 implements HashAlgorithm {
2324
};
2425

2526
getAlgorithmName = function () {
26-
return "http://www.w3.org/2001/04/xmlenc#sha256";
27+
return XMLDSIG_URIS.HASH_ALGORITHMS.SHA256;
2728
};
2829
}
2930

@@ -36,6 +37,6 @@ export class Sha512 implements HashAlgorithm {
3637
};
3738

3839
getAlgorithmName = function () {
39-
return "http://www.w3.org/2001/04/xmlenc#sha512";
40+
return XMLDSIG_URIS.HASH_ALGORITHMS.SHA512;
4041
};
4142
}

src/signature-algorithms.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as crypto from "crypto";
22
import { type SignatureAlgorithm, createOptionalCallbackFunction } from "./types";
3+
import { XMLDSIG_URIS } from "./xmldsig-uris";
34

45
export class RsaSha1 implements SignatureAlgorithm {
56
getSignature = createOptionalCallbackFunction(
@@ -23,7 +24,7 @@ export class RsaSha1 implements SignatureAlgorithm {
2324
);
2425

2526
getAlgorithmName = () => {
26-
return "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
27+
return XMLDSIG_URIS.SIGNATURE_ALGORITHMS.RSA_SHA1;
2728
};
2829
}
2930

@@ -49,7 +50,7 @@ export class RsaSha256 implements SignatureAlgorithm {
4950
);
5051

5152
getAlgorithmName = () => {
52-
return "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";
53+
return XMLDSIG_URIS.SIGNATURE_ALGORITHMS.RSA_SHA256;
5354
};
5455
}
5556

@@ -96,7 +97,7 @@ export class RsaSha256Mgf1 implements SignatureAlgorithm {
9697
);
9798

9899
getAlgorithmName = () => {
99-
return "http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1";
100+
return XMLDSIG_URIS.SIGNATURE_ALGORITHMS.RSA_SHA256_MGF1;
100101
};
101102
}
102103

@@ -122,7 +123,7 @@ export class RsaSha512 implements SignatureAlgorithm {
122123
);
123124

124125
getAlgorithmName = () => {
125-
return "http://www.w3.org/2001/04/xmldsig-more#rsa-sha512";
126+
return XMLDSIG_URIS.SIGNATURE_ALGORITHMS.RSA_SHA512;
126127
};
127128
}
128129

@@ -148,6 +149,6 @@ export class HmacSha1 implements SignatureAlgorithm {
148149
);
149150

150151
getAlgorithmName = () => {
151-
return "http://www.w3.org/2000/09/xmldsig#hmac-sha1";
152+
return XMLDSIG_URIS.SIGNATURE_ALGORITHMS.HMAC_SHA1;
152153
};
153154
}

src/signed-xml.ts

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import type {
2-
CanonicalizationAlgorithmName,
3-
TransformAlgorithmName,
2+
CanonicalizationAlgorithmURI,
3+
TransformAlgorithmURI,
44
TransformAlgorithmOptions,
55
ComputeSignatureOptions,
66
ErrorFirstCallback,
77
GetKeyInfoContentArgs,
8-
HashAlgorithmName,
8+
HashAlgorithmURI,
99
IdAttributeType,
1010
ObjectAttributes,
1111
Reference,
12-
SignatureAlgorithmName,
12+
SignatureAlgorithmURI,
1313
SignedXmlOptions,
14-
DigestAlgorithmMap,
14+
HashAlgorithmMap,
1515
SignatureAlgorithmMap,
1616
CanonicalizationAlgorithmMap,
1717
TransformAlgorithmMap,
@@ -32,7 +32,7 @@ import * as utils from "./utils";
3232
import { XMLDSIG_URIS } from "./xmldsig-uris";
3333
const {
3434
CANONICALIZATION_ALGORITHMS,
35-
DIGEST_ALGORITHMS,
35+
HASH_ALGORITHMS,
3636
SIGNATURE_ALGORITHMS,
3737
TRANSFORM_ALGORITHMS,
3838
NAMESPACES,
@@ -60,13 +60,13 @@ export class SignedXml {
6060
publicCert?: crypto.KeyLike;
6161
/**
6262
* One of the supported signature algorithms.
63-
* @see {@link SignatureAlgorithmName}
63+
* @see {@link SignatureAlgorithmURI}
6464
*/
65-
signatureAlgorithm?: SignatureAlgorithmName = undefined;
65+
signatureAlgorithm?: SignatureAlgorithmURI = undefined;
6666
/**
6767
* Rules used to convert an XML document into its canonical form.
6868
*/
69-
canonicalizationAlgorithm?: CanonicalizationAlgorithmName = undefined;
69+
canonicalizationAlgorithm?: CanonicalizationAlgorithmURI = undefined;
7070
/**
7171
* It specifies a list of namespace prefixes that should be considered "inclusive" during the canonicalization process.
7272
* Only applicable when using exclusive canonicalization.
@@ -79,7 +79,7 @@ export class SignedXml {
7979
};
8080

8181
maxTransforms: number | null;
82-
implicitTransforms: ReadonlyArray<TransformAlgorithmName> = [];
82+
implicitTransforms: ReadonlyArray<TransformAlgorithmURI> = [];
8383
keyInfoAttributes: { [attrName: string]: string } = {};
8484
getKeyInfoContent = SignedXml.getKeyInfoContent;
8585
getCertFromKeyInfo = SignedXml.getCertFromKeyInfo;
@@ -116,7 +116,7 @@ export class SignedXml {
116116
/**
117117
* To add a new hash algorithm create a new class that implements the {@link HashAlgorithm} interface, and register it here. More info: {@link https://github.com/node-saml/xml-crypto#customizing-algorithms|Customizing Algorithms}
118118
*/
119-
HashAlgorithms: DigestAlgorithmMap;
119+
HashAlgorithms: HashAlgorithmMap;
120120

121121
/**
122122
* To add a new signature algorithm create a new class that implements the {@link SignatureAlgorithm} interface, and register it here. More info: {@link https://github.com/node-saml/xml-crypto#customizing-algorithms|Customizing Algorithms}
@@ -144,11 +144,11 @@ export class SignedXml {
144144
[TRANSFORM_ALGORITHMS.ENVELOPED_SIGNATURE]: envelopedSignatures.EnvelopedSignature,
145145
});
146146

147-
static readonly getDefaultDigestAlgorithms = (): DigestAlgorithmMap => ({
147+
static readonly getDefaultHashAlgorithms = (): HashAlgorithmMap => ({
148148
// TODO: In v7.x we may consider removing sha1 from defaults
149-
[DIGEST_ALGORITHMS.SHA1]: hashAlgorithms.Sha1,
150-
[DIGEST_ALGORITHMS.SHA256]: hashAlgorithms.Sha256,
151-
[DIGEST_ALGORITHMS.SHA512]: hashAlgorithms.Sha512,
149+
[HASH_ALGORITHMS.SHA1]: hashAlgorithms.Sha1,
150+
[HASH_ALGORITHMS.SHA256]: hashAlgorithms.Sha256,
151+
[HASH_ALGORITHMS.SHA512]: hashAlgorithms.Sha512,
152152
});
153153

154154
static readonly getDefaultSignatureAlgorithms = (): SignatureAlgorithmMap => ({
@@ -187,7 +187,7 @@ export class SignedXml {
187187
getCertFromKeyInfo,
188188
objects,
189189
allowedSignatureAlgorithms,
190-
allowedDigestAlgorithms,
190+
allowedHashAlgorithms,
191191
allowedCanonicalizationAlgorithms,
192192
allowedTransformAlgorithms,
193193
} = options;
@@ -215,7 +215,7 @@ export class SignedXml {
215215
this.objects = objects;
216216
this.CanonicalizationAlgorithms =
217217
allowedCanonicalizationAlgorithms ?? SignedXml.getDefaultCanonicalizationAlgorithms();
218-
this.HashAlgorithms = allowedDigestAlgorithms ?? SignedXml.getDefaultDigestAlgorithms();
218+
this.HashAlgorithms = allowedHashAlgorithms ?? SignedXml.getDefaultHashAlgorithms();
219219
this.SignatureAlgorithms =
220220
allowedSignatureAlgorithms ?? SignedXml.getDefaultSignatureAlgorithms();
221221
// TODO: use default transform algorithms if not provided (breaking change)
@@ -503,7 +503,7 @@ export class SignedXml {
503503
}
504504
}
505505

506-
private findSignatureAlgorithm(name?: SignatureAlgorithmName) {
506+
private findSignatureAlgorithm(name?: SignatureAlgorithmURI) {
507507
if (name == null) {
508508
throw new Error("signatureAlgorithm is required");
509509
}
@@ -515,7 +515,7 @@ export class SignedXml {
515515
}
516516
}
517517

518-
private findCanonicalizationAlgorithm(name: CanonicalizationAlgorithmName) {
518+
private findCanonicalizationAlgorithm(name: CanonicalizationAlgorithmURI) {
519519
if (name != null) {
520520
const algo = this.CanonicalizationAlgorithms[name];
521521
if (algo) {
@@ -526,7 +526,7 @@ export class SignedXml {
526526
throw new Error(`canonicalization algorithm '${name}' is not supported`);
527527
}
528528

529-
private findHashAlgorithm(name: HashAlgorithmName) {
529+
private findHashAlgorithm(name: HashAlgorithmURI) {
530530
const algo = this.HashAlgorithms[name];
531531
if (algo) {
532532
return new algo();
@@ -535,7 +535,7 @@ export class SignedXml {
535535
}
536536
}
537537

538-
private findTransformAlgorithm(name: TransformAlgorithmName) {
538+
private findTransformAlgorithm(name: TransformAlgorithmURI) {
539539
// TODO: remove this fallback (breaking change)
540540
if (this.TransformAlgorithms == null) {
541541
return this.findCanonicalizationAlgorithm(name);
@@ -709,7 +709,7 @@ export class SignedXml {
709709
}
710710

711711
if (isDomNode.isAttributeNode(node)) {
712-
this.canonicalizationAlgorithm = node.value as CanonicalizationAlgorithmName;
712+
this.canonicalizationAlgorithm = node.value as CanonicalizationAlgorithmURI;
713713

714714
if (!this.findCanonicalizationAlgorithm(this.canonicalizationAlgorithm)) {
715715
throw new Error(
@@ -724,7 +724,7 @@ export class SignedXml {
724724
);
725725

726726
if (isDomNode.isAttributeNode(signatureAlgorithm)) {
727-
this.signatureAlgorithm = signatureAlgorithm.value as SignatureAlgorithmName;
727+
this.signatureAlgorithm = signatureAlgorithm.value as SignatureAlgorithmURI;
728728
}
729729

730730
const signedInfoNodes = utils.findChildren(this.signatureNode, "SignedInfo");
@@ -862,9 +862,9 @@ export class SignedXml {
862862
*/
863863
if (
864864
transforms.length === 0 ||
865-
transforms[transforms.length - 1] === "http://www.w3.org/2000/09/xmldsig#enveloped-signature"
865+
transforms[transforms.length - 1] === TRANSFORM_ALGORITHMS.ENVELOPED_SIGNATURE
866866
) {
867-
transforms.push("http://www.w3.org/TR/2001/REC-xml-c14n-20010315");
867+
transforms.push(CANONICALIZATION_ALGORITHMS.C14N);
868868
}
869869
const refUri = isDomNode.isElementNode(refNode)
870870
? refNode.getAttribute("URI") || undefined
@@ -1192,7 +1192,7 @@ export class SignedXml {
11921192
}
11931193

11941194
const currentPrefix = prefix ? `${prefix}:` : "";
1195-
const signatureNamespace = "http://www.w3.org/2000/09/xmldsig#";
1195+
const signatureNamespace = XMLDSIG_URIS.NAMESPACES.ds;
11961196

11971197
// Find the SignedInfo element to append to
11981198
const signedInfoNode = xpath.select1(`./*[local-name(.)='SignedInfo']`, signatureElem);
@@ -1395,11 +1395,7 @@ export class SignedXml {
13951395
let attr;
13961396

13971397
if (this.idMode === "wssecurity") {
1398-
attr = utils.findAttr(
1399-
node,
1400-
"Id",
1401-
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd",
1402-
);
1398+
attr = utils.findAttr(node, "Id", XMLDSIG_URIS.NAMESPACES.wsu);
14031399
} else {
14041400
this.idAttributes.some((idAttribute) => {
14051401
if (typeof idAttribute === "string") {

0 commit comments

Comments
 (0)