Skip to content

Commit 1516f7f

Browse files
panvaaduh95
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 ba1ef78 commit 1516f7f

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
@@ -332,6 +332,10 @@ void ComputeSecret(const FunctionCallbackInfo<Value>& args) {
332332
}
333333

334334
auto dp = dh.computeSecret(key);
335+
if (!dp) {
336+
return THROW_ERR_CRYPTO_OPERATION_FAILED(env,
337+
"Failed to compute shared secret");
338+
}
335339

336340
Local<Value> buffer;
337341
if (DataPointerToBuffer(env, std::move(dp)).ToLocal(&buffer)) {
@@ -349,8 +353,8 @@ void SetPublicKey(const FunctionCallbackInfo<Value>& args) {
349353
if (!buf.CheckSizeInt32()) [[unlikely]]
350354
return THROW_ERR_OUT_OF_RANGE(env, "buf is too big");
351355
BignumPointer num(buf.data(), buf.size());
352-
CHECK(num);
353-
CHECK(dh.setPublicKey(std::move(num)));
356+
if (!num || !dh.setPublicKey(std::move(num)))
357+
return THROW_ERR_INVALID_ARG_VALUE(env, "Invalid public key");
354358
}
355359

356360
void SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
@@ -363,8 +367,8 @@ void SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
363367
if (!buf.CheckSizeInt32()) [[unlikely]]
364368
return THROW_ERR_OUT_OF_RANGE(env, "buf is too big");
365369
BignumPointer num(buf.data(), buf.size());
366-
CHECK(num);
367-
CHECK(dh.setPrivateKey(std::move(num)));
370+
if (!num || !dh.setPrivateKey(std::move(num)))
371+
return THROW_ERR_INVALID_ARG_VALUE(env, "Invalid private key");
368372
}
369373

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

0 commit comments

Comments
 (0)