Mobile SSL Pinning - Implementation and Security
Strengthen your mobile application security with certificate pinning.
SSL Pinning (or Certificate Pinning) is a security technique that associates a mobile application with a specific SSL certificate or its public key. This prevents man-in-the-middle attacks even if the attacker has a valid certificate issued by a trusted certificate authority.
Without pinning, an application trusts any certificate signed by a CA in the system trust store. With pinning, the application only trusts the certificate or public key you have specifically configured.
MoniTao helps you monitor your certificates and alerts you when they change, which is crucial if you use SSL Pinning because you'll need to update your applications during renewals.
What is SSL Pinning?
Understanding the concept of certificate pinning:
- Trust store vs Pinning: normally, an application trusts all system CAs. Pinning restricts this trust to a specific certificate or key.
- MITM protection: even if an attacker obtains a valid certificate (via CA compromise or social engineering), the application will reject it.
- Pin certificate vs Pin public key: you can pin the entire certificate or just the public key. Pinning the public key allows certificate renewal without changing the pin.
- Backup pins: always configure backup pins to avoid locking out your users in case of certificate renewal.
Types of SSL Pinning
Different pinning approaches:
- Certificate Pinning: pin the exact certificate. Simple but requires an app update with each certificate renewal.
- Public Key Pinning: pin only the public key. Allows certificate renewal as long as the key stays the same.
- Intermediate/Root Pinning: pin the intermediate or root certificate. More flexible but less secure since the CA can issue other certificates.
- SPKI Hash Pinning: pin the SHA-256 hash of the Subject Public Key Info. The most common and recommended method.
Implementing SSL Pinning
Steps to implement pinning:
- Extract the pin: get the SPKI hash of your certificate or public key.
- Configure the application: add the pin in your application (iOS: Info.plist, Android: network-security-config).
- Add backup pins: include at least one backup pin to avoid lockouts during renewals.
- Test rigorously: test that the application rejects unpinned certificates and accepts correct ones.
Implementation Examples
Code for different platforms:
# Extract SPKI hash from a certificate
openssl s_client -connect api.mysite.com:443 2>/dev/null | \
openssl x509 -pubkey -noout | \
openssl pkey -pubin -outform DER | \
openssl dgst -sha256 -binary | base64
# Result: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
# Android (network_security_config.xml)
<network-security-config>
<domain-config>
<domain includeSubdomains="true">api.mysite.com</domain>
<pin-set expiration="2025-01-01">
<pin digest="SHA-256">AAAA...=</pin>
<pin digest="SHA-256">BBBB...=</pin> <!-- backup -->
</pin-set>
</domain-config>
</network-security-config>
The SPKI hash is added to the application's network security configuration. Always include a backup pin.
Best Practices
Tips for successful SSL Pinning:
- Always have backup pins: without backup, a certificate renewal will lock out all your users until they update the app.
- Pin the public key, not the certificate: this allows certificate renewal without changing the pin, as long as you keep the same key.
- Plan renewals: coordinate certificate renewal with an application update to add the new pin.
- Monitor your certificates: use MoniTao to be alerted before expiration and have time to prepare the new pin.
SSL Pinning Checklist
- SPKI hash correctly extracted
- Backup pin configured
- Configuration tested with valid certificate
- Rejection test with invalid certificate
- Update process documented
- Expiration monitoring configured
Frequently Asked Questions
Is SSL Pinning mandatory?
No, but it's strongly recommended for applications handling sensitive data (banking, health, etc.).
What happens if I change the certificate without updating the app?
The application will refuse to connect to your server. This is why backup pins are essential.
How do I handle pinning with Let's Encrypt?
Let's Encrypt renews certificates often. Pin the public key and keep the same key during renewals, or pin the intermediate certificate.
Does pinning work with CDNs?
It's complex because CDNs can have multiple certificates. Consult your CDN's documentation.
How do I debug pinning errors?
Use Charles Proxy or Wireshark to capture traffic and check which certificate is presented.
Can MoniTao help with SSL Pinning?
MoniTao monitors your certificates and alerts you before expiration, giving you time to prepare new pins.
Secure Your Mobile Applications
SSL Pinning is an essential security layer for mobile applications handling sensitive data. Well implemented, it protects against even sophisticated MITM attacks.
With MoniTao, monitor your certificates and anticipate renewals to maintain a smooth user experience while keeping optimal security.
Useful Links
Ready to Sleep Soundly?
Start free, no credit card required.