π SSL Pinning in Mobile Apps: Android & iOS (Practical Guide + Trade-offs) - Part 2
Unlike Android, where libraries like OkHttp abstract much of the complexity, iOS takes a more low-level approach to networking and security. This means one thing: You have more control β but also more responsibility.
Unlike Android, where libraries like OkHttp abstract much of the complexity, iOS takes a more low-level approach to networking and security.
This means one thing:
You have more control β but also more responsibility.
In this second part, weβll explore how SSL pinning is implemented in iOS using two different strategies:
Certificate Pinning (
.cer)Public Key Pinning (recommended for production)
Both approaches achieve the same goal β trusting only your backend β but they differ significantly in terms of stability, maintainability, and real-world viability.
Weβll also take a step back and look at the bigger picture:
When pinning makes sense
When it becomes a liability
And how it fits into a broader mobile security strategy
Letβs dive in.
π iOS Implementation
iOS is more low-level. Youβll work with:
- URLSession
- URLSessionDelegate
- Security.framework
There are two approaches:
π’ Approach 1: Certificate Pinning with .cer
This is typically the first approach developers encounter when implementing pinning on iOS.
π§ Steps
- Export your backend certificate as .cer
- Add it to your Xcode project
- Compare it at runtime
π§ͺ Example
class PinningDelegate: NSObject, URLSessionDelegate {
func urlSession(_ session: URLSession,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust,
let serverTrust = challenge.protectionSpace.serverTrust,
let serverCertificate = SecTrustGetCertificateAtIndex(serverTrust, 0) else {
completionHandler(.cancelAuthenticationChallenge, nil)
return
}
let serverCertData = SecCertificateCopyData(serverCertificate) as Data
guard let localCertPath = Bundle.main.path(forResource: "server", ofType: "cer"),
let localCertData = try? Data(contentsOf: URL(fileURLWithPath: localCertPath)) else {
completionHandler(.cancelAuthenticationChallenge, nil)
return
}
if serverCertData == localCertData {
completionHandler(.useCredential, URLCredential(trust: serverTrust))
} else {
completionHandler(.cancelAuthenticationChallenge, nil)
}
}
}
β οΈ Downside (critical)
This approach is:
π£ Fragile
- Certificates expire
- Renewal changes .cer
- Your app breaks
π You must release a new version of the app
π‘ Approach 2: Public Key Pinning (Recommended)
Instead of comparing full certificates:
π Compare public keys
βοΈ Advantages
- Survives certificate renewal
- More stable in production
- Equivalent to Android approach
π§ͺ Conceptual Example
let serverPublicKey = SecCertificateCopyKey(serverCertificate)
let localPublicKey = SecCertificateCopyKey(localCertificate)
// Compare keys or their hashes
// β οΈ Apple APIs here are verbose and require careful handling.
π§ Better Option: Use a Library
Instead of manual implementation, use:
- Alamofire (widely used networking library)
Example
let evaluators: [String: ServerTrustEvaluating] = [
"api.yourservice.com": PinnedCertificatesTrustEvaluator()
]
let manager = ServerTrustManager(evaluators: evaluators)
let session = Session(serverTrustManager: manager)
π What SSL Pinning DOES NOT Do
Letβs be clear:
| Feature | Covered by Pinning |
|---|---|
| Encrypt traffic | β (via TLS) |
| Prevent MITM | β |
| Authenticate user | β |
| Protect API access | β |
| Replace VPN | β |
π You still need:
- JWT / OAuth
- API Gateway
- Rate limiting
- Backend security
β οΈ Real-World Trade-offs
Before adding pinning, ask yourself:
β Operational cost
- Certificate rotation becomes risky
- You need fallback pins
- You need monitoring
β Release dependency
- A backend change can break clients instantly
β Debugging complexity
- Harder to inspect traffic (Charles Proxy, etc.)
π§ When Should You Use It?
Use pinning if:
- You build fintech / healthcare apps
- You operate in hostile network environments
- You have strong DevOps practices
Avoid (or delay) if:
- Youβre building a typical consumer app
- You donβt control backend infrastructure
- Your team lacks experience with cert rotation
π§© Recommended Architecture (No VPN)
Mobile App
β
HTTPS (TLS)
β
API Gateway
β
Authentication (JWT / OAuth)
β
Microservices
Optional hardening:
- Certificate Pinning π
- WAF π‘οΈ
- Rate limiting π¦
π§ Final Thoughts
βSSL pinningβ is often mentioned casually, but:
π Itβs not a silver bullet
π Itβs not a replacement for authentication
π Itβs not trivial to maintain
Used correctly, it adds a strong extra layer of defense.
Used blindly, it becomes a production risk.
π Closing
If youβre working with Kotlin Multiplatform or shared logic, keep in mind:
- Pinning is platform-specific
- Youβll need separate implementations for Android and iOS
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.