需求: java跟vc编写的服务端进行ssl通信,我需要用java导入提供的客户证书与服务端通信。
条件: 根证书、服务端证书、符合c客户端用的客户证书(java客户端需要我生成)都已在linux下用openssl生成。证书生成 过程,参考了
http://iiaf.javaeye.com/blog/465715,主要内容如下
在Linux下用下面的命令生成了证书
5.生成客户端私钥并存入sslclient.keystore
keytool -genkey -alias sslclient -validity 365 -keyalg RSA -keysize 1024 -keystore sslclient.keystore -keypass 123456 -storepass 123456
6.从sslclient.keystore中提取客户端签名请求
keytool -certreq -alias sslclient -sigalg SHA1withRSA -file sslclient.csr -keypass 123456 -storepass 123456 -keystore sslclient.keystore
7.用CA私钥为服务端请求签名生成客户端证书
openssl ca -in sslclient.csr -out sslclient.crt -cert demoCA/cacert.pem -keyfile demoCA/private/cakey.pem -notext -config openssl.cnf
8.转换客户端证书格式
openssl x509 -in sslclient.crt -out sslclient.der -outform DER
9.sslclient.keystore导入根证书
keytool -import -v -trustcacerts -alias ca_root -file demoCA/cacert.pem -storepass 123456 -keystore sslclient.keystore
10.sslclient.keystore导入客户端证书
keytool -import -v -alias sslclient -file sslclient.der -keypass 123456 -storepass 123456 -keystore sslclient.keystore
我把sslclient.keystore放到java_home/lib/security/ 更名为jssecacerts。
java客户端代码如下:
PrintWriter out=null;
InputStream in=null;
Socket s=null;
try
{
SSLSocketFactory factory=(SSLSocketFactory)SSLSocketFactory.getDefault();
s=factory.createSocket("192.168.1.17", 8000);
out=new PrintWriter(s.getOutputStream(),true);
out.print("12333");
out.flush();
in=s.getInputStream();
byte[] buf=new byte[100];
in.read(buf);
System.out.println("client_buf "+(new String(buf))+"!!!");
}
catch(Exception e)
{
e.printStackTrace();
}finally
{
out.close();
try
{
in.close();
s.close();}catch(Exception e)
{
e.printStackTrace();
}
}
问题:
与vc服务端通信时报错:
javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
提示出错位置是"in.read(buf);"
请问是证书导入的问题吗?正确的导入java客户端证书步骤是什么呢?谢谢各位