Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions deps/ncrypto/ncrypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1710,6 +1710,13 @@ DataPointer DHPointer::getPrivateKey() const {
return BignumPointer::Encode(pvt_key);
}

bool DHPointer::hasPrivateKey() const {
if (!dh_) return false;
const BIGNUM* pvt_key = nullptr;
DH_get0_key(dh_.get(), nullptr, &pvt_key);
return pvt_key != nullptr;
}

DataPointer DHPointer::generateKeys() const {
ClearErrorOnReturn clearErrorOnReturn;
if (!dh_) return {};
Expand Down
1 change: 1 addition & 0 deletions deps/ncrypto/ncrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,7 @@ class DHPointer final {
DataPointer getGenerator() const;
DataPointer getPublicKey() const;
DataPointer getPrivateKey() const;
bool hasPrivateKey() const;
DataPointer generateKeys() const;
DataPointer computeSecret(const BignumPointer& peer) const;

Expand Down
5 changes: 5 additions & 0 deletions src/crypto/crypto_dh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ void ComputeSecret(const FunctionCallbackInfo<Value>& args) {
break;
}

if (!dh.hasPrivateKey()) {
return THROW_ERR_CRYPTO_INVALID_STATE(
env, "Cannot compute shared secret without a private key");
}

auto dp = dh.computeSecret(key);

Local<Value> buffer;
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-crypto-dh-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ assert.throws(
'failed to throw the expected error.'
);

{
const group = crypto.getDiffieHellman('modp14');
const alice = crypto.createDiffieHellman(
group.getPrime(), group.getGenerator());
const bob = crypto.createDiffieHellman(
group.getPrime(), group.getGenerator());
bob.generateKeys();

assert.throws(
() => alice.computeSecret(bob.getPublicKey()),
{
name: 'Error',
code: 'ERR_CRYPTO_INVALID_STATE',
message: 'Cannot compute shared secret without a private key'
});

alice.generateKeys();
assert.deepStrictEqual(
alice.computeSecret(bob.getPublicKey()),
bob.computeSecret(alice.getPublicKey()));
}

assert.throws(
() => crypto.createDiffieHellman('', true),
{
Expand Down
Loading