Permalink
Browse files

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>
  • Loading branch information...
addaleax authored and targos committed Jan 27, 2019
1 parent ced4e71 commit 8ee481002924eab69c79d9e575f8c0cef9d5db3e
Showing with 21 additions and 20 deletions.
  1. +18 −17 src/node_crypto.cc
  2. +3 −3 src/node_crypto.h
@@ -3302,15 +3302,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)
@@ -5798,24 +5801,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;
@@ -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()

0 comments on commit 8ee4810

Please sign in to comment.