Skip to content
Go back

Certificate Pinning: A Secure Connection Guide

· Updated:
By KingPin 10 min read
Certificate Pinning: A Secure Connection Guide
Contents

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:

Benefits of Certificate Pinning

Drawbacks of Certificate Pinning

Client-Side vs. Server-Side Certificate Pinning

Best Practices for Implementing Certificate Pinning

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

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.

openssl x509 -in /etc/letsencrypt/live/yourdomain.com/fullchain.pem -pubkey -noout > publickey.pem

Step 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.

add_header Public-Key-Pins 'pin-sha256="BASE64_ENCODED_PIN"; max-age=2592000; includeSubDomains';

Replace BASE64_ENCODED_PIN with the hash you generated earlier.

sudo systemctl reload nginx

Cautions and Recommendations

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:

Terminal window
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.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
APT: Upgrade a Single Package, Not Everything
Next Post
Docker Networking Essential Guide for All Skill Levels

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts