This site is the archived OWASP Foundation Wiki and is no longer accepting Account Requests.
To view the new OWASP Foundation website, please visit https://owasp.org
Pinning Cheat Sheet
The Pinning Cheat Sheet is a technical guide to implementing certificate and public key pinning as discussed at the Virginia chapter's presentation Securing Wireless Channels in the Mobile Space. This guide is focused on providing clear, simple, actionable guidance for securing the channel in a hostile environment where actors could be malicious and the conference of trust a liability.
A verbose article is available at Certificate and Public Key Pinning. The article includes additional topics, Ephemeral Keys and Alternatives to Pinning.
- 1 What's the problem?
- 2 What Is Pinning?
- 3 How Do You Pin?
- 4 When Do You Pin?
- 5 When Do You Whitelist?
- 6 What Should Be Pinned?
- 7 Examples of Pinning
- 8 Pinning Gaps
- 9 References
- 10 Authors and Editors
- 11 Other Cheat sheets
What's the problem?
Users, developers, and applications expect end-to-end security on their secure channels, but some secure channels are not meeting the expectation. Specifically, channels built using well known protocols such as VPN, SSL, and TLS can be vulnerable to a number of attacks.
What Is Pinning?
In essence, pinning is the process of verifying a host's identity based on their expected X509 certificate or public key. Once a certificate or public key is known or seen for a host, the certificate or public key is 'pinned' to the host. Put another way, its a whitelist of known certificates or public keys for a host, server, or service. If more than one certificate or public key is acceptable, then the program holds a pinset for a host, server, or service. In this case, the peer's advertised identity must match one of the elements in the pinset.
A host or service's certificate or public key can be added to an application at development time, or it can be added upon first encountering the certificate or public key. The former - adding at development time - is preferred since preloading the certificate or public key out of band usually means the attacker cannot taint the pin.
How Do You Pin?
From 10,000 feet, the idea is to re-use the exiting protocols and infrastructure, but use them in a hardened manner. The 'pin' is the hardening.
For re-use, a program would keep doing the things it used to do when establishing a secure connection. To harden the channel, the program would would take advantage of the OnConnect callback offered by a library, framework or platform. In the callback, the program would verify the remote host's identity by validating its certificate or public key.
While pinning does not have to occur in an OnConnect callback, its often most convenient because the underlying connection information is readily available.
When Do You Pin?
You should pin anytime you want to be relatively certain of the remote host's identity or when operating in a hostile environment. Since one or both are almost always true, you should probably pin all the time.
When Do You Whitelist?
If you are working for an organization which practices "egress filtering" as part of a Data Loss Prevention (DLP) strategy, you will likely encounter Interception Proxies. I like to refer to these things as "good" bad guys (as opposed to "bad" bad guys) since both break end-to-end security and we can't tell them apart. In this case, do not offer to whitelist the interception proxy since it defeats your security goals. Add the interception proxy's public key to your pinset after being instructed to do so by the folks in Risk Acceptance.
What Should Be Pinned?
The first thing to decide is what should be pinned. For this choice, you have two options: you can (1) pin the certificate; or (2) pin the public key. If you choose public keys, you have two additional choices: (a) pin the subjectPublicKeyInfo; or (b) pin one of the concrete types such as RSAPublicKey or DSAPublicKey.
At runtime, you retrieve the website or server's certificate in the callback. Within the callback, you compare the retrieved certificate with the certificate embedded within the program. If the comparison fails, then fail the method or function.
There is a downside to pinning a certificate. If the site rotates its certificate on a regular basis, then your application would need to be updated regularly. For example, Google rotates its certificates, so you will need to update your application about once a month (if it depended on Google services). Even though Google rotates its certificates, the underlying public keys (within the certificate) remain static.
There are two downsides two public key pinning. First, its harder to work with keys (versus certificates) since you must extract the key from the certificate. Extraction is a minor inconvenience in Java and .Net, buts its uncomfortable in Cocoa/CocoaTouch and OpenSSL. Second, the key is static and may violate key rotation policies.
While the three choices above specifically callout the use of DER encoding, its also acceptable to use a hash of the information. In fact, the original sample programs were written using digested certificates and public keys. The samples were changed to allow a programmer to inspect the objects with tools like dumpasn1 and other ASN.1 decoders.
Hashing also provides three additional benefits. First, hashing allows you to anonymize a certificate or public key. This might be important if you application is concerned about leaking information during decompilation and re-engineering. Second, a digested certificate fingerprint is often available as a native API for many libraries, so its convenient to use.
Finally, an organization might want to supply a 'future' public key identity in case the primary identity is compromised. Hashing ensures your adversaries do not see the 'future' certificate or key in advance of its use. In fact, Google's IETF draft websec-key-pinning uses the technique.
Examples of Pinning
This section discusses certificate and public key pinning in Android Java, iOS, .Net, and OpenSSL. Code has been omitted for brevity, but the key points for the platform are highlighted.
All programs attempt to connect to random.org and fetch bytes (Dr. Mads Haahr participates in AOSP's pinning program, so the site should have a static key). Parameter validation, return value checking, and error checking have been omitted in the code below, but is present in the sample programs. So the sample code is ready for copy/paste. By far, the most uncomfortable languages are C-based: iOS and OpenSSL.
Pinning in Android is accomplished through a custom X509TrustManager. Be sure to call the base class to ensure customary checks are performed (thanks Nikolay Elenkov).
Download: Android sample program
iOS pinning is performed through a NSURLConnectionDelegate. The delegate must implement connection:canAuthenticateAgainstProtectionSpace: and connection:didReceiveAuthenticationChallenge:.
Download: iOS sample program.
.Net pinning can be achieved by using ServicePointManager.
Download: .Net sample program.
Pinning in OpenSSL is a lot of work, but the library offers the most control. Pinning can occur at one of two places. First is the OpenSSL SSL object pointer through use of the verify_callback. Second is after the connection is established via SSL_get_peer_certificate.
Download: OpenSSL sample program.
There are two gaps when pinning due to reuse of the existing infrastructure and protocols. First, an explicit challenge is not sent by the program to the peer server based on the server's public information. So the program never knows if the peer can actually decrypt messages. However, the shortcoming is usually academic in practice since an adversary will receive messages it can't decrypt.
Second is revocation. Clients don't usually engage in revocation checking, so it could be possible to use a known bad certificate or key in a pinset. Even if revocation is active, Certificate Revocation Lists (CRLs) and Online Certificate Status Protocol (OCSP) can be defeated in a hostile environment. An application can take steps to remediate, with the primary means being freshness. That is, an application should be updated and distributed immediately when a critical security parameter changes.
- OWASP Injection Theory
- OWASP Data Validation
- OWASP Transport Layer Protection Cheat Sheet
- IETF Public Key Pinning
- IETF RFC 5054 (SRP)
- IETF RFC 4764 (EAP-PSK)
- IETF RFC 1421 (PEM Encoding)
- IETF RFC 4648 (Base16, Base32, and Base64 Encodings)
- IETF RFC 3279 (PKI, X509 Algorithms and CRL Profiles)
- IETF RFC 4055 (PKI, X509 Additional Algorithms and CRL Profiles)
- IETF RFC 2246 (TLS 1.0)
- IETF RFC 4346 (TLS 1.1)
- IETF RFC 5246 (TLS 1.2)
- EFF Sovereign Keys
- Thoughtcrime Labs Convergence
- RSA Laboratories PKCS#1, RSA Encryption Standard
- RSA Laboratories PKCS#6, Extended-Certificate Syntax Standard
- ITU Specification of Basic Encoding Rules (BER), Canonical Encoding Rules (CER) and Distinguished Encoding Rules (DER)
- TOR Project Detecting Certificate Authority Compromises and Web Browser Collusion
- Code Project Cryptographic Interoperability: Keys
- Google I/O Security and Privacy in Android Apps
- Dr. Peter Gutmann's PKI is Broken
- Dr. Matthew Green's The Internet is Broken
- Dr. Matthew Green's How do Interception Proxies fail?
Authors and Editors
- Jeffrey Walton - jeffrey, owasp.org
- JohnSteven - john, owasp.org
- Jim Manico - jim, owasp.org
- Kevin Wall - kevin, owasp.org
Other Cheat sheets
OWASP Cheat Sheets Project Homepage