From 7b83aba9b8d17cae460601e9154a67f92463e486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Tue, 21 Jan 2020 15:14:15 -0400 Subject: [PATCH] crypto: improve errors in DiffieHellmanGroup 1. The DiffieHellmanGroup class is only instantiated from within Node.js, which always passes exactly one argument. 2. Use the existing ERR_CRYPTO_UNKNOWN_DH_GROUP error code for the existing "Unknown group" error. The message has not been changed to prevent breaking existing applications. --- src/node_crypto.cc | 7 ++----- test/parallel/test-crypto-dh.js | 6 +++++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/node_crypto.cc b/src/node_crypto.cc index f03c1e31b994f8..ce9725aa9da536 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -5697,10 +5697,7 @@ void DiffieHellman::DiffieHellmanGroup( Environment* env = Environment::GetCurrent(args); DiffieHellman* diffieHellman = new DiffieHellman(env, args.This()); - if (args.Length() != 1) { - return THROW_ERR_MISSING_ARGS(env, "Group name argument is mandatory"); - } - + CHECK_EQ(args.Length(), 1); THROW_AND_RETURN_IF_NOT_STRING(env, args[0], "Group name"); bool initialized = false; @@ -5708,7 +5705,7 @@ void DiffieHellman::DiffieHellmanGroup( const node::Utf8Value group_name(env->isolate(), args[0]); const modp_group* group = FindDiffieHellmanGroup(*group_name); if (group == nullptr) - return env->ThrowError("Unknown group"); + return THROW_ERR_CRYPTO_UNKNOWN_DH_GROUP(env, "Unknown group"); initialized = diffieHellman->Init(group->prime, group->prime_size, diff --git a/test/parallel/test-crypto-dh.js b/test/parallel/test-crypto-dh.js index 855244a70b1b93..7824800f9bee2e 100644 --- a/test/parallel/test-crypto-dh.js +++ b/test/parallel/test-crypto-dh.js @@ -389,7 +389,11 @@ assert.throws( function() { crypto.getDiffieHellman('unknown-group'); }, - /^Error: Unknown group$/, + { + name: 'Error', + code: 'ERR_CRYPTO_UNKNOWN_DH_GROUP', + message: 'Unknown group' + }, 'crypto.getDiffieHellman(\'unknown-group\') ' + 'failed to throw the expected error.' );