CodexBloom - Programming Q&A Platform

Kotlin: Trouble with Retrofit not handling SSL certificate pinning properly in Android 14

👀 Views: 1991 💬 Answers: 1 📅 Created: 2025-06-17
kotlin android retrofit okhttp ssl Kotlin

I'm dealing with I'm working with an scenario where my Retrofit instance is not respecting SSL certificate pinning, and I'm getting `javax.net.ssl.SSLHandshakeException: Handshake failed` errors when making requests. I've implemented certificate pinning using OkHttpClient, and I'm using Retrofit 2.9.0. Here’s how I’ve set it up: ```kotlin val certificatePinner = CertificatePinner.Builder() .add("example.com", "sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=") .build() val client = OkHttpClient.Builder() .certificatePinner(certificatePinner) .build() val retrofit = Retrofit.Builder() .baseUrl("https://example.com/") .client(client) .addConverterFactory(GsonConverterFactory.create()) .build() ``` Despite this configuration, when I attempt to fetch data from the server, I receive the SSL handshake exception. I’ve double-checked the SHA-256 hash of the certificate, and it matches the one provided by the server. I also verified that my device is running Android 14 and has the latest updates. In addition to that, I’ve tried testing with different devices and emulators, but the scenario continues. I've also ensured that the server’s certificate is valid and properly configured. Is there anything I might be missing in the setup, or could there be an scenario related to how Retrofit or OkHttp handles SSL pinning on Android 14? Any insights or suggestions on how to resolve this would be greatly appreciated! This is happening in both development and production on Ubuntu 20.04. Thanks for taking the time to read this!