Skip to content

feat: Add warning when insecure algorithm is used. #68

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

Merged
merged 1 commit into from
Mar 25, 2020
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
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var options = {
pem: fs.readFileSync(__dirname + '/your_public_cert.pem'),
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#aes256-cbc',
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p',
disallowEncryptionWithInsecureAlgorithm: true
disallowEncryptionWithInsecureAlgorithm: true,
warnInsecureAlgorithm: true
};

xmlenc.encrypt('content to encrypt', options, function(err, result) {
Expand Down Expand Up @@ -54,7 +55,8 @@ Result:
~~~js
var options = {
key: fs.readFileSync(__dirname + '/your_private_key.key'),
disallowDecryptionWithInsecureAlgorithm: true;
disallowDecryptionWithInsecureAlgorithm: true,
warnInsecureAlgorithm: true
};

xmlenc.decrypt('<xenc:EncryptedData ..... </xenc:EncryptedData>', options, function(err, result) {
Expand All @@ -79,9 +81,8 @@ Currently the library supports:
* http://www.w3.org/2001/04/xmlenc#aes256-cbc
* http://www.w3.org/2001/04/xmlenc#tripledes-cbc (Insecure Algorithm)

Insecure Algorithms can be disabled via disallowEncryptionWithInsecureAlgorithm/disallowDecryptionWithInsecureAlgorithm flags when encrypting/decrypting. This flag is off by default in 0.x versions.

However, you can fork and implement your own algorithm. The code supports adding more algorithms easily
Insecure Algorithms can be disabled via `disallowEncryptionWithInsecureAlgorithm`/`disallowDecryptionWithInsecureAlgorithm` flags when encrypting/decrypting. This flag is off by default in 0.x versions.
A warning will be piped to `stderr` using console.warn() by default when the aforementioned algorithms are used. This can be disabled via the `warnInsecureAlgorithm` flag.

## Issue Reporting

Expand Down
10 changes: 8 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var templates = {
'keyinfo': require('./templates/keyinfo.tpl.xml'),
};

function renderTemplate (file, data) {
function renderTemplate(file, data) {
return templates[file](data);
}

Expand All @@ -19,8 +19,14 @@ function pemToCert(pem) {
return null;
};

function warnInsecureAlgorithm(algorithm, enabled = true) {
if (enabled) {
console.warn(algorithm + " is no longer recommended due to security reasons. Please deprecate its use as soon as possible.")
}
}

module.exports = {
renderTemplate: renderTemplate,
pemToCert: pemToCert
pemToCert: pemToCert,
warnInsecureAlgorithm, warnInsecureAlgorithm
};
5 changes: 5 additions & 0 deletions lib/xmlenc.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function encryptKeyInfo(symmetricKey, options, callback) {
return encryptKeyInfoWithScheme(symmetricKey, options, 'RSA-OAEP', callback);

case 'http://www.w3.org/2001/04/xmlenc#rsa-1_5':
utils.warnInsecureAlgorithm(options.keyEncryptionAlgorithm, options.warnInsecureAlgorithm);
return encryptKeyInfoWithScheme(symmetricKey, options, 'RSAES-PKCS1-V1_5', callback);

default:
Expand Down Expand Up @@ -79,6 +80,7 @@ function encrypt(content, options, callback) {
crypto.randomBytes(32, cb); // generate a symmetric random key 32 bytes length
break;
case 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc':
utils.warnInsecureAlgorithm(options.encryptionAlgorithm, options.warnInsecureAlgorithm);
crypto.randomBytes(24, cb); // generate a symmetric random key 24 bytes (192 bits) length
break;
default:
Expand All @@ -101,6 +103,7 @@ function encrypt(content, options, callback) {
});
break;
case 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc':
utils.warnInsecureAlgorithm(options.encryptionAlgorithm, options.warnInsecureAlgorithm);
encryptWithAlgorithm('des-ede3-cbc', symmetricKey, 8, content, options.input_encoding, function (err, encryptedContent) {
if (err) return cb(err);
cb(null, encryptedContent);
Expand Down Expand Up @@ -177,6 +180,7 @@ function decrypt(xml, options, callback) {
case 'http://www.w3.org/2001/04/xmlenc#aes256-cbc':
return callback(null, decryptWithAlgorithm('aes-256-cbc', symmetricKey, 16, encrypted));
case 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc':
utils.warnInsecureAlgorithm(encryptionAlgorithm, options.warnInsecureAlgorithm);
return callback(null, decryptWithAlgorithm('des-ede3-cbc', symmetricKey, 8, encrypted));
default:
return callback(new Error('encryption algorithm ' + encryptionAlgorithm + ' not supported'));
Expand Down Expand Up @@ -216,6 +220,7 @@ function decryptKeyInfo(doc, options) {
case 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p':
return decryptKeyInfoWithScheme(encryptedKey, options, 'RSA-OAEP');
case 'http://www.w3.org/2001/04/xmlenc#rsa-1_5':
utils.warnInsecureAlgorithm(keyEncryptionAlgorithm, options.warnInsecureAlgorithm);
return decryptKeyInfoWithScheme(encryptedKey, options, 'RSAES-PKCS1-V1_5');
default:
throw new Error('key encryption algorithm ' + keyEncryptionAlgorithm + ' not supported');
Expand Down
116 changes: 115 additions & 1 deletion package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"dependencies": {
"escape-html": "^1.0.3",
"node-forge": "^0.7.0",
"sinon": "^9.0.1",
"xmldom": "~0.1.15",
"xpath": "0.0.27"
},
Expand Down
18 changes: 15 additions & 3 deletions test/xmlenc.encryptedkey.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
var assert = require('assert');
var fs = require('fs');
var should = require('should');
var sinon = require('sinon');
var xmlenc = require('../lib');
var xpath = require('xpath');

describe('encrypt', function() {
let consoleSpy = null;
beforeEach(function() {
consoleSpy = sinon.spy(console, 'warn');
});

afterEach(function() {
consoleSpy.restore();
});

var algorithms = [{
name: 'aes-256-cbc',
Expand Down Expand Up @@ -46,9 +56,10 @@ describe('encrypt', function() {
options.rsa_pub = fs.readFileSync(__dirname + '/test-auth0_rsa.pub'),
options.pem = fs.readFileSync(__dirname + '/test-auth0.pem'),
options.key = fs.readFileSync(__dirname + '/test-auth0.key'),
options.warnInsecureAlgorithm = false;

xmlenc.encrypt(content, options, function(err, result) {
xmlenc.decrypt(result, { key: fs.readFileSync(__dirname + '/test-auth0.key')}, function (err, decrypted) {
xmlenc.decrypt(result, { key: fs.readFileSync(__dirname + '/test-auth0.key'), warnInsecureAlgorithm: false}, function (err, decrypted) {
assert.equal(decrypted, content);
done();
});
Expand All @@ -68,6 +79,8 @@ describe('encrypt', function() {
xmlenc.encrypt('encrypt me', options, function(err, result) {
assert(err);
assert(!result);
//should not pop up warns due to options.warnInsecureAlgorithm = false;
consoleSpy.called.should.equal(false);
done();
});
});
Expand Down Expand Up @@ -180,7 +193,6 @@ describe('encrypt', function() {
};

var plaintext = 'The quick brown fox jumps over the lazy dog';

xmlenc.encryptKeyInfo(plaintext, options, function(err, encryptedKeyInfo) {
assert(err);
done();
Expand All @@ -198,7 +210,7 @@ describe('encrypt', function() {

xmlenc.encryptKeyInfo(plaintext, options, function(err, encryptedKeyInfo) {
if (err) return done(err);

consoleSpy.called.should.equal(true);
assert.throws(
function(){xmlenc.decryptKeyInfo(
encryptedKeyInfo,
Expand Down