@@ -9,21 +9,22 @@ const insecureAlgorithms = [
99 //https://csrc.nist.gov/News/2017/Update-to-Current-Use-and-Deprecation-of-TDEA
1010 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc' ] ;
1111
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 ) {
1413 const symmetricKeyBuffer = Buffer . isBuffer ( symmetricKey ) ? symmetricKey : Buffer . from ( symmetricKey , 'utf-8' ) ;
1514
1615 try {
1716 var encrypted = crypto . publicEncrypt ( {
1817 key : options . rsa_pub ,
18+ oaepHash : padding == crypto . constants . RSA_PKCS1_OAEP_PADDING ? options . keyEncryptionDigest : undefined ,
1919 padding : padding
2020 } , symmetricKeyBuffer ) ;
2121 var base64EncodedEncryptedKey = encrypted . toString ( 'base64' ) ;
2222
2323 var params = {
2424 encryptedKey : base64EncodedEncryptedKey ,
2525 encryptionPublicCert : '<X509Data><X509Certificate>' + utils . pemToCert ( options . pem . toString ( ) ) + '</X509Certificate></X509Data>' ,
26- keyEncryptionMethod : options . keyEncryptionAlgorithm
26+ keyEncryptionMethod : options . keyEncryptionAlgorithm ,
27+ keyEncryptionDigest : options . keyEncryptionDigest ,
2728 } ;
2829
2930 var result = utils . renderTemplate ( 'keyinfo' , params ) ;
@@ -47,13 +48,14 @@ function encryptKeyInfo(symmetricKey, options, callback) {
4748 && insecureAlgorithms . indexOf ( options . keyEncryptionAlgorithm ) >= 0 ) {
4849 return callback ( new Error ( 'encryption algorithm ' + options . keyEncryptionAlgorithm + 'is not secure' ) ) ;
4950 }
51+ options . keyEncryptionDigest = options . keyEncryptionDigest || 'sha1' ;
5052 switch ( options . keyEncryptionAlgorithm ) {
5153 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 ) ;
5355
5456 case 'http://www.w3.org/2001/04/xmlenc#rsa-1_5' :
5557 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 ) ;
5759
5860 default :
5961 return callback ( new Error ( 'encryption key algorithm not supported' ) ) ;
@@ -235,6 +237,20 @@ function decryptKeyInfo(doc, options) {
235237 throw new Error ( 'cant find encryption algorithm' ) ;
236238 }
237239
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+
238254 var keyEncryptionAlgorithm = keyEncryptionMethod . getAttribute ( 'Algorithm' ) ;
239255 if ( options . disallowDecryptionWithInsecureAlgorithm
240256 && insecureAlgorithms . indexOf ( keyEncryptionAlgorithm ) >= 0 ) {
@@ -246,19 +262,18 @@ function decryptKeyInfo(doc, options) {
246262
247263 switch ( keyEncryptionAlgorithm ) {
248264 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 ) ;
250266 case 'http://www.w3.org/2001/04/xmlenc#rsa-1_5' :
251267 utils . warnInsecureAlgorithm ( keyEncryptionAlgorithm , options . warnInsecureAlgorithm ) ;
252- return decryptKeyInfoWithScheme ( encryptedKey , options , 'RSAES-PKCS1-V1_5' ) ;
268+ return decryptKeyInfoWithScheme ( encryptedKey , options , crypto . constants . RSA_PKCS1_PADDING ) ;
253269 default :
254270 throw new Error ( 'key encryption algorithm ' + keyEncryptionAlgorithm + ' not supported' ) ;
255271 }
256272}
257273
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 ) ;
262277 return Buffer . from ( decrypted , 'binary' ) ;
263278}
264279
0 commit comments