HTTP Public Key Pinning (HPKP) is a security mechanism that allows a website to instruct web browsers to only accept specific cryptographic public keys when communicating with the server. This helps protect against man-in-the-middle attacks and certificate mis-issuance incidents. OpenSSL can be used to generate and manage the necessary public key pins for HPKP. Here's a step-by-step guide on how to use OpenSSL with HTTP Public Key Pinning:
-
Generate Public Key Pins:
Use OpenSSL to generate the SHA-256 hash of the public keys you want to pin. You can generate the hash of your server's certificate or the public key directly. Run the following command to get the hash:
# For Server Certificate openssl x509 -in server_certificate.pem -pubkey -noout | openssl rsa -pubin -outform der | openssl dgst -sha256 -binary | base64 # For Public Key openssl rsa -in public_key.pem -pubin -outform der | openssl dgst -sha256 -binary | base64
Replace
server_certificate.pem
andpublic_key.pem
with the paths to your server's certificate and public key, respectively. -
Configure HPKP Headers:
In your web server configuration or application code, set the HPKP headers in HTTP responses. The headers you need to set are:
Public-Key-Pins: pin-sha256="base64_encoded_public_key_hash"; max-age=expire_time; [includeSubDomains;] [report-uri="report_endpoint";]
-
base64_encoded_public_key_hash
: Replace this with the SHA-256 hash you generated in step 1, but without any whitespace or line breaks. -
expire_time
: Set the duration, in seconds, for how long the pins will be considered valid. After this time, the pins will be revalidated. -
includeSubDomains
(optional): If you want to include all subdomains of your website in HPKP, add this directive. -
report-uri
(optional): Specify the URL where browsers can send reports in case of pin validation failures.
-
-
Testing and Deployment:
Before deploying HPKP in a production environment, start with a short
max-age
value (e.g., 60 seconds) to test the configuration and ensure there are no issues. Once you are confident that HPKP is working correctly, gradually increase themax-age
value to the desired duration.Be cautious when deploying HPKP, as an incorrect configuration can lead to browsers blocking access to your website if the pins are not valid.
-
Monitoring and Maintenance:
Regularly monitor your server's certificates and public keys to ensure they remain valid and have not been compromised. If you change your server's public key or certificate, update the HPKP headers accordingly and deploy the changes.
-
Rolling Pins:
As HPKP relies on public keys, be mindful of key rotation. Before removing an old key, ensure the new key is in place and correctly pinned in the HPKP header to avoid any downtime.
It's important to note that HPKP has been deprecated by major browsers, including Chrome and Firefox, due to the potential risks and complexities associated with its deployment. Instead, Certificate Transparency (CT) and other Certificate Authority Authorization (CAA) mechanisms are recommended to improve certificate security and protect against certificate mis-issuance incidents.
If you are looking for consultation, fill the Contact Form below.
Appear weak when you are strong, and strong when you are weak.
Haluk YAMANER
Founder @ Future Software UAE
Founder @ Future Linux
Click here for more about me »