你好,游客 登录 注册 搜索
背景:
阅读新闻

Apache Httpd 2.2 配置CA证书实现Https加密通讯

[日期:2017-10-08] 来源:Linux社区  作者:xiaoshuaigege [字体: ]

什么是CA证书

关于什么是CA证书,以及如何使用Open-SSL申请和搭建CA证书,我们在之前的文章中已经有过介绍,这里不再赘述。若有疑问,可参考之前的文章。http://www.linuxidc.com/Linux/2017-10/147349.htm

Apache Httpd 2.2 实现https加密通讯

实际生产中CA证书一般是向一些专业认证的国际机构来进行申请的。我们会模拟使用OpenSSL生成的证书,来实现Apache的安全加密通讯,这与实际生产中是类似的。

实验环境准备

准备两台虚拟主机。他们的作用和角色分别如下图所示。
实验环境准备

接下来,我们分两个部分进行,在主机B上搭建CA证书环境,然后在主机A上配置证书环境。

主机B上搭建CA证书环境

CA证书环境中,私钥的名称以及存放路径,还有证书的名称和存放路径都是有一定规则的,所以,如果不明白的话,可以查看/etc/pki/tls/openssl.cnf 文件。

1、构建私钥文件

这里我们没有采用加密的方式。

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

2、根据私钥文件,创建自签名的根CA证书

证书的名字必须是cacert.pem ,而且存放路径必须是/etc/pki/CA/cacert.pem

[root@localhost ~]#openssl req -new -x509 -key  /etc/pki/CA/private/cakey.pem -days 7300 -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) []:shandong
Locality Name (eg, city) [Default City]:qingdao
Organization Name (eg, company) [Default Company Ltd]:pojun.tech
Organizational Unit Name (eg, section) []:Opt
Common Name (eg, your name or your server's hostname) []:ca.pojun.tech
Email Address []:

此时如果我们去查看证书的内容话,是可以查看我们刚刚指定的这些信息的。

[root@localhost ~]#openssl x509 -in /etc/pki/CA/cacert.pem -noout -text
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number: 17076170100312404196 (0xecfabe3b994470e4)
    Signature Algorithm: sha256WithRSAEncryption
        Issuer: C=CN, ST=shandong, L=qingdao, O=pojun.tech, OU=Opt, CN=ca.pojun.tech
        Validity
            Not Before: Sep 29 03:40:10 2017 GMT
            Not After : Sep 24 03:40:10 2037 GMT
        Subject: C=CN, ST=shandong, L=qingdao, O=pojun.tech, OU=Opt, CN=ca.pojun.tech	
	*******************省略了公钥和签名信息*************************

3、创建颁发证书必须的两个文件

如果不提前创建这两个文件,那么在生成证书的过程中会出现错误。
我们将文件创建在配置文件中指定的路径下面。

  • 生成证书索引数据库文件 touch /etc/pki/CA/index.txt

  • 指定第一个颁发证书的序列号 echo 01 > /etc/pki/CA/serial

在主机A上申请证书

1、首先安装mod_ssl动态模块

首先在主机A上安装mod_ssl 模块 ,然后我们来查看一下这个模块里面都包含哪些内容

[root@CentOS6 ~]$rpm -ql mod_ssl
/etc/httpd/conf.d/ssl.conf  # 配置文件
/usr/lib64/httpd/modules/mod_ssl.so  # Apache 动态模块
/var/cache/mod_ssl
/var/cache/mod_ssl/scache.dir
/var/cache/mod_ssl/scache.pag
/var/cache/mod_ssl/scache.sem

打开 /etc/httpd/conf.d/ssl.conf ,就会发现,mod_ssl已经帮助我们添加了证书文件,只不过这个证书文件没有任何的意义,我们接下来就是要申请证书,并把他们替换掉。

mod_ss

2、生成私钥文件

因为私钥文件是给Web服务器来使用的,所以私钥文件可以存放在web服务器的配置目录下。这样方便管理 。

# 首先创建一个目录用来管理生成的私钥和证书请求文件,可根据自己的实际情况而定
[root@centos6 ~]$ mkdir /etc/httpd/conf.d/ssl

# 生成自己的私钥文件
[root@centos6 ~]$(umask 066; openssl genrsa -out /etc/httpd/conf.d/ssl/httpd.key 1024)
Generating RSA private key, 1024 bit long modulus
.++++++
...++++++
e is 65537 (0x10001)

3、生成证书请求文件

生成自己的证书请求文件,这里的请求文件是要传给主机B(根CA)来申请证书的。其中的域名采用了范域名解析。
也就是说,当我们把所有的环境配置结束之后,就应该使用https://*.a.com的方式来访问网站,这样话,服务器就会自动采用加密的方式来处理我们的请求。

# 生成自己的证书申请文件,以 .csr 结尾的文件。
[root@centos6 ssl]$openssl req -new -key /etc/httpd/conf.d/ssl/httpd.key -out /etc/httpd/conf.d/ssl/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) []:shandong
Locality Name (eg, city) [Default City]:yantai
Organization Name (eg, company) [Default Company Ltd]:pojun.tech
Organizational Unit Name (eg, section) []:opt
Common Name (eg, your name or your server's hostname) []:*.a.com
Email Address []:

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

4、将证书请求文件发送给证书颁发机构(主机B)

我们需要将证书请求文件发送给证书颁发机构。

[root@centos6 ssl]$scp /etc/httpd/conf.d/ssl/httpd.csr  172.18.2.77:/etc/pki/CA/
root@172.18.2.77's password: 
httpd.csr                                        100%  647     0.6KB/s   00:00

5、在根CA(主机B)颁发证书

我们需要将证书请求文件发送给证书颁发机构。

# 根据主机A提交的证书申请内容,生成证书
[root@localhost ~]#openssl ca -in /etc/pki/CA/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 365
*****************中间省略了输出信息***********************

将生成的证书文件颁发给申请者(主机A)

#  前面我们说过,证书文件都存放在/etc/httpd/conf.d/ssl/目录下
[root@localhost ~]#scp  /etc/pki/CA/certs/httpd.crt  172.18.2.66:/etc/httpd/conf.d/ssl/
The authenticity of host '172.18.2.66 (172.18.2.66)' can't be established.
RSA key fingerprint is 00:c0:e5:a6:39:e9:a7:bb:1b:f4:ab:0d:75:9b:38:b0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.18.2.66' (RSA) to the list of known hosts.
root@172.18.2.66's password: 
httpd.crt                                         100% 3714     3.6KB/s   00:00

同时将根CA的证书也发送给Web服务器(主机A)。这一点很重要。

[root@localhost ~]#scp  /etc/pki/CA/cacert.pem   172.18.2.66:/etc/httpd/conf.d/ssl/
root@172.18.2.66's password: 
cacert.pem                                   100% 1334     1.3KB/s   00:00

在主机A上配置Web服务器

首先配置Web服务器的私钥文件和证书文件,编辑”/etc/httpd/conf.d/ssl.conf”

Apache-ssl

然后将根CA的证书路径,配置在配置文件中。

Apache-cacert

配置FQDN

关于在Apache httpd 2.2 上如何配置FQDN,已经在我的另外一片文章中介绍过了,如果有疑问的话,可移步下列站点查看 
http://www.pojun.tech/blog/2017/09/27/linux-middle-command-5
http://xiaoshuaigege.blog.51cto.com/blog/6217242/1969618
http://blog.csdn.net/eumenides_s/article/details/78130561

我们这里直接给出配置的内容,该配置文件位于/etc/httpd/conf.d/ 路径下。

[root@centos6 conf.d]$cat ca.conf 
# 这句话一定要加上NameVirtualHost *:80 

<VirtualHost *:80>
ServerName www.a.com
DocumentRoot "/var/www/html"</VirtualHost>
<VirtualHost *:80>
ServerName mail.a.com
DocumentRoot "/var/www/html"</VirtualHost>

测试实验结果

经过了上面的配置之后,我们已经实现了Apache https 的加密通讯,下面我们来验证一下实验的结果。
我们需要一台浏览器来访问一下 我们在FQDN中配置的网络地址。www.a.coomwww.b.com
注意:这里应该需要配置一下DNS。我们这里偷个懒,因为我使用的windows主机上的浏览器,所以,我们简要的修改下

C:\Windows\System32\drivers\etc\hosts 这个文件,并在文件的末尾添加一行。

# 添加这样一句解析。
172.18.2.66 www.a.com  mail.a.com

添加上面的解析之后,我们就可以通过浏览器访问我们的web服务了(建议使用IE浏览器)。

result1

result2

通过IE浏览器可以查看一下我们的证书路径。注意:根证书是需要安装的,如果不明白,可以查阅资料。

证书路径

综上,经历了这么多的配置,我们整体就会明白,https 通讯加密,以及证书申请到底是怎么回事。在实际生产中也可以结合此案例来进行操作。若有疑问,欢迎留言。

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-10/147352.htm

linux
相关资讯       CA证书  Https加密通讯 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款