The incident: nothing was deployed, but overnight every request from the mobile app started failing. The website works. The API works from curl. Only the app is broken, for every user, and the backend team sees no errors because the requests never arrive.
Then someone remembers the app pins the server's certificate, and the certificate renewed last night.
Pinning means the app accepts only specific keys for your server, on top of normal certificate validation. It protects against a mis-issued or locally installed certificate. It also means any change to those keys on the server breaks every installed copy of the app until users update. Here's how to pin without that risk, or decide not to pin.
What's actually pinned
There are three common choices, and they fail differently:
- The leaf certificate (the whole cert): breaks at every renewal. Avoid.
- The leaf's public key (SPKI hash): survives renewal only if the new certificate reuses the same key. Many ACME clients generate a new key at each renewal by default, so this breaks silently on the renewal date.
- An intermediate or root CA's public key: survives renewals, but breaks if your CA changes its intermediates or you switch CAs. Let's Encrypt, for example, issues from several intermediates and can rotate them.
Certificate lifetimes are also getting shorter: the CA/Browser Forum has set maximum public TLS certificate lifetimes to drop in stages to 47 days by 2029. More renewals mean more chances for a pin to break.
Pin safely, if you pin
- Pin public keys (SPKI SHA-256), never whole certificates.
- Always include a backup pin: the hash of a key that isn't in use yet, kept offline, so you can switch to it without an app update. Two or more pins is the rule on both platforms.
- Pin at the right level. Pinning your CA's keys (several of them, including the root) is the safer choice for most teams. Pinning your own leaf key is stricter, but then the key must be reused on renewal, and that has to be written down in the renewal process.
- Set an expiration where the platform supports it, so an old app version stops enforcing pins instead of failing forever.
- Tell whoever runs the certificates. Most pinning outages come from a routine change made by someone who didn't know the app depends on the key.
Getting a SPKI hash from a live server:
openssl s_client -connect api.example.com:443 -servername api.example.com </dev/null \ | openssl x509 -pubkey -noout \ | openssl pkey -pubin -outform der \ | openssl dgst -sha256 -binary \ | openssl enc -base64 iOS
Since iOS 14 you can pin in Info.plist without code: under NSAppTransportSecurity > NSPinnedDomains, add your domain with NSPinnedCAIdentities or NSPinnedLeafIdentities, each an array of SPKI-SHA256-BASE64 values. There's no expiration setting, so backup pins matter even more here.
If you pin in code instead (a URLSessionDelegate that checks the server trust), be careful about what you hash, and make sure the failure path is logged. A delegate that cancels the challenge produces a generic cancelled error, which tells nobody that pinning was the cause.
Android
Use the network security config:
<network-security-config> <domain-config> <domain includeSubdomains="true">api.example.com</domain> <pin-set expiration="2027-06-01"> <pin digest="SHA-256">primaryKeyHashBase64=</pin> <pin digest="SHA-256">backupKeyHashBase64=</pin> </pin-set> </domain-config> </network-security-config> After the expiration date, pinning is no longer enforced for that domain, which limits the damage from an old app version nobody updated.
With OkHttp, CertificatePinner does the same in code and throws SSLPeerUnverifiedException with "Certificate pinning failure!" and the list of hashes the server actually presented, which is useful when debugging.
Android's documentation itself recommends against pinning for most apps, because of the risk of exactly this outage.
Debug builds
Pinning blocks HTTPS inspection with a debugging proxy, because the proxy presents its own certificate. Keep pinning in release builds and turn it off in debug builds:
- Android: in
<debug-overrides>, trust user CAs with<certificates src="user" overridePins="true" />. It applies only to debuggable builds. - iOS: keep the pinning config in a release-only
Info.plistor behind a build setting, so debug builds use normal validation.
Testing before it happens in production
- Put the next key's hash in the app before you need it, then do a planned rotation on a staging server: switch staging to the backup key and confirm the current app still connects.
- Point a test build at a server with a valid certificate from a different CA. It must fail, and the failure should show up in your logs as a pinning failure, not a generic network error.
- Add the pin hashes and their expiry to the same runbook as the certificate renewal, so the next person to renew knows the app depends on them.
Or don't pin
For many apps, standard TLS validation is enough, together with Certificate Transparency checks where the platform enforces them. Pinning is worth it when the threat model really needs it, and only with backup pins, expiry, and a rotation plan. Without those it causes more outages than attacks it stops.
This article was created with the help of AI.