KeyTool證書生成與OpenSSL轉換

一、證書生成方式

  1. 使用Java JDK自帶的KeyTool工具;
  2. 使用OpenSSL生成;

二、證書工具安裝及使用

1. KeyTool安裝

  1. JDK自帶的KeyTool是隨着JDK安裝的。只要配置好JDK環境變量,KeyTool就可正常使用;

2. OpenSSL安裝

  1. 鑑於最近一直在使用Windows 10,所以需要在Windows上安裝OpenSSL;
  2. 參考文章的第二種方式,主要命令如下:
    1)安裝最新版的ActivePerl
    2)到https://www.openssl.org 下載最新OpenSSL源碼
    3)到http://www.nasm.us 下載NASM彙編工具
    4)下載並安裝VS 2010;
    5)安裝上面參考的文檔配置好環境變量;
    6)進入解壓的OpenSSL根目錄,執行如下命令(忽略perl Configure VC-WIN64A報錯):
perl Configure VC-WIN64A
nmake 
nmake test 
nmake install

可以忽略的報錯如下:

c:\openssl-OpenSSL_1_1_1>perl Configure VC-WIN64A
Configuring OpenSSL version 1.1.1 (0x1010100fL) for VC-WIN64A
Using os-specific seed configuration

It looks like you don't have either nmake.exe or dmake.exe on your PATH,
so you will not be able to execute the commands from a Makefile.  You can
install dmake.exe with the Perl Package Manager by running:

    ppm install dmake

Creating configdata.pm
Creating makefile

**********************************************************************
***                                                                ***
***   If you want to report a building issue, please include the   ***
***   output from this command:                                    ***
***                                                                ***
***     perl configdata.pm --dump                                  ***
***                                                                ***
**********************************************************************

7)執行完成後,在C:\Program Files下會生成OpenSSL安裝目錄,然後把C:\Program Files\OpenSSL\bin配置至Path環境變量;
8)重新打開cmd工具,執行openssl命令及正確的效果如下:

c:\openssl-OpenSSL_1_1_1>openssl
OpenSSL>

9)在使用Visual Studio x64 Win64 命令提示(2010)工具時(注:其它文檔中好多都叫x64本機工具),記得以管理員身份打開並執行如下的命令;

三、證書生成過程

1、生成祕鑰文件,有效期3年

keytool -genkeypair -alias stlicense -keypass test123 -storepass test@123! -dname "CN=license.test.com, OU=dev, O=test, L=Shenzhen, ST=Guangdong, C=CN" -keyalg RSA -keysize 2048 -validity 1095 -keystore server2.keystore

2、查看祕鑰詳情:

keytool -list -keystore server2.keystore  -storepass test@123! -v

3、根據祕鑰導出cer證書

keytool -exportcert -keystore server2.keystore -file server2.cer -alias stlicense -storepass test@123!

4、輸出PEM編碼格式的證書

keytool -exportcert -keystore server2.keystore -rfc -file pem_server2.cer -alias stlicense -storepass test@123!

5、導入信任證書(SSL客戶端使用)

keytool -importcert -keystore client_trust.keystore -file server2.cer -alias stlicense -storepass test@123! -noprompt

6、查看證書詳情:

keytool -printcert -file server2.cer -v

7、jks格式轉pkcs12

keytool -importkeystore -srckeystore server2.keystore -destkeystore server2.p12 -srcalias stlicense -destalias stserver -srcstoretype jks -deststoretype pkcs12 -srcstorepass test@123! -deststorepass test@321! -destkeypass test123 -noprompt

8、p12 證書提取pem證書和私鑰

openssl pkcs12 -in server2.p12 -clcerts -nokeys -password pass:test@321! -out server.crt
openssl pkcs12 -in server2.p12  -nocerts -password pass:test@321! -passout pass:test@321! -out server2.key

9、不帶密碼的配置pem證書和私鑰

openssl pkcs12 -in server2.p12 -clcerts -nokeys -out server3.crt
openssl pkcs12 -in server2.p12  -nocerts -out server3.key

四、總結

  1. OpenSSL和KeyTool都能夠生成證書,OpenSSL功能更強大,更復雜(本文使用較少),二者相互轉化比較容易;
  2. 證書是Java Web開發繞不過去的坎,無論是https、nginx、webservice、jms等,至少需要了解其原理和生成過程;

五、參考資料

[1] https://www.cnblogs.com/Savcry/p/9600117.html
[2] https://www.cnblogs.com/isylar/p/10002117.html
[3] https://www.cnblogs.com/littleatp/p/5922362.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章