Skip to content

Commit 86ec201

Browse files
committed
MDEV-14091 Support password protected SSL key in server.
Add ssl_passphrase server parameter, which works similarly to --passout/--passin openssl command line parameters. Pass phrase value can be formatted as follows. - pass:password Provide actual password after the pass: prefix. - env:var Obtain the password from the environment variable 'var'a - file:pathname Reads the password from the specified file pathname. Only the first line, up to the newline character, is read from the stream. If ssl_passphrase was set, SHOW VARIABLE will show "file:", "env:" or "pass:" (but won't reveal sensitive data)
1 parent 173b16f commit 86ec201

19 files changed

+365
-10
lines changed

include/violite.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ struct st_VioSSLFd
191191
const char *ca_file,const char *ca_path,
192192
const char *cipher, enum enum_ssl_init_error *error,
193193
const char *crl_file, const char *crl_path,
194-
ulonglong tls_version);
194+
ulonglong tls_version, const char *passphrase);
195195
void free_vio_ssl_acceptor_fd(struct st_VioSSLFd *fd);
196196
#endif /* HAVE_OPENSSL */
197197

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
MySecretPass
2+

mysql-test/lib/generate-ssl-certs.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ openssl req -x509 -newkey rsa:4096 -keyout cakey.pem -out cacert.pem -days 7300
2424
openssl req -newkey rsa:4096 -keyout server-key.pem -out demoCA/server-req.pem -days 7300 -nodes -subj '/CN=localhost/C=FI/ST=state or province within country, in other certificates in this file it is the same as L/L=location, usually an address but often ambiguously used/OU=organizational unit name, a division name within an organization/O=organization name, typically a company name'
2525
# convert the key to yassl compatible format
2626
openssl rsa -in server-key.pem -out server-key.pem
27+
# also create a password-protected server key
28+
echo MySecretPass > encrypted-server-key-password.txt
29+
openssl rsa -aes256 -in server-key.pem -out encrypted-server-key.pem -passout file:encrypted-server-key-password.txt
2730
# sign the server certificate with CA certificate
2831
openssl ca -keyfile cakey.pem -days 7300 -batch -cert cacert.pem -policy policy_anything -out server-cert.pem -in demoCA/server-req.pem
2932

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
FOUND 1 /\[ERROR\] SSL error: Unable to get certificate/ in errorlog.err
22
FOUND 1 /\[ERROR\] SSL error: Failed to set ciphers to use/ in errorlog.err
3+
FOUND 1 /\[ERROR\] SSL error: Unable to get private key/ in errorlog.err
4+
FOUND 1 /SSL passphrase error: failed to open file 'BadFile'/ in errorlog.err
5+
FOUND 1 /\[ERROR\] SSL error: Unable to get private key/ in errorlog.err
6+
FOUND 1 /\[Warning\] ssl passphrase file '.*' is not secure/ in errorlog.err
7+
FOUND 1 /\[ERROR\] SSL error: Unable to get private key/ in errorlog.err
8+
FOUND 1 /SSL passphrase error: environment variable 'BadEnv' not found/ in errorlog.err
9+
FOUND 1 /\[ERROR\] SSL error: Unable to get private key/ in errorlog.err
10+
FOUND 1 /SSL passphrase error: ssl-passphrase value must be prefixed with 'file:', 'env:', or 'pass:'/ in errorlog.err
11+
FOUND 1 /\[ERROR\] SSL error: Unable to get private key/ in errorlog.err
312
# restart

mysql-test/main/bad_startup_options.test

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,50 @@
2626
--source include/search_pattern_in_file.inc
2727
--remove_file $SEARCH_FILE
2828

29+
# Wrong SSL passphrase(pass)
30+
--error 1
31+
--exec $MYSQLD --defaults-group-suffix=.1 --defaults-file=$MYSQLTEST_VARDIR/my.cnf --ssl-key=$MYSQL_TEST_DIR/std_data/encrypted-server-key.pem --ssl-passphrase=pass:BadPassword --log-error=$errorlog
32+
--let SEARCH_PATTERN=\[ERROR\] SSL error: Unable to get private key
33+
--source include/search_pattern_in_file.inc
34+
--remove_file $SEARCH_FILE
35+
36+
# Bad SSL passphrase(file)
37+
--error 1
38+
--exec $MYSQLD --defaults-group-suffix=.1 --defaults-file=$MYSQLTEST_VARDIR/my.cnf --secure-file-priv=$MYSQLTEST_VARDIR/tmp --ssl-key=$MYSQL_TEST_DIR/std_data/encrypted-server-key.pem --ssl-passphrase=file:BadFile --log-error=$errorlog
39+
--let SEARCH_PATTERN=SSL passphrase error: failed to open file 'BadFile'
40+
--source include/search_pattern_in_file.inc
41+
--let SEARCH_PATTERN=\[ERROR\] SSL error: Unable to get private key
42+
--source include/search_pattern_in_file.inc
43+
--remove_file $SEARCH_FILE
44+
45+
46+
# Check "insecure SSL passphrase file location" warning (secure-file-priv empty)
47+
# We still let it fail later, by supply non-existing file
48+
--error 1
49+
--exec $MYSQLD --defaults-group-suffix=.1 --defaults-file=$MYSQLTEST_VARDIR/my.cnf --secure-file-priv= --ssl-key=$MYSQL_TEST_DIR/std_data/encrypted-server-key.pem --ssl-passphrase=file:$BadFile --log-error=$errorlog
50+
--let SEARCH_PATTERN=\[Warning\] ssl passphrase file '.*' is not secure
51+
--source include/search_pattern_in_file.inc
52+
--let SEARCH_PATTERN=\[ERROR\] SSL error: Unable to get private key
53+
--source include/search_pattern_in_file.inc
54+
--remove_file $SEARCH_FILE
55+
56+
57+
# Bad SSL passphrase(env)
58+
--error 1
59+
--exec $MYSQLD --defaults-group-suffix=.1 --defaults-file=$MYSQLTEST_VARDIR/my.cnf --ssl-key=$MYSQL_TEST_DIR/std_data/encrypted-server-key.pem --ssl-passphrase=env:BadEnv --log-error=$errorlog
60+
--let SEARCH_PATTERN=SSL passphrase error: environment variable 'BadEnv' not found
61+
--source include/search_pattern_in_file.inc
62+
--let SEARCH_PATTERN=\[ERROR\] SSL error: Unable to get private key
63+
--source include/search_pattern_in_file.inc
64+
--remove_file $SEARCH_FILE
65+
66+
# Bad SSL passphrase(invalid prefix)
67+
--error 1
68+
--exec $MYSQLD --defaults-group-suffix=.1 --defaults-file=$MYSQLTEST_VARDIR/my.cnf --ssl-key=$MYSQL_TEST_DIR/std_data/encrypted-server-key.pem --ssl-passphrase=BadVal --log-error=$errorlog
69+
--let SEARCH_PATTERN=SSL passphrase error: ssl-passphrase value must be prefixed with 'file:', 'env:', or 'pass:'
70+
--source include/search_pattern_in_file.inc
71+
--let SEARCH_PATTERN=\[ERROR\] SSL error: Unable to get private key
72+
--source include/search_pattern_in_file.inc
73+
--remove_file $SEARCH_FILE
74+
2975
--source include/start_mysqld.inc
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--loose-ssl_key=$MYSQL_TEST_DIR/std_data/encrypted-server-key.pem

mysql-test/main/ssl_encrypted_key.cnf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
!include include/default_my.cnf
2+
3+
[ENV]
4+
SSL_KEY_PASSWORD=MySecretPass
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[pass]
2+
loose-ssl_passphrase=pass:MySecretPass
3+
4+
[env]
5+
loose-ssl_passphrase=env:SSL_KEY_PASSWORD
6+
7+
[file]
8+
loose-ssl_passphrase=file:$MYSQL_TEST_DIR/std_data/encrypted-server-key-password.txt
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
SELECT @@ssl_passphrase;
2+
@@ssl_passphrase
3+
pass:
4+
have_ssl
5+
1
6+
FLUSH SSL;
7+
have_ssl
8+
1
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- source include/have_ssl_communication.inc
2+
--replace_result env pass file pass
3+
SELECT @@ssl_passphrase;
4+
--exec $MYSQL --ssl -e "SELECT (VARIABLE_VALUE <> '') as have_ssl FROM INFORMATION_SCHEMA.SESSION_STATUS WHERE VARIABLE_NAME='Ssl_cipher'" 2>&1
5+
FLUSH SSL;
6+
--exec $MYSQL --ssl -e "SELECT (VARIABLE_VALUE <> '') as have_ssl FROM INFORMATION_SCHEMA.SESSION_STATUS WHERE VARIABLE_NAME='Ssl_cipher'" 2>&1

0 commit comments

Comments
 (0)