Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

https handshakeFailure with CIO HttpClient #439

Closed
henry1986 opened this issue Jun 8, 2018 · 9 comments
Closed

https handshakeFailure with CIO HttpClient #439

henry1986 opened this issue Jun 8, 2018 · 9 comments
Assignees

Comments

@henry1986
Copy link

henry1986 commented Jun 8, 2018

I want to implement a HttpClient, that connects via https for a websocket communication.

Currently only the CIO HttpClient supports websockets, but it seems to have an issue with https.

When using Apache HttpClient, a

client.call("ip")

works fine, but with CIO Client it throws

Exception in thread "io-thread-1" io.ktor.network.tls.TLSException: Received alert during handshake. Level: FATAL, code: HandshakeFailure
at io.ktor.network.tls.TLSClientSession.processHandshakeMessage(TLSClientSession.kt:96)
at io.ktor.network.tls.TLSClientSession.tlsHandshakeAndNegotiation(TLSClientSession.kt:64)
at io.ktor.network.tls.TLSClientSession$tlsHandshakeAndNegotiation$1.doResume(TLSClientSession.kt)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:54)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:53)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:53)
at kotlinx.coroutines.experimental.DispatchedTask$DefaultImpls.run(Dispatched.kt:161)
at kotlinx.coroutines.experimental.io.internal.MutableDelegateContinuation.run(MutableDelegateContinuation.kt:14)
at io.ktor.network.util.IOCoroutineDispatcher$IODispatchedTask.run(IOCoroutineDispatcher.kt)
at io.ktor.network.util.IOCoroutineDispatcher$IOThread$run$1.doResume(IOCoroutineDispatcher.kt:78)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:54)
at kotlinx.coroutines.experimental.DispatchedTask$DefaultImpls.run(Dispatched.kt:161)
at kotlinx.coroutines.experimental.DispatchedContinuation.run(Dispatched.kt:25)
at kotlinx.coroutines.experimental.EventLoopBase.processNextEvent(EventLoop.kt:147)
at kotlinx.coroutines.experimental.BlockingCoroutine.joinBlocking(Builders.kt:244)
at kotlinx.coroutines.experimental.BuildersKt.runBlocking(Builders.kt:185)
at io.ktor.network.util.IOCoroutineDispatcher$IOThread.run(IOCoroutineDispatcher.kt:73)
Exception in thread "ForkJoinPool.commonPool-worker-1" io.ktor.network.tls.TLSException: Received alert during handshake. Level: FATAL, code: HandshakeFailure
at io.ktor.network.tls.TLSClientSession.processHandshakeMessage(TLSClientSession.kt:96)
at io.ktor.network.tls.TLSClientSession.tlsHandshakeAndNegotiation(TLSClientSession.kt:64)
at io.ktor.network.tls.TLSClientSession$tlsHandshakeAndNegotiation$1.doResume(TLSClientSession.kt)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:54)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:53)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:53)
at kotlinx.coroutines.experimental.DispatchedTask$DefaultImpls.run(Dispatched.kt:161)
at kotlinx.coroutines.experimental.io.internal.MutableDelegateContinuation.run(MutableDelegateContinuation.kt:14)
at io.ktor.network.util.IOCoroutineDispatcher$IODispatchedTask.run(IOCoroutineDispatcher.kt)
at io.ktor.network.util.IOCoroutineDispatcher$IOThread$run$1.doResume(IOCoroutineDispatcher.kt:78)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:54)
at kotlinx.coroutines.experimental.DispatchedTask$DefaultImpls.run(Dispatched.kt:161)
at kotlinx.coroutines.experimental.DispatchedContinuation.run(Dispatched.kt:25)
at kotlinx.coroutines.experimental.EventLoopBase.processNextEvent(EventLoop.kt:147)
at kotlinx.coroutines.experimental.BlockingCoroutine.joinBlocking(Builders.kt:244)
at kotlinx.coroutines.experimental.BuildersKt.runBlocking(Builders.kt:185)
at io.ktor.network.util.IOCoroutineDispatcher$IOThread.run(IOCoroutineDispatcher.kt:73)

Same happens on client.wss(...)

Is there a difference between the implementation of the TLS Handshake of the Apache HttpClient and the CIO client? And if so, why? If I look in the code, TLS Handling seems to be independent of Apache Client and CIO Client, but maybe I have overseen something. I would appreciate some help.

@soywiz
Copy link
Contributor

soywiz commented Jun 8, 2018

Related to this? #394

@henry1986
Copy link
Author

Actually I can't tell, if it is the same. Exception is different, and in the linked issue they are talking about a certificate problem, which it doesn't seem to be, because it doesn't even get to the TrustManager (i've tested)

@henry1986
Copy link
Author

To give you a reproducible example:

fun main(args: Array<String>) { var client: HttpClient = HttpClient(CIO) { // var client: HttpClient = HttpClient(Apache){
install(WebSockets) { } } launch { val status = client.call("https://elster.de") .response.status println("status: $status") } Thread.sleep(10000L) }

Pay attention - other websites like "https://google.com" or "https://facebook.com" have no TLS Problem. They work fine and deliver a status 301

@henry1986
Copy link
Author

And if change the url to

"https://www.elster.de" the exception is slightly different:

Exception in thread "io-thread-1" Exception in thread "ForkJoinPool.commonPool-worker-1" io.ktor.network.tls.TLSException: Handshake failed: premature end of stream
at io.ktor.network.tls.TLSClientSession.tlsHandshakeAndNegotiation(TLSClientSession.kt:61)
at io.ktor.network.tls.TLSClientSession$tlsHandshakeAndNegotiation$1.doResume(TLSClientSession.kt)

...

@e5l e5l self-assigned this Jun 9, 2018
@e5l
Copy link
Member

e5l commented Jun 9, 2018

Hi, @henry1986. Thanks for the report. It look's like the problem that had been already fixed, and the fix will appear in next alpha release.

Just to make sure, could you provide java -version output?

@e5l e5l added bug enhancement and removed bug labels Jun 9, 2018
@e5l
Copy link
Member

e5l commented Jun 9, 2018

Unfortunately "https://elster.de" use TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 encryption which is currently unsupported in CIO engine.
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 implementation WIP.

Consider using Apache engine for now.

@henry1986
Copy link
Author

henry1986 commented Jun 9, 2018

Using Apache is not really an option, because we want to use websockets ;)

Actually, elster.de was just an example, we use different websites and I have looked with chrome, what the cipher suite is:

The connection to this site is encrypted and authenticated using TLS 1.2 (a strong protocol), ECDHE_RSA with P-256 (a strong key exchange), and AES_128_GCM (a strong cipher).

Is this currently not supported, too? Or can I make a build with the master branch and use that?

EDIT: I'm using the newest java, java 10:

java 10 2018-03-20
Java(TM) SE Runtime Environment 18.3 (build 10+46)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10+46, mixed mode)

@e5l
Copy link
Member

e5l commented Jun 9, 2018

Yep, cipher suite TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 is missing too and would appear with 256/384 implementation.

e5l added a commit that referenced this issue Jun 13, 2018
e5l added a commit that referenced this issue Jun 13, 2018
e5l added a commit that referenced this issue Jun 13, 2018
@e5l
Copy link
Member

e5l commented Jun 14, 2018

Fixed in master

@e5l e5l closed this as completed Jun 14, 2018
schleinzer pushed a commit to schleinzer/ktor that referenced this issue Feb 26, 2019
lberrymage added a commit to accrescent/parcelo that referenced this issue Nov 10, 2023
The Ktor HTTP client throws a fatal TLSException HandshakeFailure when
connecting to the repository. Curiously this doesn't occur in our
staging environment, but can easily be reproduced locally.

The issue seems to find root in the CIO Ktor client's cipher support
suite as indicated by issues with the same error such as [1]. Supposedly
this issue was resolved, but others such as [2] indicate the issue is
still present despite it being closed upstream. Without useful error
information from the stack trace, the best option seems to be switching
to another well-supported client engine such as Apache5. As a bonus,
Apache5 also has HTTP/2 enabled by default.

[1]: ktorio/ktor#439
[2]: oliver-charlesworth/craft-watch#198
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants