Skip to content

Commit 741240f

Browse files
authored
Remove default for transformation algorithm (#410)
1 parent b0541b3 commit 741240f

File tree

6 files changed

+64
-17
lines changed

6 files changed

+64
-17
lines changed

README.md

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@ This will enable HMAC and disable digital signature algorithms. Due to key
4949
confusion issues, it is risky to have both HMAC-based and public key digital
5050
signature algorithms enabled at same time.
5151

52-
By default the following algorithms are used:
53-
54-
_Canonicalization/Transformation Algorithm:_ Exclusive Canonicalization <http://www.w3.org/2001/10/xml-exc-c14n#>
55-
56-
_Hashing/Digest Algorithm:_ Must be specified by the user
57-
58-
_Signature Algorithm:_ Must be specified by the user
59-
6052
[You are able to extend xml-crypto with custom algorithms.](#customizing-algorithms)
6153

6254
## Signing Xml documents
@@ -77,7 +69,13 @@ var SignedXml = require("xml-crypto").SignedXml,
7769
var xml = "<library>" + "<book>" + "<name>Harry Potter</name>" + "</book>" + "</library>";
7870

7971
var sig = new SignedXml({ privateKey: fs.readFileSync("client.pem") });
80-
sig.addReference({ xpath: "//*[local-name(.)='book']" });
72+
sig.addReference({
73+
xpath: "//*[local-name(.)='book']",
74+
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
75+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
76+
});
77+
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
78+
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
8179
sig.computeSignature(xml);
8280
fs.writeFileSync("signed.xml", sig.getSignedXml());
8381
```
@@ -243,7 +241,7 @@ The `SignedXml` constructor provides an abstraction for sign and verify xml docu
243241
- `idAttribute` - string - default `Id` or `ID` or `id` - the name of the attribute that contains the id of the element
244242
- `privateKey` - string or Buffer - default `null` - the private key to use for signing
245243
- `publicCert` - string or Buffer - default `null` - the public certificate to use for verifying
246-
- `signatureAlgorithm` - string - default `http://www.w3.org/2000/09/xmldsig#rsa-sha1` - the signature algorithm to use
244+
- `signatureAlgorithm` - string - the signature algorithm to use
247245
- `canonicalizationAlgorithm` - string - default `undefined` - the canonicalization algorithm to use
248246
- `inclusiveNamespacesPrefixList` - string - default `null` - a list of namespace prefixes to include during canonicalization
249247
- `implicitTransforms` - string[] - default `[]` - a list of implicit transforms to use during verification
@@ -257,7 +255,7 @@ A `SignedXml` object provides the following methods:
257255

258256
To sign xml documents:
259257

260-
- `addReference(xpath, [transforms], [digestAlgorithm])` - adds a reference to a xml element where:
258+
- `addReference(xpath, transforms, digestAlgorithm)` - adds a reference to a xml element where:
261259
- `xpath` - a string containing a XPath expression referencing a xml element
262260
- `transforms` - an array of [transform algorithms](#canonicalization-and-transformation-algorithms), the referenced element will be transformed for each value in the array
263261
- `digestAlgorithm` - one of the supported [hashing algorithms](#hashing-algorithms)
@@ -391,7 +389,13 @@ function signXml(xml, xpath, key, dest) {
391389
digestAlgorithm: "http://myDigestAlgorithm",
392390
});
393391

394-
sig.addReference({ xpath });
392+
sig.addReference({
393+
xpath,
394+
transforms: ["http://MyTransformation"],
395+
digestAlgorithm: "http://myDigestAlgorithm",
396+
});
397+
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
398+
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
395399
sig.computeSignature(xml);
396400
fs.writeFileSync(dest, sig.getSignedXml());
397401
}
@@ -424,6 +428,8 @@ function AsyncSignatureAlgorithm() {
424428

425429
var sig = new SignedXml({ signatureAlgorithm: "http://asyncSignatureAlgorithm" });
426430
sig.SignatureAlgorithms["http://asyncSignatureAlgorithm"] = AsyncSignatureAlgorithm;
431+
sig.signatureAlgorithm = "http://asyncSignatureAlgorithm";
432+
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
427433
sig.computeSignature(xml, opts, function (err) {
428434
var signedResponse = sig.getSignedXml();
429435
});
@@ -474,7 +480,13 @@ var SignedXml = require("xml-crypto").SignedXml,
474480
var xml = "<library>" + "<book>" + "<name>Harry Potter</name>" + "</book>" + "</library>";
475481

476482
var sig = new SignedXml({ privateKey: fs.readFileSync("client.pem") });
477-
sig.addReference({ xpath: "//*[local-name(.)='book']" });
483+
sig.addReference({
484+
xpath: "//*[local-name(.)='book']",
485+
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
486+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
487+
});
488+
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
489+
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
478490
sig.computeSignature(xml, {
479491
prefix: "ds",
480492
});
@@ -497,7 +509,13 @@ var SignedXml = require("xml-crypto").SignedXml,
497509
var xml = "<library>" + "<book>" + "<name>Harry Potter</name>" + "</book>" + "</library>";
498510

499511
var sig = new SignedXml({ privateKey: fs.readFileSync("client.pem") });
500-
sig.addReference({ xpath: "//*[local-name(.)='book']" });
512+
sig.addReference({
513+
xpath: "//*[local-name(.)='book']",
514+
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
515+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
516+
});
517+
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
518+
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
501519
sig.computeSignature(xml, {
502520
location: { reference: "//*[local-name(.)='book']", action: "after" }, //This will place the signature after the book element
503521
});

src/signed-xml.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ export class SignedXml {
639639
* Adds a reference to the signature.
640640
*
641641
* @param xpath The XPath expression to select the XML nodes to be referenced.
642-
* @param transforms An array of transform algorithms to be applied to the selected nodes. Defaults to ["http://www.w3.org/2001/10/xml-exc-c14n#"].
642+
* @param transforms An array of transform algorithms to be applied to the selected nodes.
643643
* @param digestAlgorithm The digest algorithm to use for computing the digest value.
644644
* @param uri The URI identifier for the reference. If empty, an empty URI will be used.
645645
* @param digestValue The expected digest value for the reference.
@@ -648,7 +648,7 @@ export class SignedXml {
648648
*/
649649
addReference({
650650
xpath,
651-
transforms = ["http://www.w3.org/2001/10/xml-exc-c14n#"],
651+
transforms,
652652
digestAlgorithm,
653653
uri = "",
654654
digestValue,
@@ -659,6 +659,10 @@ export class SignedXml {
659659
throw new Error("digestAlgorithm is required");
660660
}
661661

662+
if (!utils.isArrayHasLength(transforms)) {
663+
throw new Error("transforms must contain at least one transform algorithm");
664+
}
665+
662666
this.references.push({
663667
xpath,
664668
transforms,

test/hmac-tests.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ describe("HMAC tests", function () {
5050
sig.addReference({
5151
xpath: "//*[local-name(.)='book']",
5252
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
53+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
5354
});
5455
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
5556
sig.computeSignature(xml);

test/key-info-tests.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ describe("KeyInfo tests", function () {
3232
sig.addReference({
3333
xpath: "//*[local-name(.)='book']",
3434
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
35+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
3536
});
3637
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
3738
sig.computeSignature(xml);

test/signature-integration-tests.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ describe("Signature integration tests", function () {
1111
sig.privateKey = fs.readFileSync("./test/static/client.pem");
1212

1313
xpath.map(function (n) {
14-
sig.addReference({ xpath: n, digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1" });
14+
sig.addReference({
15+
xpath: n,
16+
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
17+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
18+
});
1519
});
1620

1721
sig.canonicalizationAlgorithm = canonicalizationAlgorithm;
@@ -175,6 +179,7 @@ describe("Signature integration tests", function () {
175179
sig.addReference({
176180
xpath: "//*[local-name(.)='book']",
177181
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
182+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
178183
});
179184
sig.privateKey = fs.readFileSync("./test/static/client.pem");
180185
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";

test/signature-unit-tests.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ describe("Signature unit tests", function () {
2525
sig.addReference({
2626
xpath: "//*[local-name(.)='x']",
2727
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
28+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
2829
});
2930
sig.addReference({
3031
xpath: "//*[local-name(.)='y']",
3132
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
33+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
3234
});
3335
sig.addReference({
3436
xpath: "//*[local-name(.)='w']",
3537
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
38+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
3639
});
3740

3841
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
@@ -70,6 +73,7 @@ describe("Signature unit tests", function () {
7073
sig.addReference({
7174
xpath: "//*[@wsu:Id]",
7275
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
76+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
7377
});
7478

7579
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
@@ -95,6 +99,7 @@ describe("Signature unit tests", function () {
9599
sig.addReference({
96100
xpath: "//*[local-name(.)='x']",
97101
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
102+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
98103
});
99104
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
100105
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
@@ -129,6 +134,7 @@ describe("Signature unit tests", function () {
129134
sig.addReference({
130135
xpath: "//*[local-name(.)='name']",
131136
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
137+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
132138
});
133139

134140
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
@@ -165,6 +171,7 @@ describe("Signature unit tests", function () {
165171
sig.addReference({
166172
xpath: "//*[local-name(.)='name']",
167173
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
174+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
168175
});
169176
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
170177
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
@@ -188,6 +195,7 @@ describe("Signature unit tests", function () {
188195
sig.addReference({
189196
xpath: "//*[local-name(.)='repository']",
190197
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
198+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
191199
});
192200

193201
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
@@ -219,6 +227,7 @@ describe("Signature unit tests", function () {
219227
sig.addReference({
220228
xpath: "//*[local-name(.)='repository']",
221229
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
230+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
222231
});
223232

224233
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
@@ -249,6 +258,7 @@ describe("Signature unit tests", function () {
249258
sig.addReference({
250259
xpath: "//*[local-name(.)='repository']",
251260
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
261+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
252262
});
253263

254264
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
@@ -280,6 +290,7 @@ describe("Signature unit tests", function () {
280290
sig.addReference({
281291
xpath: "//*[local-name(.)='repository']",
282292
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
293+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
283294
});
284295

285296
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
@@ -635,14 +646,17 @@ describe("Signature unit tests", function () {
635646
sig.addReference({
636647
xpath: "//*[local-name(.)='x']",
637648
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
649+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
638650
});
639651
sig.addReference({
640652
xpath: "//*[local-name(.)='y']",
641653
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
654+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
642655
});
643656
sig.addReference({
644657
xpath: "//*[local-name(.)='w']",
645658
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
659+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
646660
});
647661

648662
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
@@ -713,14 +727,17 @@ describe("Signature unit tests", function () {
713727
sig.addReference({
714728
xpath: "//*[local-name(.)='x']",
715729
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
730+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
716731
});
717732
sig.addReference({
718733
xpath: "//*[local-name(.)='y']",
719734
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
735+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
720736
});
721737
sig.addReference({
722738
xpath: "//*[local-name(.)='w']",
723739
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
740+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
724741
});
725742

726743
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
@@ -983,6 +1000,7 @@ describe("Signature unit tests", function () {
9831000
sig.addReference({
9841001
xpath: "//*[local-name(.)='repository']",
9851002
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
1003+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
9861004
});
9871005

9881006
try {

0 commit comments

Comments
 (0)