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:
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:
Java
For the Java client, configure an SSLContext using the sslContext() function inside the config {} block:
Jetty
For the Jetty client, use the sslContextFactory parameter to create an instance of SslContextFactory and pass SSLContext:
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:
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:
OkHttp
To configure SSL for the OkHttp client engine, pass a SSLSocketFactory and X509TrustManager to the sslSocketFactory() function within the config {} block:
Darwin
To configure trusted certificates for the Darwin engine, use CertificatePinner.