Skip to content

Commit 5a16a67

Browse files
tniessenaddaleax
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 bcc0913 commit 5a16a67

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
@@ -551,73 +551,65 @@ void SecureContext::Init(const FunctionCallbackInfo<Value>& args) {
551551
// are still accepted. They are OpenSSL's way of saying that all known
552552
// protocols below TLS 1.3 are supported unless explicitly disabled (which
553553
// we do below for SSLv2 and SSLv3.)
554-
if (strcmp(*sslmethod, "SSLv2_method") == 0) {
554+
if (sslmethod == "SSLv2_method" ||
555+
sslmethod == "SSLv2_server_method" ||
556+
sslmethod == "SSLv2_client_method") {
555557
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv2 methods disabled");
556558
return;
557-
} else if (strcmp(*sslmethod, "SSLv2_server_method") == 0) {
558-
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv2 methods disabled");
559-
return;
560-
} else if (strcmp(*sslmethod, "SSLv2_client_method") == 0) {
561-
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv2 methods disabled");
562-
return;
563-
} else if (strcmp(*sslmethod, "SSLv3_method") == 0) {
564-
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv3 methods disabled");
565-
return;
566-
} else if (strcmp(*sslmethod, "SSLv3_server_method") == 0) {
567-
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv3 methods disabled");
568-
return;
569-
} else if (strcmp(*sslmethod, "SSLv3_client_method") == 0) {
559+
} else if (sslmethod == "SSLv3_method" ||
560+
sslmethod == "SSLv3_server_method" ||
561+
sslmethod == "SSLv3_client_method") {
570562
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv3 methods disabled");
571563
return;
572-
} else if (strcmp(*sslmethod, "SSLv23_method") == 0) {
564+
} else if (sslmethod == "SSLv23_method") {
573565
max_version = TLS1_2_VERSION;
574-
} else if (strcmp(*sslmethod, "SSLv23_server_method") == 0) {
566+
} else if (sslmethod == "SSLv23_server_method") {
575567
max_version = TLS1_2_VERSION;
576568
method = TLS_server_method();
577-
} else if (strcmp(*sslmethod, "SSLv23_client_method") == 0) {
569+
} else if (sslmethod == "SSLv23_client_method") {
578570
max_version = TLS1_2_VERSION;
579571
method = TLS_client_method();
580-
} else if (strcmp(*sslmethod, "TLS_method") == 0) {
572+
} else if (sslmethod == "TLS_method") {
581573
min_version = 0;
582574
max_version = MAX_SUPPORTED_VERSION;
583-
} else if (strcmp(*sslmethod, "TLS_server_method") == 0) {
575+
} else if (sslmethod == "TLS_server_method") {
584576
min_version = 0;
585577
max_version = MAX_SUPPORTED_VERSION;
586578
method = TLS_server_method();
587-
} else if (strcmp(*sslmethod, "TLS_client_method") == 0) {
579+
} else if (sslmethod == "TLS_client_method") {
588580
min_version = 0;
589581
max_version = MAX_SUPPORTED_VERSION;
590582
method = TLS_client_method();
591-
} else if (strcmp(*sslmethod, "TLSv1_method") == 0) {
583+
} else if (sslmethod == "TLSv1_method") {
592584
min_version = TLS1_VERSION;
593585
max_version = TLS1_VERSION;
594-
} else if (strcmp(*sslmethod, "TLSv1_server_method") == 0) {
586+
} else if (sslmethod == "TLSv1_server_method") {
595587
min_version = TLS1_VERSION;
596588
max_version = TLS1_VERSION;
597589
method = TLS_server_method();
598-
} else if (strcmp(*sslmethod, "TLSv1_client_method") == 0) {
590+
} else if (sslmethod == "TLSv1_client_method") {
599591
min_version = TLS1_VERSION;
600592
max_version = TLS1_VERSION;
601593
method = TLS_client_method();
602-
} else if (strcmp(*sslmethod, "TLSv1_1_method") == 0) {
594+
} else if (sslmethod == "TLSv1_1_method") {
603595
min_version = TLS1_1_VERSION;
604596
max_version = TLS1_1_VERSION;
605-
} else if (strcmp(*sslmethod, "TLSv1_1_server_method") == 0) {
597+
} else if (sslmethod == "TLSv1_1_server_method") {
606598
min_version = TLS1_1_VERSION;
607599
max_version = TLS1_1_VERSION;
608600
method = TLS_server_method();
609-
} else if (strcmp(*sslmethod, "TLSv1_1_client_method") == 0) {
601+
} else if (sslmethod == "TLSv1_1_client_method") {
610602
min_version = TLS1_1_VERSION;
611603
max_version = TLS1_1_VERSION;
612604
method = TLS_client_method();
613-
} else if (strcmp(*sslmethod, "TLSv1_2_method") == 0) {
605+
} else if (sslmethod == "TLSv1_2_method") {
614606
min_version = TLS1_2_VERSION;
615607
max_version = TLS1_2_VERSION;
616-
} else if (strcmp(*sslmethod, "TLSv1_2_server_method") == 0) {
608+
} else if (sslmethod == "TLSv1_2_server_method") {
617609
min_version = TLS1_2_VERSION;
618610
max_version = TLS1_2_VERSION;
619611
method = TLS_server_method();
620-
} else if (strcmp(*sslmethod, "TLSv1_2_client_method") == 0) {
612+
} else if (sslmethod == "TLSv1_2_client_method") {
621613
min_version = TLS1_2_VERSION;
622614
max_version = TLS1_2_VERSION;
623615
method = TLS_client_method();

src/util.h

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

483483
inline std::string ToString() const { return std::string(out(), length()); }
484+
485+
inline bool operator==(const char* a) const {
486+
return strcmp(out(), a) == 0;
487+
}
484488
};
485489

486490
class TwoByteValue : public MaybeStackBuffer<uint16_t> {

0 commit comments

Comments
 (0)