Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Workaround for bug CONC-320
Support SSL connections to backends, broken in MariaDB Connector C 3.0
The patch works only for OpenSSL
  • Loading branch information
renecannao committed Mar 29, 2018
1 parent b584703 commit 1b2fe33
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions deps/Makefile
Expand Up @@ -95,6 +95,7 @@ mariadb-client-library/mariadb_client/libmariadb/libmariadbclient.a:
cd mariadb-client-library/mariadb_client && patch include/mysql.h < ../mysql.h.patch
cd mariadb-client-library/mariadb_client && patch libmariadb/ma_alloc.c < ../ma_alloc.c.patch
cd mariadb-client-library/mariadb_client && patch libmariadb/ma_charset.c < ../ma_charset.c.patch
cd mariadb-client-library/mariadb_client && patch libmariadb/ma_pvio.c < ../ma_pvio.c.patch
cd mariadb-client-library/mariadb_client && patch unittest/libmariadb/basic-t.c < ../unittest_basic-t.c.patch
cd mariadb-client-library/mariadb_client && patch unittest/libmariadb/charset.c < ../unittest_charset.c.patch
cd mariadb-client-library/mariadb_client && CC=${CC} CXX=${CXX} ${MAKE}
Expand Down
135 changes: 135 additions & 0 deletions deps/mariadb-client-library/ma_pvio.c.patch
@@ -0,0 +1,135 @@
@@ -53,6 +53,10 @@
#include <ma_pvio.h>
#include <mariadb_async.h>
#include <ma_context.h>
+#include <openssl/ssl.h> /* SSL and SSL_CTX */
+#include <openssl/err.h> /* error reporting */
+#include <openssl/conf.h>
+#include <openssl/md4.h>

/* callback functions for read/write */
LIST *pvio_callback= NULL;
@@ -215,6 +219,68 @@
}
/* }}} */

+#ifdef HAVE_TLS
+/* {{{ size_t ma_pvio_tls_write_async */
+static size_t ma_pvio_tls_write_async(MARIADB_PVIO *pvio, const uchar *buffer, size_t length)
+{
+ ssize_t res= 0;
+ struct mysql_async_context *b= pvio->mysql->options.extension->async_context;
+ int ssl_err;
+
+ if (!pvio->methods->async_read)
+ {
+ PVIO_SET_ERROR(pvio->mysql, CR_ASYNC_NOT_SUPPORTED, unknown_sqlstate, 0);
+ return -1;
+ }
+
+ for (;;)
+ {
+ res = ma_pvio_tls_write(pvio->ctls, buffer, length);
+ if (res > 0) {
+ return res;
+ } else {
+ ssl_err= SSL_get_error((SSL *)pvio->ctls->ssl, res);
+ if (ssl_err == SSL_ERROR_WANT_READ)
+ b->events_to_wait_for|= MYSQL_WAIT_READ;
+ else if (ssl_err == SSL_ERROR_WANT_WRITE)
+ b->events_to_wait_for|= MYSQL_WAIT_WRITE;
+ else
+ return 1;
+ }
+ }
+}
+/* {{{ size_t ma_pvio_tls_read_async */
+static size_t ma_pvio_tls_read_async(MARIADB_PVIO *pvio, uchar *buffer, size_t length)
+{
+ ssize_t res= 0;
+ struct mysql_async_context *b= pvio->mysql->options.extension->async_context;
+ int ssl_err;
+
+ if (!pvio->methods->async_read)
+ {
+ PVIO_SET_ERROR(pvio->mysql, CR_ASYNC_NOT_SUPPORTED, unknown_sqlstate, 0);
+ return -1;
+ }
+
+ for (;;)
+ {
+ res = ma_pvio_tls_read(pvio->ctls, buffer, length);
+ if (res > 0) {
+ return res;
+ } else {
+ ssl_err= SSL_get_error((SSL *)pvio->ctls->ssl, res);
+ if (ssl_err == SSL_ERROR_WANT_READ)
+ b->events_to_wait_for|= MYSQL_WAIT_READ;
+ else if (ssl_err == SSL_ERROR_WANT_WRITE)
+ b->events_to_wait_for|= MYSQL_WAIT_WRITE;
+ else
+ return 1;
+ }
+ }
+}
+/* }}} */
+#endif
+
/* {{{ size_t ma_pvio_read */
ssize_t ma_pvio_read(MARIADB_PVIO *pvio, uchar *buffer, size_t length)
{
@@ -223,6 +289,13 @@
return -1;
if (IS_PVIO_ASYNC_ACTIVE(pvio))
{
+#ifdef HAVE_TLS
+ if (pvio->ctls)
+ {
+ r= ma_pvio_tls_read_async(pvio, buffer, length);
+ goto end;
+ }
+#endif
r= ma_pvio_read_async(pvio, buffer, length);
goto end;
}
@@ -343,17 +416,15 @@
if (!pvio)
return -1;

- /* secure connection */
-#ifdef HAVE_TLS
- if (pvio->ctls)
- {
- r= ma_pvio_tls_write(pvio->ctls, buffer, length);
- goto end;
- }
- else
-#endif
if (IS_PVIO_ASYNC_ACTIVE(pvio))
{
+#ifdef HAVE_TLS
+ if (pvio->ctls)
+ {
+ r= ma_pvio_tls_write_async(pvio, buffer, length);
+ goto end;
+ }
+#endif
r= ma_pvio_write_async(pvio, buffer, length);
goto end;
}
@@ -370,6 +441,15 @@
}
}

+ /* secure connection */
+#ifdef HAVE_TLS
+ if (pvio->ctls)
+ {
+ r= ma_pvio_tls_write(pvio->ctls, buffer, length);
+ goto end;
+ }
+#endif
+
if (pvio->methods->write)
r= pvio->methods->write(pvio, buffer, length);
end:

0 comments on commit 1b2fe33

Please sign in to comment.