21
21
#include < string>
22
22
#include < unordered_map>
23
23
24
+ // Some OpenSSL 1.1.1 functions unnecessarily operate on and return non-const
25
+ // pointers, whereas the same functions in OpenSSL 3 use const pointers.
26
+ #if OPENSSL_VERSION_MAJOR >= 3
27
+ #define OSSL3_CONST const
28
+ #else
29
+ #define OSSL3_CONST
30
+ #endif
31
+
24
32
namespace node {
25
33
26
34
using v8::Array;
@@ -425,20 +433,15 @@ MaybeLocal<Value> GetCurveName(Environment* env, const int nid) {
425
433
MaybeLocal<Value>(Undefined (env->isolate ()));
426
434
}
427
435
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 ());
436
+ MaybeLocal<Value> GetECPubKey (Environment* env,
437
+ const EC_GROUP* group,
438
+ OSSL3_CONST EC_KEY* ec) {
439
+ const EC_POINT* pubkey = EC_KEY_get0_public_key (ec);
433
440
if (pubkey == nullptr )
434
441
return Undefined (env->isolate ());
435
442
436
- return ECPointToBuffer (
437
- env,
438
- group,
439
- pubkey,
440
- EC_KEY_get_conv_form (ec.get ()),
441
- nullptr ).FromMaybe (Local<Object>());
443
+ return ECPointToBuffer (env, group, pubkey, EC_KEY_get_conv_form (ec), nullptr )
444
+ .FromMaybe (Local<Object>());
442
445
}
443
446
444
447
MaybeLocal<Value> GetECGroupBits (Environment* env, const EC_GROUP* group) {
@@ -452,8 +455,8 @@ MaybeLocal<Value> GetECGroupBits(Environment* env, const EC_GROUP* group) {
452
455
return Integer::New (env->isolate (), bits);
453
456
}
454
457
455
- MaybeLocal<Object> GetPubKey (Environment* env, const RSAPointer& rsa) {
456
- int size = i2d_RSA_PUBKEY (rsa. get () , nullptr );
458
+ MaybeLocal<Object> GetPubKey (Environment* env, OSSL3_CONST RSA* rsa) {
459
+ int size = i2d_RSA_PUBKEY (rsa, nullptr );
457
460
CHECK_GE (size, 0 );
458
461
459
462
std::unique_ptr<BackingStore> bs;
@@ -463,7 +466,7 @@ MaybeLocal<Object> GetPubKey(Environment* env, const RSAPointer& rsa) {
463
466
}
464
467
465
468
unsigned char * serialized = reinterpret_cast <unsigned char *>(bs->Data ());
466
- CHECK_GE (i2d_RSA_PUBKEY (rsa. get () , &serialized), 0 );
469
+ CHECK_GE (i2d_RSA_PUBKEY (rsa, &serialized), 0 );
467
470
468
471
Local<ArrayBuffer> ab = ArrayBuffer::New (env->isolate (), std::move (bs));
469
472
return Buffer::New (env, ab, 0 , ab->ByteLength ()).FromMaybe (Local<Object>());
@@ -1125,8 +1128,8 @@ MaybeLocal<Object> GetEphemeralKey(Environment* env, const SSLPointer& ssl) {
1125
1128
{
1126
1129
const char * curve_name;
1127
1130
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 () ));
1131
+ OSSL3_CONST EC_KEY* ec = EVP_PKEY_get0_EC_KEY ( key.get ());
1132
+ int nid = EC_GROUP_get_curve_name (EC_KEY_get0_group (ec));
1130
1133
curve_name = OBJ_nid2sn (nid);
1131
1134
} else {
1132
1135
curve_name = OBJ_nid2sn (kid);
@@ -1285,24 +1288,24 @@ MaybeLocal<Object> X509ToObject(
1285
1288
return MaybeLocal<Object>();
1286
1289
}
1287
1290
1288
- EVPKeyPointer pkey ( X509_get_pubkey ( cert) );
1289
- RSAPointer rsa;
1290
- ECPointer ec ;
1291
- if (pkey) {
1292
- switch (EVP_PKEY_id (pkey. get () )) {
1291
+ OSSL3_CONST EVP_PKEY* pkey = X509_get0_pubkey ( cert);
1292
+ OSSL3_CONST RSA* rsa = nullptr ;
1293
+ OSSL3_CONST EC_KEY* ec = nullptr ;
1294
+ if (pkey != nullptr ) {
1295
+ switch (EVP_PKEY_id (pkey)) {
1293
1296
case EVP_PKEY_RSA:
1294
- rsa. reset ( EVP_PKEY_get1_RSA ( pkey. get ()) );
1297
+ rsa = EVP_PKEY_get0_RSA ( pkey);
1295
1298
break ;
1296
1299
case EVP_PKEY_EC:
1297
- ec. reset ( EVP_PKEY_get1_EC_KEY ( pkey. get ()) );
1300
+ ec = EVP_PKEY_get0_EC_KEY ( pkey);
1298
1301
break ;
1299
1302
}
1300
1303
}
1301
1304
1302
1305
if (rsa) {
1303
1306
const BIGNUM* n;
1304
1307
const BIGNUM* e;
1305
- RSA_get0_key (rsa. get () , &n, &e, nullptr );
1308
+ RSA_get0_key (rsa, &n, &e, nullptr );
1306
1309
if (!Set<Value>(context,
1307
1310
info,
1308
1311
env->modulus_string (),
@@ -1319,7 +1322,7 @@ MaybeLocal<Object> X509ToObject(
1319
1322
return MaybeLocal<Object>();
1320
1323
}
1321
1324
} else if (ec) {
1322
- const EC_GROUP* group = EC_KEY_get0_group (ec. get () );
1325
+ const EC_GROUP* group = EC_KEY_get0_group (ec);
1323
1326
1324
1327
if (!Set<Value>(
1325
1328
context, info, env->bits_string (), GetECGroupBits (env, group)) ||
@@ -1348,11 +1351,6 @@ MaybeLocal<Object> X509ToObject(
1348
1351
}
1349
1352
}
1350
1353
1351
- // pkey, rsa, and ec pointers are no longer needed.
1352
- pkey.reset ();
1353
- rsa.reset ();
1354
- ec.reset ();
1355
-
1356
1354
if (!Set<Value>(context,
1357
1355
info,
1358
1356
env->valid_from_string (),
0 commit comments