Skip to content

Commit

Permalink
src: pass along errors from KeyObject instantiation
Browse files Browse the repository at this point in the history
PR-URL: #25734
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
  • Loading branch information
addaleax committed Jan 29, 2019
1 parent 77fa310 commit 6202096
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
35 changes: 18 additions & 17 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3329,15 +3329,18 @@ Local<Function> KeyObject::Initialize(Environment* env, Local<Object> target) {
return function;
}

Local<Object> KeyObject::Create(Environment* env,
KeyType key_type,
const ManagedEVPPKey& pkey) {
MaybeLocal<Object> KeyObject::Create(Environment* env,
KeyType key_type,
const ManagedEVPPKey& pkey) {
CHECK_NE(key_type, kKeyTypeSecret);
Local<Value> type = Integer::New(env->isolate(), key_type);
Local<Object> obj =
env->crypto_key_object_constructor()->NewInstance(env->context(),
1, &type)
.ToLocalChecked();
Local<Object> obj;
if (!env->crypto_key_object_constructor()
->NewInstance(env->context(), 1, &type)
.ToLocal(&obj)) {
return MaybeLocal<Object>();
}

KeyObject* key = Unwrap<KeyObject>(obj);
CHECK(key);
if (key_type == kKeyTypePublic)
Expand Down Expand Up @@ -5825,24 +5828,22 @@ class GenerateKeyPairJob : public CryptoJob {
if (public_key_encoding_.output_key_object_) {
// Note that this has the downside of containing sensitive data of the
// private key.
*pubkey = KeyObject::Create(env, kKeyTypePublic, pkey_);
if (!KeyObject::Create(env, kKeyTypePublic, pkey_).ToLocal(pubkey))
return false;
} else {
MaybeLocal<Value> maybe_pubkey =
WritePublicKey(env, pkey_.get(), public_key_encoding_);
if (maybe_pubkey.IsEmpty())
if (!WritePublicKey(env, pkey_.get(), public_key_encoding_)
.ToLocal(pubkey))
return false;
*pubkey = maybe_pubkey.ToLocalChecked();
}

// Now do the same for the private key.
if (private_key_encoding_.output_key_object_) {
*privkey = KeyObject::Create(env, kKeyTypePrivate, pkey_);
if (!KeyObject::Create(env, kKeyTypePrivate, pkey_).ToLocal(privkey))
return false;
} else {
MaybeLocal<Value> maybe_privkey =
WritePrivateKey(env, pkey_.get(), private_key_encoding_);
if (maybe_privkey.IsEmpty())
if (!WritePrivateKey(env, pkey_.get(), private_key_encoding_)
.ToLocal(privkey))
return false;
*privkey = maybe_privkey.ToLocalChecked();
}

return true;
Expand Down
6 changes: 3 additions & 3 deletions src/node_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,9 @@ class KeyObject : public BaseObject {
static v8::Local<v8::Function> Initialize(Environment* env,
v8::Local<v8::Object> target);

static v8::Local<v8::Object> Create(Environment* env,
KeyType type,
const ManagedEVPPKey& pkey);
static v8::MaybeLocal<v8::Object> Create(Environment* env,
KeyType type,
const ManagedEVPPKey& pkey);

// TODO(tniessen): track the memory used by OpenSSL types
SET_NO_MEMORY_INFO()
Expand Down

0 comments on commit 6202096

Please sign in to comment.