jjanco / rpms / mariadb

Forked from rpms/mariadb 6 years ago
Clone
39a24f0
From b247fc0fb2324d0252f1f46281f9887eeb3d7925 Mon Sep 17 00:00:00 2001
39a24f0
From: Sergei Golubchik <serg@mariadb.org>
39a24f0
Date: Wed, 3 May 2017 21:22:59 +0200
39a24f0
Subject: [PATCH 2/5] MDEV-10332  support for OpenSSL 1.1 and LibreSSL
39a24f0
39a24f0
post-review fixes:
39a24f0
* move all ssl implementation related ifdefs/defines to one file
39a24f0
  (ssl_compat.h)
39a24f0
* work around OpenSSL-1.1 desire to malloc every EVP context by
39a24f0
  run-time checking that context allocated on the stack is big enough
39a24f0
  (openssl.c)
39a24f0
* use newer version of the AWS SDK for OpenSSL 1.1
39a24f0
* use get_dh2048() function as generated by openssl 1.1
39a24f0
  (viosslfactories.c)
39a24f0
---
39a24f0
 include/my_crypt.h             |  15 ------
39a24f0
 include/ssl_compat.h           |  75 ++++++++++++++++++++++++++++
39a24f0
 include/violite.h              |  12 -----
39a24f0
 mysql-test/mysql-test-run.pl   |   2 +-
39a24f0
 mysql-test/t/openssl_6975.test |   7 ++-
39a24f0
 mysql-test/t/ssl_8k_key.test   |   5 +-
39a24f0
 mysys_ssl/CMakeLists.txt       |   1 +
39a24f0
 mysys_ssl/my_crypt.cc          | 102 +++++++++++++++-----------------------
39a24f0
 mysys_ssl/my_md5.cc            |  85 +++++++++++---------------------
39a24f0
 mysys_ssl/openssl.c            |  71 +++++++++++++++++++++++++++
39a24f0
 mysys_ssl/yassl.cc             |  19 --------
39a24f0
 sql-common/client.c            |   8 +--
39a24f0
 sql/mysqld.cc                  |  49 +++++++++----------
39a24f0
 sql/slave.cc                   |  19 +-------
39a24f0
 vio/vio.c                      |   1 +
39a24f0
 vio/viosslfactories.c          | 108 ++++++++++++++++++-----------------------
39a24f0
 16 files changed, 295 insertions(+), 284 deletions(-)
39a24f0
 create mode 100644 include/ssl_compat.h
39a24f0
 create mode 100644 mysys_ssl/openssl.c
39a24f0
39a24f0
diff --git a/include/my_crypt.h b/include/my_crypt.h
39a24f0
index e7dd9d80100..719e349bfb9 100644
39a24f0
--- a/include/my_crypt.h
39a24f0
+++ b/include/my_crypt.h
39a24f0
@@ -21,19 +21,4 @@
39a24f0
 #include <my_config.h> /* HAVE_EncryptAes128{Ctr,Gcm} */
39a24f0
 #include <mysql/service_my_crypt.h>
39a24f0
 
39a24f0
-/* OpenSSL version specific definitions */
39a24f0
-#if !defined(HAVE_YASSL) && defined(OPENSSL_VERSION_NUMBER)
39a24f0
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
39a24f0
-#define ERR_remove_state(X)
39a24f0
-#else
39a24f0
-#define EVP_CIPHER_CTX_reset(X) EVP_CIPHER_CTX_cleanup(X)
39a24f0
-#define RAND_OpenSSL() RAND_SSLeay();
39a24f0
-#if defined(HAVE_ERR_remove_thread_state)
39a24f0
-#define ERR_remove_state(X) ERR_remove_thread_state(NULL)
39a24f0
-#endif
39a24f0
-#endif
39a24f0
-#elif defined(HAVE_YASSL)
39a24f0
-#define EVP_CIPHER_CTX_reset(X) EVP_CIPHER_CTX_cleanup(X)
39a24f0
-#endif /* !defined(HAVE_YASSL) */
39a24f0
-
39a24f0
 #endif /* MY_CRYPT_INCLUDED */
39a24f0
diff --git a/include/ssl_compat.h b/include/ssl_compat.h
39a24f0
new file mode 100644
39a24f0
index 00000000000..b0e3ed497cd
39a24f0
--- /dev/null
39a24f0
+++ b/include/ssl_compat.h
39a24f0
@@ -0,0 +1,75 @@
39a24f0
+/*
39a24f0
+ Copyright (c) 2016, 2017 MariaDB Corporation
39a24f0
+
39a24f0
+ This program is free software; you can redistribute it and/or modify
39a24f0
+ it under the terms of the GNU General Public License as published by
39a24f0
+ the Free Software Foundation; version 2 of the License.
39a24f0
+
39a24f0
+ This program is distributed in the hope that it will be useful,
39a24f0
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
39a24f0
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39a24f0
+ GNU General Public License for more details.
39a24f0
+
39a24f0
+ You should have received a copy of the GNU General Public License
39a24f0
+ along with this program; if not, write to the Free Software
39a24f0
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
39a24f0
+
39a24f0
+#include <openssl/opensslv.h>
39a24f0
+
39a24f0
+/* OpenSSL version specific definitions */
39a24f0
+#if !defined(HAVE_YASSL) && defined(OPENSSL_VERSION_NUMBER)
39a24f0
+
39a24f0
+#if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER)
39a24f0
+#define HAVE_X509_check_host 1
39a24f0
+#endif
39a24f0
+
39a24f0
+#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
39a24f0
+#define HAVE_OPENSSL11 1
39a24f0
+#define ERR_remove_state(X) ERR_clear_error()
39a24f0
+#define EVP_MD_CTX_cleanup(X) EVP_MD_CTX_reset(X)
39a24f0
+#define EVP_CIPHER_CTX_SIZE 168
39a24f0
+#define EVP_MD_CTX_SIZE 48
39a24f0
+#undef EVP_MD_CTX_init
39a24f0
+#define EVP_MD_CTX_init(X) do { bzero((X), EVP_MD_CTX_SIZE); EVP_MD_CTX_reset(X); } while(0)
39a24f0
+#undef EVP_CIPHER_CTX_init
39a24f0
+#define EVP_CIPHER_CTX_init(X) do { bzero((X), EVP_CIPHER_CTX_SIZE); EVP_CIPHER_CTX_reset(X); } while(0)
39a24f0
+
39a24f0
+#else
39a24f0
+#define HAVE_OPENSSL10 1
39a24f0
+/*
39a24f0
+  Unfortunately RAND_bytes manual page does not provide any guarantees
39a24f0
+  in relation to blocking behavior. Here we explicitly use SSLeay random
39a24f0
+  instead of whatever random engine is currently set in OpenSSL. That way
39a24f0
+  we are guaranteed to have a non-blocking random.
39a24f0
+*/
39a24f0
+#define RAND_OpenSSL() RAND_SSLeay()
39a24f0
+
39a24f0
+#ifdef HAVE_ERR_remove_thread_state
39a24f0
+#define ERR_remove_state(X) ERR_remove_thread_state(NULL)
39a24f0
+#endif /* HAVE_ERR_remove_thread_state */
39a24f0
+
39a24f0
+#endif /* HAVE_OPENSSL11 */
39a24f0
+
39a24f0
+#elif defined(HAVE_YASSL)
39a24f0
+#define BN_free(X) do { } while(0)
39a24f0
+#endif /* !defined(HAVE_YASSL) */
39a24f0
+
39a24f0
+#ifndef HAVE_OPENSSL11
39a24f0
+#define ASN1_STRING_get0_data(X)        ASN1_STRING_data(X)
39a24f0
+#define OPENSSL_init_ssl(X,Y)           SSL_library_init()
39a24f0
+#define DH_set0_pqg(D,P,Q,G)            ((D)->p= (P), (D)->g= (G))
39a24f0
+#define EVP_CIPHER_CTX_buf_noconst(ctx) ((ctx)->buf)
39a24f0
+#define EVP_CIPHER_CTX_encrypting(ctx)  ((ctx)->encrypt)
39a24f0
+#define EVP_CIPHER_CTX_SIZE             sizeof(EVP_CIPHER_CTX)
39a24f0
+#define EVP_MD_CTX_SIZE                 sizeof(EVP_MD_CTX)
39a24f0
+#endif
39a24f0
+
39a24f0
+#ifdef	__cplusplus
39a24f0
+extern "C" {
39a24f0
+#endif /* __cplusplus */
39a24f0
+
39a24f0
+int check_openssl_compatibility();
39a24f0
+
39a24f0
+#ifdef	__cplusplus
39a24f0
+}
39a24f0
+#endif
39a24f0
diff --git a/include/violite.h b/include/violite.h
39a24f0
index 23800696e5a..572d4741c80 100644
39a24f0
--- a/include/violite.h
39a24f0
+++ b/include/violite.h
39a24f0
@@ -123,13 +123,6 @@ int vio_getnameinfo(const struct sockaddr *sa,
39a24f0
                     int flags);
39a24f0
 
39a24f0
 #ifdef HAVE_OPENSSL
39a24f0
-#include <openssl/opensslv.h>
39a24f0
-#if OPENSSL_VERSION_NUMBER < 0x0090700f
39a24f0
-#define DES_cblock des_cblock
39a24f0
-#define DES_key_schedule des_key_schedule
39a24f0
-#define DES_set_key_unchecked(k,ks) des_set_key_unchecked((k),*(ks))
39a24f0
-#define DES_ede3_cbc_encrypt(i,o,l,k1,k2,k3,iv,e) des_ede3_cbc_encrypt((i),(o),(l),*(k1),*(k2),*(k3),(iv),(e))
39a24f0
-#endif
39a24f0
 /* apple deprecated openssl in MacOSX Lion */
39a24f0
 #ifdef __APPLE__
39a24f0
 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
39a24f0
@@ -146,11 +139,6 @@ typedef my_socket YASSL_SOCKET_T;
39a24f0
 #include <openssl/ssl.h>
39a24f0
 #include <openssl/err.h>
39a24f0
 
39a24f0
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
39a24f0
-#define ERR_remove_state(X)
39a24f0
-#elif defined(HAVE_ERR_remove_thread_state)
39a24f0
-#define ERR_remove_state(X) ERR_remove_thread_state(NULL)
39a24f0
-#endif
39a24f0
 enum enum_ssl_init_error
39a24f0
 {
39a24f0
   SSL_INITERR_NOERROR= 0, SSL_INITERR_CERT, SSL_INITERR_KEY,
39a24f0
diff --git a/mysql-test/mysql-test-run.pl b/mysql-test/mysql-test-run.pl
39a24f0
index d53a8180c22..22bcabab7b6 100755
39a24f0
--- a/mysql-test/mysql-test-run.pl
39a24f0
+++ b/mysql-test/mysql-test-run.pl
39a24f0
@@ -2300,7 +2300,7 @@ sub environment_setup {
39a24f0
   $ENV{'MYSQL_PLUGIN'}=             $exe_mysql_plugin;
39a24f0
   $ENV{'MYSQL_EMBEDDED'}=           $exe_mysql_embedded;
39a24f0
 
39a24f0
-  my $client_config_exe= 
39a24f0
+  my $client_config_exe=
39a24f0
     native_path("$bindir/libmariadb/mariadb_config$opt_vs_config/mariadb_config");
39a24f0
   my $tls_info= `$client_config_exe --tlsinfo`;
39a24f0
   ($ENV{CLIENT_TLS_LIBRARY},$ENV{CLIENT_TLS_LIBRARY_VERSION})=
39a24f0
diff --git a/mysql-test/t/openssl_6975.test b/mysql-test/t/openssl_6975.test
39a24f0
index 6cf5d82cf54..6a82d013fb6 100644
39a24f0
--- a/mysql-test/t/openssl_6975.test
39a24f0
+++ b/mysql-test/t/openssl_6975.test
39a24f0
@@ -19,9 +19,8 @@ let $mysql=$MYSQL --ssl-key=$MYSQL_TEST_DIR/std_data/client-key.pem --ssl-cert=$
39a24f0
 disable_abort_on_error;
39a24f0
 echo TLS1.2 ciphers: user is ok with any cipher;
39a24f0
 exec $mysql                  --ssl-cipher=AES128-SHA256;
39a24f0
---replace_result DHE-RSA-CHACHA20-POLY1305 DHE-RSA-AES256-GCM-SHA384
39a24f0
---replace_result ECDHE-RSA-AES256-GCM-SHA384 DHE-RSA-AES256-GCM-SHA384
39a24f0
-exec $mysql                  --ssl-cipher=TLSv1.2
39a24f0
+--replace_result DHE-RSA-CHACHA20-POLY1305 DHE-RSA-AES256-GCM-SHA384 ECDHE-RSA-AES256-GCM-SHA384 DHE-RSA-AES256-GCM-SHA384
39a24f0
+exec $mysql                  --ssl-cipher=TLSv1.2;
39a24f0
 echo TLS1.2 ciphers: user requires SSLv3 cipher AES128-SHA;
39a24f0
 exec $mysql --user ssl_sslv3 --ssl-cipher=AES128-SHA256;
39a24f0
 exec $mysql --user ssl_sslv3 --ssl-cipher=TLSv1.2;
39a24f0
@@ -31,7 +30,7 @@ exec $mysql --user ssl_tls12 --ssl-cipher=TLSv1.2;
39a24f0
 
39a24f0
 echo SSLv3 ciphers: user is ok with any cipher;
39a24f0
 exec $mysql                  --ssl-cipher=AES256-SHA;
39a24f0
-exec $mysql                  --ssl-cipher=DHE-RSA-AES256-SHA
39a24f0
+exec $mysql                  --ssl-cipher=SSLv3;
39a24f0
 echo SSLv3 ciphers: user requires SSLv3 cipher AES128-SHA;
39a24f0
 exec $mysql --user ssl_sslv3 --ssl-cipher=AES128-SHA;
39a24f0
 exec $mysql --user ssl_sslv3 --ssl-cipher=SSLv3;
39a24f0
diff --git a/mysql-test/t/ssl_8k_key.test b/mysql-test/t/ssl_8k_key.test
39a24f0
index 470d577edb8..9d5b382726e 100644
39a24f0
--- a/mysql-test/t/ssl_8k_key.test
39a24f0
+++ b/mysql-test/t/ssl_8k_key.test
39a24f0
@@ -1,6 +1,5 @@
39a24f0
-# This test should work in embedded server after we fix mysqltest
39a24f0
--- source include/require_openssl_client.inc
39a24f0
--- source include/not_embedded.inc
39a24f0
+# schannel does not support keys longer than 4k
39a24f0
+-- source include/not_windows.inc
39a24f0
 
39a24f0
 -- source include/have_ssl_communication.inc
39a24f0
 #
39a24f0
diff --git a/mysys_ssl/CMakeLists.txt b/mysys_ssl/CMakeLists.txt
39a24f0
index 4f6f7458c5b..f8a767ed6f3 100644
39a24f0
--- a/mysys_ssl/CMakeLists.txt
39a24f0
+++ b/mysys_ssl/CMakeLists.txt
39a24f0
@@ -28,6 +28,7 @@ SET(MYSYS_SSL_HIDDEN_SOURCES
39a24f0
     my_sha384.cc
39a24f0
     my_sha512.cc
39a24f0
     my_md5.cc
39a24f0
+    openssl.c
39a24f0
    )
39a24f0
 
39a24f0
 SET(MYSYS_SSL_SOURCES
39a24f0
diff --git a/mysys_ssl/my_crypt.cc b/mysys_ssl/my_crypt.cc
39a24f0
index 0ff49a2c427..ed1c82dbac6 100644
39a24f0
--- a/mysys_ssl/my_crypt.cc
39a24f0
+++ b/mysys_ssl/my_crypt.cc
39a24f0
@@ -1,6 +1,6 @@
39a24f0
 /*
39a24f0
  Copyright (c) 2014 Google Inc.
39a24f0
- Copyright (c) 2014, 2015 MariaDB Corporation
39a24f0
+ Copyright (c) 2014, 2017 MariaDB Corporation
39a24f0
 
39a24f0
  This program is free software; you can redistribute it and/or modify
39a24f0
  it under the terms of the GNU General Public License as published by
39a24f0
@@ -21,30 +21,31 @@
39a24f0
 #ifdef HAVE_YASSL
39a24f0
 #include "yassl.cc"
39a24f0
 #else
39a24f0
-
39a24f0
 #include <openssl/evp.h>
39a24f0
 #include <openssl/aes.h>
39a24f0
 #include <openssl/err.h>
39a24f0
 #include <openssl/rand.h>
39a24f0
-
39a24f0
 #endif
39a24f0
-#include <my_crypt.h>
39a24f0
 
39a24f0
-#define MY_CIPHER_CTX_SIZE 384
39a24f0
+#include <my_crypt.h>
39a24f0
+#include <ssl_compat.h>
39a24f0
 
39a24f0
 class MyCTX
39a24f0
 {
39a24f0
 public:
39a24f0
+  char ctx_buf[EVP_CIPHER_CTX_SIZE];
39a24f0
   EVP_CIPHER_CTX *ctx;
39a24f0
-  const uchar *key;
39a24f0
-  unsigned int klen;
39a24f0
-  MyCTX() {
39a24f0
-            ctx= EVP_CIPHER_CTX_new();
39a24f0
-          }
39a24f0
-  virtual ~MyCTX() {
39a24f0
-                     EVP_CIPHER_CTX_free(ctx);
39a24f0
-                     ERR_remove_state(0);
39a24f0
-                    }
39a24f0
+
39a24f0
+  MyCTX()
39a24f0
+  {
39a24f0
+    ctx= (EVP_CIPHER_CTX *)ctx_buf;
39a24f0
+    EVP_CIPHER_CTX_init(ctx);
39a24f0
+  }
39a24f0
+  virtual ~MyCTX()
39a24f0
+  {
39a24f0
+    EVP_CIPHER_CTX_cleanup(ctx);
39a24f0
+    ERR_remove_state(0);
39a24f0
+  }
39a24f0
 
39a24f0
   virtual int init(const EVP_CIPHER *cipher, int encrypt, const uchar *key,
39a24f0
                    uint klen, const uchar *iv, uint ivlen)
39a24f0
@@ -78,9 +79,12 @@ class MyCTX
39a24f0
 class MyCTX_nopad : public MyCTX
39a24f0
 {
39a24f0
 public:
39a24f0
+  const uchar *key;
39a24f0
+  uint klen, buf_len;
39a24f0
+  uchar oiv[MY_AES_BLOCK_SIZE];
39a24f0
+
39a24f0
   MyCTX_nopad() : MyCTX() { }
39a24f0
   ~MyCTX_nopad() { }
39a24f0
-  unsigned int buf_len;
39a24f0
 
39a24f0
   int init(const EVP_CIPHER *cipher, int encrypt, const uchar *key, uint klen,
39a24f0
            const uchar *iv, uint ivlen)
39a24f0
@@ -89,19 +93,8 @@ class MyCTX_nopad : public MyCTX
39a24f0
     this->key= key;
39a24f0
     this->klen= klen;
39a24f0
     this->buf_len= 0;
39a24f0
-    /* FIX-ME:
39a24f0
-       For the sake of backward compatibility we do some strange hack here:
39a24f0
-       Since ECB doesn't need an IV (and therefore is considered kind of
39a24f0
-       insecure) we need to store the specified iv.
39a24f0
-       The last nonpadding block will be encrypted with an additional
39a24f0
-       expensive crypt_call in ctr mode instead
39a24f0
-       of encrypting the entire plain text in ctr-mode */
39a24f0
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
39a24f0
-    const unsigned char *oiv= EVP_CIPHER_CTX_original_iv(ctx);
39a24f0
-#else
39a24f0
-    const unsigned char *oiv= ctx->oiv;
39a24f0
-#endif
39a24f0
-    memcpy((char *)oiv, iv, ivlen);
39a24f0
+    memcpy(oiv, iv, ivlen);
39a24f0
+    DBUG_ASSERT(ivlen == 0 || ivlen == sizeof(oiv));
39a24f0
 
39a24f0
     int res= MyCTX::init(cipher, encrypt, key, klen, iv, ivlen);
39a24f0
 
39a24f0
@@ -111,34 +104,30 @@ class MyCTX_nopad : public MyCTX
39a24f0
 
39a24f0
   int update(const uchar *src, uint slen, uchar *dst, uint *dlen)
39a24f0
   {
39a24f0
-    buf_len= slen % MY_AES_BLOCK_SIZE;
39a24f0
+    buf_len+= slen;
39a24f0
     return MyCTX::update(src, slen, dst, dlen);
39a24f0
   }
39a24f0
 
39a24f0
   int finish(uchar *dst, uint *dlen)
39a24f0
   {
39a24f0
+    buf_len %= MY_AES_BLOCK_SIZE;
39a24f0
     if (buf_len)
39a24f0
     {
39a24f0
-      const uchar *org_iv;
39a24f0
-      unsigned char *buf;
39a24f0
+      uchar *buf= EVP_CIPHER_CTX_buf_noconst(ctx);
39a24f0
       /*
39a24f0
         Not much we can do, block ciphers cannot encrypt data that aren't
39a24f0
         a multiple of the block length. At least not without padding.
39a24f0
         Let's do something CTR-like for the last partial block.
39a24f0
+
39a24f0
+        NOTE this assumes that there are only buf_len bytes in the buf.
39a24f0
+        If OpenSSL will change that, we'll need to change the implementation
39a24f0
+        of this class too.
39a24f0
       */
39a24f0
       uchar mask[MY_AES_BLOCK_SIZE];
39a24f0
       uint mlen;
39a24f0
 
39a24f0
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
39a24f0
-      org_iv= EVP_CIPHER_CTX_original_iv(ctx);
39a24f0
-      buf= EVP_CIPHER_CTX_buf_noconst(ctx);
39a24f0
-#else
39a24f0
-      org_iv= ctx->oiv;
39a24f0
-      buf= ctx->buf;
39a24f0
-#endif
39a24f0
-
39a24f0
       my_aes_crypt(MY_AES_ECB, ENCRYPTION_FLAG_ENCRYPT | ENCRYPTION_FLAG_NOPAD,
39a24f0
-                   org_iv, sizeof(mask), mask, &mlen, key, klen, 0, 0);
39a24f0
+                   oiv, sizeof(mask), mask, &mlen, key, klen, 0, 0);
39a24f0
       DBUG_ASSERT(mlen == sizeof(mask));
39a24f0
 
39a24f0
       for (uint i=0; i < buf_len; i++)
39a24f0
@@ -178,9 +167,8 @@ make_aes_dispatcher(gcm)
39a24f0
 class MyCTX_gcm : public MyCTX
39a24f0
 {
39a24f0
 public:
39a24f0
-  const uchar *aad= NULL;
39a24f0
+  const uchar *aad;
39a24f0
   int aadlen;
39a24f0
-  my_bool encrypt;
39a24f0
   MyCTX_gcm() : MyCTX() { }
39a24f0
   ~MyCTX_gcm() { }
39a24f0
 
39a24f0
@@ -192,7 +180,6 @@ class MyCTX_gcm : public MyCTX
39a24f0
     int real_ivlen= EVP_CIPHER_CTX_iv_length(ctx);
39a24f0
     aad= iv + real_ivlen;
39a24f0
     aadlen= ivlen - real_ivlen;
39a24f0
-    this->encrypt= encrypt;
39a24f0
     return res;
39a24f0
   }
39a24f0
 
39a24f0
@@ -204,14 +191,14 @@ class MyCTX_gcm : public MyCTX
39a24f0
       before decrypting the data. it can encrypt data piecewise, like, first
39a24f0
       half, then the second half, but it must decrypt all at once
39a24f0
     */
39a24f0
-    if (!this->encrypt)
39a24f0
+    if (!EVP_CIPHER_CTX_encrypting(ctx))
39a24f0
     {
39a24f0
       slen-= MY_AES_BLOCK_SIZE;
39a24f0
       if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, MY_AES_BLOCK_SIZE,
39a24f0
                               (void*)(src + slen)))
39a24f0
         return MY_AES_OPENSSL_ERROR;
39a24f0
     }
39a24f0
-    int unused= 0;
39a24f0
+    int unused;
39a24f0
     if (aadlen && !EVP_CipherUpdate(ctx, NULL, &unused, aad, aadlen))
39a24f0
       return MY_AES_OPENSSL_ERROR;
39a24f0
     aadlen= 0;
39a24f0
@@ -220,12 +207,12 @@ class MyCTX_gcm : public MyCTX
39a24f0
 
39a24f0
   int finish(uchar *dst, uint *dlen)
39a24f0
   {
39a24f0
-    int fin= 0;
39a24f0
+    int fin;
39a24f0
     if (!EVP_CipherFinal_ex(ctx, dst, &fin))
39a24f0
       return MY_AES_BAD_DATA;
39a24f0
     DBUG_ASSERT(fin == 0);
39a24f0
 
39a24f0
-    if (this->encrypt)
39a24f0
+    if (EVP_CIPHER_CTX_encrypting(ctx))
39a24f0
     {
39a24f0
       if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, MY_AES_BLOCK_SIZE, dst))
39a24f0
         return MY_AES_OPENSSL_ERROR;
39a24f0
@@ -295,20 +282,15 @@ int my_aes_crypt(enum my_aes_mode mode, int flags,
39a24f0
 {
39a24f0
   void *ctx= alloca(MY_AES_CTX_SIZE);
39a24f0
   int res1, res2;
39a24f0
-  uint d1= 0, d2= 0;
39a24f0
+  uint d1= 0, d2;
39a24f0
   if ((res1= my_aes_crypt_init(ctx, mode, flags, key, klen, iv, ivlen)))
39a24f0
     return res1;
39a24f0
   res1= my_aes_crypt_update(ctx, src, slen, dst, &d1;;
39a24f0
   res2= my_aes_crypt_finish(ctx, dst + d1, &d2;;
39a24f0
-  *dlen= d1 + d2;
39a24f0
-  /* in case of failure clear error queue */
39a24f0
-#ifndef HAVE_YASSL
39a24f0
-  /* since we don't check the crypto error messages we need to
39a24f0
-     clear the error queue - otherwise subsequent crypto or tls/ssl
39a24f0
-     calls will fail */
39a24f0
-  if (!*dlen)
39a24f0
-    ERR_clear_error();
39a24f0
-#endif
39a24f0
+  if (res1 || res2)
39a24f0
+    ERR_remove_state(0); /* in case of failure clear error queue */
39a24f0
+  else
39a24f0
+    *dlen= d1 + d2;
39a24f0
   return res1 ? res1 : res2;
39a24f0
 }
39a24f0
 
39a24f0
@@ -350,12 +332,6 @@ int my_random_bytes(uchar* buf, int num)
39a24f0
 
39a24f0
 int my_random_bytes(uchar *buf, int num)
39a24f0
 {
39a24f0
-  /*
39a24f0
-    Unfortunately RAND_bytes manual page does not provide any guarantees
39a24f0
-    in relation to blocking behavior. Here we explicitly use SSLeay random
39a24f0
-    instead of whatever random engine is currently set in OpenSSL. That way
39a24f0
-    we are guaranteed to have a non-blocking random.
39a24f0
-  */
39a24f0
   RAND_METHOD *rand = RAND_OpenSSL();
39a24f0
   if (rand == NULL || rand->bytes(buf, num) != 1)
39a24f0
     return MY_AES_OPENSSL_ERROR;
39a24f0
diff --git a/mysys_ssl/my_md5.cc b/mysys_ssl/my_md5.cc
39a24f0
index 02c01dd7148..0105082b7e1 100644
39a24f0
--- a/mysys_ssl/my_md5.cc
39a24f0
+++ b/mysys_ssl/my_md5.cc
39a24f0
@@ -1,5 +1,5 @@
39a24f0
 /* Copyright (c) 2012, Oracle and/or its affiliates.
39a24f0
-   Copyright (c) 2014, SkySQL Ab.
39a24f0
+   Copyright (c) 2017, MariaDB Corporation
39a24f0
 
39a24f0
    This program is free software; you can redistribute it and/or modify
39a24f0
    it under the terms of the GNU General Public License as published by
39a24f0
@@ -27,50 +27,34 @@
39a24f0
 #include <my_md5.h>
39a24f0
 #include <stdarg.h>
39a24f0
 
39a24f0
-#define MA_HASH_CTX_SIZE 512
39a24f0
-
39a24f0
 #if defined(HAVE_YASSL)
39a24f0
 #include "md5.hpp"
39a24f0
+#include <ssl_compat.h>
39a24f0
 
39a24f0
-typedef TaoCrypt::MD5 MD5_CONTEXT;
39a24f0
+typedef TaoCrypt::MD5 EVP_MD_CTX;
39a24f0
 
39a24f0
-static void md5_init(MD5_CONTEXT *context)
39a24f0
+static void md5_init(EVP_MD_CTX *context)
39a24f0
 {
39a24f0
-  context= new(context) MD5_CONTEXT;
39a24f0
+  context= new(context) EVP_MD_CTX;
39a24f0
   context->Init();
39a24f0
 }
39a24f0
 
39a24f0
-/*
39a24f0
-  this is a variant of md5_init to be used in this file only.
39a24f0
-  does nothing for yassl, because the context's constructor was called automatically.
39a24f0
-*/
39a24f0
-static void md5_init_fast(MD5_CONTEXT *context)
39a24f0
-{
39a24f0
-}
39a24f0
-
39a24f0
-static void md5_input(MD5_CONTEXT *context, const uchar *buf, unsigned len)
39a24f0
+static void md5_input(EVP_MD_CTX *context, const uchar *buf, unsigned len)
39a24f0
 {
39a24f0
   context->Update((const TaoCrypt::byte *) buf, len);
39a24f0
 }
39a24f0
 
39a24f0
-static void md5_result(MD5_CONTEXT *context, uchar digest[MD5_HASH_SIZE])
39a24f0
+static void md5_result(EVP_MD_CTX *context, uchar digest[MD5_HASH_SIZE])
39a24f0
 {
39a24f0
     context->Final((TaoCrypt::byte *) digest);
39a24f0
 }
39a24f0
 
39a24f0
 #elif defined(HAVE_OPENSSL)
39a24f0
-
39a24f0
-
39a24f0
 #include <openssl/evp.h>
39a24f0
+#include <ssl_compat.h>
39a24f0
 
39a24f0
-#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
39a24f0
-#define EVP_MD_CTX_reset(X) EVP_MD_CTX_cleanup(X)
39a24f0
-#endif
39a24f0
-typedef EVP_MD_CTX MD5_CONTEXT;
39a24f0
-
39a24f0
-static void md5_init(MD5_CONTEXT *context)
39a24f0
+static void md5_init(EVP_MD_CTX *context)
39a24f0
 {
39a24f0
-  memset(context, 0, my_md5_context_size());
39a24f0
   EVP_MD_CTX_init(context);
39a24f0
 #ifdef EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
39a24f0
   /* Ok to ignore FIPS: MD5 is not used for crypto here */
39a24f0
@@ -79,20 +63,15 @@ static void md5_init(MD5_CONTEXT *context)
39a24f0
   EVP_DigestInit_ex(context, EVP_md5(), NULL);
39a24f0
 }
39a24f0
 
39a24f0
-static void md5_init_fast(MD5_CONTEXT *context)
39a24f0
-{
39a24f0
-  md5_init(context);
39a24f0
-}
39a24f0
-
39a24f0
-static void md5_input(MD5_CONTEXT *context, const uchar *buf, unsigned len)
39a24f0
+static void md5_input(EVP_MD_CTX *context, const uchar *buf, unsigned len)
39a24f0
 {
39a24f0
   EVP_DigestUpdate(context, buf, len);
39a24f0
 }
39a24f0
 
39a24f0
-static void md5_result(MD5_CONTEXT *context, uchar digest[MD5_HASH_SIZE])
39a24f0
+static void md5_result(EVP_MD_CTX *context, uchar digest[MD5_HASH_SIZE])
39a24f0
 {
39a24f0
   EVP_DigestFinal_ex(context, digest, NULL);
39a24f0
-  EVP_MD_CTX_reset(context);
39a24f0
+  EVP_MD_CTX_cleanup(context);
39a24f0
 }
39a24f0
 
39a24f0
 #endif /* HAVE_YASSL */
39a24f0
@@ -108,26 +87,23 @@ static void md5_result(MD5_CONTEXT *context, uchar digest[MD5_HASH_SIZE])
39a24f0
 */
39a24f0
 void my_md5(uchar *digest, const char *buf, size_t len)
39a24f0
 {
39a24f0
-#ifdef HAVE_YASSL
39a24f0
-  MD5_CONTEXT md5_context;
39a24f0
-#else
39a24f0
-  unsigned char md5_context[MA_HASH_CTX_SIZE];
39a24f0
-#endif
39a24f0
-  md5_init_fast((MD5_CONTEXT *)&md5_context);
39a24f0
-  md5_input((MD5_CONTEXT *)&md5_context, (const uchar *)buf, len);
39a24f0
-  md5_result((MD5_CONTEXT *)&md5_context, digest);
39a24f0
+  char ctx_buf[EVP_MD_CTX_SIZE];
39a24f0
+  EVP_MD_CTX * const ctx= (EVP_MD_CTX*)ctx_buf;
39a24f0
+  md5_init(ctx);
39a24f0
+  md5_input(ctx, (const uchar *)buf, len);
39a24f0
+  md5_result(ctx, digest);
39a24f0
 }
39a24f0
 
39a24f0
 
39a24f0
 /**
39a24f0
   Wrapper function to compute MD5 message digest for
39a24f0
-  two messages in order to emulate md5(msg1, msg2).
39a24f0
+  many messages, concatenated.
39a24f0
 
39a24f0
   @param digest [out]  Computed MD5 digest
39a24f0
   @param buf1   [in]   First message
39a24f0
   @param len1   [in]   Length of first message
39a24f0
-  @param buf2   [in]   Second message
39a24f0
-  @param len2   [in]   Length of second message
39a24f0
+         ...
39a24f0
+  @param bufN   [in]   NULL terminates the list of buf,len pairs.
39a24f0
 
39a24f0
   @return              void
39a24f0
 */
39a24f0
@@ -135,37 +111,34 @@ void my_md5_multi(uchar *digest, ...)
39a24f0
 {
39a24f0
   va_list args;
39a24f0
   const uchar *str;
39a24f0
-#ifdef HAVE_YASSL
39a24f0
-  MD5_CONTEXT md5_context;
39a24f0
-#else
39a24f0
-  unsigned char md5_context[MA_HASH_CTX_SIZE];
39a24f0
-#endif
39a24f0
+  char ctx_buf[EVP_MD_CTX_SIZE];
39a24f0
+  EVP_MD_CTX * const ctx= (EVP_MD_CTX*)ctx_buf;
39a24f0
   va_start(args, digest);
39a24f0
 
39a24f0
-  md5_init_fast((MD5_CONTEXT *)&md5_context);
39a24f0
+  md5_init(ctx);
39a24f0
   for (str= va_arg(args, const uchar*); str; str= va_arg(args, const uchar*))
39a24f0
-    md5_input((MD5_CONTEXT *)&md5_context, str, va_arg(args, size_t));
39a24f0
+    md5_input(ctx, str, va_arg(args, size_t));
39a24f0
 
39a24f0
-  md5_result((MD5_CONTEXT *)&md5_context, digest);
39a24f0
+  md5_result(ctx, digest);
39a24f0
   va_end(args);
39a24f0
 }
39a24f0
 
39a24f0
 size_t my_md5_context_size()
39a24f0
 {
39a24f0
-  return MA_HASH_CTX_SIZE;
39a24f0
+  return EVP_MD_CTX_SIZE;
39a24f0
 }
39a24f0
 
39a24f0
 void my_md5_init(void *context)
39a24f0
 {
39a24f0
-  md5_init((MD5_CONTEXT *)context);
39a24f0
+  md5_init((EVP_MD_CTX *)context);
39a24f0
 }
39a24f0
 
39a24f0
 void my_md5_input(void *context, const uchar *buf, size_t len)
39a24f0
 {
39a24f0
-  md5_input((MD5_CONTEXT *)context, buf, len);
39a24f0
+  md5_input((EVP_MD_CTX *)context, buf, len);
39a24f0
 }
39a24f0
 
39a24f0
 void my_md5_result(void *context, uchar *digest)
39a24f0
 {
39a24f0
-  md5_result((MD5_CONTEXT *)context, digest);
39a24f0
+  md5_result((EVP_MD_CTX *)context, digest);
39a24f0
 }
39a24f0
diff --git a/mysys_ssl/openssl.c b/mysys_ssl/openssl.c
39a24f0
new file mode 100644
39a24f0
index 00000000000..a3f1ca29ec1
39a24f0
--- /dev/null
39a24f0
+++ b/mysys_ssl/openssl.c
39a24f0
@@ -0,0 +1,71 @@
39a24f0
+/*
39a24f0
+ Copyright (c) 2017, MariaDB Corporation.
39a24f0
+
39a24f0
+ This program is free software; you can redistribute it and/or modify
39a24f0
+ it under the terms of the GNU General Public License as published by
39a24f0
+ the Free Software Foundation; version 2 of the License.
39a24f0
+
39a24f0
+ This program is distributed in the hope that it will be useful,
39a24f0
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
39a24f0
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39a24f0
+ GNU General Public License for more details.
39a24f0
+
39a24f0
+ You should have received a copy of the GNU General Public License
39a24f0
+ along with this program; if not, write to the Free Software
39a24f0
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
39a24f0
+
39a24f0
+#include <my_global.h>
39a24f0
+#include <ssl_compat.h>
39a24f0
+
39a24f0
+#ifdef HAVE_YASSL
39a24f0
+
39a24f0
+int check_openssl_compatibility()
39a24f0
+{
39a24f0
+  return 0;
39a24f0
+}
39a24f0
+#else
39a24f0
+#include <openssl/evp.h>
39a24f0
+
39a24f0
+#ifdef HAVE_OPENSSL11
39a24f0
+typedef void *(*CRYPTO_malloc_t)(size_t, const char *, int);
39a24f0
+#endif
39a24f0
+
39a24f0
+#ifdef HAVE_OPENSSL10
39a24f0
+typedef void *(*CRYPTO_malloc_t)(size_t);
39a24f0
+#define CRYPTO_malloc   malloc
39a24f0
+#define CRYPTO_realloc  realloc
39a24f0
+#define CRYPTO_free     free
39a24f0
+#endif
39a24f0
+
39a24f0
+static uint allocated_size, allocated_count;
39a24f0
+
39a24f0
+static void *coc_malloc(size_t size)
39a24f0
+{
39a24f0
+  allocated_size+= size;
39a24f0
+  allocated_count++;
39a24f0
+  return malloc(size);
39a24f0
+}
39a24f0
+
39a24f0
+int check_openssl_compatibility()
39a24f0
+{
39a24f0
+  EVP_CIPHER_CTX *evp_ctx;
39a24f0
+  EVP_MD_CTX     *md5_ctx;
39a24f0
+
39a24f0
+  CRYPTO_set_mem_functions((CRYPTO_malloc_t)coc_malloc, CRYPTO_realloc, CRYPTO_free);
39a24f0
+
39a24f0
+  allocated_size= allocated_count= 0;
39a24f0
+  evp_ctx= EVP_CIPHER_CTX_new();
39a24f0
+  EVP_CIPHER_CTX_free(evp_ctx);
39a24f0
+  if (allocated_count != 1 || allocated_size > EVP_CIPHER_CTX_SIZE)
39a24f0
+    return 1;
39a24f0
+
39a24f0
+  allocated_size= allocated_count= 0;
39a24f0
+  md5_ctx= EVP_MD_CTX_create();
39a24f0
+  EVP_MD_CTX_destroy(md5_ctx);
39a24f0
+  if (allocated_count != 1 || allocated_size > EVP_MD_CTX_SIZE)
39a24f0
+    return 1;
39a24f0
+
39a24f0
+  CRYPTO_set_mem_functions(CRYPTO_malloc, CRYPTO_realloc, CRYPTO_free);
39a24f0
+  return 0;
39a24f0
+}
39a24f0
+#endif
39a24f0
diff --git a/mysys_ssl/yassl.cc b/mysys_ssl/yassl.cc
39a24f0
index 9e6f90d8d77..aa5631f2ab8 100644
39a24f0
--- a/mysys_ssl/yassl.cc
39a24f0
+++ b/mysys_ssl/yassl.cc
39a24f0
@@ -24,7 +24,6 @@
39a24f0
 
39a24f0
 #include <openssl/ssl.h>
39a24f0
 #include "aes.hpp"
39a24f0
-#include <my_sys.h>
39a24f0
 
39a24f0
 using yaSSL::yaERR_remove_state;
39a24f0
 
39a24f0
@@ -45,7 +44,6 @@ typedef struct
39a24f0
   int buf_len;
39a24f0
   int final_used;
39a24f0
   uchar tao_buf[sizeof(TaoCrypt::AES)];   // TaoCrypt::AES object
39a24f0
-  uchar oiv[TaoCrypt::AES::BLOCK_SIZE];   // original IV
39a24f0
   uchar buf[TaoCrypt::AES::BLOCK_SIZE];   // last partial input block
39a24f0
   uchar final[TaoCrypt::AES::BLOCK_SIZE]; // last decrypted (output) block
39a24f0
 } EVP_CIPHER_CTX;
39a24f0
@@ -76,26 +74,12 @@ static void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx)
39a24f0
   ctx->final_used= ctx->buf_len= ctx->flags= 0;
39a24f0
 }
39a24f0
 
39a24f0
-static EVP_CIPHER_CTX *EVP_CIPHER_CTX_new()
39a24f0
-{
39a24f0
-  EVP_CIPHER_CTX *ctx= (EVP_CIPHER_CTX *)my_malloc(sizeof(EVP_CIPHER_CTX), MYF(0));
39a24f0
-  if (ctx)
39a24f0
-    EVP_CIPHER_CTX_init(ctx);
39a24f0
-  return ctx;
39a24f0
-}
39a24f0
-
39a24f0
 static int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *ctx)
39a24f0
 {
39a24f0
   TAO(ctx)->~AES();
39a24f0
   return 1;
39a24f0
 }
39a24f0
 
39a24f0
-static void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
39a24f0
-{
39a24f0
-  EVP_CIPHER_CTX_cleanup(ctx);
39a24f0
-  my_free(ctx);
39a24f0
-}
39a24f0
-
39a24f0
 static int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
39a24f0
 {
39a24f0
   if (pad)
39a24f0
@@ -112,10 +96,7 @@ static int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
39a24f0
                                        : TaoCrypt::DECRYPTION, cipher->mode);
39a24f0
   TAO(ctx)->SetKey(key, cipher->key_len);
39a24f0
   if (iv)
39a24f0
-  {
39a24f0
     TAO(ctx)->SetIV(iv);
39a24f0
-    memcpy(ctx->oiv, iv, TaoCrypt::AES::BLOCK_SIZE);
39a24f0
-  }
39a24f0
   ctx->encrypt= enc;
39a24f0
   ctx->key_len= cipher->key_len;
39a24f0
   ctx->flags|= cipher->mode == TaoCrypt::CBC ? EVP_CIPH_CBC_MODE : EVP_CIPH_ECB_MODE;
39a24f0
diff --git a/sql-common/client.c b/sql-common/client.c
39a24f0
index b11ae9d3b0a..2518f669aee 100644
39a24f0
--- a/sql-common/client.c
39a24f0
+++ b/sql-common/client.c
39a24f0
@@ -104,11 +104,8 @@ my_bool	net_flush(NET *net);
39a24f0
 #define CONNECT_TIMEOUT 0
39a24f0
 #endif
39a24f0
 
39a24f0
-#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) || defined(HAVE_YASSL)
39a24f0
-#define ASN1_STRING_get0_data(X) ASN1_STRING_data(X)
39a24f0
-#endif
39a24f0
-
39a24f0
 #include "client_settings.h"
39a24f0
+#include <ssl_compat.h>
39a24f0
 #include <sql_common.h>
39a24f0
 #include <mysql/client_plugin.h>
39a24f0
 #include <my_context.h>
39a24f0
@@ -1773,9 +1770,8 @@ mysql_get_ssl_cipher(MYSQL *mysql __attribute__((unused)))
39a24f0
 
39a24f0
 #if defined(HAVE_OPENSSL)
39a24f0
 
39a24f0
-#if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(HAVE_YASSL)
39a24f0
+#ifdef HAVE_X509_check_host
39a24f0
 #include <openssl/x509v3.h>
39a24f0
-#define HAVE_X509_check_host
39a24f0
 #endif
39a24f0
 
39a24f0
 static int ssl_verify_server_cert(Vio *vio, const char* server_hostname, const char **errptr)
39a24f0
diff --git a/sql/mysqld.cc b/sql/mysqld.cc
39a24f0
index 524f5f10d53..cc64205369b 100644
39a24f0
--- a/sql/mysqld.cc
39a24f0
+++ b/sql/mysqld.cc
39a24f0
@@ -112,7 +112,6 @@
39a24f0
 #endif
39a24f0
 
39a24f0
 #include <my_systemd.h>
39a24f0
-#include <my_crypt.h>
39a24f0
 
39a24f0
 #define mysqld_charset &my_charset_latin1
39a24f0
 
39a24f0
@@ -122,7 +121,6 @@
39a24f0
 #define HAVE_CLOSE_SERVER_SOCK 1
39a24f0
 #endif
39a24f0
 
39a24f0
-
39a24f0
 extern "C" {					// Because of SCO 3.2V4.2
39a24f0
 #include <sys/stat.h>
39a24f0
 #ifndef __GNU_LIBRARY__
39a24f0
@@ -340,9 +338,13 @@ static PSI_thread_key key_thread_handle_con_sockets;
39a24f0
 static PSI_thread_key key_thread_handle_shutdown;
39a24f0
 #endif /* __WIN__ */
39a24f0
 
39a24f0
-#if defined (HAVE_OPENSSL) && !defined(HAVE_YASSL)
39a24f0
+#ifdef HAVE_OPENSSL
39a24f0
+#include <ssl_compat.h>
39a24f0
+
39a24f0
+#ifdef HAVE_OPENSSL10
39a24f0
 static PSI_rwlock_key key_rwlock_openssl;
39a24f0
 #endif
39a24f0
+#endif
39a24f0
 #endif /* HAVE_PSI_INTERFACE */
39a24f0
 
39a24f0
 #ifdef HAVE_NPTL
39a24f0
@@ -989,7 +991,7 @@ PSI_rwlock_key key_rwlock_LOCK_grant, key_rwlock_LOCK_logger,
39a24f0
 
39a24f0
 static PSI_rwlock_info all_server_rwlocks[]=
39a24f0
 {
39a24f0
-#if defined (HAVE_OPENSSL) && !defined(HAVE_YASSL)
39a24f0
+#ifdef HAVE_OPENSSL10
39a24f0
   { &key_rwlock_openssl, "CRYPTO_dynlock_value::lock", 0},
39a24f0
 #endif
39a24f0
   { &key_rwlock_LOCK_grant, "LOCK_grant", PSI_FLAG_GLOBAL},
39a24f0
@@ -1459,9 +1461,7 @@ scheduler_functions *thread_scheduler= &thread_scheduler_struct,
39a24f0
 
39a24f0
 #ifdef HAVE_OPENSSL
39a24f0
 #include <openssl/crypto.h>
39a24f0
-#ifndef HAVE_YASSL
39a24f0
-
39a24f0
-#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
39a24f0
+#ifdef HAVE_OPENSSL10
39a24f0
 typedef struct CRYPTO_dynlock_value
39a24f0
 {
39a24f0
   mysql_rwlock_t lock;
39a24f0
@@ -1472,8 +1472,7 @@ static openssl_lock_t *openssl_dynlock_create(const char *, int);
39a24f0
 static void openssl_dynlock_destroy(openssl_lock_t *, const char *, int);
39a24f0
 static void openssl_lock_function(int, int, const char *, int);
39a24f0
 static void openssl_lock(int, openssl_lock_t *, const char *, int);
39a24f0
-#endif
39a24f0
-#endif
39a24f0
+#endif /* HAVE_OPENSSL10 */
39a24f0
 char *des_key_file;
39a24f0
 #ifndef EMBEDDED_LIBRARY
39a24f0
 struct st_VioSSLFd *ssl_acceptor_fd;
39a24f0
@@ -2249,13 +2248,11 @@ static void clean_up_mutexes()
39a24f0
   mysql_mutex_destroy(&LOCK_global_index_stats);
39a24f0
 #ifdef HAVE_OPENSSL
39a24f0
   mysql_mutex_destroy(&LOCK_des_key_file);
39a24f0
-#ifndef HAVE_YASSL
39a24f0
-#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
39a24f0
+#ifdef HAVE_OPENSSL10
39a24f0
   for (int i= 0; i < CRYPTO_num_locks(); ++i)
39a24f0
     mysql_rwlock_destroy(&openssl_stdlocks[i].lock);
39a24f0
   OPENSSL_free(openssl_stdlocks);
39a24f0
-#endif
39a24f0
-#endif /* HAVE_YASSL */
39a24f0
+#endif /* HAVE_OPENSSL10 */
39a24f0
 #endif /* HAVE_OPENSSL */
39a24f0
 #ifdef HAVE_REPLICATION
39a24f0
   mysql_mutex_destroy(&LOCK_rpl_status);
39a24f0
@@ -4064,6 +4061,14 @@ static int init_common_variables()
39a24f0
     return 1;
39a24f0
   }
39a24f0
 
39a24f0
+#ifdef HAVE_OPENSSL
39a24f0
+  if (check_openssl_compatibility())
39a24f0
+  {
39a24f0
+    sql_print_error("Incompatible OpenSSL version. Cannot continue...");
39a24f0
+    return 1;
39a24f0
+  }
39a24f0
+#endif
39a24f0
+
39a24f0
   if (init_thread_environment() ||
39a24f0
       mysql_init_variables())
39a24f0
     return 1;
39a24f0
@@ -4610,8 +4615,7 @@ static int init_thread_environment()
39a24f0
 #ifdef HAVE_OPENSSL
39a24f0
   mysql_mutex_init(key_LOCK_des_key_file,
39a24f0
                    &LOCK_des_key_file, MY_MUTEX_INIT_FAST);
39a24f0
-#ifndef HAVE_YASSL
39a24f0
-#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
39a24f0
+#ifdef HAVE_OPENSSL10
39a24f0
   openssl_stdlocks= (openssl_lock_t*) OPENSSL_malloc(CRYPTO_num_locks() *
39a24f0
                                                      sizeof(openssl_lock_t));
39a24f0
   for (int i= 0; i < CRYPTO_num_locks(); ++i)
39a24f0
@@ -4620,9 +4624,8 @@ static int init_thread_environment()
39a24f0
   CRYPTO_set_dynlock_destroy_callback(openssl_dynlock_destroy);
39a24f0
   CRYPTO_set_dynlock_lock_callback(openssl_lock);
39a24f0
   CRYPTO_set_locking_callback(openssl_lock_function);
39a24f0
-#endif
39a24f0
-#endif
39a24f0
-#endif
39a24f0
+#endif /* HAVE_OPENSSL10 */
39a24f0
+#endif /* HAVE_OPENSSL */
39a24f0
   mysql_rwlock_init(key_rwlock_LOCK_sys_init_connect, &LOCK_sys_init_connect);
39a24f0
   mysql_rwlock_init(key_rwlock_LOCK_sys_init_slave, &LOCK_sys_init_slave);
39a24f0
   mysql_rwlock_init(key_rwlock_LOCK_grant, &LOCK_grant);
39a24f0
@@ -4655,8 +4658,7 @@ static int init_thread_environment()
39a24f0
 }
39a24f0
 
39a24f0
 
39a24f0
-#if defined(HAVE_OPENSSL) && !defined(HAVE_YASSL)
39a24f0
-#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
39a24f0
+#ifdef HAVE_OPENSSL10
39a24f0
 static openssl_lock_t *openssl_dynlock_create(const char *file, int line)
39a24f0
 {
39a24f0
   openssl_lock_t *lock= new openssl_lock_t;
39a24f0
@@ -4716,9 +4718,7 @@ static void openssl_lock(int mode, openssl_lock_t *lock, const char *file,
39a24f0
     abort();
39a24f0
   }
39a24f0
 }
39a24f0
-#endif
39a24f0
-#endif /* HAVE_OPENSSL */
39a24f0
-
39a24f0
+#endif /* HAVE_OPENSSL10 */
39a24f0
 
39a24f0
 static void init_ssl()
39a24f0
 {
39a24f0
@@ -4746,9 +4746,8 @@ static void init_ssl()
39a24f0
       while ((err= ERR_get_error()))
39a24f0
         sql_print_warning("SSL error: %s", ERR_error_string(err, NULL));
39a24f0
     }
39a24f0
-    else {
39a24f0
+    else
39a24f0
       ERR_remove_state(0);
39a24f0
-    }
39a24f0
   }
39a24f0
   else
39a24f0
   {
39a24f0
diff --git a/sql/slave.cc b/sql/slave.cc
39a24f0
index ea6c824fb88..aec5143b9a6 100644
39a24f0
--- a/sql/slave.cc
39a24f0
+++ b/sql/slave.cc
39a24f0
@@ -40,6 +40,7 @@
39a24f0
 #include <my_dir.h>
39a24f0
 #include <sql_common.h>
39a24f0
 #include <errmsg.h>
39a24f0
+#include <ssl_compat.h>
39a24f0
 #include <mysqld_error.h>
39a24f0
 #include <mysys_err.h>
39a24f0
 #include "rpl_handler.h"
39a24f0
@@ -60,12 +61,6 @@
39a24f0
 #include "debug_sync.h"
39a24f0
 #include "rpl_parallel.h"
39a24f0
 
39a24f0
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
39a24f0
-#define ERR_remove_state(X)
39a24f0
-#elif defined(HAVE_ERR_remove_thread_state)
39a24f0
-#define ERR_remove_state(X) ERR_remove_thread_state(NULL)
39a24f0
-#endif
39a24f0
-
39a24f0
 #define FLAGSTR(V,F) ((V)&(F)?#F" ":"")
39a24f0
 
39a24f0
 #define MAX_SLAVE_RETRY_PAUSE 5
39a24f0
@@ -4512,13 +4507,7 @@ log space");
39a24f0
 
39a24f0
   DBUG_LEAVE;                                   // Must match DBUG_ENTER()
39a24f0
   my_thread_end();
39a24f0
-#ifdef HAVE_OPENSSL
39a24f0
-#if OPENSSL_VERSION_NUMBER < 0x10000000L
39a24f0
   ERR_remove_state(0);
39a24f0
-#elif OPENSSL_VERSION_NUMBER < 0x10100000L
39a24f0
-  ERR_remove_thread_state(0);
39a24f0
-#endif
39a24f0
-#endif
39a24f0
   pthread_exit(0);
39a24f0
   return 0;                                     // Avoid compiler warnings
39a24f0
 }
39a24f0
@@ -5177,13 +5166,7 @@ pthread_handler_t handle_slave_sql(void *arg)
39a24f0
 
39a24f0
   DBUG_LEAVE;                                   // Must match DBUG_ENTER()
39a24f0
   my_thread_end();
39a24f0
-#ifdef HAVE_OPENSSL
39a24f0
-#if OPENSSL_VERSION_NUMBER < 0x10000000L
39a24f0
   ERR_remove_state(0);
39a24f0
-#elif OPENSSL_VERSION_NUMBER < 0x10100000L
39a24f0
-  ERR_remove_thread_state(0);
39a24f0
-#endif
39a24f0
-#endif
39a24f0
   pthread_exit(0);
39a24f0
   return 0;                                     // Avoid compiler warnings
39a24f0
 }
39a24f0
diff --git a/vio/vio.c b/vio/vio.c
39a24f0
index e3bc8ca8ab8..44d06092184 100644
39a24f0
--- a/vio/vio.c
39a24f0
+++ b/vio/vio.c
39a24f0
@@ -22,6 +22,7 @@
39a24f0
 */
39a24f0
 
39a24f0
 #include "vio_priv.h"
39a24f0
+#include "ssl_compat.h"
39a24f0
 
39a24f0
 #ifdef _WIN32
39a24f0
 
39a24f0
diff --git a/vio/viosslfactories.c b/vio/viosslfactories.c
39a24f0
index 497047cac72..71ef2879464 100644
39a24f0
--- a/vio/viosslfactories.c
39a24f0
+++ b/vio/viosslfactories.c
39a24f0
@@ -15,20 +15,12 @@
39a24f0
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
39a24f0
 
39a24f0
 #include "vio_priv.h"
39a24f0
+#include <ssl_compat.h>
39a24f0
 
39a24f0
 #ifdef HAVE_OPENSSL
39a24f0
-#if defined(HAVE_YASSL) || defined(LIBRESSL_VERSION_NUMBER)
39a24f0
-#define OPENSSL_init_ssl(X,Y) SSL_library_init()
39a24f0
-#else
39a24f0
+#ifndef HAVE_YASSL
39a24f0
 #include <openssl/dh.h>
39a24f0
 #include <openssl/bn.h>
39a24f0
-
39a24f0
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
39a24f0
-#define ERR_remove_state(X)
39a24f0
-#else
39a24f0
-#define OPENSSL_init_ssl(X,Y) SSL_library_init()
39a24f0
-#endif
39a24f0
-
39a24f0
 #endif
39a24f0
 
39a24f0
 static my_bool     ssl_algorithms_added    = FALSE;
39a24f0
@@ -36,59 +28,51 @@ static my_bool     ssl_error_strings_loaded= FALSE;
39a24f0
 
39a24f0
 /* the function below was generated with "openssl dhparam -2 -C 2048" */
39a24f0
 
39a24f0
-/* {{{ get_dh_2048 */
39a24f0
-static DH *get_dh_2048()
39a24f0
+static
39a24f0
+DH *get_dh2048()
39a24f0
 {
39a24f0
-  static unsigned char dh2048_p[]={
39a24f0
-    0xA1,0xBB,0x7C,0x20,0xC5,0x5B,0xC0,0x7B,0x21,0x8B,0xD6,0xA8,
39a24f0
-    0x15,0xFC,0x3B,0xBA,0xAB,0x9F,0xDF,0x68,0xC4,0x79,0x78,0x0D,
39a24f0
-    0xC1,0x12,0x64,0xE4,0x15,0xC9,0x66,0xDB,0xF6,0xCB,0xB3,0x39,
39a24f0
-    0x02,0x5B,0x78,0x62,0xFB,0x09,0xAE,0x09,0x6B,0xDD,0xD4,0x5D,
39a24f0
-    0x97,0xBC,0xDC,0x7F,0xE6,0xD6,0xF1,0xCB,0xF5,0xEB,0xDA,0xA7,
39a24f0
-    0x2E,0x5A,0x43,0x2B,0xE9,0x40,0xE2,0x85,0x00,0x1C,0xC0,0x0A,
39a24f0
-    0x98,0x77,0xA9,0x31,0xDE,0x0B,0x75,0x4D,0x1E,0x1F,0x16,0x83,
39a24f0
-    0xCA,0xDE,0xBD,0x21,0xFC,0xC1,0x82,0x37,0x36,0x33,0x0B,0x66,
39a24f0
-    0x06,0x3C,0xF3,0xAF,0x21,0x57,0x57,0x80,0xF6,0x94,0x1B,0xA9,
39a24f0
-    0xD4,0xF6,0x8F,0x18,0x62,0x0E,0xC4,0x22,0xF9,0x5B,0x62,0xCC,
39a24f0
-    0x3F,0x19,0x95,0xCF,0x4B,0x00,0xA6,0x6C,0x0B,0xAF,0x9F,0xD5,
39a24f0
-    0xFA,0x3D,0x6D,0xDA,0x30,0x83,0x07,0x91,0xAC,0x15,0xFF,0x8F,
39a24f0
-    0x59,0x54,0xEA,0x25,0xBC,0x4E,0xEB,0x6A,0x54,0xDF,0x75,0x09,
39a24f0
-    0x72,0x0F,0xEF,0x23,0x70,0xE0,0xA8,0x04,0xEA,0xFF,0x90,0x54,
39a24f0
-    0xCD,0x84,0x18,0xC0,0x75,0x91,0x99,0x0F,0xA1,0x78,0x0C,0x07,
39a24f0
-    0xB7,0xC5,0xDE,0x55,0x06,0x7B,0x95,0x68,0x2C,0x33,0x39,0xBC,
39a24f0
-    0x2C,0xD0,0x6D,0xDD,0xFA,0xDC,0xB5,0x8F,0x82,0x39,0xF8,0x67,
39a24f0
-    0x44,0xF1,0xD8,0xF7,0x78,0x11,0x9A,0x77,0x9B,0x53,0x47,0xD6,
39a24f0
-    0x2B,0x5D,0x67,0xB8,0xB7,0xBC,0xC1,0xD7,0x79,0x62,0x15,0xC2,
39a24f0
-    0xC5,0x83,0x97,0xA7,0xF8,0xB4,0x9C,0xF6,0x8F,0x9A,0xC7,0xDA,
39a24f0
-    0x1B,0xBB,0x87,0x07,0xA7,0x71,0xAD,0xB2,0x8A,0x50,0xF8,0x26,
39a24f0
-    0x12,0xB7,0x3E,0x0B,
39a24f0
-  };
39a24f0
-  static unsigned char dh2048_g[]={
39a24f0
-    0x02,
39a24f0
-  };
39a24f0
-  DH *dh;
39a24f0
-  if ((dh=DH_new()) == NULL)
39a24f0
-    return(NULL);
39a24f0
-#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
39a24f0
-  (dh)->p=BN_bin2bn(dh2048_p,sizeof(dh2048_p),NULL);
39a24f0
-  (dh)->g=BN_bin2bn(dh2048_g,sizeof(dh2048_g),NULL);
39a24f0
-  if ((dh)->p == NULL || (dh)->g == NULL)
39a24f0
-  { DH_free(dh); return NULL; }
39a24f0
-#else
39a24f0
-  {
39a24f0
-    BIGNUM *dhp_bn= BN_bin2bn(dh2048_p,sizeof(dh2048_p),NULL),
39a24f0
-           *dhg_bn= BN_bin2bn(dh2048_g,sizeof(dh2048_g),NULL);
39a24f0
-    if (dhp_bn == NULL || dhg_bn == NULL ||
39a24f0
-        !DH_set0_pqg(dh, dhp_bn, NULL, dhg_bn))
39a24f0
-    {
39a24f0
-      DH_free(dh);
39a24f0
-      BN_free(dhp_bn);
39a24f0
-      BN_free(dhg_bn);
39a24f0
-      return NULL;
39a24f0
+    static unsigned char dhp_2048[] = {
39a24f0
+        0xA1,0xBB,0x7C,0x20,0xC5,0x5B,0xC0,0x7B,0x21,0x8B,0xD6,0xA8,
39a24f0
+        0x15,0xFC,0x3B,0xBA,0xAB,0x9F,0xDF,0x68,0xC4,0x79,0x78,0x0D,
39a24f0
+        0xC1,0x12,0x64,0xE4,0x15,0xC9,0x66,0xDB,0xF6,0xCB,0xB3,0x39,
39a24f0
+        0x02,0x5B,0x78,0x62,0xFB,0x09,0xAE,0x09,0x6B,0xDD,0xD4,0x5D,
39a24f0
+        0x97,0xBC,0xDC,0x7F,0xE6,0xD6,0xF1,0xCB,0xF5,0xEB,0xDA,0xA7,
39a24f0
+        0x2E,0x5A,0x43,0x2B,0xE9,0x40,0xE2,0x85,0x00,0x1C,0xC0,0x0A,
39a24f0
+        0x98,0x77,0xA9,0x31,0xDE,0x0B,0x75,0x4D,0x1E,0x1F,0x16,0x83,
39a24f0
+        0xCA,0xDE,0xBD,0x21,0xFC,0xC1,0x82,0x37,0x36,0x33,0x0B,0x66,
39a24f0
+        0x06,0x3C,0xF3,0xAF,0x21,0x57,0x57,0x80,0xF6,0x94,0x1B,0xA9,
39a24f0
+        0xD4,0xF6,0x8F,0x18,0x62,0x0E,0xC4,0x22,0xF9,0x5B,0x62,0xCC,
39a24f0
+        0x3F,0x19,0x95,0xCF,0x4B,0x00,0xA6,0x6C,0x0B,0xAF,0x9F,0xD5,
39a24f0
+        0xFA,0x3D,0x6D,0xDA,0x30,0x83,0x07,0x91,0xAC,0x15,0xFF,0x8F,
39a24f0
+        0x59,0x54,0xEA,0x25,0xBC,0x4E,0xEB,0x6A,0x54,0xDF,0x75,0x09,
39a24f0
+        0x72,0x0F,0xEF,0x23,0x70,0xE0,0xA8,0x04,0xEA,0xFF,0x90,0x54,
39a24f0
+        0xCD,0x84,0x18,0xC0,0x75,0x91,0x99,0x0F,0xA1,0x78,0x0C,0x07,
39a24f0
+        0xB7,0xC5,0xDE,0x55,0x06,0x7B,0x95,0x68,0x2C,0x33,0x39,0xBC,
39a24f0
+        0x2C,0xD0,0x6D,0xDD,0xFA,0xDC,0xB5,0x8F,0x82,0x39,0xF8,0x67,
39a24f0
+        0x44,0xF1,0xD8,0xF7,0x78,0x11,0x9A,0x77,0x9B,0x53,0x47,0xD6,
39a24f0
+        0x2B,0x5D,0x67,0xB8,0xB7,0xBC,0xC1,0xD7,0x79,0x62,0x15,0xC2,
39a24f0
+        0xC5,0x83,0x97,0xA7,0xF8,0xB4,0x9C,0xF6,0x8F,0x9A,0xC7,0xDA,
39a24f0
+        0x1B,0xBB,0x87,0x07,0xA7,0x71,0xAD,0xB2,0x8A,0x50,0xF8,0x26,
39a24f0
+        0x12,0xB7,0x3E,0x0B,
39a24f0
+    };
39a24f0
+    static unsigned char dhg_2048[] = {
39a24f0
+        0x02
39a24f0
+    };
39a24f0
+    DH *dh = DH_new();
39a24f0
+    BIGNUM *dhp_bn, *dhg_bn;
39a24f0
+
39a24f0
+    if (dh == NULL)
39a24f0
+        return NULL;
39a24f0
+    dhp_bn = BN_bin2bn(dhp_2048, sizeof (dhp_2048), NULL);
39a24f0
+    dhg_bn = BN_bin2bn(dhg_2048, sizeof (dhg_2048), NULL);
39a24f0
+    if (dhp_bn == NULL || dhg_bn == NULL
39a24f0
+            || !DH_set0_pqg(dh, dhp_bn, NULL, dhg_bn)) {
39a24f0
+        DH_free(dh);
39a24f0
+        BN_free(dhp_bn);
39a24f0
+        BN_free(dhg_bn);
39a24f0
+        return NULL;
39a24f0
     }
39a24f0
-  }
39a24f0
-#endif
39a24f0
-  return dh;
39a24f0
+    return dh;
39a24f0
 }
39a24f0
 
39a24f0
 static const char*
39a24f0
@@ -287,7 +271,7 @@ new_VioSSLFd(const char *key_file, const char *cert_file,
39a24f0
   /* DH stuff */
39a24f0
   if (!is_client_method)
39a24f0
   {
39a24f0
-    dh=get_dh_2048();
39a24f0
+    dh=get_dh2048();
39a24f0
     if (!SSL_CTX_set_tmp_dh(ssl_fd->ssl_context, dh))
39a24f0
     {
39a24f0
       *error= SSL_INITERR_DH;
39a24f0
-- 
39a24f0
2.13.3
39a24f0