Skip to content

Commit 0290e0a

Browse files
panvaaduh95
authored andcommitted
crypto: split OpenSSL 3, BoringSSL, and legacy backends
This separates the native crypto backend paths for OpenSSL >= 3, BoringSSL, and legacy OpenSSL. The OpenSSL >= 3 path now builds with `OPENSSL_API_COMPAT=30000` and `OPENSSL_NO_DEPRECATED`, moving normal crypto/TLS code away from APIs that OpenSSL 3.0.0 marks deprecated. BoringSSL remains on its own branch, and OpenSSL < 3 remains the legacy fallback. The exception is ENGINE support. ENGINE APIs are isolated into a dedicated compatibility target so they can remain available while the JS-facing engine APIs are runtime-deprecated in 27.x. That gives us a clear removal point for 28.x, without letting ENGINE usage leak back into the strict OpenSSL 3 path. The split also makes the eventual OpenSSL 1.1.1 removal easier to reason about. Once support for OpenSSL < 3 is dropped, the legacy branch can be removed in a focused follow-up, possibly targeting 27.x, instead of untangling mixed version guards throughout the crypto implementation. No public crypto or TLS API behavior is intentionally changed. Assisted-by: Codex:gpt-5 Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #64211 Backport-PR-URL: #65087 Refs: #56733 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Richard Lau <richard.lau@ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent c7a0440 commit 0290e0a

23 files changed

Lines changed: 2477 additions & 211 deletions

common.gypi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
'node_module_version%': '',
2525
'node_with_ltcg%': '',
2626
'node_shared_openssl%': 'false',
27+
'openssl_is_boringssl%': 'false',
2728

2829
'node_tag%': '',
2930
'uv_library%': 'static_library',

configure.py

Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,50 +1328,72 @@ def get_gas_version(cc):
13281328
warn(f'Could not recognize `gas`: {gas_ret}')
13291329
return '0.0'
13301330

1331-
def get_openssl_version():
1331+
def get_openssl_macros(o):
1332+
"""Extract OpenSSL preprocessor macros from the configured headers."""
1333+
1334+
# Use the C compiler to extract preprocessor macros from OpenSSL headers.
1335+
# crypto.h is included because BoringSSL declares OPENSSL_IS_BORINGSSL there.
1336+
args = ['-E', '-dM',
1337+
'-include', 'openssl/opensslv.h',
1338+
'-include', 'openssl/crypto.h',
1339+
'-']
1340+
if not options.shared_openssl:
1341+
args = ['-I', 'deps/openssl/openssl/include'] + args
1342+
elif options.shared_openssl_includes:
1343+
args = ['-I', options.shared_openssl_includes] + args
1344+
else:
1345+
for dir in o['include_dirs']:
1346+
args = ['-I', dir] + args
1347+
1348+
proc = subprocess.Popen(
1349+
shlex.split(CC) + args,
1350+
stdin=subprocess.PIPE,
1351+
stdout=subprocess.PIPE,
1352+
stderr=subprocess.PIPE
1353+
)
1354+
with proc:
1355+
proc.stdin.write(b'\n')
1356+
out = to_utf8(proc.communicate()[0])
1357+
1358+
if proc.returncode != 0:
1359+
warn('Failed to extract OpenSSL macros from headers')
1360+
return {}
1361+
1362+
macros = {}
1363+
for line in out.split('\n'):
1364+
if line.startswith('#define OPENSSL_'):
1365+
parts = line.split()
1366+
if len(parts) >= 2:
1367+
macro_name = parts[1]
1368+
macro_value = parts[2] if len(parts) >= 3 else '1'
1369+
macros[macro_name] = macro_value
1370+
1371+
return macros
1372+
1373+
def get_openssl_version(o):
13321374
"""Parse OpenSSL version from opensslv.h header file.
13331375
13341376
Returns the version as a number matching OPENSSL_VERSION_NUMBER format:
1335-
0xMNN00PPSL where M=major, NN=minor, PP=patch, S=status(0xf=release,0x0=pre), L=0
1377+
0xMNN00PPSL where M=major, NN=minor, PP=patch, S=status(0xf=release,0x0=pre),
1378+
L denotes as a long type literal
13361379
"""
13371380

13381381
try:
1339-
# Use the C compiler to extract preprocessor macros from opensslv.h
1340-
args = ['-E', '-dM', '-include', 'openssl/opensslv.h', '-']
1341-
if not options.shared_openssl:
1342-
args = ['-I', 'deps/openssl/openssl/include'] + args
1343-
elif options.shared_openssl_includes:
1344-
args = ['-I', options.shared_openssl_includes] + args
1345-
1346-
proc = subprocess.Popen(
1347-
shlex.split(CC) + args,
1348-
stdin=subprocess.PIPE,
1349-
stdout=subprocess.PIPE,
1350-
stderr=subprocess.PIPE
1351-
)
1352-
with proc:
1353-
proc.stdin.write(b'\n')
1354-
out = to_utf8(proc.communicate()[0])
1355-
1356-
if proc.returncode != 0:
1357-
warn('Failed to extract OpenSSL version from opensslv.h header')
1358-
return 0
1359-
1360-
# Parse the macro definitions
1361-
macros = {}
1362-
for line in out.split('\n'):
1363-
if line.startswith('#define OPENSSL_VERSION_'):
1364-
parts = line.split()
1365-
if len(parts) >= 3:
1366-
macro_name = parts[1]
1367-
macro_value = parts[2]
1368-
macros[macro_name] = macro_value
1382+
macros = get_openssl_macros(o)
13691383

13701384
# Extract version components
13711385
major = int(macros.get('OPENSSL_VERSION_MAJOR', '0'))
13721386
minor = int(macros.get('OPENSSL_VERSION_MINOR', '0'))
13731387
patch = int(macros.get('OPENSSL_VERSION_PATCH', '0'))
13741388

1389+
# If major, minor and patch are all 0, this is probably OpenSSL < 3.
1390+
if (major, minor, patch) == (0, 0, 0):
1391+
version_number = macros.get('OPENSSL_VERSION_NUMBER')
1392+
# Prior to OpenSSL 3 the value should be in the format 0xMNN00PPSL.
1393+
# If it is, we need to strip the `L` suffix prior to parsing.
1394+
if version_number[:2] == "0x" and version_number[-1] == "L":
1395+
return int(version_number[:-1], 16)
1396+
13751397
# Check if it's a pre-release (has non-empty PRE_RELEASE string)
13761398
pre_release = macros.get('OPENSSL_VERSION_PRE_RELEASE', '""').strip('"')
13771399
status = 0x0 if pre_release else 0xf
@@ -1387,6 +1409,13 @@ def get_openssl_version():
13871409
warn(f'Failed to determine OpenSSL version from header: {e}')
13881410
return 0
13891411

1412+
def get_openssl_is_boringssl(o):
1413+
try:
1414+
return b('OPENSSL_IS_BORINGSSL' in get_openssl_macros(o))
1415+
except (OSError, ValueError, subprocess.SubprocessError) as e:
1416+
warn(f'Failed to determine whether OpenSSL headers are BoringSSL: {e}')
1417+
return 'false'
1418+
13901419
# Note: Apple clang self-reports as clang 4.2.0 and gcc 4.2.1. It passes
13911420
# the version check more by accident than anything else but a more rigorous
13921421
# check involves checking the build number against an allowlist. I'm not
@@ -2065,7 +2094,8 @@ def without_ssl_error(option):
20652094

20662095
configure_library('openssl', o)
20672096

2068-
o['variables']['openssl_version'] = get_openssl_version()
2097+
o['variables']['openssl_version'] = get_openssl_version(o)
2098+
o['variables']['openssl_is_boringssl'] = get_openssl_is_boringssl(o)
20692099

20702100
def configure_sqlite(o):
20712101
o['variables']['node_use_sqlite'] = b(not options.without_sqlite)

deps/ncrypto/engine.cc

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
#include "ncrypto.h"
22

3+
#if !defined(OPENSSL_NO_ENGINE) && \
4+
((defined(NCRYPTO_ENGINE_COMPAT) && NCRYPTO_ENGINE_COMPAT) || \
5+
NCRYPTO_USE_LEGACY_OPENSSL)
6+
#include <openssl/engine.h>
7+
#endif
8+
39
namespace ncrypto {
410

511
// ============================================================================
612
// Engine
713

814
#ifndef OPENSSL_NO_ENGINE
9-
EnginePointer::EnginePointer(ENGINE* engine_, bool finish_on_exit_)
15+
EnginePointer::EnginePointer(void* engine_, bool finish_on_exit_)
1016
: engine(engine_), finish_on_exit(finish_on_exit_) {}
1117

1218
EnginePointer::EnginePointer(EnginePointer&& other) noexcept
@@ -24,21 +30,22 @@ EnginePointer& EnginePointer::operator=(EnginePointer&& other) noexcept {
2430
return *new (this) EnginePointer(std::move(other));
2531
}
2632

27-
void EnginePointer::reset(ENGINE* engine_, bool finish_on_exit_) {
33+
void EnginePointer::reset(void* engine_, bool finish_on_exit_) {
2834
if (engine != nullptr) {
35+
ENGINE* current = static_cast<ENGINE*>(engine);
2936
if (finish_on_exit) {
3037
// This also does the equivalent of ENGINE_free.
31-
ENGINE_finish(engine);
38+
ENGINE_finish(current);
3239
} else {
33-
ENGINE_free(engine);
40+
ENGINE_free(current);
3441
}
3542
}
3643
engine = engine_;
3744
finish_on_exit = finish_on_exit_;
3845
}
3946

40-
ENGINE* EnginePointer::release() {
41-
ENGINE* ret = engine;
47+
void* EnginePointer::release() {
48+
void* ret = engine;
4249
engine = nullptr;
4350
finish_on_exit = false;
4451
return ret;
@@ -52,8 +59,9 @@ EnginePointer EnginePointer::getEngineByName(const char* name,
5259
// Engine not found, try loading dynamically.
5360
engine = EnginePointer(ENGINE_by_id("dynamic"));
5461
if (engine) {
55-
if (!ENGINE_ctrl_cmd_string(engine.get(), "SO_PATH", name, 0) ||
56-
!ENGINE_ctrl_cmd_string(engine.get(), "LOAD", nullptr, 0)) {
62+
ENGINE* current = static_cast<ENGINE*>(engine.engine);
63+
if (!ENGINE_ctrl_cmd_string(current, "SO_PATH", name, 0) ||
64+
!ENGINE_ctrl_cmd_string(current, "LOAD", nullptr, 0)) {
5765
engine.reset();
5866
}
5967
}
@@ -64,19 +72,24 @@ EnginePointer EnginePointer::getEngineByName(const char* name,
6472
bool EnginePointer::setAsDefault(uint32_t flags, CryptoErrorList* errors) {
6573
if (engine == nullptr) return false;
6674
ClearErrorOnReturn clear_error_on_return(errors);
67-
return ENGINE_set_default(engine, flags) != 0;
75+
return ENGINE_set_default(static_cast<ENGINE*>(engine), flags) != 0;
6876
}
6977

7078
bool EnginePointer::init(bool finish_on_exit) {
7179
if (engine == nullptr) return false;
7280
if (finish_on_exit) setFinishOnExit();
73-
return ENGINE_init(engine) == 1;
81+
return ENGINE_init(static_cast<ENGINE*>(engine)) == 1;
7482
}
7583

7684
EVPKeyPointer EnginePointer::loadPrivateKey(const char* key_name) {
7785
if (engine == nullptr) return EVPKeyPointer();
78-
return EVPKeyPointer(
79-
ENGINE_load_private_key(engine, key_name, nullptr, nullptr));
86+
return EVPKeyPointer(ENGINE_load_private_key(
87+
static_cast<ENGINE*>(engine), key_name, nullptr, nullptr));
88+
}
89+
90+
bool EnginePointer::setClientCertEngine(SSL_CTX* ctx) {
91+
if (engine == nullptr || ctx == nullptr) return false;
92+
return SSL_CTX_set_client_cert_engine(ctx, static_cast<ENGINE*>(engine)) == 1;
8093
}
8194

8295
void EnginePointer::initEnginesOnce() {

0 commit comments

Comments
 (0)