What is certificate pinning? How it works and why it breaks

11 min readUpdated

Certificate pinning is a check in which an app accepts a server only if its certificate chain contains a key the app already knows, instead of any certificate a trusted authority has issued for that name [5][7]. It narrows who can stand in for the server, which may reduce man-in-the-middle attacks through a compromised authority [1]. The cost comes later: after a change of key or authority, the app refuses the server until the app itself is updated [5][6]. The term belongs to the SSL and TLS glossary.

What a pin is
a SHA-256 hash of a certificate's SubjectPublicKeyInfo, in base64 [1][7]
Android
a <pin-set> in the app's Network Security Config [5]
Apple platforms
NSPinnedDomains under App Transport Security [7]
Browsers
HPKP, the header of RFC 7469, removed in Chrome 72 and off by default in Firefox [3][4]
Android's guidance
pinning is not recommended for Android apps [6]

How certificate pinning works

A TLS server presents a public-key certificate, one of the kinds of digital certificates, and the client runs its normal checks first, among them that the chain leads to a root in its trust store [12]. By default an app trusts every pre-installed authority, so a fraudulent certificate from any of them would put the app at risk from an on-path attacker [5]. See certificate chains.

Pinning adds one more check. The app holds a set of pins, and the chain counts as valid only if it contains at least one of the pinned public keys [5]. If none matches, the connection is refused even though every other check passed. Apple's App Transport Security behaves the same way: it blocks the connection unless it finds a pinned digest in the chain the server presented, and a pin has no effect on the other security requirements [7].

The pins ship inside the app, so if the server moves to a key that no pin covers, the connection works again only after an app update [5]. SSH clients keep a similar record: OpenSSH stores a server's host key in known_hosts on first connection and warns if it ever changes [14], as described in the comparison of SSL and SSH.

What can be pinned

What can be pinned
PinKept after renewal?Trade-off
The server's certificateno: the renewed certificate has a new fingerprint [15]the narrowest pin; breaks at every renewal
The server's public keyyes, if the renewal keeps the key [15]breaks when the key is replaced
An intermediate authority's keyyes, while certificates come from that intermediate [15]accepts any certificate that authority issues [11]
A root authority's keyyes, while the chain still ends at that root [1]accepts certificates from all of that root's intermediates [11]

Both platform mechanisms pin keys rather than whole certificates. An Android pin and an Apple SPKI-SHA256-BASE64 value are each a SHA-256 hash of the SubjectPublicKeyInfo, the field of an X.509 certificate that holds the public key and its algorithm [5][7][12]. HPKP used the same hash and sent it as pin-sha256 [1]. Apple's keys name the choice: NSPinnedLeafIdentities for the server's leaf certificate, NSPinnedCAIdentities for an authority or sub-authority, and when both are set, both must match [7].

Nothing in the SubjectPublicKeyInfo refers to the protocol version, and in our test the same pin passed over TLS 1.2 and over TLS 1.3 [12][15]. The names are explained in how SSL and TLS differ.

Why pinning breaks

Certificate pinning fixes what the server presents today, and that changes on someone else's schedule.

  • Certificate lifetimes are getting shorter. The Baseline Requirements cap publicly trusted TLS server certificates at 200 days for those issued from 2026-03-15, 100 days from 2027-03-15 and 47 days from 2029-03-15 [8]. A pin on the certificate itself breaks at each renewal.

  • Keys change unless someone keeps them. Not reusing private keys is the default behavior of Certbot, and --reuse-key turns reuse on [9]. Public key pinning then fails just as a certificate pin does [15].

  • The authority can change. Android names a move to another CA as the kind of server change that leaves pinned apps unable to connect [6]. With pins on the old authority's keys, the new chain still leads to a root in the trust store but contains none of them [5]; a pin on the server's own key survives the move only if the key is kept, as in our test [15]. An intermediate pin has the same weak point: the two test intermediates in our run had different pins [15].

What our test showed

We made a test root with two intermediates, a second root with its own intermediate and three server keys, served them with OpenSSL 3.0.13 and connected with curl 8.5.0, whose --pinnedpubkey option pins the server's public key [10][15].

What our test showed
What changed on the serverPin given to curlResult
Nothingthe server's keyHTTP 200
Certificate renewed with the same key, new SHA-256 fingerprintthe same pinHTTP 200
Certificate renewed with a new keythe old keyexit code 90, SSL: public key does not match pinned public key
The same, with -k, which skips certificate verificationthe old keyexit code 90
Server moved to the backup keyold key and backup key, separated by ;HTTP 200
Server moved to the backup keyold key onlyexit code 90
Certificate for the same key from the second root, both roots trustedthe server's keyHTTP 200
Certificate for another key from the second root, both roots trustedthe server's keyexit code 90; without the pin, HTTP 200
Connection limited to TLS 1.2, then to TLS 1.3the server's keyHTTP 200 both times

A key pin ties the app to the key, not to the authority: a certificate for the same key from another root passed, and one for another key failed although its root was trusted [15]. The backup pin was computed from the backup key file alone and matched the certificate later served with that key [15].

Computing a pin

The pin for a certificate in PEM form comes from this pipeline; curl accepted its output as sha256//<pin> [15]:

openssl x509 -in cert.pem -pubkey -noout | openssl pkey -pubin -outform der | openssl dgst -sha256 -binary | openssl base64

For a key file, openssl pkey -in key.pem -pubout -outform der replaces the first two steps [15].

Pinning in browsers: HPKP

RFC 7469 defined HTTP Public Key Pinning, a header with which a site told browsers to remember its keys for a period set by max-age [1]. A browser noted the pins only if at least one of them did not match the current chain, so every site had to publish a backup pin, for a key pair kept offline [1]. The RFC also describes hostile pinning: someone holding a valid certificate for the domain, such as a prior owner of the domain, could pin keys of their choice and keep visitors from the legitimate site, for as long as the maximum max-age allowed [1]. As one way to detect such misissued certificates, it points to certificate transparency logs [1].

Chrome removed HPKP in version 72, citing very low adoption and the risks of denial of service and hostile pinning [3]. Firefox turned it off by default: it no longer processes the header or cached HPKP pins, while pins preloaded into the browser stay enabled by default [4]. The RFC itself is still listed by the RFC Editor as a Proposed Standard and has not been obsoleted [2].

The HSTS security header, a different mechanism, tells browsers to reach a site only over HTTPS, and its directives are a lifetime and an optional includeSubDomains, with no keys [13].

When an app should pin, and how

Android does not recommend pinning for Android apps [6]. OWASP's cheat sheet, a secondary source here, goes further: do not pin if you do not control both the client and the server, or if you cannot update the pin set securely [11]. Android also describes the other way to narrow what an app accepts: limit the set of authorities it trusts instead of pinning [5].

Doing it with less risk

  1. Pin keys and keep backups. Android asks for multiple backup pins, including at least one key fully in your control [6]. RFC 7469 had the backup key pair kept offline, because whoever obtains that key can mount a man-in-the-middle attack without being discovered [1].

  2. Use the platform configuration. On Android, a <pin-set> of <pin digest="SHA-256"> entries in the Network Security Config [5]. On Apple platforms, NSPinnedDomains with leaf identities, CA identities or both; Apple suggests more than one leaf identity to allow for server-side changes [7].

  3. Set an expiration, knowing its cost. After the pin set's expiration date, Android stops pinning, which helps prevent connectivity issues in apps that have not been updated but can let attackers bypass the pins [5]. Android asks for a sufficiently short expiration period [6].

  4. Pin the next key before the server uses it. RFC 7469 stressed phasing in new keys gradually [1]. In our test the switch to the backup key passed only where its pin was already in the set [15].

Pinning and interception proxies

OWASP describes interception proxies, met in organizations that filter outgoing traffic, as breaking end-to-end security in a way that cannot be told apart from an attack. Its advice is to add the proxy's public key to the pin set only when those responsible for accepting risk say so [11]. In our test a certificate for another key failed the pin even though its root was trusted [15]. RFC 7469 allowed browsers to skip pin validation for chains that end at a user-defined trust anchor [1]. On Android, overridePins lets chosen authorities, such as debugging CAs, bypass the pins; it is off by default except inside debug-overrides [5].

A tool that does not pin but does not trust the proxy's root fails one step earlier, with unable to get local issuer certificate. Our curl run without the second root in its trust store returned exit code 60 with that message [15].

FAQ

What is certificate pinning?

A rule in an app that accepts a server only if its certificate chain contains a pinned key or certificate. A chain that is otherwise valid but has no matching pin is refused. [5]

Is certificate pinning still recommended?

Android does not recommend it for Android apps, and websites can no longer set pins in Chrome or Firefox: Chrome removed HPKP and Firefox turned it off by default. An app that does pin should use public-key pins, several backup pins and a short expiration. [6][3][4][5]

What is the difference between certificate pinning and public key pinning?

A certificate pin matches one certificate and breaks at every renewal. A public key pin matches the key inside it and survives renewals that keep the key, as it did in our test. [15]

Why did my app stop working after the SSL certificate was renewed?

If the app pins the old certificate or the old key, the renewed certificate does not match, and the app refuses it until an update brings the new pin. [5][15]

What was HPKP?

HTTP Public Key Pinning, defined in RFC 7469: a header that let websites pin keys in browsers and required a backup pin. Chrome removed it, citing very low adoption and the risks of denial of service and hostile pinning. [1][3]

Does certificate pinning stop man-in-the-middle attacks?

It stops attacks that rely on a certificate for another key, including one from an interception proxy whose root the device trusts, unless the app lets that root bypass its pins, as Android's overridePins can. It does not help if the attacker has a private key the app has pinned. [15][5][1]

Sources

  1. RFC 7469, Public Key Pinning Extension for HTTP: abstract, §2.1.1, §2.4, §2.5, §2.6, §4.1, §4.3, §4.5.

  2. RFC Editor, RFC 7469 record: status Proposed Standard, obsoleted by none.

  3. Chrome Platform Status, Remove HTTP-Based Public Key Pinning: status Removed, milestone 72.

  4. Mozilla Bugzilla, bug 1412438: preference security.cert_pinning.hpkp.enabled, false by default.

  5. Android Developers, Network security configuration: Pin certificates, trust-anchors, overridePins, debug-overrides.

  6. Android Developers, Security with network protocols: Restricting your app to specific certificates.

  7. Apple Developer Documentation: NSPinnedDomains, NSPinnedLeafIdentities, NSPinnedCAIdentities.

  8. CA/Browser Forum Baseline Requirements, §6.3.2, maximum validity periods.

  9. Certbot User Guide, command-line options: --reuse-key, --no-reuse-key.

  10. curl manual: --pinnedpubkey, --insecure, exit code 90.

  11. OWASP Pinning Cheat Sheet (secondary source).

  12. RFC 5280, X.509 profile: §4.1.2.7, subject public key info; §6.1, path validation.

  13. RFC 6797, HTTP Strict Transport Security: §6.1.

  14. OpenSSH manual page ssh(1).

  15. Test on this page: OpenSSL 3.0.13 s_server, curl 8.5.0; own test roots, intermediates and keys, localhost.