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: https://github.com/nodejs/node/pull/17482
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Daniel Bevenius 2017-12-06 09:03:42 +01:00 committed by Ruben Bridgewater
parent 4404c7619b
commit ff21fb1624
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762

View File

@ -1724,26 +1724,31 @@ void SSLWrap<Base>::OnClientHello(void* arg,
Base* w = static_cast<Base*>(arg); Base* w = static_cast<Base*>(arg);
Environment* env = w->ssl_env(); Environment* env = w->ssl_env();
HandleScope handle_scope(env->isolate()); HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context()); Local<Context> context = env->context();
Context::Scope context_scope(context);
Local<Object> hello_obj = Object::New(env->isolate()); Local<Object> hello_obj = Object::New(env->isolate());
Local<Object> buff = Buffer::Copy( Local<Object> buff = Buffer::Copy(
env, env,
reinterpret_cast<const char*>(hello.session_id()), reinterpret_cast<const char*>(hello.session_id()),
hello.session_size()).ToLocalChecked(); hello.session_size()).ToLocalChecked();
hello_obj->Set(env->session_id_string(), buff); hello_obj->Set(context, env->session_id_string(), buff).FromJust();
if (hello.servername() == nullptr) { if (hello.servername() == nullptr) {
hello_obj->Set(env->servername_string(), String::Empty(env->isolate())); hello_obj->Set(context,
env->servername_string(),
String::Empty(env->isolate())).FromJust();
} else { } else {
Local<String> servername = OneByteString(env->isolate(), Local<String> servername = OneByteString(env->isolate(),
hello.servername(), hello.servername(),
hello.servername_size()); hello.servername_size());
hello_obj->Set(env->servername_string(), servername); hello_obj->Set(context, env->servername_string(), servername).FromJust();
} }
hello_obj->Set(env->tls_ticket_string(), hello_obj->Set(context,
Boolean::New(env->isolate(), hello.has_ticket())); env->tls_ticket_string(),
hello_obj->Set(env->ocsp_request_string(), Boolean::New(env->isolate(), hello.has_ticket())).FromJust();
Boolean::New(env->isolate(), hello.ocsp_request())); hello_obj->Set(context,
env->ocsp_request_string(),
Boolean::New(env->isolate(), hello.ocsp_request())).FromJust();
Local<Value> argv[] = { hello_obj }; Local<Value> argv[] = { hello_obj };
w->MakeCallback(env->onclienthello_string(), arraysize(argv), argv); w->MakeCallback(env->onclienthello_string(), arraysize(argv), argv);
@ -1788,7 +1793,7 @@ static bool SafeX509ExtPrint(BIO* out, X509_EXTENSION* ext) {
static Local<Object> X509ToObject(Environment* env, X509* cert) { static Local<Object> X509ToObject(Environment* env, X509* cert) {
EscapableHandleScope scope(env->isolate()); EscapableHandleScope scope(env->isolate());
Local<Context> context = env->context();
Local<Object> info = Object::New(env->isolate()); Local<Object> info = Object::New(env->isolate());
BIO* bio = BIO_new(BIO_s_mem()); BIO* bio = BIO_new(BIO_s_mem());
@ -1798,18 +1803,20 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
0, 0,
X509_NAME_FLAGS) > 0) { X509_NAME_FLAGS) > 0) {
BIO_get_mem_ptr(bio, &mem); BIO_get_mem_ptr(bio, &mem);
info->Set(env->subject_string(), info->Set(context, env->subject_string(),
String::NewFromUtf8(env->isolate(), mem->data, String::NewFromUtf8(env->isolate(), mem->data,
String::kNormalString, mem->length)); String::kNormalString,
mem->length)).FromJust();
} }
USE(BIO_reset(bio)); USE(BIO_reset(bio));
X509_NAME* issuer_name = X509_get_issuer_name(cert); X509_NAME* issuer_name = X509_get_issuer_name(cert);
if (X509_NAME_print_ex(bio, issuer_name, 0, X509_NAME_FLAGS) > 0) { if (X509_NAME_print_ex(bio, issuer_name, 0, X509_NAME_FLAGS) > 0) {
BIO_get_mem_ptr(bio, &mem); BIO_get_mem_ptr(bio, &mem);
info->Set(env->issuer_string(), info->Set(context, env->issuer_string(),
String::NewFromUtf8(env->isolate(), mem->data, String::NewFromUtf8(env->isolate(), mem->data,
String::kNormalString, mem->length)); String::kNormalString,
mem->length)).FromJust();
} }
USE(BIO_reset(bio)); USE(BIO_reset(bio));
@ -1834,9 +1841,10 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
} }
BIO_get_mem_ptr(bio, &mem); BIO_get_mem_ptr(bio, &mem);
info->Set(keys[i], info->Set(context, keys[i],
String::NewFromUtf8(env->isolate(), mem->data, String::NewFromUtf8(env->isolate(), mem->data,
String::kNormalString, mem->length)); String::kNormalString,
mem->length)).FromJust();
USE(BIO_reset(bio)); USE(BIO_reset(bio));
} }
@ -1852,9 +1860,10 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
RSA_get0_key(rsa, &n, &e, nullptr); RSA_get0_key(rsa, &n, &e, nullptr);
BN_print(bio, n); BN_print(bio, n);
BIO_get_mem_ptr(bio, &mem); BIO_get_mem_ptr(bio, &mem);
info->Set(env->modulus_string(), info->Set(context, env->modulus_string(),
String::NewFromUtf8(env->isolate(), mem->data, String::NewFromUtf8(env->isolate(), mem->data,
String::kNormalString, mem->length)); String::kNormalString,
mem->length)).FromJust();
USE(BIO_reset(bio)); USE(BIO_reset(bio));
uint64_t exponent_word = static_cast<uint64_t>(BN_get_word(e)); uint64_t exponent_word = static_cast<uint64_t>(BN_get_word(e));
@ -1866,9 +1875,10 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
BIO_printf(bio, "0x%x%08x", hi, lo); BIO_printf(bio, "0x%x%08x", hi, lo);
} }
BIO_get_mem_ptr(bio, &mem); BIO_get_mem_ptr(bio, &mem);
info->Set(env->exponent_string(), info->Set(context, env->exponent_string(),
String::NewFromUtf8(env->isolate(), mem->data, String::NewFromUtf8(env->isolate(), mem->data,
String::kNormalString, mem->length)); String::kNormalString,
mem->length)).FromJust();
USE(BIO_reset(bio)); USE(BIO_reset(bio));
} }
@ -1883,16 +1893,18 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
ASN1_TIME_print(bio, X509_get_notBefore(cert)); ASN1_TIME_print(bio, X509_get_notBefore(cert));
BIO_get_mem_ptr(bio, &mem); BIO_get_mem_ptr(bio, &mem);
info->Set(env->valid_from_string(), info->Set(context, env->valid_from_string(),
String::NewFromUtf8(env->isolate(), mem->data, String::NewFromUtf8(env->isolate(), mem->data,
String::kNormalString, mem->length)); String::kNormalString,
mem->length)).FromJust();
USE(BIO_reset(bio)); USE(BIO_reset(bio));
ASN1_TIME_print(bio, X509_get_notAfter(cert)); ASN1_TIME_print(bio, X509_get_notAfter(cert));
BIO_get_mem_ptr(bio, &mem); BIO_get_mem_ptr(bio, &mem);
info->Set(env->valid_to_string(), info->Set(context, env->valid_to_string(),
String::NewFromUtf8(env->isolate(), mem->data, String::NewFromUtf8(env->isolate(), mem->data,
String::kNormalString, mem->length)); String::kNormalString,
mem->length)).FromJust();
BIO_free_all(bio); BIO_free_all(bio);
unsigned int md_size, i; unsigned int md_size, i;
@ -1913,8 +1925,8 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
fingerprint[0] = '\0'; fingerprint[0] = '\0';
} }
info->Set(env->fingerprint_string(), info->Set(context, env->fingerprint_string(),
OneByteString(env->isolate(), fingerprint)); OneByteString(env->isolate(), fingerprint)).FromJust();
} }
STACK_OF(ASN1_OBJECT)* eku = static_cast<STACK_OF(ASN1_OBJECT)*>( STACK_OF(ASN1_OBJECT)* eku = static_cast<STACK_OF(ASN1_OBJECT)*>(
@ -1926,18 +1938,20 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
int j = 0; int j = 0;
for (int i = 0; i < sk_ASN1_OBJECT_num(eku); i++) { for (int i = 0; i < sk_ASN1_OBJECT_num(eku); i++) {
if (OBJ_obj2txt(buf, sizeof(buf), sk_ASN1_OBJECT_value(eku, i), 1) >= 0) if (OBJ_obj2txt(buf, sizeof(buf), sk_ASN1_OBJECT_value(eku, i), 1) >= 0)
ext_key_usage->Set(j++, OneByteString(env->isolate(), buf)); ext_key_usage->Set(context,
j++,
OneByteString(env->isolate(), buf)).FromJust();
} }
sk_ASN1_OBJECT_pop_free(eku, ASN1_OBJECT_free); sk_ASN1_OBJECT_pop_free(eku, ASN1_OBJECT_free);
info->Set(env->ext_key_usage_string(), ext_key_usage); info->Set(context, env->ext_key_usage_string(), ext_key_usage).FromJust();
} }
if (ASN1_INTEGER* serial_number = X509_get_serialNumber(cert)) { if (ASN1_INTEGER* serial_number = X509_get_serialNumber(cert)) {
if (BIGNUM* bn = ASN1_INTEGER_to_BN(serial_number, nullptr)) { if (BIGNUM* bn = ASN1_INTEGER_to_BN(serial_number, nullptr)) {
if (char* buf = BN_bn2hex(bn)) { if (char* buf = BN_bn2hex(bn)) {
info->Set(env->serial_number_string(), info->Set(context, env->serial_number_string(),
OneByteString(env->isolate(), buf)); OneByteString(env->isolate(), buf)).FromJust();
OPENSSL_free(buf); OPENSSL_free(buf);
} }
BN_free(bn); BN_free(bn);
@ -1950,7 +1964,7 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) {
unsigned char* serialized = reinterpret_cast<unsigned char*>( unsigned char* serialized = reinterpret_cast<unsigned char*>(
Buffer::Data(buff)); Buffer::Data(buff));
i2d_X509(cert, &serialized); i2d_X509(cert, &serialized);
info->Set(env->raw_string(), buff); info->Set(context, env->raw_string(), buff).FromJust();
return scope.Escape(info); return scope.Escape(info);
} }
@ -1963,6 +1977,7 @@ void SSLWrap<Base>::GetPeerCertificate(
Base* w; Base* w;
ASSIGN_OR_RETURN_UNWRAP(&w, args.Holder()); ASSIGN_OR_RETURN_UNWRAP(&w, args.Holder());
Environment* env = w->ssl_env(); Environment* env = w->ssl_env();
Local<Context> context = env->context();
ClearErrorOnReturn clear_error_on_return; ClearErrorOnReturn clear_error_on_return;
@ -2014,7 +2029,7 @@ void SSLWrap<Base>::GetPeerCertificate(
continue; continue;
Local<Object> ca_info = X509ToObject(env, ca); Local<Object> ca_info = X509ToObject(env, ca);
info->Set(env->issuercert_string(), ca_info); info->Set(context, env->issuercert_string(), ca_info).FromJust();
info = ca_info; info = ca_info;
// NOTE: Intentionally freeing cert that is not used anymore // NOTE: Intentionally freeing cert that is not used anymore
@ -2037,7 +2052,7 @@ void SSLWrap<Base>::GetPeerCertificate(
break; break;
Local<Object> ca_info = X509ToObject(env, ca); Local<Object> ca_info = X509ToObject(env, ca);
info->Set(env->issuercert_string(), ca_info); info->Set(context, env->issuercert_string(), ca_info).FromJust();
info = ca_info; info = ca_info;
// NOTE: Intentionally freeing cert that is not used anymore // NOTE: Intentionally freeing cert that is not used anymore
@ -2049,7 +2064,7 @@ void SSLWrap<Base>::GetPeerCertificate(
// Self-issued certificate // Self-issued certificate
if (X509_check_issued(cert, cert) == X509_V_OK) if (X509_check_issued(cert, cert) == X509_V_OK)
info->Set(env->issuercert_string(), info); info->Set(context, env->issuercert_string(), info).FromJust();
CHECK_NE(cert, nullptr); CHECK_NE(cert, nullptr);
@ -2245,6 +2260,7 @@ void SSLWrap<Base>::GetEphemeralKeyInfo(
Base* w; Base* w;
ASSIGN_OR_RETURN_UNWRAP(&w, args.Holder()); ASSIGN_OR_RETURN_UNWRAP(&w, args.Holder());
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
Local<Context> context = env->context();
CHECK_NE(w->ssl_, nullptr); CHECK_NE(w->ssl_, nullptr);
@ -2259,22 +2275,24 @@ void SSLWrap<Base>::GetEphemeralKeyInfo(
if (SSL_get_server_tmp_key(w->ssl_, &key)) { if (SSL_get_server_tmp_key(w->ssl_, &key)) {
switch (EVP_PKEY_id(key)) { switch (EVP_PKEY_id(key)) {
case EVP_PKEY_DH: case EVP_PKEY_DH:
info->Set(env->type_string(), info->Set(context, env->type_string(),
FIXED_ONE_BYTE_STRING(env->isolate(), "DH")); FIXED_ONE_BYTE_STRING(env->isolate(), "DH")).FromJust();
info->Set(env->size_string(), info->Set(context, env->size_string(),
Integer::New(env->isolate(), EVP_PKEY_bits(key))); Integer::New(env->isolate(), EVP_PKEY_bits(key))).FromJust();
break; break;
case EVP_PKEY_EC: case EVP_PKEY_EC:
{ {
EC_KEY* ec = EVP_PKEY_get1_EC_KEY(key); EC_KEY* ec = EVP_PKEY_get1_EC_KEY(key);
int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec)); int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
EC_KEY_free(ec); EC_KEY_free(ec);
info->Set(env->type_string(), info->Set(context, env->type_string(),
FIXED_ONE_BYTE_STRING(env->isolate(), "ECDH")); FIXED_ONE_BYTE_STRING(env->isolate(), "ECDH")).FromJust();
info->Set(env->name_string(), info->Set(context, env->name_string(),
OneByteString(args.GetIsolate(), OBJ_nid2sn(nid))); OneByteString(args.GetIsolate(),
info->Set(env->size_string(), OBJ_nid2sn(nid))).FromJust();
Integer::New(env->isolate(), EVP_PKEY_bits(key))); info->Set(context, env->size_string(),
Integer::New(env->isolate(),
EVP_PKEY_bits(key))).FromJust();
} }
} }
EVP_PKEY_free(key); EVP_PKEY_free(key);
@ -2367,7 +2385,8 @@ void SSLWrap<Base>::VerifyError(const FunctionCallbackInfo<Value>& args) {
Local<String> reason_string = OneByteString(isolate, reason); Local<String> reason_string = OneByteString(isolate, reason);
Local<Value> exception_value = Exception::Error(reason_string); Local<Value> exception_value = Exception::Error(reason_string);
Local<Object> exception_object = exception_value->ToObject(isolate); Local<Object> exception_object = exception_value->ToObject(isolate);
exception_object->Set(w->env()->code_string(), OneByteString(isolate, code)); exception_object->Set(w->env()->context(), w->env()->code_string(),
OneByteString(isolate, code)).FromJust();
args.GetReturnValue().Set(exception_object); args.GetReturnValue().Set(exception_object);
} }
@ -2377,6 +2396,7 @@ void SSLWrap<Base>::GetCurrentCipher(const FunctionCallbackInfo<Value>& args) {
Base* w; Base* w;
ASSIGN_OR_RETURN_UNWRAP(&w, args.Holder()); ASSIGN_OR_RETURN_UNWRAP(&w, args.Holder());
Environment* env = w->ssl_env(); Environment* env = w->ssl_env();
Local<Context> context = env->context();
const SSL_CIPHER* c = SSL_get_current_cipher(w->ssl_); const SSL_CIPHER* c = SSL_get_current_cipher(w->ssl_);
if (c == nullptr) if (c == nullptr)
@ -2384,9 +2404,10 @@ void SSLWrap<Base>::GetCurrentCipher(const FunctionCallbackInfo<Value>& args) {
Local<Object> info = Object::New(env->isolate()); Local<Object> info = Object::New(env->isolate());
const char* cipher_name = SSL_CIPHER_get_name(c); const char* cipher_name = SSL_CIPHER_get_name(c);
info->Set(env->name_string(), OneByteString(args.GetIsolate(), cipher_name)); info->Set(context, env->name_string(),
info->Set(env->version_string(), OneByteString(args.GetIsolate(), cipher_name)).FromJust();
OneByteString(args.GetIsolate(), "TLSv1/SSLv3")); info->Set(context, env->version_string(),
OneByteString(args.GetIsolate(), "TLSv1/SSLv3")).FromJust();
args.GetReturnValue().Set(info); args.GetReturnValue().Set(info);
} }
@ -2695,19 +2716,22 @@ int SSLWrap<Base>::SSLCertCallback(SSL* s, void* arg) {
return -1; return -1;
Environment* env = w->env(); Environment* env = w->env();
Local<Context> context = env->context();
HandleScope handle_scope(env->isolate()); HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context()); Context::Scope context_scope(context);
w->cert_cb_running_ = true; w->cert_cb_running_ = true;
Local<Object> info = Object::New(env->isolate()); Local<Object> info = Object::New(env->isolate());
const char* servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); const char* servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
if (servername == nullptr) { if (servername == nullptr) {
info->Set(env->servername_string(), String::Empty(env->isolate())); info->Set(context,
env->servername_string(),
String::Empty(env->isolate())).FromJust();
} else { } else {
Local<String> str = OneByteString(env->isolate(), servername, Local<String> str = OneByteString(env->isolate(), servername,
strlen(servername)); strlen(servername));
info->Set(env->servername_string(), str); info->Set(context, env->servername_string(), str).FromJust();
} }
bool ocsp = false; bool ocsp = false;
@ -2715,7 +2739,8 @@ int SSLWrap<Base>::SSLCertCallback(SSL* s, void* arg) {
ocsp = SSL_get_tlsext_status_type(s) == TLSEXT_STATUSTYPE_ocsp; ocsp = SSL_get_tlsext_status_type(s) == TLSEXT_STATUSTYPE_ocsp;
#endif #endif
info->Set(env->ocsp_request_string(), Boolean::New(env->isolate(), ocsp)); info->Set(context, env->ocsp_request_string(),
Boolean::New(env->isolate(), ocsp)).FromJust();
Local<Value> argv[] = { info }; Local<Value> argv[] = { info };
w->MakeCallback(env->oncertcb_string(), arraysize(argv), argv); w->MakeCallback(env->oncertcb_string(), arraysize(argv), argv);
@ -4997,7 +5022,7 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
keylen)); keylen));
if (args[5]->IsFunction()) { if (args[5]->IsFunction()) {
obj->Set(env->ondone_string(), args[5]); obj->Set(env->context(), env->ondone_string(), args[5]).FromJust();
uv_queue_work(env->event_loop(), uv_queue_work(env->event_loop(),
req.release()->work_req(), req.release()->work_req(),
@ -5185,7 +5210,7 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
RandomBytesRequest::FREE_DATA)); RandomBytesRequest::FREE_DATA));
if (args[1]->IsFunction()) { if (args[1]->IsFunction()) {
obj->Set(env->ondone_string(), args[1]); obj->Set(env->context(), env->ondone_string(), args[1]).FromJust();
uv_queue_work(env->event_loop(), uv_queue_work(env->event_loop(),
req.release()->work_req(), req.release()->work_req(),
@ -5254,7 +5279,10 @@ void GetSSLCiphers(const FunctionCallbackInfo<Value>& args) {
for (int i = 0; i < sk_SSL_CIPHER_num(ciphers); ++i) { for (int i = 0; i < sk_SSL_CIPHER_num(ciphers); ++i) {
const SSL_CIPHER* cipher = sk_SSL_CIPHER_value(ciphers, i); const SSL_CIPHER* cipher = sk_SSL_CIPHER_value(ciphers, i);
arr->Set(i, OneByteString(args.GetIsolate(), SSL_CIPHER_get_name(cipher))); arr->Set(env->context(),
i,
OneByteString(args.GetIsolate(),
SSL_CIPHER_get_name(cipher))).FromJust();
} }
SSL_free(ssl); SSL_free(ssl);
@ -5317,7 +5345,10 @@ void GetCurves(const FunctionCallbackInfo<Value>& args) {
if (EC_get_builtin_curves(curves, num_curves)) { if (EC_get_builtin_curves(curves, num_curves)) {
for (size_t i = 0; i < num_curves; i++) { for (size_t i = 0; i < num_curves; i++) {
arr->Set(i, OneByteString(env->isolate(), OBJ_nid2sn(curves[i].nid))); arr->Set(env->context(),
i,
OneByteString(env->isolate(),
OBJ_nid2sn(curves[i].nid))).FromJust();
} }
} }