Ktor 3.6.2 Help

SSL in Ktor Client

To configure SSL in the Ktor client, you need to customize the configuration of an engine used by your client.

In this topic, you'll learn how to add an SSL certificate for engines that target JVM and Android.

Load SSL settings

In this topic, the Ktor client is using a certificate loaded from an existing KeyStore file (keystore.jks) generated for the server.

Given that every engine uses different JSSE API to configure SSL (for example, SSLContextfor Apache or TrustManager for Jetty), you need to have the capability to obtain corresponding SSL configurations.

The code snippet below creates the SslSettings object that loads a certificate from an existing KeyStore file (keystore.jks) and provides functions for loading SSL configurations.

The same KeyStore is used to trust the server certificate and, through KeyManagerFactory, to present a client certificate, which is required by a server that has a trust store configured:

object SslSettings { fun getKeyStore(): KeyStore { val keyStoreFile = FileInputStream("keystore.jks") val keyStorePassword = "foobar".toCharArray() val keyStore: KeyStore = KeyStore.getInstance(KeyStore.getDefaultType()) keyStore.load(keyStoreFile, keyStorePassword) return keyStore } fun getTrustManagerFactory(): TrustManagerFactory? { val trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()) trustManagerFactory.init(getKeyStore()) return trustManagerFactory } fun getKeyManagerFactory(): KeyManagerFactory? { val keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()) keyManagerFactory.init(getKeyStore(), "foobar".toCharArray()) return keyManagerFactory } fun getSslContext(): SSLContext? { val sslContext = SSLContext.getInstance("TLS") sslContext.init(getKeyManagerFactory()?.keyManagers, getTrustManagerFactory()?.trustManagers, null) return sslContext } fun getTrustManager(): X509TrustManager { return getTrustManagerFactory()?.trustManagers?.first { it is X509TrustManager } as X509TrustManager } }

Configure SSL in Ktor

SSL configuration differs depending on the engine. The following sections describe how to configure SSL for each engine.

JVM

Apache and Apache5

To enable SSL for the Apache and Apache 5 clients, configure an SSLContext using the sslContext parameter:

val apacheClient = HttpClient(Apache5) { engine { sslContext = SslSettings.getSslContext() } }

Java

For the Java client, configure an SSLContext using the sslContext() function inside the config {} block:

val javaClient = HttpClient(Java) { engine { config { sslContext(SslSettings.getSslContext()) } } }

Jetty

For the Jetty client, use the sslContextFactory parameter to create an instance of SslContextFactory and pass SSLContext:

val jettyClient = HttpClient(Jetty) { engine { sslContextFactory = SslContextFactory.Client().apply { sslContext = SslSettings.getSslContext() } } }

JVM and Android

CIO

The CIO engine allows you to configure HTTPS settings inside the https {} block. Inside this block, you can access TLS parameters provided by TLSConfigBuilder.

In the following example, a TrustManager instance is used to trust the server certificate, and the addKeyStore() function adds a client certificate:

val cioClient = HttpClient(CIO) { engine { https { trustManager = SslSettings.getTrustManager() addKeyStore(SslSettings.getKeyStore(), "foobar".toCharArray()) } } }

Android

The Android engine uses the sslManager property to configure SSL settings. This property accepts HttpsURLConnection as a parameter that allows you to pass SSLSocketFactory:

val androidClient = HttpClient(Android) { engine { sslManager = { httpsURLConnection -> httpsURLConnection.sslSocketFactory = SslSettings.getSslContext()?.socketFactory } } }

OkHttp

To configure SSL for the OkHttp client engine, pass a SSLSocketFactory and X509TrustManager to the sslSocketFactory() function within the config {} block:

val okHttpClient = HttpClient(OkHttp) { engine { config { sslSocketFactory(SslSettings.getSslContext()!!.socketFactory, SslSettings.getTrustManager()) } } }

Darwin

To configure trusted certificates for the Darwin engine, use CertificatePinner.

02 November 2026