how can i use ssl with raw sockets?
# ktor
o
how can i use ssl with raw sockets?
1
📋 1
📝 1
i cant find any documentation
d
It seems it is missing. Going to add it. You have to include the
ktor-network-tls
artifact, and then call the tls method:
aSocket().tcp().connect().tls(...)
o
ok thanks
d
Tried this and seems to work:
tls_sample.kts
Copy code
#!/usr/bin/env kscript

@file:MavenRepository("ktor", "<https://kotlin.bintray.com/ktor>")
@file:DependsOn("io.ktor:ktor-network-tls:0.9.2")

import io.ktor.cio.*
import io.ktor.network.sockets.*
import io.ktor.network.tls.*
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.io.*
import java.net.*

runBlocking {
    val socket = aSocket().tcp().connect(InetSocketAddress("<http://google.com|google.com>", 443)).tls()
    val w = socket.openWriteChannel(autoFlush = false)
    w.write("GET / HTTP/1.1\r\n")
    w.write("Host: <http://google.com|google.com>\r\n")
    w.write("\r\n")
    w.flush()
    val r = socket.openReadChannel()
    println(r.readASCIILine())
}
o
Exception in thread "io-thread-2" java.lang.IllegalArgumentException: Invalid TLS record type code: 58
can you help with that?
d
That happens with the snippet I put? Or with your own code?
o
my code
d
Does the snippet work for you?
o
i didnt try
d
Can you try?
o
ok
d
(Is to be sure if it is related to the server certificate, or related to your network or other thing)
^^ @e5l “Invalid TLS record type code: 58”
o
it gives me some errors, not sure its the code or the way i launched it
kotlinc ggl.kt
i dont play around with scripts 😛
d
the snippet uses kscript https://github.com/holgerbrandl/kscript But you can also copy the runBlocking block and put it in your main 🙂 As long as you include the required artifacts
o
yeah
d
Also would help knowing the endpoint or the server/certificate that backend uses
o
well cant ktor accept invalid certificates?
d
probably, but can provide more information for troubleshooting? are you connecting to a ktor backend? to an internet backend? a local backend written un C++ with a self-signed certificate… etc. I’m checking TLS record types: https://tools.ietf.org/html/rfc5246#section-7.2 And 58 doesn’t seems to be a valid record
e
`Invalid TLS record type code: 58`: need some code sample. It looks like a server error
d
can you also provide the stacktrace/or part of it? there are two places in ktor with that exception
o
sure
its a irc server written in c probably
Exception in thread "io-thread-2" java.lang.IllegalArgumentException: Invalid TLS record type code: 58 at io.ktor.network.tls.TLSRecordType$Companion.byCode(TLSRecordType.kt:15) at io.ktor.network.tls.ParserKt.readTLSRecordHeader(Parser.kt:12) at io.ktor.network.tls.TLSClientSession.readTLSRecordHeader(TLSClientSession.kt:201) at io.ktor.network.tls.TLSClientSession.tlsHandshakeAndNegotiation(TLSClientSession.kt:60) at io.ktor.network.tls.TLSClientSession.negotiate(TLSClientSession.kt:46) at io.ktor.network.tls.TLSKt.tls(TLS.kt:42) at io.ktor.network.tls.TLSKt.tls$default(TLS.kt:32) at com.otakusenpai.aghora.connection.SslConnection$1.doResume(SslConnection.kt:21) 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 kotlinx.coroutines.experimental.BuildersKt.runBlocking$default(Builders.kt:175) at com.otakusenpai.aghora.connection.SslConnection.<init>(SslConnection.kt:15) at com.otakusenpai.aghora.bot.BasicBot.<init>(BasicBot.kt:23) at com.otakusenpai.aghora.MainKt.main(main.kt:37) java.lang.IllegalArgumentException: Invalid TLS record type code: 58 at io.ktor.network.tls.TLSRecordType$Companion.byCode(TLSRecordType.kt:15) at io.ktor.network.tls.ParserKt.readTLSRecordHeader(Parser.kt:12) at io.ktor.network.tls.TLSClientSession.readTLSRecordHeader(TLSClientSession.kt:201) at io.ktor.network.tls.TLSClientSession.tlsHandshakeAndNegotiation(TLSClientSession.kt:60) at io.ktor.network.tls.TLSClientSession.negotiate(TLSClientSession.kt:46) at io.ktor.network.tls.TLSKt.tls(TLS.kt:42) at io.ktor.network.tls.TLSKt.tls$default(TLS.kt:32) at com.otakusenpai.aghora.connection.SslConnection$1.doResume(SslConnection.kt:21) 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 kotlinx.coroutines.experimental.BuildersKt.runBlocking$default(Builders.kt:175) at com.otakusenpai.aghora.connection.SslConnection.<init>(SslConnection.kt:15) at com.otakusenpai.aghora.bot.BasicBot.<init>(BasicBot.kt:23) at com.otakusenpai.aghora.MainKt.main(main.kt:37) Exception in thread "main" kotlin.UninitializedPropertyAccessException: lateinit property socket has not been initialized at com.otakusenpai.aghora.connection.Connection.getSocket(Connection.kt:23) at com.otakusenpai.aghora.connection.SslConnection$1.doResume(SslConnection.kt:26) 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 kotlinx.coroutines.experimental.BuildersKt.runBlocking$default(Builders.kt:175) at com.otakusenpai.aghora.connection.SslConnection.<init>(SslConnection.kt:15) at com.otakusenpai.aghora.bot.BasicBot.<init>(BasicBot.kt:23) at com.otakusenpai.aghora.MainKt.main(main.kt:37)
d
I see. Is it a public IRC server that we can try to connect to? If it is not the case, maybe you can capture network traffic for that connection using wireshark to try to reproduce it locally
e
Make sure that you use proper ssl port
👍 1
o
and i use ssl port
d
https://freenode.net/kb/answer/chat or ports 6697, 7000 and 7070 for SSL-encrypted connections.
going to try to connect ot one of those ports
@e5l
Copy code
val socket = aSocket().tcp().connect(InetSocketAddress("<http://chat.freenode.net|chat.freenode.net>", 6697)).tls()

//Exception in thread "main" Exception in thread "io-thread-2" io.ktor.network.tls.TLSException: Unsupported TLS handshake type CertificateRequest
o
Exception in thread "io-thread-2" io.ktor.network.tls.TLSException: Unsupported TLS handshake type CertificateRequest at io.ktor.network.tls.TLSClientSession.handshake(TLSClientSession.kt:308) at io.ktor.network.tls.TLSClientSession.processHandshakeMessage(TLSClientSession.kt:79) 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 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 kotlinx.coroutines.experimental.BuildersKt.runBlocking$default(Builders.kt:175) at com.otakusenpai.aghora.connection.SslConnection.<init>(SslConnection.kt:15) at com.otakusenpai.aghora.bot.BasicBot.<init>(BasicBot.kt:23) at com.otakusenpai.aghora.MainKt.main(main.kt:37)
d
that’s a different exception 🙂 that sounds like something is missing
o
yeah, i didnt add ssl port the first time
hello, how can i fix that?
d
Looks like that packet is not handled, create an issue at github: https://github.com/ktorio/ktor/issues Describing the error and include how to reproduce it:
val socket = aSocket().tcp().connect(InetSocketAddress("<http://chat.freenode.net|chat.freenode.net>", 6697)).tls()
For now, connect to the unsecure version (6667 without tls), we will look at it.
o
ok
389 Views