构建私有的 CA 机构(拓展)

=========================================================

CA中心申请证书的流程:

过程:
1、web服务器,生成一对非对称加密密钥(web公钥,web私钥)
2、web服务器使用 web私钥生成 web服务器的证书请求文件,并将证书请求发给CA服务器
3、CA服务器使用 CA的私钥 对 web 服务器的证书请求 进行数字签名得到 web服务器的数字证书,并将web服务器的数字证书颁发给web服务器。

1、CA 介绍

CA(Certificate Authority)证书颁发机构主要负责证书的颁发、管理以及归档和吊销。证书内包含了拥有证书者的姓名、地址、电子邮件帐号、公钥、证书有效期、发放证书的CA、CA的数字签名等信息。证书主要有三大功能:加密、签名、身份验证。

2、构建私有 CA

1、检查安装 openssl
  1. [root@https-ca ~]# rpm -qa openssl

image.png

如果未安装

  1. [root@https-ca ~]# yum install openssl openssl-devel -y

2、查看配置文件

openssl 配置/etc/pki/tls/openssl.cnf有关CA的配置。如果服务器为证书签署者的身份那么就会用到此配置文件,此配置文件对于证书申请者是无作用的。

  1. [root@https-ca ~]# vim /etc/pki/tls/openssl.cnf
  2. ####################################################################
  3. [ ca ]
  4. default_ca = CA_default # 默认的CA配置;CA_default指向下面配置块
  5. ####################################################################
  6. [ CA_default ]
  7. dir = /etc/pki/CA # CA的默认工作目录
  8. certs = $dir/certs # 认证证书的目录
  9. crl_dir = $dir/crl # 证书吊销列表的路径
  10. database = $dir/index.txt # 数据库的索引文件
  11. new_certs_dir = $dir/newcerts # 新颁发证书的默认路径
  12. certificate = $dir/cacert.pem # 此服务认证证书,如果此服务器为根CA那么这里为自颁发证书
  13. serial = $dir/serial # 下一个证书的证书编号
  14. crlnumber = $dir/crlnumber # 下一个吊销的证书编号
  15. crl = $dir/crl.pem # The current CRL
  16. private_key = $dir/private/cakey.pem# CA的私钥
  17. RANDFILE = $dir/private/.rand # 随机数文件
  18. x509_extensions = usr_cert # The extentions to add to the cert
  19. name_opt = ca_default # 命名方式,以ca_default定义为准
  20. cert_opt = ca_default # 证书参数,以ca_default定义为准
  21. default_days = 365 # 证书默认有效期
  22. default_crl_days= 30 # CRl的有效期
  23. default_md = sha256 # 加密算法
  24. preserve = no # keep passed DN ordering
  25. policy = policy_match #policy_match策略生效
  26. # For the CA policy
  27. [ policy_match ]
  28. countryName = match #国家;match表示申请者的申请信息必须与此一致
  29. stateOrProvinceName = match #州、省
  30. organizationName = match #组织名、公司名
  31. organizationalUnitName = optional #部门名称;optional表示申请者可以的信息与此可以不一致
  32. commonName = supplied
  33. emailAddress = optional
  34. # For the 'anything' policy
  35. # At this point in time, you must list all acceptable 'object'
  36. # types.
  37. [ policy_anything ] #由于定义了policy_match策略生效,所以此策略暂未生效
  38. countryName = optional
  39. stateOrProvinceName = optional
  40. localityName = optional
  41. organizationName = optional
  42. organizationalUnitName = optional
  43. commonName = supplied
  44. emailAddress = optional

3、根证书服务器目录

根CA服务器:因为只有 CA 服务器的角色,所以用到的目录只有/etc/pki/CA

网站服务器:只是证书申请者的角色,所以用到的目录只有/etc/pki/tls

4、创建所需要的文件

  1. [root@https-ca ~]# cd /etc/pki/CA/
  2. [root@https-ca CA]# ls
  3. certs crl newcerts private
  4. [root@https-ca CA]# touch index.txt #创建生成证书索引数据库文件
  5. [root@https-ca CA]# ls
  6. certs crl index.txt newcerts private
  7. [root@https-ca CA]# echo 01 > serial #指定第一个颁发证书的序列号
  8. [root@https-ca CA]# ls
  9. certs crl index.txt newcerts private serial
  10. [root@https-ca CA]#

4、创建密钥

在根CA服务器上创建密钥,密钥的位置必须为/etc/pki/CA/private/cakey.pem,这个是openssl.cnf中中指定的路径,只要与配置文件中指定的匹配即可。

  1. [root@https-ca CA]# (umask 066; openssl genrsa -out private/cakey.pem 2048)
  2. Generating RSA private key, 2048 bit long modulus
  3. ...........+++
  4. ...............+++
  5. e is 65537 (0x10001)

5、生成自签名证书

根CA自签名证书,根CA是最顶级的认证机构,没有人能够认证他,所以只能自己认证自己生成自签名证书。

  1. [root@https-ca CA]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 7300 -out /etc/pki/CA/cacert.pem -days 7300
  2. You are about to be asked to enter information that will be incorporated
  3. into your certificate request.
  4. What you are about to enter is what is called a Distinguished Name or a DN.
  5. There are quite a few fields but you can leave some blank
  6. For some fields there will be a default value,
  7. If you enter '.', the field will be left blank.
  8. -----
  9. Country Name (2 letter code) [XX]:CN
  10. State or Province Name (full name) []:BEIJING
  11. Locality Name (eg, city) [Default City]:BEIJING
  12. Organization Name (eg, company) [Default Company Ltd]:CA
  13. Organizational Unit Name (eg, section) []:OPT
  14. Common Name (eg, your name or your server's hostname) []:ca.qf.com
  15. Email Address []:
  16. [root@https-ca CA]# ls
  17. cacert.pem certs crl index.txt newcerts private serial
  1. -new: 生成新证书签署请求
  2. -x509: 专用于CA生成自签证书
  3. -key: 生成请求时用到的私钥文件
  4. -days : 证书的有效期限
  5. -out /PATH/TO/SOMECERTFILE: 证书的保存路径

6、下载安装证书

/etc/pki/CA/cacert.pem就是生成的自签名证书文件,使用 SZ/xftp工具将他导出到窗口机器中。然后双击安装此证书到受信任的根证书颁发机构

  1. [root@https-ca CA]# yum install -y lrzsz
  2. [root@https-ca CA]# sz cacert.pem

image.png

image.png

image.png

image.png

私有CA和证书 - 图6

私有CA和证书 - 图7

image.png

image.png

7、客户端(web服务器)CA 证书申请及签名

1、检查安装 openssl

  1. [root@nginx-server ~]# rpm -qa openssl

如果未安装,安装 openssl

  1. [root@nginx-server ~]# yum install openssl openssl-devel -y

2、客户端生成私钥文件

  1. [root@nginx-server ~]# (umask 066; openssl genrsa -out /etc/pki/tls/private/www.qf.com.key 2048)
  2. Generating RSA private key, 2048 bit long modulus
  3. ..............................+++
  4. ..........+++
  5. e is 65537 (0x10001)
  6. [root@nginx-server ~]# cd /etc/pki/tls/private/
  7. [root@nginx-server private]# ls
  8. www.qf.com.key
  9. [root@nginx-server private]#

8、客户端用私钥加密生成证书请求

  1. [root@nginx-server private]# ls ../
  2. cert.pem certs misc openssl.cnf private
  3. [root@nginx-server private]# openssl req -new -key /etc/pki/tls/private/www.qf.com.key -days 365 -out /etc/pki/tls/www.qf.com.csr
  4. You are about to be asked to enter information that will be incorporated
  5. into your certificate request.
  6. What you are about to enter is what is called a Distinguished Name or a DN.
  7. There are quite a few fields but you can leave some blank
  8. For some fields there will be a default value,
  9. If you enter '.', the field will be left blank.
  10. -----
  11. Country Name (2 letter code) [XX]:CN
  12. State or Province Name (full name) []:BEIJING
  13. Locality Name (eg, city) [Default City]:BEIJING
  14. Organization Name (eg, company) [Default Company Ltd]:QF
  15. Organizational Unit Name (eg, section) []:OPT
  16. Common Name (eg, your name or your server's hostname) []:www.qf.com
  17. Email Address []:
  18. Please enter the following 'extra' attributes
  19. to be sent with your certificate request
  20. A challenge password []:
  21. An optional company name []:
  22. [root@nginx-server private]# ls ../
  23. cert.pem certs misc openssl.cnf private www.qf.com.csr
  24. [root@nginx-server private]#

CSR(Certificate Signing Request)包含了公钥和名字信息。通常以.csr为后缀,是网站向CA发起认证请求的文件,是中间文件。

在这一命令执行的过程中,系统会要求填写如下信息:

私有CA和证书 - 图10

最后把生成的请求文件(/etc/pki/tls/www.qf.com.csr)传输给CA ,这里我使用scp命令,通过ssh协议,将该文件传输到CA下的/etc/pki/CA/private/目录

  1. [root@nginx-server private]# cd ../
  2. [root@nginx-server tls]# scp www.qf.com.csr 192.168.62.163:/etc/pki/CA/private

4、CA 签署证书(在ca服务器上面操作)

使用/etc/pki/tls/openssl.cnf,修改organizationName=supplied

修改 /etc/pki/tls/openssl.cnf

  1. [root@https-ca ~]# vim /etc/pki/tls/openssl.cnf
  2. policy = policy_match
  3. 82
  4. 83 # For the CA policy
  5. 84 [ policy_match ]
  6. 85 countryName = match
  7. 86 stateOrProvinceName = match
  8. 87 organizationName = supplied
  9. 88 organizationalUnitName = optional
  10. 89 commonName = supplied
  11. 90 emailAddress = optional

CA 签署证书

  1. [root@https-ca ~]# openssl ca -in /etc/pki/CA/private/www.qf.com.csr -out /etc/pki/CA/certs/www.qf.com.crt -days 365
  2. Using configuration from /etc/pki/tls/openssl.cnf
  3. Check that the request matches the signature
  4. Signature ok
  5. Certificate Details:
  6. Serial Number: 1 (0x1)
  7. Validity
  8. Not Before: Jul 3 10:12:23 2019 GMT
  9. Not After : Jul 2 10:12:23 2020 GMT
  10. Subject:
  11. countryName = CN
  12. stateOrProvinceName = BEIJING
  13. organizationName = QF
  14. organizationalUnitName = OPT
  15. commonName = www.qf.com
  16. X509v3 extensions:
  17. X509v3 Basic Constraints:
  18. CA:FALSE
  19. Netscape Comment:
  20. OpenSSL Generated Certificate
  21. X509v3 Subject Key Identifier:
  22. E3:AC:1A:55:2B:28:B9:80:DC:9C:C2:13:70:53:27:AD:3D:44:8F:D3
  23. X509v3 Authority Key Identifier:
  24. keyid:5D:2A:81:B2:E7:8D:D8:88:E5:7B:94:CA:75:65:9C:82:2B:A9:B2:3C
  25. Certificate is to be certified until Jul 2 10:12:23 2020 GMT (365 days)
  26. Sign the certificate? [y/n]:y
  27. 1 out of 1 certificate requests certified, commit? [y/n]y
  28. Write out database with 1 new entries
  29. Data Base Updated

证书通常以.crt为后缀,表示证书文件

2、查看生成的证书的信息

  1. [root@https-ca ~]# openssl x509 -in /etc/pki/CA/certs/www.qf.com.crt -noout -subject
  2. subject= /C=CN/ST=BEIJING/O=QF/OU=OPT/CN=www.qf.com

3、将生成的证书发放给请求客户端(web服务端)

  1. [root@https-ca ~]# cd /etc/pki/CA/certs/
  2. [root@https-ca certs]# scp www.qf.com.ctr 192.168.62.162:/etc/pki/CA/certs/

测试:

  1. nginx-server(充当服务端):
  2. [root@nginx-server ~]# cd /etc/pki/CA/certs/
  3. [root@nginx-server certs]# ls
  4. www.qf.com.crt
  5. [root@nginx-server certs]# find / -name *.key
  6. /etc/pki/tls/private/www.qf.com.key
  7. /usr/share/doc/openssh-7.4p1/PROTOCOL.key
  8. 还是在这台机器安装nginx并且配置证书:
  9. root@nginx-server conf.d]# pwd
  10. /etc/nginx/conf.d
  11. [root@nginx-server conf.d]# vim nginx.conf
  12. server {
  13. listen 443 ssl;
  14. server_name www.qf.com;
  15. ssl_certificate /etc/pki/CA/certs/www.qf.com.crt; #指定证书路径
  16. ssl_certificate_key /etc/pki/tls/private/www.qf.com.key; #指定私钥路径
  17. ssl_session_timeout 5m; #配置用于SSL会话的缓存
  18. ssl_protocols SSLv2 SSLv3 TLSv1; #指定使用的协议
  19. ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; # //密码指定为OpenSSL支持的格式
  20. ssl_prefer_server_ciphers on; #设置协商加密算法时,优先使用服务端的加密,而不是客户端浏览器的。
  21. location / {
  22. root /usr/share/nginx/html;
  23. index index.html index.htm;
  24. }
  25. }
  26. 保存重启
  27. [root@nginx-server conf.d]# nginx -t
  28. [root@nginx-server conf.d]# nginx -s reload

windows首先配置hosts文件解析

浏览器测试访问:

私有CA和证书 - 图11