openssl生成證書的原理及使用場(chǎng)景

1. 基本原理

參考:http://www.cnblogs.com/phpinfo/archive/2013/08/09/3246376.html

== Begin http://www.cnblogs.com/phpinfo/archive/2013/08/09/3246376.html ==

公司一個(gè)項(xiàng)目要進(jìn)行交易數(shù)據(jù)傳輸,因?yàn)檫@個(gè)項(xiàng)目銀行那邊也是剛剛開始啟動(dòng),所有的支持只有一個(gè)傳輸字段的說明文檔,好吧,總的有人做事不是嘛,于是接口開發(fā)正式展開,第一步的難點(diǎn)就是加密解密,我選擇使用OpenSSL.

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

公鑰/私鑰/簽名/驗(yàn)證簽名/加密/解密/非對(duì)稱加密

我們一般的加密是用一個(gè)密碼加密文件,然后解密也用同樣的密碼.這很好理解,這個(gè)是對(duì)稱加密.而有些加密時(shí),加密用的一個(gè)密碼,而解密用另外一組密碼,這個(gè)叫非對(duì)稱加密,意思就是加密解密的密碼不一樣.初次接觸的人恐怕無論如何都理解不了.其實(shí)這是數(shù)學(xué)上的一個(gè)素?cái)?shù)積求因子的原理的應(yīng)用,如果你一定要搞懂,百度有大把大把的資料可以看,其結(jié)果就是用這一組密鑰中的一個(gè)來加密數(shù)據(jù),可以用另一個(gè)解開.是的沒錯(cuò),公鑰和私鑰都可以用來加密數(shù)據(jù),相反用另一個(gè)解開,公鑰加密數(shù)據(jù),然后私鑰解密的情況被稱為加密解密,私鑰加密數(shù)據(jù),公鑰解密一般被稱為簽名和驗(yàn)證簽名.

因?yàn)楣€加密的數(shù)據(jù)只有它相對(duì)應(yīng)的私鑰可以解開,所以你可以把公鑰給人和人,讓他加密他想要傳送給你的數(shù)據(jù),這個(gè)數(shù)據(jù)只有到了有私鑰的你這里,才可以解開成有用的數(shù)據(jù),其他人就是得到了,也看懂內(nèi)容.同理,如果你用你的私鑰對(duì)數(shù)據(jù)進(jìn)行簽名,那這個(gè)數(shù)據(jù)就只有配對(duì)的公鑰可以解開,有這個(gè)私鑰的只有你,所以如果配對(duì)的公鑰解開了數(shù)據(jù),就說明這數(shù)據(jù)是你發(fā)的,相反,則不是.這個(gè)被稱為簽名.

實(shí)際應(yīng)用中,一般都是和對(duì)方交換公鑰,然后你要發(fā)給對(duì)方的數(shù)據(jù),用他的公鑰加密,他得到后用他的私鑰解密,他要發(fā)給你的數(shù)據(jù),用你的公鑰加密,你得到后用你的私鑰解密,這樣最大程度保證了安全性.

RSA/DSA/SHA/MD5

非對(duì)稱加密的算法有很多,比較著名的有RSA/DSA ,不同的是RSA可以用于加/解密,也可以用于簽名驗(yàn)簽,DSA則只能用于簽名.至于SHA則是一種和md5相同的算法,它不是用于加密解密或者簽名的,它被稱為摘要算法.就是通過一種算法,依據(jù)數(shù)據(jù)內(nèi)容生成一種固定長(zhǎng)度的摘要,這串摘要值與原數(shù)據(jù)存在對(duì)應(yīng)關(guān)系,就是原數(shù)據(jù)會(huì)生成這個(gè)摘要,但是,這個(gè)摘要是不能還原成原數(shù)據(jù)的,嗯....,正常情況下是這樣的,這個(gè)算法起的作用就是,如果你把原數(shù)據(jù)修改一點(diǎn)點(diǎn),那么生成的摘要都會(huì)不同,傳輸過程中把原數(shù)據(jù)給你再給你一個(gè)摘要,你把得到的原數(shù)據(jù)同樣做一次摘要算法,與給你的摘要相比較就可以知道這個(gè)數(shù)據(jù)有沒有在傳輸過程中被修改了.

實(shí)際應(yīng)用過程中,因?yàn)樾枰用艿臄?shù)據(jù)可能會(huì)很大,進(jìn)行加密費(fèi)時(shí)費(fèi)力,所以一般都會(huì)把原數(shù)據(jù)先進(jìn)行摘要,然后對(duì)這個(gè)摘要值進(jìn)行加密,將原數(shù)據(jù)的明文和加密后的摘要值一起傳給你.這樣你解開加密后的摘要值,再和你得到的數(shù)據(jù)進(jìn)行的摘要值對(duì)應(yīng)一下就可以知道數(shù)據(jù)有沒有被修改了,而且,因?yàn)樗借€只有你有,只有你能解密摘要值,所以別人就算把原數(shù)據(jù)做了修改,然后生成一個(gè)假的摘要給你也是不行的,你這邊用密鑰也根本解不開.

CA/PEM/DER/X509/PKCS

一般的公鑰不會(huì)用明文傳輸給別人的,正常情況下都會(huì)生成一個(gè)文件,這個(gè)文件就是公鑰文件,然后這個(gè)文件可以交給其他人用于加密,但是傳輸過程中如果有人惡意破壞,將你的公鑰換成了他的公鑰,然后得到公鑰的一方加密數(shù)據(jù),不是他就可以用他自己的密鑰解密看到數(shù)據(jù)了嗎,為了解決這個(gè)問題,需要一個(gè)公證方來做這個(gè)事,任何人都可以找它來確認(rèn)公鑰是誰發(fā)的.這就是CA,CA確認(rèn)公鑰的原理也很簡(jiǎn)單,它將它自己的公鑰發(fā)布給所有人,然后一個(gè)想要發(fā)布自己公鑰的人可以將自己的公鑰和一些身份信息發(fā)給CA,CA用自己的密鑰進(jìn)行加密,這里也可以稱為簽名.然后這個(gè)包含了你的公鑰和你的信息的文件就可以稱為證書文件了.這樣一來所有得到一些公鑰文件的人,通過CA的公鑰解密了文件,如果正常解密那么機(jī)密后里面的信息一定是真的,因?yàn)榧用芊街豢赡苁荂A,其他人沒它的密鑰啊.這樣你解開公鑰文件,看看里面的信息就知道這個(gè)是不是那個(gè)你需要用來加密的公鑰了.

實(shí)際應(yīng)用中,一般人都不會(huì)找CA去簽名,因?yàn)槟鞘鞘斟X的,所以可以自己做一個(gè)自簽名的證書文件,就是自己生成一對(duì)密鑰,然后再用自己生成的另外一對(duì)密鑰對(duì)這對(duì)密鑰進(jìn)行簽名,這個(gè)只用于真正需要簽名證書的人,普通的加密解密數(shù)據(jù),直接用公鑰和私鑰來做就可以了.

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

== End http://www.cnblogs.com/phpinfo/archive/2013/08/09/3246376.html ==

為了方便理解,我畫了一個(gè)圖,如下:

image

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

一、openssl 簡(jiǎn)介

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

構(gòu)成部分

  1. 密碼算法庫

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

  3. SSL通信API接口

用途

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

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

  3. 計(jì)算消息摘要

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

  5. SSL/TLS 客戶端以及服務(wù)器的測(cè)試

  6. 處理S/MIME 或者加密郵件

二、RSA密鑰操作

默認(rèn)情況下,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 進(jìn)行密碼輸入,否則會(huì)提示輸入密碼;
生成加密后的內(nèi)容如:

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC,5584D000DDDD53DD5B12AE935F05A007
Base64 Encoded 
Data-----END RSA PRIVATE KEY-----

此時(shí)若生成公鑰,需要提供密碼

openssl rsa -in rsa_aes_private.key -passin pass:111111 -pubout -out rsa_public.key

其中 passout 代替shell 進(jìn)行密碼輸入,否則會(huì)提示輸入密碼;

轉(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格式同理

查看私鑰明細(xì)

openssl rsa -in rsa_private.key -noout -text

使用-pubin參數(shù)可查看公鑰明細(xì)

私鑰PKCS#1轉(zhuǎn)PKCS#8

openssl pkcs8 -topk8 -in rsa_private.key -passout pass:111111 -out pkcs8_private.key

其中-passout指定了密碼,輸出的pkcs8格式密鑰為加密形式,pkcs8默認(rèn)采用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私鑰(測(cè)試場(chǎng)景步驟)

測(cè)試場(chǎng)景步驟1:生成 RSA 私鑰和自簽名證書:

openssl req -newkey rsa:2048 -nodes -keyout rsa_private.key -x509 -days 36500 -out cert.crt

注釋:

req是證書請(qǐng)求的子命令,

-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
此時(shí)就生成了:未加密碼的私鑰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í)行自動(dòng)輸入證書擁有者信息,可使用-subj選項(xiàng):
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自簽名證書,請(qǐng)操作如下:
openssl req -new -x509 -days 36500 -key rsa_private.key -out cert.crt
-new 指生成證書請(qǐng)求,加上-x509 表示直接輸出證書,-key 指定私鑰文件,其余選項(xiàng)與上述命令相同

四、生成服務(wù)器簽名請(qǐng)求文件及CA 簽名頒發(fā)服務(wù)器證書()

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

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

測(cè)試場(chǎng)景步驟2:使用 RSA私鑰生成 CSR 簽名請(qǐng)求:server.key為aes256位加密
openssl genrsa -aes256 -passout pass:111111 -out server.key 2048
openssl req -new -key server.key -out server.csr
實(shí)戰(zhàn)如下:對(duì)比下加-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手動(dòng)輸入密碼
Verifying - Enter pass phrase for server.key: 111111手動(dòng)輸入密碼
[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"

*** 此時(shí)生成的 csr簽名請(qǐng)求文件可提交至 CA進(jìn)行簽發(fā) ***

查看CSR 的細(xì)節(jié)
cat server.csr
-----BEGIN CERTIFICATE REQUEST-----
Base64EncodedData
-----END CERTIFICATE REQUEST-----

openssl req -noout -text -in server.csr
使用 CA 證書及CA密鑰 對(duì)服務(wù)器請(qǐng)求簽發(fā)證書進(jìn)行簽發(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 選項(xiàng)用于指定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

此時(shí)對(duì)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

此時(shí)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

至此測(cè)試場(chǎng)景私有證書配置完成

五、證書查看及轉(zhuǎn)換

查看證書細(xì)節(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指導(dǎo)出pkcs#12 證書,-inkey 指定了私鑰文件,-passin 為私鑰(文件)密碼(nodes為無加密),-password 指定 p12文件的密碼(導(dǎo)入導(dǎo)出)

** 將 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指示同時(shí)添加證書鏈,-CAfile 指定了CA證書,導(dǎo)出的p12文件將包含多個(gè)證書。(其他選項(xiàng):-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文件的密碼(導(dǎo)入導(dǎo)出),-passout指輸出私鑰的加密密碼(nodes為無加密)
導(dǎo)出的文件為pem格式,同時(shí)包含證書和私鑰(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(標(biāo)準(zhǔn)命令)
1) asn1parse: asn1parse用于解釋用ANS.1語法書寫的語句(ASN一般用于定義語法的構(gòu)成)
2) ca: ca用于CA的管理
openssl ca [options]:
2.1) -selfsign
使用對(duì)證書請(qǐng)求進(jìn)行簽名的密鑰對(duì)來簽發(fā)證書。即"自簽名",這種情況發(fā)生在生成證書的客戶端、簽發(fā)證書的CA都是同一臺(tái)機(jī)器(也是我們大多數(shù)實(shí)驗(yàn)中的情況),我們可以使用同一個(gè)
密鑰對(duì)來進(jìn)行"自簽名"
2.2) -in file
需要進(jìn)行處理的PEM格式的證書
2.3) -out file
處理結(jié)束后輸出的證書文件
2.4) -cert file
用于簽發(fā)的根CA證書
2.5) -days arg
指定簽發(fā)的證書的有效時(shí)間
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證書簽署請(qǐng)求證書
openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key

3) req: X.509證書簽發(fā)請(qǐng)求(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        
    用于簽名待生成的請(qǐng)求證書的私鑰文件的解密密碼        
    3.6) -key file
    用于簽名待生成的請(qǐng)求證書的私鑰文件        
    3.7) -keyform arg  
        3.7.1) DER            
        3.7.2) NET            
        3.7.3) PEM        
    3.8) -new
    新的請(qǐng)求        
    3.9) -x509          
    輸出一個(gè)X509格式的證書 
    3.10) -days
    X509證書的有效時(shí)間  
    3.11) -newkey rsa:bits 
    生成一個(gè)bits長(zhǎng)度的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)建一個(gè)自簽署的CA證書(X.509結(jié)構(gòu)) 
openssl req -new -x509 -days 3650 -key server.key -out ca.crt 
example2: 用server.key生成證書簽署請(qǐng)求CSR(這個(gè)CSR用于之外發(fā)送待CA中心等待簽發(fā))
openssl req -new -key server.key -out server.csr
example3: 查看CSR的細(xì)節(jié)
openssl req -noout -text -in server.csr    
4) genrsa: 生成RSA參數(shù)
openssl genrsa [args] [numbits]
    [args]        
4.1) 對(duì)生成的私鑰文件是否要使用加密算法進(jìn)行對(duì)稱加密:           
     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為對(duì)稱加密(des、des、aes)的密碼(使用這個(gè)參數(shù)就省去了console交互提示輸入密碼的環(huán)節(jié))        
4.3) -out file: 輸出證書私鑰文件
    [numbits]: 密鑰長(zhǎng)度
example: 生成一個(gè)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
    輸入這個(gè)加密密鑰文件的解密密鑰(如果在生成這個(gè)密鑰文件的時(shí)候,選擇了加密算法了的話)        
    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: 對(duì)私鑰文件進(jìn)行解密
openssl rsa -in server.key -passin pass:111 -out server_nopass.key
example:2: 利用私鑰文件生成對(duì)應(yīng)的公鑰文件
openssl rsa -in server.key -passin pass:111 -pubout -out server_public.key    6) x509:
本指令是一個(gè)功能很豐富的證書處理工具。可以用來顯示證書的內(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            
    表明輸入文件是一個(gè)"請(qǐng)求簽發(fā)證書文件(CSR)",等待進(jìn)行簽發(fā) 
    6.6) -days arg       
    表明將要簽發(fā)的證書的有效時(shí)間 
    6.7) -CA arg 
    指定用于簽發(fā)請(qǐng)求證書的根CA證書 
    6.8) -CAform arg     
    根CA證書格式(默認(rèn)是PEM) 
    6.9) -CAkey arg      
    指定用于簽發(fā)請(qǐng)求證書的CA私鑰證書文件,如果這個(gè)option沒有參數(shù)輸入,那么缺省認(rèn)為私有密鑰在CA證書文件里有        
    6.10) -CAkeyform arg  
    指定根CA私鑰證書文件格式(默認(rèn)為PEM格式)        
    6.11) -CAserial arg   
    指定序列號(hào)文件(serial number file)        
    6.12) -CAcreateserial 
    如果序列號(hào)文件(serial number file)沒有指定,則自動(dòng)創(chuàng)建它     
example1: 轉(zhuǎn)換DER證書為PEM格式
openssl x509 -in cert.cer -inform DER -outform PEM -out cert.pem
example2: 使用根CA證書對(duì)"請(qǐng)求簽發(fā)證書"進(jìn)行簽發(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對(duì)象)            
         7.1.2) PEM(默認(rèn)的格式)(base64編碼的CRL對(duì)象)        
    7.2) -outform arg
    指定文件的輸出格式 
        7.2.1) DER(DER編碼的CRL對(duì)象)            
        7.2.2) PEM(默認(rèn)的格式)(base64編碼的CRL對(duì)象)        
    7.3) -text: 
    以文本格式來打印CRL信息值。        
    7.4) -in filename
    指定的輸入文件名。默認(rèn)為標(biāo)準(zhǔn)輸入。        
    7.5) -out filename
    指定的輸出文件名。默認(rèn)為標(biāo)準(zhǔn)輸出。        
    7.6) -hash
    輸出頒發(fā)者信息值的哈希值。這一項(xiàng)可用于在文件中根據(jù)頒發(fā)者信息值的哈希值來查詢CRL對(duì)象。        
    7.7) -fingerprint
    打印CRL對(duì)象的標(biāo)識(shí)。        
    7.8) -issuer
    輸出頒發(fā)者的信息值。        
    7.9) -lastupdate
    輸出上一次更新的時(shí)間。        
    7.10) -nextupdate
    打印出下一次更新的時(shí)間。 
    7.11) -CAfile file
    指定CA文件,用來驗(yàn)證該CRL對(duì)象是否合法。 
    7.12) -verify
    是否驗(yàn)證證書。        
example1: 輸出CRL文件,包括(頒發(fā)者信息HASH值、上一次更新的時(shí)間、下一次更新的時(shí)間)
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文件可以被用于多個(gè)項(xiàng)目,例如包含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用于計(jì)算消息摘要
      openssl dgst [args]
      1.1) -hex
      以16進(jìn)制形式輸出摘要
      1.2) -binary
      以二進(jìn)制形式輸出摘要
      1.3) -sign file
      以私鑰文件對(duì)生成的摘要進(jìn)行簽名
      1.4) -verify file
      使用公鑰文件對(duì)私鑰簽名過的摘要文件進(jìn)行驗(yàn)證
      1.5) -prverify file
      以私鑰文件對(duì)公鑰簽名過的摘要文件進(jìn)行驗(yàn)證
      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算法計(jì)算文件file.txt的哈西值,輸出到stdout
      openssl dgst -sha1 file.txt
      example2: 用dss1算法驗(yàn)證file.txt的數(shù)字簽名dsasign.bin,驗(yàn)證的private key為DSA算法產(chǎn)生的文件dsakey.pem
      openssl dgst -dss1 -prverify dsakey.pem -signature dsasign.bin file.txt 2) sha1: 用于進(jìn)行RSA處理
      openssl sha1 [args]
      2.1) -sign file
      用于RSA算法的私鑰文件
      2.2) -out file
      輸出文件愛你
      2.3) -hex
      以16進(jìn)制形式輸出
      2.4) -binary
      以二進(jìn)制形式輸出
      example1: 用SHA1算法計(jì)算文件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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容