Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: improve errors in DiffieHellmanGroup #31445

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/node_crypto.cc
Expand Up @@ -5697,18 +5697,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
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