Skip to content

Commit 43d8124

Browse files
danbevMayaLekova
authored andcommitted
crypto: use non-deprecated v8::Object::Set
This commit updates node_crypto to use the non-deprecated Set functions that return a v8::Maybe<bool>. PR-URL: nodejs#17482 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent ff3ac1f commit 43d8124

File tree

1 file changed

+86
-55
lines changed

1 file changed

+86
-55
lines changed

src/node_crypto.cc

Lines changed: 86 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,26 +1724,31 @@ void SSLWrap<Base>::OnClientHello(void* arg,
17241724
Base* w = static_cast<Base*>(arg);
17251725
Environment* env = w->ssl_env();
17261726
HandleScope handle_scope(env->isolate());
1727-
Context::Scope context_scope(env->context());
1727+
Local<Context> context = env->context();
1728+
Context::Scope context_scope(context);
17281729

17291730
Local<Object> hello_obj = Object::New(env->isolate());
17301731
Local<Object> buff = Buffer::Copy(
17311732
env,
17321733
reinterpret_cast<const char*>(hello.session_id()),
17331734
hello.session_size()).ToLocalChecked();
1734-
hello_obj->Set(env->session_id_string(), buff);
1735+
hello_obj->Set(context, env->session_id_string(), buff).FromJust();
17351736
if (hello.servername() == nullptr) {
1736-
hello_obj->Set(env->servername_string(), String::Empty(env->isolate()));
1737+
hello_obj->Set(context,
1738+
env->servername_string(),
1739+
String::Empty(env->isolate())).FromJust();
17371740
} else {
17381741
Local<String> servername = OneByteString(env->isolate(),
17391742
hello.servername(),
17401743
hello.servername_size());
1741-
hello_obj->Set(env->servername_string(), servername);
1744+
hello_obj->Set(context, env->servername_string(), servername).FromJust();
17421745
}
1743-
hello_obj->Set(env->tls_ticket_string(),
1744-
Boolean::New(env->isolate(), hello.has_ticket()));
1745-
hello_obj->Set(env->ocsp_request_string(),
1746-
Boolean::New(env->isolate(), hello.ocsp_request()));
1746+
hello_obj->Set(context,
1747+
env->tls_ticket_string(),
1748+
Boolean::New(env->isolate(), hello.has_ticket())).FromJust();
1749+
hello_obj->Set(context,
1750+
env->ocsp_request_string(),
1751+
Boolean::New(env->isolate(), hello.ocsp_request())).FromJust();
17471752

17481753
Local<Value> argv[] = { hello_obj };
17491754
w->MakeCallback(env->onclienthello_string(), arraysize(argv), argv);
@@ -1788,7 +1793,7 @@ static bool SafeX509ExtPrint(BIO* out, X509_EXTENSION* ext) {
17881793

17891794
static Local<Object> X509ToObject(Environment* env, X509* cert) {
17901795
EscapableHandleScope scope(env->isolate());
1791-
1796+
Local<Context> context = env->context();
17921797
Local<Object> info = Object::New(env->isolate());
17931798

17941799
BIO* bio = BIO_new(BIO_s_mem());
@@ -1798,18 +1803,20 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
17981803
0,
17991804
X509_NAME_FLAGS) > 0) {
18001805
BIO_get_mem_ptr(bio, &mem);
1801-
info->Set(env->subject_string(),
1806+
info->Set(context, env->subject_string(),
18021807
String::NewFromUtf8(env->isolate(), mem->data,
1803-
String::kNormalString, mem->length));
1808+
String::kNormalString,
1809+
mem->length)).FromJust();
18041810
}
18051811
USE(BIO_reset(bio));
18061812

18071813
X509_NAME* issuer_name = X509_get_issuer_name(cert);
18081814
if (X509_NAME_print_ex(bio, issuer_name, 0, X509_NAME_FLAGS) > 0) {
18091815
BIO_get_mem_ptr(bio, &mem);
1810-
info->Set(env->issuer_string(),
1816+
info->Set(context, env->issuer_string(),
18111817
String::NewFromUtf8(env->isolate(), mem->data,
1812-
String::kNormalString, mem->length));
1818+
String::kNormalString,
1819+
mem->length)).FromJust();
18131820
}
18141821
USE(BIO_reset(bio));
18151822

@@ -1834,9 +1841,10 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
18341841
}
18351842

18361843
BIO_get_mem_ptr(bio, &mem);
1837-
info->Set(keys[i],
1844+
info->Set(context, keys[i],
18381845
String::NewFromUtf8(env->isolate(), mem->data,
1839-
String::kNormalString, mem->length));
1846+
String::kNormalString,
1847+
mem->length)).FromJust();
18401848

18411849
USE(BIO_reset(bio));
18421850
}
@@ -1852,9 +1860,10 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
18521860
RSA_get0_key(rsa, &n, &e, nullptr);
18531861
BN_print(bio, n);
18541862
BIO_get_mem_ptr(bio, &mem);
1855-
info->Set(env->modulus_string(),
1863+
info->Set(context, env->modulus_string(),
18561864
String::NewFromUtf8(env->isolate(), mem->data,
1857-
String::kNormalString, mem->length));
1865+
String::kNormalString,
1866+
mem->length)).FromJust();
18581867
USE(BIO_reset(bio));
18591868

18601869
uint64_t exponent_word = static_cast<uint64_t>(BN_get_word(e));
@@ -1866,9 +1875,10 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
18661875
BIO_printf(bio, "0x%x%08x", hi, lo);
18671876
}
18681877
BIO_get_mem_ptr(bio, &mem);
1869-
info->Set(env->exponent_string(),
1878+
info->Set(context, env->exponent_string(),
18701879
String::NewFromUtf8(env->isolate(), mem->data,
1871-
String::kNormalString, mem->length));
1880+
String::kNormalString,
1881+
mem->length)).FromJust();
18721882
USE(BIO_reset(bio));
18731883
}
18741884

@@ -1883,16 +1893,18 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
18831893

18841894
ASN1_TIME_print(bio, X509_get_notBefore(cert));
18851895
BIO_get_mem_ptr(bio, &mem);
1886-
info->Set(env->valid_from_string(),
1896+
info->Set(context, env->valid_from_string(),
18871897
String::NewFromUtf8(env->isolate(), mem->data,
1888-
String::kNormalString, mem->length));
1898+
String::kNormalString,
1899+
mem->length)).FromJust();
18891900
USE(BIO_reset(bio));
18901901

18911902
ASN1_TIME_print(bio, X509_get_notAfter(cert));
18921903
BIO_get_mem_ptr(bio, &mem);
1893-
info->Set(env->valid_to_string(),
1904+
info->Set(context, env->valid_to_string(),
18941905
String::NewFromUtf8(env->isolate(), mem->data,
1895-
String::kNormalString, mem->length));
1906+
String::kNormalString,
1907+
mem->length)).FromJust();
18961908
BIO_free_all(bio);
18971909

18981910
unsigned int md_size, i;
@@ -1913,8 +1925,8 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
19131925
fingerprint[0] = '\0';
19141926
}
19151927

1916-
info->Set(env->fingerprint_string(),
1917-
OneByteString(env->isolate(), fingerprint));
1928+
info->Set(context, env->fingerprint_string(),
1929+
OneByteString(env->isolate(), fingerprint)).FromJust();
19181930
}
19191931

19201932
STACK_OF(ASN1_OBJECT)* eku = static_cast<STACK_OF(ASN1_OBJECT)*>(
@@ -1926,18 +1938,20 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
19261938
int j = 0;
19271939
for (int i = 0; i < sk_ASN1_OBJECT_num(eku); i++) {
19281940
if (OBJ_obj2txt(buf, sizeof(buf), sk_ASN1_OBJECT_value(eku, i), 1) >= 0)
1929-
ext_key_usage->Set(j++, OneByteString(env->isolate(), buf));
1941+
ext_key_usage->Set(context,
1942+
j++,
1943+
OneByteString(env->isolate(), buf)).FromJust();
19301944
}
19311945

19321946
sk_ASN1_OBJECT_pop_free(eku, ASN1_OBJECT_free);
1933-
info->Set(env->ext_key_usage_string(), ext_key_usage);
1947+
info->Set(context, env->ext_key_usage_string(), ext_key_usage).FromJust();
19341948
}
19351949

19361950
if (ASN1_INTEGER* serial_number = X509_get_serialNumber(cert)) {
19371951
if (BIGNUM* bn = ASN1_INTEGER_to_BN(serial_number, nullptr)) {
19381952
if (char* buf = BN_bn2hex(bn)) {
1939-
info->Set(env->serial_number_string(),
1940-
OneByteString(env->isolate(), buf));
1953+
info->Set(context, env->serial_number_string(),
1954+
OneByteString(env->isolate(), buf)).FromJust();
19411955
OPENSSL_free(buf);
19421956
}
19431957
BN_free(bn);
@@ -1950,7 +1964,7 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
19501964
unsigned char* serialized = reinterpret_cast<unsigned char*>(
19511965
Buffer::Data(buff));
19521966
i2d_X509(cert, &serialized);
1953-
info->Set(env->raw_string(), buff);
1967+
info->Set(context, env->raw_string(), buff).FromJust();
19541968

19551969
return scope.Escape(info);
19561970
}
@@ -1963,6 +1977,7 @@ void SSLWrap<Base>::GetPeerCertificate(
19631977
Base* w;
19641978
ASSIGN_OR_RETURN_UNWRAP(&w, args.Holder());
19651979
Environment* env = w->ssl_env();
1980+
Local<Context> context = env->context();
19661981

19671982
ClearErrorOnReturn clear_error_on_return;
19681983

@@ -2014,7 +2029,7 @@ void SSLWrap<Base>::GetPeerCertificate(
20142029
continue;
20152030

20162031
Local<Object> ca_info = X509ToObject(env, ca);
2017-
info->Set(env->issuercert_string(), ca_info);
2032+
info->Set(context, env->issuercert_string(), ca_info).FromJust();
20182033
info = ca_info;
20192034

20202035
// NOTE: Intentionally freeing cert that is not used anymore
@@ -2037,7 +2052,7 @@ void SSLWrap<Base>::GetPeerCertificate(
20372052
break;
20382053

20392054
Local<Object> ca_info = X509ToObject(env, ca);
2040-
info->Set(env->issuercert_string(), ca_info);
2055+
info->Set(context, env->issuercert_string(), ca_info).FromJust();
20412056
info = ca_info;
20422057

20432058
// NOTE: Intentionally freeing cert that is not used anymore
@@ -2049,7 +2064,7 @@ void SSLWrap<Base>::GetPeerCertificate(
20492064

20502065
// Self-issued certificate
20512066
if (X509_check_issued(cert, cert) == X509_V_OK)
2052-
info->Set(env->issuercert_string(), info);
2067+
info->Set(context, env->issuercert_string(), info).FromJust();
20532068

20542069
CHECK_NE(cert, nullptr);
20552070

@@ -2245,6 +2260,7 @@ void SSLWrap<Base>::GetEphemeralKeyInfo(
22452260
Base* w;
22462261
ASSIGN_OR_RETURN_UNWRAP(&w, args.Holder());
22472262
Environment* env = Environment::GetCurrent(args);
2263+
Local<Context> context = env->context();
22482264

22492265
CHECK_NE(w->ssl_, nullptr);
22502266

@@ -2259,22 +2275,24 @@ void SSLWrap<Base>::GetEphemeralKeyInfo(
22592275
if (SSL_get_server_tmp_key(w->ssl_, &key)) {
22602276
switch (EVP_PKEY_id(key)) {
22612277
case EVP_PKEY_DH:
2262-
info->Set(env->type_string(),
2263-
FIXED_ONE_BYTE_STRING(env->isolate(), "DH"));
2264-
info->Set(env->size_string(),
2265-
Integer::New(env->isolate(), EVP_PKEY_bits(key)));
2278+
info->Set(context, env->type_string(),
2279+
FIXED_ONE_BYTE_STRING(env->isolate(), "DH")).FromJust();
2280+
info->Set(context, env->size_string(),
2281+
Integer::New(env->isolate(), EVP_PKEY_bits(key))).FromJust();
22662282
break;
22672283
case EVP_PKEY_EC:
22682284
{
22692285
EC_KEY* ec = EVP_PKEY_get1_EC_KEY(key);
22702286
int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
22712287
EC_KEY_free(ec);
2272-
info->Set(env->type_string(),
2273-
FIXED_ONE_BYTE_STRING(env->isolate(), "ECDH"));
2274-
info->Set(env->name_string(),
2275-
OneByteString(args.GetIsolate(), OBJ_nid2sn(nid)));
2276-
info->Set(env->size_string(),
2277-
Integer::New(env->isolate(), EVP_PKEY_bits(key)));
2288+
info->Set(context, env->type_string(),
2289+
FIXED_ONE_BYTE_STRING(env->isolate(), "ECDH")).FromJust();
2290+
info->Set(context, env->name_string(),
2291+
OneByteString(args.GetIsolate(),
2292+
OBJ_nid2sn(nid))).FromJust();
2293+
info->Set(context, env->size_string(),
2294+
Integer::New(env->isolate(),
2295+
EVP_PKEY_bits(key))).FromJust();
22782296
}
22792297
}
22802298
EVP_PKEY_free(key);
@@ -2367,7 +2385,8 @@ void SSLWrap<Base>::VerifyError(const FunctionCallbackInfo<Value>& args) {
23672385
Local<String> reason_string = OneByteString(isolate, reason);
23682386
Local<Value> exception_value = Exception::Error(reason_string);
23692387
Local<Object> exception_object = exception_value->ToObject(isolate);
2370-
exception_object->Set(w->env()->code_string(), OneByteString(isolate, code));
2388+
exception_object->Set(w->env()->context(), w->env()->code_string(),
2389+
OneByteString(isolate, code)).FromJust();
23712390
args.GetReturnValue().Set(exception_object);
23722391
}
23732392

@@ -2377,16 +2396,18 @@ void SSLWrap<Base>::GetCurrentCipher(const FunctionCallbackInfo<Value>& args) {
23772396
Base* w;
23782397
ASSIGN_OR_RETURN_UNWRAP(&w, args.Holder());
23792398
Environment* env = w->ssl_env();
2399+
Local<Context> context = env->context();
23802400

23812401
const SSL_CIPHER* c = SSL_get_current_cipher(w->ssl_);
23822402
if (c == nullptr)
23832403
return;
23842404

23852405
Local<Object> info = Object::New(env->isolate());
23862406
const char* cipher_name = SSL_CIPHER_get_name(c);
2387-
info->Set(env->name_string(), OneByteString(args.GetIsolate(), cipher_name));
2388-
info->Set(env->version_string(),
2389-
OneByteString(args.GetIsolate(), "TLSv1/SSLv3"));
2407+
info->Set(context, env->name_string(),
2408+
OneByteString(args.GetIsolate(), cipher_name)).FromJust();
2409+
info->Set(context, env->version_string(),
2410+
OneByteString(args.GetIsolate(), "TLSv1/SSLv3")).FromJust();
23902411
args.GetReturnValue().Set(info);
23912412
}
23922413

@@ -2695,27 +2716,31 @@ int SSLWrap<Base>::SSLCertCallback(SSL* s, void* arg) {
26952716
return -1;
26962717

26972718
Environment* env = w->env();
2719+
Local<Context> context = env->context();
26982720
HandleScope handle_scope(env->isolate());
2699-
Context::Scope context_scope(env->context());
2721+
Context::Scope context_scope(context);
27002722
w->cert_cb_running_ = true;
27012723

27022724
Local<Object> info = Object::New(env->isolate());
27032725

27042726
const char* servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
27052727
if (servername == nullptr) {
2706-
info->Set(env->servername_string(), String::Empty(env->isolate()));
2728+
info->Set(context,
2729+
env->servername_string(),
2730+
String::Empty(env->isolate())).FromJust();
27072731
} else {
27082732
Local<String> str = OneByteString(env->isolate(), servername,
27092733
strlen(servername));
2710-
info->Set(env->servername_string(), str);
2734+
info->Set(context, env->servername_string(), str).FromJust();
27112735
}
27122736

27132737
bool ocsp = false;
27142738
#ifdef NODE__HAVE_TLSEXT_STATUS_CB
27152739
ocsp = SSL_get_tlsext_status_type(s) == TLSEXT_STATUSTYPE_ocsp;
27162740
#endif
27172741

2718-
info->Set(env->ocsp_request_string(), Boolean::New(env->isolate(), ocsp));
2742+
info->Set(context, env->ocsp_request_string(),
2743+
Boolean::New(env->isolate(), ocsp)).FromJust();
27192744

27202745
Local<Value> argv[] = { info };
27212746
w->MakeCallback(env->oncertcb_string(), arraysize(argv), argv);
@@ -4997,7 +5022,7 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
49975022
keylen));
49985023

49995024
if (args[5]->IsFunction()) {
5000-
obj->Set(env->ondone_string(), args[5]);
5025+
obj->Set(env->context(), env->ondone_string(), args[5]).FromJust();
50015026

50025027
uv_queue_work(env->event_loop(),
50035028
req.release()->work_req(),
@@ -5185,7 +5210,7 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
51855210
RandomBytesRequest::FREE_DATA));
51865211

51875212
if (args[1]->IsFunction()) {
5188-
obj->Set(env->ondone_string(), args[1]);
5213+
obj->Set(env->context(), env->ondone_string(), args[1]).FromJust();
51895214

51905215
uv_queue_work(env->event_loop(),
51915216
req.release()->work_req(),
@@ -5254,7 +5279,10 @@ void GetSSLCiphers(const FunctionCallbackInfo<Value>& args) {
52545279

52555280
for (int i = 0; i < sk_SSL_CIPHER_num(ciphers); ++i) {
52565281
const SSL_CIPHER* cipher = sk_SSL_CIPHER_value(ciphers, i);
5257-
arr->Set(i, OneByteString(args.GetIsolate(), SSL_CIPHER_get_name(cipher)));
5282+
arr->Set(env->context(),
5283+
i,
5284+
OneByteString(args.GetIsolate(),
5285+
SSL_CIPHER_get_name(cipher))).FromJust();
52585286
}
52595287

52605288
SSL_free(ssl);
@@ -5317,7 +5345,10 @@ void GetCurves(const FunctionCallbackInfo<Value>& args) {
53175345

53185346
if (EC_get_builtin_curves(curves, num_curves)) {
53195347
for (size_t i = 0; i < num_curves; i++) {
5320-
arr->Set(i, OneByteString(env->isolate(), OBJ_nid2sn(curves[i].nid)));
5348+
arr->Set(env->context(),
5349+
i,
5350+
OneByteString(env->isolate(),
5351+
OBJ_nid2sn(curves[i].nid))).FromJust();
53215352
}
53225353
}
53235354

0 commit comments

Comments
 (0)