构建私有的CA证书

1、CA 介绍

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

2、构建私有 CA

2.1 安装 openssl

[root@nginx1 ~]# yum install openssl openssl-devel -y

2.2 配置文件详解

####################################################################
[ ca ]
default_ca      = CA_default            # 默认的CA配置;CA_default指向下面配置块

####################################################################
[ CA_default ]

dir             = /etc/pki/CA           # CA的默认工作目录
certs           = $dir/certs            # 认证证书的目录
crl_dir         = $dir/crl              # 证书吊销列表的路径
database        = $dir/index.txt        # 数据库的索引文件

new_certs_dir   = $dir/newcerts         # 新颁发证书的默认路径

certificate     = $dir/cacert.pem       # 此服务认证证书,如果此服务器为根CA那么这里为自颁发证书
serial          = $dir/serial           # 下一个证书的证书编号
crlnumber       = $dir/crlnumber        # 下一个吊销的证书编号
                                        
crl             = $dir/crl.pem          # The current CRL
private_key     = $dir/private/cakey.pem# CA的私钥
RANDFILE        = $dir/private/.rand    # 随机数文件

x509_extensions = usr_cert              # The extentions to add to the cert

name_opt        = ca_default            # 命名方式,以ca_default定义为准
cert_opt        = ca_default            # 证书参数,以ca_default定义为准

default_days    = 365                   # 证书默认有效期
default_crl_days= 30                    # CRl的有效期
default_md      = sha256                # 加密算法
preserve        = no                    # keep passed DN ordering

policy          = policy_match          #policy_match策略生效

# For the CA policy
[ policy_match ]
countryName             = match         #国家;match表示申请者的申请信息必须与此一致
stateOrProvinceName     = match         #州、省
organizationName        = match         #组织名、公司名
organizationalUnitName  = optional      #部门名称;optional表示申请者可以的信息与此可以不一致
commonName              = supplied
emailAddress            = optional

# For the 'anything' policy
# At this point in time, you must list all acceptable 'object'
# types.
[ policy_anything ]                     #由于定义了policy_match策略生效,所以此策略暂未生效
countryName             = optional
stateOrProvinceName     = optional
localityName            = optional
organizationName        = optional
organizationalUnitName  = optional
commonName              = supplied
emailAddress            = optional

2.3 根证书服务器目录

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

2.4 创建所需要的文件

[root@nginx1 ~]# cd /etc/pki/CA/
[root@nginx1 CA]# touch index.txt       #创建生成证书索引数据库文件
[root@nginx1 CA]# echo 01 > serial      #指定第一个颁发证书的序列号
[root@nginx1 CA]# ls
certs  crl  index.txt  newcerts  private  serial

2.5 创建密钥

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

[root@nginx1 CA]# (umask 066; openssl genrsa -out private/cakey.pem 2048)
[root@nginx1 CA]# ls private/
cakey.pem

2.6 生成自签名证书

根CA自签名证书,根CA是最顶级的认证机构,没有人能够认证他,所以只能自己认证自己生成自签名证书。
[root@nginx1 CA]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 7300 -out /etc/pki/CA/cacert.pem -days 7300

image.png

-new:   生成新证书签署请求
-x509:  专用于CA生成自签证书
-key:   生成请求时用到的私钥文件
-days n:    证书的有效期限
-out /PATH/TO/SOMECERTFILE:     证书的保存路径

2.7 下载安装证书

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

[root@nginx1 CA]# yum install -y lrzsz
[root@nginx1 CA]# sz cacert.pem 

谷歌浏览器安装证书:


image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

3、客户端CA 证书申请及签名

3.1 安装openssl

[root@nginx2 ~]# yum install openssl openssl-devel -y

3.2 客户端生成私钥文件

[root@nginx2 ~]#  (umask 066; openssl genrsa -out /etc/pki/tls/private/www.ice.com.key 2048)
[root@nginx2 ~]# cd /etc/pki/tls/private/
[root@nginx2 private]# ls
www.ice.com.key

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

[root@nginx2 private]# openssl req -new -key /etc/pki/tls/private/www.ice.com.key -days 365 -out /etc/pki/tls/www.ice.com.csr
CSR(Certificate Signing Request)包含了公钥和名字信息。通常以.csr为后缀,是网站向CA发起认证请求的文件,是中间文件。
最后把生成的请求文件(/etc/pki/tls/www.qf.com.csr)传输给CA ,这里我使用scp命令,通过ssh协议,将该文件传输到CA下的/etc/pki/CA/private/目录

[root@nginx2 private]# cd ..
[root@nginx2 tls]# scp www.ice.com.csr 10.8.156.11:/etc/pki/CA/private

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

使用/etc/pki/tls/openssl.cnf,修改organizationName=supplied
[root@nginx1 ~]# vim /etc/pki/tls/openssl.cnf

image.png

4.1 CA签署证书

[root@nginx1 ~]# openssl ca -in /etc/pki/CA/private/www.ice.com.csr -out /etc/pki/CA/certs/www.ice.com.crt

image.png

4.2 查看生成的证书信息

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

[root@nginx1 CA]# openssl x509 -in /etc/pki/CA/certs/www.ice.com.crt -noout -subject
subject= /C=CN/ST=HN/O=ali/OU=yunwei/CN=yunjisuan

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

[root@nginx1 CA]# scp /etc/pki/CA/certs/www.ice.com.crt 10.8.156.23:/etc/pki/CA/certs/

4.4 测试

nginx2充当服务端

[root@nginx2 tls]# cd /etc/pki/CA/certs/
[root@nginx2 certs]# ls
www.ice.com.crt
[root@nginx2 certs]# find / -name *.key
/etc/pki/tls/private/www.ice.com.key
/usr/share/doc/openssh-7.4p1/PROTOCOL.key
#安装nginx(略)并且配置证书
[root@nginx2 conf.d]# vim default.conf
server {
        listen 443 ssl;
        server_name localhost;
        ssl_certificate   /etc/pki/CA/certs/www.ice.com.crt;
        ssl_certificate_key  /etc/pki/tls/private/www.ice.com.key;
        ssl_session_timeout 5m;
        ssl_protocols  SSLv2 SSLv3  TLSv1;
        ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers   on;
        location / {
                root    /usr/share/nginx/html;
                index   index.html index.html;
                }
}
[root@nginx2 conf.d]# nginx -t
[root@nginx2 conf.d]# nginx -s reload

浏览器访问测试 https://10.8.156.23/


image.png

由于是私有的CA中心,所以会显示不安全,但是能通过HTTPS方式访问就可以了

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容