1. 基本原理
參考:http://www.cnblogs.com/phpinfo/archive/2013/08/09/3246376.html
== Begin http://www.cnblogs.com/phpinfo/archive/2013/08/09/3246376.html ==
公司一個項目要進行交易數(shù)據(jù)傳輸,因為這個項目銀行那邊也是剛剛開始啟動,所有的支持只有一個傳輸字段的說明文檔,好吧,總的有人做事不是嘛,于是接口開發(fā)正式展開,第一步的難點就是加密解密,我選擇使用OpenSSL.
OpenSSL初接觸的人恐怕最難的在于先理解各種概念
公鑰/私鑰/簽名/驗證簽名/加密/解密/非對稱加密
我們一般的加密是用一個密碼加密文件,然后解密也用同樣的密碼.這很好理解,這個是對稱加密.而有些加密時,加密用的一個密碼,而解密用另外一組密碼,這個叫非對稱加密,意思就是加密解密的密碼不一樣.初次接觸的人恐怕無論如何都理解不了.其實這是數(shù)學上的一個素數(shù)積求因子的原理的應用,如果你一定要搞懂,百度有大把大把的資料可以看,其結果就是用這一組密鑰中的一個來加密數(shù)據(jù),可以用另一個解開.是的沒錯,公鑰和私鑰都可以用來加密數(shù)據(jù),相反用另一個解開,公鑰加密數(shù)據(jù),然后私鑰解密的情況被稱為加密解密,私鑰加密數(shù)據(jù),公鑰解密一般被稱為簽名和驗證簽名.
因為公鑰加密的數(shù)據(jù)只有它相對應的私鑰可以解開,所以你可以把公鑰給人和人,讓他加密他想要傳送給你的數(shù)據(jù),這個數(shù)據(jù)只有到了有私鑰的你這里,才可以解開成有用的數(shù)據(jù),其他人就是得到了,也看懂內(nèi)容.同理,如果你用你的私鑰對數(shù)據(jù)進行簽名,那這個數(shù)據(jù)就只有配對的公鑰可以解開,有這個私鑰的只有你,所以如果配對的公鑰解開了數(shù)據(jù),就說明這數(shù)據(jù)是你發(fā)的,相反,則不是.這個被稱為簽名.
實際應用中,一般都是和對方交換公鑰,然后你要發(fā)給對方的數(shù)據(jù),用他的公鑰加密,他得到后用他的私鑰解密,他要發(fā)給你的數(shù)據(jù),用你的公鑰加密,你得到后用你的私鑰解密,這樣最大程度保證了安全性.
RSA/DSA/SHA/MD5
非對稱加密的算法有很多,比較著名的有RSA/DSA ,不同的是RSA可以用于加/解密,也可以用于簽名驗簽,DSA則只能用于簽名.至于SHA則是一種和md5相同的算法,它不是用于加密解密或者簽名的,它被稱為摘要算法.就是通過一種算法,依據(jù)數(shù)據(jù)內(nèi)容生成一種固定長度的摘要,這串摘要值與原數(shù)據(jù)存在對應關系,就是原數(shù)據(jù)會生成這個摘要,但是,這個摘要是不能還原成原數(shù)據(jù)的,嗯....,正常情況下是這樣的,這個算法起的作用就是,如果你把原數(shù)據(jù)修改一點點,那么生成的摘要都會不同,傳輸過程中把原數(shù)據(jù)給你再給你一個摘要,你把得到的原數(shù)據(jù)同樣做一次摘要算法,與給你的摘要相比較就可以知道這個數(shù)據(jù)有沒有在傳輸過程中被修改了.
實際應用過程中,因為需要加密的數(shù)據(jù)可能會很大,進行加密費時費力,所以一般都會把原數(shù)據(jù)先進行摘要,然后對這個摘要值進行加密,將原數(shù)據(jù)的明文和加密后的摘要值一起傳給你.這樣你解開加密后的摘要值,再和你得到的數(shù)據(jù)進行的摘要值對應一下就可以知道數(shù)據(jù)有沒有被修改了,而且,因為私鑰只有你有,只有你能解密摘要值,所以別人就算把原數(shù)據(jù)做了修改,然后生成一個假的摘要給你也是不行的,你這邊用密鑰也根本解不開.
CA/PEM/DER/X509/PKCS
一般的公鑰不會用明文傳輸給別人的,正常情況下都會生成一個文件,這個文件就是公鑰文件,然后這個文件可以交給其他人用于加密,但是傳輸過程中如果有人惡意破壞,將你的公鑰換成了他的公鑰,然后得到公鑰的一方加密數(shù)據(jù),不是他就可以用他自己的密鑰解密看到數(shù)據(jù)了嗎,為了解決這個問題,需要一個公證方來做這個事,任何人都可以找它來確認公鑰是誰發(fā)的.這就是CA,CA確認公鑰的原理也很簡單,它將它自己的公鑰發(fā)布給所有人,然后一個想要發(fā)布自己公鑰的人可以將自己的公鑰和一些身份信息發(fā)給CA,CA用自己的密鑰進行加密,這里也可以稱為簽名.然后這個包含了你的公鑰和你的信息的文件就可以稱為證書文件了.這樣一來所有得到一些公鑰文件的人,通過CA的公鑰解密了文件,如果正常解密那么機密后里面的信息一定是真的,因為加密方只可能是CA,其他人沒它的密鑰啊.這樣你解開公鑰文件,看看里面的信息就知道這個是不是那個你需要用來加密的公鑰了.
實際應用中,一般人都不會找CA去簽名,因為那是收錢的,所以可以自己做一個自簽名的證書文件,就是自己生成一對密鑰,然后再用自己生成的另外一對密鑰對這對密鑰進行簽名,這個只用于真正需要簽名證書的人,普通的加密解密數(shù)據(jù),直接用公鑰和私鑰來做就可以了.
密鑰文件的格式用OpenSSL生成的就只有PEM和DER兩種格式,PEM的是將密鑰用base64編碼表示出來的,直接打開你能看到一串的英文字母,DER格式是二進制的密鑰文件,直接打開,你可以看到........你什么也看不懂!.X509是通用的證書文件格式定義.pkcs的一系列標準是指定的存放密鑰的文件標準,你只要知道PEM DER X509 PKCS這幾種格式是可以互相轉(zhuǎn)化的.
== End http://www.cnblogs.com/phpinfo/archive/2013/08/09/3246376.html ==
為了方便理解,我畫了一個圖,如下:
使用 openssl 生成證書(含openssl詳解)
一、openssl 簡介
openssl 是目前最流行的 SSL 密碼庫工具,其提供了一個通用、健壯、功能完備的工具套件,用以支持SSL/TLS 協(xié)議的實現(xiàn)。
官網(wǎng):https://www.openssl.org/source/
構成部分
密碼算法庫
密鑰和證書封裝管理功能
SSL通信API接口
用途
建立 RSA、DH、DSA key 參數(shù)
建立 X.509 證書、證書簽名請求(CSR)和CRLs(證書回收列表)
計算消息摘要
使用各種 Cipher加密/解密
SSL/TLS 客戶端以及服務器的測試
處理S/MIME 或者加密郵件
二、RSA密鑰操作
默認情況下,openssl 輸出格式為 PKCS#1-PEM
生成RSA私鑰(無加密)
openssl genrsa -out rsa_private.key 2048
生成RSA公鑰
openssl rsa -in rsa_private.key -pubout -out rsa_public.key
生成RSA私鑰(使用aes256加密)
openssl genrsa -aes256 -passout pass:111111 -out rsa_aes_private.key 2048
其中 passout 代替shell 進行密碼輸入,否則會提示輸入密碼;
生成加密后的內(nèi)容如:
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC,5584D000DDDD53DD5B12AE935F05A007
Base64 Encoded
Data-----END RSA PRIVATE KEY-----
此時若生成公鑰,需要提供密碼
openssl rsa -in rsa_aes_private.key -passin pass:111111 -pubout -out rsa_public.key
其中 passout 代替shell 進行密碼輸入,否則會提示輸入密碼;
轉(zhuǎn)換命令
私鑰轉(zhuǎn)非加密
openssl rsa -in rsa_aes_private.key -passin pass:111111 -out rsa_private.key
私鑰轉(zhuǎn)加密
openssl rsa -in rsa_private.key -aes256 -passout pass:111111 -out rsa_aes_private.key
私鑰PEM轉(zhuǎn)DER
openssl rsa -in rsa_private.key -outform der-out rsa_aes_private.der
-inform和-outform 參數(shù)制定輸入輸出格式,由der轉(zhuǎn)pem格式同理
查看私鑰明細
openssl rsa -in rsa_private.key -noout -text
使用-pubin參數(shù)可查看公鑰明細
私鑰PKCS#1轉(zhuǎn)PKCS#8
openssl pkcs8 -topk8 -in rsa_private.key -passout pass:111111 -out pkcs8_private.key
其中-passout指定了密碼,輸出的pkcs8格式密鑰為加密形式,pkcs8默認采用des3 加密算法,內(nèi)容如下:
-----BEGIN ENCRYPTED PRIVATE KEY-----
Base64 Encoded Data
-----END ENCRYPTED PRIVATE KEY-----
使用-nocrypt參數(shù)可以輸出無加密的pkcs8密鑰,如下:
-----BEGIN PRIVATE KEY-----
Base64 Encoded Data
-----END PRIVATE KEY-----
三、生成CA自簽名證書和RSA私鑰(測試場景步驟)
測試場景步驟1:生成 RSA 私鑰和自簽名證書:
openssl req -newkey rsa:2048 -nodes -keyout rsa_private.key -x509 -days 36500 -out cert.crt
注釋:
req是證書請求的子命令,
-newkey rsa:2048
-keyout private_key.pem 表示生成私鑰(PKCS8格式),
-nodes 表示私鑰不加密,若不帶參數(shù)將提示輸入密碼;
-x509表示輸出證書,
-days36500 為有效期,
此后根據(jù)提示輸入證書擁有者信息;
操作步驟如下:提示填寫過程中如果想刪除填寫的內(nèi)容,用ctrl+Backspace刪除前面的字符
[root@szxelab01-web-100 cert]# openssl req -newkey rsa:2048 -nodes -keyout rsa_private.key -x509 -days 36500 -out cert.crt
Generating a 2048 bit RSA private key
.............+++
........................+++
writing new private key to 'rsa_private.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
此時就生成了:未加密碼的私鑰rsa_private.key和CA自簽名證書cert.crt
[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
如果想執(zhí)行自動輸入證書擁有者信息,可使用-subj選項:
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"
如果想使用已有RSA 私鑰生成CA自簽名證書,請操作如下:
openssl req -new -x509 -days 36500 -key rsa_private.key -out cert.crt
-new 指生成證書請求,加上-x509 表示直接輸出證書,-key 指定私鑰文件,其余選項與上述命令相同
四、生成服務器簽名請求文件及CA 簽名頒發(fā)服務器證書()
** server.key建議不要加密碼,如果加密碼,重啟nginx的時候每次都需要密碼才可以啟動nginx影響效率。**
nginx配置只需要server.key和server.crt兩個文件。
測試場景步驟2:使用 RSA私鑰生成 CSR 簽名請求:server.key為aes256位加密
openssl genrsa -aes256 -passout pass:111111 -out server.key 2048
openssl req -new -key server.key -out server.csr
實戰(zhàn)如下:對比下加-passout pass:111111的區(qū)別
[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證書信息完成,也可以命令行指定各類參數(shù)
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進行簽發(fā) ***
查看CSR 的細節(jié)
cat server.csr
-----BEGIN CERTIFICATE REQUEST-----
Base64EncodedData
-----END CERTIFICATE REQUEST-----
openssl req -noout -text -in server.csr
使用 CA 證書及CA密鑰 對服務器請求簽發(fā)證書進行簽發(fā),生成 x509證書:
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 參數(shù)輸入
[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的密碼才可以執(zhí)行。
[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轉(zhuǎn)換成不加密的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
至此測試場景私有證書配置完成
五、證書查看及轉(zhuǎn)換
查看證書細節(jié)
<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;">openssl x509 -in cert.crt -noout -text</pre>
轉(zhuǎn)換證書編碼格式
<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;">openssl x509 -in cert.cer -inform DER -outform PEM -out cert.pem</pre>
合成 pkcs#12 證書(含私鑰)
** 將 pem 證書和私鑰轉(zhuǎn) pkcs#12 證書 **
<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;">openssl pkcs12 -export -in server.crt -inkey server.key -passin pass:111111 -password pass:111111 -out server.p12</pre>
其中-export指導出pkcs#12 證書,-inkey 指定了私鑰文件,-passin 為私鑰(文件)密碼(nodes為無加密),-password 指定 p12文件的密碼(導入導出)
** 將 pem 證書和私鑰/CA 證書 合成pkcs#12 證書**
<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;">openssl pkcs12 -export -in server.crt -inkey server.key -passin pass:111111 \ -chain -CAfile ca.crt -password pass:111111 -out server-all.p12</pre>
其中-chain指示同時添加證書鏈,-CAfile 指定了CA證書,導出的p12文件將包含多個證書。(其他選項:-name可用于指定server證書別名;-caname用于指定ca證書別名)
** pcks#12 提取PEM文件(含私鑰) **
<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;">openssl pkcs12 -in server.p12 -password pass:111111 -passout pass:111111 -out out/server.pem</pre>
其中-password 指定 p12文件的密碼(導入導出),-passout指輸出私鑰的加密密碼(nodes為無加密)
導出的文件為pem格式,同時包含證書和私鑰(pkcs#8):
<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;">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-----</pre>
僅提取私鑰
<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;"> openssl pkcs12 -in server.p12 -password pass:111111 -passout pass:111111 -nocerts -out out/key.pem</pre>
僅提取證書(所有證書)
<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;"> openssl pkcs12 -in server.p12 -password pass:111111 -nokeys -out out/key.pem</pre>
僅提取ca證書
<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;">openssl pkcs12 -in server-all.p12 -password pass:111111 -nokeys -cacerts -out out/cacert.pem</pre>
僅提取server證書
<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;">openssl pkcs12 -in server-all.p12 -password pass:111111 -nokeys -clcerts -out out/cert.pem</pre>
六、openssl 命令參考
<pre style="font-family: "Courier New" !important; -webkit-font-smoothing: antialiased; margin: 0px; padding: 0px; font-size: 14px; white-space: pre-wrap; overflow: auto; line-height: 22px; background: rgb(240, 240, 240); color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; box-sizing: border-box; outline: 0px; word-break: break-all;">1. openssl list-standard-commands(標準命令)
1) asn1parse: asn1parse用于解釋用ANS.1語法書寫的語句(ASN一般用于定義語法的構成)
2) ca: ca用于CA的管理
openssl ca [options]:
2.1) -selfsign
使用對證書請求進行簽名的密鑰對來簽發(fā)證書。即"自簽名",這種情況發(fā)生在生成證書的客戶端、簽發(fā)證書的CA都是同一臺機器(也是我們大多數(shù)實驗中的情況),我們可以使用同一個
密鑰對來進行"自簽名"
2.2) -in file
需要進行處理的PEM格式的證書
2.3) -out file
處理結束后輸出的證書文件
2.4) -cert file
用于簽發(fā)的根CA證書
2.5) -days arg
指定簽發(fā)的證書的有效時間
2.6) -keyfile arg
CA的私鑰證書文件
2.7) -keyform arg
CA的根私鑰證書文件格式:
2.7.1) PEM
2.7.2) ENGINE
2.8) -key arg
CA的根私鑰證書文件的解密密碼(如果加密了的話)
2.9) -config file
配置文件
example1: 利用CA證書簽署請求證書
openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key
3) req: X.509證書簽發(fā)請求(CSR)管理
openssl req [options] <infile >outfile
3.1) -inform arg
輸入文件格式
3.1.1) DER
3.1.2) PEM
3.2) -outform arg
輸出文件格式
3.2.1) DER
3.2.2) PEM
3.3) -in arg
待處理文件
3.4) -out arg
待輸出文件
3.5) -passin
用于簽名待生成的請求證書的私鑰文件的解密密碼
3.6) -key file
用于簽名待生成的請求證書的私鑰文件
3.7) -keyform arg
3.7.1) DER
3.7.2) NET
3.7.3) PEM
3.8) -new
新的請求
3.9) -x509
輸出一個X509格式的證書
3.10) -days
X509證書的有效時間
3.11) -newkey rsa:bits
生成一個bits長度的RSA私鑰文件,用于簽發(fā)
3.12) -[digest]
HASH算法
3.12.1) md5
3.12.2) sha1
3.12.3) md2
3.12.4) mdc2
3.12.5) md4
3.13) -config file
指定openssl配置文件
3.14) -text: text顯示格式
example1: 利用CA的RSA密鑰創(chuàng)建一個自簽署的CA證書(X.509結構)
openssl req -new -x509 -days 3650 -key server.key -out ca.crt
example2: 用server.key生成證書簽署請求CSR(這個CSR用于之外發(fā)送待CA中心等待簽發(fā))
openssl req -new -key server.key -out server.csr
example3: 查看CSR的細節(jié)
openssl req -noout -text -in server.csr
4) genrsa: 生成RSA參數(shù)
openssl genrsa [args] [numbits]
[args]
4.1) 對生成的私鑰文件是否要使用加密算法進行對稱加密:
4.1.1) -des: CBC模式的DES加密
4.1.2) -des3: CBC模式的DES加密
4.1.3) -aes128: CBC模式的AES128加密
4.1.4) -aes192: CBC模式的AES192加密
4.1.5) -aes256: CBC模式的AES256加密
4.2) -passout arg: arg為對稱加密(des、des、aes)的密碼(使用這個參數(shù)就省去了console交互提示輸入密碼的環(huán)節(jié))
4.3) -out file: 輸出證書私鑰文件
[numbits]: 密鑰長度
example: 生成一個1024位的RSA私鑰,并用DES加密(密碼為1111),保存為server.key文件
openssl genrsa -out server.key -passout pass:1111 -des3 1024
5) rsa: RSA數(shù)據(jù)管理
openssl rsa [options] <infile >outfile
5.1) -inform arg
輸入密鑰文件格式:
5.1.1) DER(ASN1)
5.1.2) NET
5.1.3) PEM(base64編碼格式)
5.2) -outform arg
輸出密鑰文件格式
5.2.1) DER
5.2.2) NET
5.2.3) PEM
5.3) -in arg
待處理密鑰文件
5.4) -passin arg
輸入這個加密密鑰文件的解密密鑰(如果在生成這個密鑰文件的時候,選擇了加密算法了的話)
5.5) -out arg
待輸出密鑰文件
5.6) -passout arg
如果希望輸出的密鑰文件繼續(xù)使用加密算法的話則指定密碼
5.7) -des: CBC模式的DES加密
5.8) -des3: CBC模式的DES加密
5.9) -aes128: CBC模式的AES128加密
5.10) -aes192: CBC模式的AES192加密
5.11) -aes256: CBC模式的AES256加密
5.12) -text: 以text形式打印密鑰key數(shù)據(jù)
5.13) -noout: 不打印密鑰key數(shù)據(jù)
5.14) -pubin: 檢查待處理文件是否為公鑰文件
5.15) -pubout: 輸出公鑰文件
example1: 對私鑰文件進行解密
openssl rsa -in server.key -passin pass:111 -out server_nopass.key
example:2: 利用私鑰文件生成對應的公鑰文件
openssl rsa -in server.key -passin pass:111 -pubout -out server_public.key 6) x509:
本指令是一個功能很豐富的證書處理工具。可以用來顯示證書的內(nèi)容,轉(zhuǎn)換其格式,給CSR簽名等X.509證書的管理工作
openssl x509 [args]
6.1) -inform arg
待處理X509證書文件格式
6.1.1) DER
6.1.2) NET
6.1.3) PEM
6.2) -outform arg
待輸出X509證書文件格式
6.2.1) DER
6.2.2) NET
6.2.3) PEM
6.3) -in arg
待處理X509證書文件
6.4) -out arg
待輸出X509證書文件
6.5) -req
表明輸入文件是一個"請求簽發(fā)證書文件(CSR)",等待進行簽發(fā)
6.6) -days arg
表明將要簽發(fā)的證書的有效時間
6.7) -CA arg
指定用于簽發(fā)請求證書的根CA證書
6.8) -CAform arg
根CA證書格式(默認是PEM)
6.9) -CAkey arg
指定用于簽發(fā)請求證書的CA私鑰證書文件,如果這個option沒有參數(shù)輸入,那么缺省認為私有密鑰在CA證書文件里有
6.10) -CAkeyform arg
指定根CA私鑰證書文件格式(默認為PEM格式)
6.11) -CAserial arg
指定序列號文件(serial number file)
6.12) -CAcreateserial
如果序列號文件(serial number file)沒有指定,則自動創(chuàng)建它
example1: 轉(zhuǎn)換DER證書為PEM格式
openssl x509 -in cert.cer -inform DER -outform PEM -out cert.pem
example2: 使用根CA證書對"請求簽發(fā)證書"進行簽發(fā),生成x509格式證書
openssl x509 -req -days 3650 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt
example3: 打印出證書的內(nèi)容
openssl x509 -in server.crt -noout -text
7) crl: crl是用于管理CRL列表
openssl crl [args]
7.1) -inform arg
輸入文件的格式
7.1.1) DER(DER編碼的CRL對象)
7.1.2) PEM(默認的格式)(base64編碼的CRL對象)
7.2) -outform arg
指定文件的輸出格式
7.2.1) DER(DER編碼的CRL對象)
7.2.2) PEM(默認的格式)(base64編碼的CRL對象)
7.3) -text:
以文本格式來打印CRL信息值。
7.4) -in filename
指定的輸入文件名。默認為標準輸入。
7.5) -out filename
指定的輸出文件名。默認為標準輸出。
7.6) -hash
輸出頒發(fā)者信息值的哈希值。這一項可用于在文件中根據(jù)頒發(fā)者信息值的哈希值來查詢CRL對象。
7.7) -fingerprint
打印CRL對象的標識。
7.8) -issuer
輸出頒發(fā)者的信息值。
7.9) -lastupdate
輸出上一次更新的時間。
7.10) -nextupdate
打印出下一次更新的時間。
7.11) -CAfile file
指定CA文件,用來驗證該CRL對象是否合法。
7.12) -verify
是否驗證證書。
example1: 輸出CRL文件,包括(頒發(fā)者信息HASH值、上一次更新的時間、下一次更新的時間)
openssl crl -in crl.crl -text -issuer -hash -lastupdate –nextupdate
example2: 將PEM格式的CRL文件轉(zhuǎn)換為DER格式
openssl crl -in crl.pem -outform DER -out crl.der
8) crl2pkcs7: 用于CRL和PKCS#7之間的轉(zhuǎn)換
openssl crl2pkcs7 [options] <infile >outfile
轉(zhuǎn)換pem到spc
openssl crl2pkcs7 -nocrl -certfile venus.pem -outform DER -out venus.spc
https://www.openssl.org/docs/apps/crl2pkcs7.html
9) pkcs12: PKCS#12數(shù)據(jù)的管理
pkcs12文件工具,能生成和分析pkcs12文件。PKCS#12文件可以被用于多個項目,例如包含Netscape、 MSIE 和 MS Outlook
openssl pkcs12 [options]
http://blog.csdn.net/as3luyuan123/article/details/16105475
https://www.openssl.org/docs/apps/pkcs12.html
10) pkcs7: PCKS#7數(shù)據(jù)的管理
用于處理DER或者PEM格式的pkcs#7文件
openssl pkcs7 [options] <infile >outfile
http://blog.csdn.net/as3luyuan123/article/details/16105407
https://www.openssl.org/docs/apps/pkcs7.html
- openssl list-message-digest-commands(消息摘要命令)
- dgst: dgst用于計算消息摘要
openssl dgst [args]
1.1) -hex
以16進制形式輸出摘要
1.2) -binary
以二進制形式輸出摘要
1.3) -sign file
以私鑰文件對生成的摘要進行簽名
1.4) -verify file
使用公鑰文件對私鑰簽名過的摘要文件進行驗證
1.5) -prverify file
以私鑰文件對公鑰簽名過的摘要文件進行驗證
verify a signature using private key in file
1.6) 加密處理
1.6.1) -md5: MD5
1.6.2) -md4: MD4
1.6.3) -sha1: SHA1
1.6.4) -ripemd160
example1: 用SHA1算法計算文件file.txt的哈西值,輸出到stdout
openssl dgst -sha1 file.txt
example2: 用dss1算法驗證file.txt的數(shù)字簽名dsasign.bin,驗證的private key為DSA算法產(chǎn)生的文件dsakey.pem
openssl dgst -dss1 -prverify dsakey.pem -signature dsasign.bin file.txt 2) sha1: 用于進行RSA處理
openssl sha1 [args]
2.1) -sign file
用于RSA算法的私鑰文件
2.2) -out file
輸出文件愛你
2.3) -hex
以16進制形式輸出
2.4) -binary
以二進制形式輸出
example1: 用SHA1算法計算文件file.txt的HASH值,輸出到文件digest.txt
openssl sha1 -out digest.txt file.txt
example2: 用sha1算法為文件file.txt簽名,輸出到文件rsasign.bin,簽名的private key為RSA算法產(chǎn)生的文件rsaprivate.pem
openssl sha1 -sign rsaprivate.pem -out rsasign.bin file.txt
- dgst: dgst用于計算消息摘要
- openssl list-cipher-commands (Cipher命令的列表)
- 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
- 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
- rc2
- rc2-40-cbc
- rc2-64-cbc
- rc2-cbc
- rc2-cfb
- rc2-ecb
- rc2-ofb
- rc4
- rc4-40</pre>
參考:
https://blog.csdn.net/oldmtn/article/details/52208747
https://blog.csdn.net/gengxiaoming7/article/details/78505107