SSL
的实现过程

 首先,客户端与服务器端进行三次握手。(因为http基于TCP/IP协议进行通信),然后他们建立SSL会话,协商要使用的加密算法,当协商完成之后。服务器端就会将自己的证书发送给客户端,客户端验证发现没有问题之后,就会生成一个对称密钥发送给服务器端,然后客户端就发送请求给服务器端,服务器端便会使用刚刚客户端发来的对称密钥加密将内容发送给客户端。这样ssl会话就建立起来了。
但是,客户端如何验证服务器的证书是否真实呢,那么便需要一个CA:第三方证书颁发机构给我们的服务器端颁发证书。所以客户端便可以到CA去验证服务器端的证书。
这时候的CA,应该自己有一份证书保存在客户端这边,并且这段证书是自签的。(用以客户端可以到CA去验证服务器端的证书。)
那么服务器端如何到CA让CA给自己搞一份证书呢:首先服务器端先生成一份密钥,将公钥交给CA,由CA对它签署并生成证书,保存一份并回送给服务器端。服务器对其进行配置使用,然后在通话过后就可以将证书发送给客户端,客户端询问CA在进行验证。
①前提:
要想使你的web服务器支持ssl功能,第一步得安装SSL模块
 
  1. [root@Cyz ~]# yum install mod_ssl  

  2. //查看都安装了什么  

  3. [root@Cyz ~]# rpm -ql mod_ssl  

  4. /etc/httpd/conf.d/ssl.conf //说明是配置文件,更改配置需要重启  

  5. /usr/lib/httpd/modules/mod_ssl.so  

  6. /var/cache/mod_ssl          //缓存目录  

  7. /var/cache/mod_ssl/scache.dir  

  8. /var/cache/mod_ssl/scache.pag  

  9. /var/cache/mod_ssl/scache.sem  

②提供CA
重新找台主机,用这台主机做我们的CA:这台主机的IP为111.9

要想做CA,首先得生成自签证书,:
 
  1. [root@localhost ~]# cd /etc/pki/CA/  

  2. //生成私钥  

  3. [root@localhost CA]# (umask 077; openssl genrsa -out private/cakey.pem 2048)  

  4. //umask为了生成时权限其他用户无权限访问 –out表示路径)  

  5. Generating RSA private key, 2048 bit long modulus  

  6. .......................................+++  

  7. ........................+++  

  8. e is 65537 (0x10001)  

  9. //查看权限  

  10. [root@localhost CA]# ls -l private/  

  11. total 8  

  12. -rw------- 1 root root 1679 Apr 10 16:15 cakey.pem //600的权限  

  13. //然后去修改配置文件中的默认信息,将其改为我们通常使用的  

  14. [root@localhost CA]# vim ../tls/openssl.cnf  

 
  1. //为自己生成自签证书  

  2. [root@localhost CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3655  

  3. //然后继续编辑openssl.cnf找到  

  4. [ CA_default ]  

  5. dir             = /etc/pki/CA//改为这样子  

  6. //然后创建  

  7. [root@localhost CA]# mkdir certs crl newcerts  

  8. [root@localhost CA]# touch index.txt  

  9. [root@localhost CA]# echo 01 > serial  

  10. [root@localhost CA]# ls  

  11. cacert.pem  certs  crl  index.txt  newcerts  private  serial  

  12. //这个时候就已经准备好了CA就可以使用了

这个时候回到我们的客户端111.1
 
  1. [root@Cyz ~]# cd /etc/httpd/          //保存在这里  

  2. [root@Cyz httpd]# mkdir ssl  

  3. [root@Cyz httpd]# ls  

  4. conf conf.d logs modules run ssl  

  5. [root@Cyz httpd]# cd ssl/  

  6. [root@Cyz ssl]# ls  

  7. [root@Cyz ssl]# (umask 077; openssl genrsa 1024 > httpd.key)    //生成密钥  

  8. Generating RSA private key, 1024 bit long modulus  

  9. ................................++++++  

  10. .....................................................++++++  

  11. e is 65537 (0x10001)  

  12. [root@Cyz ssl]# ll  

  13. total 8  

  14. -rw------- 1 root root 887 Apr 10 16:33 httpd.key  

  15. [root@Cyz ssl]# openssl req -new -key httpd.key -out httpd.csr  //生成证书颁发请求  

  16. You are about to be asked to enter information that will be incorporated  

  17. into your certificate request.  

  18. What you are about to enter is what is called a Distinguished Name or a DN.  

  19. There are quite a few fields but you can leave some blank  

  20. For some fields there will be a default value,  

  21. If you enter '.', the field will be left blank.  

  22. -----  

  23. Country Name (2 letter code) [GB]:CN  

  24. State or Province Name (full name) [Berkshire]:Henan    

  25. Locality Name (eg, city) [Newbury]:Zhengzhou  

  26. Organization Name (eg, company) [My Company Ltd]:MageEdu    

  27. Organizational Unit Name (eg, section) []:Tech  

  28. Common Name (eg, your name or your server's hostname) []:hello.magedu.com  

  29. Email Address []:hello@magedu.com  

  30. Please enter the following 'extra' attributes  

  31. to be sent with your certificate request  

  32. A challenge password []:  

  33. An optional company name []:  

  34. [root@Cyz ssl]# ls  

  35. httpd.csr httpd.key  

  36. [root@Cyz ssl]# scp httpd.csr 172.16.111.9:/tmp //将csr(证书签署请求)复制到CA  

  37. The authenticity of host '172.16.111.9 (172.16.111.9)' can't be established.  

  38. RSA key fingerprint is 44:0a:1f:77:7f:cb:df:09:a8:8d:ac:23:47:b3:a8:99.  

  39. Are you sure you want to continue connecting (yes/no)? yes  

  40. Warning: Permanently added '172.16.111.9' (RSA) to the list of known hosts.  

  41. root@172.16.111.9's password:  

  42. httpd.csr                                                   100% 704     0.7KB/s   00:00  

然后回到CA进行签署
 
  1. [root@localhost CA]# openssl ca -in /tmp/httpd.csr -out /tmp/httpd.crt -days 3650  

  2. Using configuration from /etc/pki/tls/openssl.cnf  

  3. Check that the request matches the signature  

  4. Signature ok  

  5. Certificate Details:  

  6.        Serial Number: 1 (0x1)  

  7.        Validity  

  8.            Not Before: Apr 10 08:41:24 2013 GMT  

  9.            Not After : Apr 8 08:41:24 2023 GMT  

  10.        Subject:  

  11. countryName               = CN

  12. stateOrProvinceName       = Henan

  13. organizationName          = MageEdu

  14. organizationalUnitName    = Tech

  15. commonName                = hello.magedu.com  

  16. emailAddress              = hello@magedu.com  

  17.        X509v3 extensions:  

  18.            X509v3 Basic Constraints:  

  19.                CA:FALSE  

  20.            Netscape Comment:  

  21.                OpenSSL Generated Certificate  

  22.            X509v3 Subject Key Identifier:  

  23.                10:B1:D2:C5:48:58:66:7B:35:71:BD:62:1D:77:85:12:EB:36:DF:63  

  24.            X509v3 Authority Key Identifier:  

  25.                keyid:30:86:2F:D5:DC:10:09:DA:38:19:E5:72:34:05:D5:5D:CE:83:B2:86  

  26. Certificate is to be certified until Apr 8 08:41:24 2023 GMT (3650 days)  

  27. Sign the certificate? [y/n]:y  

  28. 1 out of 1 certificate requests certified, commit? [y/n]y  

  29. Write out database with 1 new entries  

  30. Data Base Updated //颁发成功  

  31. //去查看生成的证书  

  32. [root@localhost CA]# cd /etc/pki/CA/      

  33. [root@localhost CA]# ls  

  34. cacert.pem crl        index.txt.attr newcerts serial  

  35. certs       index.txt index.txt.old   private   serial.old  

  36. [root@localhost CA]# cat index.txt //查看内容  

  37. V 230408084124Z            01    unknown       /C=CN/ST=Henan/O=MageEdu/OU=Tech/CN=hello.magedu.com/emailAddress=hello@magedu.com  

  38. [root@localhost CA]# cat serial //已经自动排序  

  39. 02  

  40. //然后把证书发送给请求者。这里我们到客户端去复制证书  

  41. [root@Cyz ssl]# scp 172.16.111.9:/tmp/httpd.crt ./  

  42. root@172.16.111.9's password:  

  43. httpd.crt                                                   100% 3864     3.8KB/s   00:00  

  44. 这个时候要记得返回CA中将tmp下的临时文件给删除掉以免别人获取  

  45. [root@localhost CA]# cd /tmp/  

  46. [root@localhost tmp]# ls  

  47. busybox                  grub-install.log.s11228 httpd.csr whatis.Fa3163  

  48. grub-install.img.o11227 httpd.crt                initrd  

  49. [root@localhost tmp]# rm httpd.c*  

  50. rm: remove regular file `httpd.crt'? y  

  51. rm: remove regular file `httpd.csr'? y  

这个时候证书已经签署成功了,我们应该如何配置使用它呢:
 
  1. [root@Cyz ssl]# cd /etc/httpd/conf.d/  

  2. [root@Cyz conf.d]# ls  

  3. manual.conf php.conf proxy_ajp.conf README ssl.conf virtual.conf welcome.con1  

  4. [root@Cyz conf.d]# vim ssl.conf  

  5. //将里面的内容作如下修改:  

  6. <VirtualHost 172.16.111.1:443>

  7. #ServerName www.example.com:443  

  8. ServerName hello.magedu.com  

  9. DocumentRoot "/www/magedu.com"  

  10. SSLCertificateFile /etc/httpd/ssl/httpd.crt   //证书文件  

  11. SSLCertificateKeyFile /etc/httpd/ssl/httpd.key //密钥文件  

  12. //检查语法  

  13. [root@Cyz conf.d]# httpd -t  

  14. Syntax OK  

  15. //重启服务  

  16. [root@Cyz conf.d]# service httpd restart  

然后继续修改物理机的HOSTS文件

//添加如下
   172.16.111.1   hello.magedu.com
然后我们来访问下:(我们这里的是https://)系统提示我们这个网站安全证书有问题,原因是因为我们CA不受信任,解决方法自然是我们手动导入证书了:

我们来到CA,将CA的证书发送给物理主机一份

点击这里的绿色按钮

将其扩展名改为crt 会发现变了样子

然后双击进行安装
完成之后就可以打来IE来验证啦