Skip to content

Commit 0c4813d

Browse files
authored
Add support for inserting and signing Object elements inside the Signature (#506)
1 parent 34cb055 commit 0c4813d

File tree

7 files changed

+875
-66
lines changed

7 files changed

+875
-66
lines changed

.vscode/settings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
"codecov",
77
"feide",
88
"HMAC",
9+
"posteb",
10+
"preeb",
911
"reserialization",
12+
"stricttextualmsg",
1013
"wsfederation",
11-
"wssecurity"
14+
"wssecurity",
15+
"xades"
1216
]
1317
}

README.md

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,20 @@ The `SignedXml` constructor provides an abstraction for sign and verify xml docu
258258
- `keyInfoAttributes` - object - default `{}` - a hash of attributes and values `attrName: value` to add to the KeyInfo node
259259
- `getKeyInfoContent` - function - default `noop` - a function that returns the content of the KeyInfo node
260260
- `getCertFromKeyInfo` - function - default `SignedXml.getCertFromKeyInfo` - a function that returns the certificate from the `<KeyInfo />` node
261+
- `objects` - array - default `undefined` - an array of objects defining the content of the `<Object/>` nodes
261262

262263
#### API
263264

264265
A `SignedXml` object provides the following methods:
265266

266267
To sign xml documents:
267268

268-
- `addReference(xpath, transforms, digestAlgorithm)` - adds a reference to a xml element where:
269+
- `addReference({ xpath, transforms, digestAlgorithm, id, type })` - adds a reference to a xml element where:
269270
- `xpath` - a string containing a XPath expression referencing a xml element
270271
- `transforms` - an array of [transform algorithms](#canonicalization-and-transformation-algorithms), the referenced element will be transformed for each value in the array
271272
- `digestAlgorithm` - one of the supported [hashing algorithms](#hashing-algorithms)
273+
- `id` - an optional `Id` attribute to add to the reference element
274+
- `type` - the optional `Type` attribute to add to the reference element (represented as a URI)
272275
- `computeSignature(xml, [options])` - compute the signature of the given xml where:
273276
- `xml` - a string containing a xml document
274277
- `options` - an object with the following properties:
@@ -513,12 +516,12 @@ Set `action` to one of the following:
513516
- after - append to specific node (use the `referenceNode` property)
514517

515518
```javascript
516-
var SignedXml = require("xml-crypto").SignedXml,
517-
fs = require("fs");
519+
const SignedXml = require("xml-crypto").SignedXml;
520+
const fs = require("fs");
518521

519-
var xml = "<library>" + "<book>" + "<name>Harry Potter</name>" + "</book>" + "</library>";
522+
const xml = "<library>" + "<book>" + "<name>Harry Potter</name>" + "</book>" + "</library>";
520523

521-
var sig = new SignedXml({ privateKey: fs.readFileSync("client.pem") });
524+
const sig = new SignedXml({ privateKey: fs.readFileSync("client.pem") });
522525
sig.addReference({
523526
xpath: "//*[local-name(.)='book']",
524527
digestAlgorithm: "http://www.w3.org/2000/09/xmldsig#sha1",
@@ -527,10 +530,46 @@ sig.addReference({
527530
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
528531
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
529532
sig.computeSignature(xml, {
530-
location: { reference: "//*[local-name(.)='book']", action: "after" }, //This will place the signature after the book element
533+
location: { reference: "//*[local-name(.)='book']", action: "after" }, // This will place the signature after the book element
531534
});
532535
```
533536

537+
### How to add custom Objects to the signature
538+
539+
Use the `objects` option when creating a SignedXml instance to add custom Objects to the signature.
540+
541+
```javascript
542+
const SignedXml = require("xml-crypto").SignedXml;
543+
const fs = require("fs");
544+
545+
const xml = "<library>" + "<book>" + "<name>Harry Potter</name>" + "</book>" + "</library>";
546+
547+
const sig = new SignedXml({
548+
privateKey: fs.readFileSync("client.pem"),
549+
canonicalizationAlgorithm: "http://www.w3.org/2001/10/xml-exc-c14n#",
550+
signatureAlgorithm: "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256",
551+
objects: [
552+
{
553+
content: "<TestObject>Test data in Object</TestObject>",
554+
attributes: {
555+
Id: "Object1",
556+
MimeType: "text/xml",
557+
},
558+
},
559+
],
560+
});
561+
562+
// Add a reference to the Object element
563+
sig.addReference({
564+
xpath: "//*[@Id='Object1']",
565+
digestAlgorithm: "http://www.w3.org/2001/04/xmlenc#sha256",
566+
transforms: ["http://www.w3.org/2001/10/xml-exc-c14n#"],
567+
});
568+
569+
sig.computeSignature(xml);
570+
fs.writeFileSync("signed.xml", sig.getSignedXml());
571+
```
572+
534573
### more examples (_coming soon_)
535574

536575
## Development

0 commit comments

Comments
 (0)