Certificate pinning is a security technique in which an app or client stores the expected certificate or public key for a server and accepts only that identity, instead of trusting any certificate signed by a certificate authority in the device trust store. It blocks man-in-the-middle attacks using rogue or mis-issued certificates, but a mismatched pin breaks the connection, so it needs careful rotation planning.
Key takeaways
- Certificate pinning accepts only one expected certificate or public key, blocking rogue certificates that normal TLS validation would trust.
- Pin the public key, not the leaf certificate, and always keep a backup pin so rotation does not lock users out.
- OWASP advises most teams not to pin at all; it fits native mobile apps and server-to-server links where you control both ends.
Normal TLS trusts hundreds of certificate authorities. If any one of them issues a bad certificate for your server, or a rogue authority is added to a device, an attacker can sit in the middle of the connection — and the app will still show it as secure. Certificate pinning closes that gap by trusting one identity only.
What is certificate pinning?
Certificate pinning is the process of associating a host with its expected X.509 certificate or public key. Once that identity is known, the client stores it and accepts a connection only when the server presents a matching certificate or key.
OWASP defines it exactly that way in its Pinning Cheat Sheet: if more than one identity is acceptable, the program holds a pinset, and the presented credential must match one element of that set.
Think of normal TLS as accepting any passport issued by a recognized country. Pinning is accepting only one named person, by face, no matter which passport they carry. That is stronger — and much less forgiving if the person changes their appearance.
How does certificate pinning work?
During the TLS handshake, the server presents its certificate as usual. The client first runs normal validation, then adds one extra step: it compares the presented certificate, public key or hash against the pins it stored at build time or learned earlier.
If the presented identity matches a pin, the connection proceeds. If it does not match, the connection must fail. OWASP is clear on this point: on a mismatch, fail the method, log it on the client, and do not give the user an option to proceed past the warning.
The pin can be added at development time, learned on first use (trust on first use, or TOFU), or updated over an unpinned channel. Preloading at development time is preferred, because an attacker cannot taint a pin that was baked in before the app shipped.
What attack does certificate pinning stop?
Pinning stops man-in-the-middle attacks that abuse the trust system itself. OWASP names two forms: an attacker acquires a rogue certificate for the victim site from a trusted certificate authority, or an attacker injects a dangerous authority into the client trust store.
That second form matters on mobile. A user tricked into installing a profile or a corporate interception proxy can add a new authority to the device. Normal TLS then trusts certificates from that authority. A pinned app does not, because the presented certificate still fails the pin check.
Pinning also shrinks the attack surface. As OWASP Mobile Application Security puts it, withdrawing trust from unnecessary authorities reduces the number of parties that must stay honest for your connection to be safe. You can check whether a site's own certificate chain is healthy with our free SSL checker.
What should you pin: certificate, public key or hash?
You have three choices, and the choice decides how often pinning breaks. OWASP recommends pinning the public key, specifically the Subject Public Key Info (SPKI), because it includes the key parameters plus algorithm context.
| What you pin | How it works | Main advantage | Main risk |
|---|---|---|---|
| Leaf certificate | Store the whole end-entity certificate | Simplest to implement; 100% certainty about the host | Breaks on every certificate renewal unless the app is updated |
| Public key (SPKI) | Store the public key from the certificate | Survives renewal if the key is reused; pin can be made before the certificate exists | Harder to extract keys on some platforms; reusing keys to keep pins valid is poor key management |
| Hash of certificate or key | Store a digest, such as a SHA-256 fingerprint | Small, fixed length; often available as a native API | No access to key parameters; breaks on renewal like a leaf pin |
| Intermediate CA | Store the issuing CA certificate | Survives leaf rotation; useful as a backup pin | Trusts every certificate that CA issues, not just yours |
| Root CA | Store the root certificate | Rarely breaks | Not recommended; trusts all intermediates under that root |
Pinning a leaf certificate is recommended by OWASP only with a backup pin, such as the intermediate CA, so the app can fall back if the leaf changes. Pinning a root CA is generally not recommended. For most teams, the practical answer is: pin the SPKI, keep a backup pin for a second key, and plan rotation in advance.
One warning from OWASP: most certificates today last only 90 days, so a leaf pin means updating the app roughly every three months. A public key pin stretches that timeline, because the same key can appear in a renewed certificate — but renewing with the same key only to avoid breaking pins is bad key management and should be an emergency measure, not a habit.
When should you use certificate pinning?
Pinning fits a narrow set of cases: native mobile apps, thick desktop clients, and server-to-server connections where you control both ends and can push an update when a pin changes. OWASP Mobile Application Security recommends it especially for higher-assurance mobile apps.
It is most valuable in hostile environments — banking apps, health apps, messaging apps, and apps used on public or corporate networks where interception proxies are common. If your app handles money, health data or confidential messages, the rogue-certificate threat is real enough to consider.
Before you decide, answer the first question OWASP poses: "Should I pin?" Its answer is blunt — probably never. The risk of outages almost always outweighs the security gain, given modern improvements in certificate transparency, shorter certificate lifetimes and browser protections. Read the full threat model before you commit.
When should you not pin?
OWASP lists five clear stop signs. If any of them applies, do not pin.
- You do not control both sides. If you do not control the client and the server, do not pin.
- You cannot update the pinset securely. A pin you cannot change safely is a future outage.
- Updating pins is disruptive. If it needs an app redeployment users may delay, probably do not pin — unless you control forced updates inside a company.
- The key cannot be predicted in advance. If you do not know the next key before it goes into service, pinning will break the connection.
- It is not a native mobile app. Do not pin in a website or a web app; browsers have their own protections, and HPKP is deprecated.
There is one common exception. Organizations that run interception proxies for data-loss prevention will break pinned apps. In that case, do not offer to allow-list the proxy — that defeats the point. Add the interception proxy's public key to the pinset only after the risk-acceptance team instructs you to.
How is certificate pinning implemented on Android and iOS?
On Android, the preferred method since Android N is the Network Security Configuration file, which lets you declare pins without changing app code. The <pin-set> setting holds base64-encoded SHA-256 hashes of the public keys. Libraries such as OkHttp also offer programmatic pinning.
On iOS, Apple suggests pinning a CA public key through App Transport Security settings in Info.plist, and documents server trust evaluation for custom checks. Open-source libraries such as TrustKit provide an easier API for iOS and macOS pinning.
Both platforms warn against writing validation from scratch. OWASP repeats the warning for Android, iOS and .NET: implementing pinning validation yourself is extremely likely to contain mistakes that create severe vulnerabilities. Use the platform feature or a well-tested library.
Certificate pinning vs mTLS: what is the difference?
Pinning and mutual TLS (mTLS) solve different problems. Pinning is the client checking the server's identity against a known pin. mTLS is TLS in which the server also authenticates the client, using a client certificate and proof of possession of its private key.
Use pinning when you distrust the certificate authorities and want to be sure you are talking to one server. Use mTLS when the server must be sure who the client is — for example, an API that only trusted partner systems may call. The two can be combined: a server-to-server link can pin the server certificate and require a client certificate at the same time.
Neither replaces the other, and neither replaces normal certificate validation, expiry checks or hostname checks.
What are the common mistakes with certificate pinning?
Most pinning failures are self-inflicted. The classic one is pinning a leaf certificate with no backup pin, then renewing the certificate and locking every user out until they update the app. OWASP's own history notes that a broken pinning implementation is a security vulnerability, while the absence of pinning is not one.
- No backup pin. Always pin at least two keys: the current one and a backup held offline.
- Letting users bypass a mismatch. Users click past warnings, so a bypass option turns pinning into a suggestion.
- Skipping normal validation. Pinning on top of broken validation is still broken validation.
- Pinning in a web app. Browsers do not support app-level pinning; HPKP is dead.
- No rotation plan. Decide before launch who updates pins, how they ship, and what happens if a key is compromised.
- Forgetting interception proxies. Corporate networks and security tools that inspect TLS will break pinned apps unless the proxy key is in the pinset.
How do you implement certificate pinning safely?
Start with the threat model, not the code. Confirm that rogue certificates are a real risk for your users, that you control both ends, and that you can ship pin updates quickly.
- Choose what to pin. Pin the SPKI public key, with a backup pin for a second key stored offline.
- Use the platform feature. Android Network Security Configuration or iOS App Transport Security, or a tested library such as OkHttp or TrustKit.
- Keep normal validation. Chain, expiry and hostname checks stay on; pinning is an extra gate.
- Fail closed. On mismatch, close the connection, log it, and show a clear error with no bypass.
- Plan rotation. Ship the next pin before the current key retires, and test the rollover in staging.
- Monitor for failures. A spike in pin failures can mean a real attack — or a rotation you forgot. Either way, you need to see it fast, which is why certificate monitoring matters alongside pinning.
For websites rather than apps, the equivalent protections are different: keep certificates short-lived and monitored, enable Certificate Transparency monitoring, and check your headers and TLS setup regularly. Our free security headers checker is a quick place to start.
OWASP Mobile Application Security explains the same trade-off in its network communication guidance: pinning reduces the trusted authorities to the ones you actually use. Our guide to securing your Wi-Fi covers the network side of the same threat.
The bottom line
Certificate pinning is a powerful, narrow tool: it ties an app to one server identity and blocks rogue certificates that normal TLS would trust. Used well, it protects high-value mobile and server-to-server traffic.
Used carelessly, it locks your own users out. Pin the public key, keep a backup pin, fail closed, and only pin when you control both ends and can update pins safely. If that does not describe your app, strong TLS, short-lived certificates and regular certificate checks are the better investment.
Frequently asked questions
What is certificate pinning in simple terms?
It is a promise an app makes about who it will talk to. Instead of accepting any certificate signed by a trusted authority, the app checks the server against one certificate or public key it already knows. If the presented identity does not match, the app refuses to connect.
Is certificate pinning the same as public key pinning?
They are closely related. Certificate pinning stores the whole expected certificate, while public key pinning stores only the public key from it. Public key pinning is more flexible because the key can stay the same when a certificate is renewed, so OWASP recommends pinning the public key, specifically the Subject Public Key Info.
Does certificate pinning replace TLS validation?
No. Pinning is an extra check on top of normal TLS validation. The app should still verify the certificate chain, expiry and hostname, then check the pin. Skipping normal validation and relying only on a pin is a common implementation mistake.
Can certificate pinning be bypassed?
Yes, on a device the attacker controls. Someone who can modify the app, replace stored pins or hook the validation code at runtime can bypass it. Pinning raises the bar against network attackers and rogue certificates; it does not make a compromised device safe.
What is HPKP and is it still used?
HPKP, HTTP Public Key Pinning, was a browser header that let websites tell browsers which keys to trust. It is deprecated and no longer supported by modern browsers, because a wrong or lost pin could make a site unreachable for months. Do not use HPKP for websites today.
Should I use certificate pinning in my app?
Only if you control both the client and the server, you can update pins securely, and your threat model includes rogue certificates or a hostile network. OWASP says there is almost no situation where pinning is the right call for a typical app, because the outage risk usually outweighs the benefit.
What happens if a pinned certificate expires?
The app refuses to connect until it is updated with a new pin. That is why you should pin the public key rather than the certificate, keep a backup pin for a second key, and plan rotation before the current certificate expires.
Does certificate pinning work in browsers?
Not as an app feature. Browsers rely on their own trust stores and built-in protections instead. Pinning is mainly for native mobile apps, thick clients and server-to-server connections where the developer controls the client.
Sources
The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.
Checked against the OWASP Pinning Cheat Sheet and OWASP Mobile Application Security guidance on identity pinning.
How we write and fact-check →