17、加密解密技术介绍
1、通訊機(jī)制:
????同一主機(jī)間通訊機(jī)制:IPC、message quence,shm(共享內(nèi)存),semerphone
????不同主機(jī)間通訊機(jī)制:socket(ip:port),一般來說socket通訊都是成對出現(xiàn):CIP:PORT<--->SIP:PORT
2、傳輸層協(xié)議:
tcp、udp、sctp;傳輸層協(xié)議單元為段,用端口區(qū)分不同應(yīng)用 ? ?
port用來標(biāo)識進(jìn)程地址;進(jìn)程向內(nèi)核注冊使用某端口(獨(dú)占的)
3、如何保證安全的信息通訊?
????ssl:secure socket layer:安全套接字層,ssl是一種規(guī)范協(xié)議
4、信息安全目標(biāo):
????保密性:confidentiality;確保通訊的保密性
????完整性:integrity;確保信息的完整性,體現(xiàn)在數(shù)據(jù)的完整性和系統(tǒng)完整性兩個層面
????有效性(可用性):
5、***類型:
????威脅保密性的***:竊聽、通信量分析
????威脅完整性的***:更改、偽裝、重放、否認(rèn)
????威脅可用性的***:決絕服務(wù)
6、安全實(shí)現(xiàn)手段:安全通訊解決方案
????通過技術(shù)實(shí)現(xiàn):加密和解密 ? ????
????????傳統(tǒng)加密方法:替代加密、置換加密
????????現(xiàn)代加密方法:現(xiàn)代塊加密技術(shù),將存在硬盤上的數(shù)據(jù),分成塊進(jìn)行加密。 ??
????通過服務(wù)實(shí)現(xiàn):用于抵御***的服務(wù),也就是說為了實(shí)現(xiàn)上述安全目標(biāo)而特地設(shè)計(jì)的安全服務(wù)。
????????認(rèn)證機(jī)制
????????訪問控制機(jī)制
????????秘鑰算法和協(xié)議(專用于密碼交換)
6.1、linux系統(tǒng)實(shí)現(xiàn)上述解決方案工具主要有兩個:openssl、GPG
? ?openssl由三部分組成:
????libencrypt庫:實(shí)現(xiàn)加密解密的庫
????libssl庫:實(shí)現(xiàn)ssl安全通信的庫
????openssl工具:命令行工具,多用途,可以實(shí)現(xiàn)前兩者的功能
6.2、密鑰算法和協(xié)議:
????對稱加密:主要用來加密數(shù)據(jù);DES、3DES、AES
????公鑰加密:主要用來加密對稱加密密鑰;RSA、DSA
????單向加密:主要用來提取數(shù)據(jù)特征碼;md5、sha1、sha224、sha256、sha384、sha512
????認(rèn)證協(xié)議:主要用來進(jìn)行密鑰交換
6.2.1、對稱加密: ????加密和解密使用同一個密鑰,但是加密和解密的算法可能不同。 ????對稱加密算法主要有:des(56位加密技術(shù))、3des、aes(advance encryption standard;128bits、192bits、256bits、384bits)、blowfish、twofish、idea、rc6等等;常用的是3des和aes。 ????對稱加密特性: ????????①、加密解密使用同一個密鑰 ????????②、將原始的數(shù)據(jù)分隔成固定的大小的塊,逐個進(jìn)行加密 ????對稱加密的缺陷: ????? ? ?①密鑰過多(體現(xiàn)在服務(wù)端和眾多客戶端通訊時,需要每個通訊都有一個單獨(dú)的密鑰) ????? ? ?②密鑰分發(fā)困難 |
6.2.2、公鑰加密: ????密鑰成對出現(xiàn),有公鑰和私鑰。 ? ? ? ?公鑰:publickey,公開給所有人;公鑰從私鑰中提取產(chǎn)生 ????? 私鑰:secretkey:通過工具創(chuàng)建;使用者自己留存,必須保證其私密性。 ????公鑰加密特性: ????????①、用公鑰加密的數(shù)據(jù)只能由對應(yīng)的私鑰進(jìn)行機(jī)密,反之亦然。 ????公鑰加密用途: ????????①、數(shù)字簽名:主要在于讓接收方確認(rèn)發(fā)送發(fā)的身份。 ????????②、密鑰交換:發(fā)送方用對方公鑰加密一個對稱密鑰,發(fā)送給對方。 ????????③、數(shù)據(jù)加密:主要加密對稱密鑰 數(shù)字簽名的實(shí)現(xiàn)過程: ? 以a用戶和b用戶通訊為例 ????①、a用戶發(fā)送數(shù)據(jù)給b用戶,a需要用到單向算法提取發(fā)送數(shù)據(jù)的特征碼,由于單向算法有雪崩效應(yīng),改動很小一部分,特征碼就會發(fā)生很大變化 ????②、a用戶用自己的私鑰加密這段特征碼,并將其附加在這段數(shù)據(jù)的后面(如果用私鑰加密原始數(shù)據(jù),消耗內(nèi)存、cpu) ????③、a用戶生成一個臨時的對稱加密密鑰,并使用這個密鑰加密原始數(shù)據(jù)和特征碼,生成的新的數(shù)據(jù) ????④、a用戶獲取b用戶的公鑰,并用b用戶的公鑰加密a的對稱密碼,將其附在③中新生成數(shù)據(jù)包的后面,然后將整個數(shù)據(jù)包發(fā)送給b用戶。 ????⑤、b用戶用自己的私鑰去解密a用戶用b的公鑰加密的數(shù)據(jù),得到③中的數(shù)據(jù)和④中的加密后的對稱密鑰并將其解密成明文對稱密鑰。 ????⑥、b用戶用a的對稱密鑰解密③中的數(shù)據(jù),得到原始數(shù)據(jù)和原始數(shù)據(jù)提取的加密后的特征碼 ????⑦、b用戶用a的公鑰去解密②中的a私鑰加密的特征碼,如果可以解密,a用戶的身份得到驗(yàn)證。 ????⑧、b用同樣的單向加密算法去計(jì)算原始數(shù)據(jù)的特征碼,如果一樣,完整性得到了驗(yàn)證,否則說明數(shù)據(jù)被篡改過。 其中①-④說明了加密的過程,⑤-⑧說明了數(shù)字簽名和完整性驗(yàn)證。但是,此處有個問題,怎么確保對方的公鑰是安全的,就是他們需要發(fā)送對方的呢?因此有了第三方公證機(jī)構(gòu)(CA:證書頒發(fā)機(jī)構(gòu)) ????公鑰加密常用算法: ????????DSA:只能用于實(shí)現(xiàn)數(shù)字簽名,不能實(shí)現(xiàn)加密解密 ????????RSA:既能實(shí)現(xiàn)數(shù)字簽名又能實(shí)現(xiàn)加密、解密。? ??? ?? |
6.2.3、單向加密:只能實(shí)現(xiàn)加密,不能實(shí)現(xiàn)解密,用于提取數(shù)據(jù)指紋 ????單向加密特征: ????????定長輸出、雪崩效應(yīng) ????單向加密功能: ????????用于實(shí)現(xiàn)完整性 ????單向加密算法: ????????md5:messagedigest,消息摘要,5是版本號,128bits定長輸出 ????????sha1:secure hash algorithm,1是版本號,160bits定長輸出 ????????sha224、sha256、sha384、sha512 ????注意:對于centos6、7來說,密碼加密已從centos5中的md5切換到sha512了。 |
6.2.4、認(rèn)證協(xié)議:和加密算法相關(guān)的協(xié)議,密鑰交換,IKE(internet key exchange) ????密鑰交換:IKE ????????公鑰加密:密鑰在網(wǎng)上傳輸 ????????DH(diffie-hellman):密鑰不在網(wǎng)上傳輸,更安全些 |
6.3、PKI:pulic keyinfrastructure;公鑰基礎(chǔ)設(shè)施
????? 組件如下:
????????簽證機(jī)構(gòu):CA
????????注冊機(jī)構(gòu):RA
????????證書吊銷列表:CRL
????????證書存取庫:CB
?
證書:數(shù)字證書標(biāo)準(zhǔn)(x.509),它定義了證書的結(jié)構(gòu)以及認(rèn)證協(xié)議標(biāo)準(zhǔn)
? 證書組成部分:
? ? ①版本號
? ? ②序列號
? ? ③簽名算法ID
? ? ④發(fā)行者名稱
? ? ⑤有效期限
? ? ⑥主體名稱
? ? ⑦主體公鑰
? ? ⑧發(fā)行者的唯一標(biāo)識
? ? ⑨主體的唯一標(biāo)識
? ? ⑩拓展信息
? ? 11.發(fā)行者的簽名
?其中①-⑤為簽證機(jī)構(gòu)信息,⑥-⑩為個人信息,11為發(fā)證機(jī)構(gòu)
7、SSL和TLS協(xié)議:
ssl:secure socket layer,安全套接字層在ip層和傳輸層之間銜接,有網(wǎng)警公司開發(fā)
????ssl v1.0(已廢棄)、ssl v2.0(已廢棄)、ssl v3.0
tls:transport layer security,傳輸層協(xié)議安全,由ITEF研發(fā)
????tls v1.0 、tls v1.1 、tls v1.2 、tls v1.3(草案)? ?
tls分層設(shè)計(jì):
????最底層:基礎(chǔ)算法原語的實(shí)現(xiàn),aes,rsa,MD5
????向上一層:各種算法的實(shí)現(xiàn)
????再向上一層:組合算法實(shí)現(xiàn)的半層品
????最頂層:用各種組件拼裝而成的各種成品密碼學(xué)協(xié)議軟件。
注意:實(shí)現(xiàn)ssl協(xié)議和tls協(xié)議的軟件的有很多,著名的開源軟件有:openssl。
8、openssl組件:
????libcrypto庫:實(shí)現(xiàn)加密解密,主要由開發(fā)者使用
????libssl庫:實(shí)現(xiàn)ssl功能,主要由開發(fā)者使用
????openssl:多用途的明林工具
9、ssl會話步驟:
????客戶端向服務(wù)端索要并驗(yàn)證證書
????雙方協(xié)商生成“會話密鑰”
????雙方采用“會話密鑰”進(jìn)行加密通信
? ssl會話流程:ssl handshank,有四個階段
????第一階段:client hello
????????向服務(wù)器端發(fā)送①、支持的協(xié)議版本,比如tls1.2;②、客戶端生成一個隨機(jī)數(shù),稍后用戶生成“會話密鑰”,③、支持的加密算法,比如rsa、aes,④、支持的壓縮算法
????第二階段:server hello??
????????①、確認(rèn)使用的加密通信協(xié)議版本,比如tls1.2
????????②、服務(wù)器端生成一個隨機(jī)數(shù),稍后用于生成“會話密鑰”
????????③、確認(rèn)使用的加密算法
????????④、服務(wù)端發(fā)送自己的證書
????????⑤、索要客戶端證書(可省)
????第三階段:client端收到serverhello后的回應(yīng)
????????①、驗(yàn)證服務(wù)器證書,(證書發(fā)放機(jī)構(gòu)、證書完整性、證書持有者、證書有效性、吊銷列表等)
????????②、確認(rèn)以上信息無誤后,取出其公鑰
????????③、發(fā)送以下信息給服務(wù)器端:
????????????一個隨機(jī)數(shù):用于服務(wù)器上的公鑰加密
????????????編碼變更通知:表示隨后信息都將用雙方協(xié)定的加密算法加密后發(fā)送
????????????客戶端握手結(jié)束通知
????
????第四階段:服務(wù)端收到客戶端發(fā)送來的信息
????????①、收到客戶端發(fā)送來的第二個隨機(jī)數(shù),pre-master-key后,計(jì)算生成本次會話所用到的“會話密鑰”
????????②、向客戶端發(fā)送以下信息:
????????????編碼變更通知:表示隨后的信息都將用雙方協(xié)定的加密方法和密鑰發(fā)送
????????????服務(wù)端握手結(jié)束通知
? ? ? ?
10、openssl命令:
? 命令格式:openssl command [ command_opts ] [ command_args ]
????openssl命令有如下三類:
????????標(biāo)準(zhǔn)命令
????????消息摘要命令
????????加密命令
[root@localhost httpd]# openssl ? openssl:Error: '?' 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 ? ? ? ? ? ? ? [root@localhost httpd]#? |
10.1、openssl實(shí)現(xiàn)對稱加密:
?openssl enc -ciphername [-in filename] [-out filename] [-pass arg] [-e] [-d] [-a/-base64] [-A] [-k password] [-kfile filename] [-K key] [-iv IV] [-S salt] [-salt] [-nosalt] ? ? ? ? ?[-z] [-md] [-p] [-P] [-bufsize number] [-nopad] [-debug] [-none] [-engine id] ? ?
????工具:openssl enc
????支持算法:3des、aes、blowfish、towfish
????加密示例:
? ? ? ? [root@localhost sh]# openssl enc -e -des3 -a -salt -in /etc/fstab -out /tmp/fstab.cipertext ? //-in:要加密的文件,-out:加密后的文件,-e:加密
????解密示例:
???? ? [root@localhost sh]# openssl enc -d -des3 -a -salt -in /tmp/fstab.cipertext -out /tmp/fstab.plain ?//-d:解密
10.2、openssl實(shí)現(xiàn)單向加密:
?openssl dgst [-md5|-md4|-md2|-sha1|-sha|-mdc2|-ripemd160|-dss1] [-c] [-d] [-hex] [-binary] [-out filename] [-sign filename] [-keyform arg] [-passin arg] [-verify filename] ????? ??[-prverify filename] [-signature filename] [-hmac key] [file...]
???????
????? ??[md5|md4|md2|sha1|sha|mdc2|ripemd160] [-c] [-d] [file...]
????
????工具:openssl dgst
????支持的算法:md5sum、sha1sum、sha224sum、...
????加密示例:
? ? ??[root@localhost sh]# openssl dgst -md5 ? /etc/fstab
? ? ??MD5(/etc/fstab)= ec11d9db3a8d8a8dabfe0d98af76ac46
? ? ??[root@localhost sh]# md5sum /etc/fstab?
? ? ??ec11d9db3a8d8a8dabfe0d98af76ac46 ?/etc/fstab
? ? ??[root@localhost sh]#?
????生成用戶密碼工具:passwd、openssl passwd
????????[root@localhost sh]# man sslpasswd????
????????openssl passwd [-crypt] [-1] [-apr1] [-salt string] [-in file] [-stdin] [-noverify] [-quiet] [-table] {password} ? //salt:隨機(jī)數(shù)
?????示例:
????????[root@localhost sh]# openssl passwd ?-1 -salt 12345678 ? //-1:表示md5加密,salt表示隨機(jī)數(shù)
????????Password: ? ?
????????$1$12345678$KShFLCowWE89oV0dKisl2/
????????[root@localhost sh]#?
????????[root@localhost sh]# openssl passwd -1 -salt $(openssl rand -hex 5)
????????Password:?
????????$1$cccd731d$6j3DLRtJZqurDiprU/7oz0
????????[root@localhost sh]#?
????生成隨機(jī)數(shù):工具openssl rand
???? 查看rand在哪些章節(jié)有幫助文檔
????????[root@localhost sh]# whatis rand
????????rand (3) ? ? ? ? ? ? - pseudo-random number generator
????????sslrand (1ssl) ? ? ? - generate pseudo-random bytes
????????rand (3p) ? ? ? ? ? ?- pseudo-random number generator
????????[root@localhost sh]#?
????rand命令用法:openssl rand [-out file] [-rand file(s)] [-base64] [-hex] num ? //base64表示:用ASCII碼表示輸出的文件(A-Z),-hex表示僅以16進(jìn)制(1-F)表示數(shù)據(jù)
????示例:
????????[root@localhost sh]# openssl rand ?-base64 16
????????QbOTMmR3cpp5+Y2eKIoN3g==
????????[root@localhost sh]#?
????????[root@localhost sh]# openssl rand ?-hex 16
????????f41d01d6774f77029dd8ea46ca3214e8
????????[root@localhost sh]#?
10.3、openssl實(shí)現(xiàn)公鑰加密:
????公鑰加密的三種功能:
????????加密、解密:(算法:rsa;工具:openssl rsautl)
????????數(shù)字簽名:(算法:rsa、dsa,工具:rsautl)
????????密鑰交換:(算法:D-H)
? ? ? ?
????生成密鑰步驟:注意:生成密鑰對于公鑰加密來說,公鑰不是生成的,是從私鑰中提取出來的 。
? ? ?①、生成私鑰:
????????[root@localhost sh]# (umask 077;openssl genrsa -out /a.key 50) ?//命令放在括號中運(yùn)行,表示在當(dāng)前shell的子shell中運(yùn)行,運(yùn)行完則退出,避免對當(dāng)前shell的影響;-out指出生成的密鑰存儲的位置,50:表示聲場密鑰的位長。
????????Generating RSA private key, 50 bit long modulus
????????.+++++++++++++++++++++++++++
????????.+++++++++++++++++++++++++++
????????e is 65537 (0x10001)
????????[root@localhost sh]# cat /a.key?
????????-----BEGIN RSA PRIVATE KEY-----
????????MDcCAQACBwNeIm8b7bkCAwEAAQIHAPfq8fZg4QIEAdutBQIEAc/8JQIEANpH6QID
????????P5cZAgQAtovS
????????-----END RSA PRIVATE KEY-----
????????[root@localhost sh]#?
????②、生成公鑰:從私鑰中提取。
????????[root@localhost sh]# openssl rsa -in /a.key -pubout ? ? //-in:指定從哪個文件提取公鑰,-pubout:提取并展示出
????????writing RSA key
????????-----BEGIN PUBLIC KEY-----
????????MCIwDQYJKoZIhvcNAQEBBQADEQAwDgIHA14ibxvtuQIDAQAB
????????-----END PUBLIC KEY-----
????????[root@localhost sh]#?
????------------------------------------------------------------------------------------------------------????
? ? ?linux系統(tǒng)上隨機(jī)數(shù)生成器:
????????/dev/random:僅從熵池中返回隨機(jī)數(shù),隨機(jī)數(shù)用盡,進(jìn)程阻塞
????????/dev/urandom:從熵池中返回隨機(jī)數(shù),隨機(jī)數(shù)用盡,會利用軟件生成偽隨機(jī)數(shù),不阻塞進(jìn)程。但是這中不安全。
????
????????熵池中隨機(jī)數(shù)來源:
????????????①、硬盤I/0中斷時間間隔
????????????②、鍵盤I/O中斷時間間隔 ? ? ?
? ?
10.4、CA創(chuàng)建
????CA有兩種類型:公共信任CA、私有CA。
????
????建立私有CA的方法:openssl命令、openca命令
????
????CA的配置文件:[root@localhost sh]# cat /etc/pki/tls/openssl.cnf?
? ??
?構(gòu)建私有CA步驟:
????①、在確定配置為CA的服務(wù)器上生成一個自簽證書 ②、并為CA提供所需要的目錄及文件即可
?私有CA配置示例:
? ?①、生成密鑰放在/etc/pki/CA/private/目錄下?,文件名必須為/etc/pki/tls/openssl.cnf中配置的名稱。???????
????[root@localhost CA]# (umask 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem 40)
????[root@localhost CA]# ls /etc/pki/CA/private/ -l ? //確保生成的密鑰文件的權(quán)限為600
????total 4
????-rw-------. 1 root root 132 Feb ?3 10:21 cakey.pem
????[root@localhost CA]#?
??
?
? ?②、生成自簽證書(發(fā)送申請證書申請):按配置文件要求配置(certificate = /etc/pki/CA/cacert.pem )
? ? ? ? req:證書請求及證書生成工具。
? ?[root@localhost private]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 365
? ??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]:magedu
? ??Organizational Unit Name (eg, section) []:ops
? ??Common Name (eg, your name or your server's hostname) []:www2.stux.com ?//此處要寫成客戶端訪問的服務(wù)的主機(jī)信息,
? ??Email Address []:
? ? 命令解釋:
? ? ? ? -new:生成新證書簽署請求
? ? ? ? -x509:生成自簽格式證書,專用于創(chuàng)建私有CA時,此處如果不自簽,不需要加此項(xiàng),但是自簽,必須要寫此項(xiàng)
? ? ? ? -key:生成請求時用到的私鑰文件路徑
? ? ? ? -out:生成的請求文件路徑;如果自簽操作將直接生成簽署過的證書
? ? ? ? -days:證書的有效時長,單位是days。
? ? ③、為ca提供所需要的目錄(certs 、crl、 newcerts)和文件(serial、index.txt),如果這幾個個文件不存在 則需要手工創(chuàng)建
? ??[root@localhost CA]# mkdir -pv?/etc/pki/CA/{certs,crl,newcerts}
? ??[root@localhost CA]# touch ?/etc/pki/CA/{serial,index.txt} ? ?//serial:證書序列號,index.txt:證書數(shù)據(jù)庫? ?
? ??[root@localhost CA]#?echo 01 >?/etc/pki/CA/serial ? //寫入一個證書序列號。
?至此,私有ca創(chuàng)建完成。
要用到證書進(jìn)行安全通信的服務(wù)器,需要向CA請求簽署證書,比如https服務(wù)器,步驟如下:
? ? ①、用到證書的服務(wù)器主機(jī)生成私鑰(以https主機(jī)為例)
? ??[root@localhost CA]# mkdir /etc/httpd/ssl?
? ??[root@localhost CA]# cd /etc/httpd/ssl
? ??[root@localhost ssl]# (umask 077;openssl genrsa -out httpd.key 2048)
? ? ②、用到證書的服務(wù)器主機(jī)生成證書簽署請求:(因?yàn)椴皇亲院?#xff0c;所以不用加-x509)
? ? [root@localhost ssl]# openssl req -new -key httpd.key -out httpd.csr -days 365?
? ? ③、將請求通過可靠方式發(fā)送給ca主機(jī),在CA主機(jī)上進(jìn)行簽署。由于是在同一臺主機(jī)上,因此,直接簽署。
? ??[root@localhost ssl]# openssl ca -in httpd.csr -out /etc/pki/CA/httpd.crt -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: Feb 7 23:22:12 2018 GMT
? ? ? ? ? ? ??? Not After : Feb 7 23:22:12 2019 GMT
? ? ? ?? ?? Subject:
? ? ? ? ? ?? ?? countryName = cn
? ? ? ? ? ?? ?? stateOrProvinceName = beijing
? ? ? ? ? ?? ?? organizationName = magedu
? ? ? ? ? ?? ?? organizationalUnitName = ops
? ? ? ? ? ?? ?? commonName = www.magedu.com
? ? ? ? ? ??X509v3 extensions:
? ? ? ? ? ? ? ??X509v3 Basic Constraints:
? ? ? ? ? ? ? ? ? ??CA:FALSE
? ? ? ? ? ? ? ??Netscape Comment:
? ? ? ? ? ? ? ? ? ??OpenSSL Generated Certificate
? ? ? ? ? ? ? ??X509v3 Subject Key Identifier:
? ? ? ? ? ? ? ? ? ??93:33:80:F7:C5:12:6E:2A:22:92:E4:0C:70:56:00:D5:C4:76:6E:70
? ? ? ? ? ?? ?? X509v3 Authority Key Identifier:
? ? ? ? ? ? ? ? ? ??keyid:77:55:41:C6:1D:5E:77:43:F8:9E:C0:2E:67:75:87:F4:2F:FE:0E:CC
? ??Certificate is to be certified until Feb 7 23:22:12 2019 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 ssl]#
? ? ④、查看證書信息:(可以在ca和客戶機(jī)上查看,只要有ca的主機(jī)都可以查看)
? ??[root@localhost ssl]# cat /etc/pki/CA/index.txt
? ??V 190207232212Z 01 unknown /C=cn/ST=beijing/O=magedu/OU=ops/CN=www.magedu.com
? ??[root@localhost ssl]#
? 或 [root@localhost CA]# openssl x509 -in httpd.crt -noout -serial -subject
? ??serial=01
? ??subject= /C=cn/ST=beijing/O=magedu/OU=ops/CN=www.magedu.com
? ??[root@localhost CA]#
? ? ⑤、將CA簽署過的證書發(fā)送給請求的服務(wù)器。由于是本機(jī)簽署,所以不發(fā)送,拷貝簽署的證書到指定位置。注意:此時的httpd.csr(請求簽署文件)可以刪除了
? ??[root@localhost CA]# cp /etc/pki/CA/httpd.crt ?/etc/httpd/ssl/? ??
?? ?
10.5、吊銷證書:一般私有ca搭建,不用吊銷,如果要吊銷,一般會用openca工具實(shí)現(xiàn)。
? ? ①、客戶端獲取要吊銷證書的serial(序列號),(在使用證書的主機(jī)上執(zhí)行)
? ??[root@localhost ssl]# openssl x509 -in /etc/httpd/ssl/httpd.crt -noout -serial -subject
? ??serial=01
? ??subject= /C=cn/ST=beijing/O=magedu/OU=ops/CN=www.magedu.com
? ??[root@localhost ssl]#
? ? ②、CA端主機(jī)吊銷證書
? ? 先根據(jù)客戶提交的serial和subject信息,對比與本機(jī)數(shù)據(jù)庫index.txt中存儲的是否一致。
? ? [root@localhost ssl]# openssl ca -revoke /etc/pki/CA/newcerts/01.pem
? ??Using configuration from /etc/pki/tls/openssl.cnf
? ??Revoking Certificate 01.
? ??Data Base Updated
? ??[root@localhost ssl]#
? ? ③、生成吊銷證書編號(第一次吊銷證書時執(zhí)行)
? ? [root@localhost ssl]# echo 01 > /etc/pki/CA/crlnumber
? ? ④、更新證書吊銷列表
? ??[root@localhost CA]# openssl ca -gencrl -out thisca.crl
? ? ⑤、查看crl文件:
? ? #openssl crl -in /PATH/FROM/CRL_FILE.crl -noout -text
? ?
轉(zhuǎn)載于:https://blog.51cto.com/10631377/2066212
總結(jié)
以上是生活随笔為你收集整理的17、加密解密技术介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 奥数之“牛吃草问题”C++实现
- 下一篇: 深入理解JVM-垃圾回收器