linux,SSL的实现过程

简介:

 

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. //然后去修改配置文件中的默认信息,将其改为我们通常使用的  
  15. [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.  
  6. dir             = /etc/pki/CA//改为这样子  
  7. //然后创建  
  8. [root@localhost CA]# mkdir certs crl newcerts  
  9. [root@localhost CA]# touch index.txt  
  10. [root@localhost CA]# echo 01 > serial  
  11. [root@localhost CA]# ls  
  12. cacert.pem  certs  crl  index.txt  newcerts  private  serial  
  13. //这个时候就已经准备好了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.    
  31. Please enter the following 'extra' attributes  
  32. to be sent with your certificate request  
  33. A challenge password []:  
  34. An optional company name []:  
  35. [root@Cyz ssl]# ls  
  36. httpd.csr httpd.key  
  37. [root@Cyz ssl]# scp httpd.csr 172.16.111.9:/tmp //将csr(证书签署请求)复制到CA  
  38. The authenticity of host '172.16.111.9 (172.16.111.9)' can't be established.  
  39. RSA key fingerprint is 44:0a:1f:77:7f:cb:df:09:a8:8d:ac:23:47:b3:a8:99.  
  40. Are you sure you want to continue connecting (yes/no)? yes  
  41. Warning: Permanently added '172.16.111.9' (RSA) to the list of known hosts.  
  42. root@172.16.111.9's password:   
  43. 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.    
  27. Certificate is to be certified until Apr 8 08:41:24 2023 GMT (3650 days)  
  28. Sign the certificate? [y/n]:y  
  29.    
  30.    
  31. 1 out of 1 certificate requests certified, commit? [y/n]y  
  32. Write out database with 1 new entries  
  33. Data Base Updated //颁发成功  
  34.    
  35. //去查看生成的证书  
  36. [root@localhost CA]# cd /etc/pki/CA/      
  37. [root@localhost CA]# ls  
  38. cacert.pem crl        index.txt.attr newcerts serial  
  39. certs       index.txt index.txt.old   private   serial.old  
  40. [root@localhost CA]# cat index.txt //查看内容  
  41. V 230408084124Z            01    unknown       /C=CN/ST=Henan/O=MageEdu/OU=Tech/CN=hello.magedu.com/emailAddress=hello@magedu.com  
  42. [root@localhost CA]# cat serial //已经自动排序  
  43. 02  
  44.    
  45. //然后把证书发送给请求者。这里我们到客户端去复制证书  
  46. [root@Cyz ssl]# scp 172.16.111.9:/tmp/httpd.crt ./  
  47. root@172.16.111.9's password:   
  48. httpd.crt                                                   100% 3864     3.8KB/s   00:00  
  49.    
  50. 这个时候要记得返回CA中将tmp下的临时文件给删除掉以免别人获取  
  51. [root@localhost CA]# cd /tmp/  
  52. [root@localhost tmp]# ls  
  53. busybox                  grub-install.log.s11228 httpd.csr whatis.Fa3163  
  54. grub-install.img.o11227 httpd.crt                initrd  
  55. [root@localhost tmp]# rm httpd.c*  
  56. rm: remove regular file `httpd.crt'? y  
  57. 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. //将里面的内容作如下修改:  
  7.    
  8. <VirtualHost 172.16.111.1:443> 
  9.    
  10. #ServerName www.example.com:443  
  11. ServerName hello.magedu.com  
  12. DocumentRoot "/www/magedu.com"  
  13.    
  14. SSLCertificateFile /etc/httpd/ssl/httpd.crt   //证书文件  
  15.    
  16. SSLCertificateKeyFile /etc/httpd/ssl/httpd.key //密钥文件  
  17. //检查语法  
  18. [root@Cyz conf.d]# httpd -t  
  19. Syntax OK  
  20.    
  21. //重启服务  
  22. [root@Cyz conf.d]# service httpd restart  
 

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

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

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

点击这里的绿色按钮
 

 

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

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










本文转自 陈延宗 51CTO博客,原文链接:http://blog.51cto.com/407711169/1175701,如需转载请自行联系原作者
目录
相关文章
|
9月前
|
XML 安全 应用服务中间件
Linux上面配置Apache2支持Https(ssl)具体方案实现
虽然Nginx比较流行,但是由于一些老项目用到了Apache2来支持Web服务,最近想给服务上一个Https支持,虽然看似教程简单,但是也遇到一些特殊情况,经历了一番折腾也算是解决了所有问题,将过程记录如下。演示是基于Ubantu系统。
287 0
|
6月前
|
tengine 应用服务中间件 Linux
【Linux环境】如何在Nginx(或Tengine)服务器上安装ssl证书----介绍nginx服务器类型证书的下载与安装操作
【Linux环境】如何在Nginx(或Tengine)服务器上安装ssl证书----介绍nginx服务器类型证书的下载与安装操作
298 0
|
2月前
|
存储 Linux 网络安全
如何在 Linux 中删除 SSL 证书和 SSH 密码?
如何在 Linux 中删除 SSL 证书和 SSH 密码?
79 1
如何在 Linux 中删除 SSL 证书和 SSH 密码?
|
4月前
|
应用服务中间件 Linux 网络安全
Linux【脚本 06】HTTPS转发HTTP安装OpenSSL、Nginx(with-http_ssl_module)及自签名的X.509数字证书生成(一键部署生成脚本分享)
Linux【脚本 06】HTTPS转发HTTP安装OpenSSL、Nginx(with-http_ssl_module)及自签名的X.509数字证书生成(一键部署生成脚本分享)
64 1
|
7月前
|
应用服务中间件 Linux 网络安全
Linux配置Nginx SSL支持Https配置教程
Linux配置Nginx SSL支持Https配置教程
|
9月前
|
Linux 应用服务中间件 网络安全
linux中nfs的使用以及ssl(2)
linux中nfs的使用以及ssl(2)
85 0
|
9月前
|
Linux 网络安全 Apache
linux中nfs的使用以及ssl(1)
linux中nfs的使用以及ssl(1)
96 0
|
9月前
|
应用服务中间件 Linux 网络安全
宝塔Linux面板nginx配置SSL证书的解决方案
宝塔Linux面板nginx配置SSL证书的解决方案
420 0
|
安全 应用服务中间件 Linux
【Linux】宝塔面板 SSL 证书安装部署
前期有讲过Tomcat和Nginx分别部署SSL证书,但也有好多小伙伴们私信我说,帮忙出一期宝塔面板部署SSL证书的教程,毕竟宝塔的用户体量也是蛮大的,于是宠粉的博主,当然会满足粉丝的任何要求啦~如果有帮助,请点赞收藏➕关注哦~
256 0
【Linux】宝塔面板 SSL 证书安装部署
|
Linux 网络安全 Apache
Linux 系统Apache配置SSL证书
在Centos7系列系统下,配置Apache服务器,给服务器增加SSL证书功能,让页面访问是不再提示不安全,具体操作流程如下。
387 0