@@ -50,7 +50,9 @@ static SSL_CTX *SSL_context= NULL;
50
50
51
51
static pthread_mutex_t LOCK_openssl_config ;
52
52
static pthread_mutex_t * LOCK_crypto = NULL ;
53
-
53
+ static int ma_bio_read (BIO * h , char * buf , int size );
54
+ static int ma_bio_write (BIO * h , const char * buf , int size );
55
+ static BIO_METHOD ma_BIO_methods ;
54
56
55
57
static void ma_tls_set_error (MYSQL * mysql )
56
58
{
@@ -153,6 +155,26 @@ MA_SSL_SESSION *ma_tls_get_session(MYSQL *mysql)
153
155
return NULL ;
154
156
}
155
157
158
+
159
+ static int ma_bio_read (BIO * bio , char * buf , int size )
160
+ {
161
+ MARIADB_PVIO * pvio = (MARIADB_PVIO * )bio -> ptr ;
162
+ size_t rc ;
163
+
164
+ rc = pvio -> methods -> read (pvio , buf , (size_t )size );
165
+ BIO_clear_retry_flags (bio );
166
+ return (int )rc ;
167
+ }
168
+ static int ma_bio_write (BIO * bio , const char * buf , int size )
169
+ {
170
+ MARIADB_PVIO * pvio = (MARIADB_PVIO * )bio -> ptr ;
171
+ size_t rc ;
172
+
173
+ rc = pvio -> methods -> write (pvio , buf , (size_t )size );
174
+ BIO_clear_retry_flags (bio );
175
+ return (int )rc ;
176
+ }
177
+
156
178
static int ma_tls_session_cb (SSL * ssl , SSL_SESSION * session )
157
179
{
158
180
MYSQL * mysql ;
@@ -228,7 +250,7 @@ static int ssl_thread_init()
228
250
}
229
251
#endif
230
252
231
- #ifdef _WIN32
253
+ #if defined( _WIN32 ) || !defined( DISABLE_SIGPIPE )
232
254
#define disable_sigpipe ()
233
255
#else
234
256
#include <signal.h>
@@ -305,6 +327,11 @@ int ma_tls_start(char *errmsg, size_t errmsg_len)
305
327
SSL_CTX_sess_set_remove_cb (SSL_context , ma_tls_remove_session_cb );
306
328
#endif
307
329
disable_sigpipe ();
330
+
331
+ memcpy (& ma_BIO_methods , BIO_s_socket (), sizeof (BIO_METHOD ));
332
+ ma_BIO_methods .bread = ma_bio_read ;
333
+ ma_BIO_methods .bwrite = ma_bio_write ;
334
+
308
335
rc = 0 ;
309
336
ma_tls_initialized = TRUE;
310
337
end :
@@ -487,24 +514,42 @@ void *ma_tls_init(MYSQL *mysql)
487
514
my_bool ma_tls_connect (MARIADB_TLS * ctls )
488
515
{
489
516
SSL * ssl = (SSL * )ctls -> ssl ;
490
- my_bool blocking ;
517
+ my_bool blocking , try_connect = 1 ;
491
518
MYSQL * mysql ;
492
519
MARIADB_PVIO * pvio ;
493
520
int rc ;
521
+ BIO * bio ;
494
522
495
523
mysql = (MYSQL * )SSL_get_app_data (ssl );
496
524
pvio = mysql -> net .pvio ;
497
525
498
- /* Set socket to blocking if not already set */
526
+ /* Set socket to non blocking if not already set */
499
527
if (!(blocking = pvio -> methods -> is_blocking (pvio )))
500
- pvio -> methods -> blocking (pvio , TRUE , 0 );
528
+ pvio -> methods -> blocking (pvio , FALSE , 0 );
501
529
502
530
SSL_clear (ssl );
503
- /*SSL_SESSION_set_timeout(SSL_get_session(ssl),
504
- mysql->options.connect_timeout); */
505
- SSL_set_fd (ssl , mysql_get_socket (mysql ));
506
531
507
- if (SSL_connect (ssl ) != 1 )
532
+ bio = BIO_new (& ma_BIO_methods );
533
+ bio -> ptr = pvio ;
534
+ SSL_set_bio (ssl , bio , bio );
535
+ BIO_set_fd (bio , mysql_get_socket (mysql ), BIO_NOCLOSE );
536
+
537
+ while (try_connect && (rc = SSL_connect (ssl )) == -1 )
538
+ {
539
+ switch (SSL_get_error (ssl , rc )) {
540
+ case SSL_ERROR_WANT_READ :
541
+ if (pvio -> methods -> wait_io_or_timeout (pvio , TRUE, mysql -> options .connect_timeout ) < 1 )
542
+ try_connect = 0 ;
543
+ break ;
544
+ case SSL_ERROR_WANT_WRITE :
545
+ if (pvio -> methods -> wait_io_or_timeout (pvio , TRUE, mysql -> options .connect_timeout ) < 1 )
546
+ try_connect = 0 ;
547
+ break ;
548
+ default :
549
+ try_connect = 0 ;
550
+ }
551
+ }
552
+ if (rc != 1 )
508
553
{
509
554
ma_tls_set_error (mysql );
510
555
/* restore blocking mode */
@@ -683,7 +728,7 @@ my_bool ma_tls_get_protocol_version(MARIADB_TLS *ctls, struct st_ssl_version *ve
683
728
return 1 ;
684
729
685
730
ssl = (SSL * )ctls -> ssl ;
686
- version -> iversion = SSL_version (ssl );
731
+ version -> iversion = SSL_version (ssl ) - TLS1_VERSION ;
687
732
version -> cversion = ssl_protocol_version [version -> iversion ];
688
733
return 0 ;
689
734
}
0 commit comments