Skip to content

Commit 7f00bd8

Browse files
committed
Merge pull request #58 from bjrmatos/master
Documentation update
2 parents 5dea5b9 + 721e494 commit 7f00bd8

File tree

2 files changed

+109
-15
lines changed

2 files changed

+109
-15
lines changed

README.md

+107-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,45 @@ Install with [npm](http://github.com/isaacs/npm):
1010

1111
A pre requisite it to have [openssl](http://www.openssl.org/) installed and its /bin to be on the system path. I used version 1.0.1c but it should work on older versions too.
1212

13+
## Supported Algorithms
14+
15+
### Canonicalization and Transformation Algorithms
16+
17+
* Exclusive Canonicalization http://www.w3.org/2001/10/xml-exc-c14n#
18+
* Exclusive Canonicalization with comments http://www.w3.org/2001/10/xml-exc-c14n#WithComments
19+
* Enveloped Signature transform http://www.w3.org/2000/09/xmldsig#enveloped-signature
20+
21+
### Hashing Algorithms
22+
23+
* SHA1 digests http://www.w3.org/2000/09/xmldsig#sha1
24+
* SHA256 digests http://www.w3.org/2001/04/xmlenc#sha256
25+
26+
### Signature Algorithms
27+
28+
* RSA-SHA1 http://www.w3.org/2000/09/xmldsig#rsa-sha1
29+
* RSA-SHA256 http://www.w3.org/2001/04/xmldsig-more#rsa-sha256
30+
* RSA-SHA512 http://www.w3.org/2001/04/xmldsig-more#rsa-sha512
31+
32+
by default the following algorithms are used:
33+
34+
*Canonicalization/Transformation Algorithm:* Exclusive Canonicalization http://www.w3.org/2001/10/xml-exc-c14n#
35+
36+
*Hashing Algorithm:* SHA1 digest http://www.w3.org/2000/09/xmldsig#sha1
37+
38+
*Signature Algorithm:* RSA-SHA1 http://www.w3.org/2000/09/xmldsig#rsa-sha1
39+
40+
[You are able to extend xml-crypto with custom algorithms.](#customizing-algorithms)
41+
42+
1343
## Signing Xml documents
44+
45+
When signing a xml document you can specify the following properties on a `SignedXml` instance to customize the signature process:
46+
47+
- `sign.signingKey` - **[required]** a `Buffer` or pem encoded `String` containing your private key
48+
- `sign.keyInfoProvider` - **[optional]** a key info provider instance, see [customizing algorithms](#customizing-algorithms) for an implementation example
49+
- `sign.signatureAlgorithm` - **[optional]** one of the supported [signature algorithms](#signature-algorithms). Ex: `sign.signatureAlgorithm = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"`
50+
- `sign.canonicalizationAlgorithm` - **[optional]** one of the supported [canonicalization algorithms](#canonicalization-and-transformation-algorithms). Ex: `sign.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#WithComments"`
51+
1452
Use this code:
1553

1654
`````javascript
@@ -56,18 +94,21 @@ The result will be:
5694
</library>
5795
`````
5896

97+
Note:
5998

60-
Notes:
61-
62-
sig.getSignedXml() returns the original xml document with the signature pushed as the last child of the root node (as above). This assumes you are not signing the root node but only sub node(s) otherwise this is not valid. If you do sign the root node call sig.getSignatureXml() to get just the signature part and sig.getOriginalXmlWithIds() to get the original xml with Id attributes added on relevant elements (required for validation).
99+
To generate a `<X509Data></X509Data>` element in the signature you must provide a key info implementation, see [customizing algorithms](#customizing-algorithms) for an example.
63100

64101
## Verifying Xml documents
65102

103+
When verifying a xml document you must specify the following properties on a ``SignedXml` instance:
104+
105+
- `sign.keyInfoProvider` - **[required]** a key info provider instance containing your certificate, see [customizing algorithms](#customizing-algorithms) for an implementation example
106+
66107
You can use any dom parser you want in your code (or none, depending on your usage). This sample uses [xmldom](https://github.com/jindw/xmldom) so you should install it first:
67108

68-
npm install xmldom
109+
npm install xmldom
69110

70-
Then run:
111+
Example:
71112

72113
`````javascript
73114
var select = require('xml-crypto').xpath
@@ -87,18 +128,61 @@ Then run:
87128
if (!res) console.log(sig.validationErrors)
88129
`````
89130

131+
if the verification process fails `sig.validationErrors` will have the errors.
132+
90133
Note:
91134

92135
The xml-crypto api requires you to supply it separately the xml signature ("&lt;Signature&gt;...&lt;/Signature&gt;", in loadSignature) and the signed xml (in checkSignature). The signed xml may or may not contain the signature in it, but you are still required to supply the signature separately.
93136

94-
## Supported Algorithms
95-
The first release always uses the following algorithems:
96137

97-
* Exclusive Canonicalization http://www.w3.org/2001/10/xml-exc-c14n#
98-
* SHA1 digests http://www.w3.org/2000/09/xmldsig#sha1
99-
* RSA-SHA1 signature algorithm http://www.w3.org/2000/09/xmldsig#rsa-sha1
138+
## API
139+
140+
### xpath
141+
142+
See [xpath.js](https://github.com/yaronn/xpath.js) for usage
143+
144+
### SignedXml
145+
146+
The `SignedXml` constructor provides an abstraction for sign and verify xml documents. The object is constructed using `new SignedXml([idMode])` where:
147+
148+
- `idMode` - if the value of `"wssecurity"` is passed it will create/validate id's with the ws-security namespace.
149+
150+
*API*
151+
152+
A `SignedXml` object provides the following methods:
153+
154+
To sign xml documents:
155+
156+
- `addReference(xpath, [transforms], [digestAlgorithm])` - adds a reference to a xml element where:
157+
- `xpath` - a string containing a XPath expression referencing a xml element
158+
- `transforms` - an array of [transform algorithms](#canonicalization-and-transformation-algorithms), the referenced element will be transformed for each value in the array
159+
- `digestAlgorithm` - one of the supported [hashing algorithms](#hashing algorithms)
160+
- `computeSignature(xml, [options])` - compute the signature of the given xml where:
161+
- `xml` - a string containing a xml document
162+
- `options` - an object with the following properties:
163+
- `prefix` - adds this value as a prefix for the generated signature tags
164+
- `attrs` - a hash of attributes and values `attrName: value` to add to the signature root node
165+
- `location` - customize the location of the signature, pass an object with a `reference` key which should contain a XPath expression to a reference node, an `action` key which should contain one of the following values: `append`, `prepend`, `before`, `after`
166+
- `getSignedXml()` - returns the original xml document with the signature in it, **must be called only after `computeSignature`**
167+
- `getSignatureXml()` - returns just the signature part, **must be called only after `computeSignature`**
168+
- `getOriginalXmlWithIds()` - returns the original xml with Id attributes added on relevant elements (required for validation), **must be called only after `computeSignature`**
169+
170+
To verify xml documents:
171+
172+
- `loadSignature(signatureXml)` - loads the signature where:
173+
- `signatureXml` - a string or node object (like an [xml-dom](https://github.com/jindw/xmldom) node) containing the xml representation of the signature
174+
- `checkSignature(xml)` - validates the given xml document and returns true if the validation was successful, `sig.validationErrors` will have the validation errors if any, where:
175+
- `xml` - a string containing a xml document
176+
177+
178+
### FileKeyInfo
179+
180+
A basic key info provider implementation using `fs.readFileSync(file)`, is constructed using `new FileKeyInfo([file])` where:
181+
182+
- `file` - a path to a pem encoded certificate
183+
184+
See [verifying xml documents](#verifying-xml-documents) for an example usage
100185

101-
you are able to extend xml-crypto with further algorithms.
102186

103187
## Customizing Algorithms
104188
The following sample shows how to sign a message using custom algorithms.
@@ -118,8 +202,10 @@ Implement it if you want to create a signature with a KeyInfo section, or you wa
118202
`````javascript
119203
/**/
120204
function MyKeyInfo() {
121-
this.getKeyInfo = function(key) {
122-
return "<X509Data></X509Data>"
205+
this.getKeyInfo = function(key, prefix) {
206+
prefix = prefix || ''
207+
prefix = prefix ? prefix + ':' : prefix
208+
return "<" + prefix + "X509Data></" + prefix + "X509Data>"
123209
}
124210
this.getKey = function(keyInfo) {
125211
//you can use the keyInfo parameter to extract the key in any way you want
@@ -259,6 +345,14 @@ If you have .pfx certificates you can convert them to .pem using [openssl](http:
259345

260346
Then you could use the result as is for the purpose of signing. For the purpose of validation open the resulting .pem with a text editor and copy from -----BEGIN CERTIFICATE----- to -----END CERTIFICATE----- (including) to a new text file and save it as .pem.
261347

348+
## Examples
349+
350+
- [how to sign a root node](#) *coming soon*
351+
- [how to add a prefix for the signature] *coming soon*
352+
- [how to specify the location of the signature](#) *coming soon*
353+
354+
*more examples coming soon*
355+
262356
## Development
263357
The test framework is [nodeunit](https://github.com/caolan/nodeunit). To run tests use:
264358

lib/signed-xml.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ SignedXml.prototype.addReference = function(xpath, transforms, digestAlgorithm,
400400
* - `prefix` {String} Adds a prefix for the generated signature tags
401401
* - `attrs` {Object} A hash of attributes and values `attrName: value` to add to the signature root node
402402
* - `location` {{ reference: String, action: String }}
403-
* A object with a `reference` key which should
404-
* contain an XPath expression, a `action` key which
403+
* An object with a `reference` key which should
404+
* contain a XPath expression, an `action` key which
405405
* should contain one of the following values:
406406
* `append`, `prepend`, `before`, `after`
407407
*

0 commit comments

Comments
 (0)