• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • 创建SSL和RSA证书和密钥

    以下讨论描述了如何在MySQL中创建SSL和RSA支持所需的文件。可以使用MySQL本身提供的功能或直接调用openssl命令来执行文件创建。

    SSL证书和密钥文件使MySQL支持使用SSL的加密连接。请参见“配置MySQL以使用加密连接”。

    RSA密钥文件使MySQL支持通过sha256_passwordcaching_sha2_password插件认证的帐户通过未加密的连接进行安全密码交换。请参见“ SHA-256可插拔身份验证”和“缓存SHA-2可插拔身份验证”。

    使用MySQL创建SSL和RSA证书和密钥

    MySQL提供了以下方法来创建SSL证书和密钥文件以及RSA密钥对文件,以支持使用SSL的加密连接以及使用RSA通过未加密的连接进行安全密码交换(如果这些文件丢失):

    • 服务器可以在启动时为使用OpenSSL编译的MySQL发行版自动生成这些文件。
    • 用户可以手动调用mysql_ssl_rsa_setup实用程序。
    • 对于某些分发类型,例如RPM软件包,在数据目录初始化期间发生mysql_ssl_rsa_setup调用。在这种情况下,只要openssl命令可用,就不需要使用OpenSSL编译MySQL发行版。
    重要

    服务器自动生成和mysql_ssl_rsa_setup通过使生成所需文件更加容易来帮助降低使用SSL的障碍。但是,通过这些方法生成的证书是自签名的,这可能不是很安全。在获得使用此类文件的经验之后,请考虑从注册的证书颁发机构获取证书/密钥材料。

    • 自动生成SSL和RSA文件
    • 使用mysql_ssl_rsa_setup手动生成SSL和RSA文件
    • SSL和RSA文件特征

    自动生成SSL和RSA文件

    对于使用OpenSSL编译的MySQL发行版,MySQL服务器具有在启动时自动生成丢失的SSL和RSA文件的功能。的 uto_generate_certssha256_password_auto_generate_rsa_keyscaching_sha2_password_auto_generate_rsa_keys系统变量控制自动生成这些文件。默认情况下启用这些变量。它们可以在启动时启用和检查,但不能在运行时设置。

    启动时,如果 uto_generate_certs启用了系统变量,未--ssl指定除SSL选项之外的其他任何选项,并且数据中缺少服务器端SSL文件,则服务器会在数据目录中自动生成服务器端和客户端SSL证书和密钥文件。目录。这些文件使用SSL启用加密的客户端连接。请参见“配置MySQL以使用加密连接”。

    1. 服务器在数据目录中检查具有以下名称的SSL文件:

      ca.pem
      server-cert.pem
      server-key.pem
      
    2. 如果存在这些文件中的任何一个,则服务器不会创建SSL文件。否则,它将创建它们以及一些其他文件:

      ca.pem               Self-signed CA certificate
      ca-key.pem           CA private key
      server-cert.pem      Server certificate
      server-key.pem       Server private key
      client-cert.pem      Client certificate
      client-key.pem       Client private key
      
    3. 如果服务器自动生成SSL文件,它使用的名称ca.pemserver-cert.pem以及server-key.pem文件设置相应的系统变量(ssl_c ssl_certssl_key)。

    在启动时,服务器会自动生成数据目录RSA公钥/私钥对的文件,如果所有这些条件为真:sha256_password_auto_generate_rsa_keyscaching_sha2_password_auto_generate_rsa_keys系统变量启用;未指定RSA选项;数据目录中缺少RSA文件。这些密钥对文件可通过RSA sha256_passwordcaching_sha2_password插件认证的帐户,通过未加密的连接使用RSA进行安全密码交换;请参见“ SHA-256可插拔身份验证”和“缓存SHA-2可插拔身份验证”。

    1. 服务器在数据目录中检查具有以下名称的RSA文件:

      private_key.pem      Private member of private/public key pair
      public_key.pem       Public member of private/public key pair
      
    2. 如果存在这些文件中的任何一个,则服务器不会创建RSA文件。否则,它将创建它们。
    3. 如果服务器自动生成RSA文件,它将使用其名称来设置相应的系统变量(sha256_password_private_key_pathsha256_password_public_key_path;caching_sha2_password_private_key_pathcaching_sha2_password_public_key_path)。
    使用mysql_ssl_rsa_setup手动生成SSL和RSA文件

    MySQL发行版包括mysql_ssl_rsa_setup实用程序,可以手动调用该实用程序以生成SSL和RSA文件。该实用程序包含在所有MySQL发行版中,但是它确实要求openssl命令可用。有关用法说明,

    SSL和RSA文件特征

    服务器或通过调用mysql_ssl_rsa_setup自动创建的SSL和RSA文件具有以下特征:

    • SSL和RSA密钥的大小为2048位。
    • SSL CA证书是自签名的。
    • 使用sha256WithRSAEncryption签名算法,使用CA证书和密钥对SSL服务器和客户端证书进行签名。
    • SSL证书使用以下通用名称(CN)值以及相应的证书类型(CA,服务器,客户端):

      ca.pem:         MySQL_Server_suffix_Auto_Generated_CA_Certificate
      server-cert.pm: MySQL_Server_suffix_Auto_Generated_Server_Certificate
      client-cert.pm: MySQL_Server_suffix_Auto_Generated_Client_Certificate
      

      suffix值基于MySQL版本号。对于由mysql_ssl_rsa_setup生成的文件,可以使用--suffix选项明确指定后缀。

      对于服务器生成的文件,如果结果CN值超过64个字符,则名称的一部分将被省略。_suffix

    • SSL文件的国家(C),州或省(ST),组织(O),组织单位名称(OU)和电子邮件地址的值为空白。
    • 由服务器或mysql_ssl_rsa_setup创建的SSL文件自生成之日起十年内有效。
    • RSA文件不会过期。
    • 每个证书/密钥对的SSL文件具有不同的序列号(CA时1,服务器时2,客户端3)。
    • 服务器自动创建的文件归运行服务器的帐户所有。使用mysql_ssl_rsa_setup创建的文件归调用该程序的用户所有。chown()如果通过调用程序root并提供--uid选项来指定应该拥有文件的用户,则可以在支持系统调用的系统上对此进行更改。
    • 在Unix和类似Unix的系统上,证书文件的文件访问模式为644(即世界可读),密钥文件的文件访问模式为600(即只能由运行服务器的帐户访问)。

    要参见SSL证书的内容(例如,检查它的有效日期范围),请直接调用openssl

    openssl x509 -text -in c .pem
    openssl x509 -text -in server-cert.pem
    openssl x509 -text -in client-cert.pem
    

    也可以使用以下SQL语句检查SSL证书到期信息:

    mysql> SHOW STATUS LIKE 'Ssl_server_not%';
    +-----------------------	+--------------------------	+
    | Variable_name          | Value                     |
    +-----------------------	+--------------------------	+
    | Ssl_server_not_after   | Apr 28 14:16:39 2027 GMT  |
    | Ssl_server_not_before  | May 1 14:16:39 2017 GMT  |
    +-----------------------	+--------------------------	+
    

    使用openssl创建SSL证书和密钥

    本节介绍如何使用openssl命令设置SSL证书和密钥文件,以供MySQL服务器和客户端使用。第一个示例显示了简化的过程,例如您可以在命令行中使用的过程。第二个脚本显示了包含更多细节的脚本。前两个示例旨在在Unix上使用,并且都使用OpenSSL的openssl命令。第三个示例描述了如何在Windows上设置SSL文件。

    注意

    生成SSL所需文件的替代方法比此处描述的过程更简单:让服务器自动生成文件或使用mysql_ssl_rsa_setup程序。

    重要

    无论使用哪种方法生成证书和密钥文件,用于服务器和客户端证书/密钥的“公用名”值都必须与用于CA证书的“公用名”值不同。否则,证书和密钥文件将不适用于使用OpenSSL编译的服务器。这种情况下的典型错误是:

    ERROR 2026 (HY000): SSL connection error:
    error:00000001:lib(0):func(0):reason(1)
    
    • 示例1:在Unix上从命令行创建SSL文件
    • 示例2:在Unix上使用脚本创建SSL文件
    • 示例3:在Windows上创建SSL文件
    示例1:在Unix上从命令行创建SSL文件

    以下示例显示了一组用于创建MySQL服务器和客户端证书以及密钥文件的命令。您将需要通过openssl命令来响应多个提示。要生成测试文件,可以按Enter键进入所有提示。要生成供生产使用的文件,应提供非空响应。

    # Create clean environment
    rm -rf newcerts
    mkdir newcerts && cd newcerts
    
    # Create CA certificate
    openssl genrsa 2048 > ca-key.pem
    openssl req -new -x509 -nodes -days 3600 \
    -key ca-key.pem -out c .pem
    
    # Create server certificate, remove passphrase, and sign it
    # server-cert.pem = public key, server-key.pem = private key
    openssl req -newkey rs :2048 -days 3600 \
    -nodes -keyout server-key.pem -out server-req.pem
    openssl rsa -in server-key.pem -out server-key.pem
    openssl x509 -req -in server-req.pem -days 3600 \
    -CA c .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 key
    openssl req -newkey rs :2048 -days 3600 \
    -nodes -keyout client-key.pem -out client-req.pem
    openssl rsa -in client-key.pem -out client-key.pem
    openssl x509 -req -in client-req.pem -days 3600 \
    -CA c .pem -CAkey ca-key.pem -set_serial 01 -out client-cert.pem
    

    生成证书后,请验证它们:

    openssl verify -CAfile c .pem server-cert.pem client-cert.pem
    

    您应该看到这样的响应:

    server-cert.pem: OK
    client-cert.pem: OK
    

    要参见证书的内容(例如,检查证书有效的日期范围),请像下面这样调用openssl

    openssl x509 -text -in c .pem
    openssl x509 -text -in server-cert.pem
    openssl x509 -text -in client-cert.pem
    

    现在,您可以使用一组文件,如下所示:

    • ca.pem:将其用作--ssl-c 服务器端和客户端上的参数。(如果使用了CA证书,则双方必须相同。)
    • server-cert.pemserver-key.pem:使用这些作为参数--ssl-cert,并--ssl-key在服务器端。
    • client-cert.pemclient-key.pem:使用这些作为参数--ssl-cert,并--ssl-key在客户端。

    有关其他用法说明,请参见“配置MySQL以使用加密连接”。

    示例2:在Unix上使用脚本创建SSL文件

    这是一个示例脚本,显示了如何为MySQL设置SSL证书和密钥文件。执行脚本后,请按照“配置MySQL以使用加密连接”中所述使用文件进行SSL连接。

    DIR=`pwd`/openssl
    PRIV=$DIR/private
    
    mkdir $DIR $PRIV $DIR/newcerts
    cp /usr/share/ssl/openssl.cnf $DIR
    replace ./demoCA $DIR -- $DIR/openssl.cnf
    
    # Create necessary files: $database, $serial and $new_certs_dir
    # directory (optional)
    
    touch $DIR/index.txt
    echo "01" > $DIR/serial
    
    #
    # Generation of Certificate Authority(CA)
    #
    
    openssl req -new -x509 -keyout $PRIV/cakey.pem -out $DIR/c .pem \
    -days 3600 -config $DIR/openssl.cnf
    
    # Sample output:
    # Using configuration from /home/jones/openssl/openssl.cnf
    # Generating a 1024 bit RSA private key
    # ................	+	+	+	+	+	+
    # .........	+	+	+	+	+	+
    # writing new private key to '/home/jones/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/jones/openssl/openssl.cnf
    # Generating a 1024 bit RSA private key
    # ..	+	+	+	+	+	+
    # ..........	+	+	+	+	+	+
    # writing new private key to '/home/jones/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/c .pem -policy policy_anything \
    -out $DIR/server-cert.pem -config $DIR/openssl.cnf \
    -infiles $DIR/server-req.pem
    
    # Sample output:
    # Using configuration from /home/jones/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/jones/openssl/openssl.cnf
    # Generating a 1024 bit RSA private key
    # .....................................	+	+	+	+	+	+
    # .............................................	+	+	+	+	+	+
    # writing new private key to '/home/jones/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/c .pem -policy policy_anything \
    -out $DIR/client-cert.pem -config $DIR/openssl.cnf \
    -infiles $DIR/client-req.pem
    
    # Sample output:
    # Using configuration from /home/jones/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-c =$DIR/ca.pem
    ssl-cert=$DIR/client-cert.pem
    ssl-key=$DIR/client-key.pem
    [mysqld]
    ssl-c =$DIR/ca.pem
    ssl-cert=$DIR/server-cert.pem
    ssl-key=$DIR/server-key.pem
    EOF
    
    示例3:在Windows上创建SSL文件

    如果您的系统上未安装Windows版OpenSSL,请下载。可在此处参见可用软件包的概述:

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

    选择Win32 OpenSSL Light或Win64 OpenSSL Light软件包,具体取决于您的体系结构(32位或64位)。默认安装位置为C:\OpenSSL-Win32C:\OpenSSL-Win64,具体取决于您下载的软件包。以下说明假定默认位置为C:\OpenSSL-Win32。如果使用的是64位软件包,请根据需要进行修改。

    如果在安装过程中出现指示的消息'...critical component is missing: Microsoft Visual C + + 2008 Redistributables',请再次取消安装并下载以下软件包之一,具体取决于您的体系结构(32位还是64位):

    • Visual C + + 2008可再发行组件(x86),位于:

      http://www.microsoft.com/downloads/details.aspx?familyid=9B2DA534-3E03-4391-8A4D-074B9F2BC1BF
      
    • Visual C + + 2008可再发行组件(x64),位于:

      http://www.microsoft.com/downloads/details.aspx?familyid=bd2a6171-e2d6-4230-b809-9a8d7548c1b6
      

    安装其他软件包后,请重新启动OpenSSL设置过程。

    在安装过程中,将默认值保留C:\OpenSSL-Win32为安装路径,并且还将默认选项保留为'Copy OpenSSL DLL files to the Windows system directory'选中状态。

    安装完成后,将其添加C:\OpenSSL-Win32in到服务器的Windows System Path变量中(根据Windows版本的不同,以下路径设置说明可能会略有不同):

    1. 在Windows桌面上,右键单击“我的电脑”图标,然后选择“属性”。
    2. 从出现的“系统属性”菜单中选择“高级”选项卡,然后单击“环境变量”按钮。
    3. 在“系统变量”下,选择“路径”,然后单击“编辑”按钮。将出现“编辑系统变量”对话框。
    4. 添加';C:\OpenSSL-Win32in'到末尾(注意分号)。
    5. 按OK 3次。
    6. 通过打开新的命令控制台(Start> Run> cmd.exe)并验证OpenSSL是否可用,来检查OpenSSL是否已正确集成到Path变量中:

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

    安装OpenSSL之后,使用与示例1(本节前面所示)类似的说明,并进行以下更改:

    • 更改以下Unix命令:

      # Create clean environment
      rm -rf newcerts
      mkdir newcerts && cd newcerts
      

      在Windows上,请改用以下命令:

      # Create clean environment
      md c:\newcerts
      cd c:\newcerts
      
    • 当一个'\'字符显示在命令行的末尾时,'\'必须删除该字符,并且命令行必须全部输入一行。

    生成证书和密钥文件后,要使用它们进行SSL连接,请参见“配置MySQL以使用加密连接”。


    使用openssl创建RSA密钥

    本节介绍如何使用openssl命令设置RSA密钥文件,该文件使MySQL支持通过sha256_passwordcaching_sha2_password插件验证的帐户通过未加密的连接进行安全密码交换。

    注意

    生成RSA所需文件的方法比此处描述的过程更简单:让服务器自动生成文件或使用mysql_ssl_rsa_setup程序。

    要创建RSA私钥和公钥对文件,请在登录用于运行MySQL服务器的系统帐户时运行以下命令,以便该文件归该帐户所有:

    openssl genrsa -out private_key.pem 2048
    openssl rsa -in private_key.pem -pubout -out public_key.pem
    

    这些命令创建2,048位密钥。要创建更强的键,请使用更大的值。

    然后设置密钥文件的访问模式。私钥只能由服务器读取,而公钥可以自由分发给客户端用户:

    chmod 400 private_key.pem
    chmod 444 public_key.pem