HTTPS基本原理及構(gòu)建CA

一、加密算法

  1. 對(duì)稱算法:有流式、分組兩種,加密和解密都是使用的同一個(gè)密鑰。
  2. 非對(duì)稱加密:使用公私鑰進(jìn)行加密解密,公鑰和算法都是公開的,私鑰是保密的。非對(duì)稱加密算法性能較低,但是安全性超強(qiáng),由于其加密特性,非對(duì)稱加密算法能加密的數(shù)據(jù)長(zhǎng)度也是有限的。
  3. 哈希算法:將任意長(zhǎng)度的信息轉(zhuǎn)換為較短的固定長(zhǎng)度的值,通常其長(zhǎng)度要比信息小得多,且算法不可逆。
  4. 數(shù)字簽名:簽名就是在信息的后面再加上一段內(nèi)容(信息經(jīng)過hash后的值),可以證明信息沒有被修改過。hash值一般都會(huì)加密后(也就是簽名)再和信息一起發(fā)送,以保證這個(gè)hash值不被修改。

二、HTTPS原理

1、HTTP訪問過程

HTTP請(qǐng)求過程中,客戶端與服務(wù)器之間沒有任何身份確認(rèn)的過程,數(shù)據(jù)全部明文傳輸,“裸奔”在互聯(lián)網(wǎng)上,所以很容易遭到黑客的攻擊。
HTTP傳輸面臨的風(fēng)險(xiǎn):

  • 竊聽風(fēng)險(xiǎn):黑客可以獲知通信內(nèi)容。
  • 篡改風(fēng)險(xiǎn):黑客可以修改通信內(nèi)容。
  • 冒充風(fēng)險(xiǎn):黑客可以冒充他人身份參與通信。
2、HTTPS訪問過程

2.1 對(duì)稱加密

image.png

1、優(yōu)點(diǎn)
此方法屬于對(duì)稱加密,對(duì)方擁有相同的密鑰,信息得到安全傳輸。
2、缺點(diǎn)

  • 不同的客戶端、服務(wù)器數(shù)量龐大,所以雙方都需要維護(hù)大量的密鑰,維護(hù)成本很高。
  • 因每個(gè)客戶端、服務(wù)器的安全級(jí)別不同,密鑰極易泄露。

2.2 非對(duì)稱加密

image.png

客戶端用公鑰對(duì)請(qǐng)求內(nèi)容加密,服務(wù)器使用私鑰對(duì)內(nèi)容解密,反之亦然,但上述過程也存在缺點(diǎn):公鑰是公開的(也就是黑客也會(huì)有公鑰),所以第 ④ 步私鑰加密的信息,如果被黑客截獲,其可以使用公鑰進(jìn)行解密,獲取其中的內(nèi)容。

2.3 兩者結(jié)合

image.png

如上圖所示:
(1)第 ③ 步時(shí),客戶端說:(咱們后續(xù)回話采用對(duì)稱加密吧,這是對(duì)稱加密的算法和對(duì)稱密鑰)這段話用公鑰進(jìn)行加密,然后傳給服務(wù)器。
(2)服務(wù)器收到信息后,用私鑰解密,提取出對(duì)稱加密算法和對(duì)稱密鑰后,服務(wù)器說:(好的)對(duì)稱密鑰加密。
(3)后續(xù)兩者之間信息的傳輸就可以使用對(duì)稱加密的方式了。

遇到的問題:
(1)客戶端如何獲得公鑰。
(2)如何確認(rèn)服務(wù)器是真實(shí)的而不是黑客。

2.4 獲取公鑰與確認(rèn)服務(wù)器身份

image.png

1、獲取公鑰
(1)提供一個(gè)下載公鑰的地址,回話前讓客戶端去下載。(缺點(diǎn):下載地址有可能是假的;客戶端每次在回話前都先去下載公鑰也很麻煩)
(2)回話開始時(shí),服務(wù)器把公鑰發(fā)給客戶端(缺點(diǎn):黑客冒充服務(wù)器,發(fā)送給客戶端假的公鑰)

2、那有木有一種方式既可以安全的獲取公鑰,又能防止黑客冒充呢?

那就需要用到終極武器了:SSL 證書(申購(gòu))
image.png

如上圖所示,在第 ② 步時(shí)服務(wù)器發(fā)送了一個(gè)SSL證書給客戶端,SSL 證書中包含的具體內(nèi)容有:

(1)證書的發(fā)布機(jī)構(gòu)CA
(2)證書的有效期
(3)公鑰
(4)證書所有者
(5)簽名
………

3、客戶端收到服務(wù)端發(fā)來的SSL證書時(shí),會(huì)對(duì)證書的真?zhèn)芜M(jìn)行校驗(yàn)。

以瀏覽器說明為例:
(1)首先瀏覽器讀取證書中的證書所有者、有效期等信息進(jìn)行一一校驗(yàn)
(2)瀏覽器開始查找操作系統(tǒng)中已內(nèi)置的受信任的證書發(fā)布機(jī)構(gòu)CA,與服務(wù)器發(fā)來的證書中的頒發(fā)者CA比對(duì),用于校驗(yàn)證書是否為合法機(jī)構(gòu)頒發(fā)
(3)如果找不到,瀏覽器就會(huì)報(bào)錯(cuò),說明服務(wù)器發(fā)來的證書是不可信任的。
(4)如果找到,那么瀏覽器就會(huì)從操作系統(tǒng)中取出 頒發(fā)者CA 的公鑰,然后對(duì)服務(wù)器發(fā)來的證書里面的簽名進(jìn)行解密
(5)瀏覽器使用相同的hash算法計(jì)算出服務(wù)器發(fā)來的證書的hash值,將這個(gè)計(jì)算的hash值與證書中簽名做對(duì)比
(6)對(duì)比結(jié)果一致,則證明服務(wù)器發(fā)來的證書合法,沒有被冒充
(7)此時(shí)瀏覽器就可以讀取證書中的公鑰,用于后續(xù)加密了

2.5 HTTPS傳輸更加安全
(1) 所有信息都是加密傳播,黑客無法竊聽。
(2) 具有校驗(yàn)機(jī)制,一旦被篡改,通信雙方會(huì)立刻發(fā)現(xiàn)。
(3) 配備身份證書,防止身份被冒充。

三、HTTPS總結(jié)

相比 HTTP 協(xié)議,HTTPS 協(xié)議增加了很多握手、加密解密等流程,雖然過程很復(fù)雜,但其可以保證數(shù)據(jù)傳輸?shù)陌踩?/p>

1、HTTPS缺點(diǎn)

  1. SSL 證書費(fèi)用很高,以及其在服務(wù)器上的部署、更新維護(hù)非常繁瑣。
  2. HTTPS 降低用戶訪問速度(多次握手)。
  3. 網(wǎng)站改用HTTPS 以后,由HTTP 跳轉(zhuǎn)到 HTTPS 的方式增加了用戶訪問耗時(shí)(多數(shù)網(wǎng)站采用302跳轉(zhuǎn))。
  4. HTTPS 涉及到的安全算法會(huì)消耗 CPU 資源,需要增加大量機(jī)器(https訪問過程需要加解密)。

四、構(gòu)建私有的CA機(jī)構(gòu)

1、CA介紹

CA(Certificate Authority)證書頒發(fā)機(jī)構(gòu)主要負(fù)責(zé)證書的頒發(fā)、管理以及歸檔和吊銷。證書內(nèi)包含了擁有證書者的姓名、地址、電子郵件帳號(hào)、公鑰、證書有效期、發(fā)放證書的CA、CA的數(shù)字簽名等信息。
證書主要有三大功能:加密、簽名、身份驗(yàn)證。

2、構(gòu)建私有CA

2.1 檢查安裝openssl

[root@https-ca ~]# rpm -qa openssl
如若沒有安裝
[root@https-ca ~]# yum install openssl openssl-devel

2.2 查看配置文件

[root@https-ca ~]# vim /etc/pki/tls/openssl.cnf
[ ca ]
default_ca           = CA_default        # 默認(rèn)的CA配置;CA_default指向下面配置塊

[ CA_default ]
dir                  = /etc/pki/CA           # CA的默認(rèn)工作目錄
certs                = $dir/certs            # 認(rèn)證證書的目錄
crl_dir              = $dir/crl              # 證書吊銷列表的路徑
database             = $dir/index.txt        # 數(shù)據(jù)庫(kù)的索引文件
new_certs_dir   = $dir/newcerts         # 新頒發(fā)證書的默認(rèn)路徑
certificate     = $dir/cacert.pem       # 此服務(wù)認(rèn)證證書,如果此服務(wù)器為根CA那么這里為自頒發(fā)證書
serial          = $dir/serial           # 下一個(gè)證書的證書編號(hào)
crlnumber       = $dir/crlnumber        # 下一個(gè)吊銷的證書編號(hào)                                      
crl             = $dir/crl.pem          # The current CRL
private_key     = $dir/private/cakey.pem# CA的私鑰
RANDFILE        = $dir/private/.rand    # 隨機(jī)數(shù)文件
x509_extensions = usr_cert              # The extentions to add to the cert
name_opt        = ca_default            # 命名方式,以ca_default定義為準(zhǔn)
cert_opt        = ca_default            # 證書參數(shù),以ca_default定義為準(zhǔn)
default_days    = 365                   # 證書默認(rèn)有效期
default_crl_days= 30                    # CRl的有效期
default_md      = sha256                # 加密算法
preserve        = no                    # keep passed DN ordering
policy          = policy_match          #policy_match策略生效
# For the CA policy
[ policy_match ]
countryName             = match         #國(guó)家;match表示申請(qǐng)者的申請(qǐng)信息必須與此一致
stateOrProvinceName     = match         #州、省
organizationName        = match         #組織名、公司名
organizationalUnitName  = optional      #部門名稱;optional表示申請(qǐng)者可以的信息與此可以不一致
commonName              = supplied
emailAddress            = optional
# For the 'anything' policy
# At this point in time, you must list all acceptable 'object'
# types.
[ policy_anything ]                     #由于定義了policy_match策略生效,所以此策略暫未生效
countryName             = optional
stateOrProvinceName     = optional
localityName            = optional
organizationName        = optional
organizationalUnitName  = optional
commonName              = supplied
emailAddress            = optional

2.3 根證書服務(wù)器目錄

1、根CA服務(wù)器:因?yàn)橹挥?CA 服務(wù)器的角色,所以用到的目錄只有/etc/pki/CA
2、網(wǎng)站服務(wù)器:只是證書申請(qǐng)者的角色,所以用到的目錄只有/etc/pki/tls

2.4 創(chuàng)建所需文件

[root@https-ca ~]# cd /etc/pki/CA/
[root@https-ca CA]# ls
certs  crl  newcerts  private
[root@https-ca CA]# touch index.txt   #創(chuàng)建生成證書索引數(shù)據(jù)庫(kù)文件
[root@https-ca CA]# ls
certs  crl  index.txt  newcerts  private
[root@https-ca CA]# echo 01 > serial   #指定第一個(gè)頒發(fā)證書的序列號(hào)
[root@https-ca CA]# ls
certs  crl  index.txt  newcerts  private  serial

2.5 創(chuàng)建密鑰

在根CA服務(wù)器上創(chuàng)建密鑰,密鑰的位置必須為/etc/pki/CA/private/cakey.pem,這個(gè)是openssl.cnf中指定的路徑,只要與配置文件中指定的匹配即可。

[root@https-ca CA]# (umask 066; openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
...........+++
...............+++
e is 65537 (0x10001)

2.6 生成自簽名證書

根CA自簽名證書,根CA是最頂級(jí)的認(rèn)證機(jī)構(gòu),沒有人能夠認(rèn)證他,所以只能自己認(rèn)證自己生成自簽名證書。

[root@https-ca ~]# cd /etc/pki/CA/
[root@https-ca CA]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 7300 -out /etc/pki/CA/cacert.pem -days 7300
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]:BEIJING
Organization Name (eg, company) [Default Company Ltd]:CA
Organizational Unit Name (eg, section) []:OPT
Common Name (eg, your name or your server's hostname) []:ca.qf.com
Email Address []:
[root@https-ca CA]# ls
cacert.pem  certs  crl  index.txt  newcerts  private  serial

-new: 生成新證書簽署請(qǐng)求
-x509: 專用于CA生成自簽證書
-key: 生成請(qǐng)求時(shí)用到的私鑰文件
-days n: 證書的有效期限
-out /PATH/TO/SOMECERTFILE: 證書的保存路徑

2.7 下載安裝證書

/etc/pki/CA/cacert.pem就是生成的自簽名證書文件,使用 SZ/xftp工具將他導(dǎo)出到窗口機(jī)器中。然后雙擊安裝此證書到受信任的根證書頒發(fā)機(jī)構(gòu)。

[root@https-ca CA]# yum install -y lrzsz
[root@https-ca CA]# sz cacert.pem
image.png
image.png
image.png
image.png
3、客戶端CA證書申請(qǐng)及簽名

3.1 檢查安裝openssl

[root@nginx-server ~]# rpm -qa openssl
如果未安裝,安裝 openssl
[root@nginx-server ~]# yum install openssl openssl-devel

3.2 客戶端生成私鑰文件

[root@nginx-server ~]# (umask 066; openssl genrsa -out /etc/pki/tls/private/www.llf.com.key 2048)
Generating RSA private key, 2048 bit long modulus
..............................+++
..........+++
e is 65537 (0x10001)
[root@nginx-server ~]# cd /etc/pki/tls/private/
[root@nginx-server private]# ls
www.llf.com.key

3.3 客戶端私鑰加密生成證書請(qǐng)求

[root@nginx-server private]# ls ../
cert.pem  certs  misc  openssl.cnf  private
[root@nginx-server private]# openssl req -new -key /etc/pki/tls/private/www.llf.com.key -days 365 -out /etc/pki/tls/www.llf.com.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]:BEIJING
Organization Name (eg, company) [Default Company Ltd]:QF
Organizational Unit Name (eg, section) []:OPT
Common Name (eg, your name or your server's hostname) []:www.llf.com
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@nginx-server private]# ls ../
cert.pem  certs  misc  openssl.cnf  private  www.llf.com.csr

csr包含了公鑰和名字信息。通常以.csr為后綴,是網(wǎng)站向CA發(fā)起認(rèn)證請(qǐng)求的文件,是中間文件。
最后把生成的請(qǐng)求文件/etc/pki/tls/www.llf.com.csr傳輸給CA,這里使用scp命令,通過ssh協(xié)議將文件傳輸?shù)紺A下的/etc/pki/CA/private/目錄。

[root@nginx-server ~]# cd /etc/pki/tls/
[root@nginx-server tls]# scp www.llf.com.csr 10.0.105.181:/etc/pki/CA/private
root@10.0.105.181's password: 
www.llf.com.csr                                                           100%  997   331.9KB/s   00:00 

3.4 CA簽署證書

[root@https-ca ~]# openssl ca -in /etc/pki/CA/private/www.llf.com.csr -out /etc/pki/CA/certs/www.llf.com.ctr -days 365
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: Jul  3 10:12:23 2019 GMT
            Not After : Jul  2 10:12:23 2020 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = BEIJING
            organizationName          = QF
            organizationalUnitName    = OPT
            commonName                = www.qf.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                E3:AC:1A:55:2B:28:B9:80:DC:9C:C2:13:70:53:27:AD:3D:44:8F:D3
            X509v3 Authority Key Identifier: 
                keyid:5D:2A:81:B2:E7:8D:D8:88:E5:7B:94:CA:75:65:9C:82:2B:A9:B2:3C

Certificate is to be certified until Jul  2 10:12:23 2020 GMT (365 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

證書通常以.crt為后綴,表示證書文件。

3.5 查看生成的證書的信息

[root@https-ca ~]# openssl x509 -in /etc/pki/CA/certs/www.llf.com.ctr -noout -subject
subject= /C=CN/ST=BEIJING/O=QF/OU=OPT/CN=www.llf.com

3.6 將生成的證書發(fā)放給請(qǐng)求客戶端

[root@https-ca ~]# cd /etc/pki/CA/certs/
[root@https-ca certs]# scp www.llf.com.ctr 10.0.105.199:/etc/pki/CA/certs/
root@10.0.105.199's password: 
www.llf.com.ctr                                                           100% 4422   998.3KB/s   00:00 
4、CA吊銷證書

4.1 知道客戶端吊銷的證書serisal

[root@https-ca ~]# openssl x509 -in /etc/pki/tls/cert.pem  -noout -serial -subject
serial=5EC3B7A6437FA4E0
subject= /CN=ACCVRAIZ1/OU=PKIACCV/O=ACCV/C=ES

4.2 吊銷證書

先根據(jù)客戶提交的serial與subject信息,對(duì)比檢驗(yàn)是否與index.txt文件中的信息一致.

[root@https-ca ~]# openssl ca -revoke /etc/pki/CA/newcerts/01.pem

4.3 生成吊銷證書的編號(hào)
第一次吊銷一個(gè)證書才需要執(zhí)行

[root@https-ca ~]# echo 01 > /etc/pki/CA/crlnumber

4.4 更新證書吊銷列表

[root@https-ca ~]# openssl ca -gencrl -out thisca.crl

4.5 查看證書吊銷列表

[root@https-ca ~]# openssl crl -in /root/thisca.crl -noout -text
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容