1
0
mirror of https://github.com/mariadb-corporation/mariadb-connector-c.git synced 2025-08-07 02:42:49 +03:00

- Fixed license header

- More OpenSSL 1.1 fixes
This commit is contained in:
Georg Richter
2016-08-05 07:39:10 +02:00
parent 4f2c9da859
commit dd9ebcf56a
5 changed files with 41 additions and 25 deletions

View File

@@ -21,10 +21,6 @@
#ifndef _global_h #ifndef _global_h
#define _global_h #define _global_h
#define MA_ASSERT_CONCAT_(a, b) a##b
#define MA_ASSERT_CONCAT(a, b) MA_ASSERT_CONCAT_(a, b)
#define ma_assert(e) enum { MA_ASSERT_CONCAT(assert_line_, __LINE__) = 1/(!!(e)) }
#ifdef _WIN32 #ifdef _WIN32
#include <ma_config_win.h> #include <ma_config_win.h>
#else #else

View File

@@ -1,4 +1,3 @@
#ifndef MYSQL_PLUGIN_AUTH_COMMON_INCLUDED
/* Copyright (C) 2010 Sergei Golubchik and Monty Program Ab /* Copyright (C) 2010 Sergei Golubchik and Monty Program Ab
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
@@ -16,6 +15,8 @@
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA */ MA 02111-1307, USA */
#ifndef MYSQL_PLUGIN_AUTH_COMMON_INCLUDED
/** /**
@file @file

View File

@@ -283,7 +283,7 @@ int mysql_client_plugin_init()
memset(&mysql, 0, sizeof(mysql)); /* dummy mysql for set_mysql_extended_error */ memset(&mysql, 0, sizeof(mysql)); /* dummy mysql for set_mysql_extended_error */
pthread_mutex_init(&LOCK_load_client_plugin, MY_MUTEX_INIT_SLOW); pthread_mutex_init(&LOCK_load_client_plugin, MY_MUTEX_INIT_SLOW);
ma_init_ma_alloc_root(&mem_root, 128, 128); ma_init_alloc_root(&mem_root, 128, 128);
memset(&plugin_list, 0, sizeof(plugin_list)); memset(&plugin_list, 0, sizeof(plugin_list));

View File

@@ -125,7 +125,10 @@ my_context_yield(struct my_context *c)
int int
my_context_init(struct my_context *c, size_t stack_size) my_context_init(struct my_context *c, size_t stack_size)
{ {
ma_assert(sizeof(char *) <= SIZEOF_INT * 2); #if SIZEOF_CHARP > SIZEOF_INT*2
#error Error: Unable to store pointer in 2 ints on this architecture
#endif
memset(c, 0, sizeof(*c)); memset(c, 0, sizeof(*c));
if (!(c->stack= malloc(stack_size))) if (!(c->stack= malloc(stack_size)))
return -1; /* Out of memory */ return -1; /* Out of memory */

View File

@@ -49,10 +49,12 @@ static SSL_CTX *SSL_context= NULL;
#define MAX_SSL_ERR_LEN 100 #define MAX_SSL_ERR_LEN 100
static pthread_mutex_t LOCK_openssl_config; static pthread_mutex_t LOCK_openssl_config;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
static pthread_mutex_t *LOCK_crypto= NULL; static pthread_mutex_t *LOCK_crypto= NULL;
static int ma_bio_read(BIO *h, char *buf, int size); static int ma_bio_read(BIO *h, char *buf, int size);
static int ma_bio_write(BIO *h, const char *buf, int size); static int ma_bio_write(BIO *h, const char *buf, int size);
static BIO_METHOD ma_BIO_methods; static BIO_METHOD ma_BIO_methods;
#endif
static void ma_tls_set_error(MYSQL *mysql) static void ma_tls_set_error(MYSQL *mysql)
{ {
@@ -96,13 +98,13 @@ static void ma_tls_get_error(char *errmsg, size_t length)
snprintf(errmsg, length, "SSL errno=%lu", ssl_errno); snprintf(errmsg, length, "SSL errno=%lu", ssl_errno);
} }
#if (OPENSSL_VERSION_NUMBER < 0x10100000) #if OPENSSL_VERSION_NUMBER < 0x10100000L
/* /*
thread safe callbacks for OpenSSL thread safe callbacks for OpenSSL
Crypto call back functions will be Crypto call back functions will be
set during ssl_initialization set during ssl_initialization
*/ */
#if (OPENSSL_VERSION_NUMBER < 0x10000000) #if OPENSSL_VERSION_NUMBER < 0x10000000L
static unsigned long my_cb_threadid(void) static unsigned long my_cb_threadid(void)
{ {
/* cast pthread_t to unsigned long */ /* cast pthread_t to unsigned long */
@@ -156,6 +158,7 @@ MA_SSL_SESSION *ma_tls_get_session(MYSQL *mysql)
} }
#if OPENSSL_VERSION_NUMBER < 0x10100000L
static int ma_bio_read(BIO *bio, char *buf, int size) static int ma_bio_read(BIO *bio, char *buf, int size)
{ {
MARIADB_PVIO *pvio= (MARIADB_PVIO *)bio->ptr; MARIADB_PVIO *pvio= (MARIADB_PVIO *)bio->ptr;
@@ -174,6 +177,7 @@ static int ma_bio_write(BIO *bio, const char *buf, int size)
BIO_clear_retry_flags(bio); BIO_clear_retry_flags(bio);
return (int)rc; return (int)rc;
} }
#endif
static int ma_tls_session_cb(SSL *ssl, SSL_SESSION *session) static int ma_tls_session_cb(SSL *ssl, SSL_SESSION *session)
{ {
@@ -216,7 +220,7 @@ static void ma_tls_remove_session_cb(SSL_CTX* ctx, SSL_SESSION* session)
} }
#endif #endif
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) #if OPENSSL_VERSION_NUMBER < 0x10100000L
static void my_cb_locking(int mode, int n, const char *file, int line) static void my_cb_locking(int mode, int n, const char *file, int line)
{ {
if (mode & CRYPTO_LOCK) if (mode & CRYPTO_LOCK)
@@ -239,7 +243,7 @@ static int ssl_thread_init()
pthread_mutex_init(&LOCK_crypto[i], NULL); pthread_mutex_init(&LOCK_crypto[i], NULL);
} }
#if (OPENSSL_VERSION_NUMBER < 0x10000000) #if OPENSSL_VERSION_NUMBER < 0x10000000L
CRYPTO_set_id_callback(my_cb_threadid); CRYPTO_set_id_callback(my_cb_threadid);
#else #else
CRYPTO_THREADID_set_callback(my_cb_threadid); CRYPTO_THREADID_set_callback(my_cb_threadid);
@@ -293,7 +297,7 @@ int ma_tls_start(char *errmsg, size_t errmsg_len)
/* lock mutex to prevent multiple initialization */ /* lock mutex to prevent multiple initialization */
pthread_mutex_init(&LOCK_openssl_config,MY_MUTEX_INIT_FAST); pthread_mutex_init(&LOCK_openssl_config,MY_MUTEX_INIT_FAST);
pthread_mutex_lock(&LOCK_openssl_config); pthread_mutex_lock(&LOCK_openssl_config);
#if (OPENSSL_VERSION_NUMBER >= 0x10100000) #if OPENSSL_VERSION_NUMBER >= 0x10100000L
OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL); OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL);
#else #else
if (ssl_thread_init()) if (ssl_thread_init())
@@ -311,7 +315,7 @@ int ma_tls_start(char *errmsg, size_t errmsg_len)
SSL_load_error_strings(); SSL_load_error_strings();
/* digests and ciphers */ /* digests and ciphers */
OpenSSL_add_all_algorithms(); OpenSSL_add_all_algorithms();
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L) #if OPENSSL_VERSION_NUMBER >= 0x10100000L
if (!(SSL_context= SSL_CTX_new(TLS_client_method()))) if (!(SSL_context= SSL_CTX_new(TLS_client_method())))
#else #else
if (!(SSL_context= SSL_CTX_new(SSLv23_client_method()))) if (!(SSL_context= SSL_CTX_new(SSLv23_client_method())))
@@ -327,11 +331,11 @@ int ma_tls_start(char *errmsg, size_t errmsg_len)
SSL_CTX_sess_set_remove_cb(SSL_context, ma_tls_remove_session_cb); SSL_CTX_sess_set_remove_cb(SSL_context, ma_tls_remove_session_cb);
#endif #endif
disable_sigpipe(); disable_sigpipe();
#if OPENSSL_VERSION_NUMBER < 0x10100000L
memcpy(&ma_BIO_methods, BIO_s_socket(), sizeof(BIO_METHOD)); memcpy(&ma_BIO_method, BIO_s_socket(), sizeof(BIO_METHOD));
ma_BIO_methods.bread= ma_bio_read; ma_BIO_method.bread= ma_bio_read;
ma_BIO_methods.bwrite= ma_bio_write; ma_BIO_method.bwrite= ma_bio_write;
#endif
rc= 0; rc= 0;
ma_tls_initialized= TRUE; ma_tls_initialized= TRUE;
end: end:
@@ -355,16 +359,18 @@ void ma_tls_end()
{ {
if (ma_tls_initialized) if (ma_tls_initialized)
{ {
int i;
pthread_mutex_lock(&LOCK_openssl_config); pthread_mutex_lock(&LOCK_openssl_config);
#if OPENSSL_VERSION_NUMBER < 0x10100000L
CRYPTO_set_locking_callback(NULL); CRYPTO_set_locking_callback(NULL);
CRYPTO_set_id_callback(NULL); CRYPTO_set_id_callback(NULL);
{
for (i=0; i < CRYPTO_num_locks(); i++) int i;
pthread_mutex_destroy(&LOCK_crypto[i]); for (i=0; i < CRYPTO_num_locks(); i++)
pthread_mutex_destroy(&LOCK_crypto[i]);
}
ma_free((gptr)LOCK_crypto); ma_free((gptr)LOCK_crypto);
LOCK_crypto= NULL; LOCK_crypto= NULL;
#endif
if (SSL_context) if (SSL_context)
{ {
@@ -373,7 +379,7 @@ void ma_tls_end()
} }
if (mariadb_deinitialize_ssl) if (mariadb_deinitialize_ssl)
{ {
#if OPENSSL_VERSION_NUMBER < 0x10100000 #if OPENSSL_VERSION_NUMBER < 0x10100000L
ERR_remove_state(0); ERR_remove_state(0);
#endif #endif
EVP_cleanup(); EVP_cleanup();
@@ -518,7 +524,10 @@ my_bool ma_tls_connect(MARIADB_TLS *ctls)
MYSQL *mysql; MYSQL *mysql;
MARIADB_PVIO *pvio; MARIADB_PVIO *pvio;
int rc; int rc;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
BIO_METHOD *bio_method= NULL;
BIO *bio; BIO *bio;
#endif
mysql= (MYSQL *)SSL_get_app_data(ssl); mysql= (MYSQL *)SSL_get_app_data(ssl);
pvio= mysql->net.pvio; pvio= mysql->net.pvio;
@@ -529,10 +538,14 @@ my_bool ma_tls_connect(MARIADB_TLS *ctls)
SSL_clear(ssl); SSL_clear(ssl);
bio= BIO_new(&ma_BIO_methods); #if OPENSSL_VERSION_NUMBER < 0x10100000L
bio= BIO_new(&ma_BIO_method);
bio->ptr= pvio; bio->ptr= pvio;
SSL_set_bio(ssl, bio, bio); SSL_set_bio(ssl, bio, bio);
BIO_set_fd(bio, mysql_get_socket(mysql), BIO_NOCLOSE); BIO_set_fd(bio, mysql_get_socket(mysql), BIO_NOCLOSE);
#else
SSL_set_fd(ssl, mysql_get_socket(mysql));
#endif
while (try_connect && (rc= SSL_connect(ssl)) == -1) while (try_connect && (rc= SSL_connect(ssl)) == -1)
{ {
@@ -601,6 +614,9 @@ my_bool ma_tls_close(MARIADB_TLS *ctls)
if ((rc= SSL_shutdown(ssl))) if ((rc= SSL_shutdown(ssl)))
break; break;
/* Since we transferred ownership of BIO to ssl, BIO will
automatically freed - no need for an explicit BIO_free_all */
SSL_free(ssl); SSL_free(ssl);
ctls->ssl= NULL; ctls->ssl= NULL;