SSL库失败,通常是协议错误

时间:2017-12-18 08:51:14

标签: android ssl

我知道在stackoverflow中有很多关于ssl证书的类似问题。我看了所有这些问题,但我没有解决问题。我尝试使用ssl证书https连接。我正在关注android文档 https://developer.android.com/training/articles/security-ssl.html。 这是我的代码:

        ProviderInstaller.installIfNeeded(this)
        val cf = CertificateFactory.getInstance("X.509")
        val caInput = resources.openRawResource(R.raw.cert)
        val ca: Certificate
        try {
            ca = cf.generateCertificate(caInput)
        } finally {
            caInput.close()
        }
        val keyStoreType = KeyStore.getDefaultType()
        val keyStore = KeyStore.getInstance(keyStoreType)
        keyStore.load(null, null)
        keyStore.setCertificateEntry("ca", ca)

        val tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm()
        val tmf = TrustManagerFactory.getInstance(tmfAlgorithm)
        tmf.init(keyStore)

        val kmf = KeyManagerFactory.getInstance("X509")
        kmf.init(keyStore, pass.toCharArray())
        val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
            override fun getAcceptedIssuers(): Array<X509Certificate> {
                return arrayOf<X509Certificate>();
            }
            override fun checkClientTrusted(p0: Array<out X509Certificate>?, p1: String?) {
            }
            override fun checkServerTrusted(p0: Array<out X509Certificate>?, p1: String?) {
            }
        })
        val context = SSLContext.getInstance("TLSv1")
        context.init(kmf.keyManagers, trustAllCerts, java.security.SecureRandom())
        val socketFactory = context.socketFactory
        HttpsURLConnection.setDefaultSSLSocketFactory(socketFactory)
        doAsync {
            val url = URL(sourceUrl)
            val urlConnection = url.openConnection() as HttpsURLConnection
            val `in` = urlConnection.inputStream
        }

我收到了SSLProtocolException:

javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted:
     

ssl = 0x5266a740:SSL库失败,通常是协议错误       W:错误:14077102:SSL例程:SSL23_GET_SERVER_HELLO:不支持的协议(external / openssl / ssl / s23_clnt.c:714 0x52587f10:0x00000000)       W:at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:448)       W:在com.android.okhttp.Connection.upgradeToTls(Connection.java:146)       W:在com.android.okhttp.Connection.connect(Connection.java:107)       W:at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)       W:at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)       W:at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)       W:at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)       W:at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)       W:at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)       W:at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:246)       W:at com.triomobil.triotys.pinning.PinningActivity $ onCreate $ 1.invoke(PinningActivity.kt:142)       W:at com.triomobil.triotys.pinning.PinningActivity $ onCreate $ 1.invoke(PinningActivity.kt:33)       W:at org.jetbrains.anko.AsyncKt $ doAsync $ 1.invoke(Async.kt:140)       W:at org.jetbrains.anko.AsyncKt $ doAsync $ 1.invoke(Async.kt)       W:at org.jetbrains.anko.AsyncKt $ sam $ Callable $ 761a5578.call(Async.kt)       W:at java.util.concurrent.FutureTask.run(FutureTask.java:237)       W:at java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.access $ 201(ScheduledThreadPoolExecutor.java:152)       W:at java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)       W:在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)       W:在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)       W:在java.lang.Thread.run(Thread.java:841)       W:引起:javax.net.ssl.SSLProtocolException:SSL握手中止:ssl = 0x5266a740:SSL库失败,通常是协议   错误       W:错误:14077102:SSL例程:SSL23_GET_SERVER_HELLO:不支持的协议(external / openssl / ssl / s23_clnt.c:714 0x52587f10:0x00000000)       W:at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)       W:at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:406)       W:......还有20个

我正在寻找所有类似的问题stackoverflow。我尝试了一切,但它没有奏效。

0 个答案:

没有答案