Skip to content

Commit 25d22fd

Browse files
authored
feat: Add warning when insecure algorithm is used. (#68)
The warning is piped to stderr using console.warn(). Added option to turn it off; defaults to true.
1 parent f5651cc commit 25d22fd

File tree

6 files changed

+149
-10
lines changed

6 files changed

+149
-10
lines changed

Diff for: README.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ var options = {
1818
pem: fs.readFileSync(__dirname + '/your_public_cert.pem'),
1919
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#aes256-cbc',
2020
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p',
21-
disallowEncryptionWithInsecureAlgorithm: true
21+
disallowEncryptionWithInsecureAlgorithm: true,
22+
warnInsecureAlgorithm: true
2223
};
2324

2425
xmlenc.encrypt('content to encrypt', options, function(err, result) {
@@ -54,7 +55,8 @@ Result:
5455
~~~js
5556
var options = {
5657
key: fs.readFileSync(__dirname + '/your_private_key.key'),
57-
disallowDecryptionWithInsecureAlgorithm: true;
58+
disallowDecryptionWithInsecureAlgorithm: true,
59+
warnInsecureAlgorithm: true
5860
};
5961

6062
xmlenc.decrypt('<xenc:EncryptedData ..... </xenc:EncryptedData>', options, function(err, result) {
@@ -81,9 +83,8 @@ Currently the library supports:
8183
* http://www.w3.org/2009/xmlenc11#aes256-gcm
8284
* http://www.w3.org/2001/04/xmlenc#tripledes-cbc (Insecure Algorithm)
8385

84-
Insecure Algorithms can be disabled via disallowEncryptionWithInsecureAlgorithm/disallowDecryptionWithInsecureAlgorithm flags when encrypting/decrypting. This flag is off by default in 0.x versions.
85-
86-
However, you can fork and implement your own algorithm. The code supports adding more algorithms easily
86+
Insecure Algorithms can be disabled via `disallowEncryptionWithInsecureAlgorithm`/`disallowDecryptionWithInsecureAlgorithm` flags when encrypting/decrypting. This flag is off by default in 0.x versions.
87+
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.
8788

8889
## Issue Reporting
8990

Diff for: lib/utils.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var templates = {
66
'keyinfo': require('./templates/keyinfo.tpl.xml'),
77
};
88

9-
function renderTemplate (file, data) {
9+
function renderTemplate(file, data) {
1010
return templates[file](data);
1111
}
1212

@@ -19,8 +19,14 @@ function pemToCert(pem) {
1919
return null;
2020
};
2121

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

2328
module.exports = {
2429
renderTemplate: renderTemplate,
25-
pemToCert: pemToCert
30+
pemToCert: pemToCert,
31+
warnInsecureAlgorithm, warnInsecureAlgorithm
2632
};

Diff for: lib/xmlenc.js

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ function encryptKeyInfo(symmetricKey, options, callback) {
4747
return encryptKeyInfoWithScheme(symmetricKey, options, 'RSA-OAEP', callback);
4848

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

5253
default:
@@ -85,6 +86,7 @@ function encrypt(content, options, callback) {
8586
crypto.randomBytes(32, cb); // generate a symmetric random key 32 bytes length
8687
break;
8788
case 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc':
89+
utils.warnInsecureAlgorithm(options.encryptionAlgorithm, options.warnInsecureAlgorithm);
8890
crypto.randomBytes(24, cb); // generate a symmetric random key 24 bytes (192 bits) length
8991
break;
9092
default:
@@ -119,6 +121,7 @@ function encrypt(content, options, callback) {
119121
});
120122
break;
121123
case 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc':
124+
utils.warnInsecureAlgorithm(options.encryptionAlgorithm, options.warnInsecureAlgorithm);
122125
encryptWithAlgorithm('des-ede3-cbc', symmetricKey, 8, content, options.input_encoding, function (err, encryptedContent) {
123126
if (err) return cb(err);
124127
cb(null, encryptedContent);
@@ -193,6 +196,7 @@ function decrypt(xml, options, callback) {
193196
case 'http://www.w3.org/2001/04/xmlenc#aes256-cbc':
194197
return callback(null, decryptWithAlgorithm('aes-256-cbc', symmetricKey, 16, encrypted));
195198
case 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc':
199+
utils.warnInsecureAlgorithm(encryptionAlgorithm, options.warnInsecureAlgorithm);
196200
return callback(null, decryptWithAlgorithm('des-ede3-cbc', symmetricKey, 8, encrypted));
197201
case 'http://www.w3.org/2009/xmlenc11#aes128-gcm':
198202
return callback(null, decryptWithAlgorithm('aes-128-gcm', symmetricKey, 12, encrypted));
@@ -236,6 +240,7 @@ function decryptKeyInfo(doc, options) {
236240
case 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p':
237241
return decryptKeyInfoWithScheme(encryptedKey, options, 'RSA-OAEP');
238242
case 'http://www.w3.org/2001/04/xmlenc#rsa-1_5':
243+
utils.warnInsecureAlgorithm(keyEncryptionAlgorithm, options.warnInsecureAlgorithm);
239244
return decryptKeyInfoWithScheme(encryptedKey, options, 'RSAES-PKCS1-V1_5');
240245
default:
241246
throw new Error('key encryption algorithm ' + keyEncryptionAlgorithm + ' not supported');

Diff for: package-lock.json

+114
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"dependencies": {
2222
"escape-html": "^1.0.3",
2323
"node-forge": "^0.7.0",
24+
"sinon": "^9.0.1",
2425
"xmldom": "~0.1.15",
2526
"xpath": "0.0.27"
2627
},

Diff for: test/xmlenc.encryptedkey.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
var assert = require('assert');
22
var fs = require('fs');
3+
var should = require('should');
4+
var sinon = require('sinon');
35
var xmlenc = require('../lib');
46
var xpath = require('xpath');
57

68
describe('encrypt', function() {
9+
let consoleSpy = null;
10+
beforeEach(function() {
11+
consoleSpy = sinon.spy(console, 'warn');
12+
});
13+
14+
afterEach(function() {
15+
consoleSpy.restore();
16+
});
717

818
var algorithms = [{
919
name: 'aes-256-cbc',
@@ -58,9 +68,10 @@ describe('encrypt', function() {
5868
options.rsa_pub = fs.readFileSync(__dirname + '/test-auth0_rsa.pub'),
5969
options.pem = fs.readFileSync(__dirname + '/test-auth0.pem'),
6070
options.key = fs.readFileSync(__dirname + '/test-auth0.key'),
71+
options.warnInsecureAlgorithm = false;
6172

6273
xmlenc.encrypt(content, options, function(err, result) {
63-
xmlenc.decrypt(result, { key: fs.readFileSync(__dirname + '/test-auth0.key')}, function (err, decrypted) {
74+
xmlenc.decrypt(result, { key: fs.readFileSync(__dirname + '/test-auth0.key'), warnInsecureAlgorithm: false}, function (err, decrypted) {
6475
assert.equal(decrypted, content);
6576
done();
6677
});
@@ -80,6 +91,8 @@ describe('encrypt', function() {
8091
xmlenc.encrypt('encrypt me', options, function(err, result) {
8192
assert(err);
8293
assert(!result);
94+
//should not pop up warns due to options.warnInsecureAlgorithm = false;
95+
consoleSpy.called.should.equal(false);
8396
done();
8497
});
8598
});
@@ -192,7 +205,6 @@ describe('encrypt', function() {
192205
};
193206

194207
var plaintext = 'The quick brown fox jumps over the lazy dog';
195-
196208
xmlenc.encryptKeyInfo(plaintext, options, function(err, encryptedKeyInfo) {
197209
assert(err);
198210
done();
@@ -210,7 +222,7 @@ describe('encrypt', function() {
210222

211223
xmlenc.encryptKeyInfo(plaintext, options, function(err, encryptedKeyInfo) {
212224
if (err) return done(err);
213-
225+
consoleSpy.called.should.equal(true);
214226
assert.throws(
215227
function(){xmlenc.decryptKeyInfo(
216228
encryptedKeyInfo,

0 commit comments

Comments
 (0)