@@ -238,6 +238,25 @@ function decryptKeyInfo(doc, options) {
238
238
throw new Error ( 'cant find encryption algorithm' ) ;
239
239
}
240
240
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
+
245
+ switch ( keyDigestMethodAlgorithm ) {
246
+ case 'http://www.w3.org/2000/09/xmldsig#sha1' :
247
+ options . oaepHash = 'sha1' ;
248
+ break ;
249
+ case 'http://www.w3.org/2000/09/xmldsig#sha256' :
250
+ options . oaepHash = 'sha256' ;
251
+ break ;
252
+ case 'http://www.w3.org/2000/09/xmldsig#sha512' :
253
+ options . oaepHash = 'sha512' ;
254
+ break ;
255
+ default :
256
+ throw new Error ( 'key encryption digest algorithm ' + keyDigestMethodAlgorithm + ' not supported' ) ;
257
+ }
258
+ }
259
+
241
260
var keyEncryptionAlgorithm = keyEncryptionMethod . getAttribute ( 'Algorithm' ) ;
242
261
if ( options . disallowDecryptionWithInsecureAlgorithm
243
262
&& insecureAlgorithms . indexOf ( keyEncryptionAlgorithm ) >= 0 ) {
@@ -259,10 +278,10 @@ function decryptKeyInfo(doc, options) {
259
278
}
260
279
261
280
function decryptKeyInfoWithScheme ( encryptedKey , options , scheme ) {
262
- var padding = scheme === 'RSA-OAEP' ? crypto . constants . RSA_PKCS1_OAEP_PADDING : crypto . constants . RSA_PKCS1_PADDING ;
263
- var key = Buffer . from ( encryptedKey . textContent , 'base64' ) ;
281
+ const padding = scheme === 'RSA-OAEP' ? crypto . constants . RSA_PKCS1_OAEP_PADDING : crypto . constants . RSA_PKCS1_PADDING ;
282
+ const key = Buffer . from ( encryptedKey . textContent , 'base64' ) ;
264
283
const oaepHash = options . oaepHash || 'sha1' ;
265
- var decrypted = crypto . privateDecrypt ( { key : options . key , oaepHash : oaepHash , padding : padding } , key ) ;
284
+ const decrypted = crypto . privateDecrypt ( { key : options . key , oaepHash, padding} , key ) ;
266
285
return Buffer . from ( decrypted , 'binary' ) ;
267
286
}
268
287
0 commit comments