加密解密基礎、PKI以及自建私有CA
加密是以某種特殊的算法改變原有的信息數據,使得未授權的用戶即使獲得了已加密的信息,但因不知解密的方法,仍然無法了解信息的內容。
解密就是加密的逆向操作,將加密過的不可讀的數據經過處理,能夠是之前不可讀的信息還原成可讀信息。
PKI是Public Key Infrastructure的首字母縮寫,翻譯過來就是公鑰基礎設施;PKI是一種遵循標準的利用公鑰加密技術為電子商務的開展提供一套安全基礎平臺的技術和規范。
加密、解密
計算機進程間的通信可以分為同一主機上的和不同主機上的進程通信;同一主機上的進程間通訊通過IPC、Message Queue(消息隊列)、SHM(空閑內存)、Semerphor等4種;而不同主機將的進程通信則是通過Socket完成,其主要的形式為:ServerIP:prot<—->ClientIP:port。信息從一臺主機的某個端口出來,到達目的主機后,將數據送達某端口代表的運行程序,完成主機間的信息傳送。
如果在傳送的過程中,信息被截獲,那么數據在沒有進行任何處理的情況下,截獲信息的人就能很容易的指導傳送的信息內容。這樣就造成了信息泄露。為了不讓這樣的事情發生,于是就有了將數據在處送前進行處理,使得沒有得到授權的人即使獲取了處理后的信息,也沒有辦法將從中獲得有用信息,這就是加密;只有被授權的人才能通過某種處理方式將處理過的信息二次處理,還原原信息,從而能夠獲取原有信息,這個過程稱之為解密。
隨著互聯網的發展,信息安全也被人越來越重視。因此,加密技術得到的很好的發展。加密方式可以分為對稱加密、非對稱加密、單向加密、認證協議等。
對稱加密
即加密和解密使用同一個密鑰,將數據塊分割成固定大小的塊逐個進行加密。常用的算法有:DES(Date Encryption Standard)、3DES(Triple DES)、AES(Advanced)、Blowfish、Twofish、IDEA, RC6, CAST5等;
特性:
1、加密、解密使用同一個密鑰,效率高
2、將原始數據分割成固定大小的塊,逐個進行加密
缺陷:
1、 密鑰過多
2、密鑰分發
3、數據來源無法確認
非對稱加密
與對稱加密算法不同,非對稱加密算法需要兩個密鑰:公開密鑰(publickey)和私有密鑰(privatekey)。公開密鑰與私有密鑰是一對,如果用公開密鑰對數據進行加密,只有用對應的私有密鑰才能解密;如果用私有密鑰對數據進行加密,那么只有用對應的公開密鑰才能解密。因為加密和解密使用的是兩個不同的密鑰,所以這種算法叫作非對稱加密算法。
常見的非對稱加密算法有:RSA(加密、簽名)、Diffie-Hellman(DH)、El Gamal、DSA(只能數字簽名用)。其中DH算法常用與密鑰交換,其實現過程為:
第一步:雙方協商兩個公開的數:整數a以及一個大素數P;
第二步:一方生成隱私數m,計算a^m%p得到隱私數據發給另一方;另一方同樣的生成隱私數n,從而生成隱私數據a^n%p發給對方;
第三步:雙方在得到對方發的隱私數據后,通過對方的隱私數據和自己的隱私數算出密鑰:即(a^m%p)^n和(a^n%p)^m得到相同的密鑰a^mn%p。
使用非對稱加密,公鑰是任何人都可以得到的,而密鑰只能使用者一個人使用,必須保證其私密性,若果泄露,則必須馬上更換密鑰對。
特性:
數字簽名:主要在于讓接收方確認發送方身份
對稱密鑰交換:發送方用對方的公鑰加密一個對稱密鑰后發送給
對方
數據加密:適合加密較小數據
缺陷:
密碼長,加密解密效率低下
單向加密
不管加密的數據多長都會定長輸出;不可逆轉即無法將得到的數據在逆著解密;雪崩效應即數據微小的變化就會導致結果的巨大變化;
常用的算法有:MD5、 SHA1 、CRC-32。
加密過程
3種加密算法各有優勢,但是在現今復雜的網絡環境都無法保證數據在傳輸過程中的安全。因此,在現今的網絡數據傳輸中常常根據各算法的優勢,在對不同的數據加密上使用不同的算法。以小明和小紅在網絡上通信為例:
小明和小紅在建立其鏈接以后,向小紅發起數據傳送請求,小紅在收到請求以后,回應小明,雙方一起協商,雙方交換彼此的公鑰,協商加密的算法。在完成協商以后,小明就開始將要傳送的數據加密:
第一步:用單向加密算法將要傳輸的數據提取特征碼;
第二步:將提取出的特征碼,用小明的私鑰加密;
第三步:用協商好的對稱加密協議對要傳送的數據和已經加密后的特征碼加密;
第四步:用小紅的公鑰將第三步的密鑰進行加密附加到第三步加密好的數據的后面;
完成上面四步后,小明就將數據通過網絡向小紅發送。小紅在拿到數據后經過以下步驟解密:
第一步:小紅用私鑰解密,拿到加密數據的密鑰;
第二步:然后用拿到的密鑰解密,拿到數據和加密后的特征碼;
第三步:用小明的公鑰解密加密后的特征碼,從而驗證數據的來源;
第四部:在獲得數據特征碼后,在通過相同的算法對數據進行單向加密,提取特征碼進行對比,驗證數據的完整性和正確性。
上述的過程,在雙方確定對方的身份沒有問題后,傳出數據的安全得到了很大的保障;但是,在傳送數據前,如何保證對方的身份沒有問題?這就需要一個權威的公證機構,即CA。
PKI
PKI(Public Key Infrastructure)公鑰基礎設施是提供公鑰加密和數字簽名服務的系統或平臺,目的是為了管理密鑰和證書。一個機構通過采用PKI 框架管理密鑰和證書可以建立一個安全的網絡環境。PKI 主要包括四個部分:X.509 格式的證書(X.509 V3)和證書廢止列表CRL(X.509 V2);CA 操作協議;CA 管理協議;CA 政策制定。一個典型、完整、有效的PKI 應用系統至少應具有以下五個部分;
1) 認證中心CA CA 是PKI 的核心,CA 負責管理PKI 結構下的所有用戶(包括各種應用程序)的證書,把用戶的公鑰和用戶的其他信息捆綁在一起,在網上驗證用戶的身份,CA 還要負責用戶證書的黑名單登記和黑名單發布,后面有CA 的詳細描述。
2) X.500 目錄服務器 X.500 目錄服務器用于發布用戶的證書和黑名單信息,用戶可通過標準的LDAP 協議查詢自己或其他人的證書和下載黑名單信息。
3) 具有高強度密碼算法(SSL)的安全WWW服務器 Secure socket layer(SSL)協議最初由Netscape 企業發展,現已成為網絡用來鑒別網站和網頁瀏覽者身份,以及在瀏覽器使用者及網頁服務器之間進行加密通訊的全球化標準。
4) Web(安全通信平臺) Web 有Web Client 端和Web Server 端兩部分,分別安裝在客戶端和服務器端,通過具有高強度密碼算法的SSL 協議保證客戶端和服務器端數據的機密性、完整性、身份驗證。
5) 自開發安全應用系統 自開發安全應用系統是指各行業自開發的各種具體應用系統,例如銀行、證券的應用系統等。完整的PKI 包括認證政策的制定(包括遵循的技術標準、各CA 之間的上下級或同級關系、安全策略、安全程度、服務對象、管理原則和框架等)、認證規則、運作制度的制定、所涉及的各方法律關系內容以及技術的實現等。
完整的PKI系統必須具有權威認證機構(CA)、數字證書庫、密鑰備份及恢復系統、證書作廢系統、應用接口(API)等基本構成部分,構建PKI也將圍繞著這五大系統來著手構建。
PKI的基礎技術包括加密、數字簽名、數據完整性機制、數字信封、雙重數字簽名等。一個典型、完整、有效的PKI應用系統至少應具有以下部分:
· 公鑰密碼證書管理。
· 黑名單的發布和管理。
· 密鑰的備份和恢復。
· 自動更新密鑰。
· 自動管理歷史密鑰。
· 支持交叉認證。
認證機構(CA):即數字證書的申請及簽發機關,CA必須具備權威性的特征;
CA
認證中心CA 作為PKI 的核心部分,CA 實現了PKI 中一些很重要的功能,概括地說,認證中心(CA)的功能有:證書發放、證書更新、證書撤銷和證書驗證。CA 的核心功能就是發放和管理數字證書,具體描述如下:
?。?)接收驗證最終用戶數字證書的申請。
?。?)確定是否接受最終用戶數字證書的申請-證書的審批。
(3)向申請者頒發、拒絕頒發數字證書-證書的發放。
(4)接收、處理最終用戶的數字證書更新請求-證書的更新。
?。?)接收最終用戶數字證書的查詢、撤銷。
(6)產生和發布證書廢止列表(CRL)。
?。?)數字證書的歸檔。
?。?)密鑰歸檔。
(9)歷史數據歸檔。
認證中心CA 為了實現其功能,主要由以下三部分組成:
注冊服務器:通過 Web Server 建立的站點,可為客戶提供24×7 不間斷的服務??蛻粼诰W上提出證書申請和填寫相應的證書申請表。
證書申請受理和審核機構:負責證書的申請和審核。它的主要功能是接受客戶證書申請并進行審核。
認證中心服務器:是數字證書生成、發放的運行實體,同時提供發放證書的管理、證書廢止列表(CRL)的生成和處理等服務。
在具體實施時,CA 的必須做到以下幾點:
1) 驗證并標識證書申請者的身份。
2) 確保CA 用于簽名證書的非對稱密鑰的質量。
3) 確保整個簽證過程的安全性,確保簽名私鑰的安全性。
4) 證書資料信息(包括公鑰證書序列號,CA 標識等)的管理。
5) 確定并檢查證書的有效期限。
6) 確保證書主體標識的唯一性,防止重名。
7) 發布并維護作廢證書列表。
8) 對整個證書簽發過程做日志記錄。
9) 向申請人發出通知。
在這其中最重要的是CA 自己的一對密鑰的管理,它必須確保其高度的機密性,防止他方偽造證書。CA 的公鑰在網上公開,因此整個網絡系統必須保證完整性。CA 的數字簽名保證了證書(實質是持有者的公鑰)的合法性和權威性。
用戶的公鑰有兩種產生的方式:
(1)用戶自己生成密鑰隊,然后將公鑰以安全的方式傳送給CA,該過程必須保證用戶公鑰的驗證性和完整性。
?。?)CA 替用戶生成密鑰隊,然后將其以安全的方式傳送給用戶,該過程必須確保密鑰對的機密性,完整性和可驗證性。該方式下由于用戶的私鑰為CA 所產生,所以對CA 的可信性有更高的要求。CA 必須在事后銷毀用戶的私鑰。
一般而言公鑰有兩大類用途,一個是用于驗證數字簽名,一個是用于加密信息。相應的在CA 系統中也需要配置用于數字簽名/驗證簽名的密鑰對和用于數據加密/脫密的密鑰對,分別稱為簽名密鑰對和加密密鑰對。由于兩種密鑰對的功能不同,管理起來也不大相同,所以在CA 中為一個用戶配置兩對密鑰,兩張證書。
CA的作用
回到小明和小紅通信的例子,在小明和小紅都拿到對方的公鑰后,不會輕易的信任拿到的公鑰就是對方真正的公鑰,這時候,雙方都會驗證對方公鑰:
首先,用簽發對方公鑰的機構即CA的公鑰解密對方證書(CA簽發的權威的認證),得到CA的簽名,驗證證書的來源合法性;
然后,用同樣的單向加密方式加密證書,得到證書的特則碼,兩者相比較,驗證證書的完整性;
再然后分別檢查驗證的有效期,驗證是否過期,同時驗證證書的名字和通信的對象是否一致;最后驗證證書是否被吊銷。
所有驗證都通過以后,才會有數據的加密通信過程。
證書類型:
證書授權機構的證書
服務器
用戶證書
獲取證書兩種方法:
使用證書授權機構
生成簽名請求( csr)
將csr發送給CA
從CA處接收簽名
自簽名的證書
自已簽發自己的公鑰
SSL/TLS協議
SSL即Secure Socket Layer,安全套接字層
TLS即Transport Layer Security,傳輸層安全
SSL/TLS工作在TCP/IP模型的傳輸層和因特網層之間,如下圖:
分為兩層:HandShake Layer和Record Layer:
Handshake協議:包括協商安全參數和密碼套件、服務器身份認證(客戶端身份認證可選)、密鑰交換:
ChangeCipherSpec 協議:一條消息表明握手協議已經完成;
Alert 協議:對握手協議中一些異常的錯誤提醒,分為fatal和warning兩個級別, fatal類型錯誤會直接中斷SSL鏈接,而warning級別的錯誤SSL鏈接仍可繼續,只是會給出錯誤警告;
Record 協議:包括對消息的分段、壓縮、消息認證和完整性保護、加密等;
基于SSl/TLS協議通信的實現過程
可以通過一幅圖來表示其通信的實現過程:
第一步:客戶端向服務端發起握手請求,給出協議版本號、一個生成的隨機數(Client random),以及客戶端支持的加密方法。
第二步,服務端確認雙方使用的加密方法,并給出數字證書、以及一個服務端生成的隨機數(Server random)。
第三步,客戶端確認數字證書有效,然后生成一個新的隨機數(Premaster secret),并使用數字證書中的公鑰,加密這個隨機數,發給服務端。
第四步,服務端使用自己的私鑰,獲取客戶端發來的隨機數(即Premaster secret)。
第五步,雙方根據約定的加密方法,使用前面的三個隨機數,生成”對話密鑰”(session key),用來加密接下來的整個對話過程。
自建私有CA
OpenSSL
OpenSSL是一個開放源代碼的軟件庫包,應用程序可以使用這個包來進行安全通信,避免竊聽,同時確認另一端連接者的身份。這個包廣泛被應用在互聯網的網頁服務器上。實現了基本的加密功能,實現了SSL與TLS協議。
OpenSSL由三部分組成:libssl庫、libcrypto庫以及openssl命令行工具,重點在于openssl工具的使用;
openssl又分為標準命令、信息摘要命令和加密命令:
[root:~]# openssl -l
openssl:Error: '-l' 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
使用openssl工具可以實現對稱加密、非對稱加密和單向加密
對稱加密
對稱加密使用命令openssl enc,使用方法如下:
###加密過程
[root:~]# openssl enc -e -des3 -a -in functions -out functions.ciphers
enter des-ede3-cbc encryption password:
Verifying - enter des-ede3-cbc encryption password:
[root:~]# ll functions functions.ciphers
-rw-r--r-- 1 root root 13948 Oct 26 14:40 functions
-rw-r--r-- 1 root root 18915 Dec 4 14:19 functions.ciphers
###解密過程
[root:~]# openssl enc -d -des3 -a -in functions.ciphers -out functions.pl
enter des-ede3-cbc decryption password:
[root:~]# ll functions functions.pl
-rw-r--r-- 1 root root 13948 Oct 26 14:40 functions
-rw-r--r-- 1 root root 13948 Dec 4 14:22 functions.pl
[root:~]# diff functions functions.
functions.ciphers functions.pl
[root:~]# diff functions functions.pl
[root:~]#
-e:加密;
-d:解密;
-des3:加密算法
-a:Base64編碼
-in:后面跟被加密/解密的文件
-out:后面輸入文件名,解密/加密后的內容保存的文件
單向加密使用命令openssl dgst,使用方法如下:
##默認使用md5算法
[root:~]# openssl dgst functions
MD5(functions)= 7ef254bfccd88ec4c113d0a1bfaede89
###指定其他的加密算法
[root:~]# openssl dgst -sha512 functions
SHA512(functions)= 75dd013dba7182aecbaa28c4df31a00b359106b8db0088d0c3c938ba0fa1aeca6c8548993326cd3c38b2776fe38b09c8edb3c0e660de24d542cd927a75a7e6b1
單向加密
使用openssl單向加密,可以使用很多的加密算法:
-md4 to use the md4 message digest algorithm
-md5 to use the md5 message digest algorithm
-ripemd160 to use the ripemd160 message digest algorithm
-sha to use the sha message digest algorithm
-sha1 to use the sha1 message digest algorithm
-sha224 to use the sha224 message digest algorithm
-sha256 to use the sha256 message digest algorithm
-sha384 to use the sha384 message digest algorithm
-sha512 to use the sha512 message digest algorithm
-whirlpool to use the whirlpool message digest algorithm
在Linux系統中,用戶登錄的密碼也是單向算法生成的,同時,openssl工具也有專門為用戶生成登錄密碼的工具:openssl passwd,可以使用幫助命令man sslpasswd獲取相應的幫助,使用方法如下:
[root:~]# openssl passwd -1 -salt 123
Password:
$1$123$Ok9FhQy4YioYZeBPwQgm3/
-1:指定的加密算法是md5,也可以不指定;不指定加密算法,默認使用md5;
-salt 123:給密碼加”鹽“(最多8位),也可以不指定;
提示輸入密碼后,下面的字符串就是生成的密碼。
非對稱加密
非對稱加密需要一對鑰匙,即公鑰和私鑰,要生成這對鑰匙,可以使用openssl工具和gpg工具。不同的工具生成的密鑰的使用也是不一樣。一般來說,openssl工具生成的密鑰,很多使用用于CA證書的申請,gpg工具用來加密解密文件。
使用openssl工具生成
生成私鑰:
[root:~]# openssl genrsa -out abc.pri ##生成一個私鑰文件abc.pri,沒有加密的私鑰文件
Generating RSA private key, 1024 bit long modulus ##長度為1024位
......++++++
.......................++++++
e is 65537 (0x10001)
[root:~]# ll abc.pri ;file abc.pri
-rw-r--r-- 1 root root 887 Dec 4 16:30 abc.pri
abc.pri: PEM RSA private key
###一下生成一個機密的指定位數的機密私鑰文件
[root:~]# openssl genrsa -out abc.pir1 -des3 2048 ##-des3指定的加密私鑰文件的加密算法,一般使用”-des|-des3|-idea“中的一個
Generating RSA private key, 2048 bit long modulus ##2048位的長度
.................................................+++
.....................+++
e is 65537 (0x10001)
Enter pass phrase for abc.pir1:
Verifying - Enter pass phrase for abc.pir1:
[root:~]# file abc.pir1;ll abc.pir1
abc.pir1: PEM RSA private key
-rw-r--r-- 1 root root 1751 Dec 4 16:36 abc.pir1
注意:上面的命令生成的私鑰文件的權限為644,很危險,為了保證安全,將私鑰文件的權限設置為600。可以使用chmod命令改變權限,也可以在命令之前使用umask命令指定權限掩碼后在執行私鑰生成命令。在此過程中,值得注意的是,指定umask的時候只能影響后面生成私鑰的命令,不能改變整個shell環境的umask,因此,在執行的時候,需要用小括號,指定在子shell中生效:
[root:~]# ( umask 066;openssl genrsa -out abc.pir2 -des3 2048 )
Generating RSA private key, 2048 bit long modulus
..........................+++
.......................................................................................................................................................................................................................+++
e is 65537 (0x10001)
Enter pass phrase for abc.pir2:
Verifying - Enter pass phrase for abc.pir2:
[root:~]# ll abc.pir2
-rw------- 1 root root 1751 Dec 4 16:55 abc.pir2
[root:~]# umask
0022
gpg工具生成密鑰對
與openssl工具相比,gpg工具生成密鑰,一次性生成一對:
[root:~]# gpg --gen-key
gpg (GnuPG) 2.0.22; Copyright (C) 2013 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Please select what kind of key you want:
(1) RSA and RSA (default)
(2) DSA and Elgamal
(3) DSA (sign only)
(4) RSA (sign only)
Your selection?
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) ##鑰匙大?。?024-4096
Requested keysize is 2048 bits
Please specify how long the key should be valid.
0 = key does not expire
<n> = key expires in n days
<n>w = key expires in n weeks
<n>m = key expires in n months
<n>y = key expires in n years
Key is valid for? (0)
Key does not expire at all
Is this correct? (y/N) y
GnuPG needs to construct a user ID to identify your key.
Real name: wangcai ##輸入鑰匙的名字,至少5字符以上
Email address:
Comment:
You selected this USER-ID:
"wangcai"
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
You need a Passphrase to protect your secret key. ##提示輸入鑰匙密碼,以保護鑰匙的安全,可以不輸入
### 下面是產生隨機數以填充密鑰,在此過程中需要產生IO操作,最好就是敲鍵盤,移動鼠標或者讓磁盤使勁讀寫
You don't want a passphrase - this is probably a *bad* idea!
I will do it anyway. You can change your passphrase at any time,
using this program with the option "--edit-key".
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
上述操作完成后,可以使用命令查看密鑰對:
[root:~]# gpg --list-key
/root/.gnupg/pubring.gpg ##密鑰存放的位置
------------------------
pub 2048R/B1A2454A 2016-12-04
uid wangcai ###密鑰對的ID
sub 2048R/D23D0478 2016-12-04
[root:~]# cd .gnupg/
[root:.gnupg]# ll
total 28
-rw------- 1 root root 7680 Nov 30 16:37 gpg.conf
drwx------ 2 root root 6 Nov 30 16:37 private-keys-v1.d
-rw------- 1 root root 1169 Dec 4 17:18 pubring.gpg ##公鑰
-rw------- 1 root root 1169 Dec 4 17:18 pubring.gpg~
-rw------- 1 root root 600 Dec 4 17:18 random_seed
-rw------- 1 root root 2471 Dec 4 17:18 secring.gpg ##私鑰
srwxr-xr-x 1 root root 0 Dec 4 17:20 S.gpg-agent
-rw------- 1 root root 1280 Dec 4 17:18 trustdb.gpg
要使用公鑰,必須將公鑰導出并且傳遞給需要的對象,并且將公鑰導入對方的公鑰庫中。
##導出密鑰并發送給對方主機
[root:.gnupg]# gpg -a --export -o wangcai.pu
[root:.gnupg]# scp wangcai.pu root@172.16.251.219:/root
root@172.16.251.219's password:
wangcai.pu 100% 1687 1.7KB/s 00:00
###對方生成的密鑰對
[root:~]# gpg --list-key
/root/.gnupg/pubring.gpg
------------------------
pub 2048R/8BF9F56C 2016-12-04
uid gentoo
sub 2048R/991066D5 2016-12-04
###將收到的公鑰文件導入
[root:~]# gpg --import wangcai.pu
gpg: key 0CA15C47: public key "wangcai" imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
[root:~]# gpg --list-keys
/root/.gnupg/pubring.gpg
------------------------
pub 1024R/32E2119F 2016-12-04
uid gentoo
sub 1024R/25F92931 2016-12-04
pub 1024R/0CA15C47 2016-12-04
uid wangcai
sub 1024R/A0FDCCAD 2016-12-04
將wangcai公鑰加密的文件發送回去,就能夠用wangcai私鑰解:
[root:~]# gpg -e -r wangcai adb.txt ## -r 指定用誰的鑰匙加密
gpg: A0FDCCAD: There is no assurance this key belongs to the named user
pub 1024R/A0FDCCAD 2016-12-04 wangcai
Primary key fingerprint: 9D56 F853 D32F 2B9C A380 1111 ABD9 3929 0CA1 5C47
Subkey fingerprint: 8A6F ECE9 A098 B7F4 2F7A DB16 EE56 E7E8 A0FD CCAD
It is NOT certain that the key belongs to the person named
in the user ID. If you *really* know what you are doing,
you may answer the next question with yes.
Use this key anyway? (y/N) y
[root:~]# scp adb.txt.gpg 172.16.252.81:/root
root@172.16.252.81's password:
adb.txt.gpg 100% 246 0.2KB/s 00:00
###在wangcai主機上用wangci的私鑰解開文件
[root:~]# gpg -d adb.txt.gpg ## -d 解密文件
gpg: encrypted with 1024-bit RSA key, ID A0FDCCAD, created 2016-12-04
"wangcai"
name score sex
yang 80 m
zhang 46 f
[root:~]# gpg -d adb.txt.gpg -o adb.txt
usage: gpg [options] --decrypt [filename]
[root:~]# gpg -o adb.txt -d adb.txt.gpg ## -o 將解密后的內容保存為文件
gpg: encrypted with 1024-bit RSA key, ID A0FDCCAD, created 2016-12-04
"wangcai"
[root:~]# ll adb.txt
-rw-r--r-- 1 root root 43 Dec 4 18:41 adb.txt
[root:~]# cat adb.txt
name score sex
yang 80 m
zhang 46 f
刪除主機上的公鑰:
##刪除主機上的wangcai的公鑰
[root:~]# gpg --delete-key wangcai
gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
pub 1024R/0CA15C47 2016-12-04 wangcai
Delete this key from the keyring? (y/N) y
[root:~]# gpg --list-keys
/root/.gnupg/pubring.gpg
------------------------
pub 1024R/32E2119F 2016-12-04
uid gentoo
sub 1024R/25F92931 2016-12-04
##刪除主機上的密鑰對
[root:~]# gpg --delete-secret-key gentoo ##刪除私鑰
gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
sec 1024R/32E2119F 2016-12-04 gentoo
Delete this key from the keyring? (y/N) y
This is a secret key! - really delete? (y/N) y
[root:~]# gpg --list-key
/root/.gnupg/pubring.gpg
------------------------
pub 1024R/32E2119F 2016-12-04
uid gentoo
sub 1024R/25F92931 2016-12-04
[root:~]# cd .gnupg/
[root:.gnupg]# ll
total 28
-rw------- 1 root root 7856 Oct 15 15:16 gpg.conf
drwx------ 2 root root 4096 Dec 4 16:16 private-keys-v1.d
-rw------- 1 root root 649 Dec 4 19:30 pubring.gpg
-rw------- 1 root root 1300 Dec 4 18:12 pubring.gpg~
-rw------- 1 root root 600 Dec 4 18:38 random_seed
-rw------- 1 root root 0 Dec 4 19:33 secring.gpg ##私鑰大小為0 已經刪除
-rw------- 1 root root 1360 Dec 4 16:43 trustdb.gpg
[root:.gnupg]# gpg --delete-key gentoo ##刪除公鑰
gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
pub 1024R/32E2119F 2016-12-04 gentoo
Delete this key from the keyring? (y/N) y
[root:.gnupg]# ll
total 24
-rw------- 1 root root 7856 Oct 15 15:16 gpg.conf
drwx------ 2 root root 4096 Dec 4 16:16 private-keys-v1.d
-rw------- 1 root root 0 Dec 4 19:35 pubring.gpg ##公鑰大小為0 已經刪除
-rw------- 1 root root 649 Dec 4 19:30 pubring.gpg~
-rw------- 1 root root 600 Dec 4 18:38 random_seed
-rw------- 1 root root 0 Dec 4 19:33 secring.gpg
-rw------- 1 root root 1360 Dec 4 19:35 trustdb.gpg
自建CA
在構建CA之前,查看PKI的相關配置文件:
[root:~]# ll /etc/pki/tls/openssl.cnf
-rw-r--r--. 1 root root 10923 Mar 6 2015 /etc/pki/tls/openssl.cnf
打開該文件,尋找關于CA的配置,在CentOS7 中的配置如下:
####################################################################
36 [ ca ]
37 default_ca = CA_default # The default ca section
38
39 ####################################################################
40 [ CA_default ]
41
42 dir = /etc/pki/CA # Where everything is kept
43 certs = $dir/certs # Where the issued certs are kept
44 crl_dir = $dir/crl # Where the issued crl are kept
45 database = $dir/index.txt # database index file.
46 #unique_subject = no # Set to 'no' to allow creation of
47 # several ctificates with same subject.
48 new_certs_dir = $dir/newcerts # default place for new certs.
49
50 certificate = $dir/cacert.pem # The CA certificate
51 serial = $dir/serial # The current serial number
52 crlnumber = $dir/crlnumber # the current crl number
53 # must be commented out to leave a V1 CRL
54 crl = $dir/crl.pem # The current CRL
55 private_key = $dir/private/cakey.pem# The private key
56 RANDFILE = $dir/private/.rand # private random number file
57
58 x509_extensions = usr_cert # The extentions to add to the cert
59
60 # Comment out the following two lines for the "traditional"
61 # (and highly broken) format.
62 name_opt = ca_default # Subject Name options
63 cert_opt = ca_default # Certificate field options
64
65 # Extension copying option: use with caution.
66 # copy_extensions = copy
67
68 # Extensions to add to a CRL. Note: Netscape communicator chokes on V2 CRLs
69 # so this is commented out by default to leave a V1 CRL.
70 # crlnumber must also be commented out to leave a V1 CRL.
71 # crl_extensions = crl_ext
72
73 default_days = 365 # how long to certify for
74 default_crl_days= 30 # how long before next CRL
75 default_md = sha256 # use SHA-256 by default
76 preserve = no # keep passed DN ordering
77
78 # A few difference way of specifying how similar the request should look
79 # For type CA, the listed attributes must be the same, and the optional
80 # and supplied fields are just that :-)
81 policy = policy_match
82
83 # For the CA policy
84 [ policy_match ]
85 countryName = match
86 stateOrProvinceName = match
87 organizationName = match
88 organizationalUnitName = optional
89 commonName = supplied
90 emailAddress = optional
創建CA的相關配置按照配置文件的配置進行,否則會出錯。
在操作之前,在/etc/pki/CA目錄下創建必要的文件以及目錄:
[root:CA]# ll
total 4
drwxr-xr-x. 2 root root 6 Jun 29 2015 certs
drwxr-xr-x. 2 root root 6 Jun 29 2015 crl
-rw-r--r-- 1 root root 0 Dec 4 20:25 index.txt
drwxr-xr-x. 2 root root 6 Jun 29 2015 newcerts
drwx------. 2 root root 22 Dec 4 20:06 private
-rw-r--r-- 1 root root 0 Dec 4 20:25 serial
第一步:在要當CA服務器的主機上生成私鑰,使用工具openssl。注意:私鑰的存放位置要按照配置文件的要求,默認的位置為:private_key = $dir/private/cakey.pem,即/etc/pki/CA/private目錄下面:
[root:~]# cd /etc/pki/CA/private/
[root:private]# ( umask 077;openssl genrsa -out cakey.pem )
Generating RSA private key, 1024 bit long modulus
....................................................................++++++
.....................................++++++
e is 65537 (0x10001)
[root:private]# ls
cakey.pem
第二步:生成自簽證書
[root:private]# cd .. ##證書的位置,按照配置文件的要求
[root:CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 7000
\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) []:BEIJNG
Locality Name (eg, city) [Default City]:BEIJING
Organization Name (eg, company) [Default Company Ltd]:zonekey
Organizational Unit Name (eg, section) []:tl
Common Name (eg, your name or your server's hostname) []:roger
Email Address []:
[root:CA]# ll
total 4
-rw-r--r-- 1 root root 944 Dec 4 20:13 cacert.pem
drwxr-xr-x. 2 root root 6 Jun 29 2015 certs #生成的證書,自簽證書生成。
-rw-r--r-- 1 root root 0 Dec 4 20:25 index.txt
drwxr-xr-x. 2 root root 6 Jun 29 2015 crl
drwxr-xr-x. 2 root root 6 Jun 29 2015 newcerts
drwx------. 2 root root 22 Dec 4 20:06 private
-rw-r--r-- 1 root root 0 Dec 4 20:25 serial
注意:生成的自簽證書的命令中:
-new:生成新證書的簽署請求
-x509:生成自簽格式證書,專用于創建私有CA時
-key:生成請求時用到的私有文件路徑
-out:生成的請求文件路徑,如果自簽操作將直接生成簽署過的證書
-days:證書的有效時長,單位為天
到此,CA服務器的搭建完成,就可以為其他的主機簽署證書了。
簽署證書
在被簽署的主機上生成私鑰:
[root:~]# cd /etc/pki/tls
[root:tls]# ls
cert.pem certs misc openssl.cnf private
[root:tls]# cd private/
[root:private]# ls
[root:private]# (umask 077;openssl genrsa -out http.key)
Generating RSA private key, 1024 bit long modulus
.......................++++++
.............++++++
e is 65537 (0x10001)
生成證書簽署請求:
[root:private]# 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]:beijing
Organization Name (eg, company) [Default Company Ltd]:zoekey
Organizational Unit Name (eg, section) []:it
Common Name (eg, your name or your server's hostname) []:roger
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root:private]# cd ..
[root:tls]# ls
cert.pem certs http.csr misc openssl.cnf private
將上步的請求證書拷貝到CA主機上,在CA主機上簽署證書:
[root:CA]# openssl ca -in /root/http.csr -out certs/http.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 2 (0x2)
Validity
Not Before: Dec 4 14:09:42 2016 GMT
Not After : Dec 4 14:09:42 2017 GMT
Subject:
countryName = CN
stateOrProvinceName = beijing
organizationName = zoekey
organizationalUnitName = it
commonName = roger
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
A2:B5:9A:22:A3:33:6D:7F:C6:16:6D:3B:FA:38:B4:3D:EC:AC:2F:EB
X509v3 Authority Key Identifier:
keyid:E3:13:89:7C:66:67:D5:14:A6:1D:22:65:71:32:47:C1:06:86:A2:8A
Certificate is to be certified until Dec 4 14:09:42 2017 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
將生成的http.crt證書發送給請求方,到此,證書簽發完成。
吊銷證書
分兩步,第一步,客戶機上獲取需要吊銷的證書的序列號等信息:
[root:certs]# openssl x509 -in http.crt -noout -serial -subject
serial=02
subject= /C=CN/ST=beijing/O=zoekey/OU=it/CN=roger
根據客戶機提交的信息,在CA主機上對比信息是否一致:
[root:newcerts]# cat 02.pem
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 2 (0x2)
Signature Algorithm: sha256WithRSAEncryption
Issuer: C=CN, ST=beijing, L=beijing, O=zonekey, OU=tl, CN=roger
Validity
Not Before: Dec 4 14:09:42 2016 GMT
Not After : Dec 4 14:09:42 2017 GMT
Subject: C=CN, ST=beijing, O=zoekey, OU=it, CN=roger
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (1024 bit)
Modulus:
00:be:86:b5:95:f8:0d:6c:62:a0:7e:27:b6:44:cb:
77:2b:1f:3b:ea:d4:9e:16:79:23:9e:ab:ed:56:91:
9d:c7:5e:b3:12:d2:34:c5:d2:f8:f6:6e:a7:8a:b0:
ae:a9:3d:e0:f1:2a:96:3e:59:c3:1b:6f:be:1f:06:
2d:6e:0e:8a:46:67:c4:e4:92:77:81:6c:b2:56:ec:
fd:65:cb:c5:2e:b4:01:de:f7:5e:cf:57:22:0d:b7:
5d:e3:f9:ba:9b:d6:18:23:50:e8:e5:ef:8e:dc:91:
9a:ab:40:de:06:8c:8a:9c:cb:8b:23:c4:c6:84:9d:
27:f2:6a:42:c0:f7:14:df:ab
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
A2:B5:9A:22:A3:33:6D:7F:C6:16:6D:3B:FA:38:B4:3D:EC:AC:2F:EB
X509v3 Authority Key Identifier:
keyid:E3:13:89:7C:66:67:D5:14:A6:1D:22:65:71:32:47:C1:06:86:A2:8A
Signature Algorithm: sha256WithRSAEncryption
80:c1:53:2f:d0:17:ce:a6:22:a5:05:2c:a8:6a:b9:be:35:db:
bb:be:45:df:5a:31:b3:83:82:ed:57:f0:9a:f9:6e:ba:d5:a1:
d2:ae:80:a4:1d:26:04:e9:55:a0:b9:89:0e:17:b2:8a:35:2c:
9f:df:ac:08:fe:a7:98:29:6e:9c:9a:12:2c:00:73:ae:d7:b5:
2c:c0:d4:21:4f:07:8f:1b:19:3a:06:5a:86:38:a0:22:f0:de:
fc:a8:53:98:b8:c7:04:13:c2:94:87:90:ea:ad:4b:67:f8:dd:
d6:2f:7e:76:8c:03:a8:5a:4f:49:7f:0a:b6:93:47:df:e7:b4:
78:1e
-----BEGIN CERTIFICATE-----
MIICnjCCAgegAwIBAgIBAjANBgkqhkiG9w0BAQsFADBgMQswCQYDVQQGEwJDTjEQ
MA4GA1UECAwHYmVpamluZzEQMA4GA1UEBwwHYmVpamluZzEQMA4GA1UECgwHem9u
ZWtleTELMAkGA1UECwwCdGwxDjAMBgNVBAMMBXJvZ2VyMB4XDTE2MTIwNDE0MDk0
MloXDTE3MTIwNDE0MDk0MlowTTELMAkGA1UEBhMCQ04xEDAOBgNVBAgMB2JlaWpp
bmcxDzANBgNVBAoMBnpvZWtleTELMAkGA1UECwwCaXQxDjAMBgNVBAMMBXJvZ2Vy
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+hrWV+A1sYqB+J7ZEy3crHzvq
1J4WeSOeq+1WkZ3HXrMS0jTF0vj2bqeKsK6pPeDxKpY+WcMbb74fBi1uDopGZ8Tk
kneBbLJW7P1ly8UutAHe917PVyINt13j+bqb1hgjUOjl747ckZqrQN4GjIqcy4sj
xMaEnSfyakLA9xTfqwIDAQABo3sweTAJBgNVHRMEAjAAMCwGCWCGSAGG+EIBDQQf
Fh1PcGVuU1NMIEdlbmVyYXRlZCBDZXJ0aWZpY2F0ZTAdBgNVHQ4EFgQUorWaIqMz
bX/GFm07+ji0PeysL+swHwYDVR0jBBgwFoAU4xOJfGZn1RSmHSJlcTJHwQaGooow
DQYJKoZIhvcNAQELBQADgYEAgMFTL9AXzqYipQUsqGq5vjXbu75F31oxs4OC7Vfw
mvluutWh0q6ApB0mBOlVoLmJDheyijUsn9+sCP6nmClunJoSLABzrte1LMDUIU8H
jxsZOgZahjigIvDe/KhTmLjHBBPClIeQ6q1LZ/jd1i9+dowDqFpPSX8KtpNH3+e0
eB4=
-----END CERTIFICATE-----
[root:newcerts]# cd ..
[root:CA]# cat index.txt
V 171204124322Z 01 unknown /C=CN/ST=beijing/O=zonekey/OU=tl/CN=rogerr
V 171204140942Z 02 unknown /C=CN/ST=beijing/O=zoekey/OU=it/CN=roger
確認無誤后,吊銷證書:
[root:CA]# openssl ca -revoke newcerts/02.pem
Using configuration from /etc/pki/tls/openssl.cnf
Revoking Certificate 02.
Data Base Updated
[root:CA]# touch crlnumber ##生成吊銷證書的編號,第一次吊銷的時候執行
[root:CA]# echo 01 > crlnumber
##查看現在的證書情況
[root:CA]# cat index.txt
V 171204124322Z 01 unknown /C=CN/ST=beijing/O=zonekey/OU=tl/CN=rogerr
R 171204140942Z 161204142254Z 02 unknown /C=CN/ST=beijing/O=zoekey/OU=it/CN=roger
##更新吊銷證書的列表
[root:CA]# openssl ca -gencrl -out crl/ca.crl
Using configuration from /etc/pki/tls/openssl.cnf
[root:CA]# cat crl/ca.crl
-----BEGIN X509 CRL-----
MIIBTjCBuAIBATANBgkqhkiG9w0BAQsFADBgMQswCQYDVQQGEwJDTjEQMA4GA1UE
CAwHYmVpamluZzEQMA4GA1UEBwwHYmVpamluZzEQMA4GA1UECgwHem9uZWtleTEL
MAkGA1UECwwCdGwxDjAMBgNVBAMMBXJvZ2VyFw0xNjEyMDQxNDI4NDFaFw0xNzAx
MDMxNDI4NDFaMBQwEgIBAhcNMTYxMjA0MTQyMjU0WqAOMAwwCgYDVR0UBAMCAQEw
DQYJKoZIhvcNAQELBQADgYEAj7xOVlkJrmk416MkrORKzxRtPcK5LImnLUW9Nzjc
5XHvrbntahv2oaJKQ35yO6dQlbox7spIvIuKMIV+JLABFzcelWrkfWC2qqejOXIN
8PAq0boCjX7GJkMJ7iKiXeSp/ou0dWk7O7KxbxpNE7hdJxMN9B6nXT3bLRvszpTo
2ig=
-----END X509 CRL-----
[root:CA]# openssl crl -in crl/ca.crl -noout -text
Certificate Revocation List (CRL):
Version 2 (0x1)
Signature Algorithm: sha256WithRSAEncryption
Issuer: /C=CN/ST=beijing/L=beijing/O=zonekey/OU=tl/CN=roger
Last Update: Dec 4 14:28:41 2016 GMT
Next Update: Jan 3 14:28:41 2017 GMT
CRL extensions:
X509v3 CRL Number:
1
Revoked Certificates:
Serial Number: 02
Revocation Date: Dec 4 14:22:54 2016 GMT
Signature Algorithm: sha256WithRSAEncryption
8f:bc:4e:56:59:09:ae:69:38:d7:a3:24:ac:e4:4a:cf:14:6d:
3d:c2:b9:2c:89:a7:2d:45:bd:37:38:dc:e5:71:ef:ad:b9:ed:
6a:1b:f6:a1:a2:4a:43:7e:72:3b:a7:50:95:ba:31:ee:ca:48:
bc:8b:8a:30:85:7e:24:b0:01:17:37:1e:95:6a:e4:7d:60:b6:
aa:a7:a3:39:72:0d:f0:f0:2a:d1:ba:02:8d:7e:c6:26:43:09:
ee:22:a2:5d:e4:a9:fe:8b:b4:75:69:3b:3b:b2:b1:6f:1a:4d:
13:b8:5d:27:13:0d:f4:1e:a7:5d:3d:db:2d:1b:ec:ce:94:e8:
da:28
證書吊銷完成。
原創文章,作者:王更生,如若轉載,請注明出處:http://www.www58058.com/62958