https://kotlinlang.org logo
#ktor
Title
# ktor
a

Alex Styl

11/27/2023, 7:32 AM
I am trying to setup a https ktor server running on my android app. i don't have a central server, so I want every app to generate their own self signed certificate. but i can't seem to figure this out. I use this for my keyStore:
Copy code
val keyStore = KeyStore.getInstance(KeyStore.getDefaultType())
keyStore.load(null, null)
and this to setup the server:
Copy code
val environment = applicationEngineEnvironment {
                    sslConnector(
                        keyStore = keyStore,
                        keyAlias = "",
                        keyStorePassword = { "".toCharArray() },
                        privateKeyPassword = { "".toCharArray() }
                    ) {
                        port = SERVER_HTTPS_PORT
                    }
                    module {
                        routing {
// ...
however, when I run the app i get
Copy code
java.lang.NullPointerException: connector.keyStore.getCe…Chain(connector.keyAlias) must not be null
                                                                                                    	at io.ktor.server.netty.NettyChannelInitializer.<init>(NettyChannelInitializer.kt:54)
                                                                                                    	at io.ktor.server.netty.NettyApplicationEngine.createBootstrap(NettyApplicationEngine.kt:182)
                                                                                                    	at io.ktor.server.netty.NettyApplicationEngine.access$createBootstrap(NettyApplicationEngine.kt:30)
                                                                                                    	at io.ktor.server.netty.NettyApplicationEngine$bootstraps$2.invoke(NettyApplicationEngine.kt:163)
                                                                                                    	at io.ktor.server.netty.NettyApplicationEngine$bootstraps$2.invoke(NettyApplicationEngine.kt:162)
The error seems to be that ktor expects to have some key alias, however the Android Keystore afaik has none. Was anyone able to get this working?
a

Aleksei Tirman [JB]

11/27/2023, 11:07 AM
How do you import the key to the keystore or how do you generate entries in the keystore?
a

Alex Styl

11/27/2023, 11:25 AM
@Aleksei Tirman [JB] I don't, this is all the code I am using right now. First time using keystores and whatnot so probably i am missing something here
currently checking online on how to do what you mentioned
kind of offtopic but still relevant: I noticed how ktor has a nice buildKeyStore function but it doesnt work on Android. Do you happen to know if there anything similar that might be working for both android and jvm?
a

Aleksei Tirman [JB]

11/27/2023, 11:30 AM
I don't think there is a solution that works both on Android and JVM. You need to find out how to generate self-signed certificates on Android in the code.
a

Alex Styl

11/27/2023, 11:33 AM
alright. ill see if i can figure out how to add keys to the keystore as you said. thanks for th epointer Aleksei
@Aleksei Tirman [JB] i think i sorted it out. i copy pasted the buildKeyStore{} functionality from ktor and updated the keystore's keytype to use the default one
Do you know why ktor uses "JKS"? Is there something special about that one?
a

Aleksei Tirman [JB]

11/27/2023, 12:01 PM
What do you mean by "JKS"?
a

Alex Styl

11/27/2023, 12:10 PM
the reason why buildKeyStore {} doesnt work on Android is because Android does not support "JKS". It's a type you pass to:
Copy code
val store = KeyStore.getInstance("JKS")!!
see the build() part of buildKeyStore{} at that line, Android throw a:
Copy code
Caused by: java.security.KeyStoreException: JKS not found
if you replace that with
val store = KeyStore.getInstance(KeyStore.getDefaultType())!!
it work on Android
I think that's how it should be by default on ktor, but i am no security/keystore expert. I just think Android devs shouldn't have to worry about this. too hidden
Al openned a PR for this 🙂 https://github.com/ktorio/ktor/pull/3854