侧边栏壁纸
博主头像
张种恩的技术小栈博主等级

行动起来,活在当下

  • 累计撰写 748 篇文章
  • 累计创建 65 个标签
  • 累计收到 39 条评论

目 录CONTENT

文章目录

OpenSSL(2)之PKI简述及建立私有CA并签发证书

zze
zze
2020-03-09 / 0 评论 / 0 点赞 / 767 阅读 / 8434 字

不定期更新相关视频,抖音点击左上角加号后扫一扫右方侧边栏二维码关注我~正在更新《Shell其实很简单》系列

在开始建立 CA 之前我们需要了解一下 PKI。

公钥基础设施 PKI

PKI,全称“Public Key Infrastructure”,译为公钥基础设施,是一个包括硬件、软件、人员、策略和规程的集合,用来实现基于公钥密码体制的密钥和证书的产生、管理、存储、分发和撤销等功能。

PKI 体系是计算机软硬件、权威机构及应用系统的结合。它为实施电子商务、电子政务、办公自动化等提供了基本的安全服务,从而使那些彼此不认识或距离很远的用户能通过信任链安全地交流。

PKI 的组成

一个典型的 PKI 系统包括 PKI 策略、软硬件系统、证书机构 CA、注册机构 RA、证书吊销列表 CRL、证书发布系统和 PKI 应用等。

PKI 安全策略

建立和定义了一个组织信息安全方面的指导方针,同时也定义了密码系统使用的处理方法和原则。它包括一个组织怎样处理密钥和有价值的信息,根据风险的级别定义安全控制的级别。

证书机构 CA

证书机构 CA 是 PKI 的信任基础,它管理公钥的整个生命周期,其作用包括:发放证书、规定证书的有效期和通过发布证书废除列表(CRL)确保必要时可以废除证书。

注册机构 RA

注册机构 RA 提供用户和 CA 之间的一个接口,它获取并认证用户的身份,向 CA 提出证书请求。它主要完成收集用户信息和确认用户身份的功能。这里指的用户,是指将要向认证中心(即 CA)申请数字证书的客户,可以是个人,也可以是集团或团体、某政府机构等。注册管理一般由一个独立的注册机构(即 RA)来承担。它接受用户的注册申请,审查用户的申请资格,并决定是否同意 CA 给其签发数字证书。注册机构并不给用户签发证书,而只是对用户进行资格审查。因此,RA 可以设置在直接面对客户的业务部门,如银行的营业部、机构认识部门等。当然,对于一个规模较小的 PKI 应用系统来说,可把注册管理的职能由认证中心 CA 来完成,而不设立独立运行的 RA。但这并不是取消了 PKI 的注册功能,而只是将其作为 CA 的一项功能而已。PKI 国际标准推荐由一个独立的 RA 来完成注册管理的任务,可以增强应用系统的安全。

证书吊销列表 CRL

证书具有一个指定的寿命,但 CA 可通过称为证书吊销的过程来缩短这一寿命。CA 发布一个证书吊销列表 (CRL),列出被认为不能再使用的证书的序列号。

证书发布系统(证书存取库)

证书发布系统负责证书的发放,如可以通过用户自己,或是通过目录服务器发放。目录服务器可以是一个组织中现存的,也可以是 PKI 方案中提供的。

使用 OpenSSL 建立私有CA

先要了解一下证书申请及签署的步骤:

  1. 生成申请请求;
  2. RA 核验;
  3. CA 签署;
  4. 从证书存取库获取证书;

这里我们直接使用 OpenSSL 建立私有 CA,它同时具有 CA 和 RA 功能。

配置文件

OpenSSL 对 CA 功能的主配置文件为 /etc/pki/tls/openssl.cnf,我们无需修改它,下面仅列出其部分配置,了解一下其各文件的存储结构。

[ ca ]
default_ca      = CA_default            # The default ca section

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

dir             = /etc/pki/CA           # CA 的工作目录
certs           = $dir/certs            # 保留颁发的证书的位置
crl_dir         = $dir/crl              # 证书吊销列表
database        = $dir/index.txt        # 数据库索引文件,每添加一个证书这个文件会自动添加一行数据
#unique_subject = no                    # 证书主体信息是否必须唯一,哪个国家、省份、公司名等等信息
new_certs_dir   = $dir/newcerts         # 新证书的保存的默认位置

certificate     = $dir/cacert.pem       # CA 证书
serial          = $dir/serial           # 证书序列号
crlnumber       = $dir/crlnumber        # 吊销证书序列号
crl             = $dir/crl.pem          # 当前的 CRL
private_key     = $dir/private/cakey.pem# CA 的私钥
RANDFILE        = $dir/private/.rand    # 私有随机数文件

x509_extensions = usr_cert              # 扩展

default_days    = 365                   # 证书的有效期
default_crl_days= 30                    # 吊销列表的有效期
default_md      = default               # use public key default MD
preserve        = no                    # keep passed DN ordering

开始

下面我使用 10.0.1.201 作为 CA 主机,10.0.1.200 作为申请证书的主机。

初始化文件

由于是第一次使用 CA,所以我们需要手动初始化一下初始的文件,如证书索引文件(index.txt)、证书序列号文件 serial

$ cd /etc/pki/CA
$ touch index.txt
$ echo 01 > serial

CA 自签证书

1、生成私钥:

$ (umask 077; openssl genrsa -out private/cakey.pem 2048)

2、生成证书请求:

# 生成证书请求
#	-new:生成新证书签署请求
#	-x509:专用于 CA 生成自签证书
#	-key:生成请求时用到的私钥文件;
#	-days n:证书的有效期限;
#	-out /PATH/TO/SOMEFILE:证书的保存路径
$ openssl req -new -x509 -key private/cakey.pem -days 7300 -out 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) []:Guangdong
# 城市
Locality Name (eg, city) [Default City]:Shenzhen  
# 组织/公司名
Organization Name (eg, company) [Default Company Ltd]:zze
# 部门
Organizational Unit Name (eg, section) []:Ops
# 要使用 https 的主机名,必须一致
Common Name (eg, your name or your server's hostname) []:ca.zze.xyz
# 邮箱
Email Address []:zhangzhongen326@gmail.com

用到证书的主机生成证书请求

1、这里我申请一个证书用于让 httpd 使用 https,所以我在 httpd 配置目录下创建一个目录:

$ cd /etc/httpd && mkdir ssl && cd ssl

2、生成私钥。

$ (umask 077; openssl genrsa -out httpd.key 2048)

3、生成证书请求,由于这里是私有 CA,所以要保证组织部门信息和 CA 自签时相同(毕竟你也不可能用私有 CA 给别的组织发证)。

$ openssl req -new -key httpd.key -days 365 -out httpd.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) []:Guangdong 
Locality Name (eg, city) [Default City]:Shenzhen
Organization Name (eg, company) [Default Company Ltd]:zze
Organizational Unit Name (eg, section) []:Ops
Common Name (eg, your name or your server's hostname) []:www.zze.com  
Email Address []:zhangzhongen326@gmail.com

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

4、把请求文件传输给 CA,这里我就直接 scp 拷贝到 CA 主机了,实际公网环境中可能是使用诸如 ftp 的方式提交给 CA 主机。

$ scp httpd.csr root@10.0.1.201:/tmp

发证

1、CA 检查证书申请,如若无误则签署证书。

$ openssl ca -in /tmp/httpd.csr -out /etc/pki/CA/certs/httpd.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: Mar 10 07:53:19 2020 GMT
            Not After : Mar 10 07:53:19 2021 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = Guangdong
            organizationName          = zze
            organizationalUnitName    = Ops
            commonName                = www.zze.com
            emailAddress              = zhangzhongen326@gmail.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                91:E8:F1:05:60:49:78:12:E8:1C:3B:A6:3C:7F:64:5B:2B:8A:89:A5
            X509v3 Authority Key Identifier: 
                keyid:D2:7F:DB:09:C0:A5:FB:87:88:7C:4B:D8:3A:AC:ED:DA:6F:6A:A1:F6

Certificate is to be certified until Mar 10 07:53:19 2021 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

2、将签署后的证书发放给申请证书的主机,还是由于这里使用的是私有 CA,只是模拟 CA 环境,所以直接远程拷贝到申请主机,实际环境中肯定是以其它途径发放证书。

$ scp /etc/pki/CA/certs/httpd.crt root@10.0.1.200:/etc/httpd/ssl

至此,证书已申请完毕。

补充

查看证书信息

如果要查看证书中的信息,可使用如下命令:

$ openssl x509 -in <cert_file> -noout <-text|-subject|-serial>
	cert_file:证书文件;
	-text:以文本格式输出证书全部内容;
	-subject:仅输出证书主体内容;
	-serial:输出证书的序列号;

吊销证书

如果客户端要申请吊销某证书,可通过如下步骤实现。

1、客户端获取要吊销证书的 serial:

$ openssl x509 -in <cert_file> -noout -serial -subject
	cert_file:证书文件;

2、CA 端先根据客户端提交的 serial 与 subject 信息,对比检验是否与 index.txt 文件中的信息一致,如果无误则吊销证书:

# 吊销证书
$ openssl ca -revoke /etc/pki/CA/newcerts/<serial>.pem
	serial:要吊销的证书编号;
# 初始化吊销证书编号文件(第一次吊销证书才需操作)
$ echo 01 > /etc/pki/CA/crlnumber
# 更新证书吊销列表,thisca.crl 保存吊销的证书信息
$ openssl ca -gencrl -out thisca.crl
# 如果要查看吊销证书的信息,可使用如下命令:
$ openssl -crl -in thisca.crl -noout -text

现在证书有了,那么该怎么使用呢???可参考【给 httpd 服务整上 https】与【Nginx(13)之 https 支持】。

0

评论区