码迷,mamicode.com
首页 > Web开发 > 详细

​https提供安全的web通讯

时间:2017-11-15 00:55:52      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:​https提供安全的web通讯

1.原理部分:

1)了解加密算法:

加密算法的分类:对称加密和非对称加密

a.对称加密:加密和解密使用同一个密钥,优点是速度快,缺点是密钥的共享困难。典型的对称加密算法有DES/AES/RC5/3DES等。

b.非对称加密:生成一个秘密对(公钥和私钥),加密过程中可以是私钥加密公钥解密;也可以是公钥加密私钥解密;一般情况下私钥由服务器保存,公钥共享给客户端,采用公加私解的方式。它的特征是不论你得到公钥还是私钥都是无法逆推密钥对的另一半,这保证了密钥的安全性。缺点是加密速度极慢,不适合加密数据量大的流量。典型的非对称加密算法有RSA/DSA.

如何选择加密算法?

如果选择对称加密,密码的共享(传输)过程不安全;如果选择非对称加密,加密速度慢。

一个完美的解决方案:

用对称加密的密钥用于加密数据,用非对称加密来保护对称加密的密钥,实现又快又安全的数据加密。保证了数据的私密性。


2)证书服务器:CA

在上述方案中还存在这样一个问题:如何确认公钥是由真正的密钥对拥有者所共享(传输)的。解决该问题的方案是证书认证,CA服务器提供证书认证服务。

证书认证的过程:

a.服务器生成密钥对(公钥和私钥)和认证请求,

b.CA服务器根据认证请求为服务器颁发根证书,

c.服务器获取根证书并共享给客户机,客户导入根证书.

d.通讯过程中,客户机依据根证书确认公钥的合法性.


证书服务器分为:公共证书服务器(如微软、google等)和企业自建的私有证书服务器(openssl实现)。证书认证服务器提供了数据的不可否认性。


3)数字签名:HASH

在上述的方案中,依然还存在一个问题:无法判断数据在传输过程中的完整性(是否被篡改过)。

典型的HASH算法:MD5,SHA1,SHA256,SHA512等。


服务器使用HASH算法对所需传输的数据进行hash计算的出一串数字,并将这串数字公布,数据从服务器上传输到客户机后,客户机使用相同的hash算法计算hash值,如果和服务器公布的数字签名一致,则数据没有被篡改,反之亦然。这样就保证了数据的完整性。


4)了解https的工作原理:

https(Hypertext Transfer Protocol over Secure Socket Layer),即http下加入了SSL,端口默认为443.

SSL:安全套接字层,是netscape公司设计的主要用于安全传输。

https通讯过程:

a.客户端请求https链接(通过https://实现),服务端返回证书(携带了公钥、证书的颁发机构、选择一组加密算法和HASH算法等信息)给客户端。

b.客户端收到证书后:验证证书的合法性,生成随机密码(使用协商好的对称加密算法)并使用公钥加密,使用约定的HASH计算握手消息并使用随机密码对消息进行加密。

c.客户端将由公钥加密的随机密码和由随机密码加密过的HASH数字签名发给服务器。

d.服务器(网站)收到随机密码和数字签名后:用私钥解密得到随机密码,用随机密码解密得到数字签名,用数字签名验证握手消息的完整性。并使用随机密码加密一段握手消息发给客户端(浏览器)。

e.浏览器解密握手并计算握手hash,确保数据的完整性。之后的通信数据使用随机密码进行加密(对称算法)。


2.实验:实现https的安全web服务

1)配置域名支持ca:

[root@ns ~]# vim /var/named/chroot/var/named/gxfc.com.zone  ##添加ca主机记录

ca   IN   A    192.168.100.151

:wq

[root@ns ~]# /etc/init.d/named restart  ##重启服务

[root@ns ~]# nslookup 

> server 192.168.100.100

Default server: 192.168.100.100

Address: 192.168.100.100#53

> ca.gxfc.com

Server: 192.168.100.100

Address: 192.168.100.100#53


Name: ca.gxfc.com

Address: 192.18.100.151

> exit


2)配置CA服务器:(192.168.100.151)

a.使用母盘克隆虚拟机,命名为ca服务器,修改如下:

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 

DEVICE=eth0
HWADDR=00:0C:29:75:e6:eb
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=static
IPADDR=192.168.100.151
NETMASK=255.255.255.0
DNS1=192.168.100.100
GATEWAY=192.168.100.100

:wq


[root@localhost ~]# vim /etc/sysconfig/network

HOSTNAME=ca.gxfc.com

:wq


[root@localhost ~]#  vim /etc/udev/rules.d/70-persistent-net.rules   ##删除eth0,修改eth1为eth0(已经修改的略过次步骤)

[root@localhost ~]#  reboot

b.配置CA:

[root@ca ~]# hostname 

ca.sggfu.com

[root@ca ~]# yum -y install openssl openssl-devel   ##安装openssl

[root@ca ~]# rpm -ql openssl

/etc/pki/CA

/etc/pki/CA/certs   ##证书存放目录

/etc/pki/CA/crl     ##吊销的证书存放的目录

/etc/pki/CA/newcerts##新证书目录

/etc/pki/CA/private ##私钥存放目录

/etc/pki/tls/openssl.cnf   ##主配置文件

/usr/bin/openssl     ##主程序命令

[root@ca ~]# vim /etc/pki/tls/openssl.cnf   ##修改主配置文件使用“:set nu”打印行号

 40 [ CA_default ]
 41 
 42 dir             = /etc/pki/CA           # Where everything is kept
 43 certs           = $dir/certs            # Where the issued certs are kept
 44 crl_dir         = $dir/crl              # Where the issued crl are kept
 45 database        = $dir/index.txt        # database index file.
 46 #unique_subject = no                    # Set to ‘no‘ to allow creation of
 47                                         # several ctificates with same subject.
 48 new_certs_dir   = $dir/newcerts         # default place for new certs.
 49 
 50 certificate     = $dir/cacert.pem       # The CA certificate
 51 serial          = $dir/serial           # The current serial number
 52 crlnumber       = $dir/crlnumber        # the current crl number
 53                                         # must be commented out to leave a V1 CRL
 54 crl             = $dir/crl.pem          # The current CRL
 55 private_key     = $dir/private/cakey.pem# The private key
130 countryName_default             = CN    ##修国家
135 stateOrProvinceName_default     = beijing   ##设置省
138 localityName_default            = beijing   ##设置城市
141 0.organizationName_default      = gxfc.com Ltd   ##设置组织名称
148 organizationalUnitName_default  = tech  ##设置部门

:wq

[root@ca ~]# cd /etc/pki/CA/

[root@ca CA]# ls private/

[root@ca CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)  ##生成私钥同时将权限设置为600

Generating RSA private key, 2048 bit long modulus

....................+++

...........................................................................................+++

e is 65537 (0x10001)

[root@ca CA]# ls -l private/  ##验证私钥

总用量 4

-rw-------. 1 root root 1679 1月   2 20:09 cakey.pem

[root@ca CA]# 

[root@ca CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650  ##生成自签证书(根证书)

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) [BeiJing]:

Locality Name (eg, city) [BeiJing]:

Organization Name (eg, company) [gxfc.com]:

Organizational Unit Name (eg, section) [tech]:

Common Name (eg, your name or your server‘s hostname) []:ca.gxfc.com   ##主机名填写CA服务器的主机名

Email Address []:admin@gxfc.com

[root@ca CA]# ls -l cacert.pem 

-rw-r--r--. 1 root root 1419 1月   2 20:13 cacert.pem

[root@ca CA]# 


[root@ca CA]# mkdir -p certs crl newcerts

[root@ca CA]# touch index.txt  ##证书索引

[root@ca CA]# echo 00 >serial  ##证书序列号

[root@ca CA]# ls

cacert.pem  certs  crl  index.txt  newcerts  private  serial

[root@ca CA]# 


3)配置web服务器支持https:

a.为web服务器生成密钥和证书请求:

[root@www ~]# mkdir /usr/local/httpd/conf/ssl

[root@www ~]# cd /usr/local/httpd/conf/ssl/

[root@www ssl]# (umask 077;openssl genrsa 2048 >httpd.key)

[root@www ssl]# scp root@192.168.100.151:/etc/pki/tls/openssl.cnf /etc/pki/tls/openssl.cnf  ##复制openssl配置文件

[root@www 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) [BeiJing]:

Locality Name (eg, city) [BeiJing]:

Organization Name (eg, company) [gxfc.com]:

Organizational Unit Name (eg, section) [tech]:

Common Name (eg, your name or your server‘s hostname) []:www.gxfc.com  ##必须填写web服务器的主机名,注意web虚拟主机只能有唯一一个站点可以设置为https

Email Address []:admin@gxfc.com


Please enter the following ‘extra‘ attributes

to be sent with your certificate request

A challenge password []:   ##证书保护的密码短语,直接回车

An optional company name []:

[root@www ssl]# 

[root@www ssl]# scp httpd.csr root@192.168.100.151:/tmp  ##将证书认证请求复制给CA服务器


b.登录到192.168.100.151,为web服务器签发证书:

[root@ca CA]# openssl ca -in /tmp/httpd.csr -out /tmp/httpd.crt -days 3650  ##签发证书httpd.crt,执行中y回车即可

[root@ca CA]# ls /tmp/httpd.c*  ##验证

/tmp/httpd.crt  /tmp/httpd.csr

[root@ca CA]# scp /tmp/httpd.crt root@192.168.100.150:/usr/local/httpd/conf/ssl   ##复制证书给web服务器

[root@ca CA]# rm -rf /tmp/httpd.*  ##删除证书,避免非法用户获取证书


c.修改web服务器配置文件:登录192.168.100.150

[root@www ~]# cd /usr/local/httpd/conf/extra/

[root@www extra]# cp httpd-ssl.conf httpd-ssl.conf.bak  ##备份证书

[root@www extra]# vim httpd-ssl.conf  ##修改如下

74 <VirtualHost 192.168.100.150:443>
77 DocumentRoot "/usr/local/httpd/htdocs/gxfc/"   ##注意和http的网页根目录一致
78 ServerName www.gxfc.com:443
79 ServerAdmin admin@gxfc.com
80 ErrorLog "/usr/local/httpd/logs/error_log"
81 TransferLog "/usr/local/httpd/logs/access_log"
85 SSLEngine on   ##确认为on,表示开启https
99 SSLCertificateFile "/usr/local/httpd/conf/ssl/httpd.crt"  ##指定证书路径
107 SSLCertificateKeyFile "/usr/local/httpd/conf/ssl/httpd.key"  ##指定私钥路径,注意私钥必须小心保管

:wq

[root@www extra]# vim /usr/local/httpd/conf/httpd.conf  ##修改主配置文件,调用httpd-ssl.conf

399 Include conf/extra/httpd-ssl.conf

:wq

[root@www extra]# /etc/init.d/httpd restart  ##重启服务器


4)共享根证书:

[root@www ~]# cd /usr/local/httpd/htdocs/gxfc/

[root@www sggfu]# scp root@192.168.100.151:/etc/pki/CA/cacert.pem cacert.crt  ##复制CA服务器的证书(根证书)

[root@www sggfu]# vim index.html   ##通过首页共享根证书

<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title>www.gxfc.com</title>
</head>
<body>
  <h1>www.gxfc.com</h1>
  为了你更好的访问网站,请下载安装<a href="cacert.crt" target="_blank">根证书</a>
</body>
</html>

:wq


5)测试:

http://www.gxfc.com  ##下载证书并导入证书

https://www.gxfc.com  ##访问测试


本文出自 “11628205” 博客,请务必保留此出处http://11638205.blog.51cto.com/11628205/1981873

​https提供安全的web通讯

标签:​https提供安全的web通讯

原文地址:http://11638205.blog.51cto.com/11628205/1981873

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!