Sanu0074
@Sanu0074

Почему Retrofit не шлет запрос по https?

Падает java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
При этом запрашиваемый домен домен имеет верифицированный сертификат.
Если бы речь шла о self-signed сертификате, то причина была бы ясна.
Объясните, что нужно делать чтобы можно было послать запрос на "нормальный" (верифицированный) домен?

UPDATE:

Так создается инстанс ретрофита:
init {
    App.getComponent().inject(this)
    val builder = OkHttpClient().newBuilder().socketFactory(getSSLConfig(App.getContext()).socketFactory)
    builder.addInterceptor(httpInterceptor)
    val client = builder.build()
    retrofit = Retrofit.Builder()
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(Net.BASE_URL.value)
            .client(client)
            .build()
}

@Throws(CertificateException::class, IOException::class, KeyStoreException::class, NoSuchAlgorithmException::class, KeyManagementException::class)
private fun getSSLConfig(context: Context): SSLContext {

    // Loading CAs from an InputStream
    var cf: CertificateFactory? = null
    cf = CertificateFactory.getInstance("X.509")

    var ca: Certificate? = null
    // I'm using Java7. If you used Java6 close it manually with finally.
    context.resources.openRawResource(R.raw.certificate_ca).use({ cert -> ca = cf.generateCertificate(cert) })

    // Creating a KeyStore containing our trusted CAs
    val keyStoreType = KeyStore.getDefaultType()
    val keyStore = KeyStore.getInstance(keyStoreType)
    keyStore.load(null, null)
    keyStore.setCertificateEntry("ca", ca)

    // Creating a TrustManager that trusts the CAs in our KeyStore.
    val tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm()
    val tmf = TrustManagerFactory.getInstance(tmfAlgorithm)
    tmf.init(keyStore)

    // Creating an SSLSocketFactory that uses our TrustManager
    val sslContext = SSLContext.getInstance("TLS")
    sslContext.init(null, tmf.trustManagers, null)

    return sslContext
}


При обращении к серверу получаю Handshake failed. Что я делаю не верно?

Stacktrace:

01-19 12:50:46.578 7161-7161/ru.rst.console W/System.err: javax.net.ssl.SSLHandshakeException: Handshake failed
01-19 12:50:46.579 7161-7161/ru.rst.console W/System.err:     at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:423)
                                                              at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:281)
                                                              at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:251)
                                                              at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:151)
                                                              at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:195)
                                                              at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121)
                                                              at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100)
                                                              at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
                                                              at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
                                                              at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
                                                              at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
                                                              at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
                                                              at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
01-19 12:50:46.580 7161-7161/ru.rst.console W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
                                                              at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
                                                              at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
                                                              at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
                                                              at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
                                                              at ru.rst.console.base.http.HttpInterceptor.handleResponse(HttpInterceptor.kt:29)
                                                              at ru.rst.console.base.http.HttpInterceptor.intercept(HttpInterceptor.kt:25)
                                                              at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
                                                              at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
                                                              at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185)
                                                              at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135)
                                                              at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
                                                              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
                                                              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
01-19 12:50:46.582 7161-7161/ru.rst.console W/System.err:     at java.lang.Thread.run(Thread.java:764)
                                                          Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xa5522bc0: Failure in SSL library, usually a protocol error
                                                          error:10000066:SSL routines:OPENSSL_internal:BAD_ALERT (external/boringssl/src/ssl/tls_record.c:465 0xa3078e9f:0x00000000)
                                                              at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
01-19 12:50:46.583 7161-7161/ru.rst.console W/System.err:     at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:351)
                                                            ... 27 more
  • Вопрос задан
  • 1974 просмотра
Пригласить эксперта
Ответы на вопрос 1
@aol-nnov
необходимо проверить на сервере, что в файл сертификата, который подключен к веб серверу, вписана вся цепочка сертификатов

на клиенте проверить, что в хранилище корневых сертификатов ничего не протухло.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы