用OpenSSL生成CA根证书来签名Keytool生成的证书请求

我上一篇文章 (配置JAVA SSL/TLS 之websocket wss交互式认证)生成的证书都是java  keytool 的证书,都是自签名的证书, 不是第三方签名的证书。下面我要虚拟一个CA出来, 用CA来签名。


1、创建CA的私钥

openssl genrsa -out ca.key 2048

2、创建CA自签名证书

openssl req -x509 -new -nodes -key ca.key -subj "/CN=localhost" -days 36500 -out ca.crt


3、生成服务端私钥

keytool -genkey -v -alias server_ks -keysize 2048 -keyalg RSA -dname "CN=localhost" -keypass 123456 -storepass 123456 -keystore ./server.keystore -storetype jks -validity 36500


4、生成证书请求文件

keytool -certreq -alias   server_ks -keyalg RSA -file ./server.csr  -keystore  ./server.keystore  -keypass 123456 -storepass 123456


5、用CA根证书来签名服务器端的证书请求文件

openssl ca -days 3650 -keyfile ./ca.key -cert ./ca.crt -in ./server.csr -out ./server.pem

[root@iZm5ecjozew819um7k89xxZ newssl]# openssl ca -days 3650 -keyfile ./ca.key -cert ./ca.crt -in ./server.csr -out ./server.pem
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
The mandatory countryName field was missing

上面的错误主要是因为  openssl  用到了配置文件   /etc/pki/tls/openssl.cnf , 而配置文件 openssl.cnf 里有:

[ ca ]
default_ca    = CA_default        # The default ca section

然后在节

[ CA_default ] 里有下面的策略

policy          = policy_match

在节 [ policy_match ] 里, countryName = match,    把match修改为  optional 即可。

[ policy_match ]
countryName        = match
stateOrProvinceName    = match
organizationName    = match
organizationalUnitName    = optional
commonName        = supplied
emailAddress        = optional

另外还需要注意的是

default_days    = 365                   # how long to certify for     证书的有效期这里是一年,你可以修改为10年
default_crl_days= 30                    # how long before next CRL    这个还没搞懂啥意思


6、把CA签名过的文本文件证书server.pem转化为cer二进制文件

openssl x509 -in ./server.pem -inform pem -outform der -out ./server.cer


7、将服务端证书导入到客户端信任列表中 trustkeystroe.jks
keytool -import -v -alias xxx -keystore ./clientTrust.jks -storepass pwdtesla2017 -file ./server.cer 


然后就可以使用我上传的源代码来进行交互式认证通信了。

源码下载地址:http://download.csdn.net/detail/langeldep/9746171


希望对大家有用!

猜你喜欢

转载自blog.csdn.net/langeldep/article/details/54846720