使用Openssl構建私有CA

使用Openssl構建私有CA

Openssl是SSL的開源實現,是一種安全機密程序,主要用於提高遠程登錄訪問的安全性。也是目前加密算法所使用的工具之一,功能很強大。

    Openssl爲網絡通信提供安全及數據完整性的一種安全協議,包括了主要的密碼算法、常用的密鑰和證書封裝管理功能(CA)以及SSL協議,並提供了豐富的應用程序供測試或其它目的使用,例如我們將會使用Openssl實現私有CA,並實現證書頒發。

OpenSSL:SSL的開源實現  

     libcrypto:通用加密庫,提供了各種加密函數  

     libssl:TLS/SSL協議的實現,基於會話的、實現了身份認證、數據機密性和會話完整性的TLS/SSL庫  

     openssl:多用途的命令行工具;能夠實現私有證書頒發機構;即在公司內部實現身份的驗證; 

     SSL:(Secure Socket Layer)安全套接字層,通過一種機制在互聯網上提供密鑰傳輸。其主要目標是保證兩個應用間通信數據的保密性和可靠性,可在服務器端和用戶端同時支持的一種加密算法。目前主流版本SSLV2、SSLV3

一、SSL提供以下功能:

數據的機密性:通過對稱加密算法實現數據的機密性。  

數據的完整性:通過單向加密算法保證數據的完整性。  

身份的安全驗證:提供數據發送者的身份。


二、加密類型及功能:

單向加密:提取數據特徵碼,實現數據完整性驗證

對稱加密:數據加密,實現數據私密性

公鑰加密:使用對方公鑰加密,實現祕鑰交換;使用自己私鑰加密,實現身份驗證

三、SSL 會話流程

https會話.jpg   ssl.jpg


1.客戶端向服務端發起SSL會話信息,將自己支持的一套加密規則發送給網站。 

2.服務端從中選出一組加密算法與HASH算法與瀏覽器進行協商確認,並將自己的身份信息CA證書的發送給瀏覽器。

證書信息如圖所示

x509.jpg

3.驗證證書的合法性(頒發證書的機構是否合法,證書是否完整有無篡改、證書是否在CA吊銷庫中、證書中包含的網站地址是否與正在訪問的地址一致等);

4.如果證書不受信任,瀏覽器會彈出警告信息;等待操作人員確認是否繼續,繼續極爲信任證書執行4步驟,如不信任終止會話;

5.如果證書受信任,或者是用戶接受了不受信的證書,瀏覽器會生成一串隨機數的密碼(這串密碼將作爲後續對稱加密通訊的初始祕鑰使用);

6.使用協商好的HASH算法對隨機祕鑰進行單向加密計算出指紋信息,並使用客戶端私鑰對指紋信息進行加密,使用服務端公鑰信息對祕鑰和加密的指紋信息加密後發送給服務端;

7.服務端使用自己的私鑰將密文解開後獲取祕鑰,使用客戶端公鑰解密獲得祕鑰指紋信息並作對比並驗證客戶端身份;

8.之後所有的通信數據將由之前瀏覽器生成的隨機密碼並利用對稱加密算法進行加密,並按照協商定期更換祕鑰。

CA證書是一個加密會話建立信任的初始階段,互聯網上的知名網站的證書肯定是要想CA認證機構申請的,費用也是相當可觀的。而公司內部一些建立於CA之上的通訊完成可以通過Openssl來建立私有的CA服務。

四、使用Openssl建立私有CA

openssl 配置文件 /etc/pki/tls/openssl.cnf

dir             /etc/pki/CA               # Where everything is kept 
certs           = $dir/certs                # Where the issued certs are kept
crl_dir         = $dir/crl                  # Where the issued crl are kept
database        = $dir/index.txt            # database index file.
#unique_subject = no                        # Set to 'no' to allow creation of
                                            # several ctificates with same subject.
new_certs_dir   = $dir/newcerts             # default place for new certs.
 
certificate     = $dir/cacert.pem           # The CA certificate
serial          = $dir/serial               # The current serial number
crlnumber       = $dir/crlnumber            # the current crl number
                                            # must be commented out to leave a V1 CRL
crl             = $dir/crl.pem              # The current CRL
private_key     = $dir/private/cakey.pem    # The private key
RANDFILE        = $dir/private/.rand        # private random number file

  4.1初始化服務器工作環境    

[root@localhost ~]# touch /etc/pki/CA/{index.txit,serial,crlnumber} #創建證書發送數據庫、證書序列號、吊銷證書數字文件
[root@localhost ~]# echo 100001 > /etc/pki/CA/serial        #建立發放證書初始序列號
[root@localhost ~]# echo 000001 > /etc/pki/CA/crlnumber                 #鍵入吊銷證書初始序列號

4.2 創建祕鑰對及自認證證書

[root@localhost ~]# (umask 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)   #創建私鑰;(umask 077;)改變umask並只對此子shell有效括號內的命令之行結束後即失效
Generating RSA private key, 2048 bit long modulus
...............+++
.........................................................................................+++
e is 65537 (0x10001)
[root@localhost ~]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 365
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) []:shandong                        #定義證書所屬省份(州)
Locality Name (eg, city) [Default City]:dezhou                        #定義證書所屬城市
Organization Name (eg, company) [Default Company Ltd]:eway            #定義證書所屬組織、公司
Organizational Unit Name (eg, section) []:support                     #定義證書所屬部門
Common Name (eg, your name or your server's hostname) []:ca.eway.com  #定義使用此證書的主機名稱
Email Address []:[email protected]                                   #定義管理員郵箱
 
 
###################################################
命令參數
req: 生成證書籤署請求
     -news: 新請求
     -key /path/to/keyfile: 指定私鑰文件
     -out /path/to/somefile: 指定生成證書位置
     -x509: 生成自簽署證書
     -days n: 有效天數

4.3 證書申請節點生成私鑰及簽名請求

#####生成私鑰
[root@localhost ~]# (umask 077; openssl genrsa -out /root/cekay.pam 2048)
Generating RSA private key, 2048 bit long modulus
............................................................+++
..............+++
e is 65537 (0x10001)
#####生成證書籤名請求
[root@localhost ~]# openssl req -new -key /root/cekay.pam -out /root/cakey.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                        #需要跟CA服務器保持一致             
State or Province Name (full name) []:shandong              #需要跟CA服務器保持一致
Locality Name (eg, city) [Default City]:dezhou              #需要跟CA服務器保持一致
Organization Name (eg, company) [Default Company Ltd]:eway  #需要跟CA服務器保持一致
Organizational Unit Name (eg, section) []:www
Common Name (eg, your name or your server's hostname) []:    
Email Address []:[email protected]
 
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:                                   #證書祕鑰(可以不加密碼)
An optional company name []:                               #可選的公司名稱
#####將證書籤名請求發送給CA服務器
[root@localhost ~]# scp /root/cakey.csr [email protected]:/home/mylinux
[email protected]'s password: 
cakey.csr                                          100% 1050     1.0KB/s   00:00

   4.4 CA服務器對此證書請求進行簽名並返回給此服務器

[root@localhost ~]# openssl ca -in /home/mylinux/cakey.csr -out /home/mylinux/cakey.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1048577 (0x100001)
        Validity
            Not Before: Oct  5 21:21:14 2015 GMT
            Not After : Oct  4 21:21:14 2016 GMT
        Subject:
            countryName               = cn
            stateOrProvinceName       = shandong
            organizationName          = eway
            organizationalUnitName    = www
            commonName                = www.eway.com
            emailAddress              = [email protected]
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                73:0E:C3:34:70:BE:AF:E4:4F:B6:86:5E:94:33:29:E8:84:8F:A6:2D
            X509v3 Authority Key Identifier: 
                keyid:B1:46:C8:1F:7F:05:63:B5:B5:6E:36:A9:CF:D4:5C:A2:B7:DF:D7:91
 
Certificate is to be certified until Oct  4 21:21:14 2016 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
 
#####將證書遠程複製給客戶機
[root@localhost ~]# scp /home/mylinux/cakey.crt [email protected]:/root/
[email protected]'s password: 
cakey.crt                                      100% 4620     4.5KB/s   00:00

吊銷證書

如果你的私鑰泄露了,那麼證書就不能使用了,需要去CA服務器申請吊銷此證書

#####獲取你證書的序列號和相關信息
[root@localhost ~]# openssl x509 -in /root/cakey.crt -noout  -serial  -subject
serial=100001
subject= /C=cn/ST=shandong/O=eway/OU=www/CN=
#####將此信息及相關的***明信息發送給CA服務管理員,管理員在覈對信息無誤後對證書進項吊銷操作。隨後你需要重新申請證書。
 
#####CA服務吊銷證書操作
[root@localhost ~]# openssl ca -revoke /etc/pki/CA/newcerts/100001.pem     #證書吊銷是以序列名稱來實現的
Using configuration from /etc/pki/tls/openssl.cnf
Revoking Certificate 100001.
Data Base Updated
[root@localhost ~]# openssl ca -gencrl -out /etc/pki/CA/crl/ca.crl         #更新證書吊銷列表
Using configuration from /etc/pki/tls/openssl.cnf

j_0028.gif國慶假期快樂~!

Openssl是SSL的開源實現,是一種安全機密程序,主要用於提高遠程登錄訪問的安全性。也是目前加密算法所使用的工具之一,功能很強大。

    Openssl爲網絡通信提供安全及數據完整性的一種安全協議,包括了主要的密碼算法、常用的密鑰和證書封裝管理功能(CA)以及SSL協議,並提供了豐富的應用程序供測試或其它目的使用,例如我們將會使用Openssl實現私有CA,並實現證書頒發。

OpenSSL:SSL的開源實現  

     libcrypto:通用加密庫,提供了各種加密函數  

     libssl:TLS/SSL協議的實現,基於會話的、實現了身份認證、數據機密性和會話完整性的TLS/SSL庫  

     openssl:多用途的命令行工具;能夠實現私有證書頒發機構;即在公司內部實現身份的驗證; 

     SSL:(Secure Socket Layer)安全套接字層,通過一種機制在互聯網上提供密鑰傳輸。其主要目標是保證兩個應用間通信數據的保密性和可靠性,可在服務器端和用戶端同時支持的一種加密算法。目前主流版本SSLV2、SSLV3

一、SSL提供以下功能:

數據的機密性:通過對稱加密算法實現數據的機密性。  

數據的完整性:通過單向加密算法保證數據的完整性。  

身份的安全驗證:提供數據發送者的身份。


二、加密類型及功能:

單向加密:提取數據特徵碼,實現數據完整性驗證

對稱加密:數據加密,實現數據私密性

公鑰加密:使用對方公鑰加密,實現祕鑰交換;使用自己私鑰加密,實現身份驗證

三、SSL 會話流程

https會話.jpg   ssl.jpg


1.客戶端向服務端發起SSL會話信息,將自己支持的一套加密規則發送給網站。 

2.服務端從中選出一組加密算法與HASH算法與瀏覽器進行協商確認,並將自己的身份信息CA證書的發送給瀏覽器。

證書信息如圖所示

x509.jpg

3.驗證證書的合法性(頒發證書的機構是否合法,證書是否完整有無篡改、證書是否在CA吊銷庫中、證書中包含的網站地址是否與正在訪問的地址一致等);

4.如果證書不受信任,瀏覽器會彈出警告信息;等待操作人員確認是否繼續,繼續極爲信任證書執行4步驟,如不信任終止會話;

5.如果證書受信任,或者是用戶接受了不受信的證書,瀏覽器會生成一串隨機數的密碼(這串密碼將作爲後續對稱加密通訊的初始祕鑰使用);

6.使用協商好的HASH算法對隨機祕鑰進行單向加密計算出指紋信息,並使用客戶端私鑰對指紋信息進行加密,使用服務端公鑰信息對祕鑰和加密的指紋信息加密後發送給服務端;

7.服務端使用自己的私鑰將密文解開後獲取祕鑰,使用客戶端公鑰解密獲得祕鑰指紋信息並作對比並驗證客戶端身份;

8.之後所有的通信數據將由之前瀏覽器生成的隨機密碼並利用對稱加密算法進行加密,並按照協商定期更換祕鑰。

CA證書是一個加密會話建立信任的初始階段,互聯網上的知名網站的證書肯定是要想CA認證機構申請的,費用也是相當可觀的。而公司內部一些建立於CA之上的通訊完成可以通過Openssl來建立私有的CA服務。

四、使用Openssl建立私有CA

openssl 配置文件 /etc/pki/tls/openssl.cnf

dir             /etc/pki/CA               # Where everything is kept 
certs           = $dir/certs                # Where the issued certs are kept
crl_dir         = $dir/crl                  # Where the issued crl are kept
database        = $dir/index.txt            # database index file.
#unique_subject = no                        # Set to 'no' to allow creation of
                                            # several ctificates with same subject.
new_certs_dir   = $dir/newcerts             # default place for new certs.
 
certificate     = $dir/cacert.pem           # The CA certificate
serial          = $dir/serial               # The current serial number
crlnumber       = $dir/crlnumber            # the current crl number
                                            # must be commented out to leave a V1 CRL
crl             = $dir/crl.pem              # The current CRL
private_key     = $dir/private/cakey.pem    # The private key
RANDFILE        = $dir/private/.rand        # private random number file

  4.1初始化服務器工作環境    

[root@localhost ~]# touch /etc/pki/CA/{index.txit,serial,crlnumber} #創建證書發送數據庫、證書序列號、吊銷證書數字文件
[root@localhost ~]# echo 100001 > /etc/pki/CA/serial        #建立發放證書初始序列號
[root@localhost ~]# echo 000001 > /etc/pki/CA/crlnumber                 #鍵入吊銷證書初始序列號

4.2 創建祕鑰對及自認證證書

[root@localhost ~]# (umask 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)   #創建私鑰;(umask 077;)改變umask並只對此子shell有效括號內的命令之行結束後即失效
Generating RSA private key, 2048 bit long modulus
...............+++
.........................................................................................+++
e is 65537 (0x10001)
[root@localhost ~]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 365
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) []:shandong                        #定義證書所屬省份(州)
Locality Name (eg, city) [Default City]:dezhou                        #定義證書所屬城市
Organization Name (eg, company) [Default Company Ltd]:eway            #定義證書所屬組織、公司
Organizational Unit Name (eg, section) []:support                     #定義證書所屬部門
Common Name (eg, your name or your server's hostname) []:ca.eway.com  #定義使用此證書的主機名稱
Email Address []:[email protected]                                   #定義管理員郵箱
 
 
###################################################
命令參數
req: 生成證書籤署請求
     -news: 新請求
     -key /path/to/keyfile: 指定私鑰文件
     -out /path/to/somefile: 指定生成證書位置
     -x509: 生成自簽署證書
     -days n: 有效天數

4.3 證書申請節點生成私鑰及簽名請求

#####生成私鑰
[root@localhost ~]# (umask 077; openssl genrsa -out /root/cekay.pam 2048)
Generating RSA private key, 2048 bit long modulus
............................................................+++
..............+++
e is 65537 (0x10001)
#####生成證書籤名請求
[root@localhost ~]# openssl req -new -key /root/cekay.pam -out /root/cakey.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                        #需要跟CA服務器保持一致             
State or Province Name (full name) []:shandong              #需要跟CA服務器保持一致
Locality Name (eg, city) [Default City]:dezhou              #需要跟CA服務器保持一致
Organization Name (eg, company) [Default Company Ltd]:eway  #需要跟CA服務器保持一致
Organizational Unit Name (eg, section) []:www
Common Name (eg, your name or your server's hostname) []:    
Email Address []:[email protected]
 
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:                                   #證書祕鑰(可以不加密碼)
An optional company name []:                               #可選的公司名稱
#####將證書籤名請求發送給CA服務器
[root@localhost ~]# scp /root/cakey.csr [email protected]:/home/mylinux
[email protected]'s password: 
cakey.csr                                          100% 1050     1.0KB/s   00:00

   4.4 CA服務器對此證書請求進行簽名並返回給此服務器

[root@localhost ~]# openssl ca -in /home/mylinux/cakey.csr -out /home/mylinux/cakey.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1048577 (0x100001)
        Validity
            Not Before: Oct  5 21:21:14 2015 GMT
            Not After : Oct  4 21:21:14 2016 GMT
        Subject:
            countryName               = cn
            stateOrProvinceName       = shandong
            organizationName          = eway
            organizationalUnitName    = www
            commonName                = www.eway.com
            emailAddress              = [email protected]
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                73:0E:C3:34:70:BE:AF:E4:4F:B6:86:5E:94:33:29:E8:84:8F:A6:2D
            X509v3 Authority Key Identifier: 
                keyid:B1:46:C8:1F:7F:05:63:B5:B5:6E:36:A9:CF:D4:5C:A2:B7:DF:D7:91
 
Certificate is to be certified until Oct  4 21:21:14 2016 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
 
#####將證書遠程複製給客戶機
[root@localhost ~]# scp /home/mylinux/cakey.crt [email protected]:/root/
[email protected]'s password: 
cakey.crt                                      100% 4620     4.5KB/s   00:00

吊銷證書

如果你的私鑰泄露了,那麼證書就不能使用了,需要去CA服務器申請吊銷此證書

#####獲取你證書的序列號和相關信息
[root@localhost ~]# openssl x509 -in /root/cakey.crt -noout  -serial  -subject
serial=100001
subject= /C=cn/ST=shandong/O=eway/OU=www/CN=
#####將此信息及相關的***明信息發送給CA服務管理員,管理員在覈對信息無誤後對證書進項吊銷操作。隨後你需要重新申請證書。
 
#####CA服務吊銷證書操作
[root@localhost ~]# openssl ca -revoke /etc/pki/CA/newcerts/100001.pem     #證書吊銷是以序列名稱來實現的
Using configuration from /etc/pki/tls/openssl.cnf
Revoking Certificate 100001.
Data Base Updated
[root@localhost ~]# openssl ca -gencrl -out /etc/pki/CA/crl/ca.crl         #更新證書吊銷列表
Using configuration from /etc/pki/tls/openssl.cnf

j_0028.gif國慶假期快樂~!


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