Skip to content

Commit a382db5

Browse files
panvanodejs-github-bot
authored andcommitted
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: #64851 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent a4aa3c0 commit a382db5

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

src/crypto/crypto_dh.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ void ComputeSecret(const FunctionCallbackInfo<Value>& args) {
337337
}
338338

339339
auto dp = dh.computeSecret(key);
340+
if (!dp) {
341+
return THROW_ERR_CRYPTO_OPERATION_FAILED(env,
342+
"Failed to compute shared secret");
343+
}
340344

341345
Local<Value> buffer;
342346
if (DataPointerToBuffer(env, std::move(dp)).ToLocal(&buffer)) {
@@ -354,8 +358,8 @@ void SetPublicKey(const FunctionCallbackInfo<Value>& args) {
354358
if (!buf.CheckSizeInt32()) [[unlikely]]
355359
return THROW_ERR_OUT_OF_RANGE(env, "buf is too big");
356360
BignumPointer num(buf.data(), buf.size());
357-
CHECK(num);
358-
CHECK(dh.setPublicKey(std::move(num)));
361+
if (!num || !dh.setPublicKey(std::move(num)))
362+
return THROW_ERR_INVALID_ARG_VALUE(env, "Invalid public key");
359363
}
360364

361365
void SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
@@ -368,8 +372,8 @@ void SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
368372
if (!buf.CheckSizeInt32()) [[unlikely]]
369373
return THROW_ERR_OUT_OF_RANGE(env, "buf is too big");
370374
BignumPointer num(buf.data(), buf.size());
371-
CHECK(num);
372-
CHECK(dh.setPrivateKey(std::move(num)));
375+
if (!num || !dh.setPrivateKey(std::move(num)))
376+
return THROW_ERR_INVALID_ARG_VALUE(env, "Invalid private key");
373377
}
374378

375379
void Check(const FunctionCallbackInfo<Value>& args) {

0 commit comments

Comments
 (0)