diff --git a/src/node_metadata.cc b/src/node_metadata.cc index 3bf9b9e768551d..1594cd325ab29c 100644 --- a/src/node_metadata.cc +++ b/src/node_metadata.cc @@ -23,7 +23,7 @@ #endif // NODE_BUNDLED_ZLIB #if HAVE_OPENSSL -#include +#include #if NODE_OPENSSL_HAS_QUIC #include #endif @@ -55,9 +55,10 @@ static constexpr size_t search(const char* s, char c, size_t n = 0) { static inline std::string GetOpenSSLVersion() { // sample openssl version string format // for reference: "OpenSSL 1.1.0i 14 Aug 2018" - constexpr size_t start = search(OPENSSL_VERSION_TEXT, ' ') + 1; - constexpr size_t len = search(&OPENSSL_VERSION_TEXT[start], ' '); - return std::string(OPENSSL_VERSION_TEXT, start, len); + const char* version = OpenSSL_version(OPENSSL_VERSION); + const size_t start = search(version, ' ') + 1; + const size_t len = search(&version[start], ' '); + return std::string(version, start, len); } #endif // HAVE_OPENSSL diff --git a/test/common/index.js b/test/common/index.js index 86667fd39deee1..ce000d1c52fbeb 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -57,14 +57,24 @@ const noop = () => {}; const hasCrypto = Boolean(process.versions.openssl) && !process.env.NODE_SKIP_CRYPTO; -const hasOpenSSL3 = hasCrypto && - require('crypto').constants.OPENSSL_VERSION_NUMBER >= 0x30000000; - -const hasOpenSSL31 = hasCrypto && - require('crypto').constants.OPENSSL_VERSION_NUMBER >= 0x30100000; +// Synthesize OPENSSL_VERSION_NUMBER format with the layout 0xMNN00PPSL +const opensslVersionNumber = (major = 0, minor = 0, patch = 0) => { + assert(major >= 0 && major <= 0xf); + assert(minor >= 0 && minor <= 0xff); + assert(patch >= 0 && patch <= 0xff); + return (major << 28) | (minor << 20) | (patch << 4); +}; -const hasOpenSSL32 = hasCrypto && - require('crypto').constants.OPENSSL_VERSION_NUMBER >= 0x30200000; +let OPENSSL_VERSION_NUMBER; +const hasOpenSSL = (major = 0, minor = 0, patch = 0) => { + if (!hasCrypto) return false; + if (OPENSSL_VERSION_NUMBER === undefined) { + const regexp = /(?\d+)\.(?\d+)\.(?

\d+)/; + const { m, n, p } = process.versions.openssl.match(regexp).groups; + OPENSSL_VERSION_NUMBER = opensslVersionNumber(m, n, p); + } + return OPENSSL_VERSION_NUMBER >= opensslVersionNumber(major, minor, patch); +}; const hasQuic = hasCrypto && !!process.config.variables.openssl_quic; @@ -969,9 +979,7 @@ const common = { getTTYfd, hasIntl, hasCrypto, - hasOpenSSL3, - hasOpenSSL31, - hasOpenSSL32, + hasOpenSSL, hasQuic, hasMultiLocalhost, invalidArgTypeHelper, @@ -1032,6 +1040,18 @@ const common = { }); }, + get hasOpenSSL3() { + return hasOpenSSL(3); + }, + + get hasOpenSSL31() { + return hasOpenSSL(3, 1); + }, + + get hasOpenSSL32() { + return hasOpenSSL(3, 2); + }, + get inFreeBSDJail() { if (inFreeBSDJail !== null) return inFreeBSDJail; diff --git a/test/parallel/test-crypto-dh.js b/test/parallel/test-crypto-dh.js index 3b738b7f47ec59..fb580e1b315445 100644 --- a/test/parallel/test-crypto-dh.js +++ b/test/parallel/test-crypto-dh.js @@ -86,8 +86,8 @@ const crypto = require('crypto'); } { - const v = crypto.constants.OPENSSL_VERSION_NUMBER; - const hasOpenSSL3WithNewErrorMessage = (v >= 0x300000c0 && v <= 0x30100000) || (v >= 0x30100040 && v <= 0x30200000); + const hasOpenSSL3WithNewErrorMessage = (common.hasOpenSSL(3, 0, 12) && !common.hasOpenSSL(3, 1, 1)) || + (common.hasOpenSSL(3, 1, 4) && !common.hasOpenSSL(3, 2, 1)); assert.throws(() => { dh3.computeSecret(''); }, { message: common.hasOpenSSL3 && !hasOpenSSL3WithNewErrorMessage ?