Skip to content

Commit 63d5817

Browse files
tniessentargos
authored andcommitted
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: nodejs#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>
1 parent 9718497 commit 63d5817

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/node_crypto.cc

+2-5
Original file line numberDiff line numberDiff line change
@@ -5158,18 +5158,15 @@ void DiffieHellman::DiffieHellmanGroup(
51585158
Environment* env = Environment::GetCurrent(args);
51595159
DiffieHellman* diffieHellman = new DiffieHellman(env, args.This());
51605160

5161-
if (args.Length() != 1) {
5162-
return THROW_ERR_MISSING_ARGS(env, "Group name argument is mandatory");
5163-
}
5164-
5161+
CHECK_EQ(args.Length(), 1);
51655162
THROW_AND_RETURN_IF_NOT_STRING(env, args[0], "Group name");
51665163

51675164
bool initialized = false;
51685165

51695166
const node::Utf8Value group_name(env->isolate(), args[0]);
51705167
const modp_group* group = FindDiffieHellmanGroup(*group_name);
51715168
if (group == nullptr)
5172-
return env->ThrowError("Unknown group");
5169+
return THROW_ERR_CRYPTO_UNKNOWN_DH_GROUP(env, "Unknown group");
51735170

51745171
initialized = diffieHellman->Init(group->prime,
51755172
group->prime_size,

test/parallel/test-crypto-dh.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,11 @@ assert.throws(
389389
function() {
390390
crypto.getDiffieHellman('unknown-group');
391391
},
392-
/^Error: Unknown group$/,
392+
{
393+
name: 'Error',
394+
code: 'ERR_CRYPTO_UNKNOWN_DH_GROUP',
395+
message: 'Unknown group'
396+
},
393397
'crypto.getDiffieHellman(\'unknown-group\') ' +
394398
'failed to throw the expected error.'
395399
);

0 commit comments

Comments
 (0)