Skip to content

Add ability to customize ssl mode settings #475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 15, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion MySQLdb/_mysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,10 @@ _mysql_ConnectionObject_Initialize(
return -1;
}
#else
#ifndef MARIADB_BASE_VERSION
PyErr_SetString(_mysql_NotSupportedError, "MySQL client library does not support ssl_mode specification");
return -1;
#endif
#endif
}

Expand All @@ -486,6 +488,21 @@ _mysql_ConnectionObject_Initialize(
}
Py_BEGIN_ALLOW_THREADS ;
self->open = 1;

#ifdef MARIADB_BASE_VERSION
if (ssl_mode) {
if (strcasecmp(ssl_mode, "PREFERRED") != 0)
{
int enforce_tls= 0;
if (strcasecmp(ssl_mode, "REQUIRED") == 0)
enforce_tls = 1;
#ifdef MYSQL_OPT_SSL_ENFORCE
mysql_optionsv(&(self->connection), MYSQL_OPT_SSL_ENFORCE, (void *)&enforce_tls);
mysql_optionsv(&(self->connection), MYSQL_OPT_SSL_VERIFY_SERVER_CERT, (void *)&enforce_tls);
#endif
}
}
#endif
if (connect_timeout) {
unsigned int timeout = connect_timeout;
mysql_options(&(self->connection), MYSQL_OPT_CONNECT_TIMEOUT,
Expand Down Expand Up @@ -522,7 +539,12 @@ _mysql_ConnectionObject_Initialize(
}
#ifdef HAVE_ENUM_MYSQL_OPT_SSL_MODE
if (ssl_mode) {
int ssl_mode_num = _get_ssl_mode_num(ssl_mode);
char *corrected_ssl_mode = NULL;
if (strcasecmp(ssl_mode, "REQUIRED") == 0 || strcasecmp(ssl_mode, "VERIFY_CA") == 0)
corrected_ssl_mode = "VERIFY_IDENTITY";
else
corrected_ssl_mode = ssl_mode;
int ssl_mode_num = _get_ssl_mode_num(corrected_ssl_mode);
mysql_options(&(self->connection), MYSQL_OPT_SSL_MODE, &ssl_mode_num);
}
#endif
Expand Down