Skip to content

Commit 683834c

Browse files
shunkicacjbarth
andcommitted
Add support for inserting and signing Object elements inside the Signature
Co-authored-by: Chris Barth <[email protected]>
1 parent ff1f58a commit 683834c

File tree

7 files changed

+870
-61
lines changed

7 files changed

+870
-61
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: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,20 @@ The `SignedXml` constructor provides an abstraction for sign and verify xml docu
250250
- `keyInfoAttributes` - object - default `{}` - a hash of attributes and values `attrName: value` to add to the KeyInfo node
251251
- `getKeyInfoContent` - function - default `noop` - a function that returns the content of the KeyInfo node
252252
- `getCertFromKeyInfo` - function - default `SignedXml.getCertFromKeyInfo` - a function that returns the certificate from the `<KeyInfo />` node
253+
- `objects` - array - default `undefined` - an array of objects defining the content of the `<Object/>` nodes
253254

254255
#### API
255256

256257
A `SignedXml` object provides the following methods:
257258

258259
To sign xml documents:
259260

260-
- `addReference(xpath, transforms, digestAlgorithm)` - adds a reference to a xml element where:
261+
- `addReference({ xpath, transforms, digestAlgorithm, id, type })` - adds a reference to a xml element where:
261262
- `xpath` - a string containing a XPath expression referencing a xml element
262263
- `transforms` - an array of [transform algorithms](#canonicalization-and-transformation-algorithms), the referenced element will be transformed for each value in the array
263264
- `digestAlgorithm` - one of the supported [hashing algorithms](#hashing-algorithms)
265+
- `id` - an optional `Id` attribute to add to the reference element
266+
- `type` - the optional `Type` attribute to add to the reference element (represented as a URI)
264267
- `computeSignature(xml, [options])` - compute the signature of the given xml where:
265268
- `xml` - a string containing a xml document
266269
- `options` - an object with the following properties:
@@ -523,6 +526,42 @@ sig.computeSignature(xml, {
523526
});
524527
```
525528

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

528567
## Development

0 commit comments

Comments
 (0)