Certificate pinning is required because it closes a critical gap in the public key infrastructure (PKI) system that otherwise allows attackers to intercept encrypted traffic. Without pinning, a mobile app or client will trust any certificate signed by a trusted certificate authority (CA), which means a compromised CA, a malicious root certificate installed on a device, or a man-in-the-middle (MITM) proxy can silently decrypt and modify supposedly secure connections. Pinning forces the client to accept only a specific, pre-defined certificate or public key, so even a valid certificate from a trusted CA will be rejected if it is not the one the app expects.
Background: How PKI Trust Works and Where It Fails
When a client connects to a server over TLS, it validates the server's certificate against a list of trusted root CAs stored in the operating system or browser. This system relies on hundreds of CAs worldwide, each of which can issue certificates for any domain. The trust model assumes all CAs are honest and secure, but history shows otherwise: CAs have been breached, tricked into issuing fraudulent certificates, or coerced by governments. In 2011, a Dutch CA was compromised and issued a fraudulent certificate for a major domain, and in 2015, a subordinate CA was misused to issue certificates for Google domains. These incidents demonstrate that the CA trust model is only as strong as its weakest link.
Additionally, users can install their own root certificates on devices—sometimes deliberately for debugging, sometimes unknowingly through malware. Once a rogue root is installed, every TLS connection becomes vulnerable to interception. Certificate pinning addresses these weaknesses by bypassing the CA trust chain entirely for a specific connection.
How Certificate Pinning Works
Pinning embeds a known certificate or public key hash directly into the client application. During the TLS handshake, after the server presents its certificate, the client compares the certificate or its public key against the pinned value. If they do not match, the connection is aborted. This check happens in addition to standard certificate validation, not instead of it.
There are two common pinning methods:
- Certificate pinning: The entire certificate (or its hash) is pinned. This is strict but requires updating the app whenever the certificate is renewed.
- Public key pinning: The public key (or its hash) is pinned. This is more flexible because the same key can be reused across certificate renewals, reducing update frequency.
Pinning can be implemented at the application layer (e.g., in mobile apps using libraries like OkHttp or TrustKit) or at the network layer (e.g., in browsers via HTTP Public Key Pinning, though this standard has been deprecated). For mobile apps, pinning is typically done in code and bundled with the app binary.
Why Pinning Is Required: Key Threats Mitigated
Pinning is required primarily to defend against man-in-the-middle attacks that exploit the weaknesses of the CA system. Specific threats include:
- Compromised or malicious CA: An attacker who controls a CA can issue a valid certificate for any domain. Pinning rejects such certificates because they do not match the pinned key.
- Rogue root certificates: Malware or malicious insiders can install a root certificate on a device, allowing interception. Pinning ignores the device's trust store for the pinned host.
- Corporate or government interception: Some organizations use TLS inspection proxies that present their own certificates. Pinning can block such interception, which may be desirable for privacy-sensitive apps.
- DNS spoofing and BGP hijacking: Even if an attacker redirects traffic to a server they control, they cannot present the correct pinned certificate, so the connection fails.
Without pinning, an app that handles sensitive data—such as banking, healthcare, or messaging—could be silently intercepted, leading to data theft or manipulation. Pinning adds a layer of certainty that the client is talking to the intended server.
Implementing Pinning: Steps and Considerations
Implementing pinning requires careful planning to avoid breaking connectivity. The general steps are:
- Obtain the server's certificate or public key. This can be done using tools like OpenSSL to extract the public key and compute its hash (e.g., SHA-256).
- Embed the pin in the client application. Store the hash in the app code or configuration. Use a secure storage mechanism if the pin is dynamic.
- Include backup pins. Always pin at least one backup key (e.g., a future key or a key from a different CA) to allow for key rotation without breaking the app.
- Test thoroughly. Verify that the app connects successfully to the legitimate server and rejects connections to a test server with a different certificate.
- Plan for key rotation. Establish a process to update pins before the current key expires. Consider using pins with short lifetimes or dynamic pinning via remote configuration.
Pinning should be applied to all connections that carry sensitive data, but not necessarily to every endpoint. Over-pinning can cause operational headaches, especially if the pinned key is lost or compromised.
Common Problems and Pitfalls
Pinning is not without risks. Common issues include:
- App breakage due to key rotation: If the server key changes and the app does not have an updated pin, the app will fail to connect. This is the most frequent cause of pinning-related outages.
- Difficulty with certificate authorities: Some CAs rotate keys frequently, making pinning challenging. Using a CA that supports long-lived keys or pinning to a intermediate CA can help.
- Incompatibility with TLS inspection: Enterprise networks that perform TLS inspection will break pinned apps. Organizations may need to exempt such apps or use mobile device management to install custom pins.
- False sense of security: Pinning does not protect against compromised servers or application-layer vulnerabilities. It only ensures the client is talking to the expected server.
To mitigate these problems, use backup pins, monitor certificate expiration, and have a rollback plan. Some teams use pinning only for high-risk endpoints and rely on standard validation elsewhere.
Pinning vs. Other Security Measures
Pinning is often compared to other TLS security enhancements. Here is how it differs:
- HSTS (HTTP Strict Transport Security): HSTS forces browsers to use HTTPS but does not pin certificates. It protects against protocol downgrade attacks, not CA compromise.
- Certificate Transparency (CT): CT logs certificates publicly, making it harder for CAs to issue fraudulent certificates unnoticed. However, CT does not prevent a client from accepting a fraudulent certificate; it only aids detection. Pinning actively blocks such certificates.
- OCSP Stapling and CRLs: These check revocation status but rely on the CA's honesty and availability. Pinning does not depend on revocation checks.
Pinning is complementary to these measures and is most effective when combined with standard TLS best practices.
Frequently Asked Questions
Is certificate pinning required by regulations?
Some industry standards and regulations recommend or require pinning for specific applications. For example, the Payment Card Industry Data Security Standard (PCI DSS) does not explicitly mandate pinning, but it requires strong cryptography for cardholder data. Mobile app security guidelines from organizations like OWASP recommend pinning for high-risk apps. Always check the specific regulatory requirements for your industry.
Does pinning work with CDNs and load balancers?
Yes, but it requires coordination. If your app connects to a CDN, you must pin the CDN's certificate or public key. Many CDNs support custom certificates, allowing you to use your own key. Alternatively, you can pin the CDN's public key if it is stable. Be aware that CDNs may rotate keys, so backup pins are essential.
Can pinning be bypassed?
Pinning can be bypassed on compromised devices, for example through runtime hooking frameworks like Frida or Xposed, which can modify the app's code to disable pinning checks. This is why pinning is not a silver bullet; it raises the bar but does not make interception impossible. Defense-in-depth, including code obfuscation and tamper detection, is recommended.
What happens if I lose the pinned key?
If the private key corresponding to the pinned public key is lost or compromised, the server must generate a new key pair. Clients with the old pin will reject the new certificate, causing an outage until the app is updated. This is why backup pins and a key rotation plan are critical.
Is pinning still relevant with TLS 1.3?
Yes. TLS 1.3 improves handshake security and reduces latency, but it does not change the trust model. Pinning remains relevant because it addresses CA compromise and rogue certificates, which are independent of the TLS version.
How often should pins be updated?
Pins should be updated before the pinned certificate or key expires. The frequency depends on the certificate lifetime, which is typically one year or less for public CAs. Many organizations update pins annually or use a rolling pinning strategy with multiple pins valid for overlapping periods.