Skip to content

Commit

Permalink
fixup! src: move more crypto_dh.cc code to ncrypto
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Aug 20, 2024
1 parent 836a24b commit fe1bfa1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
23 changes: 17 additions & 6 deletions src/crypto/crypto_dh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "crypto/crypto_util.h"
#include "env-inl.h"
#include "memory_tracker-inl.h"
#include "ncrypto.h"
#include "node_errors.h"
#include "threadpoolwork-inl.h"
#include "v8.h"
Expand Down Expand Up @@ -83,7 +84,12 @@ void New(const FunctionCallbackInfo<Value>& args) {
if (args[0]->IsInt32()) {
int32_t bits = args[0].As<Int32>()->Value();
if (bits < 2) {
return THROW_ERR_OUT_OF_RANGE(env, "Invalid prime length");
#if OPENSSL_VERSION_MAJOR >= 3
ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
#else
ERR_raise(ERR_LIB_BN, BN_R_BITS_TOO_SMALL);
#endif
return ThrowCryptoError(env, ERR_get_error(), "Invalid prime length");
}

// If the first argument is an Int32 then we are generating a new
Expand All @@ -95,7 +101,8 @@ void New(const FunctionCallbackInfo<Value>& args) {
}
int32_t generator = args[1].As<Int32>()->Value();
if (generator < 2) {
return THROW_ERR_OUT_OF_RANGE(env, "Invalid generator");
ERR_raise(ERR_LIB_DH, DH_R_BAD_GENERATOR);
return ThrowCryptoError(env, ERR_get_error(), "Invalid generator");
}

auto dh = DHPointer::New(bits, generator);
Expand Down Expand Up @@ -123,22 +130,26 @@ void New(const FunctionCallbackInfo<Value>& args) {
if (args[1]->IsInt32()) {
int32_t generator = args[1].As<Int32>()->Value();
if (generator < 2) {
return THROW_ERR_OUT_OF_RANGE(env, "Invalid generator");
ERR_raise(ERR_LIB_DH, DH_R_BAD_GENERATOR);
return ThrowCryptoError(env, ERR_get_error(), "Invalid generator");
}
bn_g = BignumPointer::New();
if (!bn_g.setWord(generator)) {
return THROW_ERR_INVALID_ARG_VALUE(env, "Invalid generator");
ERR_raise(ERR_LIB_DH, DH_R_BAD_GENERATOR);
return ThrowCryptoError(env, ERR_get_error(), "Invalid generator");
}
} else {
ArrayBufferOrViewContents<char> arg1(args[1]);
if (UNLIKELY(!arg1.CheckSizeInt32()))
return THROW_ERR_OUT_OF_RANGE(env, "generator is too big");
bn_g = BignumPointer(reinterpret_cast<uint8_t*>(arg1.data()), arg1.size());
if (!bn_g) {
return THROW_ERR_INVALID_ARG_VALUE(env, "Invalid generator");
ERR_raise(ERR_LIB_DH, DH_R_BAD_GENERATOR);
return ThrowCryptoError(env, ERR_get_error(), "Invalid generator");
}
if (bn_g.getWord() < 2) {
return THROW_ERR_OUT_OF_RANGE(env, "Invalid generator");
ERR_raise(ERR_LIB_DH, DH_R_BAD_GENERATOR);
return ThrowCryptoError(env, ERR_get_error(), "Invalid generator");
}
}

Expand Down
29 changes: 18 additions & 11 deletions test/parallel/test-crypto-dh-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,26 @@ assert.throws(() => crypto.createDiffieHellman('abcdef', 13.37), {
});

for (const bits of [-1, 0, 1]) {
assert.throws(() => crypto.createDiffieHellman(bits), {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: /Invalid prime length/,
});
if (common.hasOpenSSL3) {
assert.throws(() => crypto.createDiffieHellman(bits), {
code: 'ERR_OSSL_DH_MODULUS_TOO_SMALL',
name: 'Error',
message: /modulus too small/,
});
} else {
assert.throws(() => crypto.createDiffieHellman(bits), {
code: 'ERR_OSSL_BN_BITS_TOO_SMALL',
name: 'Error',
message: /bits too small/,
});
}
}

for (const g of [-1, 1]) {
const ex = {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: /Invalid generator/,
code: 'ERR_OSSL_DH_BAD_GENERATOR',
name: 'Error',
message: /bad generator/,
};

assert.throws(() => crypto.createDiffieHellman('abcdef', g), ex);
Expand All @@ -46,9 +54,8 @@ for (const g of [Buffer.from([]),
Buffer.from([0]),
Buffer.from([1])]) {
const ex = {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: /Invalid generator/,
code: 'ERR_OSSL_DH_BAD_GENERATOR',
message: /bad generator/,
};
assert.throws(() => crypto.createDiffieHellman('abcdef', g), ex);
assert.throws(() => crypto.createDiffieHellman('abcdef', 'hex', g), ex);
Expand Down

0 comments on commit fe1bfa1

Please sign in to comment.