加解密基礎(chǔ)及openssl命令

加密算法和協(xié)議:

  1. 對(duì)稱(chēng)加密:
    。加解密使用同一個(gè)密鑰;將數(shù)據(jù)分割成固定大小的塊,逐塊加密;且塊與塊之間有關(guān)聯(lián)關(guān)系;
    。算法:DES,3DES,AES,Blowfish,Twofish,IDEA,RC6,CAST5
    。缺陷:密鑰過(guò)多;密鑰分發(fā)成為難題;

2.公鑰加密:
。密鑰分為公鑰和私鑰;
。公鑰:從私鑰中提取產(chǎn)生;可公開(kāi)給所有人;pubkey
。私鑰:通過(guò)工具創(chuàng)建,使用者自己保留,必須保證其私密性;secret key
。用公鑰加密的數(shù)據(jù),只能用與之配對(duì)的私鑰解密;私鑰加密的數(shù)據(jù),只能用與之配對(duì)的公鑰解密;
。用途:
(1)數(shù)字簽名:確認(rèn)發(fā)送方的身份;
(2)密鑰交換:發(fā)送方用對(duì)方的公鑰加密一個(gè)對(duì)稱(chēng)密鑰,并發(fā)送給對(duì)方;
。算法:RSA,DSA,ELGamal

3.單向加密:
。提取數(shù)據(jù)指紋;只能加密,不能解密;
。定長(zhǎng)輸出;雪崩效應(yīng)
。作用:數(shù)據(jù)完整性驗(yàn)證;
。算法:md5(128bits),sha1(160bits),sha224,sha256,sha384,sha512

4.密鑰交換:
(1)公鑰加密:
(2)DH算法(Deffie-Hellman):

DH算法:
A與B要交換密鑰:
A-->p,g;B-->p,g(p,g為大素?cái)?shù))
A-->x;B-->y
A:px%g-->B==>(px%g)^y
B:py%g-->A==>(py%g)^x
密鑰:(px%g)y=(py%g)x

Bob和Alice安全通信過(guò)程:

。加密:
(1)A生成數(shù)據(jù),用單向加密提取數(shù)據(jù)特征碼;
(2)用A的私鑰加密特征碼(加密后的特征碼就叫做數(shù)字簽名);
(3)A生成臨時(shí)對(duì)稱(chēng)加密鑰,加密數(shù)據(jù)及特征碼;
(4)獲取B的公鑰加密臨時(shí)對(duì)稱(chēng)密鑰;并附加在最后面;

。解密:
(1)B用自己的私鑰解密對(duì)稱(chēng)密鑰;并用它來(lái)解密獲得數(shù)據(jù)和特征碼;(保密性)
(2)用A的公鑰解密特征碼;能解密,身份認(rèn)證;
(3)B用同樣的單向加密算法提取數(shù)據(jù)特征碼;并對(duì)比解密后的特征碼;如相同,數(shù)據(jù)完整性驗(yàn)證;

上述過(guò)程中,存在巨大問(wèn)題:如何可靠的獲取對(duì)方的公鑰?

PKI(Public Key Infrastructure)

  • 公鑰基礎(chǔ)設(shè)施:
    。簽證機(jī)構(gòu):CA
    。注冊(cè)機(jī)構(gòu):RA
    。證書(shū)吊銷(xiāo)列表:CRL
    。證書(shū)存取庫(kù):

  • x509V3定義了證書(shū)的結(jié)構(gòu)和認(rèn)證協(xié)議標(biāo)準(zhǔn):
    。協(xié)議版本號(hào):v1;v2;v3
    。序列號(hào):證書(shū)的編號(hào);
    。簽名算法ID:
    。發(fā)行者的名稱(chēng):CA的名字
    。證書(shū)有效期:
    。主體名稱(chēng):
    。主體公鑰:
    。發(fā)行者的唯一標(biāo)識(shí):
    。主體的唯一標(biāo)識(shí):
    。擴(kuò)展信息:
    。發(fā)行者的簽名:

  • 協(xié)議:

(1)SSL:Secure Socket Layer
。NetScape公司研發(fā);版本:v1/2/3

(2)TLS:Transport Layer Security
。IETF研發(fā);版本:v1.0/1.1/1.2/1.3

  • ssl,tls的開(kāi)源實(shí)現(xiàn):openssl

SSL Handshake Protocol

  • 第一階段:
    Client Hello:要進(jìn)行加密通信
    。支持的協(xié)議版本;tls1.1,tls1.2...
    。客戶(hù)端生成一個(gè)隨機(jī)數(shù),用于稍后生成“對(duì)稱(chēng)密鑰”;
    。支持的加密算法;比如:AES,RSA...
    。支持的壓縮算法;

  • 第二階段:
    Server Hello:
    。確定使用的加密通信協(xié)議版本;
    。服務(wù)器生成一個(gè)隨機(jī)數(shù),用于稍后生成“對(duì)稱(chēng)密鑰”;
    。確定使用的加密算法
    。發(fā)送自己的證書(shū)(CA頒發(fā)的公鑰文件)

  • 第三階段:
    Client:
    。驗(yàn)證Server證書(shū)(發(fā)證機(jī)構(gòu),證書(shū)完整性,證書(shū)持有者,證書(shū)有效期,吊銷(xiāo)列表);在確認(rèn)無(wú)誤后提取Server的公鑰;

發(fā)送以下信息給Server:
。發(fā)送一個(gè)隨機(jī)數(shù)給Server;
。編碼變更通知(表示隨后的信息都將用雙方商定的加密方法和密鑰發(fā)送)
??蛻?hù)端握手結(jié)束通知;

  • 第四階段:
    Server:
    。接受到客戶(hù)端發(fā)來(lái)的第三個(gè)隨機(jī)數(shù)(pre-master-key)后,計(jì)算生成“對(duì)稱(chēng)密鑰”;

發(fā)送以下信息給Clinet:
。編碼變更通知;
。服務(wù)器握手結(jié)束通知;

openssl:

  • 組件:libcrypto,libssl,openssl

  • openssl多用途命令行工具:
    有眾多自命令;分三類(lèi):
    。標(biāo)準(zhǔn)命令;
    。消息摘要命令(dgst子命令)
    。加密命令:enc,ca,req,genrsa...

  • 對(duì)稱(chēng)加密:
    。工具:openssl enc
    。支持的算法:3des,aes,blowfish,twofish...
    加密:openssl enc -e -des3 -a -salt -in /path/from/somefile -out /path/to/somefile
    。解密:openssl enc -d -des3 -a -salt -out /path/to/somefile -in /path/to/somefile

  • 單向加密:
    。工具:openssl dgst {md5sum,sha1sum,sha512sum...}
    openssl dgst -md5 /path/from/somefile
    。生成用戶(hù)密碼:openssl passwd
    openssl passwd -1 -salt RANDOM
    。生成隨機(jī)數(shù):
    工具:openssl rand
    openssl rand -hex NUM
    openssl rand -base64 NUM

注意:生成用戶(hù)密碼可簡(jiǎn)寫(xiě)為:openssl passwd -1 -salt $(openssl rand -hex 4)
。linux系統(tǒng)上的隨機(jī)數(shù)生成器:
/dev/random:僅從熵池返回隨機(jī)數(shù);隨機(jī)數(shù)用盡,阻塞;
/dev/urandom:從熵池返回隨機(jī)數(shù);隨機(jī)數(shù)用盡,會(huì)利用軟件生成偽隨機(jī)數(shù),非阻塞;(偽隨機(jī)數(shù),不安全)
熵池中隨機(jī)數(shù)的來(lái)源:
(1)硬盤(pán)IO中斷時(shí)間間隔
(2)鍵盤(pán)IO中斷時(shí)間間隔

  • 公鑰加密:
    作用:
    1.加解密;算法:RSA,ELGamal;工具:openssl rsautl
    2.數(shù)字簽名:算法:RSA,ELGamal;工具:openssl rsautl
    3.密鑰交換:DH算法

    。生成私鑰:
    (umask 077;openssl genrsa -out /path/to/somefile 1024) ##此處 的數(shù)字1024為2^n;
    。提取公鑰:
    openssl rsa -in /path/to/私鑰文件 -pubout -out xxx.pub

建立私有CA:

。工具:openssl
。openssl配置文件:/etc/pki/tls/openssl.cnf

vim /etc/pki/tls/openssl.cnf
[ ca ]
default_ca  = CA_default        # The default ca section

####################################################################
[ CA_default ]

dir     = /etc/pki/CA       # Where everything is kept ##工作目錄
certs       = $dir/certs        # Where the issued certs are kept ##已經(jīng)簽發(fā)的證書(shū)存放的目錄
crl_dir     = $dir/crl      # Where the issued crl are kept##吊銷(xiāo)的證書(shū)位置
database    = $dir/index.txt    # database index file.##數(shù)據(jù)庫(kù);已經(jīng)頒發(fā)的所有證書(shū)列表
#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##CA自簽證書(shū)
serial      = $dir/serial       # The current serial number##證書(shū)序列號(hào)
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 ##CA的私鑰
RANDFILE    = $dir/private/.rand    # private random number file

x509_extensions = usr_cert      # The extentions to add to the cert

default_days    = 365           # how long to certify for##證書(shū)有效期
default_crl_days= 30            # how long before next CRL
default_md  = sha256        # use SHA-256 by defaultxxx
preserve    = no            # keep passed DN ordering

  • 在準(zhǔn)備配置為CA的服務(wù)器上生成一個(gè)自簽證書(shū);并為CA提供需要的目錄及文件;

1.生成私鑰:
(umask 077;openssl genrsa -out /etc/pki/CA/private/cakey.pem 4096)

2.生成自簽證書(shū):

[root@localhost ~]# openssl req -x509 -new -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3655
//-new:生成新證書(shū)
//-x509:生成自簽證書(shū);CA服務(wù)器專(zhuān)用
//-key:指明私鑰文件的路徑
//-out:生成的請(qǐng)求文件路徑;如果自簽將直接生成簽署過(guò)的證書(shū)

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) []:shanghai
Locality Name (eg, city) [Default City]:shanghai
Organization Name (eg, company) [Default Company Ltd]:ops
Organizational Unit Name (eg, section) []:magedu
Common Name (eg, your name or your server's hostname) []:www.magedu.com
Email Address []:acadmin.magedu.com

3.為CA提供所需的目錄及文件:
mkdir /etc/pki/CA/{certs,crl,newcerts}
touch /etc/pki/CA/{serial,index.txt}
echo 01 > /etc/pki/CA/serial

要用到證書(shū)進(jìn)行安全通信的主機(jī),要向CA服務(wù)器請(qǐng)求簽署證書(shū):

以http服務(wù)為例:
(1)要用到證書(shū)的主機(jī)生成私鑰:
mkidr /etc/httpd/ssl
cd /etc/httpd/ssl;(umask 077;openssl genrsa -out httpd.key 2048)
(2)生成證書(shū)簽署請(qǐng)求:
openssl req -new -key /etc/httpd/ssl/httpd.key -out httpd.csr -days 365
(3)將httpd.csr通過(guò)有效途徑發(fā)送給CA服務(wù)器(scp)
(4)在CA服務(wù)器上簽署證書(shū):
openssl ca -in xxx/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 365
(5)將httpd.crt通過(guò)有效途徑發(fā)送給請(qǐng)求證書(shū)的主機(jī):
scp httpd.crt root@172.16.18.130:/etc/httpd/ssl

  • 查看證書(shū)信息:
    cd /etc/pki/CA/certs/;openssl x509 -in httpd.crt -noout -serial -subject serial=01 subject= /C=cn/ST=shanghai/O=magedu/OU=ops/CN=www.magedu.com/emailAddress=webadmin@magedu.com
最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • CA和證書(shū)安全協(xié)議(SSL/TLS)OpenSSH 一、CA和證書(shū) (一) PKI(Public Key Infr...
    哈嘍別樣閱讀 1,518評(píng)論 0 0
  • 似乎不管看什么書(shū),最后都會(huì)走上查據(jù)考證這條路。 舉個(gè)最近看到的例子,西安是十一朝古都?還是十三朝古都?常識(shí)上普遍認(rèn)...
    王家人寧閱讀 479評(píng)論 0 0
  • 玲異奮作者 2017.08.12 15:40 打開(kāi)App 最后是今天老師要求的作業(yè) 1,高血壓的危害 答:高血壓對(duì)...
    異玲奮閱讀 350評(píng)論 0 1
  • 你在自己的故事里流汗, 涼風(fēng)在它的故事里乘涼。 你想乘涼, 涼風(fēng)不想流汗……
    小劇在成長(zhǎng)閱讀 209評(píng)論 0 6
  • 一年前開(kāi)始在簡(jiǎn)書(shū)寫(xiě)字,記錄自己在生活中的所見(jiàn)所聞、所思所思,這里面有快樂(lè)、有悲傷、有感動(dòng)、有頓悟。 我從未寫(xiě)出過(guò)稱(chēng)...
    宋宋的白銀時(shí)代閱讀 2,180評(píng)論 13 122

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