@@ -9,21 +9,22 @@ const insecureAlgorithms = [
9
9
//https://csrc.nist.gov/News/2017/Update-to-Current-Use-and-Deprecation-of-TDEA
10
10
'http://www.w3.org/2001/04/xmlenc#tripledes-cbc' ] ;
11
11
12
- function encryptKeyInfoWithScheme ( symmetricKey , options , scheme , callback ) {
13
- const padding = scheme === 'RSA-OAEP' ? crypto . constants . RSA_PKCS1_OAEP_PADDING : crypto . constants . RSA_PKCS1_PADDING ;
12
+ function encryptKeyInfoWithScheme ( symmetricKey , options , padding , callback ) {
14
13
const symmetricKeyBuffer = Buffer . isBuffer ( symmetricKey ) ? symmetricKey : Buffer . from ( symmetricKey , 'utf-8' ) ;
15
14
16
15
try {
17
16
var encrypted = crypto . publicEncrypt ( {
18
17
key : options . rsa_pub ,
18
+ oaepHash : padding == crypto . constants . RSA_PKCS1_OAEP_PADDING ? options . keyEncryptionDigest : undefined ,
19
19
padding : padding
20
20
} , symmetricKeyBuffer ) ;
21
21
var base64EncodedEncryptedKey = encrypted . toString ( 'base64' ) ;
22
22
23
23
var params = {
24
24
encryptedKey : base64EncodedEncryptedKey ,
25
25
encryptionPublicCert : '<X509Data><X509Certificate>' + utils . pemToCert ( options . pem . toString ( ) ) + '</X509Certificate></X509Data>' ,
26
- keyEncryptionMethod : options . keyEncryptionAlgorithm
26
+ keyEncryptionMethod : options . keyEncryptionAlgorithm ,
27
+ keyEncryptionDigest : options . keyEncryptionDigest ,
27
28
} ;
28
29
29
30
var result = utils . renderTemplate ( 'keyinfo' , params ) ;
@@ -47,13 +48,14 @@ function encryptKeyInfo(symmetricKey, options, callback) {
47
48
&& insecureAlgorithms . indexOf ( options . keyEncryptionAlgorithm ) >= 0 ) {
48
49
return callback ( new Error ( 'encryption algorithm ' + options . keyEncryptionAlgorithm + 'is not secure' ) ) ;
49
50
}
51
+ options . keyEncryptionDigest = options . keyEncryptionDigest || 'sha1' ;
50
52
switch ( options . keyEncryptionAlgorithm ) {
51
53
case 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p' :
52
- return encryptKeyInfoWithScheme ( symmetricKey , options , 'RSA-OAEP' , callback ) ;
54
+ return encryptKeyInfoWithScheme ( symmetricKey , options , crypto . constants . RSA_PKCS1_OAEP_PADDING , callback ) ;
53
55
54
56
case 'http://www.w3.org/2001/04/xmlenc#rsa-1_5' :
55
57
utils . warnInsecureAlgorithm ( options . keyEncryptionAlgorithm , options . warnInsecureAlgorithm ) ;
56
- return encryptKeyInfoWithScheme ( symmetricKey , options , 'RSAES-PKCS1-V1_5' , callback ) ;
58
+ return encryptKeyInfoWithScheme ( symmetricKey , options , crypto . constants . RSA_PKCS1_PADDING , callback ) ;
57
59
58
60
default :
59
61
return callback ( new Error ( 'encryption key algorithm not supported' ) ) ;
@@ -235,6 +237,20 @@ function decryptKeyInfo(doc, options) {
235
237
throw new Error ( 'cant find encryption algorithm' ) ;
236
238
}
237
239
240
+ let oaepHash = 'sha1' ;
241
+ const keyDigestMethod = xpath . select ( "//*[local-name(.)='KeyInfo']/*[local-name(.)='EncryptedKey']/*[local-name(.)='EncryptionMethod']/*[local-name(.)='DigestMethod']" , doc ) [ 0 ] ;
242
+ if ( keyDigestMethod ) {
243
+ const keyDigestMethodAlgorithm = keyDigestMethod . getAttribute ( 'Algorithm' ) ;
244
+ switch ( keyDigestMethodAlgorithm ) {
245
+ case 'http://www.w3.org/2000/09/xmldsig#sha256' :
246
+ oaepHash = 'sha256' ;
247
+ break ;
248
+ case 'http://www.w3.org/2000/09/xmldsig#sha512' :
249
+ oaepHash = 'sha512' ;
250
+ break ;
251
+ }
252
+ }
253
+
238
254
var keyEncryptionAlgorithm = keyEncryptionMethod . getAttribute ( 'Algorithm' ) ;
239
255
if ( options . disallowDecryptionWithInsecureAlgorithm
240
256
&& insecureAlgorithms . indexOf ( keyEncryptionAlgorithm ) >= 0 ) {
@@ -246,19 +262,18 @@ function decryptKeyInfo(doc, options) {
246
262
247
263
switch ( keyEncryptionAlgorithm ) {
248
264
case 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p' :
249
- return decryptKeyInfoWithScheme ( encryptedKey , options , 'RSA-OAEP' ) ;
265
+ return decryptKeyInfoWithScheme ( encryptedKey , options , crypto . constants . RSA_PKCS1_OAEP_PADDING , oaepHash ) ;
250
266
case 'http://www.w3.org/2001/04/xmlenc#rsa-1_5' :
251
267
utils . warnInsecureAlgorithm ( keyEncryptionAlgorithm , options . warnInsecureAlgorithm ) ;
252
- return decryptKeyInfoWithScheme ( encryptedKey , options , 'RSAES-PKCS1-V1_5' ) ;
268
+ return decryptKeyInfoWithScheme ( encryptedKey , options , crypto . constants . RSA_PKCS1_PADDING ) ;
253
269
default :
254
270
throw new Error ( 'key encryption algorithm ' + keyEncryptionAlgorithm + ' not supported' ) ;
255
271
}
256
272
}
257
273
258
- function decryptKeyInfoWithScheme ( encryptedKey , options , scheme ) {
259
- var padding = scheme === 'RSA-OAEP' ? crypto . constants . RSA_PKCS1_OAEP_PADDING : crypto . constants . RSA_PKCS1_PADDING ;
260
- var key = Buffer . from ( encryptedKey . textContent , 'base64' ) ;
261
- var decrypted = crypto . privateDecrypt ( { key : options . key , padding : padding } , key ) ;
274
+ function decryptKeyInfoWithScheme ( encryptedKey , options , padding , oaepHash ) {
275
+ const key = Buffer . from ( encryptedKey . textContent , 'base64' ) ;
276
+ const decrypted = crypto . privateDecrypt ( { key : options . key , padding, oaepHash} , key ) ;
262
277
return Buffer . from ( decrypted , 'binary' ) ;
263
278
}
264
279
0 commit comments