From a382db596dee29c403008d216779d268e363b799 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Tue, 28 Jul 2026 15:02:45 +0200 Subject: [PATCH] crypto: handle DH operation failures Report DH failures instead of aborting or returning an empty secret. Signed-off-by: Filip Skokan PR-URL: https://github.com/nodejs/node/pull/64851 Reviewed-By: James M Snell Reviewed-By: Luigi Pinca --- src/crypto/crypto_dh.cc | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/crypto/crypto_dh.cc b/src/crypto/crypto_dh.cc index a824e4e0ce9..92780cfeeeb 100644 --- a/src/crypto/crypto_dh.cc +++ b/src/crypto/crypto_dh.cc @@ -337,6 +337,10 @@ void ComputeSecret(const FunctionCallbackInfo& args) { } auto dp = dh.computeSecret(key); + if (!dp) { + return THROW_ERR_CRYPTO_OPERATION_FAILED(env, + "Failed to compute shared secret"); + } Local buffer; if (DataPointerToBuffer(env, std::move(dp)).ToLocal(&buffer)) { @@ -354,8 +358,8 @@ void SetPublicKey(const FunctionCallbackInfo& args) { if (!buf.CheckSizeInt32()) [[unlikely]] return THROW_ERR_OUT_OF_RANGE(env, "buf is too big"); BignumPointer num(buf.data(), buf.size()); - CHECK(num); - CHECK(dh.setPublicKey(std::move(num))); + if (!num || !dh.setPublicKey(std::move(num))) + return THROW_ERR_INVALID_ARG_VALUE(env, "Invalid public key"); } void SetPrivateKey(const FunctionCallbackInfo& args) { @@ -368,8 +372,8 @@ void SetPrivateKey(const FunctionCallbackInfo& args) { if (!buf.CheckSizeInt32()) [[unlikely]] return THROW_ERR_OUT_OF_RANGE(env, "buf is too big"); BignumPointer num(buf.data(), buf.size()); - CHECK(num); - CHECK(dh.setPrivateKey(std::move(num))); + if (!num || !dh.setPrivateKey(std::move(num))) + return THROW_ERR_INVALID_ARG_VALUE(env, "Invalid private key"); } void Check(const FunctionCallbackInfo& args) {