如何创建私有CA以及如何进行证书管理

概念

CA(Certificate Authority)是数字证书认证中心的简称,是指发放、管理、废除数字证书的机构。

CA的作用是检查证书持有者身份的合法性,并签发证书,以防证书被伪造或篡改,以及对证书和密钥进行管理。

CA的概念性东西是很多的,这篇博客我就简单阐述以上两条关于CA是什么和CA的作用的概念,接下来我就直接进行操作演示。


创建私有CA

注意:这里我用了两台CentOS7.3的虚拟机,在下文中统一称CA为A机器,需要签名证书的机器称为B机器。

第一步,创建所需要的文件

当我们在系统中装好openssl之后/etc/pki/CA这个目录就自动生成,关于CA的所有数据都存放在这个文件夹里,但是配置文件是/etc/pki/tls/openssl.cnf。

那么第一步我们需要创建一个证书索引数据库,这个数据库的作用是存放证书的序号和被颁发者的域名等信息。

[root@mini7 CA]# touch /etc/pki/CA/index.txt
[root@mini7 CA]# tree
.
├── certs
├── crl
├── index.txt
├── newcerts
└── private

4 directories, 1 file
[root@mini7 CA]# 

证书索引数据库已经创建好了,接下来还需要一个指定颁发证书的序列号的文件,同样是放在CA目录下。

[root@mini7 CA]# echo 01 > /etc/pki/CA/serial
[root@mini7 CA]# tree
.
├── certs
├── crl
├── index.txt
├── newcerts
├── private
└── serial

4 directories, 2 files
[root@mini7 CA]# 

我在上面用echo 01重定向的意义在于给下一个颁发的证书指定序列号01,要注意的是这里是十六进制数只输单个数字是错误的。


第二步,CA自身给自身签名

A机器作为CA的话就没有比A机器更上级的CA来给A机器签名,那么A机器就需要自己给自己签名,自身证明自身的真实性。

在生成自签名证书前A机器首先得有一个私钥。

[root@mini7 CA]# (umask 066;openssl genrsa -out /etc/pki/CA/private/cakey.pem 1024)
Generating RSA private key, 1024 bit long modulus
....++++++
.........................................................++++++
e is 65537 (0x10001)
[root@mini7 CA]# 

执行上面的操作便在指定目录下生成了名为ca.pem的私钥,这里要注意的是生成的私钥默认权限是644,为了安全我们并不希望除了自己以外任何人能看到私钥的内容,所以直接用umask 066设置默认创建的文件权限,但是直接用umask 066又会有一个问题,那就是我们只是为了创建一个私钥文件把整个系统的默认创建文件权限都改了,而且之后改得去改回来,如果进行后面的操作的时候忘了将umask改回来,那么有可能造成系统中多处出现文件权限混乱问题,所以加上小括号将两行命令放在一起执行这样就不影响括号外的系统环境了。

现在我们已经有了私钥,可以生成自签名证书了。

[root@mini7 CA]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 3650 -out /etc/pki/CA/cacert.pem
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) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:driver_c
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:driver_c.com
[root@mini7 CA]# 

上面的命令虽然比较长,但是都是按格式来的,所以理解记忆还是能做到的。其中-x509表示专用于CA生成自签名证书,days后面跟的数字表示的是证书的有效期是多少天。

这条命令执行后会出现一个交互式的界面,如上面几根短横线下开始的地方,这里会按要求输入一些信息,这些信息是不能胡乱输入的,部分信息是要和申请证书的主机发来的申请信息一致的,那么到底哪些信息需要一致呢?这就要查询刚才提到过的配置文件/etc/pki/tls/openssl.cnf了。

[ policy_match ]
countryName             = match
stateOrProvinceName     = match
organizationName        = match
organizationalUnitName  = optional
commonName              = supplied
emailAddress            = optional

上面就是从配置文件中截取出来的一段设置,位置大概在84行左右开始。后面是match的项就是需要匹配的项,这些是不能乱填的。


第三步,给其他主机颁发证书

到目前为止CA就算是创建完成,现在B机器需要A机器给它发证书,那么我们现在需要在B机器上给A机器发送证书请求。

首先也是生成私钥

[root@centos7 app]# (umask 066;openssl genrsa -out /etc/pki/tls/private/web.key 1024)
Generating RSA private key, 1024 bit long modulus
..........................................................++++++
..........++++++
e is 65537 (0x10001)
[root@centos7 app]# 

然后是生成证书申请文件

[root@centos7 app]# openssl req -new -key /etc/pki/tls/private/web.key -days 365 -out /etc/pki/tls/web.csr
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) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:driver_c
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:www.test.com
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@centos7 app]# 

生成证书申请文件的命令和CA自签名生成的命令基本一样,只是少了x509,并且目录不一样了。要注意的是那几项需要匹配的是不能输错的。

证书申请文件已经生成了,下一步就是把证书申请文件发给A机器签署,传输文件方法很多过程就不演示了。

[root@mini7 CA]# openssl ca -in /app/web.csr -out /etc/pki/CA/certs/web.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Jul 16 09:49:46 2017 GMT
            Not After : Jul 16 09:49:46 2018 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = beijing
            organizationName          = driver_c
            commonName                = www.test.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                3A:18:24:88:E5:4D:78:5D:49:BB:7B:B3:D1:71:22:43:6A:D8:29:DB
            X509v3 Authority Key Identifier: 
                keyid:D3:72:16:28:C7:96:01:73:08:16:43:27:36:1D:16:70:ED:E8:F7:F7

Certificate is to be certified until Jul 16 09:49:46 2018 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
[root@mini7 CA]# 

当我们把证书申请文件发到A机器之后,在A机器(CA)上执行以上文件进行签署生成证书。这个时候会显示出申请者在申请时输入的各项信息,这里如果默认的三个选项与CA不一致的话就会报错。

到这里证书签署部分就基本完成了。


吊销证书

颁发证书我已经讲了,那么怎么将已经颁发了的证书删掉呢?

和创建一样,首先我们得创建一个被吊销的证书的编号文件,放在指定的地方。

[root@mini7 CA]# echo 01 > /etc/pki/CA/crlnumber
[root@mini7 CA]# 

现在便可以执行吊销命令,不过在吊销前我们先看看证书的状态

[root@mini7 CA]# cat index.txt
V   180716094946Z       01  unknown /C=CN/ST=beijing/O=driver_c/CN=www.test.com
[root@mini7 CA]# 

然后执行吊销命令

[root@mini7 CA]# openssl ca -revoke /etc/pki/CA/newcerts/01.pem 
Using configuration from /etc/pki/tls/openssl.cnf
Revoking Certificate 01.
Data Base Updated
[root@mini7 CA]# cat index.txt
R   180716094946Z   170716113724Z   01  unknown /C=CN/ST=beijing/O=driver_c/CN=www.test.com
[root@mini7 CA]# 

在执行了吊销命令后再次查看证书状态可以发现证书状态已经不一样了。执行到这一步还不算完,虽然已经吊销了证书,但是没有更新到吊销证书列表,那么下一步就是更新被吊销的证书到吊销证书列表。

[root@mini7 CA]# openssl ca -gencrl -out /etc/pki/CA/crl/crl.pem
Using configuration from /etc/pki/tls/openssl.cnf
[root@mini7 CA]# 

到目前位置证书吊销就已经完成了。如果想查询被吊销的证书信息的话可以用以下命令查看。

[root@mini7 CA]# openssl crl -in crl/crl.pem -noout -text
Certificate Revocation List (CRL):
        Version 2 (0x1)
    Signature Algorithm: sha256WithRSAEncryption
        Issuer: /C=CN/ST=beijing/L=beijing/O=driver_c/CN=driver_c.com
        Last Update: Jul 16 11:40:44 2017 GMT
        Next Update: Aug 15 11:40:44 2017 GMT
        CRL extensions:
            X509v3 CRL Number: 
                1
Revoked Certificates:
    Serial Number: 01
        Revocation Date: Jul 16 11:37:24 2017 GMT
    Signature Algorithm: sha256WithRSAEncryption
         4d:12:d0:ee:99:bf:93:f1:0b:c9:15:fc:87:20:47:37:5e:56:
         ...
         8e:74:fc:34:70:59:cf:23:db:71:58:46:6f:82:8b:f1:5f:4e:
         ed:ef
[root@mini7 CA]# 

结语

在理解CA的概念上其实没有遇到太大的苦难,但是做这个实验的时候记住命令还真是有些头疼,所以在学习这一部分以及做这个实验的时候记忆命令也是一个需要多多注意的问题。