1. 证书格式

HTTPS 使用了PKI技术,其主要作用是绑定证书持有者信息和相关的密钥对,其中 HTTPS 证书的格式采用了X.509。
用户的证书,公私钥对一般分为两种方式保存:

  • 公钥和证书放在一个文件中,私钥单独存放在一个文件中
  • 公钥、证书和私钥共同放在一个文件中

网上生成证书的教程,涉及许多证书格式,这里列举几种最常见的证书格式以作说明。

后缀 说明 特点
p12 PCKS#12标准证书 包含证书和公钥,和使用了密码保护的私钥
pem PEM BASED64位编码的 DER 文件 包含证书和公钥,可包含私钥
cer crt der DER 二进制编码的X.509证书 包含证书和公钥
key BASED64位编码的私钥文件 包含证书和公钥
jks Java的keytools证书工具支持的证书私钥格式 包含私钥
csr X.509证书请求文件。CA签名后生成crt证书 X.509证书请求文件。CA签名后生成crt证书

2. 制作证书

本文使用openssl制作证书,在一个单独的目录中,进行以下操作

2.1 制作根证书

  1. # 1.制作证书私钥
  2. $ openssl genrsa -out root-key.key 1024
  3. # 2.创建根证书请求文件,需要填写一些身份信息
  4. $ openssl req -new -out root-req.csr -key root-key.key
  5. You are about to be asked to enter information that will be incorporated
  6. into your certificate request.
  7. What you are about to enter is what is called a Distinguished Name or a DN.
  8. There are quite a few fields but you can leave some blank
  9. For some fields there will be a default value,
  10. If you enter '.', the field will be left blank.
  11. -----
  12. Country Name (2 letter code) [XX]:cn //国家
  13. State or Province Name (full name) []:beijing //地区,本行和下一行一致
  14. Locality Name (eg, city) [Default City]:beijing //地区
  15. Organization Name (eg, company) [Default Company Ltd]:nr //公司,本行和下一行一致
  16. Organizational Unit Name (eg, section) []:nr //公司
  17. Common Name (eg, your name or your server hostname) []:root //服务器名
  18. Email Address []: //不用填,直接回车
  19. Please enter the following 'extra' attributes
  20. to be sent with your certificate request
  21. A challenge password []: //不用填,直接回车
  22. An optional company name []: //不用填,直接回车
  23. # 3.自签根证书
  24. $ openssl x509 -req -in root-req.csr -out root-cert.cer -signkey root-key.key -CAcreateserial -days 3650
  25. # 4.生成p12格式根证书,密码123456
  26. openssl pkcs12 -export -clcerts -in root-cert.cer -inkey root-key.key -out root.p12

2.2 制作服务端证书

  1. # 1.生成服务端key
  2. $ openssl genrsa -out server-key.key 1024
  3. # 2.生成服务端请求文件
  4. $ openssl req -new -out server-req.csr -key server-key.key
  5. You are about to be asked to enter information that will be incorporated
  6. into your certificate request.
  7. What you are about to enter is what is called a Distinguished Name or a DN.
  8. There are quite a few fields but you can leave some blank
  9. For some fields there will be a default value,
  10. If you enter '.', the field will be left blank.
  11. -----
  12. Country Name (2 letter code) [XX]:cn //国家
  13. State or Province Name (full name) []:beijing //地区,本行和下一行一致
  14. Locality Name (eg, city) [Default City]:beijing //地区
  15. Organization Name (eg, company) [Default Company Ltd]:nr //公司,本行和下一行一致
  16. Organizational Unit Name (eg, section) []:nr //公司
  17. Common Name (eg, your name or your server hostname) []:server //服务器名
  18. Email Address []: //不用填,直接回车
  19. Please enter the following 'extra' attributes
  20. to be sent with your certificate request
  21. A challenge password []: //不用填,直接回车
  22. An optional company name []: //不用填,直接回车
  23. # 3.生成服务端证书
  24. $ openssl x509 -req -in server-req.csr -out server-cert.cer -signkey server-key.key -CA root-cert.cer -CAkey root-key.key -CAcreateserial -days 3650
  25. # 4.生成p12格式服务端证书,密码123456
  26. $ openssl pkcs12 -export -clcerts -in server-cert.cer -inkey server-key.key -out server.p12

2.3 制作客户端证书

  1. # 1.生成客户端key
  2. $ openssl genrsa -out client-key.key 1024
  3. # 2.生成客户端请求文件
  4. $ openssl req -new -out client-req.csr -key client-key.key
  5. You are about to be asked to enter information that will be incorporated
  6. into your certificate request.
  7. What you are about to enter is what is called a Distinguished Name or a DN.
  8. There are quite a few fields but you can leave some blank
  9. For some fields there will be a default value,
  10. If you enter '.', the field will be left blank.
  11. -----
  12. Country Name (2 letter code) [XX]:cn //国家
  13. State or Province Name (full name) []:beijing //地区,本行和下一行一致
  14. Locality Name (eg, city) [Default City]:beijing //地区
  15. Organization Name (eg, company) [Default Company Ltd]:nr //公司,本行和下一行一致
  16. Organizational Unit Name (eg, section) []:nr //公司
  17. Common Name (eg, your name or your client hostname) []:client //服务器名
  18. Email Address []: //不用填,直接回车
  19. Please enter the following 'extra' attributes
  20. to be sent with your certificate request
  21. A challenge password []: //不用填,直接回车
  22. An optional company name []: //不用填,直接回车
  23. # 3.生成客户端证书
  24. $ openssl x509 -req -in client-req.csr -out client-cert.cer -signkey client-key.key -CA root-cert.cer -CAkey root-key.key -CAcreateserial -days 3650
  25. # 4.生成p12格式客户端证书,密码123456
  26. $ openssl pkcs12 -export -clcerts -in client-cert.cer -inkey client-key.key -out client.p12

2.4 制作服务端信任库

  1. # 制作信任库
  2. $ keytool -keystore truststore.jks -keypass 123456 -storepass 123456 -alias ca -import -trustcacerts -file root-cert.cer

2.5 文件列表

  1. # 当前目录下所有文件
  2. $ ls
  3. root-key.key // 根证书 key
  4. root-req.csr // 根证书请求文件
  5. root-cert.cer // 根证书,只包含公钥
  6. root.p12 // 根证书,包含公钥和私钥,私钥用密码进行了保护
  7. root-cert.srl
  8. server-key.key // 服务端证书 key
  9. server-req.csr // 服务端证书请求文件
  10. server-cert.cer // 服务端证书,只包含公钥
  11. server.p12 // 服务端证书,包含公钥和私钥,私钥用密码进行了保护
  12. client-key.key // 客户端证书 key
  13. client-req.csr // 客户端证书请求文件
  14. client-cert.cer // 服务端证书,只包含公钥
  15. client.p12 // 服务端证书,包含公钥和私钥,私钥用密码进行了保护
  16. truststore.jks // 服务端信任库

3. 单向认证

3.1 Tomcat 单向认证

  1. 生成相关证书
    需要 server.p12 ,详见第二章。
  2. 修改配置文件
    修改tomcat /conf/server.xml文件,在 Server->Service 标签内加入如下 Connector 配置

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <Server port="8005" shutdown="SHUTDOWN">
    3. <Service name="Catalina">
    4. <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
    5. maxThreads="150" SSLEnabled="true" scheme="https"
    6. secure="true" sslProtocol="TLS"
    7. keystoreFile="server.p12" keystorePass="123456" />
    8. </Service>
    9. </Server>
  3. 验证
    访问 https://localhost:8443

    3.2 Nginx 单项认证

  4. 生成相关证书
    需要 server-key.key 和 server-cert.cer ,详见第二章。

  5. 修改配置文件
    修改 /etc/nginx/conf.d/default.conf 配置文件,修改或添加下面内容

    1. server {
    2. listen 443 ssl; # 在 443 端口开启https
    3. ssl_certificate server-cert.cer; # 服务端证书,只包含公钥
    4. ssl_certificate_key server-key.key; # 服务端私钥
    5. }
  6. 验证
    访问 https://localhost

    3.3 WebLogic 单项认证

    weblogic配置https使用可视化方式,具体操作见下方博客。
    https://blog.csdn.net/u013310119/article/details/80182548

    4. 双向认证

    4.1 Tomcat 双向认证

  7. 生成相关证书
    需要 server.p12 、truststore.jks 和 client.p12 ,详见第二章。

  8. 修改配置文件
    修改tomcat /conf/server.xml文件,在 Server->Service 标签内加入如下 Connector 配置

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <Server port="8005" shutdown="SHUTDOWN">
    3. <Service name="Catalina">
    4. <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
    5. maxThreads="150" SSLEnabled="true" scheme="https"
    6. secure="true" clientAuth="true" sslProtocol="TLS"
    7. keystoreFile="server.p12" keystorePass="123456"
    8. truststoreFile="truststore.jks" truststorePass="123456"/>
    9. </Service>
    10. </Server>
  9. 验证
    访问 https://localhost:8443

    4.2 Nginx 双向认证

  10. 生成相关证书
    需要 server-key.key 、server-cert.cer 、 root-cert.cer 和 client.p12 ,详见第二章。

  11. 修改配置文件
    修改 /etc/nginx/conf.d/default.conf 配置文件,修改或添加下面内容 ```yaml server { listen 443 ssl; # 在 443 端口开启https ssl_certificate server-cert.cer; # 服务端证书,只包含公钥 ssl_certificate_key server-key.key; # 服务端私钥

    ssl_verify_client on; # 开启客户端证书验证 ssl_client_certificate root-cert.cer; # 根证书

} ```

  1. 验证
    访问 https://localhost

    4.3 WebLogic 双向认证

    weblogic配置https使用可视化方式,具体操作见下方博客。
    https://blog.csdn.net/u013310119/article/details/80182548