验证服务器Verisign证书会引发不受信任的服务器证书异常

时间:2013-05-07 11:14:35

标签: java android ssl certificate verisign

在尝试将HttpsURLConnection连接到后端时,我在验证Verisign证书时遇到问题。

目前的认证链: $openssl s_client -connect host:443

0 s:/C=AT/ST=xxx/L=xxx/O=xxx/CN=host
   i:/C=US/O=VeriSign, Inc./OU=VeriSign Trust Network/OU=Terms of use at https://www.verisign.com/rpa (c)10/CN=VeriSign Class 3 International Server CA - G3

1 s:/C=US/O=VeriSign, Inc./OU=VeriSign Trust Network/OU=Terms of use at https://www.verisign.com/rpa (c)10/CN=VeriSign Class 3 International Server CA - G3
   i:/C=US/O=VeriSign, Inc./OU=VeriSign Trust Network/OU=(c) 2006 VeriSign, Inc. - For authorized use only/CN=VeriSign Class 3 Public Primary Certification Auth
ority - G5

2 s:/C=US/O=VeriSign, Inc./OU=VeriSign Trust Network/OU=(c) 2006 VeriSign, Inc. - For authorized use only/CN=VeriSign Class 3 Public Primary Certification Auth
ority - G5
   i:/C=US/O=VeriSign, Inc./OU=Class 3 Public Primary Certification Authority

3 s:/C=US/O=VeriSign, Inc./OU=Class 3 Public Primary Certification Authority
   i:/C=US/O=VeriSign, Inc./OU=Class 3 Public Primary Certification Authority

我很确定我已经下载并将所有证书添加到KeyStore中,但我仍然会遇到异常:“ javax.net.ssl.SSLException:不可信服务器证书”,原因是{ {1}}

我获取连接的代码:

java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: TrustAnchor found but certificate validation failed.
当调用String keyStoreType = KeyStore.getDefaultType(); KeyStore keyStore = KeyStore.getInstance(keyStoreType); keyStore.load(null, null); keyStore.setCertificateEntry("verisign_0", getVerisign0()); keyStore.setCertificateEntry("verisign_1", getVerisign1()); keyStore.setCertificateEntry("verisign_2", getVerisign2()); keyStore.setCertificateEntry("verisign_3", getVerisign3()); String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); tmf.init(keyStore); SSLContext context = SSLContext.getInstance("TLS"); context.init(null, tmf.getTrustManagers(), null); HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(); urlConnection.setSSLSocketFactory(context.getSocketFactory()); return urlConnection; 时,

问题正在发生。所有getVerisign()方法都返回正确的证书。有没有我忘记的一步?也许是一个特殊的订单,应该添加证书?

只是为了澄清,我已经使用swisssign证书确切地使用了这种技术并且它有效。我正面临这个问题,因为一些Android 2.1,2.2设备没有一些根证书。提前谢谢。

堆栈跟踪:

connection.connect()

2 个答案:

答案 0 :(得分:2)

如果没有进一步的代码,我无法分辨,但是,我可以看到你加载了Trusted Certs,但是你在哪里加载你自己的Keystore?如果它在其他代码中,我无法分辨。

当您尝试使用外部Truststore时会发生什么? $ JAVA_HOME / JRE / LIB /安全/ cacerts中

假设您确实想要使用动态信任存储(通常它们非常静态),请查看

http://jcalcote.wordpress.com/2010/06/22/managing-a-dynamic-java-trust-store/

答案 1 :(得分:2)

如前所述,JVM中的TrustStore是静态的,一旦初始化,证书就无法附加到它上面(正如我观察到的类似问题)。 简单的解决方案是将证书添加到默认证书:$ JAVA_HOME / jre / lib / security / cacerts

或者尝试将您的TrustStore初始化代码包含在静态{}块中,以便更快地初始化它,并有机会绕过默认行为。

您还可以使用自定义文件KeysStore并要求jvm将其与-Djavax.net.ssl.keyStore *参数一起使用。