Spec-Zone .ru
спецификации, руководства, описания, API

6.3.9.5. Setting Up SSL Certificates and Keys for MySQL

This section demonstrates how to set up SSL certificate and key files for use by MySQL servers and clients. The first example shows a simplified procedure such as you might use from the command line. The second shows a script that contains more detail. The first two examples are intended for use on Unix and both use the openssl command that is part of OpenSSL. The third example describes how to set up SSL files on Windows.

Example 1: Creating SSL Files from the Command Line on Unix

The following example shows a set of commands to create MySQL server and client certificate and key files. You will need to respond to several prompts by the openssl commands. To generate test files, you can press Enter to all prompts. To generate files for production use, you should provide nonempty responses.

# Create clean environmentshell> rm -rf
        newcertsshell> mkdir newcerts && cd
        newcerts# Create CA certificateshell> openssl genrsa 2048
        > ca-key.pemshell> openssl req -new -x509 -nodes -days
        3600 \         -key ca-key.pem -out ca-cert.pem# Create server certificate, remove passphrase, and sign it# server-cert.pem = public key, server-key.pem = private keyshell> openssl req -newkey rsa:2048 -days 3600 \         -nodes -keyout server-key.pem -out server-req.pemshell> openssl rsa -in server-key.pem -out server-key.pemshell> openssl x509 -req -in server-req.pem -days 3600 \         -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out
        server-cert.pem# Create client certificate, remove passphrase, and sign it# client-cert.pem = public key, client-key.pem = private keyshell> openssl req -newkey rsa:2048 -days 3600 \         -nodes -keyout client-key.pem -out client-req.pemshell> openssl rsa -in client-key.pem -out client-key.pemshell> openssl x509 -req -in client-req.pem -days 3600 \         -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out
        client-cert.pem

After generating the certificates, verify them:

shell> openssl verify -CAfile ca-cert.pem
        server-cert.pem client-cert.pemserver-cert.pem: OKclient-cert.pem: OK

Now you have a set of files that can be used as follows:

To use the files to test SSL connections, see Section 6.3.9.3, "Using SSL Connections".

Example 2: Creating SSL Files Using a Script on Unix

Here is an example script that shows how to set up SSL certificate and key files for MySQL. After executing the script, use the files to test SSL connections as described in Section 6.3.9.3, "Using SSL Connections".

DIR=`pwd`/opensslPRIV=$DIR/privatemkdir $DIR $PRIV $DIR/newcertscp /usr/share/ssl/openssl.cnf $DIRreplace ./demoCA $DIR -- $DIR/openssl.cnf# Create necessary files: $database, $serial and $new_certs_dir# directory (optional)touch $DIR/index.txtecho "01" > $DIR/serial## Generation of Certificate Authority(CA)#openssl req -new -x509 -keyout $PRIV/cakey.pem -out $DIR/ca-cert.pem \    -days 3600 -config $DIR/openssl.cnf# Sample output:# Using configuration from /home/monty/openssl/openssl.cnf# Generating a 1024 bit RSA private key# ................++++++# .........++++++# writing new private key to '/home/monty/openssl/private/cakey.pem'# Enter PEM pass phrase:# Verifying password - Enter PEM pass phrase:# -----# You are about to be asked to enter information that will be# incorporated into your certificate request.# What you are about to enter is what is called a Distinguished Name# or a DN.# There are quite a few fields but you can leave some blank# For some fields there will be a default value,# If you enter '.', the field will be left blank.# -----# Country Name (2 letter code) [AU]:FI# State or Province Name (full name) [Some-State]:.# Locality Name (eg, city) []:# Organization Name (eg, company) [Internet Widgits Pty Ltd]:MySQL AB# Organizational Unit Name (eg, section) []:# Common Name (eg, YOUR name) []:MySQL admin# Email Address []:## Create server request and key#openssl req -new -keyout $DIR/server-key.pem -out \    $DIR/server-req.pem -days 3600 -config $DIR/openssl.cnf# Sample output:# Using configuration from /home/monty/openssl/openssl.cnf# Generating a 1024 bit RSA private key# ..++++++# ..........++++++# writing new private key to '/home/monty/openssl/server-key.pem'# Enter PEM pass phrase:# Verifying password - Enter PEM pass phrase:# -----# You are about to be asked to enter information that will be# incorporated into your certificate request.# What you are about to enter is what is called a Distinguished Name# or a DN.# There are quite a few fields but you can leave some blank# For some fields there will be a default value,# If you enter '.', the field will be left blank.# -----# Country Name (2 letter code) [AU]:FI# State or Province Name (full name) [Some-State]:.# Locality Name (eg, city) []:# Organization Name (eg, company) [Internet Widgits Pty Ltd]:MySQL AB# Organizational Unit Name (eg, section) []:# Common Name (eg, YOUR name) []:MySQL server# Email Address []:## Please enter the following 'extra' attributes# to be sent with your certificate request# A challenge password []:# An optional company name []:## Remove the passphrase from the key#openssl rsa -in $DIR/server-key.pem -out $DIR/server-key.pem## Sign server cert#openssl ca -cert $DIR/ca-cert.pem -policy policy_anything \    -out $DIR/server-cert.pem -config $DIR/openssl.cnf \    -infiles $DIR/server-req.pem# Sample output:# Using configuration from /home/monty/openssl/openssl.cnf# Enter PEM pass phrase:# Check that the request matches the signature# Signature ok# The Subjects Distinguished Name is as follows# countryName           :PRINTABLE:'FI'# organizationName      :PRINTABLE:'MySQL AB'# commonName            :PRINTABLE:'MySQL admin'# Certificate is to be certified until Sep 13 14:22:46 2003 GMT# (365 days)# Sign the certificate? [y/n]:y### 1 out of 1 certificate requests certified, commit? [y/n]y# Write out database with 1 new entries# Data Base Updated## Create client request and key#openssl req -new -keyout $DIR/client-key.pem -out \    $DIR/client-req.pem -days 3600 -config $DIR/openssl.cnf# Sample output:# Using configuration from /home/monty/openssl/openssl.cnf# Generating a 1024 bit RSA private key# .....................................++++++# .............................................++++++# writing new private key to '/home/monty/openssl/client-key.pem'# Enter PEM pass phrase:# Verifying password - Enter PEM pass phrase:# -----# You are about to be asked to enter information that will be# incorporated into your certificate request.# What you are about to enter is what is called a Distinguished Name# or a DN.# There are quite a few fields but you can leave some blank# For some fields there will be a default value,# If you enter '.', the field will be left blank.# -----# Country Name (2 letter code) [AU]:FI# State or Province Name (full name) [Some-State]:.# Locality Name (eg, city) []:# Organization Name (eg, company) [Internet Widgits Pty Ltd]:MySQL AB# Organizational Unit Name (eg, section) []:# Common Name (eg, YOUR name) []:MySQL user# Email Address []:## Please enter the following 'extra' attributes# to be sent with your certificate request# A challenge password []:# An optional company name []:## Remove the passphrase from the key#openssl rsa -in $DIR/client-key.pem -out $DIR/client-key.pem## Sign client cert#openssl ca -cert $DIR/ca-cert.pem -policy policy_anything \    -out $DIR/client-cert.pem -config $DIR/openssl.cnf \    -infiles $DIR/client-req.pem# Sample output:# Using configuration from /home/monty/openssl/openssl.cnf# Enter PEM pass phrase:# Check that the request matches the signature# Signature ok# The Subjects Distinguished Name is as follows# countryName           :PRINTABLE:'FI'# organizationName      :PRINTABLE:'MySQL AB'# commonName            :PRINTABLE:'MySQL user'# Certificate is to be certified until Sep 13 16:45:17 2003 GMT# (365 days)# Sign the certificate? [y/n]:y### 1 out of 1 certificate requests certified, commit? [y/n]y# Write out database with 1 new entries# Data Base Updated## Create a my.cnf file that you can use to test the certificates#cat <<EOF > $DIR/my.cnf[client]ssl-ca=$DIR/ca-cert.pemssl-cert=$DIR/client-cert.pemssl-key=$DIR/client-key.pem[mysqld]ssl-ca=$DIR/ca-cert.pemssl-cert=$DIR/server-cert.pemssl-key=$DIR/server-key.pemEOF

Example 3: Creating SSL Files on Windows

Download OpenSSL for Windows if it is not installed on your system. An overview of available packages can be seen here:

http://www.slproweb.com/products/Win32OpenSSL.html

Choose the Win32 OpenSSL Light or Win64 OpenSSL Light package, depending on your architecture (32-bit or 64-bit). The default installation location will be C:\OpenSSL-Win32 or C:\OpenSSL-Win64, depending on which package you downloaded. The following instructions assume a default location of C:\OpenSSL-Win32. Modify this as necessary if you are using the 64-bit package.

If a message occurs during setup indicating '...critical component is missing: Microsoft Visual C++ 2008 Redistributables', cancel the setup and download one of the following packages as well, again depending on your architecture (32-bit or 64-bit):

After installing the additional package, restart the OpenSSL setup procedure.

During installation, leave the default C:\OpenSSL-Win32 as the install path, and also leave the default option 'Copy OpenSSL DLL files to the Windows system directory' selected.

When the installation has finished, add C:\OpenSSL-Win32\bin to the Windows System Path variable of your server:

  1. On the Windows desktop, right-click the My Computer icon, and select Properties.

  2. Select the Advanced tab from the System Properties menu that appears, and click the Environment Variables button.

  3. Under System Variables, select Path, then click the Edit button. The Edit System Variable dialogue should appear.

  4. Add ';C:\OpenSSL-Win32\bin' to the end (notice the semicolon).

  5. Press OK 3 times.

  6. Check that OpenSSL was correctly integrated into the Path variable by opening a new command console (Start>Run>cmd.exe) and verifying that OpenSSL is available:

    Microsoft Windows [Version ...]Copyright (c) 2006 Microsoft Corporation. All rights reserved.C:\Windows\system32>cd \C:\>opensslOpenSSL> exit <<< If you see the OpenSSL prompt, installation was successful.C:\>

Depending on your version of Windows, the preceding path-setting instructions might differ slightly.

After OpenSSL has been installed, use instructions similar to those from from Example 1 (shown earlier in this section), with the following changes:

After generating the certificate and key files, to use them to test SSL connections, see Section 6.3.9.3, "Using SSL Connections".