Skip to content

Commit fe1bfa1

Browse files
committed
fixup! src: move more crypto_dh.cc code to ncrypto
1 parent 836a24b commit fe1bfa1

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

src/crypto/crypto_dh.cc

+17-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "crypto/crypto_util.h"
66
#include "env-inl.h"
77
#include "memory_tracker-inl.h"
8+
#include "ncrypto.h"
89
#include "node_errors.h"
910
#include "threadpoolwork-inl.h"
1011
#include "v8.h"
@@ -83,7 +84,12 @@ void New(const FunctionCallbackInfo<Value>& args) {
8384
if (args[0]->IsInt32()) {
8485
int32_t bits = args[0].As<Int32>()->Value();
8586
if (bits < 2) {
86-
return THROW_ERR_OUT_OF_RANGE(env, "Invalid prime length");
87+
#if OPENSSL_VERSION_MAJOR >= 3
88+
ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
89+
#else
90+
ERR_raise(ERR_LIB_BN, BN_R_BITS_TOO_SMALL);
91+
#endif
92+
return ThrowCryptoError(env, ERR_get_error(), "Invalid prime length");
8793
}
8894

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

101108
auto dh = DHPointer::New(bits, generator);
@@ -123,22 +130,26 @@ void New(const FunctionCallbackInfo<Value>& args) {
123130
if (args[1]->IsInt32()) {
124131
int32_t generator = args[1].As<Int32>()->Value();
125132
if (generator < 2) {
126-
return THROW_ERR_OUT_OF_RANGE(env, "Invalid generator");
133+
ERR_raise(ERR_LIB_DH, DH_R_BAD_GENERATOR);
134+
return ThrowCryptoError(env, ERR_get_error(), "Invalid generator");
127135
}
128136
bn_g = BignumPointer::New();
129137
if (!bn_g.setWord(generator)) {
130-
return THROW_ERR_INVALID_ARG_VALUE(env, "Invalid generator");
138+
ERR_raise(ERR_LIB_DH, DH_R_BAD_GENERATOR);
139+
return ThrowCryptoError(env, ERR_get_error(), "Invalid generator");
131140
}
132141
} else {
133142
ArrayBufferOrViewContents<char> arg1(args[1]);
134143
if (UNLIKELY(!arg1.CheckSizeInt32()))
135144
return THROW_ERR_OUT_OF_RANGE(env, "generator is too big");
136145
bn_g = BignumPointer(reinterpret_cast<uint8_t*>(arg1.data()), arg1.size());
137146
if (!bn_g) {
138-
return THROW_ERR_INVALID_ARG_VALUE(env, "Invalid generator");
147+
ERR_raise(ERR_LIB_DH, DH_R_BAD_GENERATOR);
148+
return ThrowCryptoError(env, ERR_get_error(), "Invalid generator");
139149
}
140150
if (bn_g.getWord() < 2) {
141-
return THROW_ERR_OUT_OF_RANGE(env, "Invalid generator");
151+
ERR_raise(ERR_LIB_DH, DH_R_BAD_GENERATOR);
152+
return ThrowCryptoError(env, ERR_get_error(), "Invalid generator");
142153
}
143154
}
144155

test/parallel/test-crypto-dh-errors.js

+18-11
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,26 @@ assert.throws(() => crypto.createDiffieHellman('abcdef', 13.37), {
2424
});
2525

2626
for (const bits of [-1, 0, 1]) {
27-
assert.throws(() => crypto.createDiffieHellman(bits), {
28-
code: 'ERR_OUT_OF_RANGE',
29-
name: 'RangeError',
30-
message: /Invalid prime length/,
31-
});
27+
if (common.hasOpenSSL3) {
28+
assert.throws(() => crypto.createDiffieHellman(bits), {
29+
code: 'ERR_OSSL_DH_MODULUS_TOO_SMALL',
30+
name: 'Error',
31+
message: /modulus too small/,
32+
});
33+
} else {
34+
assert.throws(() => crypto.createDiffieHellman(bits), {
35+
code: 'ERR_OSSL_BN_BITS_TOO_SMALL',
36+
name: 'Error',
37+
message: /bits too small/,
38+
});
39+
}
3240
}
3341

3442
for (const g of [-1, 1]) {
3543
const ex = {
36-
code: 'ERR_OUT_OF_RANGE',
37-
name: 'RangeError',
38-
message: /Invalid generator/,
44+
code: 'ERR_OSSL_DH_BAD_GENERATOR',
45+
name: 'Error',
46+
message: /bad generator/,
3947
};
4048

4149
assert.throws(() => crypto.createDiffieHellman('abcdef', g), ex);
@@ -46,9 +54,8 @@ for (const g of [Buffer.from([]),
4654
Buffer.from([0]),
4755
Buffer.from([1])]) {
4856
const ex = {
49-
code: 'ERR_OUT_OF_RANGE',
50-
name: 'RangeError',
51-
message: /Invalid generator/,
57+
code: 'ERR_OSSL_DH_BAD_GENERATOR',
58+
message: /bad generator/,
5259
};
5360
assert.throws(() => crypto.createDiffieHellman('abcdef', g), ex);
5461
assert.throws(() => crypto.createDiffieHellman('abcdef', 'hex', g), ex);

0 commit comments

Comments
 (0)