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: unify validation of checkPrime checks #47165

Merged
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: 4 additions & 3 deletions lib/internal/crypto/random.js
Expand Up @@ -51,7 +51,6 @@ const {
validateFunction,
validateInt32,
validateObject,
validateUint32,
} = require('internal/validators');

const {
Expand Down Expand Up @@ -560,7 +559,8 @@ function checkPrime(candidate, options = kEmptyObject, callback) {
checks = 0,
} = options;

validateUint32(checks, 'options.checks');
// The checks option is unsigned but must fit into a signed C int for OpenSSL.
validateInt32(checks, 'options.checks', 0);

const job = new CheckPrimeJob(kCryptoJobAsync, candidate, checks);
job.ondone = callback;
Expand Down Expand Up @@ -588,7 +588,8 @@ function checkPrimeSync(candidate, options = kEmptyObject) {
checks = 0,
} = options;

validateUint32(checks, 'options.checks');
// The checks option is unsigned but must fit into a signed C int for OpenSSL.
validateInt32(checks, 'options.checks', 0);

const job = new CheckPrimeJob(kCryptoJobSync, candidate, checks);
const { 0: err, 1: result } = job.run();
Expand Down
15 changes: 4 additions & 11 deletions src/crypto/crypto_random.cc
Expand Up @@ -15,6 +15,7 @@ using v8::ArrayBuffer;
using v8::BackingStore;
using v8::False;
using v8::FunctionCallbackInfo;
using v8::Int32;
using v8::Just;
using v8::Local;
using v8::Maybe;
Expand Down Expand Up @@ -185,8 +186,6 @@ Maybe<bool> CheckPrimeTraits::AdditionalConfig(
const FunctionCallbackInfo<Value>& args,
unsigned int offset,
CheckPrimeConfig* params) {
Environment* env = Environment::GetCurrent(args);

ArrayBufferOrViewContents<unsigned char> candidate(args[offset]);

params->candidate =
Expand All @@ -195,15 +194,9 @@ Maybe<bool> CheckPrimeTraits::AdditionalConfig(
candidate.size(),
nullptr));

CHECK(args[offset + 1]->IsUint32()); // Checks

const int checks = static_cast<int>(args[offset + 1].As<Uint32>()->Value());
if (checks < 0) {
THROW_ERR_OUT_OF_RANGE(env, "invalid options.checks");
return Nothing<bool>();
}

params->checks = checks;
CHECK(args[offset + 1]->IsInt32()); // Checks
params->checks = args[offset + 1].As<Int32>()->Value();
CHECK_GE(params->checks, 0);

return Just(true);
}
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-crypto-prime.js
Expand Up @@ -240,6 +240,17 @@ for (const checks of ['hello', {}, []]) {
});
}

for (const checks of [-(2 ** 31), -1, 2 ** 31, 2 ** 32 - 1, 2 ** 32, 2 ** 50]) {
assert.throws(() => checkPrime(2n, { checks }, common.mustNotCall()), {
code: 'ERR_OUT_OF_RANGE',
message: /<= 2147483647/
});
assert.throws(() => checkPrimeSync(2n, { checks }), {
code: 'ERR_OUT_OF_RANGE',
message: /<= 2147483647/
});
}

assert(!checkPrimeSync(Buffer.from([0x1])));
assert(checkPrimeSync(Buffer.from([0x2])));
assert(checkPrimeSync(Buffer.from([0x3])));
Expand Down