Skip to content

Commit 18d02eb

Browse files
committed
crypto: clear OpenSSL error queue after calling X509_check_private_key()
Fixes: #45485
1 parent 405ea2a commit 18d02eb

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/crypto/crypto_x509.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ void X509Certificate::CheckPrivateKey(const FunctionCallbackInfo<Value>& args) {
453453
ASSIGN_OR_RETURN_UNWRAP(&key, args[0]);
454454
CHECK_EQ(key->Data()->GetKeyType(), kKeyTypePrivate);
455455

456+
ClearErrorOnReturn clear_error_on_return;
457+
456458
args.GetReturnValue().Set(
457459
X509_check_private_key(
458460
cert->get(),

test/parallel/test-crypto-x509.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ if (!common.hasCrypto)
88
const {
99
X509Certificate,
1010
createPrivateKey,
11+
generateKeyPairSync,
1112
} = require('crypto');
1213

1314
const {
@@ -186,10 +187,17 @@ const der = Buffer.from(
186187
code: 'ERR_INVALID_ARG_VALUE'
187188
});
188189

189-
// Confirm failure of X509Certificate:verify() doesn't affect other functions that use OpenSSL.
190-
assert(!x509.verify(x509.publicKey));
191-
// This call should not throw.
192-
createPrivateKey(key);
190+
{
191+
// https://github.com/nodejs/node/issues/45377
192+
// https://github.com/nodejs/node/issues/45485
193+
// Confirm failures of X509Certificate:verify() and X509Certificate:CheckPrivateKey()
194+
// do not affect other functions that use OpenSSL.
195+
const keyPair = generateKeyPairSync('ed25519');
196+
assert(!x509.verify(keyPair.publicKey));
197+
assert(!x509.checkPrivateKey(keyPair.privateKey));
198+
// This call should not throw.
199+
createPrivateKey(key);
200+
}
193201

194202
// X509Certificate can be cloned via MessageChannel/MessagePort
195203
const mc = new MessageChannel();

0 commit comments

Comments
 (0)