@@ -380,7 +380,14 @@ static int _mysql_ResultObject_clear(_mysql_ResultObject *self)
380
380
return 0 ;
381
381
}
382
382
383
- #ifdef HAVE_ENUM_MYSQL_OPT_SSL_MODE
383
+ enum {
384
+ SSLMODE_DISABLED = 1 ,
385
+ SSLMODE_PREFERRED = 2 ,
386
+ SSLMODE_REQUIRED = 3 ,
387
+ SSLMODE_VERIFY_CA = 4 ,
388
+ SSLMODE_VERIFY_IDENTITY = 5
389
+ };
390
+
384
391
static int
385
392
_get_ssl_mode_num (char * ssl_mode )
386
393
{
@@ -395,7 +402,6 @@ _get_ssl_mode_num(char *ssl_mode)
395
402
}
396
403
return -1 ;
397
404
}
398
- #endif
399
405
400
406
static int
401
407
_mysql_ConnectionObject_Initialize (
@@ -429,6 +435,7 @@ _mysql_ConnectionObject_Initialize(
429
435
int read_timeout = 0 ;
430
436
int write_timeout = 0 ;
431
437
int compress = -1 , named_pipe = -1 , local_infile = -1 ;
438
+ int ssl_mode_num = SSLMODE_DISABLED ;
432
439
char * init_command = NULL ,
433
440
* read_default_file = NULL ,
434
441
* read_default_group = NULL ,
@@ -469,15 +476,10 @@ _mysql_ConnectionObject_Initialize(
469
476
_stringsuck (cipher , value , ssl );
470
477
}
471
478
if (ssl_mode ) {
472
- #ifdef HAVE_ENUM_MYSQL_OPT_SSL_MODE
473
- if (_get_ssl_mode_num (ssl_mode ) <= 0 ) {
479
+ if ((ssl_mode_num = _get_ssl_mode_num (ssl_mode )) <= 0 ) {
474
480
PyErr_SetString (_mysql_NotSupportedError , "Unknown ssl_mode specification" );
475
481
return -1 ;
476
482
}
477
- #else
478
- PyErr_SetString (_mysql_NotSupportedError , "MySQL client library does not support ssl_mode specification" );
479
- return -1 ;
480
- #endif
481
483
}
482
484
483
485
conn = mysql_init (& (self -> connection ));
@@ -487,6 +489,7 @@ _mysql_ConnectionObject_Initialize(
487
489
}
488
490
Py_BEGIN_ALLOW_THREADS ;
489
491
self -> open = 1 ;
492
+
490
493
if (connect_timeout ) {
491
494
unsigned int timeout = connect_timeout ;
492
495
mysql_options (& (self -> connection ), MYSQL_OPT_CONNECT_TIMEOUT ,
@@ -521,12 +524,23 @@ _mysql_ConnectionObject_Initialize(
521
524
if (ssl ) {
522
525
mysql_ssl_set (& (self -> connection ), key , cert , ca , capath , cipher );
523
526
}
524
- #ifdef HAVE_ENUM_MYSQL_OPT_SSL_MODE
525
527
if (ssl_mode ) {
526
- int ssl_mode_num = _get_ssl_mode_num ( ssl_mode );
528
+ #ifdef HAVE_ENUM_MYSQL_OPT_SSL_MODE
527
529
mysql_options (& (self -> connection ), MYSQL_OPT_SSL_MODE , & ssl_mode_num );
528
- }
530
+ #else
531
+ // MariaDB doesn't support MYSQL_OPT_SSL_MODE.
532
+ // See https://github.com/PyMySQL/mysqlclient/issues/474
533
+ // TODO: Does MariaDB supports PREFERRED and VERIFY_CA?
534
+ // We support only two levels for now.
535
+ if (sslmode_num >= SSLMODE_REQUIRED ) {
536
+ mysql_optionsv (& (self -> connection ), MYSQL_OPT_SSL_ENFORCE , (void * )& enforce_tls );
537
+ }
538
+ if (sslmode_num >= SSLMODE_VERIFY_CA ) {
539
+ mysql_optionsv (& (self -> connection ), MYSQL_OPT_SSL_VERIFY_SERVER_CERT , (void * )& enforce_tls );
540
+ }
529
541
#endif
542
+ }
543
+
530
544
if (charset ) {
531
545
mysql_options (& (self -> connection ), MYSQL_SET_CHARSET_NAME , charset );
532
546
}
0 commit comments