1- var utils = require ( '../utils' ) ;
2- var SignedXml = require ( 'xml-crypto' ) . SignedXml ;
1+ const utils = require ( '../utils' ) ;
2+ const SignedXml = require ( 'xml-crypto' ) . SignedXml ;
33
4- var algorithms = {
4+ const algorithms = {
55 signature : {
66 'rsa-sha256' : 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256' ,
7- 'rsa-sha1' : 'http://www.w3.org/2000/09/xmldsig#rsa-sha1'
7+ 'rsa-sha1' : 'http://www.w3.org/2000/09/xmldsig#rsa-sha1'
88 } ,
99 digest : {
1010 'sha256' : 'http://www.w3.org/2001/04/xmlenc#sha256' ,
@@ -22,61 +22,80 @@ exports.fromSignXmlOptions = function (options) {
2222 if ( ! options . xpathToNodeBeforeSignature )
2323 throw new Error ( 'xpathToNodeBeforeSignature is required' )
2424
25- var key = options . key ;
26- var pem = options . cert ;
27- var signatureAlgorithm = options . signatureAlgorithm || 'rsa-sha256' ;
28- var digestAlgorithm = options . digestAlgorithm || 'sha256' ;
29- var signatureNamespacePrefix = ( function ( prefix ) {
25+ const key = options . key ;
26+ const pem = options . cert ;
27+ const signatureAlgorithm = options . signatureAlgorithm || 'rsa-sha256' ;
28+ const digestAlgorithm = options . digestAlgorithm || 'sha256' ;
29+ const signatureNamespacePrefix = ( function ( prefix ) {
3030 // 0.10.1 added prefix, but we want to name it signatureNamespacePrefix - This is just to keep supporting prefix
3131 return typeof prefix === 'string' ? prefix : '' ;
3232 } ) ( options . signatureNamespacePrefix || options . prefix ) ;
33- var xpathToNodeBeforeSignature = options . xpathToNodeBeforeSignature ;
34- var idAttribute = options . signatureIdAttribute ;
33+ const xpathToNodeBeforeSignature = options . xpathToNodeBeforeSignature ;
34+ const idAttribute = options . signatureIdAttribute ;
3535
3636 /**
3737 * @param {Document } doc
3838 * @param {Function } [callback]
3939 * @return {string }
4040 */
4141 return function signXmlDocument ( doc , callback ) {
42- var unsigned = exports . unsigned ( doc ) ;
43- var cert = utils . pemToCert ( pem ) ;
42+ function sign ( key ) {
43+ const unsigned = exports . unsigned ( doc ) ;
44+ const cert = utils . pemToCert ( pem ) ;
4445
45- var sig = new SignedXml ( null , { signatureAlgorithm : algorithms . signature [ signatureAlgorithm ] , idAttribute : idAttribute } ) ;
46- sig . addReference ( "//*[local-name(.)='Assertion']" ,
47- [ "http://www.w3.org/2000/09/xmldsig#enveloped-signature" , "http://www.w3.org/2001/10/xml-exc-c14n#" ] ,
48- algorithms . digest [ digestAlgorithm ] ) ;
46+ const sig = new SignedXml ( null , {
47+ signatureAlgorithm : algorithms . signature [ signatureAlgorithm ] ,
48+ idAttribute : idAttribute
49+ } ) ;
50+ sig . addReference ( "//*[local-name(.)='Assertion']" ,
51+ [ "http://www.w3.org/2000/09/xmldsig#enveloped-signature" , "http://www.w3.org/2001/10/xml-exc-c14n#" ] ,
52+ algorithms . digest [ digestAlgorithm ] ) ;
4953
50- sig . signingKey = key ;
54+ sig . signingKey = key ;
5155
52- sig . keyInfoProvider = {
53- getKeyInfo : function ( key , prefix ) {
54- prefix = prefix ? prefix + ':' : prefix ;
55- return "<" + prefix + "X509Data><" + prefix + "X509Certificate>" + cert + "</" + prefix + "X509Certificate></" + prefix + "X509Data>" ;
56- }
57- } ;
56+ sig . keyInfoProvider = {
57+ getKeyInfo : function ( key , prefix ) {
58+ prefix = prefix ? prefix + ':' : prefix ;
59+ return "<" + prefix + "X509Data><" + prefix + "X509Certificate>" + cert + "</" + prefix + "X509Certificate></" + prefix + "X509Data>" ;
60+ }
61+ } ;
62+
63+ sig . computeSignature ( unsigned , {
64+ location : { reference : xpathToNodeBeforeSignature , action : 'after' } ,
65+ prefix : signatureNamespacePrefix
66+ } ) ;
67+
68+ return sig . getSignedXml ( ) ;
69+ }
5870
59- sig . computeSignature ( unsigned , {
60- location : { reference : xpathToNodeBeforeSignature , action : 'after' } ,
61- prefix : signatureNamespacePrefix
62- } ) ;
71+ let signed
72+ try {
73+ try {
74+ signed = sign ( key )
75+ } catch ( err ) {
76+ signed = sign ( utils . fixPemFormatting ( key ) )
77+ }
6378
64- var signed = sig . getSignedXml ( ) ;
65- if ( callback ) {
66- setImmediate ( callback , null , signed ) ;
67- } else {
68- return signed ;
79+ if ( callback ) {
80+ setImmediate ( callback , null , signed ) ;
81+ } else {
82+ return signed ;
83+ }
84+ } catch ( e ) {
85+ if ( callback ) {
86+ setImmediate ( callback , e )
87+ }
88+ throw e
6989 }
7090 } ;
7191} ;
72-
7392/**
7493 * @param {Document } doc
7594 * @param {Function } [callback]
7695 * @return {string }
7796 */
7897exports . unsigned = function ( doc , callback ) {
79- var xml = utils . removeWhitespace ( doc . toString ( ) ) ;
98+ const xml = utils . removeWhitespace ( doc . toString ( ) ) ;
8099 if ( callback ) {
81100 setImmediate ( callback , null , xml )
82101 } else {
0 commit comments