Skip to content

Commit cc12a35

Browse files
committed
MDEV-7697 Client reports ERROR 2006 (MySQL server has gone away) or ERROR 2013 (Lost connection to MySQL server during query) while executing AES* functions under SSL
Clear OpenSSL error queue after an error in AES_ENCRYPT/AES_DECRYPT. Otherwise it might affect current ssl-encrypted connection.
1 parent f875c9f commit cc12a35

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

mysql-test/r/ssl.result

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,3 +2166,9 @@ drop table t1;
21662166
SHOW STATUS LIKE 'Ssl_cipher';
21672167
Variable_name Value
21682168
Ssl_cipher DHE-RSA-AES256-SHA
2169+
select aes_decrypt('MySQL','adf');
2170+
aes_decrypt('MySQL','adf')
2171+
NULL
2172+
select 'still connected?';
2173+
still connected?
2174+
still connected?

mysql-test/t/ssl.test

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ SHOW STATUS LIKE 'Ssl_server_not_after';
2525
--replace_result DHE-RSA-AES256-GCM-SHA384 DHE-RSA-AES256-SHA
2626
SHOW STATUS LIKE 'Ssl_cipher';
2727

28+
#
29+
# MDEV-7697 Client reports ERROR 2006 (MySQL server has gone away) or ERROR 2013 (Lost connection to MySQL server during query) while executing AES* functions under SSL
30+
#
31+
select aes_decrypt('MySQL','adf');
32+
select 'still connected?';
33+
2834
connection default;
2935
disconnect ssl_con;
3036

3137
# Wait till all disconnects are completed
3238
--source include/wait_until_count_sessions.inc
33-
34-
## This test file is for testing encrypted communication only, not other
35-
## encryption routines that the SSL library happens to provide!

mysys_ssl/my_aes.cc

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#elif defined(HAVE_OPENSSL)
2525
#include <openssl/aes.h>
2626
#include <openssl/evp.h>
27+
#include <openssl/err.h>
2728

2829
// Wrap C struct, to ensure resources are released.
2930
struct MyCipherCtx
@@ -165,14 +166,17 @@ int my_aes_encrypt(const char* source, int source_length, char* dest,
165166
#elif defined(HAVE_OPENSSL)
166167
if (! EVP_EncryptInit(&ctx.ctx, EVP_aes_128_ecb(),
167168
(const unsigned char *) rkey, NULL))
168-
return AES_BAD_DATA; /* Error */
169+
goto err;
169170
if (! EVP_EncryptUpdate(&ctx.ctx, (unsigned char *) dest, &u_len,
170171
(unsigned const char *) source, source_length))
171-
return AES_BAD_DATA; /* Error */
172+
goto err;
172173
if (! EVP_EncryptFinal(&ctx.ctx, (unsigned char *) dest + u_len, &f_len))
173-
return AES_BAD_DATA; /* Error */
174+
goto err;
174175

175176
return u_len + f_len;
177+
err:
178+
ERR_remove_state(0);
179+
return AES_BAD_DATA;
176180
#endif
177181
}
178182

@@ -248,13 +252,16 @@ int my_aes_decrypt(const char *source, int source_length, char *dest,
248252
#elif defined(HAVE_OPENSSL)
249253
if (! EVP_DecryptInit(&ctx.ctx, EVP_aes_128_ecb(),
250254
(const unsigned char *) rkey, NULL))
251-
return AES_BAD_DATA; /* Error */
255+
goto err;
252256
if (! EVP_DecryptUpdate(&ctx.ctx, (unsigned char *) dest, &u_len,
253257
(unsigned const char *) source, source_length))
254-
return AES_BAD_DATA; /* Error */
258+
goto err;
255259
if (! EVP_DecryptFinal(&ctx.ctx, (unsigned char *) dest + u_len, &f_len))
256-
return AES_BAD_DATA; /* Error */
260+
goto err;
257261
return u_len + f_len;
262+
err:
263+
ERR_remove_state(0);
264+
return AES_BAD_DATA;
258265
#endif
259266
}
260267

0 commit comments

Comments
 (0)