安全工具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認證,你會了嗎?可能會有一種暈的感覺,嘿嘿,多做幾遍就可以了,不要急哦!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章