Blog | G5 Cyber Security

HTTP Public Key Pinning: A Security Guide

TL;DR

HTTP Public Key Pinning (HPKP) is a security mechanism that tells web browsers which public keys they should trust for your website. It helps prevent man-in-the-middle attacks by ensuring the browser only accepts certificates signed by those specific keys. While HPKP has been deprecated, understanding it provides valuable context for modern certificate pinning techniques.

What is HTTP Public Key Pinning?

Normally, browsers trust Certificate Authorities (CAs) to verify website identities. HPKP allowed you to add extra security by explicitly listing the public keys your site uses in a special header. If a rogue CA issued a certificate not matching those pinned keys, the browser would refuse to connect.

Why was HPKP deprecated?

HPKP proved difficult to implement correctly and had several drawbacks:

Modern alternatives like Certificate Transparency (CT) and HSTS with preloading offer better security and are easier to manage.

How did HPKP work?

  1. Generate a Pin Set: You needed the public keys of your website’s certificates. These were typically SHA-256 hashes of the public key itself.
  2. Add the Public-Key-Pins header: This header was sent with every HTTPS response from your server. It contained a list of these pins, along with directives like pin-allowlist and pin-sha256.
  3. Browser Validation: When a user visited your site, the browser checked if the certificate’s public key matched one of the pinned keys in the header. If there was no match, the connection was blocked.

Example Public-Key-Pins Header

Public-Key-Pins: pin-allowlist; pin-sha256="MkWADK5aolxvfEHP9cQOzw5+rlX8dmJXTsqjWmW7a0g="; includeSubDomains; report-uri /hpkp-report

Explanation:

Generating Pin Hashes

You can use OpenSSL to generate the SHA-256 hash of a public key:

openssl x509 -pubkey -noout -modulus -in your_certificate.pem | openssl sha256

Alternatives to HPKP

Conclusion

While HPKP is no longer recommended due to its drawbacks, understanding its principles can help you appreciate the importance of certificate validation and explore modern alternatives that offer better security and manageability.

Exit mobile version