簡述ssl協議及利用openssl創建私有CA

我在這個鏈接中簡單的介紹了下加密解密原理和相關算法及其實現http://starli.blog.51cto.com/8813574/1671408


CA是什麼?爲什麼需要CA? 先看下面的互聯網安全通訊模型!

現在互聯網上常用的安全通訊模型

wKiom1Wbj03zPkvAAACz-_IZLNA961.jpg

       發送方發送數據時執行的步驟如下(分別對應於上圖發送方的1、2、3):

            1、發送方將需要在互聯網上進行安全傳輸的數據採用單向加密算法提取數據的特徵碼,然後用自己的私鑰加密這段特徵碼放在數據的尾部;

            2、發送方生成一個一次性的對稱加密算法的祕鑰,然後使用對稱加密算法和生成的祕鑰將數據和加密後的特徵碼加密後生成密文;

            3、將上一步對稱加密用到的祕鑰使用接收方的公鑰加密後放在密文的後面;隨後就可以將數據放到互聯網上進行傳輸。

   

       接收方接收到數據後執行的步驟如下(分別對應於上圖接收方的1、2、3):

            1、接收方接收到數據後,用自己的私鑰解密加密後的對稱加密算法的祕鑰。(如果能解密則能驗證數據的機密性

            2、接收方用解密後得到的對稱加密算法的祕鑰後使用與發送方同樣的對稱加密算法解密加密的數據和加密的數據的特徵碼。

            3、接收方使用與發送方相同的單向加密算法提取解密後的數據的特徵碼,而後使用發送方的公鑰對上一步解密得到的加密的數據的特徵碼(如果能解密,則可以發送接收方的身份),並比較這兩個特徵碼進行比較是否一致。(如果一致,則可以驗證數據的完整)。


    在上面的通訊過程中,雙方都加密時都會用到對方的公鑰。由於公鑰是公開的,任何人都可以得到並使用。私鑰是保密的,只有私鑰的擁有者才能使用。那麼在互聯網上的安全通訊過程中,通訊雙方如何可靠的得到對方的公鑰呢?這就需要一種手段來實現。CA其實就是實現讓通訊雙方可靠的得到對方公鑰的一種手段。CA的實現方式是通過給通訊者頒發證書,目前通用的證書格式爲x509,證書基本格式如下

wKioL1WbmQ-g9rjVAADrUqTggqg256.jpg

         CA其實就是一個大家都信任的公信機構,因此如果信任該CA就信任此CA給通訊者頒發的證書,那麼就可以放心的使用證書中的公鑰跟證書的擁有者通訊。CA要想給客戶端發證書,首先就要給自己簽署一個自簽證書。而且此證書也需要分發給各個信任他的互聯網主機。

證書主要有以下幾個組件組成(統稱爲PKI):

          證書存取庫:證書申請者或擁有者跟RA、CA、CRL打交道的接口

          RA(證書註冊機構):證書申請者申請證書的機構

          CA(證書頒發機構):給證書申請者簽署證書的機構

          CRL(證書吊銷列表):  證書擁有者的私鑰丟失所要用到的列表       

          PS: PKI全稱爲公鑰基礎設施

         

          早期的互聯網中,大多數的協議都是明文的,使得在互聯網上傳輸的數據非常的不安全。Netscape公司爲了實現數據在互聯網上安全傳輸,就開發了一種實現數據安全傳輸的名叫SSL的協議,常見的SSL協議共有三個版本SSLv1、SSLv2、SSLv3,目前常用的是SSLv3。後來在開源界也有SSL協議的開源實現TLS,有TLSv1的版本,此版本相當於SSLv3。

          TLS(transfer layer security)、SSL(secure socket layer):他們都是一種位於應用層和傳輸層之間的協議,可以基於TCP和UDP,上層應用可以使用該協議對數據進行加密,從而保證傳輸數據的安裝也可以不使用此協議。因此,大多數人都稱ssl是一種位於應用層和傳輸層之間的半層協議。


         如果想使用證書在企業或組織內部進行安全通訊,那麼就需要構建私有CA,頒發證書時也有一個流程 ,具體流程和步驟如下:

              

             openssl作爲CA時用到的目錄或文件:                

    [root@localhost CA]# rpm -ql openssl    
    /etc/pki/CA
    /etc/pki/CA/certs
    /etc/pki/CA/crl
    /etc/pki/CA/newcerts           # 新生成的證書的副本存放位置
    /etc/pki/CA/private
    /etc/pki/tls
    /etc/pki/tls/certs
    /etc/pki/tls/certs/Makefile
    /etc/pki/tls/certs/make-dummy-cert
    /etc/pki/tls/certs/renew-dummy-cert
    /etc/pki/tls/misc
    /etc/pki/tls/misc/CA
    /etc/pki/tls/misc/c_hash
    /etc/pki/tls/misc/c_info
    /etc/pki/tls/misc/c_issuer
    /etc/pki/tls/misc/c_name
    /etc/pki/tls/openssl.cnf       # openssl配置文件信息
    /etc/pki/tls/private           # CA私鑰存放位置

        

                建立私有CA服務器:

               1、給CA自己生成私鑰

    [root@ca private]# openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048
    Generating RSA private key, 2048 bit long modulus
    .................................+++
    .........................................................................................+++
    e is 65537 (0x10001)
    # 此處-out選項後面跟的文件名必須是cakey.pem。


               2、然後提取CA的私鑰中的公鑰給自己頒發一個自簽證書

    [root@ca CA]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem  -out cacert.pem -days 36500
    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) []:BEIJING
    Locality Name (eg, city) [Default City]:BJ
    Organization Name (eg, company) [Default Company Ltd]:movekj.com
    Organizational Unit Name (eg, section) []:tech
    Common Name (eg, your name or your server's hostname) []:ca.movekj.com
    Email Address []:
     # 此處-out選項後面跟的文件名必須是cacert.pem。

        

               3、初始化CA工作環境             

    [root@ca CA]# touch /etc/pki/CA/{index.txt,serial}
    [root@ca CA]# echo 00 > /etc/pki/CA/serial
    [root@ca CA]# cat /etc/pki/CA/serial 
    00

             客戶端申請證書           

               1、給客戶端生成私鑰

    [root@http ~]# openssl genrsa -out ~/http.key 2048
    Generating RSA private key, 2048 bit long modulus
    ...................................................+++
    ....................................................+++
    e is 65537 (0x10001)

 

               2 、在本地生成證書籤署請求

    [root@http ~]# openssl req -new -key ~/http.key -out ~/http.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) []:BEIJING
    Locality Name (eg, city) [Default City]:BJ
    Organization Name (eg, company) [Default Company Ltd]:movekj.com
    Organizational Unit Name (eg, section) []:sell
    Common Name (eg, your name or your server's hostname) []:www.movekj.com
    Email Address []:
    
    Please enter the following 'extra' attributes
    to be sent with your certificate request
    A challenge password []:
    An optional company name []:

             

            3、將證書請求複製到CA服務器上

    [root@http ~]# scp ~/http.csr [email protected]:~/    
    The authenticity of host '10.0.0.200 (10.0.0.200)' can't be established.
    RSA key fingerprint is 2c:de:35:e7:d4:f1:a2:29:82:86:ff:d3:c4:75:11:42.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '10.0.0.200' (RSA) to the list of known hosts.
    [email protected]'s password: 
    http.csr                                                                                                                  100% 1005     1.0KB/s   00:00

                

               4、在CA服務器上給客戶端簽署證書

    [root@ca ~]# openssl ca -in ~/http.csr -out ~/http.crt -days 300
    Using configuration from /etc/pki/tls/openssl.cnf
    Check that the request matches the signature
    Signature ok
    Certificate Details:
            Serial Number: 0 (0x0)
            Validity
                Not Before: Jul  9 02:49:02 2015 GMT
                Not After : May  4 02:49:02 2016 GMT
            Subject:
                countryName               = CN
                stateOrProvinceName       = BEIJING
                organizationName          = movekj.com
                organizationalUnitName    = sell
                commonName                = www.movekj.com
            X509v3 extensions:
                X509v3 Basic Constraints: 
                    CA:FALSE
                Netscape Comment: 
                    OpenSSL Generated Certificate
                X509v3 Subject Key Identifier: 
                    16:91:A6:EA:6B:0D:5C:E8:BC:43:F8:64:52:A8:83:75:33:13:B7:CE
                X509v3 Authority Key Identifier: 
                    keyid:C2:AC:56:05:CD:7F:A0:58:D9:BF:65:9F:37:B4:3B:19:4D:35:21:F2
    
    Certificate is to be certified until May  4 02:49:02 2016 GMT (300 days)
    Sign the certificate? [y/n]:y
    
    
    1 out of 1 certificate requests certified, commit? [y/n]y
    Write out database with 1 new entries
    Data Base Updated

 

               5、將證書傳給客戶端

    [root@ca ~]# scp ~/http.csr [email protected]:~    
    The authenticity of host '10.0.0.150 (10.0.0.150)' can't be established.
    RSA key fingerprint is 5c:ea:49:68:f7:0f:77:d3:4e:82:0d:04:7e:ab:13:01.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '10.0.0.150' (RSA) to the list of known hosts.
    [email protected]'s password: 
    http.csr                                                                                                                  100% 1005     1.0KB/s   00:00

        

         

         有了CA頒發的證書之後,互聯網上的安全通訊過程如下(以https爲例,這個過程也稱之爲SSL、TLS會話的建立過程):

wKiom1Wd8T2D63SSAADOgEl9ws0974.jpg

           由於https是基於tcp/443號端口,因此在SSL會話開始之前有TCP的三次握手過程,而後纔是SSL:

           1、客戶端向服務器請求服務器的證書;

           2、服務器準備證書,客戶端從服務器處下載證書

           3、客戶端用事先下載在本地的CA證書中的公鑰解密從服務器下載下來的證書的數字簽名來獲得證書主體部分的特徵碼(如果能解密,則就能驗證此證書確實是客戶端信任的CA頒發的證書),而後用與同樣的算法提取證書主體部分的特徵碼,並比較兩者是否一致。(如果一致,就能驗證證書是完整的,沒有被人篡改過,後續的通訊過程客戶端就用從服務器上下載下來的證書中的公鑰來進行加密通訊

         




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章