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.

PR-URL: https://github.com/nodejs/node/pull/31445
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Tobias Nießen authored and Michaël Zasso committed 2020-04-28 16:00:36 +02:00
1 parent 30633acf20
commit 907252d4cf
2 files changed
+7 -6

No files matched your search

+2 -5
View File
@@ -5158,10 +5158,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;
@@ -5169,7 +5166,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,
+5 -1
View File
@@ -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.'
);