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>());
@@ -1124,8 +1127,8 @@ MaybeLocal<Object> GetEphemeralKey(Environment* env, const SSLPointer& ssl) {
1124
1127
{
1125
1128
const char * curve_name;
1126
1129
if (kid == EVP_PKEY_EC) {
1127
- ECKeyPointer ec ( EVP_PKEY_get1_EC_KEY ( key.get () ));
1128
- int nid = EC_GROUP_get_curve_name (EC_KEY_get0_group (ec. get () ));
1130
+ OSSL3_CONST EC_KEY* ec = EVP_PKEY_get0_EC_KEY ( key.get ());
1131
+ int nid = EC_GROUP_get_curve_name (EC_KEY_get0_group (ec));
1129
1132
curve_name = OBJ_nid2sn (nid);
1130
1133
} else {
1131
1134
curve_name = OBJ_nid2sn (kid);
@@ -1284,24 +1287,24 @@ MaybeLocal<Object> X509ToObject(
1284
1287
return MaybeLocal<Object>();
1285
1288
}
1286
1289
1287
- EVPKeyPointer pkey ( X509_get_pubkey ( cert) );
1288
- RSAPointer rsa;
1289
- ECPointer ec ;
1290
- if (pkey) {
1291
- switch (EVP_PKEY_id (pkey. get () )) {
1290
+ OSSL3_CONST EVP_PKEY* pkey = X509_get0_pubkey ( cert);
1291
+ OSSL3_CONST RSA* rsa = nullptr ;
1292
+ OSSL3_CONST EC_KEY* ec = nullptr ;
1293
+ if (pkey != nullptr ) {
1294
+ switch (EVP_PKEY_id (pkey)) {
1292
1295
case EVP_PKEY_RSA:
1293
- rsa. reset ( EVP_PKEY_get1_RSA ( pkey. get ()) );
1296
+ rsa = EVP_PKEY_get0_RSA ( pkey);
1294
1297
break ;
1295
1298
case EVP_PKEY_EC:
1296
- ec. reset ( EVP_PKEY_get1_EC_KEY ( pkey. get ()) );
1299
+ ec = EVP_PKEY_get0_EC_KEY ( pkey);
1297
1300
break ;
1298
1301
}
1299
1302
}
1300
1303
1301
1304
if (rsa) {
1302
1305
const BIGNUM* n;
1303
1306
const BIGNUM* e;
1304
- RSA_get0_key (rsa. get () , &n, &e, nullptr );
1307
+ RSA_get0_key (rsa, &n, &e, nullptr );
1305
1308
if (!Set<Value>(context,
1306
1309
info,
1307
1310
env->modulus_string (),
@@ -1318,7 +1321,7 @@ MaybeLocal<Object> X509ToObject(
1318
1321
return MaybeLocal<Object>();
1319
1322
}
1320
1323
} else if (ec) {
1321
- const EC_GROUP* group = EC_KEY_get0_group (ec. get () );
1324
+ const EC_GROUP* group = EC_KEY_get0_group (ec);
1322
1325
1323
1326
if (!Set<Value>(
1324
1327
context, info, env->bits_string (), GetECGroupBits (env, group)) ||
@@ -1347,11 +1350,6 @@ MaybeLocal<Object> X509ToObject(
1347
1350
}
1348
1351
}
1349
1352
1350
- // pkey, rsa, and ec pointers are no longer needed.
1351
- pkey.reset ();
1352
- rsa.reset ();
1353
- ec.reset ();
1354
-
1355
1353
if (!Set<Value>(context,
1356
1354
info,
1357
1355
env->valid_from_string (),
0 commit comments