ktor client https request with self signed certificate


Jay Yanez

I have a Ktor server application (REST API) with a self signed certificate.

From the browser (after the warning and confirmation) it works fine, port 80 redirects to 8443.

However, if I try this from the Ktor Apache Client:

fun main(args: Array<String>) = runBlocking {

    val client = HttpClient(Apache) {
        install(JsonFeature) {
            serializer = GsonSerializer()
        }
    }

    val job = GlobalScope.launch {
        try {
            //self-signed certificate
            val resultWillFail = client.get<String>("https://10.0.0.11:8443/get-my-services")
            println("${resultWillFail}")
            val resultOk = client.get<String>("https://en.wikipedia.org/wiki/Main_Page") //ok
            println("${resultOk}")
        } catch (e: Exception) {
            println("Error: ${e.message}")
        }
    }

    job.join()
}

My request to https://10.0.0.11:8443/get-my-services will fail:

Error: General SSLEngine problem

I also tried the same using curl:

curl: (77) schannel: Next InitializeSecurityContext failed: SEC_E_UNTRUSTED_ROOT (0x80090325) - The certificate chain was issued by an untrusted authority.

So my question is: how to use self signed certificate in ktor client (Apache)?

Thanks, J

Erik Pragt

You need to configure Apache HttpClient to ignore self-signed certificates and pass them to KTor. Based on the information here , you can ignore self-signed certificate verification with the following code:

// use the TrustSelfSignedStrategy to allow Self Signed Certificates
val sslContext = SSLContextBuilder
        .create()
        .loadTrustMaterial(TrustSelfSignedStrategy())
        .build()

val allowAllHosts = NoopHostnameVerifier()
val connectionFactory = SSLConnectionSocketFactory(sslContext, allowAllHosts)

val client = HttpClients
        .custom()
        .setSSLSocketFactory(connectionFactory)
        .build() 

The last thing you want to do is use the client in your KTor code. I haven't tried it yet, but let me know about yours.

Related


ktor client https request with self signed certificate

Jay Yanez I have a Ktor server application (REST API) with a self signed certificate. From the browser (after the warning and confirmation) it works fine, port 80 redirects to 8443. However, if I try this from the Ktor Apache Client: fun main(args: Array<Strin

Java ssl/https client using self signed certificate

csheets: I am trying to write a Java https client using jdk version 1.6.0_32. I have a self-signed public certificate that I have imported into a new truststore. The problem is that I keep getting "Exception in thread "main" "javax.net.ssl.SSLHandshakeExceptio

Java ssl/https client using self signed certificate

csheets: I am trying to write a Java https client using jdk version 1.6.0_32. I have a self-signed public certificate that I have imported into a new truststore. The problem is that I keep getting "Exception in thread "main" "javax.net.ssl.SSLHandshakeExceptio

Java ssl/https client using self signed certificate

csheets: I am trying to write a Java https client using jdk version 1.6.0_32. I have a self-signed public certificate that I have imported into a new truststore. The problem is that I keep getting "Exception in thread "main" "javax.net.ssl.SSLHandshakeExceptio

Https iOS with self signed certificate

Michael 67 I have a server with a self signed certificate. I want to connect device to server via https form. I heard I had to accept this connection. but I do not know. I have a self signed certificate as it is a test server. But I want to access it in https

Https iOS with self signed certificate

Michael 67 I have a server with a self signed certificate. I want to connect device to server via https form. I heard I had to accept this connection. but I do not know. I have a self signed certificate as it is a test server. But I want to access it in https

Https iOS with self signed certificate

Michael 67 I have a server with a self signed certificate. I want to connect device to server via https form. I heard I had to accept this connection. but I do not know. I have a self signed certificate as it is a test server. But I want to access it in https

egit with self-signed certificate, https

Hubidubi : I want to use a git repo accessible via https, the Https server has a self signed certificate. When trying to clone a repository using eclipse + egit, I always get the error: https://host/path : cannot open git-upload-pack sun.security.validator.Val

egit with self-signed certificate, https

Hubidubi : I want to use a git repo accessible via https, the Https server has a self signed certificate. When trying to clone a repository using eclipse + egit, I always get the error: https://host/path : cannot open git-upload-pack sun.security.validator.Val

egit with self-signed certificate, https

Hubidubi : I want to use a git repo accessible via https, the Https server has a self signed certificate. When trying to clone a repository using eclipse + egit, I always get the error: https://host/path : cannot open git-upload-pack sun.security.validator.Val

Apache HTTP Client Self-Signed Certificate

Wong Fei Hung: I'm trying to make an HTTP request to a web service through an Apache HTTP client, but I'm getting ajavax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCert

Java: SSL client authentication with self signed certificate

Chris: I'm trying to secure a connection for a Java client/server application that communicates over the internet. My idea is to use SSL sockets with self signed certificates and client authentication. I did the following: Server: The keystore containing the n

Apache HTTP Client Self-Signed Certificate

Wong Fei Hung: I'm trying to make an HTTP request to a web service through an Apache HTTP client, but I'm getting ajavax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCert

Apache HTTP Client Self-Signed Certificate

Wong Fei Hung: I'm trying to make an HTTP request to a web service through an Apache HTTP client, but I'm getting ajavax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCert

Java Rest client using self signed certificate

MrBreaker_1972 Hello, I'm trying to write a small Rest client to access our cloud server (Rest Webservices). The connection is secured with an SSL client certificate which, if I understand correctly, is not signed by any certificate authority and the problem o

Java Rest client using self signed certificate

MrBreaker_1972 Hello, I'm trying to write a small Rest client to access our cloud server (Rest Webservices). The connection is secured with an SSL client certificate which, if I understand correctly, is not signed by any certificate authority and the problem o

Java: SSL client authentication with self signed certificate

Chris: I'm trying to secure a connection for a Java client/server application that communicates over the internet. My idea is to use SSL sockets with self signed certificates and client authentication. I did the following: Server: The keystore containing the n

Java: SSL client authentication with self signed certificate

Chris: I'm trying to secure a connection for a Java client/server application that communicates over the internet. My idea is to use SSL sockets with self signed certificates and client authentication. I did the following: Server: The keystore containing the n

Apache HTTP Client Self-Signed Certificate

Wong Fei Hung: I'm trying to make an HTTP request to a web service through an Apache HTTP client, but I'm getting ajavax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCert

Apache HTTP Client Self-Signed Certificate

Wong Fei Hung: I'm trying to make an HTTP request to a web service through an Apache HTTP client, but I'm getting ajavax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCert

Java Rest client using self signed certificate

MrBreaker_1972 Hello, I'm trying to write a small Rest client to access our cloud server (Rest Webservices). The connection is secured with an SSL client certificate which, if I understand correctly, is not signed by any certificate authority and the problem o

Java Rest client using self signed certificate

MrBreaker_1972 Hello, I'm trying to write a small Rest client to access our cloud server (Rest Webservices). The connection is secured with an SSL client certificate which, if I understand correctly, is not signed by any certificate authority and the problem o

Java: SSL client authentication with self signed certificate

Chris: I'm trying to secure a connection for a Java client/server application that communicates over the internet. My idea is to use SSL sockets with self signed certificates and client authentication. I did the following: Server: The keystore containing the n