在 CentOS 7 中配置证书颁发机构 (CA)

在本文中,我们将学习如何在 CentOS 7 服务器中安装和配置证书颁发机构 (CA)。

https://onitroad.com 更多教程

在 CentOS 7 中配置证书颁发机构 (CA):

登录到 CA服务器。

Openssl 包提供了创建 SSL 证书和密钥所需的命令。

Openssl 软件包默认安装在最低限度安装的 CentOS 7 上。

但是,如果我们没有找到它,我们可以使用 yum 命令安装它。

[root@ca-01 ~]# yum install -y openssl

Openssl 已安装在此服务器上。

为了实现公共加密,首先我们需要一个私钥,稍后用于生成 CA 证书。

[root@ca-01 ~]# cd /etc/pki/CA/private/
[root@ca-01 private]# openssl genrsa -aes128 -out ourCA.key 2048
Generating RSA private key, 2048 bit long modulus
.................................................................+++
..............+++
e is 65537 (0x10001)
Enter pass phrase for ourCA.key:
Verifying - Enter pass phrase for ourCA.key:

在这里,我们使用 RSA 算法生成了一个私钥,密钥大小相对较大,为 2048 位,以提高安全性。

虽然,我们可以生成没有密码短语的私钥。
但是,强烈建议我们在生成私钥时设置密码短语。
它增加了私钥传输或者备份过程中的安全性。

现在使用 ourCA.key 创建证书颁发机构 (CA) 证书。

[root@ca-01 private]# openssl req -new -x509 -days 1825 \
> -key /etc/pki/CA/private/ourCA.key \
> -out /etc/pki/CA/certs/ourCA.crt
Enter pass phrase for /etc/pki/CA/private/ourCA.key:
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) []:ZheJ
Locality Name (eg, city) [Default City]:Hangzh
Organization Name (eg, company) [Default Company Ltd]:JackLi system tech ltd
Organizational Unit Name (eg, section) []:systech
Common Name (eg, your name or your server's hostname) []:ca-01.onitroad.com
Email Address []:jackli@onitroad.com

我们的证书颁发机构 (CA) 服务器现已准备就绪。

什么是证书颁发机构 (CA - Certificate Authority)

证书颁发机构,简称CA,是颁发数字证书的实体。
数字证书通过证书的 CN(通用名称)来证明公钥的所有权。
第三方信任由可信 CA 数字签名的网站 SSL 证书。

在 CentOS 7 中配置证书颁发机构 (CA) 服务器是一个简单而直接的操作。
但是,在本文中,我们不仅要配置 CA,而且还要配置 Apache 网站以使用 SSL 证书,然后将根 CA 证书添加到客户端的受信任 CA 存储中。

配置环境

CA服务器

主机名:ca-01.onitroad.com
IP: 192.168.1.54 /24
操作系统:CentOS 7.6

web服务器

主机名:web-01.onitroad.com
IP:192.168.1.51 /24
操作系统:CentOS 7.6

配置 Apache HTTP Server 以使用 SSL 证书

登录到apache服务器, web-01.onitroad.com。

我们已经在 web-01.onitroad.com 上配置了 Apache HTTP Server。

但它在默认端口 80 上使用 HTTP 协议运行。

我们的目标是将此网站从 HTTP 转换为 HTTPS。

为此,我们需要 Apache 的 mod_ssl 模块。
因此,我们使用 yum 命令安装它。

[root@web-01 ~]# yum install -y mod_ssl

在安装过程中 mod_ssl 在 /etc/httpd/conf.d 目录下创建了一个默认的配置文件 ssl.conf。

我们可以修改 ssl.conf 来添加网站的 SSL 证书。
但首先我们必须为我们的网站获取 SSL 证书。

确保在此服务器上安装了 openssl,因为我们需要它生成私钥和 CSR(证书签名请求)。

为服务器 web-01.onitroad.com 生成私钥。

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

我们使用 RSA 密钥算法生成了一个私钥,密钥大小为 1024 。

这次我们没有用密码保护我们的私钥,因为如果我们设置了密码,那么无论何时我们启动 httpd.service 都会要求这个密码。

现在,为我们的网站生成一个 CSR(证书签名请求)。

[root@web-01 ~]# openssl req -new -key /etc/pki/tls/private/web-01.key \
> -out /etc/pki/tls/web-01.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) []:Zhej
Locality Name (eg, city) [Default City]:Hangzh
Organization Name (eg, company) [Default Company Ltd]:JackLi's system tech Ltd
Organizational Unit Name (eg, section) []: systech
Common Name (eg, your name or your server's hostname) []:web-01.onitroad.com
Email Address []:jackli@onitroad.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

我们已经生成了一个 CSR 。
现在我们将其发送给 CA 进行数字签名。

[root@web-01 ~]# scp /etc/pki/tls/web-01.csr root@ca-01:~/web-01.csr
root@ca-01's password:
web-01.csr                                    100%  729   353.1KB/s   00:00

连接到 ca-01.onitroad.com 并对该 CSR 进行数字签名。

[root@ca-01 ~]# openssl x509 -req -in web-01.csr \
> -CA /etc/pki/CA/certs/ourCA.crt \
> -CAkey /etc/pki/CA/private/ourCA.key \
> -CAcreateserial \
> -out web-01.crt \
> -days 365
Signature ok
subject=/C=PK/ST=Sindh/L=Karachi/O=JackLi's SysAdmin Recipes/OU=ITLAB/CN=web-01.onitroad.com/emailAddress=jackli@onitroad.com
Getting CA Private Key
Enter pass phrase for /etc/pki/CA/private/ourCA.key:

我们的 CSR 已由我们的证书颁发机构 (CA) 进行数字签名。

将 web-01.crt 转移到 web-01.onitroad.com 。

[root@ca-01 ~]# scp web-01.crt root@web-01:/etc/pki/tls/certs/web-01.crt
root@web-01's password:
web-01.crt                                    100% 1180     1.0MB/s   00:00

以 root 用户身份使用 ssh 连接到 web-01.onitroad.com。

现在,我们有了一个数字签名的 SSL 证书。
在 ssl.conf 文件中添加此证书和私钥。

[root@web-01 ~]# vi /etc/httpd/conf.d/ssl.conf

我们只需要更新其中的两个指令。

SSLCertificateFile /etc/pki/tls/certs/web-01.crt
SSLCertificateKeyFile /etc/pki/tls/private/web-01.key

重启 Apache 服务。

[root@web-01 ~]# systemctl restart httpd.service

在 Linux 防火墙中允许 https 服务。

[root@web-01 ~]# firewall-cmd --permanent --add-service=https
success
[root@web-01 ~]# firewall-cmd --reload
success

我们已经成功地将我们的 Apache 服务器配置为使用 HTTPS 协议。

日期:2020-09-17 00:12:27 来源:oir作者:oir