In the digital age, ensuring secure communication between a user’s browser and a website is critical. Certificate pinning is a security measure that helps safeguard against certain types of cyber threats, particularly man-in-the-middle (MitM) attacks.
What is Certificate Pinning?
Certificate pinning, also known as “SSL/TLS pinning,” is a technique used to enhance the security of a server and client connection against impersonation by attackers. It involves hard-coding the certificate or public key of a trusted server into an application or web service. This way, the application can independently verify the server’s identity without relying solely on the infrastructure of the Certificate Authorities (CAs).
How Certificate Pinning Works
To understand certificate pinning, you first need to look at how certificates are typically verified:
-
Certificate Authorities: When you visit a secure website (https), the server presents a certificate issued by a CA. The browser checks this certificate against a list of trusted CAs. If the issuing CA is on this list, and the certificate is valid, the connection proceeds.
-
Public Key Infrastructure: The certificate includes a public key, which corresponds to a private key held by the server. This key pair is used to encrypt and decrypt data sent between the client and the server, ensuring that the data remains confidential and tamper-proof.
-
Pinning Process: In certificate pinning, the application developer embeds the expected server’s certificate or public key directly into the application. When the application connects to the server, it compares the server’s certificate to the one that has been embedded. If they match, the connection is considered secure. If not, the connection is terminated.
Benefits of Certificate Pinning
-
Enhanced Security: By pinning a certificate, an application can prevent attackers from successfully posing as a legitimate server, even if they have managed to obtain a certificate from a less scrupulous CA.
-
Mitigation of MitM Attacks: Certificate pinning makes it harder for attackers to intercept or tamper with data, as the client expects a specific certificate or public key.
-
Increased Trust: Users can feel more secure knowing that the application is taking extra steps to ensure communications are secure and trusted.
Drawbacks of Certificate Pinning
-
Maintenance Overhead: Certificates have expiration dates and may be rotated for security reasons. Each time a certificate is updated, the application needs to be updated as well to reflect this change.
-
Complexity in Implementation: Implementing certificate pinning requires a deep understanding of how certificates work, which can add complexity to development.
-
Potential for Blocking Users: If the pinning implementation is not handled correctly, there could be scenarios where users are blocked from accessing the service because the pinned certificate is outdated or incorrect.
Client-Side vs. Server-Side Certificate Pinning
-
Client-Side Certificate Pinning: This is typically used in mobile apps or desktop applications where the application itself verifies the server’s certificate against a known good copy (the pinned certificate). This is useful for protecting against attacks where an attacker might intercept or manipulate the communication between the client and the server.
-
Server-Side Certificate Pinning: This is less common but involves the server pinning the certificate of another server it communicates with. For example, if your server needs to securely connect to an API, it can pin the API’s certificate to ensure it’s talking to the correct server.
Best Practices for Implementing Certificate Pinning
-
Use Backup Pins: Always include one or more backup pins in the application. This ensures that if the primary certificate needs to be replaced, the application can still verify the server’s identity using the backup.
-
Regular Updates: Keep the application updated with the latest certificates. This includes monitoring the validity of the current certificates and preparing updates in advance of certificate expirations.
-
Graceful Failure Mechanisms: Implement mechanisms that allow the application to fail gracefully if the pinned certificate does not match. This could include alerting the user or providing steps to resolve the issue.
-
Dynamic Pinning: Consider implementing dynamic pinning mechanisms where the application can fetch and update pins without needing a full update. This approach must be secured to prevent attackers from injecting malicious pins.
Certificate pinning is a powerful tool in the arsenal of web security techniques. While it does introduce additional complexity and maintenance requirements, the security benefits it provides can be substantial. By understanding and implementing certificate pinning correctly, developers can protect their applications from sophisticated attacks and ensure their users’ data remains secure.
Certificate Pinning example: Implementing on WordPress with Nginx
In the context of enhancing web security, implementing certificate pinning on a WordPress site running on an Nginx server can be a strategic move. Below, we’ll outline a detailed procedure to implement certificate pinning, specifically focusing on the server configuration to ensure secure HTTPS connections. This guide will include code snippets, examples, and best practices.
Pre-requisites
-
A running WordPress site on Nginx.
-
An SSL/TLS certificate, such as one from Let’s Encrypt.
-
Access to the server with sufficient privileges to modify Nginx configurations.
Step 1: Extract the Public Key
First, you need to extract the public key from your SSL certificate. This key will be used to create a pin.
-
Locate Your SSL Certificate: Typically, for Let’s Encrypt, this would be in
/etc/letsencrypt/live/yourdomain.com/fullchain.pem. -
Extract the Public Key:
openssl x509 -in /etc/letsencrypt/live/yourdomain.com/fullchain.pem -pubkey -noout > publickey.pemStep 2: Generate the Pin
Convert the public key to a SHA-256 hash to use as the pin.
openssl pkey -pubin -in publickey.pem -outform der | openssl dgst -sha256 -binary | openssl enc -base64(Use openssl pkey rather than openssl rsa here, rsa chokes on ECDSA keys, and Let’s Encrypt hands out ECDSA certs by default these days. pkey handles both.)
This command outputs a base64-encoded SHA-256 hash of your public key. Note this hash as it will be used in the Nginx configuration.
Step 3: Configure Nginx for Certificate Pinning
Do not deploy this on a production site. Browsers ignore the HPKP header today (see Cautions below), and a stale pin on a browser that still honored it would lock visitors out for the whole max-age. The steps below are kept as a historical walkthrough. Modify your Nginx configuration to include the HTTP Public Key Pinning (HPKP) header. This step involves adding a specific line to your server block in the Nginx configuration file.
-
Edit Nginx Configuration: Open your site’s configuration file, usually located at
/etc/nginx/sites-available/yourdomain.com. -
Add HPKP Header: Inside the server block, add the following line:
add_header Public-Key-Pins 'pin-sha256="BASE64_ENCODED_PIN"; max-age=2592000; includeSubDomains';Replace BASE64_ENCODED_PIN with the hash you generated earlier.
- Reload Nginx: Apply the changes by reloading Nginx:
sudo systemctl reload nginxCautions and Recommendations
-
Risk of HPKP: HPKP is dead. Chrome deprecated it in version 67 (2018) and removed it in 72, Firefox removed it in 78, and no major browser honors the
Public-Key-Pinsheader anymore, largely because of the very real risk of locking yourself (and your users) out of your own site if a pin went stale. Treat this section as historical context, not a recommendation. -
Use of Certificate Transparency: Instead of HPKP, consider using Certificate Transparency logs to monitor and audit certificates in real-time, which provides a safer alternative to detect misissued or malicious certificates.
-
Backup Pins: The HPKP spec (RFC 7469) requires at least one backup pin in the header, and the single-pin example above would have been invalid. Include one. This should be the pin of another trusted certificate that you control, which can be used in case the primary certificate needs to be replaced unexpectedly.
-
Testing: Before implementing on a live site, test the configuration in a staging environment to ensure that it does not disrupt normal operations.
-
Monitoring and Updating: Regularly monitor your SSL/TLS configuration and update your pinning information before certificate renewals to avoid service interruptions.
Conclusion
While certificate pinning can enhance the security of a website by ensuring that browsers and applications are connecting to the correct server, it comes with high risks and maintenance overhead. Modern web security practices recommend using alternatives like Certificate Transparency and diligent CA management to achieve similar levels of security without the associated risks of HPKP. Always consider these factors when deciding on implementing certificate pinning in your web infrastructure.
What to Use Instead on Public Sites
Browsers ignore pinning headers now, so public sites get their protection from two other places.
CAA DNS records tell certificate authorities which of them may issue for your domain:
example.com. CAA 0 issue "letsencrypt.org"example.com. CAA 0 issuewild "letsencrypt.org"example.com. CAA 0 iodef "mailto:security@example.com"Check them with dig CAA example.com. CAs must consult CAA before issuing. A rogue CA can ignore the record, but the violation shows up in Certificate Transparency logs.
Certificate Transparency monitoring catches certificates you never requested. Every publicly trusted certificate lands in a public log, so you can watch for surprises:
curl -s "https://crt.sh/?q=%25.example.com&output=json" | \ python3 -c 'import json,sys; [print(c["name_value"]) for c in json.load(sys.stdin)]'For alerts instead of manual lookups, run certspotter and let it email you when a new certificate appears for your domain.
Pinning in Your Own Client Code
Pinning still works when you control both ends: mobile apps talking to your backend, internal API clients, and high-security financial or medical apps. Pin the public key (SPKI hash) instead of the whole certificate, so you can renew the certificate without shipping a new client. Pin your internal CA rather than individual leaf certificates, and always ship a backup pin. Skip pinning for public websites entirely.
Common Questions
How do I generate a certificate pin with OpenSSL?
Extract the public key from your certificate with openssl x509 -pubkey -noout, then hash it. Pipe the key through openssl pkey -pubin -outform der, openssl dgst -sha256 -binary, and openssl enc -base64. The base64 output is your pin. Use pkey instead of rsa, because rsa fails on ECDSA keys.
Does certificate pinning still work in browsers?
No, browsers no longer honor pinning through the Public-Key-Pins header. Chrome deprecated HPKP in version 67 and removed it in 72. Firefox removed it in 78. Pinning inside your own app still works, because the application compares the server certificate to an embedded copy.
What happens when the server certificate does not match the pin?
The application terminates the connection. Pinning compares the server’s certificate or public key to the copy embedded in the application, and a mismatch means the server is not trusted. A stale pin after certificate rotation blocks legitimate users too, so backup pins and timely app updates matter.
Can a server pin another server’s certificate?
Yes. Server-side pinning is less common than client-side pinning, but a server can pin the certificate of another server it talks to. For example, your server can pin an API’s certificate, so it only connects to the correct API server and not an impostor.