Skip to content

Commit b967620

Browse files
author
holzboote@googlemail.com
committed
Fix for CONC-95: SSL connection with require X509 privilege doesn't work.
- all pems and ciphers are now stored in global context - create new ssl instance after loading pems into global context
1 parent 684287a commit b967620

File tree

9 files changed

+71
-171
lines changed

9 files changed

+71
-171
lines changed

libmariadb/ma_secure.c

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static void my_SSL_error(MYSQL *mysql)
7272
*/
7373
static unsigned long my_cb_threadid(void)
7474
{
75-
/* chast pthread_t to unsigned long */
75+
/* cast pthread_t to unsigned long */
7676
return (unsigned long) pthread_self();
7777
}
7878

@@ -180,7 +180,7 @@ void my_ssl_end()
180180
EVP_cleanup();
181181
CRYPTO_cleanup_all_ex_data();
182182
ERR_free_strings();
183-
ENGINE_cleanup();
183+
//ENGINE_cleanup();
184184
CONF_modules_free();
185185
CONF_modules_unload(1);
186186
sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
@@ -194,53 +194,45 @@ void my_ssl_end()
194194
/*
195195
Set certification stuff.
196196
*/
197-
static int my_ssl_set_certs(SSL *ssl)
197+
static int my_ssl_set_certs(MYSQL *mysql)
198198
{
199-
int have_cert= 0;
200-
MYSQL *mysql;
201-
199+
char *key_file= mysql->options.ssl_key ? mysql->options.ssl_key : mysql->options.ssl_cert;
202200
DBUG_ENTER("my_ssl_set_certs");
203201

204202
/* Make sure that ssl was allocated and
205203
ssl_system was initialized */
206-
DBUG_ASSERT(ssl != NULL);
207204
DBUG_ASSERT(my_ssl_initialized == TRUE);
208205

209-
/* get connection for current ssl */
210-
mysql= (MYSQL *)SSL_get_app_data(ssl);
211-
212206
/* add cipher */
213207
if ((mysql->options.ssl_cipher &&
214208
mysql->options.ssl_cipher[0] != 0) &&
215-
SSL_set_cipher_list(ssl, mysql->options.ssl_cipher) == 0)
209+
SSL_CTX_set_cipher_list(SSL_context, mysql->options.ssl_cipher) == 0)
216210
goto error;
217211

212+
/* ca_file and ca_path */
213+
if (SSL_CTX_load_verify_locations(SSL_context,
214+
mysql->options.ssl_ca,
215+
mysql->options.ssl_capath) == 0)
216+
{
217+
if (mysql->options.ssl_ca || mysql->options.ssl_capath)
218+
goto error;
219+
if (SSL_CTX_set_default_verify_paths(SSL_context) == 0)
220+
goto error;
221+
}
222+
218223
/* set cert */
219224
if (mysql->options.ssl_cert && mysql->options.ssl_cert[0] != 0)
220-
{
221225
if (SSL_CTX_use_certificate_chain_file(SSL_context, mysql->options.ssl_cert) <= 0)
222226
goto error;
223-
have_cert= 1;
224-
}
225227

226-
/* set key */
227-
if (mysql->options.ssl_key && mysql->options.ssl_key[0])
228+
/* set key */
229+
if (key_file)
228230
{
229-
if (SSL_CTX_use_PrivateKey_file(SSL_context, mysql->options.ssl_key, SSL_FILETYPE_PEM) <= 0)
231+
if (SSL_CTX_use_PrivateKey_file(SSL_context, key_file, SSL_FILETYPE_PEM) <= 0)
230232
goto error;
231233

232234
/* verify key */
233-
if (have_cert && SSL_CTX_check_private_key(SSL_context) != 1)
234-
goto error;
235-
}
236-
/* ca_file and ca_path */
237-
if (SSL_CTX_load_verify_locations(SSL_context,
238-
mysql->options.ssl_ca,
239-
mysql->options.ssl_capath) == 0)
240-
{
241-
if (mysql->options.ssl_ca || mysql->options.ssl_capath)
242-
goto error;
243-
if (SSL_CTX_set_default_verify_paths(SSL_context) == 0)
235+
if (!SSL_CTX_check_private_key(SSL_context))
244236
goto error;
245237
}
246238
if (mysql->options.extension &&
@@ -291,20 +283,18 @@ static int my_verify_callback(int ok, X509_STORE_CTX *ctx)
291283
DBUG_RETURN(0);
292284
depth= X509_STORE_CTX_get_error_depth(ctx);
293285
if (depth == 0)
294-
{
295286
ok= 1;
296-
DBUG_RETURN(1);
297-
}
298287
}
299-
else
300-
DBUG_RETURN(1);
301288

302-
my_set_error(mysql, CR_SSL_CONNECTION_ERROR, SQLSTATE_UNKNOWN,
303-
ER(CR_SSL_CONNECTION_ERROR),
304-
X509_verify_cert_error_string(ctx->error));
305-
DBUG_RETURN(0);
289+
/*
290+
my_set_error(mysql, CR_SSL_CONNECTION_ERROR, SQLSTATE_UNKNOWN,
291+
ER(CR_SSL_CONNECTION_ERROR),
292+
X509_verify_cert_error_string(ctx->error));
293+
*/
294+
DBUG_RETURN(ok);
306295
}
307296

297+
308298
/*
309299
allocates a new ssl object
310300
@@ -328,18 +318,20 @@ SSL *my_ssl_init(MYSQL *mysql)
328318
if (!my_ssl_initialized)
329319
my_ssl_start(mysql);
330320

321+
if (my_ssl_set_certs(mysql))
322+
goto error;
323+
331324
if (!(ssl= SSL_new(SSL_context)))
332325
goto error;
333326

334327
if (!SSL_set_app_data(ssl, mysql))
335328
goto error;
336-
if (my_ssl_set_certs(ssl))
337-
goto error;
338329

339330
verify= (!mysql->options.ssl_ca && !mysql->options.ssl_capath) ?
340331
SSL_VERIFY_NONE : SSL_VERIFY_PEER;
341-
SSL_set_verify(ssl, verify, my_verify_callback);
342-
SSL_set_verify_depth(ssl, 1);
332+
333+
SSL_CTX_set_verify(SSL_context, verify, my_verify_callback);
334+
SSL_CTX_set_verify_depth(SSL_context, 1);
343335

344336
DBUG_RETURN(ssl);
345337
error:

unittest/libmariadb/certs/ca.pem

Lines changed: 0 additions & 30 deletions
This file was deleted.

unittest/libmariadb/certs/client-cert.pem

Lines changed: 0 additions & 15 deletions
This file was deleted.

unittest/libmariadb/certs/client-key-enc.pem

Lines changed: 0 additions & 17 deletions
This file was deleted.

unittest/libmariadb/certs/client-key.pem

Lines changed: 0 additions & 15 deletions
This file was deleted.

unittest/libmariadb/certs/server-cert.pem

Lines changed: 0 additions & 15 deletions
This file was deleted.

unittest/libmariadb/certs/server-key-enc.pem

Lines changed: 0 additions & 17 deletions
This file was deleted.

unittest/libmariadb/certs/server-key.pem

Lines changed: 0 additions & 15 deletions
This file was deleted.

unittest/libmariadb/ssl.c.in

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ static int test_ssl(MYSQL *mysql)
6565

6666
if (!skip_ssl)
6767
{
68-
rc= mysql_query(mysql, "DROP USER 'ssltest'@'localhost'");
69-
68+
rc= mysql_query(mysql, "DROP USER 'ssluser'@'localhost'");
7069
rc= mysql_query(mysql, "GRANT ALL ON test.* TO 'ssluser'@'localhost' IDENTIFIED BY 'sslpw' REQUIRE SSL");
7170
rc= mysql_query(mysql, "FLUSH PRVILEGES");
7271
}
@@ -96,6 +95,40 @@ static int test_ssl_cipher(MYSQL *unused)
9695
return OK;
9796
}
9897

98+
static int test_conc95(MYSQL *my)
99+
{
100+
MYSQL *mysql;
101+
int rc;
102+
103+
if (check_skip_ssl())
104+
return SKIP;
105+
106+
rc= mysql_query(my, "DROP USER 'ssluser1'@'localhost'");
107+
check_mysql_rc(rc, my);
108+
rc= mysql_query(my, "GRANT ALL ON test.* TO 'ssluser1'@'localhost' IDENTIFIED BY 'sslpw' REQUIRE X509");
109+
check_mysql_rc(rc, my);
110+
rc= mysql_query(my, "FLUSH PRIVILEGES");
111+
check_mysql_rc(rc, my);
112+
113+
mysql= mysql_init(mysql);
114+
mysql_ssl_set(mysql,
115+
"@CMAKE_SOURCE_DIR@/unittest/libmariadb/certs/server-key.pem",
116+
"@CMAKE_SOURCE_DIR@/unittest/libmariadb/certs/server-cert.pem",
117+
"@CMAKE_SOURCE_DIR@/unittest/libmariadb/certs/ca.pem",
118+
NULL,
119+
NULL);
120+
121+
if (!mysql_real_connect(mysql, hostname, "ssluser1", sslpw, schema,
122+
port, socketname, 0))
123+
{
124+
mysql_close(mysql);
125+
diag("could not establish x509 connection");
126+
return FAIL;
127+
}
128+
mysql_close(mysql);
129+
return OK;
130+
}
131+
99132
static int test_multi_ssl_connections(MYSQL *unused)
100133
{
101134
MYSQL *mysql[50], *my;
@@ -347,6 +380,7 @@ static int test_conc50_3(MYSQL *my)
347380

348381
mysql_real_connect(mysql, hostname, "ssltest", NULL, schema,
349382
port, socketname, 0);
383+
diag("Error: %s<", mysql_error(mysql));
350384
FAIL_IF(mysql_errno(mysql), "No error expected");
351385
mysql_close(mysql);
352386

@@ -367,7 +401,7 @@ static int test_conc50_4(MYSQL *my)
367401

368402
mysql_real_connect(mysql, hostname, ssluser, sslpw, schema,
369403
port, socketname, 0);
370-
FAIL_IF(mysql_errno(mysql) , "Expected no error");
404+
FAIL_IF(!mysql_errno(mysql) , "Error expected");
371405
mysql_close(mysql);
372406

373407
return OK;
@@ -381,9 +415,6 @@ static int verify_ssl_server_cert(MYSQL *my)
381415
if (check_skip_ssl())
382416
return SKIP;
383417

384-
diag("certs needs to be fixed.");
385-
return SKIP;
386-
387418
mysql= mysql_init(NULL);
388419
FAIL_IF(!mysql, "Can't allocate memory");
389420

@@ -460,6 +491,7 @@ struct my_tests_st my_tests[] = {
460491
{"test_conc50_2", test_conc50_2, TEST_CONNECTION_NEW, 0, NULL, NULL},
461492
{"test_conc50_3", test_conc50_3, TEST_CONNECTION_NEW, 0, NULL, NULL},
462493
{"test_conc50_4", test_conc50_4, TEST_CONNECTION_NEW, 0, NULL, NULL},
494+
{"test_conc95", test_conc95, TEST_CONNECTION_NEW, 0, NULL, NULL},
463495
{"verify_ssl_server_cert", verify_ssl_server_cert, TEST_CONNECTION_NEW, 0, NULL, NULL},
464496
{"test_bug62743", test_bug62743, TEST_CONNECTION_NEW, 0, NULL, NULL},
465497
{"test_phpbug51647", test_phpbug51647, TEST_CONNECTION_NONE, 0, NULL, NULL},

0 commit comments

Comments
 (0)