Skip to content

Commit 906bd1f

Browse files
committed
tls: avoid taking ownership of OpenSSL objects
It is often unnecessary to obtain (shared) ownership of OpenSSL objects in this code, and it generally is more costly to do so as opposed to just obtaining a pointer to the respective OpenSSL object. Therefore, this patch replaces various OpenSSL function calls that take ownership with ones that do not.
1 parent 73fa9ab commit 906bd1f

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

src/crypto/crypto_common.cc

+20-30
Original file line numberDiff line numberDiff line change
@@ -425,20 +425,15 @@ MaybeLocal<Value> GetCurveName(Environment* env, const int nid) {
425425
MaybeLocal<Value>(Undefined(env->isolate()));
426426
}
427427

428-
MaybeLocal<Value> GetECPubKey(
429-
Environment* env,
430-
const EC_GROUP* group,
431-
const ECPointer& ec) {
432-
const EC_POINT* pubkey = EC_KEY_get0_public_key(ec.get());
428+
MaybeLocal<Value> GetECPubKey(Environment* env,
429+
const EC_GROUP* group,
430+
const EC_KEY* ec) {
431+
const EC_POINT* pubkey = EC_KEY_get0_public_key(ec);
433432
if (pubkey == nullptr)
434433
return Undefined(env->isolate());
435434

436-
return ECPointToBuffer(
437-
env,
438-
group,
439-
pubkey,
440-
EC_KEY_get_conv_form(ec.get()),
441-
nullptr).FromMaybe(Local<Object>());
435+
return ECPointToBuffer(env, group, pubkey, EC_KEY_get_conv_form(ec), nullptr)
436+
.FromMaybe(Local<Object>());
442437
}
443438

444439
MaybeLocal<Value> GetECGroupBits(Environment* env, const EC_GROUP* group) {
@@ -452,8 +447,8 @@ MaybeLocal<Value> GetECGroupBits(Environment* env, const EC_GROUP* group) {
452447
return Integer::New(env->isolate(), bits);
453448
}
454449

455-
MaybeLocal<Object> GetPubKey(Environment* env, const RSAPointer& rsa) {
456-
int size = i2d_RSA_PUBKEY(rsa.get(), nullptr);
450+
MaybeLocal<Object> GetPubKey(Environment* env, const RSA* rsa) {
451+
int size = i2d_RSA_PUBKEY(rsa, nullptr);
457452
CHECK_GE(size, 0);
458453

459454
std::unique_ptr<BackingStore> bs;
@@ -463,7 +458,7 @@ MaybeLocal<Object> GetPubKey(Environment* env, const RSAPointer& rsa) {
463458
}
464459

465460
unsigned char* serialized = reinterpret_cast<unsigned char*>(bs->Data());
466-
CHECK_GE(i2d_RSA_PUBKEY(rsa.get(), &serialized), 0);
461+
CHECK_GE(i2d_RSA_PUBKEY(rsa, &serialized), 0);
467462

468463
Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(bs));
469464
return Buffer::New(env, ab, 0, ab->ByteLength()).FromMaybe(Local<Object>());
@@ -1125,8 +1120,8 @@ MaybeLocal<Object> GetEphemeralKey(Environment* env, const SSLPointer& ssl) {
11251120
{
11261121
const char* curve_name;
11271122
if (kid == EVP_PKEY_EC) {
1128-
ECKeyPointer ec(EVP_PKEY_get1_EC_KEY(key.get()));
1129-
int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec.get()));
1123+
const EC_KEY* ec = EVP_PKEY_get0_EC_KEY(key.get());
1124+
int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
11301125
curve_name = OBJ_nid2sn(nid);
11311126
} else {
11321127
curve_name = OBJ_nid2sn(kid);
@@ -1285,24 +1280,24 @@ MaybeLocal<Object> X509ToObject(
12851280
return MaybeLocal<Object>();
12861281
}
12871282

1288-
EVPKeyPointer pkey(X509_get_pubkey(cert));
1289-
RSAPointer rsa;
1290-
ECPointer ec;
1291-
if (pkey) {
1292-
switch (EVP_PKEY_id(pkey.get())) {
1283+
const EVP_PKEY* pkey = X509_get0_pubkey(cert);
1284+
const RSA* rsa = nullptr;
1285+
const EC_KEY* ec = nullptr;
1286+
if (pkey != nullptr) {
1287+
switch (EVP_PKEY_id(pkey)) {
12931288
case EVP_PKEY_RSA:
1294-
rsa.reset(EVP_PKEY_get1_RSA(pkey.get()));
1289+
rsa = EVP_PKEY_get0_RSA(pkey);
12951290
break;
12961291
case EVP_PKEY_EC:
1297-
ec.reset(EVP_PKEY_get1_EC_KEY(pkey.get()));
1292+
ec = EVP_PKEY_get0_EC_KEY(pkey);
12981293
break;
12991294
}
13001295
}
13011296

13021297
if (rsa) {
13031298
const BIGNUM* n;
13041299
const BIGNUM* e;
1305-
RSA_get0_key(rsa.get(), &n, &e, nullptr);
1300+
RSA_get0_key(rsa, &n, &e, nullptr);
13061301
if (!Set<Value>(context,
13071302
info,
13081303
env->modulus_string(),
@@ -1319,7 +1314,7 @@ MaybeLocal<Object> X509ToObject(
13191314
return MaybeLocal<Object>();
13201315
}
13211316
} else if (ec) {
1322-
const EC_GROUP* group = EC_KEY_get0_group(ec.get());
1317+
const EC_GROUP* group = EC_KEY_get0_group(ec);
13231318

13241319
if (!Set<Value>(
13251320
context, info, env->bits_string(), GetECGroupBits(env, group)) ||
@@ -1348,11 +1343,6 @@ MaybeLocal<Object> X509ToObject(
13481343
}
13491344
}
13501345

1351-
// pkey, rsa, and ec pointers are no longer needed.
1352-
pkey.reset();
1353-
rsa.reset();
1354-
ec.reset();
1355-
13561346
if (!Set<Value>(context,
13571347
info,
13581348
env->valid_from_string(),

0 commit comments

Comments
 (0)