一、证书生成方式
- 使用Java JDK自带的KeyTool工具;
- 使用OpenSSL生成;
二、证书工具安装及使用
1. KeyTool安装
- JDK自带的KeyTool是随着JDK安装的。只要配置好JDK环境变量,KeyTool就可正常使用;
2. OpenSSL安装
- 鉴于最近一直在使用Windows 10,所以需要在Windows上安装OpenSSL;
- 参考文章的第二种方式,主要命令如下:
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
四、总结
- OpenSSL和KeyTool都能够生成证书,OpenSSL功能更强大,更复杂(本文使用较少),二者相互转化比较容易;
- 证书是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