crypto: handle DH operation failures

Report DH failures instead of aborting or returning an empty secret.

Signed-off-by: Filip Skokan <panva.ip@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/64851
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Filip Skokan authored and Node.js GitHub Bot committed 2026-08-01 20:34:26 +00:00
1 parent a4aa3c02cc
commit a382db596d
1 file changed
+8 -4
+8 -4
View File
@@ -337,6 +337,10 @@ void ComputeSecret(const FunctionCallbackInfo<Value>& args) {
}
auto dp = dh.computeSecret(key);
if (!dp) {
return THROW_ERR_CRYPTO_OPERATION_FAILED(env,
"Failed to compute shared secret");
}
Local<Value> buffer;
if (DataPointerToBuffer(env, std::move(dp)).ToLocal(&buffer)) {
@@ -354,8 +358,8 @@ void SetPublicKey(const FunctionCallbackInfo<Value>& 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<Value>& args) {
@@ -368,8 +372,8 @@ void SetPrivateKey(const FunctionCallbackInfo<Value>& 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<Value>& args) {