Skip to content
Closed
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
src: pass along errors from KeyObject instantiation
  • Loading branch information
addaleax committed Jan 27, 2019
commit 6a68457e2b8510db25e8b862620f649c5e0bbb2b
35 changes: 18 additions & 17 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3327,15 +3327,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 @@ -5823,24 +5826,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