π SSL Pinning in Mobile Apps: Android & iOS (Practical Guide + Trade-offs) - Part 1
When building mobile apps that consume APIs over the internet, HTTPS is mandatoryβbut sometimes itβs not enough. If your app handles sensitive data (finance, health, enterprise), you might want to go one step further:
When building mobile apps that consume APIs over the internet, HTTPS is mandatoryβbut sometimes itβs not enough.
If your app handles sensitive data (finance, health, enterprise), you might want to go one step further:
π Certificate Pinning (aka βSSL pinningβ)
This article explains:
What SSL pinning actually is (and what it isnβt)
How to implement it in Android
In a second part, these topics will be covered:
How to implement it in iOS (both
.cerand Public Key approaches)The real trade-offs nobody tells you
π§ What is SSL Pinning (really)?
Despite the name, modern apps use TLS, not SSL.
π The correct term is:
Certificate Pinning
or TLS Pinning
But βSSL pinningβ is still widely used.
π Default HTTPS behavior
By default, your app trusts any valid certificate signed by trusted Certificate Authorities (CAs).
That means:
App β HTTPS β Server (valid cert) β OK
π¨ The problem
If an attacker installs a malicious certificate (e.g. on public WiFi), they could:
- Intercept traffic
- Decrypt requests
- Act as a proxy (MITM attack)
π What Pinning Changes
Instead of trusting all valid certs:
π Your app trusts only a specific certificate or public key
If it doesnβt match β β connection rejected
π± Android Implementation (OkHttp)
Android makes this relatively straightforward thanks to OkHttp.
βοΈ Public Key Pinning (recommended)
val certificatePinner = CertificatePinner.Builder()
.add("api.yourservice.com", "sha256/AAAAAAAAAAAAAAAAAAAA...")
.build()
val client = OkHttpClient.Builder()
.certificatePinner(certificatePinner)
.build()
π§ͺ How to get the SHA-256 hash
You can extract it using OpenSSL:
openssl s_client -connect api.yourservice.com:443 -servername api.yourservice.com \
| openssl x509 -pubkey -noout \
| openssl pkey -pubin -outform der \
| openssl dgst -sha256 -binary \
| openssl enc -base64
Wrapping Up (Part 1)
At this point, weβve covered what SSL pinning really is, what problems it solves (and what it doesnβt), and how to implement it on Android using a modern, production-ready approach.
If thereβs one takeaway from this first part, itβs this:
Pinning is not about replacing your security model β itβs about strengthening the transport layer.
On Android, the ecosystem makes it relatively straightforward to adopt public key pinning with tools like OkHttp. However, the real challenge isnβt implementation β itβs operational discipline:
- Handling certificate rotation
- Avoiding hard failures in production
- Understanding when pinning actually adds value
Before moving forward, ask yourself:
Do I really need pinning, or am I trying to compensate for missing backend security?
In the next part, weβll move to iOS β where things are a bit more low-level, and where the trade-offs become even more evident.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.