HTTPD
服务器使用SSL[Secure Sockets Layer]
来实现的对于来往HTTPD
服务器的数据进行加密,完整的及时参照传输层安全性协议(英语:Transport Layer Security,缩写作 TLS),及其前身安全套接层(Secure Sockets Layer,缩写作 SSL)是一种安全协议,目的是为互联网通信,提供安全及数据完整性保障。网景公司(Netscape)在1994年推出首版网页浏览器,网景导航者时,推出HTTPS协议,以SSL进行加密,这是SSL的起源。IETF将SSL进行标准化,1999年公布第一版TLS标准文件。随后又公布RFC 5246 (2008年8月)与 RFC 6176 (2011年3月)。在浏览器、电子邮件、即时通信、VoIP、网络传真等应用程序中,广泛支持这个协议。主要的网站,如Google、Facebook等也以这个协议来创建安全连接,发送数据。目前已成为互联网上保密通信的工业标准 ——–>来自于维基百科[]
SSL
协议一共包含三个版本:V1
:很少有人知道,因为没有公开;V2
:存在较多的安全漏洞;V3
:虽然进行重新设计,但是被暴露出存在重大缺陷,Google
,Mozilla
强制使用TLS
来代替SSL
;
- 参照维基百科的解释来说明
SSL
的工作的大致过程
- 协商加密算法以及协议版本:
- 发送一个“ClientHello”消息,内容包括:支持的协议版本,比如TLS1.0版,一个客户端生成的随机数(稍后用于生成“会话密钥”),支持的加密算法(如RSA公钥加密)和支持的压缩算法;
- 然后收到一个“ServerHello”消息,内容包括:确认使用的加密通信协议版本,比如TLS 1.0版本(如果浏览器与服务器支持的版本不一致,服务器关闭加密通信),一个服务器生成的随机数(稍后用于生成“对话密钥”),确认使用的加密方法(如RSA公钥加密),服务器证书
- 证书交换的过程:
- 当双方知道了连接参数,客户端与服务器交换证书(依靠被选择的公钥系统)。这些证书通常基于X.509,不过已有草案支持以OpenPGP为基础的证书;
- 服务器请求客户端公钥。客户端有证书即双向身份认证,没证书时随机生成公钥;
- 客户端与服务器通过公钥保密协商共同的主私钥(双方随机协商),这通过精心谨慎设计的伪随机数功能实现。结果可能使用Diffie-Hellman交换,或简化的公钥加密,双方各自用私钥解密。所有其他关键数据的加密均使用这个“主密钥”。数据传输中记录层(Record layer)用于封装更高层的HTTP等协议。记录层数据可以被随意压缩、加密,与消息验证码压缩在一起。每个记录层包都有一个Content-Type段用以记录更上层用的协议。
- 协商加密算法以及协议版本:
- HTTPS结合SSL来实现
- 这里需要注意的是
SSL
和绘画仅仅能够基于IP
地址进行,认证是对于每个一IP
进行的认证,所以在基于域名的虚拟主机里面只有一个可以用于创建SSL
会话; - 首先查看
HTTPD
支持的模块是否包括SSL
模块
[root@server9 conf.d]# httpd -M
Loaded Modules:
core_module (static)
mpm_prefork_module (static)
http_module (static)
so_module (static)
auth_basic_module (shared)
auth_digest_module (shared)
authn_file_module (shared)
authn_alias_module (shared)
....
- 虽然包括很多,但是这里面时没有提供
SSL
模块的,首先需要安装
[root@server9 conf.d]# yum install mod_ssl.x86_64 -y
- 这个模块生成的文件包括以下
[root@server9 conf.d]# rpm -ql mod_ssl
/etc/httpd/conf.d/ssl.conf //ssl的配置文件
/usr/lib64/httpd/modules/mod_ssl.so //动态库文件
/var/cache/mod_ssl
/var/cache/mod_ssl/scache.dir
/var/cache/mod_ssl/scache.pag
/var/cache/mod_ssl/scache.sem
接下来提供一个机器作为
CA
,然后在提供HTTPD
服务器server9.com 172.25.23.9
server10.com 172.25.23.10选择将
server10.com
作为CA
服务器,选择将server9.com
作为HTTPD
服务器- 首先进入下面的目录,首先生成私钥
[root@server10 CA]# pwd
/etc/pki/CA
[root@server10 CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
............+++
...............+++
e is 65537 (0x10001)
- 既然是私钥,权限应该是下面
[root@server10 CA]# ll private/cakey.pem
-rw-------. 1 root root 1675 4月 8 20:29 private/cakey.pem
- 接下来编辑配置文件,提供生成私钥时的默认选项
[root@server10 CA]# vim ../tls/openssl.cnf
countryName_default = CN
stateOrProvinceName_default = ShaanXi
localityName_default = XiAn
0.organizationName_default = linux
organizationalUnitName_default = Know
- 然后生成自签证书,上面的操作不是必须的,只是为了防止重复的写入会比较麻烦
[root@server10 CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3655
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) [CN]:
State or Province Name (full name) [ShaanXi]:
Locality Name (eg, city) [XiAn]:
Organization Name (eg, company) [linux]:
Organizational Unit Name (eg, section) [Know]:
Common Name (eg, your name or your server's hostname) []:ca.linux.com //自签证书的主机名称并没有要求;
Email Address []:admin@localhost.com
- 接下来更改
SSL
配置文件关于自签证书的配置文件
[root@server10 CA]# vim ../tls/openssl.cnf
修改
dir = /etc/pki/CA
- 这些目录时需要进行创建的
[root@server10 CA]# ls
cacert.pem certs crl index.txt newcerts private serial
创建生成以上文件,并且执行
[root@server10 CA]# echo 01 > serial
- 接下来
HTTPD
服务器生成自己的秘钥
[root@server9 ~]# mkdir /etc/httpd/ssl
[root@server9 ~]# cd /etc/httpd/ssl
[root@server9 ssl]# (umask 077; openssl genrsa 1024 > httpd.key)
Generating RSA private key, 1024 bit long modulus
.....++++++
........++++++
e is 65537 (0x10001)
- 查看生成的秘钥的权限
[root@server9 ssl]# ll httpd.key
-rw-------. 1 root root 887 4月 8 20:56 httpd.key
- 生成证书签署请求,由于是私有
CA
,在生成证书颁发请求时,必须和前面的CA
的信息保持一致,所以这里将前面的配置文件复制过来
[root@server10 CA]# scp ../tls/openssl.cnf 172.25.23.9:/etc/pki/tls/
The authenticity of host '172.25.23.9 (172.25.23.9)' can't be established.
RSA key fingerprint is 3e:14:3e:2f:0d:86:45:07:9b:80:93:05:83:0a:97:56.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.23.9' (RSA) to the list of known hosts.
openssl.cnf 100% 11KB 10.7KB/s 00:00
- 在生成自签证书时,需要注意的时关于主机名称的填写
[root@server9 ssl]# openssl req -new -key httpd.key -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) [CN]:
State or Province Name (full name) [ShaanXi]:
Locality Name (eg, city) [XiAn]:
Organization Name (eg, company) [linux]:
Organizational Unit Name (eg, section) [Know]:
Common Name (eg, your name or your server's hostname) []:www.linux.org
Email Address []:admin@localhost.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
- 上面将
CA
的请求主机名称交给www.linux.org
这个主机 - 将上面的证书签署请求发送给
CA
,请求CA
进行签署
[root@server9 ssl]# scp httpd.csr 172.25.23.10:/tmp/
httpd.csr 100% 696 0.7KB/s 00:00
- 在
server10.com
上面进行签署
[root@server10 CA]# openssl ca -in /tmp/httpd.csr -out /tmp/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: Apr 8 13:10:20 2018 GMT
Not After : Apr 8 13:10:20 2019 GMT
Subject:
countryName = CN
stateOrProvinceName = ShaanXi
organizationName = linux
organizationalUnitName = Know
commonName = www.linux.org
emailAddress = admin@localhost.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
11:A7:A4:F8:4E:2A:DC:2F:0E:E6:4F:B2:5A:9C:BB:27:71:10:21:C5
X509v3 Authority Key Identifier:
keyid:E6:9F:AA:1D:33:0F:08:D0:3C:42:6E:02:0A:FA:08:3D:C2:0A:B1:08
//上面是证书的详细信息;
Certificate is to be certified until Apr 8 13:10:20 2019 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@server9 ssl]# scp 172.25.23.10:/tmp/httpd.crt ./
httpd.crt 100% 3857 3.8KB/s 00:00
- 删除多余的文件,例如
/tmp
目录里面的文件
[root@server10 CA]# rm -f /tmp/httpd.c*
- 配置服务器使用生成的证书,这个需要编辑的是
ssl
的配置文件
更改或者添加以下信息:
<VirtualHost 172.25.23.9:443>
DocumentRoot "/var/www/html"
ServerName www.linux.org:443
SSLCertificateFile /etc/httpd/ssl/httpd.crt
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key
</VirtualHost>
- 检测配置文件的正确性,并且启动服务
[root@server9 ~]# httpd -t
Syntax OK
[root@server9 ~]# /etc/init.d/httpd start
Starting httpd: [ OK ]
- 还需要添加解析
172.25.23.9 www.linux.org
- 首先通过浏览器查看
httpd:80
端口是否正常
- 接下来通过
https
协议来进行访问,由于是自签名的证书,这里存在一个警告
- 点击
advanced
,可以看到具体的错误类型
- 点击
add exception
- 可以查看详细的签署界面
- 之后浏览器的访问页面就是正常的了
HTTPS
自签名生成证书的过程就完成了