當前位置:編程學習大全網 - 網絡軟體 - OpenSSL詳解

OpenSSL詳解

OpenSSL初接觸的人恐怕最難的在於先理解各種概念

公鑰/私鑰/簽名/驗證簽名/加密/解密/非對稱加密

我們壹般的加密是用壹個密碼加密文件,然後解密也用同樣的密碼.這很好理解,這個是對稱加密.而有些加密時,加密用的壹個密碼,而解密用另外壹組密碼,這個叫非對稱加密,意思就是加密解密的密碼不壹樣.初次接觸的人恐怕無論如何都理解不了.其實這是數學上的壹個素數積求因子的原理的應用,如果妳壹定要搞懂,百度有大把大把的資料可以看,其結果就是用這壹組密鑰中的壹個來加密數據,可以用另壹個解開.是的沒錯,公鑰和私鑰都可以用來加密數據,相反用另壹個解開,公鑰加密數據,然後私鑰解密的情況被稱為加密解密,私鑰加密數據,公鑰解密壹般被稱為簽名和驗證簽名.

因為公鑰加密的數據只有它相對應的私鑰可以解開,所以妳可以把公鑰給人和人,讓他加密他想要傳送給妳的數據,這個數據只有到了有私鑰的妳這裏,才可以解開成有用的數據,其他人就是得到了,也看懂內容.同理,如果妳用妳的私鑰對數據進行簽名,那這個數據就只有配對的公鑰可以解開,有這個私鑰的只有妳,所以如果配對的公鑰解開了數據,就說明這數據是妳發的,相反,則不是.這個被稱為簽名.

實際應用中,壹般都是和對方交換公鑰,然後妳要發給對方的數據,用他的公鑰加密,他得到後用他的私鑰解密,他要發給妳的數據,用妳的公鑰加密,妳得到後用妳的私鑰解密,這樣最大程度保證了安全性.

RSA/DSA/SHA/MD5

非對稱加密的算法有很多,比較著名的有RSA/DSA ,不同的是RSA可以用於加/解密,也可以用於簽名驗簽,DSA則只能用於簽名.至於SHA則是壹種和md5相同的算法,它不是用於加密解密或者簽名的,它被稱為摘要算法.就是通過壹種算法,依據數據內容生成壹種固定長度的摘要,這串摘要值與原數據存在對應關系,就是原數據會生成這個摘要,但是,這個摘要是不能還原成原數據的,嗯....,正常情況下是這樣的,這個算法起的作用就是,如果妳把原數據修改壹點點,那麽生成的摘要都會不同,傳輸過程中把原數據給妳再給妳壹個摘要,妳把得到的原數據同樣做壹次摘要算法,與給妳的摘要相比較就可以知道這個數據有沒有在傳輸過程中被修改了.

實際應用過程中,因為需要加密的數據可能會很大,進行加密費時費力,所以壹般都會把原數據先進行摘要,然後對這個摘要值進行加密,將原數據的明文和加密後的摘要值壹起傳給妳.這樣妳解開加密後的摘要值,再和妳得到的數據進行的摘要值對應壹下就可以知道數據有沒有被修改了,而且,因為私鑰只有妳有,只有妳能解密摘要值,所以別人就算把原數據做了修改,然後生成壹個假的摘要給妳也是不行的,妳這邊用密鑰也根本解不開.

CA/PEM/DER/X509/PKCS

壹般的公鑰不會用明文傳輸給別人的,正常情況下都會生成壹個文件,這個文件就是公鑰文件,然後這個文件可以交給其他人用於加密,但是傳輸過程中如果有人惡意破壞,將妳的公鑰換成了他的公鑰,然後得到公鑰的壹方加密數據,不是他就可以用他自己的密鑰解密看到數據了嗎,為了解決這個問題,需要壹個公證方來做這個事,任何人都可以找它來確認公鑰是誰發的.這就是CA,CA確認公鑰的原理也很簡單,它將它自己的公鑰發布給所有人,然後壹個想要發布自己公鑰的人可以將自己的公鑰和壹些身份信息發給CA,CA用自己的密鑰進行加密,這裏也可以稱為簽名.然後這個包含了妳的公鑰和妳的信息的文件就可以稱為證書文件了.這樣壹來所有得到壹些公鑰文件的人,通過CA的公鑰解密了文件,如果正常解密那麽機密後裏面的信息壹定是真的,因為加密方只可能是CA,其他人沒它的密鑰啊.這樣妳解開公鑰文件,看看裏面的信息就知道這個是不是那個妳需要用來加密的公鑰了.

實際應用中,壹般人都不會找CA去簽名,因為那是收錢的,所以可以自己做壹個自簽名的證書文件,就是自己生成壹對密鑰,然後再用自己生成的另外壹對密鑰對這對密鑰進行簽名,這個只用於真正需要簽名證書的人,普通的加密解密數據,直接用公鑰和私鑰來做就可以了.

密鑰文件的格式用OpenSSL生成的就只有PEM和DER兩種格式,PEM的是將密鑰用base64編碼表示出來的,直接打開妳能看到壹串的英文字母,DER格式是二進制的密鑰文件,直接打開,妳可以看到........妳什麽也看不懂!.X509是通用的證書文件格式定義.pkcs的壹系列標準是指定的存放密鑰的文件標準,妳只要知道PEM DER X509 PKCS這幾種格式是可以互相轉化的.

== End blogs.com/phpinfo/archive/2013/08/09/3246376.html ==

為了方便理解,我畫了壹個圖,如下:

使用 openssl 生成證書(含openssl詳解)

壹、openssl 簡介

openssl 是目前最流行的 SSL 密碼庫工具,其提供了壹個通用、健壯、功能完備的工具套件,用以支持SSL/TLS 協議的實現。

官網: pany) [Default Company Ltd]:SunFoBank

Organizational Unit Name (eg, section) []:IT Dept

Common Name (eg, your name or your server's hostname) []:sunfobank.com

Email Address [] :admin@sunfobank.com

[root@szxjdwins01-web-27 cert]# ll

total 8

-rw-r--r--. 1 root root 1452 Jun 22 14:29 cert.crt

-rw-r--r--. 1 root root 1708 Jun 22 14:29 rsa_private.key

openssl req -newkey rsa:2048 -nodes -keyout rsa_private.key -x509 -days 36500 -out cert.crt -subj "/C=CN/ST=GuangDong/L=ShenZhen/O=SunFoBank/OU=IT Dept/CN= sunfobank.com/emailAddress=admin@sunfobank.com "

openssl req -new -x509 -days 36500 -key rsa_private.key -out cert.crt

四、生成服務器簽名請求文件及CA 簽名頒發服務器證書()

server.key建議不要加密碼,如果加密碼,重啟nginx的時候每次都需要密碼才可以啟動nginx影響效率。

nginx配置只需要server.key和server.crt兩個文件。

openssl genrsa -aes256 -passout pass:111111 -out server.key 2048

openssl req -new -key server.key -out server.csr

[root@szxjdwins01-web-27 cert]# openssl genrsa -aes256 -passout pass:111111 -out server.key 2048

Generating RSA private key, 2048 bit long modulus

............................+++

.......+++

e is 65537 (0x10001)

[root@szxjdwins01-web-27 cert]# openssl genrsa -aes256 -out server.key 2048

Generating RSA private key, 2048 bit long modulus

.............................................+++

........................................................+++

e is 65537 (0x10001)

Enter pass phrase for server.key: 111111手動輸入密碼

Verifying - Enter pass phrase for server.key: 111111手動輸入密碼

[root@szxelab01-web-27 cert]# openssl req -new -key server.key -out server.csr

Enter pass phrase for server.key:

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) []:GuangDong

Locality Name (eg, city) [Default City]:ShenZhen

Organization Name (eg, company) [Default Company Ltd]:SunFoBank

Organizational Unit Name (eg, section) []:IT Dept

Common Name (eg, your name or your server's hostname) []:sunfobank.com

Email Address [] :admin@sunfobank.com

Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge password []: 不輸入密碼

An optional company name []: 不輸入密碼

此後輸入密碼、server證書信息完成,也可以命令行指定各類參數

openssl req -new -key server.key -passin pass:111111 -out server.csr -subj "/C=CN/ST=GuangDong/L=ShenZhen/O=SunFoBank/OU=IT Dept/CN= sunfobank.com/emailAddress=admin@sunfobank.com "

*** 此時生成的 csr簽名請求文件可提交至 CA進行簽發 ***

cat server.csr

-----BEGIN CERTIFICATE REQUEST-----

Base64EncodedData

-----END CERTIFICATE REQUEST-----

openssl req -noout -text -in server.csr

openssl x509 -req -days 365000 -in server.csr -CA cert.crt -CAkey rsa_private.key -passin pass:111111 -CAcreateserial -out server.crt

[root@szxelab01-web-27 cert]# openssl x509 -req -days 365000 -in server.csr -CA cert.crt -CAkey rsa_private.key -passin pass:111111 -CAcreateserial -out server.crt

Signature ok

subject=/C=CN/ST=GuangDong/L=ShenZhen/O=SunFoBank/OU=IT Dept/CN= sunfobank.com/emailAddress=admin@sunfobank.com

Getting CA Private Key

其中 CAxxx 選項用於指定CA 參數輸入

[root@szxelab01-web-27 cert]# ll

total 24

-rw-r--r--. 1 root root 1452 Jun 22 14:29 cert.crt

-rw-r--r--. 1 root root 17 Jun 22 15:07 cert.srl

-rw-r--r--. 1 root root 1708 Jun 22 14:29 rsa_private.key

-rw-r--r--. 1 root root 1334 Jun 22 15:07 server.crt

-rw-r--r--. 1 root root 1070 Jun 22 15:04 server.csr

-rw-r--r--. 1 root root 1766 Jun 22 14:54 server.key

此時對nginx任何操作,都需要提示輸入server.key的密碼才可以執行。

[root@szxelab01-web-27 nginx]# /application/nginx/sbin/nginx -t

Enter PEM pass phrase: 輸入密碼111111

nginx: the configuration file /application/nginx-1.12.2//conf/nginx.conf syntax is ok

為例不輸入密碼,需要把加密server.key轉換成不加密的server.key

[root@szxelab01-web-27 cert]# openssl rsa -in server.key -passin pass:111111 -out server.key

writing RSA key

此時nginx操作就不提示輸入密碼了:

[root@szxelab01-web-27 cert]# /application/nginx/sbin/nginx -t

nginx: the configuration file /application/nginx-1.12.2//conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.12.2//conf/nginx.conf test is successful

證書位置:

[root@szxelab01-web-27 cert]# pwd

/application/nginx/cert

[root@szxelab01-web-27 cert]# ll

total 24

-rw-r--r--. 1 root root 1452 Jun 22 14:29 cert.crt

-rw-r--r--. 1 root root 17 Jun 22 15:07 cert.srl

-rw-r--r--. 1 root root 1708 Jun 22 14:29 rsa_private.key

-rw-r--r--. 1 root root 1334 Jun 22 15:07 server.crt

-rw-r--r--. 1 root root 1070 Jun 22 15:04 server.csr

-rw-r--r--. 1 root root 1679 Jun 22 15:19 server.key

至此測試場景私有證書配置完成

五、證書查看及轉換

查看證書細節

openssl x509 -in cert.crt -noout -text

轉換證書編碼格式

openssl x509 -in cert.cer -inform DER -outform PEM -out cert.pem

合成 pkcs#12 證書(含私鑰)

** 將 pem 證書和私鑰轉 pkcs#12 證書 **

openssl pkcs12 -export -in server.crt -inkey server.key -passin pass:111111 -password pass:111111 -out server.p12

其中-export指導出pkcs#12 證書,-inkey 指定了私鑰文件,-passin 為私鑰(文件)密碼(nodes為無加密),-password 指定 p12文件的密碼(導入導出)

** 將 pem 證書和私鑰/CA 證書 合成pkcs#12 證書**

openssl pkcs12 -export -in server.crt -inkey server.key -passin pass:111111 \ -chain -CAfile ca.crt -password pass:111111 -out server-all.p12

其中-chain指示同時添加證書鏈,-CAfile 指定了CA證書,導出的p12文件將包含多個證書。(其他選項:-name可用於指定server證書別名;-caname用於指定ca證書別名)

** pcks#12 提取PEM文件(含私鑰) **

openssl pkcs12 -in server.p12 -password pass:111111 -passout pass:111111 -out out/server.pem

其中-password 指定 p12文件的密碼(導入導出),-passout指輸出私鑰的加密密碼(nodes為無加密)

導出的文件為pem格式,同時包含證書和私鑰(pkcs#8):

Bag Attributes

localKeyID: 97 DD 46 3D 1E 91 EF 01 3B 2E 4A 75 81 4F 11 A6 E7 1F 79 40 subject=/C=CN/ST=GD/L=SZ/O=vihoo/OU=dev/CN= vihoo.com/emailAddress=yy@vihoo.com

issuer=/C=CN/ST=GD/L=SZ/O=viroot/OU=dev/CN= viroot.com/emailAddress=yy@viroot.com-----BEGIN CERTIFICATE-----MIIDazCCAlMCCQCIOlA9/dcfEjANBgkqhkiG9w0BAQUFADB5MQswCQYDVQQGEwJD

1LpQCA+2B6dn4scZwaCD-----END CERTIFICATE-----Bag Attributes

localKeyID: 97 DD 46 3D 1E 91 EF 01 3B 2E 4A 75 81 4F 11 A6 E7 1F 79 40 Key Attributes: <No Attributes>

-----BEGIN ENCRYPTED PRIVATE KEY-----MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDC/6rAc1YaPRNf

K9ZLHbyBTKVaxehjxzJHHw==

-----END ENCRYPTED PRIVATE KEY-----

僅提取私鑰

openssl pkcs12 -in server.p12 -password pass:111111 -passout pass:111111 -nocerts -out out/key.pem

僅提取證書(所有證書)

openssl pkcs12 -in server.p12 -password pass:111111 -nokeys -out out/key.pem

僅提取ca證書

openssl pkcs12 -in server-all.p12 -password pass:111111 -nokeys -cacerts -out out/cacert.pem

僅提取server證書

openssl pkcs12 -in server-all.p12 -password pass:111111 -nokeys -clcerts -out out/cert.pem

六、openssl 命令參考

  • 上一篇:《老農民》各種人物結局分別是什麽?
  • 下一篇:“壹封難忘的家書”征文活動方案
  • copyright 2024編程學習大全網