android – 使用超过3g时找不到证书路径的信任锚,但在WiFi上工作正常

我的 android项目正在使用api 15.我使用HttpsURLConnection类通过https连接到服务器.一切都可以正常通过WiFi,但如果我关闭WiFi并运行超过3克我得到以下内容
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.       at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:413)
   at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:257)       at libcore.net.http.httpconnection.setupSecureSocket(httpconnection.java:210)
   at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:477)
   at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
   at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
   at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)       at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
   at libcore.net.http.HttpURLConnectionImpl.getoutputStream(HttpURLConnectionImpl.java:188)       at libcore.net.http.HttpsURLConnectionImpl.getoutputStream(HttpsURLConnectionImpl.java:280)

如果我做错了什么,为什么它会通过WiFi工作?

这里有更多信息.

如果我使用openssl查看服务器证书信息,

echo | openssl s_client -connect myserver.com:443

返回服务器级自签名证书,而

echo | openssl s_client -connect myserver.com:443 -servername myserver.com

返回“正确”的证书.我的服务器上有多个vhost,每个都有自己的rapidssl发布的证书,所以我认为这意味着我需要使用支持TLS的客户端.至少这是我对启动时Apache日志中看到的消息的解释:

Name-based SSL virtual hosts only work for clients with TLS server name indication support

如果到目前为止我是正确的,这是否意味着我的移动3G网络可能与TLS搞砸了或者我还应该做些什么呢?

我可以通过继承DefaultHttpClient并导入包含服务器自签名证书的密钥库来获得超过3g的工作,但这绝对不是我的首选选项.

解决方法

添加-servername选项只是在Client Hello消息中设置Server Name Indication字段,这有助于我们在目标主机包含许多证书时选择正确的证书,就像您的情况一样.但是,它与问题无关.

在SSL握手期间,证书以主题/发行者对的形式提供,形成证书链.

即google.com证书链看起来像这样:

openssl s_client -connect google.com:443
CONNECTED(00000003)
depth=2 C = US,O = GeoTrust Inc.,CN = GeoTrust Global CA
verify error:num=20:unable to get local issuer certificate
verify return:0
---
Certificate chain
 0 s:/C=US/ST=California/L=Mountain View/O=Google Inc/CN=*.google.com
   i:/C=US/O=Google Inc/CN=Google Internet Authority G2
 1 s:/C=US/O=Google Inc/CN=Google Internet Authority G2
   i:/C=US/O=GeoTrust Inc./CN=GeoTrust Global CA
 2 s:/C=US/O=GeoTrust Inc./CN=GeoTrust Global CA
   i:/C=US/O=Equifax/OU=Equifax Secure Certificate Authority
---

收到后,客户端会尝试从下到上验证链中的所有发卡行(根).如果客户端无法验证根证书,则会显示“未找到证书路径的信任锚”消息.

因此,回到WiFi / 3G问题,可能您的移动网络的DNS无法解析证书链中某个中间发行人的地址.

更新:

您可以将发卡机构的证书放入APK,并通过代码中的TrustManager添加.这种方法可以克服接入网络限制(如果有的话).

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...