Skip to content

Commit 17d5d94

Browse files
committed
src: replace more toLocalCheckeds in crypto_*
Signed-off-by: James M Snell <[email protected]> PR-URL: #35509 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 095be6a commit 17d5d94

11 files changed

+58
-46
lines changed

Diff for: src/crypto/crypto_cipher.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ void CipherBase::GetAuthTag(const FunctionCallbackInfo<Value>& args) {
594594

595595
args.GetReturnValue().Set(
596596
Buffer::Copy(env, cipher->auth_tag_, cipher->auth_tag_len_)
597-
.ToLocalChecked());
597+
.FromMaybe(Local<Value>()));
598598
}
599599

600600
void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
@@ -784,7 +784,7 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
784784
}
785785

786786
CHECK(out.data() != nullptr || out.size() == 0);
787-
args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
787+
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
788788
});
789789
}
790790

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

878-
args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
878+
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
879879
}
880880

881881
template <PublicKeyCipher::Operation operation,

Diff for: src/crypto/crypto_common.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ MaybeLocal<Value> GetPeerCert(
931931
// First and main certificate.
932932
X509Pointer first_cert(sk_X509_value(peer_certs.get(), 0));
933933
CHECK(first_cert);
934-
maybe_cert = X509ToObject(env, first_cert.release()).ToLocalChecked();
934+
maybe_cert = X509ToObject(env, first_cert.release());
935935
if (!maybe_cert.ToLocal(&result))
936936
return MaybeLocal<Value>();
937937

Diff for: src/crypto/crypto_dh.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
253253
CHECK_EQ(size,
254254
BN_bn2binpad(
255255
pub_key, reinterpret_cast<unsigned char*>(data.data()), size));
256-
args.GetReturnValue().Set(data.ToBuffer().ToLocalChecked());
256+
args.GetReturnValue().Set(data.ToBuffer().FromMaybe(Local<Value>()));
257257
}
258258

259259

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

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

360-
args.GetReturnValue().Set(ret.ToBuffer().ToLocalChecked());
360+
args.GetReturnValue().Set(ret.ToBuffer().FromMaybe(Local<Value>()));
361361
}
362362

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

616-
args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
616+
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
617617
}
618618

619619
Maybe<bool> DHBitsTraits::AdditionalConfig(

Diff for: src/crypto/crypto_ecdh.cc

+2-4
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
197197
if (!r)
198198
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to compute ECDH key");
199199

200-
Local<Object> buf = out.ToBuffer().ToLocalChecked();
201-
args.GetReturnValue().Set(buf);
200+
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
202201
}
203202

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

247-
Local<Object> buf = out.ToBuffer().ToLocalChecked();
248-
args.GetReturnValue().Set(buf);
246+
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
249247
}
250248

251249
void ECDH::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {

Diff for: src/crypto/crypto_hash.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {
198198
env->isolate()->ThrowException(error);
199199
return;
200200
}
201-
args.GetReturnValue().Set(rc.ToLocalChecked());
201+
args.GetReturnValue().Set(rc.FromMaybe(Local<Value>()));
202202
}
203203

204204
HashConfig::HashConfig(HashConfig&& other) noexcept

Diff for: src/crypto/crypto_hmac.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {
134134
env->isolate()->ThrowException(error);
135135
return;
136136
}
137-
args.GetReturnValue().Set(rc.ToLocalChecked());
137+
args.GetReturnValue().Set(rc.FromMaybe(Local<Value>()));
138138
}
139139

140140
HmacConfig::HmacConfig(HmacConfig&& other) noexcept

Diff for: src/crypto/crypto_keys.cc

+21-12
Original file line numberDiff line numberDiff line change
@@ -266,20 +266,22 @@ ParseKeyResult ParsePrivateKey(EVPKeyPointer* pkey,
266266
return ParseKeyResult::kParseKeyFailed;
267267
}
268268

269-
Local<Value> BIOToStringOrBuffer(Environment* env,
270-
BIO* bio,
271-
PKFormatType format) {
269+
MaybeLocal<Value> BIOToStringOrBuffer(
270+
Environment* env,
271+
BIO* bio,
272+
PKFormatType format) {
272273
BUF_MEM* bptr;
273274
BIO_get_mem_ptr(bio, &bptr);
274275
if (format == kKeyFormatPEM) {
275276
// PEM is an ASCII format, so we will return it as a string.
276277
return String::NewFromUtf8(env->isolate(), bptr->data,
277278
NewStringType::kNormal,
278-
bptr->length).ToLocalChecked();
279+
bptr->length).FromMaybe(Local<Value>());
279280
} else {
280281
CHECK_EQ(format, kKeyFormatDER);
281282
// DER is binary, return it as a buffer.
282-
return Buffer::Copy(env, bptr->data, bptr->length).ToLocalChecked();
283+
return Buffer::Copy(env, bptr->data, bptr->length)
284+
.FromMaybe(Local<Value>());
283285
}
284286
}
285287

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

11101112
if (!result.IsEmpty())
1111-
args.GetReturnValue().Set(result.ToLocalChecked());
1113+
args.GetReturnValue().Set(result.FromMaybe(Local<Value>()));
11121114
}
11131115

1114-
Local<Value> KeyObjectHandle::ExportSecretKey() const {
1116+
MaybeLocal<Value> KeyObjectHandle::ExportSecretKey() const {
11151117
const char* buf = data_->GetSymmetricKey();
11161118
unsigned int len = data_->GetSymmetricKeySize();
1117-
return Buffer::Copy(env(), buf, len).ToLocalChecked();
1119+
return Buffer::Copy(env(), buf, len).FromMaybe(Local<Value>());
11181120
}
11191121

11201122
MaybeLocal<Value> KeyObjectHandle::ExportPublicKey(
@@ -1183,7 +1185,9 @@ void NativeKeyObject::CreateNativeKeyObjectClass(
11831185
KeyObjectHandle::kInternalFieldCount);
11841186
t->Inherit(BaseObject::GetConstructorTemplate(env));
11851187

1186-
Local<Value> ctor = t->GetFunction(env->context()).ToLocalChecked();
1188+
Local<Value> ctor;
1189+
if (!t->GetFunction(env->context()).ToLocal(&ctor))
1190+
return;
11871191

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

1213-
Local<Value> handle = KeyObjectHandle::Create(env, data_).ToLocalChecked();
1217+
Local<Value> handle;
1218+
if (!KeyObjectHandle::Create(env, data_).ToLocal(&handle))
1219+
return {};
1220+
12141221
Local<Function> key_ctor;
12151222
Local<Value> arg = FIXED_ONE_BYTE_STRING(env->isolate(),
12161223
"internal/crypto/keys");
@@ -1232,8 +1239,10 @@ BaseObjectPtr<BaseObject> NativeKeyObject::KeyObjectTransferData::Deserialize(
12321239
CHECK(false);
12331240
}
12341241

1235-
Local<Value> key =
1236-
key_ctor->NewInstance(context, 1, &handle).ToLocalChecked();
1242+
Local<Value> key;
1243+
if (!key_ctor->NewInstance(context, 1, &handle).ToLocal(&key))
1244+
return {};
1245+
12371246
return BaseObjectPtr<BaseObject>(Unwrap<KeyObjectHandle>(key.As<Object>()));
12381247
}
12391248

Diff for: src/crypto/crypto_keys.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class KeyObjectHandle : public BaseObject {
196196

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

199-
v8::Local<v8::Value> ExportSecretKey() const;
199+
v8::MaybeLocal<v8::Value> ExportSecretKey() const;
200200
v8::MaybeLocal<v8::Value> ExportPublicKey(
201201
const PublicKeyEncodingConfig& config) const;
202202
v8::MaybeLocal<v8::Value> ExportPrivateKey(

Diff for: src/crypto/crypto_sig.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {
377377
if (ret.error != kSignOk)
378378
return crypto::CheckThrow(env, ret.error);
379379

380-
args.GetReturnValue().Set(ret.signature.ToBuffer().ToLocalChecked());
380+
args.GetReturnValue().Set(ret.signature.ToBuffer().FromMaybe(Local<Value>()));
381381
}
382382

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

584-
args.GetReturnValue().Set(signature.ToBuffer().ToLocalChecked());
584+
args.GetReturnValue().Set(signature.ToBuffer().FromMaybe(Local<Value>()));
585585
}
586586

587587
void Verify::VerifySync(const FunctionCallbackInfo<Value>& args) {

Diff for: src/crypto/crypto_spkac.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
8282
if (pkey.data() == nullptr)
8383
return args.GetReturnValue().SetEmptyString();
8484

85-
args.GetReturnValue().Set(pkey.ToBuffer().ToLocalChecked());
85+
args.GetReturnValue().Set(pkey.ToBuffer().FromMaybe(Local<Value>()));
8686
}
8787

8888
OpenSSLBuffer ExportChallenge(const ArrayBufferOrViewContents<char>& input) {

Diff for: src/crypto/crypto_util.cc

+21-16
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,14 @@ MaybeLocal<Value> CryptoErrorVector::ToException(
178178
if (copy.empty()) copy.push_back("no error"); // But possibly a bug...
179179
// Use last element as the error message, everything else goes
180180
// into the .opensslErrorStack property on the exception object.
181-
auto exception_string =
182-
String::NewFromUtf8(env->isolate(), copy.back().data(),
183-
NewStringType::kNormal, copy.back().size())
184-
.ToLocalChecked();
181+
Local<String> exception_string;
182+
if (!String::NewFromUtf8(
183+
env->isolate(),
184+
copy.back().data(),
185+
NewStringType::kNormal,
186+
copy.back().size()).ToLocal(&exception_string)) {
187+
return MaybeLocal<Value>();
188+
}
185189
copy.pop_back();
186190
return copy.ToException(env, exception_string);
187191
}
@@ -192,11 +196,12 @@ MaybeLocal<Value> CryptoErrorVector::ToException(
192196
if (!empty()) {
193197
CHECK(exception_v->IsObject());
194198
Local<Object> exception = exception_v.As<Object>();
195-
Maybe<bool> ok = exception->Set(env->context(),
196-
env->openssl_error_stack(),
197-
ToV8Value(env->context(), *this).ToLocalChecked());
198-
if (ok.IsNothing())
199+
Local<Value> stack;
200+
if (!ToV8Value(env->context(), *this).ToLocal(&stack) ||
201+
exception->Set(env->context(), env->openssl_error_stack(), stack)
202+
.IsNothing()) {
199203
return MaybeLocal<Value>();
204+
}
200205
}
201206

202207
return exception_v;
@@ -470,18 +475,18 @@ void ThrowCryptoError(Environment* env,
470475
message = message_buffer;
471476
}
472477
HandleScope scope(env->isolate());
473-
Local<String> exception_string =
474-
String::NewFromUtf8(env->isolate(), message).ToLocalChecked();
475-
CryptoErrorVector errors;
476-
errors.Capture();
478+
Local<String> exception_string;
477479
Local<Value> exception;
478-
if (!errors.ToException(env, exception_string).ToLocal(&exception))
479-
return;
480480
Local<Object> obj;
481-
if (!exception->ToObject(env->context()).ToLocal(&obj))
481+
if (!String::NewFromUtf8(env->isolate(), message).ToLocal(&exception_string))
482482
return;
483-
if (error::Decorate(env, obj, err).IsNothing())
483+
CryptoErrorVector errors;
484+
errors.Capture();
485+
if (!errors.ToException(env, exception_string).ToLocal(&exception) ||
486+
!exception->ToObject(env->context()).ToLocal(&obj) ||
487+
error::Decorate(env, obj, err).IsNothing()) {
484488
return;
489+
}
485490
env->isolate()->ThrowException(exception);
486491
}
487492

0 commit comments

Comments
 (0)