Skip to content

Commit 4f3bf04

Browse files
danbevrvagg
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>. Backport-PR-URL: #20931 PR-URL: #17482 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 2cf2635 commit 4f3bf04

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
@@ -1630,26 +1630,31 @@ void SSLWrap<Base>::OnClientHello(void* arg,
16301630
Base* w = static_cast<Base*>(arg);
16311631
Environment* env = w->ssl_env();
16321632
HandleScope handle_scope(env->isolate());
1633-
Context::Scope context_scope(env->context());
1633+
Local<Context> context = env->context();
1634+
Context::Scope context_scope(context);
16341635

16351636
Local<Object> hello_obj = Object::New(env->isolate());
16361637
Local<Object> buff = Buffer::Copy(
16371638
env,
16381639
reinterpret_cast<const char*>(hello.session_id()),
16391640
hello.session_size()).ToLocalChecked();
1640-
hello_obj->Set(env->session_id_string(), buff);
1641+
hello_obj->Set(context, env->session_id_string(), buff).FromJust();
16411642
if (hello.servername() == nullptr) {
1642-
hello_obj->Set(env->servername_string(), String::Empty(env->isolate()));
1643+
hello_obj->Set(context,
1644+
env->servername_string(),
1645+
String::Empty(env->isolate())).FromJust();
16431646
} else {
16441647
Local<String> servername = OneByteString(env->isolate(),
16451648
hello.servername(),
16461649
hello.servername_size());
1647-
hello_obj->Set(env->servername_string(), servername);
1650+
hello_obj->Set(context, env->servername_string(), servername).FromJust();
16481651
}
1649-
hello_obj->Set(env->tls_ticket_string(),
1650-
Boolean::New(env->isolate(), hello.has_ticket()));
1651-
hello_obj->Set(env->ocsp_request_string(),
1652-
Boolean::New(env->isolate(), hello.ocsp_request()));
1652+
hello_obj->Set(context,
1653+
env->tls_ticket_string(),
1654+
Boolean::New(env->isolate(), hello.has_ticket())).FromJust();
1655+
hello_obj->Set(context,
1656+
env->ocsp_request_string(),
1657+
Boolean::New(env->isolate(), hello.ocsp_request())).FromJust();
16531658

16541659
Local<Value> argv[] = { hello_obj };
16551660
w->MakeCallback(env->onclienthello_string(), arraysize(argv), argv);
@@ -1694,7 +1699,7 @@ static bool SafeX509ExtPrint(BIO* out, X509_EXTENSION* ext) {
16941699

16951700
static Local<Object> X509ToObject(Environment* env, X509* cert) {
16961701
EscapableHandleScope scope(env->isolate());
1697-
1702+
Local<Context> context = env->context();
16981703
Local<Object> info = Object::New(env->isolate());
16991704

17001705
BIO* bio = BIO_new(BIO_s_mem());
@@ -1704,18 +1709,20 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
17041709
0,
17051710
X509_NAME_FLAGS) > 0) {
17061711
BIO_get_mem_ptr(bio, &mem);
1707-
info->Set(env->subject_string(),
1712+
info->Set(context, env->subject_string(),
17081713
String::NewFromUtf8(env->isolate(), mem->data,
1709-
String::kNormalString, mem->length));
1714+
String::kNormalString,
1715+
mem->length)).FromJust();
17101716
}
17111717
(void) BIO_reset(bio);
17121718

17131719
X509_NAME* issuer_name = X509_get_issuer_name(cert);
17141720
if (X509_NAME_print_ex(bio, issuer_name, 0, X509_NAME_FLAGS) > 0) {
17151721
BIO_get_mem_ptr(bio, &mem);
1716-
info->Set(env->issuer_string(),
1722+
info->Set(context, env->issuer_string(),
17171723
String::NewFromUtf8(env->isolate(), mem->data,
1718-
String::kNormalString, mem->length));
1724+
String::kNormalString,
1725+
mem->length)).FromJust();
17191726
}
17201727
(void) BIO_reset(bio);
17211728

@@ -1740,9 +1747,10 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
17401747
}
17411748

17421749
BIO_get_mem_ptr(bio, &mem);
1743-
info->Set(keys[i],
1750+
info->Set(context, keys[i],
17441751
String::NewFromUtf8(env->isolate(), mem->data,
1745-
String::kNormalString, mem->length));
1752+
String::kNormalString,
1753+
mem->length)).FromJust();
17461754

17471755
(void) BIO_reset(bio);
17481756
}
@@ -1758,9 +1766,10 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
17581766
RSA_get0_key(rsa, &n, &e, nullptr);
17591767
BN_print(bio, n);
17601768
BIO_get_mem_ptr(bio, &mem);
1761-
info->Set(env->modulus_string(),
1769+
info->Set(context, env->modulus_string(),
17621770
String::NewFromUtf8(env->isolate(), mem->data,
1763-
String::kNormalString, mem->length));
1771+
String::kNormalString,
1772+
mem->length)).FromJust();
17641773
(void) BIO_reset(bio);
17651774

17661775
uint64_t exponent_word = static_cast<uint64_t>(BN_get_word(e));
@@ -1772,9 +1781,10 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
17721781
BIO_printf(bio, "0x%x%08x", hi, lo);
17731782
}
17741783
BIO_get_mem_ptr(bio, &mem);
1775-
info->Set(env->exponent_string(),
1784+
info->Set(context, env->exponent_string(),
17761785
String::NewFromUtf8(env->isolate(), mem->data,
1777-
String::kNormalString, mem->length));
1786+
String::kNormalString,
1787+
mem->length)).FromJust();
17781788
(void) BIO_reset(bio);
17791789
}
17801790

@@ -1789,16 +1799,18 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
17891799

17901800
ASN1_TIME_print(bio, X509_get_notBefore(cert));
17911801
BIO_get_mem_ptr(bio, &mem);
1792-
info->Set(env->valid_from_string(),
1802+
info->Set(context, env->valid_from_string(),
17931803
String::NewFromUtf8(env->isolate(), mem->data,
1794-
String::kNormalString, mem->length));
1804+
String::kNormalString,
1805+
mem->length)).FromJust();
17951806
(void) BIO_reset(bio);
17961807

17971808
ASN1_TIME_print(bio, X509_get_notAfter(cert));
17981809
BIO_get_mem_ptr(bio, &mem);
1799-
info->Set(env->valid_to_string(),
1810+
info->Set(context, env->valid_to_string(),
18001811
String::NewFromUtf8(env->isolate(), mem->data,
1801-
String::kNormalString, mem->length));
1812+
String::kNormalString,
1813+
mem->length)).FromJust();
18021814
BIO_free_all(bio);
18031815

18041816
unsigned int md_size, i;
@@ -1819,8 +1831,8 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
18191831
fingerprint[0] = '\0';
18201832
}
18211833

1822-
info->Set(env->fingerprint_string(),
1823-
OneByteString(env->isolate(), fingerprint));
1834+
info->Set(context, env->fingerprint_string(),
1835+
OneByteString(env->isolate(), fingerprint)).FromJust();
18241836
}
18251837

18261838
STACK_OF(ASN1_OBJECT)* eku = static_cast<STACK_OF(ASN1_OBJECT)*>(
@@ -1832,18 +1844,20 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
18321844
int j = 0;
18331845
for (int i = 0; i < sk_ASN1_OBJECT_num(eku); i++) {
18341846
if (OBJ_obj2txt(buf, sizeof(buf), sk_ASN1_OBJECT_value(eku, i), 1) >= 0)
1835-
ext_key_usage->Set(j++, OneByteString(env->isolate(), buf));
1847+
ext_key_usage->Set(context,
1848+
j++,
1849+
OneByteString(env->isolate(), buf)).FromJust();
18361850
}
18371851

18381852
sk_ASN1_OBJECT_pop_free(eku, ASN1_OBJECT_free);
1839-
info->Set(env->ext_key_usage_string(), ext_key_usage);
1853+
info->Set(context, env->ext_key_usage_string(), ext_key_usage).FromJust();
18401854
}
18411855

18421856
if (ASN1_INTEGER* serial_number = X509_get_serialNumber(cert)) {
18431857
if (BIGNUM* bn = ASN1_INTEGER_to_BN(serial_number, nullptr)) {
18441858
if (char* buf = BN_bn2hex(bn)) {
1845-
info->Set(env->serial_number_string(),
1846-
OneByteString(env->isolate(), buf));
1859+
info->Set(context, env->serial_number_string(),
1860+
OneByteString(env->isolate(), buf)).FromJust();
18471861
OPENSSL_free(buf);
18481862
}
18491863
BN_free(bn);
@@ -1856,7 +1870,7 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
18561870
unsigned char* serialized = reinterpret_cast<unsigned char*>(
18571871
Buffer::Data(buff));
18581872
i2d_X509(cert, &serialized);
1859-
info->Set(env->raw_string(), buff);
1873+
info->Set(context, env->raw_string(), buff).FromJust();
18601874

18611875
return scope.Escape(info);
18621876
}
@@ -1869,6 +1883,7 @@ void SSLWrap<Base>::GetPeerCertificate(
18691883
Base* w;
18701884
ASSIGN_OR_RETURN_UNWRAP(&w, args.Holder());
18711885
Environment* env = w->ssl_env();
1886+
Local<Context> context = env->context();
18721887

18731888
ClearErrorOnReturn clear_error_on_return;
18741889

@@ -1920,7 +1935,7 @@ void SSLWrap<Base>::GetPeerCertificate(
19201935
continue;
19211936

19221937
Local<Object> ca_info = X509ToObject(env, ca);
1923-
info->Set(env->issuercert_string(), ca_info);
1938+
info->Set(context, env->issuercert_string(), ca_info).FromJust();
19241939
info = ca_info;
19251940

19261941
// NOTE: Intentionally freeing cert that is not used anymore
@@ -1943,7 +1958,7 @@ void SSLWrap<Base>::GetPeerCertificate(
19431958
break;
19441959

19451960
Local<Object> ca_info = X509ToObject(env, ca);
1946-
info->Set(env->issuercert_string(), ca_info);
1961+
info->Set(context, env->issuercert_string(), ca_info).FromJust();
19471962
info = ca_info;
19481963

19491964
// NOTE: Intentionally freeing cert that is not used anymore
@@ -1955,7 +1970,7 @@ void SSLWrap<Base>::GetPeerCertificate(
19551970

19561971
// Self-issued certificate
19571972
if (X509_check_issued(cert, cert) == X509_V_OK)
1958-
info->Set(env->issuercert_string(), info);
1973+
info->Set(context, env->issuercert_string(), info).FromJust();
19591974

19601975
CHECK_NE(cert, nullptr);
19611976

@@ -2151,6 +2166,7 @@ void SSLWrap<Base>::GetEphemeralKeyInfo(
21512166
Base* w;
21522167
ASSIGN_OR_RETURN_UNWRAP(&w, args.Holder());
21532168
Environment* env = Environment::GetCurrent(args);
2169+
Local<Context> context = env->context();
21542170

21552171
CHECK_NE(w->ssl_, nullptr);
21562172

@@ -2165,22 +2181,24 @@ void SSLWrap<Base>::GetEphemeralKeyInfo(
21652181
if (SSL_get_server_tmp_key(w->ssl_, &key)) {
21662182
switch (EVP_PKEY_id(key)) {
21672183
case EVP_PKEY_DH:
2168-
info->Set(env->type_string(),
2169-
FIXED_ONE_BYTE_STRING(env->isolate(), "DH"));
2170-
info->Set(env->size_string(),
2171-
Integer::New(env->isolate(), EVP_PKEY_bits(key)));
2184+
info->Set(context, env->type_string(),
2185+
FIXED_ONE_BYTE_STRING(env->isolate(), "DH")).FromJust();
2186+
info->Set(context, env->size_string(),
2187+
Integer::New(env->isolate(), EVP_PKEY_bits(key))).FromJust();
21722188
break;
21732189
case EVP_PKEY_EC:
21742190
{
21752191
EC_KEY* ec = EVP_PKEY_get1_EC_KEY(key);
21762192
int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
21772193
EC_KEY_free(ec);
2178-
info->Set(env->type_string(),
2179-
FIXED_ONE_BYTE_STRING(env->isolate(), "ECDH"));
2180-
info->Set(env->name_string(),
2181-
OneByteString(args.GetIsolate(), OBJ_nid2sn(nid)));
2182-
info->Set(env->size_string(),
2183-
Integer::New(env->isolate(), EVP_PKEY_bits(key)));
2194+
info->Set(context, env->type_string(),
2195+
FIXED_ONE_BYTE_STRING(env->isolate(), "ECDH")).FromJust();
2196+
info->Set(context, env->name_string(),
2197+
OneByteString(args.GetIsolate(),
2198+
OBJ_nid2sn(nid))).FromJust();
2199+
info->Set(context, env->size_string(),
2200+
Integer::New(env->isolate(),
2201+
EVP_PKEY_bits(key))).FromJust();
21842202
}
21852203
}
21862204
EVP_PKEY_free(key);
@@ -2273,7 +2291,8 @@ void SSLWrap<Base>::VerifyError(const FunctionCallbackInfo<Value>& args) {
22732291
Local<String> reason_string = OneByteString(isolate, reason);
22742292
Local<Value> exception_value = Exception::Error(reason_string);
22752293
Local<Object> exception_object = exception_value->ToObject(isolate);
2276-
exception_object->Set(w->env()->code_string(), OneByteString(isolate, code));
2294+
exception_object->Set(w->env()->context(), w->env()->code_string(),
2295+
OneByteString(isolate, code)).FromJust();
22772296
args.GetReturnValue().Set(exception_object);
22782297
}
22792298

@@ -2283,16 +2302,18 @@ void SSLWrap<Base>::GetCurrentCipher(const FunctionCallbackInfo<Value>& args) {
22832302
Base* w;
22842303
ASSIGN_OR_RETURN_UNWRAP(&w, args.Holder());
22852304
Environment* env = w->ssl_env();
2305+
Local<Context> context = env->context();
22862306

22872307
const SSL_CIPHER* c = SSL_get_current_cipher(w->ssl_);
22882308
if (c == nullptr)
22892309
return;
22902310

22912311
Local<Object> info = Object::New(env->isolate());
22922312
const char* cipher_name = SSL_CIPHER_get_name(c);
2293-
info->Set(env->name_string(), OneByteString(args.GetIsolate(), cipher_name));
2294-
info->Set(env->version_string(),
2295-
OneByteString(args.GetIsolate(), "TLSv1/SSLv3"));
2313+
info->Set(context, env->name_string(),
2314+
OneByteString(args.GetIsolate(), cipher_name)).FromJust();
2315+
info->Set(context, env->version_string(),
2316+
OneByteString(args.GetIsolate(), "TLSv1/SSLv3")).FromJust();
22962317
args.GetReturnValue().Set(info);
22972318
}
22982319

@@ -2601,27 +2622,31 @@ int SSLWrap<Base>::SSLCertCallback(SSL* s, void* arg) {
26012622
return -1;
26022623

26032624
Environment* env = w->env();
2625+
Local<Context> context = env->context();
26042626
HandleScope handle_scope(env->isolate());
2605-
Context::Scope context_scope(env->context());
2627+
Context::Scope context_scope(context);
26062628
w->cert_cb_running_ = true;
26072629

26082630
Local<Object> info = Object::New(env->isolate());
26092631

26102632
const char* servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
26112633
if (servername == nullptr) {
2612-
info->Set(env->servername_string(), String::Empty(env->isolate()));
2634+
info->Set(context,
2635+
env->servername_string(),
2636+
String::Empty(env->isolate())).FromJust();
26132637
} else {
26142638
Local<String> str = OneByteString(env->isolate(), servername,
26152639
strlen(servername));
2616-
info->Set(env->servername_string(), str);
2640+
info->Set(context, env->servername_string(), str).FromJust();
26172641
}
26182642

26192643
bool ocsp = false;
26202644
#ifdef NODE__HAVE_TLSEXT_STATUS_CB
26212645
ocsp = SSL_get_tlsext_status_type(s) == TLSEXT_STATUSTYPE_ocsp;
26222646
#endif
26232647

2624-
info->Set(env->ocsp_request_string(), Boolean::New(env->isolate(), ocsp));
2648+
info->Set(context, env->ocsp_request_string(),
2649+
Boolean::New(env->isolate(), ocsp)).FromJust();
26252650

26262651
Local<Value> argv[] = { info };
26272652
w->MakeCallback(env->oncertcb_string(), arraysize(argv), argv);
@@ -5550,7 +5575,7 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
55505575
keylen);
55515576

55525577
if (args[5]->IsFunction()) {
5553-
obj->Set(env->ondone_string(), args[5]);
5578+
obj->Set(env->context(), env->ondone_string(), args[5]).FromJust();
55545579

55555580
if (env->in_domain()) {
55565581
obj->Set(env->context(),
@@ -5758,7 +5783,7 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
57585783
RandomBytesRequest::FREE_DATA);
57595784

57605785
if (args[1]->IsFunction()) {
5761-
obj->Set(env->ondone_string(), args[1]);
5786+
obj->Set(env->context(), env->ondone_string(), args[1]).FromJust();
57625787

57635788
if (env->in_domain()) {
57645789
obj->Set(env->context(),
@@ -5846,7 +5871,10 @@ void GetSSLCiphers(const FunctionCallbackInfo<Value>& args) {
58465871

58475872
for (int i = 0; i < sk_SSL_CIPHER_num(ciphers); ++i) {
58485873
const SSL_CIPHER* cipher = sk_SSL_CIPHER_value(ciphers, i);
5849-
arr->Set(i, OneByteString(args.GetIsolate(), SSL_CIPHER_get_name(cipher)));
5874+
arr->Set(env->context(),
5875+
i,
5876+
OneByteString(args.GetIsolate(),
5877+
SSL_CIPHER_get_name(cipher))).FromJust();
58505878
}
58515879

58525880
SSL_free(ssl);
@@ -5909,7 +5937,10 @@ void GetCurves(const FunctionCallbackInfo<Value>& args) {
59095937

59105938
if (EC_get_builtin_curves(curves, num_curves)) {
59115939
for (size_t i = 0; i < num_curves; i++) {
5912-
arr->Set(i, OneByteString(env->isolate(), OBJ_nid2sn(curves[i].nid)));
5940+
arr->Set(env->context(),
5941+
i,
5942+
OneByteString(env->isolate(),
5943+
OBJ_nid2sn(curves[i].nid))).FromJust();
59135944
}
59145945
}
59155946

0 commit comments

Comments
 (0)