安全工具ssl

如何实现ssl
  
  
  
  
  1. 1、要想使我们的web服务器支持ssl的功能,第一步需要安装ssl的模块  
  2. yum install mod_ssl -y   #安装在172.16.50.5上  
  3. 2、提供CA,CA自签证书让我们的服务器生成一段密钥,把公钥发送给服务器端,让服务器端实现签名  
  4. 在这里我们使用两台主机来实现CA,它们的IP地址分别为  
  5. 172.16.50.5,172.16.50.4,让172.16.50.4作为CA  
  6. 3、生成一个私钥(这是在172.16.50.4上生成的) 如图1

图1

  
  
  
  
  1. 4、生成自签证书   #如图2
  2. 在生成自签证书时它会让我们填很多的信息,填起来很烦人,但是他也有默认信息,我们只有修改  
  3. 一下它的默认选项,下次再用时就不用填了  
  4. vim /etc/pki/tls/openssl.cnf   #编辑配置文件  
  5. [ req_distinguished_name ]  
  6. countryName                     = Country Name (2 letter code)  
  7. countryName_default             = CN   #默认国家名  
  8. countryName_min                 = 2  
  9. countryName_max                 = 2  
  10.  
  11. stateOrProvinceName             = State or Province Name (full name)  
  12. stateOrProvinceName_default     = Henan   #默认省份  
  13.  
  14. localityName                    = Locality Name (eg, city)  
  15. localityName_default            = zhengzhou   #默认城市名  
  16.  
  17. 0.organizationName              = Organization  Name  (eg, company)  
  18. 0.organizationName_default      = Magedu   #组织名称  
  19.  
  20. # we can do this but it  is   not  needed normally :-)  
  21. #1.organizationName             =  Second  Organization  Name  (eg, company)  
  22. #1.organizationName_default     = World Wide Web Pty Ltd  
  23.  
  24. organizationalUnitName          = Organizational Unit  Name  (eg,  section )  
  25. organizationalUnitName_default  = Tech   #部门名称  
  26. 好了,现在我们来生成自签证书 
图2
  
  
  
  
  1. 5、这里我们还要改一下配置文件中我们生成自签证书的路径  
  2. vim /etc/pki/tls/openssl.cnf   
  3. [ CA_default ]  
  4.  
  5. dir             = /etc/pki/CA  路径位置 # Where everything is kept  
  6. certs           = $dir/certs 生成证书的位置 # Where the issued certs are kept  
  7. crl_dir         = $dir/crl  证书吊销列表的位置  # Where the issued crl are kept  
  8. database        = $dir/index.txt 签订的证书放在这个索引文件中# database index file.  
  9. #unique_subject = no                    # Set to 'no' to allow creation of 
  10.                                         # several ctificates with same subject.  
  11. new_certs_dir   = $dir/newcerts 新签的证书的位置 # default place for new certs.  
  12.  
  13. certificate     = $dir/cacert.pem CA证书      # The CA certificate  
  14. serial          = $dir/serial 序列号          # The current serial number  
  15. crlnumber       = $dir/crlnumber        # the current crl number  
  16.                                         # must be commented out to leave a V1 CRL  
  17. crl             = $dir/crl.pem          # The current CRL  
  18. private_key     = $dir/private/cakey.pem生成的私钥# The private key 
  19. RANDFILE        = $dir/private/.rand    # private random number file  
  20.  
  21. x509_extensions = usr_cert              # The extentions to add to the cert  
  22. 6、好了配置文件改好,接下来准备CA需要的目录和文件(注意这些工作是在CA目录下完成的)  
  23. [root@server21 CA]# mkdir certs crl newcerts   #创建目录  
  24. [root@server21 CA]# touch index.txt    #创建文件  
  25. [root@server21 CA]# echo 01 > serial    #序列号  
  26. [root@server21 CA]# ls    #查看生成的目录及文件  
  27. cacert.pem  certs  crl  index.txt  newcerts  private  serial  
  28. 现在CA就可以用了,那么接下来如果有人需要用到证书,他只需要申请一对密钥、并把他  
  29. 的申请放到我们这里,并生成一个证书签署请求,把请求发到我们的服务器上来签署就可以了  
  30. 7、回到我们的服务器端(172.16.50.5)  
  31. 如果刚才那个证书就是给我们的web服务器用的,因此我们需要把证书放在/etc/httpd/  
  32. [root@station41 httpd]# cd /etc/httpd/  
  33. [root@station41 httpd]# cd  
  34. [root@station41 ~]# cd /etc/httpd/  
  35. [root@station41 httpd]# ls  
  36. conf  conf.d  logs  modules  run  
  37. [root@station41 httpd]# mkdir ssl -pv  
  38. mkdir: created directory `ssl'  
  39. [root@station41 httpd]# cd ssl/  
  40. #生成一对密钥,把公钥包装成证书签署请求发送给服务器  
  41. [root@station41 ssl]# (umask 077; openssl genrsa 1024 > httpd.key)  
  42. Generating RSA private key, 1024 bit long modulus         
  43. ..........................++++++  
  44. ..............................++++++  
  45. is 65537 (0x10001)  
  46. #生成证书颁发请求  
  47. [root@station41 ssl]# openssl req -new -key httpd.key -out httpd.csr  
  48. You are about to be asked to enter information that will be incorporated  
  49. into your certificate request.  
  50. What you are about to enter is what is called a Distinguished Name or a DN.  
  51. There are quite a few fields but you can leave some blank  
  52. For some fields there will be a default value,  
  53. If you enter '.', the field will be left blank.  
  54. -----  
  55. Country Name (2 letter code) [CN]:国家  
  56. State or Province Name (full name) [Henan]:省份  
  57. Locality Name (eg, city) [Zhengzhou]:zhengzhou城市  
  58. Organization Name (eg, company) [MagEdu]:Magedu组织机构  
  59. Organizational Unit Name (eg, section) [Tech]: 部门  
  60. Common Name (eg, your name or your server's hostname) []:www.jll.com 主机名,你给谁用就写谁  
  61. Email Address []:[email protected]    #邮件  
  62.  
  63. Pleaseenter the following 'extra' attributes  
  64. to be sent with your certificate request  
  65. A challenge password []:  
  66. An optional company name []:  
  67. [root@station41 ssl]# ls  
  68. httpd.csr  httpd.key 
  69. 8、返回CA主机,把刚才的复制过来,或者直接在服务器端传送,都可以  
  70. [root@server21 tmp]# scp 172.16.50.5:/etc/httpd/ssl/httpd.csr ./  
  71. [email protected]'s password:   
  72. httpd.csr                                                     100%  688     0.7KB/s   00:00      
  73. [root@server21 tmp]# ll  
  74. total 8  
  75. -rw-r--r-- 1 root root 688 Apr 10 02:15 httpd.csr  
  76. 9、CA签署  
  77. [root@server21 ~]# openssl ca -in /tmp/httpd.csr -out /tmp/httpd.crt -days 3650  
  78. Using configuration from /etc/pki/tls/openssl.cnf  
  79. Check that the request matches the signature  
  80. Signature ok  
  81. Certificate Details:  
  82.         Serial Number: 1 (0x1)  
  83.         Validity  
  84.             Not Before: Apr  9 18:20:00 2013 GMT  
  85.             Not After : Apr  7 18:20:00 2023 GMT  
  86.         Subject:  
  87.             countryName               = CN  
  88.             stateOrProvinceName       = Henan  
  89.             organizationName          = Magedu  
  90.             organizationalUnitName    = Tech  
  91.             commonName                = www.jll.com  
  92.             emailAddress              = [email protected]  
  93.         X509v3 extensions:  
  94.             X509v3 Basic Constraints:   
  95.                 CA:FALSE 
  96.             Netscape Comment:   
  97.                 OpenSSL Generated Certificate  
  98.             X509v3 Subject Key Identifier:   
  99.                 C2:94:C8:E7:A1:70:36:09:92:4F:0D:BD:42:8A:F9:5D:1F:64:32:DC  
  100.             X509v3 Authority Key Identifier:   
  101.                 keyid:27:71:DB:56:8E:33:29:76:1B:D6:92:BC:5E:57:D0:AE:70:5F:BB:8A  
  102. Certificate is to be certified until Apr  7 18:20:00 2023 GMT (3650 days)  
  103. Sign the certificate? [y/n]:y  #确定  
  104. out of 1 certificate requests certified, commit? [y/n]y  #确定  
  105. Write out database with 1 new entries  
  106. Data Base Updated  
  107. [root@server21 ~]# cd /etc/pki/CA/  #验证  
  108. [root@server21 CA]# ls  
  109. cacert.pem  crl        index.txt.attr  newcerts  serial  
  110. certs       index.txt  index.txt.old   private   serial.old  
  111. [root@server21 CA]# cat index.txt  #查看  
  112. V   230407182000Z       01  unknown /C=CN/ST=Henan/O=Magedu/OU=Tech/CN=www.jll.com/[email protected]  
  113. [root@server21 CA]# cat serial   #查看序列号  
  114. 02  
  115. 10、签署完成,把证书复制给172.16.50.4即可  
  116. root@server21 CA]# scp /tmp/httpd.crt 172.16.50.5:/etc/httpd/ssl/  
  117. [email protected]'s password:   
  118. httpd.crt                                                     100% 3822     3.7KB/s    
  119. 11、回到172.16.50.5  
  120. 查看是否复制成功  
  121. [root@station41 ssl]# ls  
  122. httpd.crt  httpd.csr  httpd.key 
  123. 12、再回到172.16.50.4  
  124. 此时为了安全起见我们应该删除tmp下安全性文件,以防别人获取你的安全信息  
  125. [root@server21 CA]# cd /tmp/  
  126. [root@server21 tmp]# ls  
  127. httpd.crt  httpd.csr  
  128. [root@server21 tmp]# rm -rf httpd.c*  
  129. [root@server21 tmp]# ls  
  130. [root@server21 tmp]#   
  131. 13、创建工作环境  
  132. [root@station41 ~]# cd /etc/httpd/conf.d/  
  133. [root@station41 conf.d]# ls  
  134. manual.conf  proxy_ajp.conf  README  ssl.conf  welcome.conf  
  135. [root@station41 conf.d]# cp ssl.conf ssl.conf.bak  #修改配置文件前先备份一份  
  136. [root@station41 conf.d]# vim ssl.conf   #修改配置文件,如图,修改完成后在进行下面的工作  
  137.  
  138.  
  139. [root@station41 conf.d]# httpd -t  
  140. Warning: DocumentRoot [/www/jll.com] does not exist   #目录不存在,创建一下即可  
  141. Syntax OK  
  142. 创建虚拟主机  
  143. vim /etc/httpd/conf.d/virtual.conf  
  144.  NameVirtualHost 172.16.50.5:80  
  145. <VirtualHost 172.16.50.5:80>  
  146.  ServerName www.jll.com  
  147.  DocumentRoot "/www/jll.com" 
  148. </VirtualHost>     #这部分知识在博文apache的配置中有详细介绍  
  149. 取消中心主机  
  150. vim /etc/httpd/conf/httpd.conf  
  151. DocumentRoot "/var/www/html"    #将此行注释掉  
  152.  
  153. [root@station41 conf.d]# mkdir /www/jll.com -pv  #创建目录  
  154. mkdir: created directory `/www'  
  155. mkdir: created directory `/www/jll.com'  
  156. [root@station41 conf.d]# httpd -t  
  157. Syntax OK  
  158. [root@station41 conf.d]# vim /www/jll.com/index.html   #编辑一下文档  
  159. <h1>jll.com</h1>  
  160. [root@station41 conf.d]# service httpd restart   #重启服务  
  161. Stopping httpd:                                            [  OK  ]  
  162. Starting httpd:                                            [  OK  ]  
  163. [root@station41 conf.d]# netstat -tnlp   #查看监听的443端口是否启动  
  164. tcp        0      0 0.0.0.0:443                 0.0.0.0:*                   LISTEN      7901/httpd            
  165. 14、在windows上使用主机名解析  
  166. 首先在hosts文件中添加  
  167. 172.16.50.5 www.jll.com  
  168. [root@server21 ~]# cd /etc/pki/CA/    #在172.16.50.4上完成的操作  
  169. 将此/etc/pki/httpd/cacert.pem导出到物理主机上,并重命名为cacert.crt  
  170. 双击并安装  
  171. 此时在访问www.jll.com就可以了  
  172. https://www.jll.com 如图 
  173. 到此我们的CA认证就做好了 
 
这就是简单的CA认证,你会了吗?可能会有一种晕的感觉,嘿嘿,多做几遍就可以了,不要急哦!

你可能感兴趣的:(ssl)