@@ -2,18 +2,23 @@ var crypto = require('crypto');
2
2
var xmldom = require ( '@xmldom/xmldom' ) ;
3
3
var xpath = require ( 'xpath' ) ;
4
4
var utils = require ( './utils' ) ;
5
- var pki = require ( 'node-forge' ) . pki ;
6
5
7
6
const insecureAlgorithms = [
8
7
//https://www.w3.org/TR/xmlenc-core1/#rsav15note
9
8
'http://www.w3.org/2001/04/xmlenc#rsa-1_5' ,
10
9
//https://csrc.nist.gov/News/2017/Update-to-Current-Use-and-Deprecation-of-TDEA
11
10
'http://www.w3.org/2001/04/xmlenc#tripledes-cbc' ] ;
11
+
12
12
function encryptKeyInfoWithScheme ( symmetricKey , options , scheme , callback ) {
13
+ const padding = scheme === 'RSA-OAEP' ? crypto . constants . RSA_PKCS1_OAEP_PADDING : crypto . constants . RSA_PKCS1_PADDING ;
14
+ const symmetricKeyBuffer = Buffer . isBuffer ( symmetricKey ) ? symmetricKey : Buffer . from ( symmetricKey , 'utf-8' ) ;
15
+
13
16
try {
14
- var rsa_pub = pki . publicKeyFromPem ( options . rsa_pub ) ;
15
- var encrypted = rsa_pub . encrypt ( symmetricKey . toString ( 'binary' ) , scheme ) ;
16
- var base64EncodedEncryptedKey = Buffer . from ( encrypted , 'binary' ) . toString ( 'base64' ) ;
17
+ var encrypted = crypto . publicEncrypt ( {
18
+ key : options . rsa_pub ,
19
+ padding : padding
20
+ } , symmetricKeyBuffer ) ;
21
+ var base64EncodedEncryptedKey = encrypted . toString ( 'base64' ) ;
17
22
18
23
var params = {
19
24
encryptedKey : base64EncodedEncryptedKey ,
@@ -248,9 +253,9 @@ function decryptKeyInfo(doc, options) {
248
253
}
249
254
250
255
function decryptKeyInfoWithScheme ( encryptedKey , options , scheme ) {
251
- var key = Buffer . from ( encryptedKey . textContent , 'base64' ) . toString ( 'binary' ) ;
252
- var private_key = pki . privateKeyFromPem ( options . key ) ;
253
- var decrypted = private_key . decrypt ( key , scheme ) ;
256
+ var padding = scheme === 'RSA-OAEP' ? crypto . constants . RSA_PKCS1_OAEP_PADDING : crypto . constants . RSA_PKCS1_PADDING ;
257
+ var key = Buffer . from ( encryptedKey . textContent , 'base64' ) ;
258
+ var decrypted = crypto . privateDecrypt ( { key : options . key , padding : padding } , key ) ;
254
259
return Buffer . from ( decrypted , 'binary' ) ;
255
260
}
256
261
0 commit comments