Skip to content

Commit ba0ed07

Browse files
committed
Implemented read/write bio functions, so we don't need sigpipe handler anymore.
1 parent da38af3 commit ba0ed07

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

libmariadb/ma_tls.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
my_bool ma_tls_initialized= FALSE;
5252
unsigned int mariadb_deinitialize_ssl= 1;
5353

54-
char *ssl_protocol_version[5]= {"unknown", "SSL3", "TLS1.0", "TLS1.1", "TLS1.2"};
54+
char *ssl_protocol_version[5]= {"TLS1.0", "TLS1.1", "TLS1.2"};
5555

5656
MARIADB_TLS *ma_pvio_tls_init(MYSQL *mysql)
5757
{

libmariadb/secure/openssl.c

+55-10
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ static SSL_CTX *SSL_context= NULL;
5050

5151
static pthread_mutex_t LOCK_openssl_config;
5252
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;
5456

5557
static void ma_tls_set_error(MYSQL *mysql)
5658
{
@@ -153,6 +155,26 @@ MA_SSL_SESSION *ma_tls_get_session(MYSQL *mysql)
153155
return NULL;
154156
}
155157

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+
156178
static int ma_tls_session_cb(SSL *ssl, SSL_SESSION *session)
157179
{
158180
MYSQL *mysql;
@@ -228,7 +250,7 @@ static int ssl_thread_init()
228250
}
229251
#endif
230252

231-
#ifdef _WIN32
253+
#if defined(_WIN32) || !defined(DISABLE_SIGPIPE)
232254
#define disable_sigpipe()
233255
#else
234256
#include <signal.h>
@@ -305,6 +327,11 @@ int ma_tls_start(char *errmsg, size_t errmsg_len)
305327
SSL_CTX_sess_set_remove_cb(SSL_context, ma_tls_remove_session_cb);
306328
#endif
307329
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+
308335
rc= 0;
309336
ma_tls_initialized= TRUE;
310337
end:
@@ -487,24 +514,42 @@ void *ma_tls_init(MYSQL *mysql)
487514
my_bool ma_tls_connect(MARIADB_TLS *ctls)
488515
{
489516
SSL *ssl = (SSL *)ctls->ssl;
490-
my_bool blocking;
517+
my_bool blocking, try_connect= 1;
491518
MYSQL *mysql;
492519
MARIADB_PVIO *pvio;
493520
int rc;
521+
BIO *bio;
494522

495523
mysql= (MYSQL *)SSL_get_app_data(ssl);
496524
pvio= mysql->net.pvio;
497525

498-
/* Set socket to blocking if not already set */
526+
/* Set socket to non blocking if not already set */
499527
if (!(blocking= pvio->methods->is_blocking(pvio)))
500-
pvio->methods->blocking(pvio, TRUE, 0);
528+
pvio->methods->blocking(pvio, FALSE, 0);
501529

502530
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));
506531

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)
508553
{
509554
ma_tls_set_error(mysql);
510555
/* restore blocking mode */
@@ -683,7 +728,7 @@ my_bool ma_tls_get_protocol_version(MARIADB_TLS *ctls, struct st_ssl_version *ve
683728
return 1;
684729

685730
ssl = (SSL *)ctls->ssl;
686-
version->iversion= SSL_version(ssl);
731+
version->iversion= SSL_version(ssl) - TLS1_VERSION;
687732
version->cversion= ssl_protocol_version[version->iversion];
688733
return 0;
689734
}

0 commit comments

Comments
 (0)