linux搭建ssl代理(Linux安全之SSL協議)
2023-10-16 12:51:30
ssl(Secure Sockets Layer 安全套接字協議),及其繼任者傳輸層安全(Transport Layer Security,TLS)是為網絡通信提供安全及數據完整性的一種安全協議。tls 與 SSL 在傳輸層與應用層之間對網絡連接進行加密。
Linux安全之SSL協議
1. 密碼學基礎密碼算法和協議
對稱加密公鑰加密單向加密認證協議1.1 對稱加密對稱加密:加密和解密使用同一個密鑰
常見算法
DES3DESAESBlowfishTwofishIDEARC6CAST5特性
加密、解密使用同一個密鑰將原始數據分割成固定大小的塊,逐個進行加密缺陷
密鑰過多密鑰分發1.2 公鑰加密公鑰加密:密鑰是成對兒出現
常見算法
RSADSAELGamal加密介紹
公鑰pubkey公開給所有人私鑰secret key自己留存,必須保證其私密性特點用公鑰加密的數據,只能使用與之配對兒的私鑰解密,反之亦然1.3 單向加密單向加密:只能解密,不能解密,提取數據指紋
算法
md5: 128bitssha1: 160bitssha224sha256sha384sha512特性
定長輸出雪崩效應功能
完整性1.4 PKI 和 SSLPKI是Public Key Infrastructure首字母的一個縮寫,表示公鑰基礎設施,主要是用於認證的。
X.509定義了證書的結構以及認證協議標準現在我們使用的為ssl的第三版
PKI 中的相關角色
籤證機構:CA註冊機構:RA證書吊銷列表:CRL證書存取庫X.509
版本號序列號籤名算法ID發行者名稱有效期限主體名稱主體公鑰發行者惟一標識主體的惟一標識擴展發行者籤名SSL 版本
**SSL**:Secure Socket Layer**TLS**:Transport Layer SecuritySSLTLSSSL 的分層設計
最低層:基礎算法原語的實現aes, rsa, md5向上一層各種算法的實現再向上一層組合算法實現的半成品用各種組件拼裝而成的種種成品密碼學協議/軟體tls, ssh
Linux安全之SSL協議
2. openssl**OpenSSL**是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程式供測試或其它目的使用。
OpenSSL功能強大,可以進行加解密OpenSSL也可以進行PKI的認證機制
OpenSSL 有兩種運行模式
交互模式直接輸入openssl回車進入交互模式批處理模式輸入帶命令選項的openssl進入批處理模式2.1 openssl 的三個組件openssl多用途的命令行工具libcrypto公共加密庫,實現了各種各樣的加密算法libssl也是一個庫,實現了ssl及tls2.2 PKI 中的相關角色籤證機構:CA註冊機構:RA證書吊銷列表:CRL證書存取庫2.3 證書申請及籤署步驟(1)生成申請請求,需要輸出相關信息(2)RA核驗(3)CA籤署(4)獲取證書2.4 創建私有 CA 的方法實現工具
創建私有CA的專業開源工具OpenCA創建私有CA的簡要實現工具OpenSSL,這裡選擇後者配置文件
這裡以CentOS6中進行配置openssl的配置文件:/etc/pki/tls/openssl.cnf創建私有 CA 的步驟
(1) 創建所需要的文件# 伺服器上配置[root@localhost ~]# cd /etc/pki/CA[root@localhost CA]# lscerts crl newcerts private[root@localhost CA]# touch index.txt; echo 01 > serial[root@localhost CA]# lscerts crl index.txt newcerts private serial
(2) CA 自籤證書# 伺服器上配置# 表示在子shell中運行,不會影響父SHELL的環境,這裡生成秘鑰對# -new: 生成新證書籤署請求# -x509: 專用於CA生成自籤證書# -key: 生成請求時用到的私鑰文件# -days n: 證書的有效期限# -out /PATH/TO/SOMECERTFILE: 證書的保存路徑# 創建私鑰文件[root@localhost CA]# (umask 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)Generating RSA private key, 2048 bit long modulus................................................................................................................................................ ....... e is 65537 (0x10001)[root@localhost CA]# ls -l private/總用量 4-rw-------. 1 root root 1679 6月 26 11:35 cakey.pem# 生成CA自籤證書,只有CA才可以自籤署# 注意這裡的Common Name必須和放置CA的主機名一致[root@localhost CA]# openssl req -new -x509 -key private/cakey.pem -days 7300 -out cacert.pemYou are about to be asked to enter information that will be incorporatedinto 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 blankFor some fields there will be a default value,If you enter '.', the field will be left blank.-----Country Name (2 letter code) [XX]:CNState or Province Name (full name) []:BeijingLocality Name (eg, city) [Default City]:BeijingOrganization Name (eg, company) [Default Company Ltd]:wsescapeOrganizational Unit Name (eg, section) []:OpsCommon Name (eg, your name or your server is hostname) []:ca.wsescape.comEmail Address []:[email protected][root@localhost CA]# lscacert.pem certs crl index.txt newcerts private serial
(3) 給應用發證書# (a) 用到證書的主機生成證書請求# 客戶端上配置[root@localhost ~]# cd /etc/httpd/[root@localhost httpd]# mkdir ssl; cd ssl# 生成httpd的私鑰文件httpd.key[root@localhost ssl]# (umask 077; openssl genrsa -out /etc/httpd/ssl/httpd.key 2048)# 用httpd.key的私鑰文件生成一個公鑰文件httpd.csr# 這裡填寫的數據和伺服器中的信息需要一致才可能夠使用[root@localhost ssl]# openssl req -new -key /etc/httpd/ssl/httpd.key -days 365 -out /etc/httpd/ssl/httpd.csr
# (b) 把請求文件傳輸給CA# 客戶端上配置# 172.16.100.6為伺服器CA地址[root@localhost ssl]# scp httpd.csr [email protected]:/tmp/
# (c) CA籤署證書,並將證書發還給請求者# 伺服器上配置# 172.16.100.9為客戶端配置[root@localhost ~]# openssl ca -in /tmp/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 365[root@localhost ~]# scp /tmp/httpd.csr [email protected]:/etc/httpd/ssl/# 查看證書中的信息[root@localhost ~]# openssl x509 -in /PATH/FROM/CERT_FILE -noout -text|-subject|-serial
(4) 吊銷證書# (a) 客戶端獲取要吊銷的證書的serialopenssl x509 -in /PATH/FROM/CERT_FILE -noout -serial -subject# (b) CA端執行以下步驟# 先根據客戶提交的serial與subject信息,對比檢驗是否與index.txt文件中的信息一致# 吊銷證書openssl ca -revoke /etc/pki/CA/newcerts/SERIAL.pem# (c) 生成吊銷證書的編號(第一次吊銷一個證書)echo 01 > /etc/pki/CA/crlnumber# (d) 更新證書吊銷列表openssl ca -gencrl -out thisca.crl# 查看crl文件openssl crl -in /PATH/FROM/CRL_FILE.crl -noout -text
2.5 PKI 的配置文件[root@localhost ~]# cat /etc/pki/tls/openssl.cnf...####################################################################[ ca ]default_ca = CA_default # The default ca section####################################################################[ CA_default ]dir = /etc/pki/CA # Where everything is keptcerts = $dir/certs # Where the issued certs are keptcrl_dir = $dir/crl # Where the issued crl are keptdatabase = $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 certificateserial = $dir/serial # The current serial numbercrlnumber = $dir/crlnumber # the current crl number # must be commented out to leave a V1 CRLcrl = $dir/crl.pem # The current CRLprivate_key = $dir/private/cakey.pem# The private keyrandFILE = $dir/private/.rand # private random number filex509_extensions = usr_cert # The extentions to add to the cert# Comment out the following two lines for the "traditional"# (and highly broken) format.name_opt = ca_default # Subject Name optionscert_opt = ca_default # Certificate field options# Extension copying option: use with caution.# copy_extensions = copy# Extensions to add to a CRL. Note: Netscape communicator chokes on V2 CRLs# so this is commented out by default to leave a V1 CRL.# crlnumber must also be commented out to leave a V1 CRL.# crl_extensions = crl_extdefault_days = 365 # how long to certify fordefault_crl_days = 30 # how long before next CRLdefault_md = default # use public key default MDpreserve = no # keep passed DN ordering...
3. OpenSSL 命令分為三類
標準命令消息摘要命令加密命令3.1 對稱加密要點
工具:openssl enc, gpg算法:3des, aes, blowfish, twofishenc命令
enc是openssl的一個子命令,可以用來實現對稱加密-e加密-d解密-salt表示添加雜質進去-in需要加密或者解密的文件-out需要輸出的文件文件名稱這裡的加密算法可以自己指定使用方式
幫助信息man enc加密openssl enc -e -des3 -a -salt -in fstab -out fstab.ciphertext解密openssl enc -d -des3 -a -salt -in fstab.ciphertext -out fstab實例展示
[root@localhost tset]# cat mimaI love you[root@localhost tset]# openssl enc -e -des3 -a -salt -in mima -out jiamienter des-ede3-cbc encryption password:Verifying - enter des-ede3-cbc encryption password:[root@localhost tset]# cat jiamiU2FsdGVkX1 OK5GvMnyeZpST9ncIuIXU9zM FzdKJmM=[root@localhost tset]# openssl enc -d -des3 -a -salt -in jiami -out jiemienter des-ede3-cbc decryption password:[root@localhost tset]# cat jiemiI love you
3.2 單向加密工具
md5sumsha1sumsha224sumsha256sumopenssl dgstdgst命令
要點dgst命令是openssl的一個子命令,可以用來實現單向加密幫助man dgst格式openssl dgst -md5 /PATH/TO/SOMEFILE實戰演示
# 這裡可以指定-out保存到文件中[root@localhost tset]# openssl dgst -md5 mimaMD5(mima)= ffa9b546d36ae095e2a4252a4981a942
3.3 單向加密的用途單向加密可以實現下面三種方式的用途
(1)MAC 算法
特點
Message Authentication Code單向加密的一種延伸應用,用於實現在網絡通信中保證所傳輸的數據的完整性機制
CBC-MACHMAC:使用md5或sha1算法在集群中用於各節點之間認證的加密機制(2)生成用戶密碼
特點
使用openssl的子命令passwd實現這裡加-salt的加密密碼就是Linux下給用戶設置密碼的機制相同使用
幫助man sslpasswd格式openssl passwd -1 -salt SALT實戰演示
# -1表示MD5[root@localhost tset]# openssl passwd -1 -salt 123456Password:$1$123456$I.fsDBpqtKp/xeL5gqYrz/
(3)生成隨機數
使用
幫助man sslrand格式openssl rand [-base64|-hex] NUM-base64表示-base64編碼-hex表示16進位編碼NUM: 表示輸出的字節數,除以2就可以當密碼啦實戰演示
[root@localhost tset]# openssl rand -base64 6vZ8Qj7n1[root@localhost tset]# openssl rand -base64 63Wh8exeQ[root@localhost tset]# openssl rand -hex 6a1c269b8b1fd[root@localhost tset]# openssl rand -hex 658d677343fb7
3.4 公鑰加密公鑰加密可以實現下面三種方式的用途
(1)加密和解密
算法RSAELGamal工具gpgopenssl rsautl(2)數字籤名
算法RSADSAELGamal工具一般沒有必要自己生成(3)密鑰交換
算法dh隨機數生成器
/dev/random僅從熵池返回隨機數隨機數用盡,阻塞/dev/urandom從熵池返回隨機數隨機數用盡,會利用軟體生成偽隨機數非阻塞實戰演示
# 生成密鑰對兒,依賴於隨機數生成器,需要修改生成的文件權限openssl genrsa -out /PATH/TO/PRIVATEKEY.FILE NUM_BITS# 提取出公鑰openssl rsa -in /PATH/FROM/PRIVATEKEY.FILE -pubout
# 生成密鑰對兒[root@localhost tset]# openssl genrsa -out rsakey.private 2048Generating RSA private key, 2048 bit long modulus................ ................................. e is 65537 (0x10001)# 需要修改生成的文件權限# 這裡的括號表示在子SHELL中運行,由於在子shell中運行,所以本地的umask不會該表[root@localhost tset]# (umask 077; openssl genrsa -out rsakey.private 2048)Generating RSA private key, 2048 bit long modulus............................ ....................................................................................................................................................... e is 65537 (0x10001)# 提取出公鑰[root@localhost tset]# openssl rsa -in rsakey.private -puboutwriting RSA key-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqHk7QIMJ2b1w79YnH 2D5RSbRAG6SlFcoDCSbJkXoSewV59QqSxDD4Ctk3oYB5c/TxsbJV F 2Y31KFgqdZzqzaBP u8cKTmGXiMaOgybrlU/1Jx3 QGiQrLLsh73H/VC5//ymJBApRgeO4C AWP9Eytfn9cer5Ch0XdTsBjYgxV8BO1BrJCfqckdVmxhaIWzaJbPzqulM sxLN6MNQNeAclRMJCp Sa8dx2amfpCKJS6RstdDh jg6ryY6sbCAQwIQECBRGhODw1tWwatN4mYd4RvQlMwovgef1Aaoh3Q/ZfFycF6o/Gx54VHoQmXdWfSM JXX6MDvr/GPaBnxvQQIDAQAB-----END PUBLIC KEY-----
3.5 openssl 的命令幫助# openssl的命令幫助# 標準命令Standard commandsasn1parse ca ciphers crl crl2pkcs7dgst dh dhparam dsa dsaparamec ecparam enc engine errstrgendh gendsa genrsa nseq ocsppasswd pkcs12 pkcs7 pkcs8 primerand req rsa rsautl s_clients_server s_time sess_id smime speedspkac verify version x509# 做信息摘要時候使用的算法,是做單向加密的Message Digest commands (see the dgst command for more details)md2 md4 md5 mdc2 rmd160sha sha1# 支持的算法Cipher commands (see the enc command for more details)aes-128-cbc aes-128-ecb aes-192-cbc aes-192-ecb aes-256-cbcaes-256-ecb base64 bf bf-cbc bf-cfbbf-ecb bf-ofb cast cast-cbc cast5-cbccast5-cfb cast5-ecb cast5-ofb des des-cbcdes-cfb des-ecb des-ede des-ede-cbc des-ede-cfbdes-ede-ofb des-ede3 des-ede3-cbc des-ede3-cfb des-ede3-ofbdes-ofb des3 desx rc2 rc2-40-cbcrc2-64-cbc rc2-cbc rc2-cfb rc2-ecb rc2-ofbrc4 rc4-40 rc5 rc5-cbc rc5-cfbrc5-ecb rc5-ofb seed seed-cbc seed-cfbseed-ecb seed-ofb
3.6 openssl 應用實例(1)消息摘要算法應用例子# 用SHA1算法計算文件file.txt的哈西值,輸出到stdoutopenssl dgst -sha1 file.txt# 用SHA1算法計算文件file.txt的哈西值,輸出到文件digest.txtopenssl sha1 -out digest.txt file.txt# 用DSS1(SHA1)算法為文件file.txt籤名,輸出到文件dsasign.bin# 籤名的private key必須為DSA算法產生的,保存在文件dsakey.pem中openssl dgst -dss1 -sign dsakey.pem -out dsasign.bin file.txt# 用dss1算法驗證file.txt的數字籤名dsasign.bin,# 驗證的private key為DSA算法產生的文件dsakey.pemopenssl dgst -dss1 -prverify dsakey.pem -signature dsasign.bin file.txt# 用sha1算法為文件file.txt籤名,輸出到文件rsasign.bin# 籤名的private key為RSA算法產生的文件rsaprivate.pemopenssl sha1 -sign rsaprivate.pem -out rsasign.bin file.txt# 用sha1算法驗證file.txt的數字籤名rsasign.bin,# 驗證的public key為RSA算法生成的rsapublic.pemopenssl sha1 -verify rsapublic.pem -signature rsasign.bin file.txt
(2)對稱加密應用例子# 對稱加密應用例子# 用DES3算法的CBC模式加密文件plaintext.doc,# 加密結果輸出到文件ciphertext.binopenssl enc -des3 -salt -in plaintext.doc -out ciphertext.bin# 用DES3算法的OFB模式解密文件ciphertext.bin,# 提供的口令為trousers,輸出到文件plaintext.doc# 注意:因為模式不同,該命令不能對以上的文件進行解密openssl enc -des-ede3-ofb -d -in ciphertext.bin -out plaintext.doc -pass pass:trousers# 用Blowfish的CFB模式加密plaintext.doc,口令從環境變量PASSWORD中取# 輸出到文件ciphertext.binopenssl bf-cfb -salt -in plaintext.doc -out ciphertext.bin -pass env:PASSWORD# 給文件ciphertext.bin用base64編碼,輸出到文件base64.txtopenssl base64 -in ciphertext.bin -out base64.txt# 用RC5算法的CBC模式加密文件plaintext.doc# 輸出到文件ciphertext.bin,# salt、key和初始化向量(iv)在命令行指定openssl rc5 -in plaintext.doc -out ciphertext.bin -S C62CB1D49F158ADC -iv E9EDACA1BD7090C6 -K 89D4B1678D604FAA3DBFFD030A314B29
(3)Diffie-Hellman 應用例子# 使用生成因子2和隨機的1024-bit的素數產生D0ffie-Hellman參數# 輸出保存到文件dhparam.pemopenssl dhparam -out dhparam.pem -2 1024# 從dhparam.pem中讀取Diffie-Hell參數,以C代碼的形式# 輸出到stdoutopenssl dhparam -in dhparam.pem -noout -C
(4)DSA 應用例子應用例子# 生成1024位DSA參數集,並輸出到文件dsaparam.pemopenssl dsaparam -out dsaparam.pem 1024# 使用參數文件dsaparam.pem生成DSA私鑰匙,# 採用3DES加密後輸出到文件dsaprivatekey.pemopenssl gendsa -out dsaprivatekey.pem -des3 dsaparam.pem# 使用私鑰匙dsaprivatekey.pem生成公鑰匙,# 輸出到dsapublickey.pemopenssl dsa -in dsaprivatekey.pem -pubout -out dsapublickey.pem# 從dsaprivatekey.pem中讀取私鑰匙,解密並輸入新口令進行加密,# 然後寫回文件dsaprivatekey.pemopenssl dsa -in dsaprivatekey.pem -out dsaprivatekey.pem -des3 -passin
(5)RSA 應用例子# 產生1024位RSA私匙,用3DES加密它,口令為trousers,# 輸出到文件rsaprivatekey.pemopenssl genrsa -out rsaprivatekey.pem -passout pass:trousers -des3 1024# 從文件rsaprivatekey.pem讀取私匙,用口令trousers解密,# 生成的公鑰匙輸出到文件rsapublickey.pemopenssl rsa -in rsaprivatekey.pem -passin pass:trousers -pubout -out rsapubckey.pem# 用公鑰匙rsapublickey.pem加密文件plain.txt,# 輸出到文件cipher.txtopenssl rsautl -encrypt -pubin -inkey rsapublickey.pem -in plain.txt -out cipher.txt# 使用私鑰匙rsaprivatekey.pem解密密文cipher.txt,# 輸出到文件plain.txtopenssl rsautl -decrypt -inkey rsaprivatekey.pem -in cipher.txt -out plain.txt# 用私鑰匙rsaprivatekey.pem給文件plain.txt籤名,# 輸出到文件signature.binopenssl rsautl -sign -inkey rsaprivatekey.pem -in plain.txt -out signature.bin# 用公鑰匙rsapublickey.pem驗證籤名signature.bin,# 輸出到文件plain.txtopenssl rsautl -verify -pubin -inkey rsapublickey.pem -in signature.bin -out plain# 從X.509證書文件cert.pem中獲取公鑰匙,# 用3DES加密mail.txt# 輸出到文件mail.encopenssl smime -encrypt -in mail.txt -des3 -out mail.enc cert.pem# 從X.509證書文件cert.pem中獲取接收人的公鑰匙,# 用私鑰匙key.pem解密S/MIME消息mail.enc,# 結果輸出到文件mail.txtopenssl smime -decrypt -in mail.enc -recip cert.pem -inkey key.pem -out mail.txt# cert.pem為X.509證書文件,用私匙key,pem為mail.txt籤名,# 證書被包含在S/MIME消息中,輸出到文件mail.sgnopenssl smime -sign -in mail.txt -signer cert.pem -inkey key.pem -out mail.sgn# 驗證S/MIME消息mail.sgn,輸出到文件mail.txt# 籤名者的證書應該作為S/MIME消息的一部分包含在mail.sgn中openssl smime -verify -in mail.sgn -out mail.txt
文章作者: Escape
文章連結: https://www.escapelife.site/posts/48693f50.html
,