Skip to content

Commit f84b34d

Browse files
tniessencodebytere
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: #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 4591202 commit f84b34d

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
@@ -5682,18 +5682,15 @@ void DiffieHellman::DiffieHellmanGroup(
56825682
Environment* env = Environment::GetCurrent(args);
56835683
DiffieHellman* diffieHellman = new DiffieHellman(env, args.This());
56845684

5685-
if (args.Length() != 1) {
5686-
return THROW_ERR_MISSING_ARGS(env, "Group name argument is mandatory");
5687-
}
5688-
5685+
CHECK_EQ(args.Length(), 1);
56895686
THROW_AND_RETURN_IF_NOT_STRING(env, args[0], "Group name");
56905687

56915688
bool initialized = false;
56925689

56935690
const node::Utf8Value group_name(env->isolate(), args[0]);
56945691
const modp_group* group = FindDiffieHellmanGroup(*group_name);
56955692
if (group == nullptr)
5696-
return env->ThrowError("Unknown group");
5693+
return THROW_ERR_CRYPTO_UNKNOWN_DH_GROUP(env, "Unknown group");
56975694

56985695
initialized = diffieHellman->Init(group->prime,
56995696
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)