Skip to content

Commit 2bd18e5

Browse files
committed
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.
1 parent 4d1557a commit 2bd18e5

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/crypto/crypto_keys.cc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ MaybeLocal<Value> ToV8Value(
8888
const EVPKeyPointer::AsymmetricKeyEncodingConfig& config) {
8989
if (!bio) return {};
9090
BUF_MEM* bptr = bio;
91+
if (!bptr) return {};
9192
if (config.format == EVPKeyPointer::PKFormatType::PEM) {
9293
// PEM is an ASCII format, so we will return it as a string.
9394
return String::NewFromUtf8(
@@ -106,7 +107,12 @@ MaybeLocal<Value> WritePrivateKey(
106107
const EVPKeyPointer::PrivateKeyEncodingConfig& config) {
107108
if (!pkey) return {};
108109
auto res = pkey.writePrivateKey(config);
109-
if (res) return ToV8Value(env, std::move(res.value), config);
110+
if (res) {
111+
auto value = ToV8Value(env, std::move(res.value), config);
112+
if (!value.IsEmpty()) {
113+
return value;
114+
}
115+
}
110116

111117
ThrowCryptoError(
112118
env, res.openssl_error.value_or(0), "Failed to encode private key");
@@ -119,7 +125,12 @@ MaybeLocal<Value> WritePublicKey(
119125
const EVPKeyPointer::PublicKeyEncodingConfig& config) {
120126
if (!pkey) return {};
121127
auto res = pkey.writePublicKey(config);
122-
if (res) return ToV8Value(env, res.value, config);
128+
if (res) {
129+
auto value = ToV8Value(env, res.value, config);
130+
if (!value.IsEmpty()) {
131+
return value;
132+
}
133+
}
123134

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

0 commit comments

Comments
 (0)