Skip to content

Lesson 8 of 13 · 13m

Certificate Pinning: What It Buys and What It Breaks

Pin the intermediate's SPKI with a backup pin, accept that expiry is a brick event and objection defeats it on-device in minutes — pinning fights the network, not the device owner.

What pinning actually does

TLS trusts any CA the platform trusts — over a hundred of them, and any one can mint a certificate for your domain. Certificate pinning narrows that: the app carries an expected key hash and refuses the connection when the presented chain doesn't contain it. The incident that made the idea famous was DigiNotar, summer 2011 — an Iranian CA compromise produced a fraudulent *.google.com certificate that was used against real users in the wild, and Chrome responded by hardcoding Google's keys into the browser. Pinning kills rogue-but-trusted-CA MITM, and it kills the far more common version: a user-installed CA on a device someone else controls, or the Burp CA your red team just installed.

Pin the key, not the cert

The wrong model: "pin the certificate." Leaf certs rotate — Let's Encrypt leaves die every 90 days, managed services rotate without asking you. What survives rotation is the SubjectPublicKeyInfo — the public key — of your intermediate or your own re-used leaf key. The correct pin is the SPKI's SHA-256, base64:

CertificatePinner.Builder()
    .add("api.example.com", "sha256/<base64-of-SPKI>")
    .add("api.example.com", "sha256/<backup-CA-SPKI>")   // offline, different CA
    .build()

Two pins minimum: your current issuer's intermediate SPKI, and a backup pin — an SPKI from a second CA whose cert you haven't even issued yet, kept documented offline. The backup pin is what lets you emergency-reissue after a CA breach without shipping a new binary first. Android's declarative equivalent is <pin-set> in network_security_config.xml, whose expiration attribute is worth understanding: past that date the pin set is ignored — fail-open by design, so a forgotten expiry degrades to system trust instead of bricking the fleet forever.

Browsers already ran this experiment and lost. HTTP Public Key Pinning let sites tell Chrome "only trust these keys" — sites bricked themselves at rotation, and Chrome deprecated HPKP in 67 and removed it in 72 (January 2019). The mobile rule learned from the wreckage: pin SPKI, carry a backup pin, plan rotation, and accept that a pin failure is an availability event — the app goes dark for every user until they update. That's the cost. The benefit is a hard wall against CA-level MITM.

What pinning doesn't do

On the device side it does not survive device ownership. objection -g com.app explore then android sslpinning disable hooks CertificatePinner.check, TrustManager.checkServerTrusted, and friends; ios sslpinning disable does the same to SecTrustEvaluate and NSURLSession delegates; a frida script against ssl_session_verify catches Conscrypt-native checks. A motivated person with the app on their own phone reads your API in an afternoon regardless of pinning. Pinning is a defense against the network adversary — the hotel Wi-Fi, the hostile CA, the DNS redirect — and a cost-raiser against the reverse engineer. It is not a boundary against the person holding the device, and your server should be written as if pinning doesn't exist.

iOS mechanics

URLSession:didReceiveChallenge → SecTrustEvaluateWithError → extract SecCertificateCopyKey of each cert and compare SPKI digests — or use TrustKit, which wraps exactly this plus backup pins and expiry handling. WKWebView connections go through ATS but not your URLSession delegate, so in-webview pinning needs a different layer (SPKI checks at the navigation delegate are partial — know the gap).

Practice

Pin the intermediate SPKI, carry a backup pin from a second CA, store the SPKI hashes in the app — never the private material. Test rotation in staging before the intermediate actually rotates. Keep expiration set on declarative pin-sets and treat approaching expiry as a release-blocking issue. Then write the server to distrust the client anyway — pinning decided who the client talks to, not whether the client is telling the truth.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.