Skip to content

Commit 6202096

Browse files
committed
src: pass along errors from KeyObject instantiation
PR-URL: #25734 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
1 parent 77fa310 commit 6202096

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

src/node_crypto.cc

+18-17
Original file line numberDiff line numberDiff line change
@@ -3329,15 +3329,18 @@ Local<Function> KeyObject::Initialize(Environment* env, Local<Object> target) {
33293329
return function;
33303330
}
33313331

3332-
Local<Object> KeyObject::Create(Environment* env,
3333-
KeyType key_type,
3334-
const ManagedEVPPKey& pkey) {
3332+
MaybeLocal<Object> KeyObject::Create(Environment* env,
3333+
KeyType key_type,
3334+
const ManagedEVPPKey& pkey) {
33353335
CHECK_NE(key_type, kKeyTypeSecret);
33363336
Local<Value> type = Integer::New(env->isolate(), key_type);
3337-
Local<Object> obj =
3338-
env->crypto_key_object_constructor()->NewInstance(env->context(),
3339-
1, &type)
3340-
.ToLocalChecked();
3337+
Local<Object> obj;
3338+
if (!env->crypto_key_object_constructor()
3339+
->NewInstance(env->context(), 1, &type)
3340+
.ToLocal(&obj)) {
3341+
return MaybeLocal<Object>();
3342+
}
3343+
33413344
KeyObject* key = Unwrap<KeyObject>(obj);
33423345
CHECK(key);
33433346
if (key_type == kKeyTypePublic)
@@ -5825,24 +5828,22 @@ class GenerateKeyPairJob : public CryptoJob {
58255828
if (public_key_encoding_.output_key_object_) {
58265829
// Note that this has the downside of containing sensitive data of the
58275830
// private key.
5828-
*pubkey = KeyObject::Create(env, kKeyTypePublic, pkey_);
5831+
if (!KeyObject::Create(env, kKeyTypePublic, pkey_).ToLocal(pubkey))
5832+
return false;
58295833
} else {
5830-
MaybeLocal<Value> maybe_pubkey =
5831-
WritePublicKey(env, pkey_.get(), public_key_encoding_);
5832-
if (maybe_pubkey.IsEmpty())
5834+
if (!WritePublicKey(env, pkey_.get(), public_key_encoding_)
5835+
.ToLocal(pubkey))
58335836
return false;
5834-
*pubkey = maybe_pubkey.ToLocalChecked();
58355837
}
58365838

58375839
// Now do the same for the private key.
58385840
if (private_key_encoding_.output_key_object_) {
5839-
*privkey = KeyObject::Create(env, kKeyTypePrivate, pkey_);
5841+
if (!KeyObject::Create(env, kKeyTypePrivate, pkey_).ToLocal(privkey))
5842+
return false;
58405843
} else {
5841-
MaybeLocal<Value> maybe_privkey =
5842-
WritePrivateKey(env, pkey_.get(), private_key_encoding_);
5843-
if (maybe_privkey.IsEmpty())
5844+
if (!WritePrivateKey(env, pkey_.get(), private_key_encoding_)
5845+
.ToLocal(privkey))
58445846
return false;
5845-
*privkey = maybe_privkey.ToLocalChecked();
58465847
}
58475848

58485849
return true;

src/node_crypto.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ class KeyObject : public BaseObject {
442442
static v8::Local<v8::Function> Initialize(Environment* env,
443443
v8::Local<v8::Object> target);
444444

445-
static v8::Local<v8::Object> Create(Environment* env,
446-
KeyType type,
447-
const ManagedEVPPKey& pkey);
445+
static v8::MaybeLocal<v8::Object> Create(Environment* env,
446+
KeyType type,
447+
const ManagedEVPPKey& pkey);
448448

449449
// TODO(tniessen): track the memory used by OpenSSL types
450450
SET_NO_MEMORY_INFO()

0 commit comments

Comments
 (0)