未找到 Android SSL 连接的信任锚

新手上路,请多包涵

我正在尝试连接到运行 godaddy 256 位 SSL 证书的 IIS6 框,但出现错误:

 java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

一直在尝试确定可能导致这种情况的原因,但现在却一片空白。

这是我的连接方式:

 HttpsURLConnection conn;
conn = (HttpsURLConnection) (new URL(mURL)).openConnection();
conn.setConnectTimeout(20000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
String tempString = toString(conn.getInputStream());

原文由 Chrispix 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

@Chrispix 的解决方案很危险! 信任所有证书允许任何人进行中间人攻击! 只需将任何证书发送给客户端,它就会接受它!

将您的证书添加到自定义信任管理器,如本文所述: 使用 HttpClient over HTTPS 信任所有证书

虽然使用自定义证书建立安全连接有点复杂,但它会为您带来想要的 ssl 加密安全,而没有中间人攻击的危险!

原文由 Matthias B 发布,翻译遵循 CC BY-SA 4.0 许可协议

与接受的答案相反,您 不需要 自定义信任管理器,您需要修复服务器配置!

我在使用错误安装的 dynadot/alphassl 证书连接到 Apache 服务器时遇到了同样的问题。我正在使用 HttpsUrlConnection (Java/Android) 进行连接,它正在抛出 -

 javax.net.ssl.SSLHandshakeException:
  java.security.cert.CertPathValidatorException:
    Trust anchor for certification path not found.

实际问题是服务器配置错误 - 使用 http://www.digicert.com/help/ 或类似工具对其进行测试,它甚至会告诉您解决方案:

“该证书不是由受信任的机构签署的(检查 Mozilla 的根存储)。如果您从受信任的机构购买证书, _您可能只需要安装一个或多个中间证书_。请联系您的证书提供商以获取帮助,为您执行此操作服务器平台。”

您还可以使用 openssl 检查证书:

openssl s_client -debug -connect www.thedomaintocheck.com:443

你可能会看到:

Verify return code: 21 (unable to verify the first certificate)

并且,在输出的前面:

 depth=0 OU = Domain Control Validated, CN = www.thedomaintocheck.com
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=0 OU = Domain Control Validated, CN = www.thedomaintocheck.com
verify error:num=27:certificate not trusted
verify return:1
depth=0 OU = Domain Control Validated, CN = www.thedomaintocheck.com
verify error:num=21:unable to verify the first certificate`

证书链将仅包含 1 个元素(您的证书):

 Certificate chain
 0 s:/OU=Domain Control Validated/CN=www.thedomaintocheck.com
  i:/O=AlphaSSL/CN=AlphaSSL CA - G2

…但应该将链中的签名机构引用回 Android 信任的机构(Verisign、GlobalSign 等):

 Certificate chain
 0 s:/OU=Domain Control Validated/CN=www.thedomaintocheck.com
   i:/O=AlphaSSL/CN=AlphaSSL CA - G2
 1 s:/O=AlphaSSL/CN=AlphaSSL CA - G2
   i:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA
 2 s:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA
   i:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA

配置服务器的说明(和中间证书)通常由颁发证书的机构提供,例如: http ://www.alphassl.com/support/install-root-certificate.html

安装我的证书颁发者提供的中间证书后,我现在在使用 HttpsUrlConnection 连接时没有错误。

原文由 Stevie 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题