Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

Commit

Permalink
crypto: don't crash X509ToObject on error
Browse files Browse the repository at this point in the history
Use MaybeLocal::ToLocal and don't crash X509ToObject on error.

PR-URL: nodejs/node#25717
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
davidben authored and deepak1556 committed Jul 10, 2019
1 parent c2ac649 commit 4f63360
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1556,24 +1556,27 @@ static void AddFingerprintDigest(const unsigned char* md,
}
}


static MaybeLocal<Object> ECPointToBuffer(Environment* env,
const EC_GROUP* group,
const EC_POINT* point,
point_conversion_form_t form) {
point_conversion_form_t form,
const char** error) {
size_t len = EC_POINT_point2oct(group, point, form, nullptr, 0, nullptr);
if (len == 0) {
env->ThrowError("Failed to get public key length");
if (error != nullptr) *error = "Failed to get public key length";
return MaybeLocal<Object>();
}
MallocedBuffer<unsigned char> buf(len);
len = EC_POINT_point2oct(group, point, form, buf.data, buf.size, nullptr);
if (len == 0) {
env->ThrowError("Failed to get public key");
if (error != nullptr) *error = "Failed to get public key";
return MaybeLocal<Object>();
}
return Buffer::New(env, buf.release(), len);
}


static Local<Object> X509ToObject(Environment* env, X509* cert) {
EscapableHandleScope scope(env->isolate());
Local<Context> context = env->context();
Expand Down Expand Up @@ -4474,6 +4477,7 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
ECDH* ecdh;
ASSIGN_OR_RETURN_UNWRAP(&ecdh, args.Holder());

const EC_GROUP* group = EC_KEY_get0_group(ecdh->key_.get());
const EC_POINT* pub = EC_KEY_get0_public_key(ecdh->key_.get());
if (pub == nullptr)
return env->ThrowError("Failed to get ECDH public key");
Expand All @@ -4482,10 +4486,11 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
uint32_t val = args[0].As<Uint32>()->Value();
point_conversion_form_t form = static_cast<point_conversion_form_t>(val);

MaybeLocal<Object> buf =
ECPointToBuffer(env, EC_KEY_get0_group(ecdh->key_.get()), pub, form);
if (buf.IsEmpty()) return;
args.GetReturnValue().Set(buf.ToLocalChecked());
const char* error;
Local<Object> buf;
if (!ECPointToBuffer(env, group, pub, form, &error).ToLocal(&buf))
return env->ThrowError(error);
args.GetReturnValue().Set(buf);
}


Expand Down Expand Up @@ -5088,9 +5093,11 @@ void ConvertKey(const FunctionCallbackInfo<Value>& args) {
uint32_t val = args[2].As<Uint32>()->Value();
point_conversion_form_t form = static_cast<point_conversion_form_t>(val);

MaybeLocal<Object> buf = ECPointToBuffer(env, group.get(), pub.get(), form);
if (buf.IsEmpty()) return;
args.GetReturnValue().Set(buf.ToLocalChecked());
const char* error;
Local<Object> buf;
if (!ECPointToBuffer(env, group.get(), pub.get(), form, &error).ToLocal(&buf))
return env->ThrowError(error);
args.GetReturnValue().Set(buf);
}


Expand Down

0 comments on commit 4f63360

Please sign in to comment.