【发布时间】:2018-08-03 11:11:04
【问题描述】:
我尝试向远程安全服务器发送 POST 请求(https 调用)。 首先,我使用 OkHTTP 库进行调用,因为该库可以在以前的应用程序中进行 http 调用,但使用 https 它不再起作用,请参阅我之前的帖子:Cannot make HTTPS calls with OKHTTP lib
我决定尝试使用 HttpURLConnection 来拨打电话,如下所述:https://developer.android.com/reference/java/net/HttpURLConnection.html 我使用的代码仅适用于某些设备。
它适用于配备 SIM 卡和 wifi 的 Android 6.0 上的 Wiko,不适用于仅通过 Wifi 连接到互联网的 Android 4.2.2 上的三星。
这是我遇到的问题的logcat:
02-23 12:29:59.893 11026-11951 W/System.err: javax.net.ssl.SSLHandshakeException: com.android.org.bouncycastle.jce.exception.ExtCertPathValidatorException: Could not validate certificate signature.
02-23 12:29:59.923 11026-11951 W/System.err: at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:382)
02-23 12:29:59.923 11026-11951 W/System.err: at libcore.net.http.HttpConnection.setupSecureSocket(HttpConnection.java:231)
02-23 12:29:59.923 11026-11951 W/System.err: at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:478)
02-23 12:29:59.923 11026-11951 W/System.err: at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
02-23 12:29:59.923 11026-11951 W/System.err: at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
02-23 12:29:59.923 11026-11951 W/System.err: at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
02-23 12:29:59.923 11026-11951 W/System.err: at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
02-23 12:29:59.923 11026-11951 W/System.err: at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
02-23 12:29:59.923 11026-11951 W/System.err: at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:281)
02-23 12:29:59.923 11026-11951 W/System.err: at com.gulplug.gulplugtoolbox.DiagnosisOnline$NetworkAsyncTask.doInBackground(DiagnosisOnline.java:109)
02-23 12:29:59.923 11026-11951 W/System.err: at com.gulplug.gulplugtoolbox.DiagnosisOnline$NetworkAsyncTask.doInBackground(DiagnosisOnline.java:82)
02-23 12:29:59.923 11026-11951 W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-23 12:29:59.923 11026-11951 W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-23 12:29:59.923 11026-11951 W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-23 12:29:59.923 11026-11951 W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-23 12:29:59.923 11026-11951 W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-23 12:29:59.933 11026-11951 W/System.err: at java.lang.Thread.run(Thread.java:856)
02-23 12:29:59.933 11026-11951 W/System.err: Caused by: java.security.cert.CertificateException: com.android.org.bouncycastle.jce.exception.ExtCertPathValidatorException: Could not validate certificate signature.
02-23 12:29:59.933 11026-11951 W/System.err: at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:296)
02-23 12:29:59.933 11026-11951 W/System.err: at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:197)
02-23 12:29:59.933 11026-11951 W/System.err: at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:598)
02-23 12:29:59.933 11026-11951 W/System.err: at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
02-23 12:29:59.933 11026-11951 W/System.err: at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:379)
02-23 12:29:59.933 11026-11951 W/System.err: ... 16 more
02-23 12:29:59.933 11026-11951 W/System.err: Caused by: com.android.org.bouncycastle.jce.exception.ExtCertPathValidatorException: Could not validate certificate signature.
02-23 12:29:59.943 11026-11951 W/System.err: at com.android.org.bouncycastle.jce.provider.RFC3280CertPathUtilities.processCertA(RFC3280CertPathUtilities.java:1475)
02-23 12:29:59.943 11026-11951 W/System.err: at com.android.org.bouncycastle.jce.provider.PKIXCertPathValidatorSpi.engineValidate(PKIXCertPathValidatorSpi.java:305)
02-23 12:29:59.943 11026-11951 W/System.err: at com.sec.android.security.pkix.SecCertPathValidatorSpi.engineValidate(SecCertPathValidatorSpi.java:99)
02-23 12:29:59.953 11026-11951 W/System.err: at java.security.cert.CertPathValidator.validate(CertPathValidator.java:190)
02-23 12:29:59.953 11026-11951 W/System.err: at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:283)
02-23 12:29:59.953 11026-11951 W/System.err: ... 20 more
02-23 12:29:59.953 11026-11951 W/System.err: Caused by: java.security.SignatureException: Signature was not verified
02-23 12:29:59.953 11026-11951 W/System.err: at org.apache.harmony.security.provider.cert.X509CertImpl.verify(X509CertImpl.java:384)
02-23 12:29:59.953 11026-11951 W/System.err: at com.android.org.bouncycastle.jce.provider.CertPathValidatorUtilities.verifyX509Certificate(CertPathValidatorUtilities.java:1428)
02-23 12:29:59.953 11026-11951 W/System.err: at com.android.org.bouncycastle.jce.provider.RFC3280CertPathUtilities.processCertA(RFC3280CertPathUtilities.java:1470)
02-23 12:29:59.953 11026-11951 W/System.err: ... 24 more
以及附加到此日志的代码:
class NetworkAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
Authentification authentification = new Authentification();
authentification.setApplicationToken(applicationtoken);
Gson gson = new Gson();
//String json = gson.toJson(authentification);
String json = "";
Log.d(TAG, json);
JSONObject auth = new JSONObject();
try {
auth.put("application_token",mytoken);
} catch (JSONException e) {
e.printStackTrace();
}
String url = myurl;
try {
URL object = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) object.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Content-Lenght", String.valueOf(json.getBytes("UTF-8").length));
con.setRequestMethod("POST");
con.setSSLSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault());
OutputStreamWriter wr = null;
wr = new OutputStreamWriter(con.getOutputStream());
wr.write(auth.toString());
wr.flush();
StringBuilder sb = new StringBuilder();
int HttpResult = 0;
HttpResult = con.getResponseCode();
if (HttpResult == HttpURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(
new InputStreamReader(con.getInputStream(), "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
Log.d(TAG, "First log");
Log.d(TAG, "" + sb.toString());
} else {
Log.d(TAG, "response code : " + HttpResult);
Log.d(TAG, "Http not ok");
Log.d(TAG, con.getResponseMessage());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
我不明白为什么我的第一个设备可以验证签名而另一个不能。 我尝试按照此处的说明同步日期和时间:Could not validate certificate signature?
这里可能解释了我的问题,但我不明白https://groups.google.com/forum/#!topic/android-security-discuss/C_cm3k9SdaM
有人遇到同样的问题并解决了吗?
UPDT: Alex 和 Anton 提出的两种方法都不适用于我的设备。 使用 Google play 服务表示该提供商是最新的。使用此处描述的方法启用 TLS 1.1 和 1.2 时,我仍然没有得到签名验证:https://blog.dev-area.net/2015/08/13/android-4-1-enable-tls-1-1-and-tls-1-2/
UPDT 2: 在此链接之后,服务器应使用 TLS 1.1:https://www.ssllabs.com
【问题讨论】:
-
设备型号是什么?可能它只是不能很好地与 TLS 配合使用。您可以尝试在设备上的 Chrome 中打开地址吗?
-
在 Galaxy S2 上它不起作用(android 4.2.2)。在 S4 mini 上(android 4.4)和 wiko LENNY 3(android 6.0)一样。它可能是一个不支持 TLS 的设备,我会尝试按照你的说法在 chrome 中打开。无论如何,我要关闭这个主题,因为每个人都对我的问题给出了正确的答案。
标签: android https sslhandshakeexception httpsurlconnection