crypto: createDiffieHellman throw for bad args

Previously crypto.createDiffieHellman() would fail silently when a bad
argument was passed for prime/prime_length. Now throws TypeError.

Fixes: https://github.com/joyent/node/issues/8480
Signed-off-by: Trevor Norris <trev.norris@gmail.com>
This commit is contained in:
Trevor Norris 2014-10-08 15:38:46 -07:00
Родитель d87480beb2
Коммит 1a4a189578
2 изменённых файлов: 21 добавлений и 0 удалений

Просмотреть файл

@ -376,6 +376,11 @@ function DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
if (!(this instanceof DiffieHellman))
return new DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding);
if (!util.isBuffer(sizeOrKey) &&
typeof sizeOrKey !== 'number' &&
typeof sizeOrKey !== 'string')
throw new TypeError('First argument should be number, string or Buffer');
if (keyEncoding) {
if (typeof keyEncoding !== 'string' ||
(!Buffer.isEncoding(keyEncoding) && keyEncoding !== 'buffer')) {

Просмотреть файл

@ -719,6 +719,22 @@ assert.equal(secret1, secret2.toString('base64'));
assert.equal(dh1.verifyError, 0);
assert.equal(dh2.verifyError, 0);
assert.throws(function() {
crypto.createDiffieHellman([0x1, 0x2]);
});
assert.throws(function() {
crypto.createDiffieHellman(function() { });
});
assert.throws(function() {
crypto.createDiffieHellman(/abc/);
});
assert.throws(function() {
crypto.createDiffieHellman({});
});
// Create "another dh1" using generated keys from dh1,
// and compute secret again
var dh3 = crypto.createDiffieHellman(p1, 'buffer');