加密、解密,以及OpenSSL建立私有CA


一、常見算法


常見的加密算法和協議有對稱加密,非對稱加密,單向加密

  1對稱加密:加密和解密使用一個密鑰;依賴於算法和密鑰,算法是可以公開的,密鑰是不能公共的,因爲加密是依賴於密鑰的。安全性依賴於密鑰,而非算法;

  常見算法:

  DESData Encryption Standard56bits)、3DESAESAdvanced EncryptionStandard)、BlowfishTowfiskIDEARC6CASTS

  特性:

    1、加密、解密使用同一密鑰

    2、將明文分隔成固定大小的塊,逐個進行加密;

 缺陷:

   1、密鑰過多;

   2、密鑰分發;

 2、非對稱加密:公鑰加密

   密碼對兒:私鑰(secret key)和公鑰(public key);私鑰僅允許個人使用;公鑰是公開給所有獲取;

   公鑰和私鑰不是在創建時就成對出現的,而公鑰是從私鑰中提取出而來;創建時只需要創建私鑰,從私鑰中使用某種工具,把公鑰從私鑰中提取出來就可以了。

  工作特性:使用公鑰加密的數據,只能使用與此公鑰配對兒的私鑰解密,反之亦然。

  用處

   1、身份認證:私鑰擁有者用自己的私鑰加密的數據,只要用其公鑰解密,即可認證其身份;私鑰加密不能保證數據的安全性,只有用於身份認證;

   2、密碼交換:被通信方通信之前,首先獲取到對方的公鑰,自己生成一個加密密碼(對稱加密密碼),用對方的公鑰加密,併發送給對方;

   3、數據加密:

   爲什麼不用非對稱加密來加密數據呢?因爲非對稱加密數據比起對稱加密要慢上10^3倍,都是用對稱加密來加密數據,在用非對稱加密來加密對稱加密的密鑰。

  常用算法:

   RSADSAELGamal

  特性

    1、密鑰長度較大,例如512bits20484096bits

    2、加密解密分別使用密鑰對兒中的密鑰相對進行;

    3、常用於數字簽名和密鑰交換;

  3、單向加密

   提出數據的特徵碼;每一個數據的都一個唯一的特性碼,嚴重的依賴於算法。

 特性

    1、定長輸出:無論原來的數據是多大級別,其加密結果長度一樣;

    2、雪崩效應;原始數據的微小改變,將會導致結果巨大變化;

    3、不可逆;

 算法

    MD5SHA1SHA256SHA384SHA512

   單向加密已經被運行到CentOS操作系統中的用戶名密碼加密/etc/shadow文件中,其中CentOS 5運行的MD5CentOS 6運行的SHA512

  用處:數據完整性

 

二、加密解密通信過程

 

wKioL1U66TGDFZnqABmcmAwfebA308.bmp

wKiom1U65_iTyAyzAAHfSNTP9RQ300.bmp

三、數字證書的格式(x.509 v3

wKiom1U66MqhBxhpAAZYcChRhFA650.bmp

   版本號(version

   序列號(serial number):是一個整數,CA使用唯一標識此證書的號碼;

   簽名算法標誌(Signature algorithmidentifier):它是一個帶參數的,用於給證書籤名本身說明,主要是讓用戶驗證證書是可靠的

   發行者的名稱:即CA自己的名稱

   有效期:兩個日期,起始日期和終止日期;

   證書主體名稱:證書擁有者的自己名字

   證書主體公鑰信息:證書擁有者自己的公鑰

   發行商的唯一標識:(可選)

   證書主體的唯一標識:(可選)證書擁有者自己的數字標識

   擴展信息:

   簽名:CA對此證書的數字簽名,用於驗證證書是自己的簽發的


四、https

wKioL1U66pqSJn3BAAlJdID6uc8337.bmp

  客戶端和服務端進行通信時要進行三次握手,ssl也是基於TCP來實現的,在進行三次握手之後客戶端向服務端要證書,服務端把證書給客戶端,此時客戶端拿到證書進行驗證,先用本地存的根證書找看哪個CA給證書籤的證,於是在本地找CA的證書,如果本地有說明我們信任此證書,接着用CA去解密證書中的數字簽名,能解密說明此證書是CA發的,解密的結果是特徵碼,然後在用同樣的單向加密算法去計算證書的特徵碼,並比較,比較之後發現結果相同,說明證書可靠,否則證書不可靠;在驗證訪問的域名(FQDN)必須與證書中的主體保持一致,否則客戶端會認爲拿的不是服務端的證書,在驗證有效期等

  如果以上都是合法的,在從證書中提取對方的公鑰信息,並且雙方開始建立協商,雙方使用哪種對稱加密算法,使用哪種單向加密算法,使用哪種公鑰加密算法;而後客戶端生成一個臨時加密密鑰,並使用對方的公鑰加密之後發送給服務端,於是服務器就已經拿到客戶端訪問請求的頁面,把頁面使用對稱加密後發送給客戶端,客戶端擁有私鑰解密就能在瀏覽器中顯示;

wKioL1U66tywj-BvAA35GCtF1-A833.bmp

  第一步:驗證證書主體、有效期、有沒有在證書吊銷列表中等;

  第二步:從證書中提取對方的公鑰信息;

  第三步:生成對稱加密的密鑰,使用對方的公鑰加密密鑰,傳送給服務端;

  第四步:服務端使用私鑰解密密文;

  第五步:使用對稱加密密鑰加密客戶端請求頁面,發送給客戶端;

  第六步:客戶端使用對稱加密密鑰解密請求頁面,並在瀏覽器顯示頁面;

 

五、使用OpenSSL構建私有CA

  CA和申請證書都在同一臺機器上進行的。

1CA端操作流程

操作步驟:

    1、生成私鑰文件;       

    2、生成自簽署證書;          

    3、相關輔助文件

        touch/etc/pki/CA/index.txt

        echo01 > /etc/pki/CA/serial

再次說明

  1)私鑰用於簽發證書時,向證書添加數字簽名使用;

  2)證書:每個通信方都導入此證書至“受信任的證書頒發機構”;

1)、配置文件

   /etc/pki/tls/openssl.cnf,此文件格式爲INI格式的

………省略……………
####################################################################
[ ca ]
default_ca      =CA_default        # The default casection
 
####################################################################
[ CA_default ]  #定義Openssl做爲CA時自己的相關路徑
 
dir             =/etc/pki/CA # Where everythingis kept  #指明CA自己的工作目錄
certs           =$dir/certs   # Where the issuedcerts are kept #已頒發證書的存取位置
crl_dir         =$dir/crl     # Where the issuedcrl are kept #已吊銷證書的吊銷鏈表存取位置
database        =$dir/index.txt  # database indexfile.  #證書索引文件
#unique_subject = no           # Set to 'no' to allowcreation of
                     #several ctificates with same subject.
new_certs_dir   =$dir/newcerts     # default place fornew certs.  #新證書的存放位置
    
certificate     =$dir/cacert.pem   # The CAcertificate    #指CA自己證書文件名
serial          =$dir/serial   # The currentserial number #序列號,下一個要發的證書的序列號,每籤一個序列號會怎麼加1
crlnumber       =$dir/crlnumber   # the current crlnumber   #吊銷證書列表的編號
                  # mustbe commented out to leave a V1 CRL
crl             =$dir/crl.pem # The current CRL   #當前正在使用的吊銷鏈
private_key     =$dir/private/cakey.pem# The private key   #CA自己的私鑰文件
RANDFILE        =$dir/private/.rand # private randomnumber file #隨機數的獲取位置
 
x509_extensions = usr_cert        # The extentions to add to thecert #定義了x.509的擴展信息中記錄爲“用戶證書”
 
…………省略……………………



2)、工作目錄

   /etc/pki/CA/

  注意:CentOS要想構建CA必須是基於/etc/pki/CA目錄進行

[root@node-2 ~]# cd /etc/pki/CA
[root@node-2 CA]# ll
total 16
drwxr-xr-x  2 root root4096 Oct 15  2014 certs         #證書文件存放位置,
drwxr-xr-x  2 root root4096 Oct 15  2014 crl          #證書吊銷列表存取文件
drwxr-xr-x. 2 root root 4096 Oct 15  2014 newcerts       #新創建證書存取位置
drwx------. 2 root root 4096 Oct 15  2014 private        #CA自己私鑰存儲位置


3)、創建CA自己的私鑰

[root@node-2 CA]# pwd
/etc/pki/CA
[root@node-2 CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit longmodulus
........................................................+++
..............+++
e is 65537 (0x10001)
[root@node-2 CA]# ll ./private/
total 4
-rw------- 1 root root 1675 Apr 22 22:23cakey.pem

 

4)、生成一個自簽署證書

  此自簽署證書在/etc/pki/tls/openssl.cnf文件中定義必須放在CA目錄下叫cacert.pem;從私鑰中提取公鑰,並構建成證書籤署請求,然後讓CA給簽署;

格式:openssl req -new -x509-key  /etc/pki/CA/private/cakey.pem -out/etc/pki/CA/cacert.pem -days #

       -x509:表示自簽署證書,不加表示證書籤署請求;

       -key:從那個私鑰中自動提取出公鑰,並創建一個證書籤署請求;

       -days:指明給自己簽署證書的有效期

[root@node-2 CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days3360
You are about to be asked to enterinformation that will be incorporated
into your certificate request.
What you are about to enter is what is calleda Distinguished Name or a DN.
There are quite a few fields but you canleave some blank
For some fields there will be a defaultvalue,
If you enter '.', the field will be leftblank.
-----
Country Name (2 letter code) [XX]:CN            #國家代碼,必須使用兩位數字來描述
State or Province Name (full name) []:HA          #省名或州名
Locality Name (eg, city) [Default City]:ZZ          #城市名
Organization Name (eg, company) [DefaultCompany Ltd]:MagEdu  #組織名或公司名
Organizational Unit Name (eg, section)[]:Ops     #部門
Common Name (eg, your name or your server'shostname) []:www.magedu.com  #此項很重要,證書主題自己的名字,別人和你通信是就使用此名字來聯繫你,而後你證書中的名字和通信時所使用名字必須要保持一致;否則就認爲證書驗證無法通過;
Email Address []:[email protected]                     #主機的聯繫人
[root@node-2 CA]# ll
total 20
-rw-r--r--  1 root root 1395 Apr 22 22:40 cacert.pem
drwxr-xr-x  2 root root4096 Oct 15  2014 certs
drwxr-xr-x  2 root root4096 Oct 15  2014 crl
drwxr-xr-x. 2 root root 4096 Oct 15  2014 newcerts
drwx------. 2 root root 4096 Apr 22 22:23 private

注意:構建Ca是給通信方簽發證書的


5)、生成相關的輔助文件

[root@node-2 ~]# cd /etc/pki/CA/
[root@node-2 CA]# ls
cacert.pem certs  crl  newcerts private
[root@node-2 CA]# touch index.txt
[root@node-2 CA]# echo 01 > serial
[root@node-2 CA]# cat serial
01

 

2、給節點發證書

操作步驟:

    1、節點申請證書

     在證書申請的主機上進行如下步驟;

       1)生成私鑰

       2)生成證書籤署請求;                

       3)把請求發送給CA

    2CA簽發證書

      1)驗證請求者信息

      2)簽署證書

     3)把簽署好的證書發還給請求者

1)、以http目錄爲示例,本機給本地簽署證書:

[root@node-2 ~]# cd /etc/httpd/
[root@node-2 httpd]# mkdir ssl
[root@node-2 httpd]# cd ssl/

2)、生成節點私鑰:

[root@node-2 ssl]# (umask 077; openssl genrsa -out httpd.key 1024)
Generating RSA private key, 1024 bit longmodulus
....................++++++
......++++++
e is 65537 (0x10001)
[root@node-2 ssl]# ll
total 4
-rw------- 1 root root 891 Apr 23 08:19httpd.key

 

3)、生成簽署證書請求:

[root@node-2 ssl]# openssl  req -new -key httpd.key-out httpd.csr

You are about to be asked to enterinformation that will be incorporated

into your certificate request.

What you are about to enter is what is calleda Distinguished Name or a DN.

There are quite a few fields but you canleave some blank

For some fields there will be a defaultvalue,

If you enter '.', the field will be leftblank.

-----

Country Name (2 letter code) [XX]:CN

State or Province Name (full name) []:HA

Locality Name (eg, city) [Default City]:ZZ

Organization Name (eg, company) [DefaultCompany Ltd]:MagEdu

Organizational Unit Name (eg, section)[]:Ops  

Common Name (eg, your name or your server'shostname) []:www.zh.com   #此名稱要和證書申請者的FQDN的保持一致

Email Address []:[email protected]

 

Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge password []:

An optional company name []:

[root@node-2 ssl]# ls

httpd.csr httpd.key

#提示:以上的國家、省名、城市名要和CA的保持一致

3CA簽署證書

[root@node-2 ssl]# openssl ca -in /etc/httpd/ssl/httpd.csr -out /etc/httpd/ssl/httpd.crt-days 3360
Using configuration from/etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1(0x1)
        Validity
            Not Before:Apr 23 00:30:38 2015 GMT
            Not After :Jul  4 00:30:38 2024 GMT
        Subject:
           countryName           = CN
           stateOrProvinceName       = HA
           organizationName         = MagEdu
           organizationalUnitName       = Ops
           commonName            =www.zh.com
           emailAddress           [email protected]
        X509v3 extensions:
            X509v3 BasicConstraints: 
                CA:FALSE
            NetscapeComment: 
                OpenSSLGenerated Certificate
            X509v3 SubjectKey Identifier: 
               C9:1A:C2:19:08:28:FF:1F:B4:85:90:32:00:E1:5B:E8:FF:6E:7E:95
            X509v3Authority Key Identifier: 
               keyid:66:1E:36:DB:E7:40:30:ED:B1:04:24:86:11:08:73:0C:DF:5E:DD:52
 
Certificate is to be certified until Jul  4 00:30:38 2024 GMT (3360 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

 

操作過程遇到以下問題1

[root@node-2 ssl]# openssl ca -in/etc/httpd/ssl/httpd.csr -out /etc/httpd/ssl/httpd.crt -days 3360

Using configuration from /etc/pki/tls/openssl.cnf

/etc/pki/CA/index.txt:No such file or directory   #沒有/etc/pki/CA/index.txt文件

unable to open '/etc/pki/CA/index.txt'

139960522676040:error:02001002:systemlibrary:fopen:No such file or directory:bss_file.c:398:fopen('/etc/pki/CA/index.txt','r')

139960522676040:error:20074002:BIOroutines:FILE_CTRL:system lib:bss_file.c:400:

操作過程遇到以下問題2

[root@node-2 CA]# openssl ca -in/etc/httpd/ssl/httpd.csr -out /etc/httpd/ssl/httpd.crt -days 3360

Using configuration from/etc/pki/tls/openssl.cnf

/etc/pki/CA/serial:No such file or directory      #沒有/etc/pki/CA/serial文件

error while loading serial number

140170056320840:error:02001002:systemlibrary:fopen:No such file ordirectory:bss_file.c:398:fopen('/etc/pki/CA/serial','r')

140170056320840:error:20074002:BIOroutines:FILE_CTRL:system lib:bss_file.c:400:

4、查看節點所生成的文件:

[root@node-2 ~]# cd /etc/httpd/ssl/
[root@node-2 ssl]# ll
total 8
-rw-r--r-- 1 root root   0 Apr 23 19:47 httpd.crt       #證書文件
-rw-r--r-- 1 root root 676 Apr 23 08:22  httpd.csr       #證書請求文件
-rw------- 1 root root 891 Apr 23 08:19  httpd.key       #私鑰文件


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