1. 程式人生 > >openssl基本原理 + 生成證書 + 使用實例-測試場景使用

openssl基本原理 + 生成證書 + 使用實例-測試場景使用

ctr 基本原理 sha cursor 應用 1.4 x509證書 ica acer

1. 基本原理

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

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


公司一個項目要進行交易數據傳輸,因為這個項目銀行那邊也是剛剛開始啟動,所有的支持只有一個傳輸字段的說明文檔,好吧,總的有人做事不是嘛,於是接口開發正式展開,第一步的難點就是加密解密,我選擇使用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 http://www.cnblogs.com/phpinfo/archive/2013/08/09/3246376.html ==

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

技術分享圖片

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

一、openssl 簡介

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

構成部分

  1. 密碼算法庫

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

  3. SSL通信API接口

用途

  1. 建立 RSA、DH、DSA key 參數

  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 進行密碼輸入,否則會提示輸入密碼;
生成加密後的內容如:

-----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 進行密碼輸入,否則會提示輸入密碼;

轉換命令

私鑰轉非加密

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

私鑰轉加密

openssl rsa -in rsa_private.key -aes256 -passout pass:111111 -out rsa_aes_private.key

私鑰PEM轉DER

openssl rsa -in rsa_private.key -outform der-out rsa_aes_private.der

-inform和-outform 參數制定輸入輸出格式,由der轉pem格式同理

查看私鑰明細

openssl rsa -in rsa_private.key -noout -text

使用-pubin參數可查看公鑰明細

私鑰PKCS#1轉PKCS#8

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

其中-passout指定了密碼,輸出的pkcs8格式密鑰為加密形式,pkcs8默認采用des3 加密算法,內容如下:

-----BEGIN ENCRYPTED PRIVATE KEY-----
Base64 Encoded Data
-----END ENCRYPTED PRIVATE KEY-----

使用-nocrypt參數可以輸出無加密的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 表示私鑰不加密,若不帶參數將提示輸入密碼;

-x509表示輸出證書,

-days36500 為有效期,

此後根據提示輸入證書擁有者信息;

操作步驟如下:提示填寫過程中如果想刪除填寫的內容,用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 []:[email protected]

此時就生成了:未加密碼的私鑰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

如果想執行自動輸入證書擁有者信息,可使用-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/[email protected]"

如果想使用已有RSA 私鑰生成CA自簽名證書,請操作如下:

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

-new 指生成證書請求,加上-x509 表示直接輸出證書,-key 指定私鑰文件,其余選項與上述命令相同

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

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

實戰如下:對比下加-passout pass:111111的區別

[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 []:[email protected]

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/[email protected]"

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


查看CSR 的細節

cat server.csr
-----BEGIN CERTIFICATE REQUEST-----
Base64EncodedData
-----END CERTIFICATE REQUEST-----

openssl req -noout -text -in server.csr

使用 CA 證書及CA密鑰 對服務器請求簽發證書進行簽發,生成 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/[email protected]
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/[email protected]
issuer=/C=CN/ST=GD/L=SZ/O=viroot/OU=dev/CN=viroot.com/[email protected] 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 命令參考


1. openssl list-standard-commands(標準命令)   
     1) asn1parse: asn1parse用於解釋用ANS.1語法書寫的語句(ASN一般用於定義語法的構成) 
     2) ca: ca用於CA的管理 
    openssl ca [options]:       
        2.1) -selfsign
        使用對證書請求進行簽名的密鑰對來簽發證書。即"自簽名",這種情況發生在生成證書的客戶端、簽發證書的CA都是同一臺機器(也是我們大多數實驗中的情況),我們可以使用同一個
密鑰對來進行"自簽名"
        2.2) -in file
        需要進行處理的PEM格式的證書       
        2.3) -out file
        處理結束後輸出的證書文件       
        2.4) -cert file
        用於簽發的根CA證書     
        2.5) -days arg 
        指定簽發的證書的有效時間       
        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證書簽發請求(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私鑰文件,用於簽發  
        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密鑰創建一個自簽署的CA證書(X.509結構) 
    openssl req -new -x509 -days 3650 -key server.key -out ca.crt 
    example2: 用server.key生成證書簽署請求CSR(這個CSR用於之外發送待CA中心等待簽發)
    openssl req -new -key server.key -out server.csr
    example3: 查看CSR的細節
    openssl req -noout -text -in server.csr    
    4) genrsa: 生成RSA參數
    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)的密碼(使用這個參數就省去了console交互提示輸入密碼的環節)        
    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數據管理
    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  
        如果希望輸出的密鑰文件繼續使用加密算法的話則指定密碼 
        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數據 
        5.13) -noout: 不打印密鑰key數據 
        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:
    本指令是一個功能很豐富的證書處理工具。可以用來顯示證書的內容,轉換其格式,給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            
        表明輸入文件是一個"請求簽發證書文件(CSR)",等待進行簽發 
        6.6) -days arg       
        表明將要簽發的證書的有效時間 
        6.7) -CA arg 
        指定用於簽發請求證書的根CA證書 
        6.8) -CAform arg     
        根CA證書格式(默認是PEM) 
        6.9) -CAkey arg      
        指定用於簽發請求證書的CA私鑰證書文件,如果這個option沒有參數輸入,那麽缺省認為私有密鑰在CA證書文件裏有        
        6.10) -CAkeyform arg  
        指定根CA私鑰證書文件格式(默認為PEM格式)        
        6.11) -CAserial arg   
        指定序列號文件(serial number file)        
        6.12) -CAcreateserial 
        如果序列號文件(serial number file)沒有指定,則自動創建它     
    example1: 轉換DER證書為PEM格式
    openssl x509 -in cert.cer -inform DER -outform PEM -out cert.pem
    example2: 使用根CA證書對"請求簽發證書"進行簽發,生成x509格式證書
    openssl x509 -req -days 3650 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt
    example3: 打印出證書的內容
    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
        輸出頒發者信息值的哈希值。這一項可用於在文件中根據頒發者信息值的哈希值來查詢CRL對象。        
        7.7) -fingerprint
        打印CRL對象的標識。        
        7.8) -issuer
        輸出頒發者的信息值。        
        7.9) -lastupdate
        輸出上一次更新的時間。        
        7.10) -nextupdate
        打印出下一次更新的時間。 
        7.11) -CAfile file
        指定CA文件,用來驗證該CRL對象是否合法。 
        7.12) -verify
        是否驗證證書。        
    example1: 輸出CRL文件,包括(頒發者信息HASH值、上一次更新的時間、下一次更新的時間)
    openssl crl -in crl.crl -text -issuer -hash -lastupdate –nextupdate 
    example2: 將PEM格式的CRL文件轉換為DER格式
    openssl crl -in crl.pem -outform DER -out crl.der  

    8) crl2pkcs7: 用於CRL和PKCS#7之間的轉換 
    openssl crl2pkcs7 [options] <infile >outfile
    轉換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數據的管理
    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數據的管理 
    用於處理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
 2. 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的數字簽名dsasign.bin,驗證的private key為DSA算法產生的文件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算法產生的文件rsaprivate.pem
    openssl sha1 -sign rsaprivate.pem -out rsasign.bin file.txt
3. 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



參考:

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

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


openssl基本原理 + 生成證書 + 使用實例-測試場景使用