Skip to content

Commit

Permalink
feat: Add warning when insecure algorithm is used. (#68)
Browse files Browse the repository at this point in the history
The warning is piped to stderr using console.warn().
Added option to turn it off; defaults to true.
  • Loading branch information
gkwang committed Mar 25, 2020
1 parent f5651cc commit 25d22fd
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 10 deletions.
11 changes: 6 additions & 5 deletions README.md
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 @@ -81,9 +83,8 @@ Currently the library supports:
* http://www.w3.org/2009/xmlenc11#aes256-gcm
* 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
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
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 @@ -85,6 +86,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 Down Expand Up @@ -119,6 +121,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 @@ -193,6 +196,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));
case 'http://www.w3.org/2009/xmlenc11#aes128-gcm':
return callback(null, decryptWithAlgorithm('aes-128-gcm', symmetricKey, 12, encrypted));
Expand Down Expand Up @@ -236,6 +240,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
114 changes: 114 additions & 0 deletions 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
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
@@ -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 @@ -58,9 +68,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 @@ -80,6 +91,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 @@ -192,7 +205,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 @@ -210,7 +222,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

0 comments on commit 25d22fd

Please sign in to comment.