Skip to content

Commit 9d40af5

Browse files
tniessenMylesBorins
authored andcommitted
src: avoid strcmp in SecureContext::Init
PR-URL: #34329 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent e9c7722 commit 9d40af5

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

src/node_crypto.cc

+21-29
Original file line numberDiff line numberDiff line change
@@ -572,73 +572,65 @@ void SecureContext::Init(const FunctionCallbackInfo<Value>& args) {
572572
// are still accepted. They are OpenSSL's way of saying that all known
573573
// protocols below TLS 1.3 are supported unless explicitly disabled (which
574574
// we do below for SSLv2 and SSLv3.)
575-
if (strcmp(*sslmethod, "SSLv2_method") == 0) {
575+
if (sslmethod == "SSLv2_method" ||
576+
sslmethod == "SSLv2_server_method" ||
577+
sslmethod == "SSLv2_client_method") {
576578
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv2 methods disabled");
577579
return;
578-
} else if (strcmp(*sslmethod, "SSLv2_server_method") == 0) {
579-
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv2 methods disabled");
580-
return;
581-
} else if (strcmp(*sslmethod, "SSLv2_client_method") == 0) {
582-
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv2 methods disabled");
583-
return;
584-
} else if (strcmp(*sslmethod, "SSLv3_method") == 0) {
585-
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv3 methods disabled");
586-
return;
587-
} else if (strcmp(*sslmethod, "SSLv3_server_method") == 0) {
588-
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv3 methods disabled");
589-
return;
590-
} else if (strcmp(*sslmethod, "SSLv3_client_method") == 0) {
580+
} else if (sslmethod == "SSLv3_method" ||
581+
sslmethod == "SSLv3_server_method" ||
582+
sslmethod == "SSLv3_client_method") {
591583
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv3 methods disabled");
592584
return;
593-
} else if (strcmp(*sslmethod, "SSLv23_method") == 0) {
585+
} else if (sslmethod == "SSLv23_method") {
594586
max_version = TLS1_2_VERSION;
595-
} else if (strcmp(*sslmethod, "SSLv23_server_method") == 0) {
587+
} else if (sslmethod == "SSLv23_server_method") {
596588
max_version = TLS1_2_VERSION;
597589
method = TLS_server_method();
598-
} else if (strcmp(*sslmethod, "SSLv23_client_method") == 0) {
590+
} else if (sslmethod == "SSLv23_client_method") {
599591
max_version = TLS1_2_VERSION;
600592
method = TLS_client_method();
601-
} else if (strcmp(*sslmethod, "TLS_method") == 0) {
593+
} else if (sslmethod == "TLS_method") {
602594
min_version = 0;
603595
max_version = MAX_SUPPORTED_VERSION;
604-
} else if (strcmp(*sslmethod, "TLS_server_method") == 0) {
596+
} else if (sslmethod == "TLS_server_method") {
605597
min_version = 0;
606598
max_version = MAX_SUPPORTED_VERSION;
607599
method = TLS_server_method();
608-
} else if (strcmp(*sslmethod, "TLS_client_method") == 0) {
600+
} else if (sslmethod == "TLS_client_method") {
609601
min_version = 0;
610602
max_version = MAX_SUPPORTED_VERSION;
611603
method = TLS_client_method();
612-
} else if (strcmp(*sslmethod, "TLSv1_method") == 0) {
604+
} else if (sslmethod == "TLSv1_method") {
613605
min_version = TLS1_VERSION;
614606
max_version = TLS1_VERSION;
615-
} else if (strcmp(*sslmethod, "TLSv1_server_method") == 0) {
607+
} else if (sslmethod == "TLSv1_server_method") {
616608
min_version = TLS1_VERSION;
617609
max_version = TLS1_VERSION;
618610
method = TLS_server_method();
619-
} else if (strcmp(*sslmethod, "TLSv1_client_method") == 0) {
611+
} else if (sslmethod == "TLSv1_client_method") {
620612
min_version = TLS1_VERSION;
621613
max_version = TLS1_VERSION;
622614
method = TLS_client_method();
623-
} else if (strcmp(*sslmethod, "TLSv1_1_method") == 0) {
615+
} else if (sslmethod == "TLSv1_1_method") {
624616
min_version = TLS1_1_VERSION;
625617
max_version = TLS1_1_VERSION;
626-
} else if (strcmp(*sslmethod, "TLSv1_1_server_method") == 0) {
618+
} else if (sslmethod == "TLSv1_1_server_method") {
627619
min_version = TLS1_1_VERSION;
628620
max_version = TLS1_1_VERSION;
629621
method = TLS_server_method();
630-
} else if (strcmp(*sslmethod, "TLSv1_1_client_method") == 0) {
622+
} else if (sslmethod == "TLSv1_1_client_method") {
631623
min_version = TLS1_1_VERSION;
632624
max_version = TLS1_1_VERSION;
633625
method = TLS_client_method();
634-
} else if (strcmp(*sslmethod, "TLSv1_2_method") == 0) {
626+
} else if (sslmethod == "TLSv1_2_method") {
635627
min_version = TLS1_2_VERSION;
636628
max_version = TLS1_2_VERSION;
637-
} else if (strcmp(*sslmethod, "TLSv1_2_server_method") == 0) {
629+
} else if (sslmethod == "TLSv1_2_server_method") {
638630
min_version = TLS1_2_VERSION;
639631
max_version = TLS1_2_VERSION;
640632
method = TLS_server_method();
641-
} else if (strcmp(*sslmethod, "TLSv1_2_client_method") == 0) {
633+
} else if (sslmethod == "TLSv1_2_client_method") {
642634
min_version = TLS1_2_VERSION;
643635
max_version = TLS1_2_VERSION;
644636
method = TLS_client_method();

src/util.h

+4
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,10 @@ class Utf8Value : public MaybeStackBuffer<char> {
486486
explicit Utf8Value(v8::Isolate* isolate, v8::Local<v8::Value> value);
487487

488488
inline std::string ToString() const { return std::string(out(), length()); }
489+
490+
inline bool operator==(const char* a) const {
491+
return strcmp(out(), a) == 0;
492+
}
489493
};
490494

491495
class TwoByteValue : public MaybeStackBuffer<uint16_t> {

0 commit comments

Comments
 (0)