Openssl軟件部署過程與介紹


OpenSSL

OpenSSL軟件介紹

Netscape網景公司生產了最初的瀏覽器,但為了提高瀏覽器訪問頁面的安全性,對TCP/IP模型進行了一定改進,在傳輸層與應用層之間,創建了一個3.5層的概念,稱為SSL((Secure Sockets Layer 安全套接層))層,SSL不是一個軟件,只是一個庫,讓應用層將數據傳輸到傳輸層前,調用了ssl層的功能對數據進行了加密,目前比較流行的版本是(SSLv2 v3),但是SSL是netscape公司進行定義的,不夠開放性,因此為了使加密功能更加開放,TSL(傳輸層安全協議)協議就出現了,目前比較流行的版本是(TSLv1==ssl v3),TSL更像是傳輸層上實現的數據加密。

SSL數據通訊實現原理

由於借助SSL實現安全數據通訊的方式比較多,本文只以最基本的HTTP協議為例,了解一下SSL數據安全通訊的過程。具體實現過程說明如下:

  1. http是基於tcp協議進行建立的,因此在建立tcp連接之前要先進行tcp的三次握手過程
  2. 三次握手之后,不馬上進行數據的傳輸,而是進行SSL的建立
  3. 客戶端首先向服務器端發起數據傳輸請求,但服務端不會回復http數據包,而是與客戶端一來一回協商生成/建立SSL會話,確認是選用SSLv2還是SSLv3,TSL協議,並且還要協商加密算法。
  4. 服務端與客戶端的加密協議協商好后,服務端會將自己的證書發送給客戶端
  5. 客戶端在獲取到證書后,先進行對證書的驗證,確認是否是信任機構頒發的,並且驗證證書是否是完整的,以及確認證書是沒有注銷的,驗證完畢后,獲得的證書中的公鑰就可以進行使用了
  6. 證書合法性驗證完畢后,客戶端會建立生成一個會話密鑰
  7. 客戶端選擇隨機數生成一個隨機密鑰,並且利用服務端的公鑰對隨機密鑰進行加密,發給服務端
  8. 服務器端收到公鑰加密的數據,就可以利用自己的私鑰進行解密,獲得對稱密鑰,就可以對傳輸數據進行加密了

OpenSSL軟件概念說明

ssl功能的開源實現,就稱為openssl,功能非常強大,幾乎實現了市面上主流的加密算法,並且工作性能非常的好。openssl的官方鏈接:http://openssl.org/

OpenSSL軟件組成

openssl是由三部分組成:
1.libcrpto:通用加密庫
2.libssl:TSL/SSL功能的實現,基於會話的,實現了身份認證,數據機密性和會話完整性的TSL/SSL庫
3.openssl:提供的命令行工具,多用途命令工具,模擬實現私有證書頒發機構;命令行工具是通過多種子命令實現openssl的相應功能

OpenSSL軟件詳細說明

01. 獲取OpenSSL軟件的版本信息:

openssl version  <- 查看openssl版本信息

02.獲取OpenSSL配置文件信息:

/etc/pki/tls/openssl.cnf  <- openssl配置文件,主要用於配置成私有ca時進行使用

03.獲取OpenSSL命令詳細參數:

[root@web01 ~]# openssl ?
openssl:Error: '?' is an invalid command.
#標准命令
Standard commands
asn1parse         ca                ciphers           cms               
crl               crl2pkcs7         dgst              dh                
dhparam           dsa               dsaparam          ec                
ecparam           enc               engine            errstr            
gendh             gendsa            genpkey           genrsa            
nseq              ocsp              passwd            pkcs12            
pkcs7             pkcs8             pkey              pkeyparam         
pkeyutl           prime             rand              req               
rsa               rsautl            s_client          s_server          
s_time            sess_id           smime             speed             
spkac             ts                verify            version           
x509              

Message Digest commands (see the `dgst' command for more details)
#單向加密命令
md2               md4               md5               rmd160            
sha               sha1              

Cipher commands (see the `enc' command for more details)
#加密命令
aes-128-cbc       aes-128-ecb       aes-192-cbc       aes-192-ecb       
aes-256-cbc       aes-256-ecb       base64            bf                
bf-cbc            bf-cfb            bf-ecb            bf-ofb            
camellia-128-cbc  camellia-128-ecb  camellia-192-cbc  camellia-192-ecb  
camellia-256-cbc  camellia-256-ecb  cast              cast-cbc          
cast5-cbc         cast5-cfb         cast5-ecb         cast5-ofb         
des               des-cbc           des-cfb           des-ecb           
des-ede           des-ede-cbc       des-ede-cfb       des-ede-ofb       
des-ede3          des-ede3-cbc      des-ede3-cfb      des-ede3-ofb      
des-ofb           des3              desx              idea              
idea-cbc          idea-cfb          idea-ecb          idea-ofb          
rc2               rc2-40-cbc        rc2-64-cbc        rc2-cbc           
rc2-cfb           rc2-ecb           rc2-ofb           rc4               
rc4-40            seed              seed-cbc          seed-cfb          
seed-ecb          seed-ofb          zlib              

[root@web01 ~]#openssl speed   <- 測試當前主機對所有openssl支持的加密算法的加密速度進行測試顯示,同時也對服務器性能做一個測試
# openssl enc -ciphername [-in filename] [-out filename] [-pass arg] [-e] [-d]
  [-a/-base64] [-A] [-k password] [-kfile filename] [-K key] [-iv IV] [-S salt]
  [-salt] [-nosalt] [-z] [-md] [-p] [-P] [-bufsize number] [-nopad] [-debug]
  [-none] [-engine id]
  ciphername            <- 指定加密的算法
  [-in filename]            <- 指定要加密的文件
  [-out filename]            <- 指定加密后輸出的文件
  [-e] [-d]                 <- e是加密文件,d是解密文件,不指定默認就是加密的
  [-a]                    <- 基於base64位進行編碼,可選參數

#舉例說明,加密一個文件
# openssl enc -des3 -salt -a -in inittab -out initab.des3        <- 輸入密碼后即加密成功
# openssl enc -des3 -d -salt -a -in initab.des3 -out inittab     <- 輸入密鑰后即解密成功
說明:其中命令中的salt參數,主要用於避免密碼加密后,對密鑰串的反推

#輸出一個文件的特征碼方式
md5sum inittab
sha1sum inittab 
openssl dgst -sha1 inittab             <- 利用openssl生成文件特征碼
dgst---表示指定使用信息摘要命令
-sha1---表示指定摘要命令選用sha1算法 

#生成和用戶一樣的密碼串
openssl passwd -1                    <- 采用md5加密用戶密碼串

#生成偽隨機數方法
openssl rand -base64 45                <- 給出一個任意的數字,就會生成任意的隨機數

利用OpenSSL軟件實現HTTPS訪問過程

第一步:創建一個私鑰文件(運維人員需要會)

[root@web01 serv_key]# openssl genrsa 2048 > server.key --->創建私鑰信息,並指定私鑰長度為2048,將生成的私鑰信息保存在一個文件中
Generating RSA private key, 2048 bit long modulus
...................................................................................
............................................................+++...................+++
e is 65537 (0x10001)
[root@web01 serv_key]# (umask 077;openssl genrsa -out server.key 2048) -->將私鑰信息直接進行保存,加密長度一定人放在輸出文件后台,同時臨時修改umask,將文件的權限變為600
Generating RSA private key, 2048 bit long modulus
.....+++
........................+++
e is 65537 (0x10001)
[root@web01 serv_key]# ll
total 4
-rw-r--r-- 1 root root 1679 Mar  1 10:49 server.key
[root@web01 serv_key]# 

第二步:生成證書文件信息

1.生成自簽發證書(運維人員可以自行操作)

[root@web01 serv_key]# openssl req -new -x509 -key server.key -out server.crt -days
req  ------> 用於請求創建一個證書文件
new  ------>表示創建的是新的證書
x509 ------>表示定義證書的格式為標准格式
key  ------>表示調用的私鑰文件信息
out  ------>表示輸出證書文件信息
days ------->表示證書的有效期
 365You 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) [XX]:cn  <---- 定義生成證書的國家
State or Province Name (full name) []:gd <---- 定義生成證書的省份
Locality Name (eg, city) [Default City]:gz <---- 定義生成證書的城市
Organization Name (eg, company) [Default Company Ltd]:baidu <---- 定義生成證書的組織
Organizational Unit Name (eg, section) []:it <- 定義生成證書的職能部門
Common Name (eg, your name or your server's hostname) []:web01 <---- 定義主機服務器名稱
說明:此輸出信息非常重要,客戶端在獲取證書前,會利用主機名與相應服務器之間建立連接,然后獲得證書
Email Address []:liuyang97@baidu.com

2.向證書頒發機構申請證書(ca證書機械完成)

1.生成請求的證書文件
[root@web01 serv_key]# openssl req -new -key server.key -out server_new.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) [XX]:cn
State or Province Name (full name) []:gd
Locality Name (eg, city) [Default City]:gz
Organization Name (eg, company) [Default Company Ltd]:baidu
Organizational Unit Name (eg, section) []:it
Common Name (eg, your name or your server's hostname) []:web01
Email Address []:liuyang97@baidu.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:liuyang1     <-- 設置證書的密碼信息
An optional company name []:liuyang1  <-- 確認證書的密碼信息
[root@web01 serv_key]# ll
total 12
-rw-r--r-- 1 root root 1367 Mar  1 10:59 server.crt
-rw------- 1 root root 1675 Mar  1 10:52 server.key
-rw-r--r-- 1 root root 1094 Mar  1 11:07 server_new.csr
3.將生成的證書文件發給證書機構,證書機構會返還一個公鑰回來

第三步:配置Nginx負載均衡服務器配置,加載私鑰和證書信息

[root@lb01 server_key]# cat /application/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream oldboy {
       server 10.0.0.7:80;
       server 10.0.0.8:80;
       server 10.0.0.9:80;    
     }
    #永久定向
    server {
        listen 80;
        server_name www1.etiantian.org;
        rewrite ^(.*)$ https://$host$1 permanent;

    }
    #ssl認證
    server {
        listen  10.0.0.3:443 ssl;
        server_name www1.etiantian.org;
        ssl_certificate /server/server_key/server.crt;
        ssl_certificate_key /server/server_key/server.key;
        root   html;
        index  index.html index.htm;
        location / {
           proxy_pass http://oldboy;
           proxy_set_header Host $host;
           proxy_set_header X-Forwarded-For $remote_addr;
        }
    }
    server {
        listen 10.0.0.4:80;
        server_name bbs.etiantian.org;
        root   html;
        index  index.html index.htm;
        location / {
           proxy_pass http://oldboy;
           proxy_set_header Host $host;
           proxy_set_header X-Forwarded-For $remote_addr;
        }
    }
}
[root@lb01 server_key]# 

第四步:測試訪問

訪問www1.etiantian.org會自動跳轉

訪問bbs正常訪問


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM