Skip to content

Commit

Permalink
crypto: improve errors in DiffieHellmanGroup
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: David Carlier <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
tniessen authored and targos committed Apr 28, 2020
1 parent 30633ac commit 907252d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
7 changes: 2 additions & 5 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5158,18 +5158,15 @@ 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;

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,
Expand Down
6 changes: 5 additions & 1 deletion test/parallel/test-crypto-dh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
);
Expand Down

0 comments on commit 907252d

Please sign in to comment.