OpenSSL初接觸的人恐怕最難的在于先理解各種概念
公鑰/私鑰/簽名/驗(yàn)證簽名/加密/解密/非對(duì)稱(chēng)加密
我們一般的加密是用一個(gè)密碼加密文件,然后解密也用同樣的密碼.這很好理解,這個(gè)是對(duì)稱(chēng)加密.而有些加密時(shí),加密用的一個(gè)密碼,而解密用另外一組密碼,這個(gè)叫非對(duì)稱(chēng)加密,意思就是加密解密的密碼不一樣.初次接觸的人恐怕無(wú)論如何都理解不了.其實(shí)這是數(shù)學(xué)上的一個(gè)素?cái)?shù)積求因子的原理的應(yīng)用,如果你一定要搞懂,百度有大把大把的資料可以看,其結(jié)果就是用這一組密鑰中的一個(gè)來(lái)加密數(shù)據(jù),可以用另一個(gè)解開(kāi).是的沒(méi)錯(cuò),公鑰和私鑰都可以用來(lái)加密數(shù)據(jù),相反用另一個(gè)解開(kāi),公鑰加密數(shù)據(jù),然后私鑰解密的情況被稱(chēng)為加密解密,私鑰加密數(shù)據(jù),公鑰解密一般被稱(chēng)為簽名和驗(yàn)證簽名.
因?yàn)楣€加密的數(shù)據(jù)只有它相對(duì)應(yīng)的私鑰可以解開(kāi),所以你可以把公鑰給人和人,讓他加密他想要傳送給你的數(shù)據(jù),這個(gè)數(shù)據(jù)只有到了有私鑰的你這里,才可以解開(kāi)成有用的數(shù)據(jù),其他人就是得到了,也看懂內(nèi)容.同理,如果你用你的私鑰對(duì)數(shù)據(jù)進(jìn)行簽名,那這個(gè)數(shù)據(jù)就只有配對(duì)的公鑰可以解開(kāi),有這個(gè)私鑰的只有你,所以如果配對(duì)的公鑰解開(kāi)了數(shù)據(jù),就說(shuō)明這數(shù)據(jù)是你發(fā)的,相反,則不是.這個(gè)被稱(chēng)為簽名.
實(shí)際應(yīng)用中,一般都是和對(duì)方交換公鑰,然后你要發(fā)給對(duì)方的數(shù)據(jù),用他的公鑰加密,他得到后用他的私鑰解密,他要發(fā)給你的數(shù)據(jù),用你的公鑰加密,你得到后用你的私鑰解密,這樣最大程度保證了安全性.
RSA/DSA/SHA/MD5
非對(duì)稱(chēng)加密的算法有很多,比較著名的有RSA/DSA ,不同的是RSA可以用于加/解密,也可以用于簽名驗(yàn)簽,DSA則只能用于簽名.至于SHA則是一種和md5相同的算法,它不是用于加密解密或者簽名的,它被稱(chēng)為摘要算法.就是通過(guò)一種算法,依據(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ì)不同,傳輸過(guò)程中把原數(shù)據(jù)給你再給你一個(gè)摘要,你把得到的原數(shù)據(jù)同樣做一次摘要算法,與給你的摘要相比較就可以知道這個(gè)數(shù)據(jù)有沒(méi)有在傳輸過(guò)程中被修改了.
實(shí)際應(yīng)用過(guò)程中,因?yàn)樾枰用艿臄?shù)據(jù)可能會(huì)很大,進(jìn)行加密費(fèi)時(shí)費(fèi)力,所以一般都會(huì)把原數(shù)據(jù)先進(jìn)行摘要,然后對(duì)這個(gè)摘要值進(jìn)行加密,將原數(shù)據(jù)的明文和加密后的摘要值一起傳給你.這樣你解開(kāi)加密后的摘要值,再和你得到的數(shù)據(jù)進(jìn)行的摘要值對(duì)應(yīng)一下就可以知道數(shù)據(jù)有沒(méi)有被修改了,而且,因?yàn)樗借€只有你有,只有你能解密摘要值,所以別人就算把原數(shù)據(jù)做了修改,然后生成一個(gè)假的摘要給你也是不行的,你這邊用密鑰也根本解不開(kāi).
CA/PEM/DER/X509/PKCS
一般的公鑰不會(huì)用明文傳輸給別人的,正常情況下都會(huì)生成一個(gè)文件,這個(gè)文件就是公鑰文件,然后這個(gè)文件可以交給其他人用于加密,但是傳輸過(guò)程中如果有人惡意破壞,將你的公鑰換成了他的公鑰,然后得到公鑰的一方加密數(shù)據(jù),不是他就可以用他自己的密鑰解密看到數(shù)據(jù)了嗎,為了解決這個(gè)問(wèn)題,需要一個(gè)公證方來(lái)做這個(gè)事,任何人都可以找它來(lái)確認(rèn)公鑰是誰(shuí)發(fā)的.這就是CA,CA確認(rèn)公鑰的原理也很簡(jiǎn)單,它將它自己的公鑰發(fā)布給所有人,然后一個(gè)想要發(fā)布自己公鑰的人可以將自己的公鑰和一些身份信息發(fā)給CA,CA用自己的密鑰進(jìn)行加密,這里也可以稱(chēng)為簽名.然后這個(gè)包含了你的公鑰和你的信息的文件就可以稱(chēng)為證書(shū)文件了.這樣一來(lái)所有得到一些公鑰文件的人,通過(guò)CA的公鑰解密了文件,如果正常解密那么機(jī)密后里面的信息一定是真的,因?yàn)榧用芊街豢赡苁荂A,其他人沒(méi)它的密鑰啊.這樣你解開(kāi)公鑰文件,看看里面的信息就知道這個(gè)是不是那個(gè)你需要用來(lái)加密的公鑰了.
實(shí)際應(yīng)用中,一般人都不會(huì)找CA去簽名,因?yàn)槟鞘鞘斟X(qián)的,所以可以自己做一個(gè)自簽名的證書(shū)文件,就是自己生成一對(duì)密鑰,然后再用自己生成的另外一對(duì)密鑰對(duì)這對(duì)密鑰進(jìn)行簽名,這個(gè)只用于真正需要簽名證書(shū)的人,普通的加密解密數(shù)據(jù),直接用公鑰和私鑰來(lái)做就可以了.
密鑰文件的格式用OpenSSL生成的就只有PEM和DER兩種格式,PEM的是將密鑰用base64編碼表示出來(lái)的,直接打開(kāi)你能看到一串的英文字母,DER格式是二進(jìn)制的密鑰文件,直接打開(kāi),你可以看到........你什么也看不懂!.X509是通用的證書(shū)文件格式定義.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 ==
為了方便理解,我畫(huà)了一個(gè)圖郭宝,如下:
使用 openssl 生成證書(shū)(含openssl詳解)
一、openssl 簡(jiǎn)介
openssl 是目前最流行的 SSL 密碼庫(kù)工具,其提供了一個(gè)通用、健壯粉洼、功能完備的工具套件茂嗓,用以支持SSL/TLS 協(xié)議的實(shí)現(xiàn)。
官網(wǎng): https://www.openssl.org/source/
構(gòu)成部分
密碼算法庫(kù)
密鑰和證書(shū)封裝管理功能
SSL通信API接口
用途
建立 RSA康栈、DH、DSA key 參數(shù)
建立 X.509 證書(shū)喷橙、證書(shū)簽名請(qǐng)求(CSR)和CRLs(證書(shū)回收列表)
計(jì)算消息摘要
使用各種 Cipher加密/解密
SSL/TLS 客戶(hù)端以及服務(wù)器的測(cè)試
處理S/MIME 或者加密郵件
二啥么、RSA密鑰操作
默認(rèn)情況下,openssl 輸出格式為 PKCS#1-PEM
生成RSA私鑰(無(wú)加密)
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ù)可以輸出無(wú)加密的pkcs8密鑰,如下:
-----BEGIN PRIVATE KEY-----
Base64 Encoded Data
-----END PRIVATE KEY-----
三轿曙、生成CA自簽名證書(shū)和RSA私鑰(測(cè)試場(chǎng)景步驟)
測(cè)試場(chǎng)景步驟1:生成 RSA 私鑰和自簽名證書(shū):
openssl req -newkey rsa:2048 -nodes -keyout rsa_private.key -x509 -days 36500 -out cert.crt
注釋?zhuān)?/p>
req是證書(shū)請(qǐng)求的子命令弄捕,
-newkey rsa:2048
-keyout private_key.pem 表示生成私鑰(PKCS8格式),
-nodes 表示私鑰不加密导帝,若不帶參數(shù)將提示輸入密碼守谓;
-x509表示輸出證書(shū),
-days36500 為有效期舟扎,
此后根據(jù)提示輸入證書(shū)擁有者信息分飞;
操作步驟如下:提示填寫(xiě)過(guò)程中如果想刪除填寫(xiě)的內(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自簽名證書(shū)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)輸入證書(shū)擁有者信息睹限,可使用-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自簽名證書(shū)譬猫,請(qǐng)操作如下:
openssl req -new -x509 -days 36500 -key rsa_private.key -out cert.crt
-new 指生成證書(shū)請(qǐng)求,加上-x509 表示直接輸出證書(shū)羡疗,-key 指定私鑰文件染服,其余選項(xiàng)與上述命令相同
四、生成服務(wù)器簽名請(qǐng)求文件及CA 簽名頒發(fā)服務(wù)器證書(shū)()
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ū)信息完成,也可以命令行指定各類(lèi)參數(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 證書(shū)及CA密鑰 對(duì)服務(wù)器請(qǐng)求簽發(fā)證書(shū)進(jìn)行簽發(fā)送矩,生成 x509證書(shū):
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
證書(shū)位置:
[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)景私有證書(shū)配置完成
五菇怀、證書(shū)查看及轉(zhuǎn)換
查看證書(shū)細(xì)節(jié)
openssl x509 -in cert.crt -noout -text
轉(zhuǎn)換證書(shū)編碼格式
openssl x509 -in cert.cer -inform DER -outform PEM -out cert.pem
合成 pkcs#12 證書(shū)(含私鑰)
** 將 pem 證書(shū)和私鑰轉(zhuǎn) pkcs#12 證書(shū) **
openssl pkcs12 -export -in server.crt -inkey server.key -passin pass:111111 -password pass:111111 -out server.p12
其中-export指導(dǎo)出pkcs#12 證書(shū),-inkey 指定了私鑰文件晌块,-passin 為私鑰(文件)密碼(nodes為無(wú)加密)爱沟,-password 指定 p12文件的密碼(導(dǎo)入導(dǎo)出)
** 將 pem 證書(shū)和私鑰/CA 證書(shū) 合成pkcs#12 證書(shū)**
openssl pkcs12 -export -in server.crt -inkey server.key -passin pass:111111 \ -chain -CAfile ca.crt -password pass:111111 -out server-all.p12
其中-chain指示同時(shí)添加證書(shū)鏈,-CAfile 指定了CA證書(shū)匆背,導(dǎo)出的p12文件將包含多個(gè)證書(shū)呼伸。(其他選項(xiàng):-name可用于指定server證書(shū)別名;-caname用于指定ca證書(shū)別名)
** pcks#12 提取PEM文件(含私鑰) **
openssl pkcs12 -in server.p12 -password pass:111111 -passout pass:111111 -out out/server.pem
其中-password 指定 p12文件的密碼(導(dǎo)入導(dǎo)出)钝尸,-passout指輸出私鑰的加密密碼(nodes為無(wú)加密)
導(dǎo)出的文件為pem格式括享,同時(shí)包含證書(shū)和私鑰(pkcs#8):
Bag Attributes
localKeyID: 97 DD 46 3D 1E 91 EF 01 3B 2E 4A 75 81 4F 11 A6 E7 1F 79 40 subject=/C=CN/ST=GD/L=SZ/O=vihoo/OU=dev/CN=vihoo.com/emailAddress=yy@vihoo.com
issuer=/C=CN/ST=GD/L=SZ/O=viroot/OU=dev/CN=viroot.com/emailAddress=yy@viroot.com-----BEGIN CERTIFICATE-----MIIDazCCAlMCCQCIOlA9/dcfEjANBgkqhkiG9w0BAQUFADB5MQswCQYDVQQGEwJD
1LpQCA+2B6dn4scZwaCD-----END CERTIFICATE-----Bag Attributes
localKeyID: 97 DD 46 3D 1E 91 EF 01 3B 2E 4A 75 81 4F 11 A6 E7 1F 79 40 Key Attributes: <No Attributes>
-----BEGIN ENCRYPTED PRIVATE KEY-----MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDC/6rAc1YaPRNf
K9ZLHbyBTKVaxehjxzJHHw==
-----END ENCRYPTED PRIVATE KEY-----
僅提取私鑰
openssl pkcs12 -in server.p12 -password pass:111111 -passout pass:111111 -nocerts -out out/key.pem
僅提取證書(shū)(所有證書(shū))
openssl pkcs12 -in server.p12 -password pass:111111 -nokeys -out out/key.pem
僅提取ca證書(shū)
openssl pkcs12 -in server-all.p12 -password pass:111111 -nokeys -cacerts -out out/cacert.pem
僅提取server證書(shū)
openssl pkcs12 -in server-all.p12 -password pass:111111 -nokeys -clcerts -out out/cert.pem
六闽铐、openssl 命令參考
- openssl list-standard-commands(標(biāo)準(zhǔn)命令)
asn1parse: asn1parse用于解釋用ANS.1語(yǔ)法書(shū)寫(xiě)的語(yǔ)句(ASN一般用于定義語(yǔ)法的構(gòu)成)
ca: ca用于CA的管理
openssl ca [options]:
2.1) -selfsign
使用對(duì)證書(shū)請(qǐng)求進(jìn)行簽名的密鑰對(duì)來(lái)簽發(fā)證書(shū)。即"自簽名"奶浦,這種情況發(fā)生在生成證書(shū)的客戶(hù)端、簽發(fā)證書(shū)的CA都是同一臺(tái)機(jī)器(也是我們大多數(shù)實(shí)驗(yàn)中的情況)踢星,我們可以使用同一個(gè)
密鑰對(duì)來(lái)進(jìn)行"自簽名"
2.2) -in file
需要進(jìn)行處理的PEM格式的證書(shū)
2.3) -out file
處理結(jié)束后輸出的證書(shū)文件
2.4) -cert file
用于簽發(fā)的根CA證書(shū)
2.5) -days arg
指定簽發(fā)的證書(shū)的有效時(shí)間
2.6) -keyfile arg
CA的私鑰證書(shū)文件
2.7) -keyform arg
CA的根私鑰證書(shū)文件格式:
2.7.1) PEM
2.7.2) ENGINE
2.8) -key arg
CA的根私鑰證書(shū)文件的解密密碼(如果加密了的話(huà))
2.9) -config file
配置文件
example1: 利用CA證書(shū)簽署請(qǐng)求證書(shū)
openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.keyreq: X.509證書(shū)簽發(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)求證書(shū)的私鑰文件的解密密碼
3.6) -key file
用于簽名待生成的請(qǐng)求證書(shū)的私鑰文件
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格式的證書(shū)
3.10) -days
X509證書(shū)的有效時(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證書(shū)(X.509結(jié)構(gòu))
openssl req -new -x509 -days 3650 -key server.key -out ca.crt
example2: 用server.key生成證書(shū)簽署請(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.csrgenrsa: 生成RSA參數(shù)
openssl genrsa [args] [numbits]
[args]
4.1) 對(duì)生成的私鑰文件是否要使用加密算法進(jìn)行對(duì)稱(chēng)加密:
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ì)稱(chēng)加密(des、des沐悦、aes)的密碼(使用這個(gè)參數(shù)就省去了console交互提示輸入密碼的環(huán)節(jié))
4.3) -out file: 輸出證書(shū)私鑰文件
[numbits]: 密鑰長(zhǎng)度
example: 生成一個(gè)1024位的RSA私鑰成洗,并用DES加密(密碼為1111),保存為server.key文件
openssl genrsa -out server.key -passout pass:1111 -des3 1024rsa: 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í)候藏否,選擇了加密算法了的話(huà))
5.5) -out arg
待輸出密鑰文件
5.6) -passout arg
如果希望輸出的密鑰文件繼續(xù)使用加密算法的話(huà)則指定密碼
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è)功能很豐富的證書(shū)處理工具瓶殃。可以用來(lái)顯示證書(shū)的內(nèi)容副签,轉(zhuǎn)換其格式遥椿,給CSR簽名等X.509證書(shū)的管理工作
openssl x509 [args]
6.1) -inform arg
待處理X509證書(shū)文件格式
6.1.1) DER
6.1.2) NET
6.1.3) PEM
6.2) -outform arg
待輸出X509證書(shū)文件格式
6.2.1) DER
6.2.2) NET
6.2.3) PEM
6.3) -in arg
待處理X509證書(shū)文件
6.4) -out arg
待輸出X509證書(shū)文件
6.5) -req
表明輸入文件是一個(gè)"請(qǐng)求簽發(fā)證書(shū)文件(CSR)",等待進(jìn)行簽發(fā)
6.6) -days arg
表明將要簽發(fā)的證書(shū)的有效時(shí)間
6.7) -CA arg
指定用于簽發(fā)請(qǐng)求證書(shū)的根CA證書(shū)
6.8) -CAform arg
根CA證書(shū)格式(默認(rèn)是PEM)
6.9) -CAkey arg
指定用于簽發(fā)請(qǐng)求證書(shū)的CA私鑰證書(shū)文件淆储,如果這個(gè)option沒(méi)有參數(shù)輸入冠场,那么缺省認(rèn)為私有密鑰在CA證書(shū)文件里有
6.10) -CAkeyform arg
指定根CA私鑰證書(shū)文件格式(默認(rèn)為PEM格式)
6.11) -CAserial arg
指定序列號(hào)文件(serial number file)
6.12) -CAcreateserial
如果序列號(hào)文件(serial number file)沒(méi)有指定,則自動(dòng)創(chuàng)建它
example1: 轉(zhuǎn)換DER證書(shū)為PEM格式
openssl x509 -in cert.cer -inform DER -outform PEM -out cert.pem
example2: 使用根CA證書(shū)對(duì)"請(qǐng)求簽發(fā)證書(shū)"進(jìn)行簽發(fā)本砰,生成x509格式證書(shū)
openssl x509 -req -days 3650 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt
example3: 打印出證書(shū)的內(nèi)容
openssl x509 -in server.crt -noout -textcrl: 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:
以文本格式來(lái)打印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ā)者信息值的哈希值來(lái)查詢(xún)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文件珠十,用來(lái)驗(yàn)證該CRL對(duì)象是否合法料扰。
7.12) -verify
是否驗(yàn)證證書(shū)。
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.dercrl2pkcs7: 用于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.htmlpkcs12: 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.htmlpkcs7: 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用于計(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ì)私鑰簽名過(guò)的摘要文件進(jìn)行驗(yàn)證
1.5) -prverify file
以私鑰文件對(duì)公鑰簽名過(guò)的摘要文件進(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
輸出文件愛(ài)你
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
- dgst: dgst用于計(jì)算消息摘要
- 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