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‎

Lines changed: 21 additions & 29 deletions
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-
} elseif (strcmp(*sslmethod, "SSLv2_server_method") == 0){
558-
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv2 methods disabled");
559-
return;
560-
} elseif (strcmp(*sslmethod, "SSLv2_client_method") == 0){
561-
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv2 methods disabled");
562-
return;
563-
} elseif (strcmp(*sslmethod, "SSLv3_method") == 0){
564-
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv3 methods disabled");
565-
return;
566-
} elseif (strcmp(*sslmethod, "SSLv3_server_method") == 0){
567-
THROW_ERR_TLS_INVALID_PROTOCOL_METHOD(env, "SSLv3 methods disabled");
568-
return;
569-
} elseif (strcmp(*sslmethod, "SSLv3_client_method") == 0){
559+
} elseif (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-
} elseif (strcmp(*sslmethod, "SSLv23_method") == 0){
564+
} elseif (sslmethod== "SSLv23_method"){
573565
max_version = TLS1_2_VERSION;
574-
} elseif (strcmp(*sslmethod, "SSLv23_server_method") == 0){
566+
} elseif (sslmethod== "SSLv23_server_method"){
575567
max_version = TLS1_2_VERSION;
576568
method = TLS_server_method();
577-
} elseif (strcmp(*sslmethod, "SSLv23_client_method") == 0){
569+
} elseif (sslmethod== "SSLv23_client_method"){
578570
max_version = TLS1_2_VERSION;
579571
method = TLS_client_method();
580-
} elseif (strcmp(*sslmethod, "TLS_method") == 0){
572+
} elseif (sslmethod== "TLS_method"){
581573
min_version = 0;
582574
max_version = MAX_SUPPORTED_VERSION;
583-
} elseif (strcmp(*sslmethod, "TLS_server_method") == 0){
575+
} elseif (sslmethod== "TLS_server_method"){
584576
min_version = 0;
585577
max_version = MAX_SUPPORTED_VERSION;
586578
method = TLS_server_method();
587-
} elseif (strcmp(*sslmethod, "TLS_client_method") == 0){
579+
} elseif (sslmethod== "TLS_client_method"){
588580
min_version = 0;
589581
max_version = MAX_SUPPORTED_VERSION;
590582
method = TLS_client_method();
591-
} elseif (strcmp(*sslmethod, "TLSv1_method") == 0){
583+
} elseif (sslmethod== "TLSv1_method"){
592584
min_version = TLS1_VERSION;
593585
max_version = TLS1_VERSION;
594-
} elseif (strcmp(*sslmethod, "TLSv1_server_method") == 0){
586+
} elseif (sslmethod== "TLSv1_server_method"){
595587
min_version = TLS1_VERSION;
596588
max_version = TLS1_VERSION;
597589
method = TLS_server_method();
598-
} elseif (strcmp(*sslmethod, "TLSv1_client_method") == 0){
590+
} elseif (sslmethod== "TLSv1_client_method"){
599591
min_version = TLS1_VERSION;
600592
max_version = TLS1_VERSION;
601593
method = TLS_client_method();
602-
} elseif (strcmp(*sslmethod, "TLSv1_1_method") == 0){
594+
} elseif (sslmethod== "TLSv1_1_method"){
603595
min_version = TLS1_1_VERSION;
604596
max_version = TLS1_1_VERSION;
605-
} elseif (strcmp(*sslmethod, "TLSv1_1_server_method") == 0){
597+
} elseif (sslmethod== "TLSv1_1_server_method"){
606598
min_version = TLS1_1_VERSION;
607599
max_version = TLS1_1_VERSION;
608600
method = TLS_server_method();
609-
} elseif (strcmp(*sslmethod, "TLSv1_1_client_method") == 0){
601+
} elseif (sslmethod== "TLSv1_1_client_method"){
610602
min_version = TLS1_1_VERSION;
611603
max_version = TLS1_1_VERSION;
612604
method = TLS_client_method();
613-
} elseif (strcmp(*sslmethod, "TLSv1_2_method") == 0){
605+
} elseif (sslmethod== "TLSv1_2_method"){
614606
min_version = TLS1_2_VERSION;
615607
max_version = TLS1_2_VERSION;
616-
} elseif (strcmp(*sslmethod, "TLSv1_2_server_method") == 0){
608+
} elseif (sslmethod== "TLSv1_2_server_method"){
617609
min_version = TLS1_2_VERSION;
618610
max_version = TLS1_2_VERSION;
619611
method = TLS_server_method();
620-
} elseif (strcmp(*sslmethod, "TLSv1_2_client_method") == 0){
612+
} elseif (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‎

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

483483
inline std::string ToString() const{returnstd::string(out(), length())}
484+
485+
inlinebooloperator==(constchar* a) const{
486+
returnstrcmp(out(), a) == 0;
487+
}
484488
};
485489

486490
classTwoByteValue : publicMaybeStackBuffer<uint16_t>{

0 commit comments

Comments
(0)