Xml digital signature and encryption library for Node.js
Install with npm:
npm install xml-crypto
A pre requisite it to have openssl installed and its /bin to be on the system path. I used version 1.0.1c but it should work on older versions too.
Use this code:
var SignedXml = require'xml-crypto'SignedXmlfs = require'fs'var xml = "<library>" +"<book>" +"<name>Harry Potter</name>" +"</book>" +"</library>"var sig =sigaddReference"//*[local-name(.)='book']"sigsigningKey = fsreadFileSync"client.pem"sigcomputeSignaturexmlfswriteFileSync"signed.xml" siggetSignedXml
The result will be:
Harry PottercdiS43aFDQMnb3X8yaIUej3+z9Q=vhWzpQyIYuncHUZV9W...[long base64 removed]...
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).
You can use any dom parser you want in your code (or none, depending on your usage). This sample uses xmldom so you should install it first:
npm install xmldom
var select = require'xml-crypto'xpathdom = require'xmldom'DOMParserSignedXml = require'xml-crypto'SignedXmlFileKeyInfo = require'xml-crypto'FileKeyInfofs = require'fs'var xml = fsreadFileSync"signed.xml"toStringvar doc = parseFromStringxmlvar signature = selectdoc "/*/*[local-name(.)='Signature' and namespace-uri(.)='#']"0var sig =sigkeyInfoProvider = "client_public.pem"sigloadSignaturesignaturetoStringvar res = sigcheckSignaturexmlif !res console.logsigvalidationErrors
The xml-crypto api requires you to supply it separately the xml signature ("<Signature>...</Signature>", 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.
The first release always uses the following algorithems:
- Exclusive Canonicalization http://www.w3.org/2001/10/xml-exc-c14n#
- SHA1 digests http://www.w3.org/2000/09/xmldsig#sha1
- RSA-SHA1 signature algorithm http://www.w3.org/2000/09/xmldsig#rsa-sha1
you are able to extend xml-crypto with further algorithms.
The following sample shows how to sign a message using custom algorithms.
First import some modules:
var SignedXml = require'xml-crypto'SignedXmlfs = require'fs'
Now define the extension point you want to implement. You can choose one ore more.
A key info provider is used to extract and construct the key and the KeyInfo xml section. Implement it if you want to create a signature with a KeyInfo section, or you want to read your key in a different way then the default file read option.
/**/return "<X509Data></X509Data>"//you can use the keyInfo parameter to extract the key in any way you wantreturn fsreadFileSync"key.pem"
A custom hash algorithm is used to calculate digests. Implement it if you want a hash other than the default SHA1.
return "the base64 hash representation of the given xml string"return ""
A custom signing algorithm. The default is RSA-SHA1
/*sign the given SignedInfo using the key. return base64 signature value*/return "signature of signedInfo as base64..."return ""
Custom transformation algorithm. The default is exclusive canonicalization.
/*given a node (from the xmldom module) return its canonical representation (as string)*///you should apply your transformation before returningreturn nodetoStringreturn ""
Custom canonicalization is actually the same as custom transformation. It is applied on the SignedInfo rather than on references.
/*given a node (from the xmldom module) return its canonical representation (as string)*///you should apply your transformation before returningreturn "< x/>"return ""
Now you need to register the new algorithms:
/*register all the custom algorithms*/SignedXmlCanonicalizationAlgorithms"" = MyTransformationSignedXmlCanonicalizationAlgorithms"" = MyCanonicalizationSignedXmlHashAlgorithms"" = MyDigestSignedXmlSignatureAlgorithms"" = MySignatureAlgorithm
Now do the signing. Note how we configure the signature to use the above algorithms:
var sig =/*configure the signature object to use the custom algorithms*/sigsignatureAlgorithm = ""sigkeyInfoProvider =sigcanonicalizationAlgorithm = ""sigaddReference"//*[local-name(.)='x']" "" ""sigsigningKey = fsreadFileSynckeysigaddReferencexpathsigcomputeSignaturexmlfswriteFileSyncdest siggetSignedXmlvar xml = "<library>" +"<book>" +"<name>Harry Potter</name>" +"</book>""</library>"signXmlxml"//*[local-name(.)='book']""client.pem""result.xml"
You can always look at the actual code as a sample (or drop me a mail).
Xml-Crypto internally relies on node's crypto module. This means pem encoded certificates are supported. So to sign an xml use key.pem that looks like this (only the begining of the key content is shown):
-----BEGIN PRIVATE KEY----- MIICdwIBADANBgkqhkiG9w0... -----END PRIVATE KEY-----
And for verification use key_public.pem:
-----BEGIN CERTIFICATE----- MIIBxDCCAW6gAwIBAgIQxUSX... -----END CERTIFICATE-----
Converting .pfx certificates to pem
If you have .pfx certificates you can convert them to .pem using openssl:
openssl pkcs12 -in c:\certs\yourcert.pfx -out c:\certs\cag.pem
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.
The test framework is nodeunit. To run tests use:
$> npm test