openssl生成證書的原理及使用場景

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 ==

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

image

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

一、openssl 簡介

openssl 是目前最流行的 SSL 密碼庫工具,其提供了一個通用、健壯、功能完備的工具套件,用以支持SSL/TLS 協(xié)議的實現(xiàn)。
官網(wǎng):https://www.openssl.org/source/

構成部分

  1. 密碼算法庫

  2. 密鑰和證書封裝管理功能

  3. SSL通信API接口

用途

  1. 建立 RSA、DH、DSA key 參數(shù)

  2. 建立 X.509 證書、證書簽名請求(CSR)和CRLs(證書回收列表)

  3. 計算消息摘要

  4. 使用各種 Cipher加密/解密

  5. SSL/TLS 客戶端以及服務器的測試

  6. 處理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
  1. openssl list-message-digest-commands(消息摘要命令)
    1. 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
  2. openssl list-cipher-commands (Cipher命令的列表)
    1. aes-128-cbc
    2. aes-128-ecb
    3. aes-192-cbc
    4. aes-192-ecb
    5. aes-256-cbc
    6. aes-256-ecb
    7. base64
    8. bf
    9. bf-cbc
    10. bf-cfb
    11. bf-ecb
    12. bf-ofb
    13. cast
    14. cast-cbc
    15. cast5-cbc
    16. cast5-cfb
    17. cast5-ecb
    18. cast5-ofb
    19. des
    20. des-cbc
    21. des-cfb
    22. des-ecb
    23. des-ede
    24. des-ede-cbc
    25. des-ede-cfb
    26. des-ede-ofb
    27. des-ede3
    28. des-ede3-cbc
    29. des-ede3-cfb
    30. des-ede3-ofb
    31. des-ofb
    32. des3
    33. desx
    34. rc2
    35. rc2-40-cbc
    36. rc2-64-cbc
    37. rc2-cbc
    38. rc2-cfb
    39. rc2-ecb
    40. rc2-ofb
    41. rc4
    42. rc4-40</pre>

參考:

https://blog.csdn.net/oldmtn/article/details/52208747

https://blog.csdn.net/gengxiaoming7/article/details/78505107

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,406評論 6 538
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,034評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,413評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,449評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,165評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,559評論 1 325
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,606評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,781評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,327評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,084評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,278評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,849評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,495評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,927評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,172評論 1 291
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,010評論 3 396
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,241評論 2 375

推薦閱讀更多精彩內(nèi)容