Skip to content

Commit 4842836

Browse files
panvaaduh95
authored andcommitted
src: make crypto backend checks explicit
Reject unsupported backends at compile time so OpenSSL 1.1.1 backports cannot silently use a BoringSSL or provider-specific path. Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #67056 Reviewed-By: Richard Lau <richard.lau@ibm.com> Reviewed-By: Tim Perry <pimterry@gmail.com>
1 parent 1cb68c9 commit 4842836

9 files changed

Lines changed: 284 additions & 14 deletions

File tree

‎deps/ncrypto/ncrypto.cc‎

Lines changed: 195 additions & 9 deletions
Large diffs are not rendered by default.

‎deps/ncrypto/ncrypto.h‎

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@
5252
#ifdef OPENSSL_IS_BORINGSSL
5353
#define NCRYPTO_USE_BORINGSSL 1
5454
#define NCRYPTO_USE_OPENSSL_PROVIDER 0
55-
#else
55+
#elif OPENSSL_VERSION_PREREQ(3, 0)
5656
#define NCRYPTO_USE_BORINGSSL 0
5757
#define NCRYPTO_USE_OPENSSL_PROVIDER 1
58+
#else
59+
#error "Unsupported crypto backend"
5860
#endif
5961

6062
#define NCRYPTO_USE_LEGACY_KEY_TYPES NCRYPTO_USE_BORINGSSL
@@ -404,6 +406,8 @@ class DigestCache final {
404406
static_cast<void>(id);
405407
static_cast<void>(generation);
406408
return {};
409+
#else
410+
#error "Unsupported crypto backend"
407411
#endif
408412
}
409413
Result insert(const char* name, const EVP_MD* digest, uint64_t generation);
@@ -591,6 +595,8 @@ class Dsa final {
591595
explicit Dsa(const EVP_PKEY* pkey);
592596
#elif NCRYPTO_USE_BORINGSSL
593597
Dsa(OSSL3_CONST DSA* dsa);
598+
#else
599+
#error "Unsupported crypto backend"
594600
#endif
595601
NCRYPTO_DISALLOW_COPY_AND_MOVE(Dsa)
596602

@@ -600,6 +606,8 @@ class Dsa final {
600606
}
601607
#elif NCRYPTO_USE_BORINGSSL
602608
inline operator bool() const { return dsa_ != nullptr; }
609+
#else
610+
#error "Unsupported crypto backend"
603611
#endif
604612
#if NCRYPTO_USE_LEGACY_KEY_TYPES
605613
inline operator OSSL3_CONST DSA*() const { return dsa_; }
@@ -617,6 +625,8 @@ class Dsa final {
617625
DeleteFnPtr<BIGNUM, BN_free> q_;
618626
#elif NCRYPTO_USE_BORINGSSL
619627
OSSL3_CONST DSA* dsa_;
628+
#else
629+
#error "Unsupported crypto backend"
620630
#endif
621631
};
622632

@@ -632,6 +642,8 @@ class Rsa final {
632642
explicit Rsa(const EVP_PKEY* pkey, Selection selection = Selection::Private);
633643
#elif NCRYPTO_USE_BORINGSSL
634644
Rsa(OSSL3_CONST RSA* rsa);
645+
#else
646+
#error "Unsupported crypto backend"
635647
#endif
636648
NCRYPTO_DISALLOW_COPY_AND_MOVE(Rsa)
637649

@@ -641,6 +653,8 @@ class Rsa final {
641653
}
642654
#elif NCRYPTO_USE_BORINGSSL
643655
inline operator bool() const { return rsa_ != nullptr; }
656+
#else
657+
#error "Unsupported crypto backend"
644658
#endif
645659
#if NCRYPTO_USE_LEGACY_KEY_TYPES
646660
inline operator OSSL3_CONST RSA*() const { return rsa_; }
@@ -724,6 +738,8 @@ class Rsa final {
724738
std::optional<PssParams> pss_params_;
725739
#elif NCRYPTO_USE_BORINGSSL
726740
OSSL3_CONST RSA* rsa_;
741+
#else
742+
#error "Unsupported crypto backend"
727743
#endif
728744
};
729745

@@ -734,6 +750,8 @@ class Ec final {
734750
explicit Ec(const EVP_PKEY* pkey);
735751
#elif NCRYPTO_USE_BORINGSSL
736752
Ec(OSSL3_CONST EC_KEY* key);
753+
#else
754+
#error "Unsupported crypto backend"
737755
#endif
738756
NCRYPTO_DISALLOW_COPY_AND_MOVE(Ec)
739757

@@ -772,6 +790,8 @@ class Ec final {
772790
point_conversion_form_t form_ = POINT_CONVERSION_UNCOMPRESSED;
773791
#elif NCRYPTO_USE_BORINGSSL
774792
OSSL3_CONST EC_KEY* ec_ = nullptr;
793+
#else
794+
#error "Unsupported crypto backend"
775795
#endif
776796
};
777797

@@ -1216,6 +1236,8 @@ class EVPKeyPointer final {
12161236
static EVPKeyPointer NewRSA(const Rsa& rsa);
12171237
#elif NCRYPTO_USE_BORINGSSL
12181238
static EVPKeyPointer NewRSA(RSAPointer&& rsa);
1239+
#else
1240+
#error "Unsupported crypto backend"
12191241
#endif
12201242

12211243
enum class PKEncodingType {
@@ -1404,6 +1426,8 @@ class DHPointer final {
14041426
DHPointer(BignumPointer&& p, BignumPointer&& g, const char* group_name);
14051427
#elif NCRYPTO_USE_BORINGSSL
14061428
explicit DHPointer(DH* dh);
1429+
#else
1430+
#error "Unsupported crypto backend"
14071431
#endif
14081432
DHPointer(DHPointer&& other) noexcept;
14091433
DHPointer& operator=(DHPointer&& other) noexcept;
@@ -1420,17 +1444,21 @@ class DHPointer final {
14201444
#elif NCRYPTO_USE_BORINGSSL
14211445
inline bool operator==(std::nullptr_t) noexcept { return dh_ == nullptr; }
14221446
inline operator bool() const { return dh_ != nullptr; }
1447+
#else
1448+
#error "Unsupported crypto backend"
14231449
#endif
1424-
#if NCRYPTO_USE_LEGACY_KEY_TYPES
1450+
#if NCRYPTO_USE_BORINGSSL
14251451
inline DH* get() const { return dh_.get(); }
14261452
void reset(DH* dh = nullptr);
14271453
DH* release();
1428-
#else
1454+
#elif NCRYPTO_USE_OPENSSL_PROVIDER
14291455
inline EVP_PKEY* get() const {
14301456
return dh_.get();
14311457
}
14321458
void reset(EVP_PKEY* dh = nullptr);
14331459
EVP_PKEY* release();
1460+
#else
1461+
#error "Unsupported crypto backend"
14341462
#endif
14351463

14361464
enum class CheckResult {
@@ -1488,6 +1516,8 @@ class DHPointer final {
14881516
const char* group_name_ = nullptr;
14891517
#elif NCRYPTO_USE_BORINGSSL
14901518
DeleteFnPtr<DH, DH_free> dh_;
1519+
#else
1520+
#error "Unsupported crypto backend"
14911521
#endif
14921522
};
14931523

@@ -1824,14 +1854,18 @@ class ECKeyPointer final {
18241854
#elif NCRYPTO_USE_BORINGSSL
18251855
inline bool operator==(std::nullptr_t) noexcept { return key_ == nullptr; }
18261856
inline operator bool() const { return key_ != nullptr; }
1857+
#else
1858+
#error "Unsupported crypto backend"
18271859
#endif
1828-
#if NCRYPTO_USE_LEGACY_KEY_TYPES
1860+
#if NCRYPTO_USE_BORINGSSL
18291861
inline EC_KEY* get() const { return key_.get(); }
18301862
inline operator EC_KEY*() const { return key_.get(); }
18311863
void reset(EC_KEY* key = nullptr);
18321864
EC_KEY* release();
1833-
#else
1865+
#elif NCRYPTO_USE_OPENSSL_PROVIDER
18341866
void reset();
1867+
#else
1868+
#error "Unsupported crypto backend"
18351869
#endif
18361870

18371871
ECKeyPointer clone() const;
@@ -1865,6 +1899,8 @@ class ECKeyPointer final {
18651899
DeleteFnPtr<BIGNUM, BN_clear_free> priv_;
18661900
#elif NCRYPTO_USE_BORINGSSL
18671901
DeleteFnPtr<EC_KEY, EC_KEY_free> key_;
1902+
#else
1903+
#error "Unsupported crypto backend"
18681904
#endif
18691905
};
18701906

‎src/crypto/crypto_context.cc‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,8 @@ void SecureContext::Init(const FunctionCallbackInfo<Value>& args) {
18761876
TicketCompatibilityCallback);
18771877
#elif NCRYPTO_USE_BORINGSSL
18781878
SSL_CTX_set_tlsext_ticket_key_cb(sc->ctx_.get(), TicketCompatibilityCallback);
1879+
#else
1880+
#error "Unsupported crypto backend"
18791881
#endif
18801882
}
18811883

@@ -2140,6 +2142,8 @@ void SecureContext::SetDHParam(const FunctionCallbackInfo<Value>& args) {
21402142
if (params && params.isA(KeyAlgorithm::DH)) dh.reset(params.release());
21412143
#elif NCRYPTO_USE_BORINGSSL
21422144
dh.reset(PEM_read_bio_DHparams(bio.get(), nullptr, nullptr, nullptr));
2145+
#else
2146+
#error "Unsupported crypto backend"
21432147
#endif
21442148
}
21452149

@@ -2164,6 +2168,8 @@ void SecureContext::SetDHParam(const FunctionCallbackInfo<Value>& args) {
21642168
if (!SSL_CTX_set0_tmp_dh_pkey(sc->ctx_.get(), dh_pkey.get())) {
21652169
#elif NCRYPTO_USE_BORINGSSL
21662170
if (!SSL_CTX_set_tmp_dh(sc->ctx_.get(), dh.get())) {
2171+
#else
2172+
#error "Unsupported crypto backend"
21672173
#endif
21682174
return THROW_ERR_CRYPTO_OPERATION_FAILED(
21692175
env, "Error setting temp DH parameter");
@@ -2515,6 +2521,8 @@ void SecureContext::EnableTicketKeyCallback(
25152521
SSL_CTX_set_tlsext_ticket_key_evp_cb(wrap->ctx_.get(), TicketKeyCallback);
25162522
#elif NCRYPTO_USE_BORINGSSL
25172523
SSL_CTX_set_tlsext_ticket_key_cb(wrap->ctx_.get(), TicketKeyCallback);
2524+
#else
2525+
#error "Unsupported crypto backend"
25182526
#endif
25192527
}
25202528

@@ -2536,6 +2544,8 @@ bool InitTicketHmac(EVP_MAC_CTX* hctx,
25362544
bool InitTicketHmac(HMAC_CTX* hctx, const unsigned char* key, size_t key_len) {
25372545
return HMAC_Init_ex(hctx, key, key_len, Digest::SHA256, nullptr) == 1;
25382546
}
2547+
#else
2548+
#error "Unsupported crypto backend"
25392549
#endif
25402550
} // namespace
25412551

@@ -2547,6 +2557,8 @@ int SecureContext::TicketKeyCallback(SSL* ssl,
25472557
EVP_MAC_CTX* hctx,
25482558
#elif NCRYPTO_USE_BORINGSSL
25492559
HMAC_CTX* hctx,
2560+
#else
2561+
#error "Unsupported crypto backend"
25502562
#endif
25512563
int enc) {
25522564
static const int kTicketPartSize = 16;
@@ -2644,6 +2656,8 @@ int SecureContext::TicketCompatibilityCallback(SSL* ssl,
26442656
EVP_MAC_CTX* hctx,
26452657
#elif NCRYPTO_USE_BORINGSSL
26462658
HMAC_CTX* hctx,
2659+
#else
2660+
#error "Unsupported crypto backend"
26472661
#endif
26482662
int enc) {
26492663
SecureContext* sc = static_cast<SecureContext*>(

‎src/crypto/crypto_context.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ class SecureContext final : public BaseObject {
170170
EVP_MAC_CTX* hctx,
171171
#elif NCRYPTO_USE_BORINGSSL
172172
HMAC_CTX* hctx,
173+
#else
174+
#error "Unsupported crypto backend"
173175
#endif
174176
int enc);
175177

@@ -181,6 +183,8 @@ class SecureContext final : public BaseObject {
181183
EVP_MAC_CTX* hctx,
182184
#elif NCRYPTO_USE_BORINGSSL
183185
HMAC_CTX* hctx,
186+
#else
187+
#error "Unsupported crypto backend"
184188
#endif
185189
int enc);
186190

‎src/crypto/crypto_hash.cc‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ void SaveSupportedHashAlgorithms(const EVP_MD* md,
235235
Environment* env = static_cast<Environment*>(arg);
236236
env->supported_hash_algorithms.push_back(from);
237237
}
238+
#else
239+
#error "Unsupported crypto backend"
238240
#endif // NCRYPTO_USE_OPENSSL_PROVIDER
239241

240242
const std::vector<std::string>& GetSupportedHashAlgorithms(Environment* env) {
@@ -255,6 +257,8 @@ const std::vector<std::string>& GetSupportedHashAlgorithms(Environment* env) {
255257
EVP_MD_do_all_provided(nullptr, SaveSupportedProviderHashAlgorithms, env);
256258
#elif NCRYPTO_USE_BORINGSSL
257259
EVP_MD_do_all_sorted(SaveSupportedHashAlgorithms, env);
260+
#else
261+
#error "Unsupported crypto backend"
258262
#endif
259263
#if NCRYPTO_USE_BORINGSSL
260264
// SHA-3 descriptors are not included in BoringSSL's name registry.
@@ -354,6 +358,8 @@ const EVP_MD* GetDigestImplementation(
354358
#elif NCRYPTO_USE_BORINGSSL
355359
Utf8Value utf8(env->isolate(), algorithm);
356360
return ncrypto::getDigestByName(*utf8);
361+
#else
362+
#error "Unsupported crypto backend"
357363
#endif
358364
}
359365

@@ -362,6 +368,8 @@ void MarkInvalidXofLength() {
362368
ERR_raise(ERR_LIB_EVP, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
363369
#elif NCRYPTO_USE_BORINGSSL
364370
EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
371+
#else
372+
#error "Unsupported crypto backend"
365373
#endif
366374
}
367375

@@ -379,6 +387,8 @@ bool IsShakeDigest(const EVP_MD* md) {
379387
const char* name = OBJ_nid2sn(EVP_MD_type(md));
380388
return name != nullptr &&
381389
(strcmp(name, "SHAKE128") == 0 || strcmp(name, "SHAKE256") == 0);
390+
#else
391+
#error "Unsupported crypto backend"
382392
#endif
383393
}
384394
#endif
@@ -550,6 +560,8 @@ void Hash::OneShotDigest(const FunctionCallbackInfo<Value>& args) {
550560
Utf8Value utf8(env->isolate(), args[0]);
551561
return OneShotDigestWithMD(
552562
env, args, ncrypto::getDigestByName(*utf8), nullptr);
563+
#else
564+
#error "Unsupported crypto backend"
553565
#endif
554566
}
555567

‎src/crypto/crypto_rsa.cc‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ bool IsRsaPssDigestEncodable(const Digest& digest) {
5353
#elif NCRYPTO_USE_BORINGSSL
5454
static_cast<void>(digest);
5555
return true;
56+
#else
57+
#error "Unsupported crypto backend"
5658
#endif
5759
}
5860
} // namespace
@@ -403,6 +405,8 @@ KeyObjectData ImportJWKRsaKey(Environment* env, Local<Object> jwk) {
403405
}
404406

405407
ncrypto::Rsa rsa_view(rsa.get());
408+
#else
409+
#error "Unsupported crypto backend"
406410
#endif
407411

408412
ByteSource n = ByteSource::FromEncodedString(env, n_value.As<String>());
@@ -511,6 +515,8 @@ KeyObjectData ImportJWKRsaKey(Environment* env, Local<Object> jwk) {
511515
auto pkey = EVPKeyPointer::NewRSA(rsa_view);
512516
#elif NCRYPTO_USE_BORINGSSL
513517
auto pkey = EVPKeyPointer::NewRSA(std::move(rsa));
518+
#else
519+
#error "Unsupported crypto backend"
514520
#endif
515521
if (!pkey) {
516522
THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Unable to create key pointer");

‎src/crypto/crypto_tls.cc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,8 @@ void TLSWrap::ClearOut() {
955955
const char* fs = nullptr;
956956
#elif NCRYPTO_USE_BORINGSSL
957957
const char* fs = ERR_func_error_string(ssl_err);
958+
#else
959+
#error "Unsupported crypto backend"
958960
#endif
959961
const char* rs = ERR_reason_error_string(ssl_err);
960962
if (!Set(env(), obj, env()->library_string(), ls) ||

‎src/crypto/crypto_util.cc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,8 @@ Maybe<void> Decorate(Environment* env,
886886
const char* fs = nullptr;
887887
#elif NCRYPTO_USE_BORINGSSL
888888
const char* fs = ERR_func_error_string(err);
889+
#else
890+
#error "Unsupported crypto backend"
889891
#endif
890892
const char* rs = ERR_reason_error_string(err);
891893

‎test/cctest/test_node_crypto.cc‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ TEST(NodeCrypto, EcKeyComponents) {
455455
EXPECT_EQ(memcmp(raw_public.get(), point.get(), point.size()), 0);
456456
#elif NCRYPTO_USE_BORINGSSL
457457
EXPECT_FALSE(raw_public);
458+
#else
459+
#error "Unsupported crypto backend"
458460
#endif
459461
EXPECT_FALSE(
460462
ncrypto::Ec::TryExportPublic(key, POINT_CONVERSION_COMPRESSED));
@@ -1015,6 +1017,8 @@ TEST(NodeCrypto, X509FormattingPreservesUnrelatedErrors) {
10151017
ERR_raise(ERR_LIB_USER, 1);
10161018
#elif NCRYPTO_USE_BORINGSSL
10171019
ERR_put_error(ERR_LIB_USER, 0, 1, __FILE__, __LINE__);
1020+
#else
1021+
#error "Unsupported crypto backend"
10181022
#endif
10191023
const unsigned long expected = ERR_peek_error(); // NOLINT(runtime/int)
10201024
ASSERT_NE(expected, 0UL);
@@ -1147,6 +1151,8 @@ TEST(NodeCrypto, X509KeyVisitorsHandleMissingPublicKey) {
11471151
ERR_raise(ERR_LIB_USER, 1);
11481152
#elif NCRYPTO_USE_BORINGSSL
11491153
ERR_put_error(ERR_LIB_USER, 0, 1, __FILE__, __LINE__);
1154+
#else
1155+
#error "Unsupported crypto backend"
11501156
#endif
11511157
const unsigned long expected = ERR_peek_error(); // NOLINT(runtime/int)
11521158
ASSERT_NE(expected, 0UL);
@@ -1179,6 +1185,8 @@ TEST(NodeCrypto, X509UsageVisitorPreservesCallbackErrors) {
11791185
ERR_raise(ERR_LIB_USER, 1);
11801186
#elif NCRYPTO_USE_BORINGSSL
11811187
ERR_put_error(ERR_LIB_USER, 0, 1, __FILE__, __LINE__);
1188+
#else
1189+
#error "Unsupported crypto backend"
11821190
#endif
11831191
}));
11841192
EXPECT_EQ(calls, 1);

0 commit comments

Comments
 (0)