Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace node-forge by native node crypto. #86

Merged
merged 1 commit into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

W3C XML Encryption implementation for node.js (http://www.w3.org/TR/xmlenc-core/)

Supports node >= 8
Supports node >= 12

## Usage

Expand Down
19 changes: 12 additions & 7 deletions lib/xmlenc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ var crypto = require('crypto');
var xmldom = require('@xmldom/xmldom');
var xpath = require('xpath');
var utils = require('./utils');
var pki = require('node-forge').pki;

const insecureAlgorithms = [
//https://www.w3.org/TR/xmlenc-core1/#rsav15note
'http://www.w3.org/2001/04/xmlenc#rsa-1_5',
//https://csrc.nist.gov/News/2017/Update-to-Current-Use-and-Deprecation-of-TDEA
'http://www.w3.org/2001/04/xmlenc#tripledes-cbc'];

function encryptKeyInfoWithScheme(symmetricKey, options, scheme, callback) {
const padding = scheme === 'RSA-OAEP' ? crypto.constants.RSA_PKCS1_OAEP_PADDING : crypto.constants.RSA_PKCS1_PADDING;
const symmetricKeyBuffer = Buffer.isBuffer(symmetricKey) ? symmetricKey : Buffer.from(symmetricKey, 'utf-8');

try {
var rsa_pub = pki.publicKeyFromPem(options.rsa_pub);
var encrypted = rsa_pub.encrypt(symmetricKey.toString('binary'), scheme);
var base64EncodedEncryptedKey = Buffer.from(encrypted, 'binary').toString('base64');
var encrypted = crypto.publicEncrypt({
key: options.rsa_pub,
padding: padding
}, symmetricKeyBuffer);
var base64EncodedEncryptedKey = encrypted.toString('base64');

var params = {
encryptedKey: base64EncodedEncryptedKey,
Expand Down Expand Up @@ -248,9 +253,9 @@ function decryptKeyInfo(doc, options) {
}

function decryptKeyInfoWithScheme(encryptedKey, options, scheme) {
var key = Buffer.from(encryptedKey.textContent, 'base64').toString('binary');
var private_key = pki.privateKeyFromPem(options.key);
var decrypted = private_key.decrypt(key, scheme);
var padding = scheme === 'RSA-OAEP' ? crypto.constants.RSA_PKCS1_OAEP_PADDING : crypto.constants.RSA_PKCS1_PADDING;
var key = Buffer.from(encryptedKey.textContent, 'base64');
var decrypted = crypto.privateDecrypt({ key: options.key, padding: padding}, key);
return Buffer.from(decrypted, 'binary');
}

Expand Down
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"dependencies": {
"@xmldom/xmldom": "^0.7.0",
"escape-html": "^1.0.3",
"node-forge": "^0.10.0",
"xpath": "0.0.32"
},
"files": [
Expand Down