diff --git a/src/crypto/crypto_dh.cc b/src/crypto/crypto_dh.cc index 3a7dc70c1eee91..ddf3c0725fd856 100644 --- a/src/crypto/crypto_dh.cc +++ b/src/crypto/crypto_dh.cc @@ -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" @@ -83,7 +84,12 @@ void New(const FunctionCallbackInfo& args) { if (args[0]->IsInt32()) { int32_t bits = args[0].As()->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 @@ -95,7 +101,8 @@ void New(const FunctionCallbackInfo& args) { } int32_t generator = args[1].As()->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); @@ -123,11 +130,13 @@ void New(const FunctionCallbackInfo& args) { if (args[1]->IsInt32()) { int32_t generator = args[1].As()->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 arg1(args[1]); @@ -135,10 +144,12 @@ void New(const FunctionCallbackInfo& args) { return THROW_ERR_OUT_OF_RANGE(env, "generator is too big"); bn_g = BignumPointer(reinterpret_cast(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"); } } diff --git a/test/parallel/test-crypto-dh-errors.js b/test/parallel/test-crypto-dh-errors.js index dda09167e6f09e..bbe70f42a9365e 100644 --- a/test/parallel/test-crypto-dh-errors.js +++ b/test/parallel/test-crypto-dh-errors.js @@ -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); @@ -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);