UrlConnection:SSLProtocolException:SSL握手中止:ssl = 0x631b60c8:SSL库失败,通常是协议错误

时间:2016-02-15 18:18:24

标签: java android ssl https urlconnection

我目前正致力于将Android从HttpClient迁移到UrlConnection。我至少在Android 4.2.2上面临一些问题。

我尝试拨打的网址是https网址,我必须使用客户端证书。

基于this blog article,我写了以下代码:

// We create the client PKCS12 certificate
final KeyStore clientCertificateKeyStore = KeyStore.getInstance("PKCS12");
try
{
  clientCertificateKeyStore.load(this.clientCertificatePayload, this.clientCertificatePassword.toCharArray());
}
finally
{
  this.clientCertificatePayload.close();
}

final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("X509");
keyManagerFactory.init(clientCertificateKeyStore, clientCertificatePassword.toCharArray());

final KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();
sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, null, null);

稍后在我的代码中,我使用sslContext变量将SSL工厂设置为httpURLConnection

((HttpsURLConnection) httpURLConnection).setSSLSocketFactory(sslContext.getSocketFactory()); 

此代码在我的Android版Nexus 5上完美运行,但在使用Android 4.2.2的Wiko Rainbow上无效。事实上,在将sslSocketFactory设置为httpURLConnection之后,当我调用getOutputStream方法时,我有以下异常:

Caused by: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x631b60c8: Failure in SSL library, usually a protocol error
error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:741 0x59b4c8a8:0x00000000)
   at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:421)
   at libcore.net.http.HttpConnection.setupSecureSocket(HttpConnection.java:209)
   at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:486)
   at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:450)
   at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
   at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
   at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
   at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:205)
   at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:281)
   at com.splunk.mint.network.http.MonitorableHttpsURLConnection.getOutputStream(MonitorableHttpsURLConnection.java:93)
   at com.smartnsoft.droid4me.ws.URLConnectionWebServiceCaller.performHttpRequest(URLConnectionWebServiceCaller.java:472)
   at com.smartnsoft.droid4me.ws.URLConnectionWebServiceCaller.performHttpRequest(URLConnectionWebServiceCaller.java:562)
   at com.smartnsoft.droid4me.ws.URLConnectionWebServiceCaller.getInputStream(URLConnectionWebServiceCaller.java:158)
    ... 14 more
Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x631b60c8: Failure in SSL library, usually a protocol error
error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:741 0x59b4c8a8:0x00000000)
   at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHand

我尝试了多种解决方案,包括this one,但我仍然有例外。

您是否知道如何使代码在Android 4.2.2上运行?

感谢您的帮助!

修改:这是我使用NoSSLv3Factory的方式。

基本上,我使用相同的代码来创建客户端PKCS12证书并初始化SSLContext。不同之处在于我将SSLSocketFactory设置为HttpUrlConnection。这是我试图使用的条件:

if (httpURLConnection instanceof HttpsURLConnection)
{
  if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP)
  {
    ((HttpsURLConnection) httpURLConnection).setSSLSocketFactory(sslContext.getSocketFactory());
  }
  else
  {
    ((HttpsURLConnection) httpURLConnection).setSSLSocketFactory(new NoSSLv3Factory());
  }

}

我还尝试使用代码的另一个版本的构造函数:

((HttpsURLConnection) httpURLConnection).setSSLSocketFactory(new NoSSLv3Factory(sslContext.getSocketFactory())); 

但我仍然有同样的例外:(

0 个答案:

没有答案