Trusting all certificates with okHttp
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When dealing with HTTP connections in Android development, OkHttp is a popular and effective choice among developers due to its reliability and performance. However, there might be situations, especially during development or testing, where one may want to trust all SSL certificates without performing validation. While it's advantageous for testing purposes, trusting all certificates in production software is not advised due to security vulnerabilities. This article will delve into the technicalities of trusting all certificates with OkHttp, its implementations, and the importance of understanding the underlying security risks.
Understanding SSL and Certificate Validation
Secure Sockets Layer (SSL) is a standard technology that ensures data privacy over an Internet connection. It uses encryption methods to secure the data transmitted between a server and a client. Certificate validation involves verifying that the certificate presented by the server was issued by a trusted Certificate Authority (CA) and has not expired or been revoked. This process helps in ensuring that users connect exclusively to trusted endpoints.
When to Trust All Certificates
During testing, developers might work with self-signed certificates or internal test servers not recognized by well-known Certificate Authorities. In these scenarios, configuring OkHttp to trust all certificates can save time and simplify testing. However, it is vital to revert back to the default configuration before moving to production.
Implementing Trust-All Certificates with OkHttp
Here's a simple example demonstrating how to configure OkHttp to trust all SSL certificates. This implementation should not be used in production due to potential security risks.
- Custom TrustManager: This example uses a custom `TrustManager` that doesn't validate certificate chains.
- Custom SSLSocketFactory: By setting a custom `SSLSocketFactory`, all certificates, valid or not, are accepted.
- Hostname Verification: By setting a `HostnameVerifier` that always returns true, the hostnames fo SSL sessions are effectively ignored.
- Man-in-the-Middle (MITM) Attacks: If any user intercepts the network communication, they could introduce a fake server. This is the primary reason to avoid this in production.
- Sensitive Data Exposure: Lack of validated encryption could lead to the exposure of sensitive user data.
- No Accountability: Without proper certificate validation, there’s no way to ensure that data is sent to or received from the intended server.
Related reading
- UIDevice uniqueIdentifier deprecated - What to do now?
- Unable to connect to Postgres DB due to the authentication type 10 is not supported
- Unable to connect to the server net/http TLS handshake timeout
- Unable to export Apple production push SSL certificate in .p12 format
- Try-finally block prevents StackOverflowError
- Trying to use Spring Boot REST to Read JSON String from POST
- Unable to find valid certification path to requested target - error even after cert imported
- Unable to produce messages to Kafka with SSL enabled

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.