Skip to content

Commit

Permalink
src: replace more toLocalCheckeds in crypto_*
Browse files Browse the repository at this point in the history
Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: #35509
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
jasnell committed Oct 13, 2020
1 parent 095be6a commit 17d5d94
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 46 deletions.
6 changes: 3 additions & 3 deletions src/crypto/crypto_cipher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ void CipherBase::GetAuthTag(const FunctionCallbackInfo<Value>& args) {

args.GetReturnValue().Set(
Buffer::Copy(env, cipher->auth_tag_, cipher->auth_tag_len_)
.ToLocalChecked());
.FromMaybe(Local<Value>()));
}

void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
Expand Down Expand Up @@ -784,7 +784,7 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
}

CHECK(out.data() != nullptr || out.size() == 0);
args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
});
}

Expand Down Expand Up @@ -875,7 +875,7 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
return ThrowCryptoError(env, ERR_get_error(), msg);
}

args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
}

template <PublicKeyCipher::Operation operation,
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ MaybeLocal<Value> GetPeerCert(
// First and main certificate.
X509Pointer first_cert(sk_X509_value(peer_certs.get(), 0));
CHECK(first_cert);
maybe_cert = X509ToObject(env, first_cert.release()).ToLocalChecked();
maybe_cert = X509ToObject(env, first_cert.release());
if (!maybe_cert.ToLocal(&result))
return MaybeLocal<Value>();

Expand Down
8 changes: 4 additions & 4 deletions src/crypto/crypto_dh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
CHECK_EQ(size,
BN_bn2binpad(
pub_key, reinterpret_cast<unsigned char*>(data.data()), size));
args.GetReturnValue().Set(data.ToBuffer().ToLocalChecked());
args.GetReturnValue().Set(data.ToBuffer().FromMaybe(Local<Value>()));
}


Expand All @@ -275,7 +275,7 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
CHECK_EQ(
size,
BN_bn2binpad(num, reinterpret_cast<unsigned char*>(data.data()), size));
args.GetReturnValue().Set(data.ToBuffer().ToLocalChecked());
args.GetReturnValue().Set(data.ToBuffer().FromMaybe(Local<Value>()));
}

void DiffieHellman::GetPrime(const FunctionCallbackInfo<Value>& args) {
Expand Down Expand Up @@ -357,7 +357,7 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(size, 0);
ZeroPadDiffieHellmanSecret(static_cast<size_t>(size), &ret);

args.GetReturnValue().Set(ret.ToBuffer().ToLocalChecked());
args.GetReturnValue().Set(ret.ToBuffer().FromMaybe(Local<Value>()));
}

void DiffieHellman::SetKey(const FunctionCallbackInfo<Value>& args,
Expand Down Expand Up @@ -613,7 +613,7 @@ void DiffieHellman::Stateless(const FunctionCallbackInfo<Value>& args) {
if (out.size() == 0)
return ThrowCryptoError(env, ERR_get_error(), "diffieHellman failed");

args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
}

Maybe<bool> DHBitsTraits::AdditionalConfig(
Expand Down
6 changes: 2 additions & 4 deletions src/crypto/crypto_ecdh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
if (!r)
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to compute ECDH key");

Local<Object> buf = out.ToBuffer().ToLocalChecked();
args.GetReturnValue().Set(buf);
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
}

void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
Expand Down Expand Up @@ -244,8 +243,7 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
reinterpret_cast<unsigned char*>(out.data()),
size));

Local<Object> buf = out.ToBuffer().ToLocalChecked();
args.GetReturnValue().Set(buf);
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
}

void ECDH::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {
env->isolate()->ThrowException(error);
return;
}
args.GetReturnValue().Set(rc.ToLocalChecked());
args.GetReturnValue().Set(rc.FromMaybe(Local<Value>()));
}

HashConfig::HashConfig(HashConfig&& other) noexcept
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_hmac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {
env->isolate()->ThrowException(error);
return;
}
args.GetReturnValue().Set(rc.ToLocalChecked());
args.GetReturnValue().Set(rc.FromMaybe(Local<Value>()));
}

HmacConfig::HmacConfig(HmacConfig&& other) noexcept
Expand Down
33 changes: 21 additions & 12 deletions src/crypto/crypto_keys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -266,20 +266,22 @@ ParseKeyResult ParsePrivateKey(EVPKeyPointer* pkey,
return ParseKeyResult::kParseKeyFailed;
}

Local<Value> BIOToStringOrBuffer(Environment* env,
BIO* bio,
PKFormatType format) {
MaybeLocal<Value> BIOToStringOrBuffer(
Environment* env,
BIO* bio,
PKFormatType format) {
BUF_MEM* bptr;
BIO_get_mem_ptr(bio, &bptr);
if (format == kKeyFormatPEM) {
// PEM is an ASCII format, so we will return it as a string.
return String::NewFromUtf8(env->isolate(), bptr->data,
NewStringType::kNormal,
bptr->length).ToLocalChecked();
bptr->length).FromMaybe(Local<Value>());
} else {
CHECK_EQ(format, kKeyFormatDER);
// DER is binary, return it as a buffer.
return Buffer::Copy(env, bptr->data, bptr->length).ToLocalChecked();
return Buffer::Copy(env, bptr->data, bptr->length)
.FromMaybe(Local<Value>());
}
}

Expand Down Expand Up @@ -1108,13 +1110,13 @@ void KeyObjectHandle::Export(const FunctionCallbackInfo<Value>& args) {
}

if (!result.IsEmpty())
args.GetReturnValue().Set(result.ToLocalChecked());
args.GetReturnValue().Set(result.FromMaybe(Local<Value>()));
}

Local<Value> KeyObjectHandle::ExportSecretKey() const {
MaybeLocal<Value> KeyObjectHandle::ExportSecretKey() const {
const char* buf = data_->GetSymmetricKey();
unsigned int len = data_->GetSymmetricKeySize();
return Buffer::Copy(env(), buf, len).ToLocalChecked();
return Buffer::Copy(env(), buf, len).FromMaybe(Local<Value>());
}

MaybeLocal<Value> KeyObjectHandle::ExportPublicKey(
Expand Down Expand Up @@ -1183,7 +1185,9 @@ void NativeKeyObject::CreateNativeKeyObjectClass(
KeyObjectHandle::kInternalFieldCount);
t->Inherit(BaseObject::GetConstructorTemplate(env));

Local<Value> ctor = t->GetFunction(env->context()).ToLocalChecked();
Local<Value> ctor;
if (!t->GetFunction(env->context()).ToLocal(&ctor))
return;

Local<Value> recv = Undefined(env->isolate());
Local<Value> ret_v;
Expand All @@ -1210,7 +1214,10 @@ BaseObjectPtr<BaseObject> NativeKeyObject::KeyObjectTransferData::Deserialize(
return {};
}

Local<Value> handle = KeyObjectHandle::Create(env, data_).ToLocalChecked();
Local<Value> handle;
if (!KeyObjectHandle::Create(env, data_).ToLocal(&handle))
return {};

Local<Function> key_ctor;
Local<Value> arg = FIXED_ONE_BYTE_STRING(env->isolate(),
"internal/crypto/keys");
Expand All @@ -1232,8 +1239,10 @@ BaseObjectPtr<BaseObject> NativeKeyObject::KeyObjectTransferData::Deserialize(
CHECK(false);
}

Local<Value> key =
key_ctor->NewInstance(context, 1, &handle).ToLocalChecked();
Local<Value> key;
if (!key_ctor->NewInstance(context, 1, &handle).ToLocal(&key))
return {};

return BaseObjectPtr<BaseObject>(Unwrap<KeyObjectHandle>(key.As<Object>()));
}

Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class KeyObjectHandle : public BaseObject {

static void Export(const v8::FunctionCallbackInfo<v8::Value>& args);

v8::Local<v8::Value> ExportSecretKey() const;
v8::MaybeLocal<v8::Value> ExportSecretKey() const;
v8::MaybeLocal<v8::Value> ExportPublicKey(
const PublicKeyEncodingConfig& config) const;
v8::MaybeLocal<v8::Value> ExportPrivateKey(
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/crypto_sig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {
if (ret.error != kSignOk)
return crypto::CheckThrow(env, ret.error);

args.GetReturnValue().Set(ret.signature.ToBuffer().ToLocalChecked());
args.GetReturnValue().Set(ret.signature.ToBuffer().FromMaybe(Local<Value>()));
}

Verify::Verify(Environment* env, Local<Object> wrap)
Expand Down Expand Up @@ -581,7 +581,7 @@ void Sign::SignSync(const FunctionCallbackInfo<Value>& args) {
signature = ConvertSignatureToP1363(env, key, std::move(signature));
}

args.GetReturnValue().Set(signature.ToBuffer().ToLocalChecked());
args.GetReturnValue().Set(signature.ToBuffer().FromMaybe(Local<Value>()));
}

void Verify::VerifySync(const FunctionCallbackInfo<Value>& args) {
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_spkac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
if (pkey.data() == nullptr)
return args.GetReturnValue().SetEmptyString();

args.GetReturnValue().Set(pkey.ToBuffer().ToLocalChecked());
args.GetReturnValue().Set(pkey.ToBuffer().FromMaybe(Local<Value>()));
}

OpenSSLBuffer ExportChallenge(const ArrayBufferOrViewContents<char>& input) {
Expand Down
37 changes: 21 additions & 16 deletions src/crypto/crypto_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,14 @@ MaybeLocal<Value> CryptoErrorVector::ToException(
if (copy.empty()) copy.push_back("no error"); // But possibly a bug...
// Use last element as the error message, everything else goes
// into the .opensslErrorStack property on the exception object.
auto exception_string =
String::NewFromUtf8(env->isolate(), copy.back().data(),
NewStringType::kNormal, copy.back().size())
.ToLocalChecked();
Local<String> exception_string;
if (!String::NewFromUtf8(
env->isolate(),
copy.back().data(),
NewStringType::kNormal,
copy.back().size()).ToLocal(&exception_string)) {
return MaybeLocal<Value>();
}
copy.pop_back();
return copy.ToException(env, exception_string);
}
Expand All @@ -192,11 +196,12 @@ MaybeLocal<Value> CryptoErrorVector::ToException(
if (!empty()) {
CHECK(exception_v->IsObject());
Local<Object> exception = exception_v.As<Object>();
Maybe<bool> ok = exception->Set(env->context(),
env->openssl_error_stack(),
ToV8Value(env->context(), *this).ToLocalChecked());
if (ok.IsNothing())
Local<Value> stack;
if (!ToV8Value(env->context(), *this).ToLocal(&stack) ||
exception->Set(env->context(), env->openssl_error_stack(), stack)
.IsNothing()) {
return MaybeLocal<Value>();
}
}

return exception_v;
Expand Down Expand Up @@ -470,18 +475,18 @@ void ThrowCryptoError(Environment* env,
message = message_buffer;
}
HandleScope scope(env->isolate());
Local<String> exception_string =
String::NewFromUtf8(env->isolate(), message).ToLocalChecked();
CryptoErrorVector errors;
errors.Capture();
Local<String> exception_string;
Local<Value> exception;
if (!errors.ToException(env, exception_string).ToLocal(&exception))
return;
Local<Object> obj;
if (!exception->ToObject(env->context()).ToLocal(&obj))
if (!String::NewFromUtf8(env->isolate(), message).ToLocal(&exception_string))
return;
if (error::Decorate(env, obj, err).IsNothing())
CryptoErrorVector errors;
errors.Capture();
if (!errors.ToException(env, exception_string).ToLocal(&exception) ||
!exception->ToObject(env->context()).ToLocal(&obj) ||
error::Decorate(env, obj, err).IsNothing()) {
return;
}
env->isolate()->ThrowException(exception);
}

Expand Down

0 comments on commit 17d5d94

Please sign in to comment.