Skip to content

Commit 33f2a72

Browse files
committed
WL14816: Remove support for TLS 1.0 and 1.1
1 parent 1c237d8 commit 33f2a72

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

driver/mysql_connection.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ static const String2IntMap stringOptions[]=
301301
{OPT_READ_DEFAULT_FILE, MYSQL_READ_DEFAULT_FILE, false},
302302
{OPT_CHARSET_NAME, MYSQL_SET_CHARSET_NAME, true},
303303
#if MYCPPCONN_STATIC_MYSQL_VERSION_ID >= 50700
304-
{OPT_TLS_VERSION, MYSQL_OPT_TLS_VERSION, false},
304+
{OPT_TLS_VERSION, MYSQL_OPT_TLS_VERSION, true},
305305
#endif
306306
{OPT_LOAD_DATA_LOCAL_DIR, MYSQL_OPT_LOAD_DATA_LOCAL_DIR, false}
307307
};
@@ -755,6 +755,23 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
755755
throw sql::InvalidArgumentException("No string value passed for sslCipher");
756756
}
757757
ssl_used = true;
758+
} else if (!it->first.compare(OPT_TLS_VERSION)) {
759+
try {
760+
p_s = (it->second).get< sql::SQLString >();
761+
} catch (sql::InvalidArgumentException&) {
762+
throw sql::InvalidArgumentException("Wrong type passed for OPT_TLS_VERSION expected sql::SQLString");
763+
}
764+
if (p_s) {
765+
try {
766+
proxy->options(sql::mysql::MYSQL_OPT_TLS_VERSION, *p_s);
767+
} catch (const sql::InvalidArgumentException&) {
768+
//We will not throw error here, but wait for connection error
769+
//libmysqlclient treats not valid TLS versions as invalid options.
770+
}
771+
772+
} else {
773+
throw sql::InvalidArgumentException("No string value passed for OPT_TLS_VERSION");
774+
}
758775
} else if (!it->first.compare(OPT_DEFAULT_STMT_RESULT_TYPE)) {
759776
try {
760777
p_i = (it->second).get< int >();
@@ -1103,7 +1120,13 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
11031120
" the password with mysql client that is capable to do that,"
11041121
" or rebuild your instance of Connector/C++ against mysql client"
11051122
" library that supports resetting of an expired password.";
1106-
} else {
1123+
} else if(native_error == CR_SSL_CONNECTION_ERROR){
1124+
error_message= proxy->error();
1125+
if(error_message.find("TLS version") != std::string::npos)
1126+
{
1127+
error_message+=", valid versions are: TLSv1.2, TLSv1.3";
1128+
}
1129+
}else {
11071130
error_message= proxy->error();
11081131
}
11091132

test/unit/classes/connection.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3572,6 +3572,21 @@ void connection::tls_version()
35723572

35733573
connection_properties["OPT_SSL_MODE"] = sql::SSL_MODE_REQUIRED;
35743574

3575+
// Using ALL TLS version... should connect
3576+
connection_properties["OPT_TLS_VERSION"] = tls_available;
3577+
3578+
created_objects.clear();
3579+
try
3580+
{
3581+
con.reset(driver->connect(connection_properties));
3582+
}
3583+
catch (sql::SQLException &)
3584+
{
3585+
FAIL("ALL TLS available versions used and still can't connect!");
3586+
}
3587+
3588+
3589+
35753590
// Using wrong TLS version... should fail to connect
35763591
connection_properties["OPT_TLS_VERSION"] = sql::SQLString("TLSv999");
35773592

@@ -3587,6 +3602,8 @@ void connection::tls_version()
35873602
}
35883603

35893604

3605+
3606+
35903607
for (std::vector<std::string>::const_iterator version = tls_versions.begin();
35913608
version != tls_versions.end();
35923609
++version)
@@ -3882,5 +3899,59 @@ void connection::mfa()
38823899

38833900
}
38843901

3902+
void connection::tls_deprecation()
3903+
{
3904+
sql::ConnectOptionsMap opt;
3905+
opt[OPT_HOSTNAME]=url;
3906+
opt[OPT_USERNAME]=user;
3907+
opt[OPT_PASSWORD]=passwd;
3908+
opt[OPT_SSL_MODE]=sql::SSL_MODE_REQUIRED;
3909+
3910+
struct TEST_CASES
3911+
{
3912+
const std::string& tls_versions;
3913+
bool succeed;
3914+
};
3915+
3916+
TEST_CASES test_cases[] =
3917+
{
3918+
{"TLSv1.1,TLSv1.2" ,true },
3919+
{"foo,TLSv1.3" ,true },
3920+
{"TLSv1.0,TLSv1.1" ,false},
3921+
{"foo,TLSv1.1" ,false},
3922+
{"foo,bar" ,false},
3923+
{"" ,false}
3924+
};
3925+
3926+
for(auto test : test_cases)
3927+
{
3928+
logMsg(test.tls_versions);
3929+
opt[OPT_TLS_VERSION] = test.tls_versions;
3930+
try {
3931+
Connection test_connection(driver->connect(opt));
3932+
if(!test.succeed)
3933+
{
3934+
std::stringstream err;
3935+
err << "TLS versions (" << test.tls_versions << ") SHOULD THROW EXCEPTION";
3936+
FAIL(err.str());
3937+
}
3938+
stmt.reset(test_connection->createStatement());
3939+
res.reset(stmt->executeQuery("select @@version"));
3940+
3941+
res->next();
3942+
std::string version = res->getString(1);
3943+
3944+
logMsg(std::string("Server Version ")+version);
3945+
3946+
} catch (sql::SQLException &e) {
3947+
logMsg(e.what());
3948+
ASSERT_EQUALS(false, test.succeed);
3949+
ASSERT_EQUALS(2026, e.getErrorCode());
3950+
ASSERT_EQUALS("SSL connection error: TLS version is invalid, valid versions are: TLSv1.2, TLSv1.3", e.what());
3951+
}
3952+
}
3953+
3954+
}
3955+
38853956
} /* namespace connection */
38863957
} /* namespace testsuite */

test/unit/classes/connection.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class connection : public unit_fixture
9292
TEST_CASE(socket);
9393
TEST_CASE(dns_srv);
9494
TEST_CASE(mfa);
95+
TEST_CASE(tls_deprecation);
9596
}
9697

9798
/**
@@ -290,6 +291,12 @@ class connection : public unit_fixture
290291
*/
291292
void mfa();
292293

294+
/*
295+
* Test of MySQL_Connection::tls_deprecation()
296+
*
297+
*/
298+
void tls_deprecation();
299+
293300

294301
};
295302

0 commit comments

Comments
 (0)