Skip to content
Merged
Changes from 1 commit
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
Next Next commit
crypto: fix handling of null BUF_MEM* in ToV8Value()
The assignment to `bptr` calls `BIO_get_mem_ptr` which can fail and
leave the `bptr` as nullptr. This then later causes a null pointer
deref.
This is inconsistent with uses of the similar function
`BIO_get_mem_data` that do check its return value, e.g.
`node::crypto::X509sToArrayOfStrings()`.
Solve it by checking for a null pointer and handling the `Nothing`
return value at the call sites.
  • Loading branch information
ndossche committed Feb 19, 2026
commit 2bd18e52dc58bb8ea9679362b9beb3f9ed2d888a
15 changes: 13 additions & 2 deletions src/crypto/crypto_keys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ MaybeLocal<Value> ToV8Value(
const EVPKeyPointer::AsymmetricKeyEncodingConfig& config) {
if (!bio) return {};
BUF_MEM* bptr = bio;
if (!bptr) return {};
if (config.format == EVPKeyPointer::PKFormatType::PEM) {
// PEM is an ASCII format, so we will return it as a string.
return String::NewFromUtf8(
Expand All @@ -106,7 +107,12 @@ MaybeLocal<Value> WritePrivateKey(
const EVPKeyPointer::PrivateKeyEncodingConfig& config) {
if (!pkey) return {};
auto res = pkey.writePrivateKey(config);
if (res) return ToV8Value(env, std::move(res.value), config);
if (res) {
auto value = ToV8Value(env, std::move(res.value), config);
if (!value.IsEmpty()) {
return value;
}
}

ThrowCryptoError(
env, res.openssl_error.value_or(0), "Failed to encode private key");
Expand All @@ -119,7 +125,12 @@ MaybeLocal<Value> WritePublicKey(
const EVPKeyPointer::PublicKeyEncodingConfig& config) {
if (!pkey) return {};
auto res = pkey.writePublicKey(config);
if (res) return ToV8Value(env, res.value, config);
if (res) {
auto value = ToV8Value(env, res.value, config);
if (!value.IsEmpty()) {
return value;
}
}

ThrowCryptoError(
env, res.openssl_error.value_or(0), "Failed to encode public key");
Expand Down