@@ -4,11 +4,16 @@ var xpath = require('xpath');
44var utils = require ( './utils' ) ;
55var pki = require ( 'node-forge' ) . pki ;
66
7+ const insecureAlgorithms = [
8+ //https://www.w3.org/TR/xmlenc-core1/#rsav15note
9+ 'http://www.w3.org/2001/04/xmlenc#rsa-1_5' ,
10+ //https://csrc.nist.gov/News/2017/Update-to-Current-Use-and-Deprecation-of-TDEA
11+ 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc' ] ;
712function encryptKeyInfoWithScheme ( symmetricKey , options , scheme , callback ) {
813 try {
914 var rsa_pub = pki . publicKeyFromPem ( options . rsa_pub ) ;
1015 var encrypted = rsa_pub . encrypt ( symmetricKey . toString ( 'binary' ) , scheme ) ;
11- var base64EncodedEncryptedKey = new Buffer ( encrypted , 'binary' ) . toString ( 'base64' ) ;
16+ var base64EncodedEncryptedKey = Buffer . from ( encrypted , 'binary' ) . toString ( 'base64' ) ;
1217
1318 var params = {
1419 encryptedKey : base64EncodedEncryptedKey ,
@@ -33,7 +38,10 @@ function encryptKeyInfo(symmetricKey, options, callback) {
3338
3439 if ( ! options . keyEncryptionAlgorighm )
3540 return callback ( new Error ( 'encryption without encrypted key is not supported yet' ) ) ;
36-
41+ if ( options . disallowEncryptionWithInsecureAlgorithm
42+ && insecureAlgorithms . indexOf ( options . keyEncryptionAlgorighm ) >= 0 ) {
43+ return callback ( new Error ( 'encryption algorithm ' + options . keyEncryptionAlgorighm + 'is not secure' ) ) ;
44+ }
3745 switch ( options . keyEncryptionAlgorighm ) {
3846 case 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p' :
3947 return encryptKeyInfoWithScheme ( symmetricKey , options , 'RSA-OAEP' , callback ) ;
@@ -55,7 +63,10 @@ function encrypt(content, options, callback) {
5563 return callback ( new Error ( 'rsa_pub option is mandatory and you should provide a valid RSA public key' ) ) ;
5664 if ( ! options . pem )
5765 return callback ( new Error ( 'pem option is mandatory and you should provide a valid x509 certificate encoded as PEM' ) ) ;
58-
66+ if ( options . disallowEncryptionWithInsecureAlgorithm
67+ && insecureAlgorithms . indexOf ( options . keyEncryptionAlgorighm ) >= 0 ) {
68+ return callback ( new Error ( 'encryption algorithm ' + options . keyEncryptionAlgorighm + 'is not secure' ) ) ;
69+ }
5970 options . input_encoding = options . input_encoding || 'utf8' ;
6071
6172 function generate_symmetric_key ( cb ) {
@@ -144,17 +155,20 @@ function decrypt(xml, options, callback) {
144155 return callback ( new Error ( 'must provide XML to encrypt' ) ) ;
145156 if ( ! options . key )
146157 return callback ( new Error ( 'key option is mandatory and you should provide a valid RSA private key' ) ) ;
147-
148158 try {
149159 var doc = typeof xml === 'string' ? new xmldom . DOMParser ( ) . parseFromString ( xml ) : xml ;
150160
151161 var symmetricKey = decryptKeyInfo ( doc , options ) ;
152162 var encryptionMethod = xpath . select ( "//*[local-name(.)='EncryptedData']/*[local-name(.)='EncryptionMethod']" , doc ) [ 0 ] ;
153163 var encryptionAlgorithm = encryptionMethod . getAttribute ( 'Algorithm' ) ;
154164
165+ if ( options . disallowDecryptionWithInsecureAlgorithm
166+ && insecureAlgorithms . indexOf ( encryptionAlgorithm ) >= 0 ) {
167+ throw new Error ( 'encryption algorithm ' + encryptionAlgorithm + ' is not secure, fail to decrypt' ) ;
168+ }
155169 var encryptedContent = xpath . select ( "//*[local-name(.)='EncryptedData']/*[local-name(.)='CipherData']/*[local-name(.)='CipherValue']" , doc ) [ 0 ] ;
156170
157- var encrypted = new Buffer ( encryptedContent . textContent , 'base64' ) ;
171+ var encrypted = Buffer . from ( encryptedContent . textContent , 'base64' ) ;
158172
159173 switch ( encryptionAlgorithm ) {
160174 case 'http://www.w3.org/2001/04/xmlenc#aes128-cbc' :
@@ -188,12 +202,16 @@ function decryptKeyInfo(doc, options) {
188202 throw new Error ( 'cant find encryption algorithm' ) ;
189203 }
190204
191- var keyEncryptionAlgorighm = keyEncryptionMethod . getAttribute ( 'Algorithm' ) ;
205+ var keyEncryptionAlgorithm = keyEncryptionMethod . getAttribute ( 'Algorithm' ) ;
206+ if ( options . disallowDecryptionWithInsecureAlgorithm
207+ && insecureAlgorithms . indexOf ( keyEncryptionAlgorithm ) >= 0 ) {
208+ throw new Error ( 'encryption algorithm ' + keyEncryptionAlgorithm + ' is not secure, fail to decrypt' ) ;
209+ }
192210 var encryptedKey = keyRetrievalMethodUri ?
193211 xpath . select ( "//*[local-name(.)='EncryptedKey' and @Id='" + keyRetrievalMethodUri . substring ( 1 ) + "']/*[local-name(.)='CipherData']/*[local-name(.)='CipherValue']" , keyInfo ) [ 0 ] :
194212 xpath . select ( "//*[local-name(.)='CipherValue']" , keyInfo ) [ 0 ] ;
195213
196- switch ( keyEncryptionAlgorighm ) {
214+ switch ( keyEncryptionAlgorithm ) {
197215 case 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p' :
198216 return decryptKeyInfoWithScheme ( encryptedKey , options , 'RSA-OAEP' ) ;
199217 case 'http://www.w3.org/2001/04/xmlenc#rsa-1_5' :
@@ -204,10 +222,10 @@ function decryptKeyInfo(doc, options) {
204222}
205223
206224function decryptKeyInfoWithScheme ( encryptedKey , options , scheme ) {
207- var key = new Buffer ( encryptedKey . textContent , 'base64' ) . toString ( 'binary' ) ;
225+ var key = Buffer . from ( encryptedKey . textContent , 'base64' ) . toString ( 'binary' ) ;
208226 var private_key = pki . privateKeyFromPem ( options . key ) ;
209227 var decrypted = private_key . decrypt ( key , scheme ) ;
210- return new Buffer ( decrypted , 'binary' ) ;
228+ return Buffer . from ( decrypted , 'binary' ) ;
211229}
212230
213231function encryptWithAlgorithm ( algorithm , symmetricKey , ivLength , content , encoding , callback ) {
@@ -218,7 +236,7 @@ function encryptWithAlgorithm(algorithm, symmetricKey, ivLength, content, encodi
218236 var cipher = crypto . createCipheriv ( algorithm , symmetricKey , iv ) ;
219237 // encrypted content
220238 var encrypted = cipher . update ( content , encoding , 'binary' ) + cipher . final ( 'binary' ) ;
221- return callback ( null , Buffer . concat ( [ iv , new Buffer ( encrypted , 'binary' ) ] ) ) ;
239+ return callback ( null , Buffer . concat ( [ iv , Buffer . from ( encrypted , 'binary' ) ] ) ) ;
222240 } ) ;
223241}
224242
@@ -237,7 +255,7 @@ function decryptWithAlgorithm(algorithm, symmetricKey, ivLength, content) {
237255 return ;
238256 }
239257
240- return new Buffer ( decrypted , 'binary' ) . toString ( 'utf8' ) ;
258+ return Buffer . from ( decrypted , 'binary' ) . toString ( 'utf8' ) ;
241259}
242260
243261exports = module . exports = {
0 commit comments