HTTP Public Key Pinning (HPKP) was a security feature that used to tell a web client to associate a specific cryptographic public key with a certain web server to decrease the risk of MITM attacks with forged certificates. To prevent CA compromise this feature is introduced.
- Public key pinning allows trusted certificates to be whitelisted.
- It instructs the browser how to behave in the future.
- Protects againts the use of fraudulent certs.
UNDERSTANDING HPKP
- The below is the response header model of HTTP public key pinning.
HEADER :
Public-Key-Pins: pin-sha256=[pin 1]; pin-sha256=[pin 2]; max-age=2592000; report-uri=[uri]; includeSubdomains
- HPKP relies on “ Trust On First Use ( TOFU ) “

NOTE : Internet explorer doesnot have HPKP feature on it . To show it in demo a rogue certificate has been uploaded on a site and it is simultaneously opened in IE as well as in chrome. The chrome flags it insecure but IE allows the site wihout any warning. This is the amount of risk we will face on the absence of HPKP because Man-in-the-middle attack can now occur on the rogue certificate issued site and all the user communication will be decrypted & obtained by the attacker. Check the below screenshot for your reference ( LEFT : IE Browser and RIGHT : Chrome Browser ).


THE PIN-SHA256 KEYWORD
- HPKP declares multiple ( at least 2 ) hashes of the certificate thumbprint.
- The thumbprint can be from any public certificate in the certificate chain.
- It can also be the thumbprint of the certificate signing request ( CSR )
The HPKP hashes are mostly generated using certificate file and the CSR file.So to check whether the HPKP hashes are not modified , you need to run the below commands and compare the output hashes with the HPKP header hashes in browser developer mode as shown below. If the hashes are same then there is not forgery with the certificate but if it is different then you are in big trouble and your site must have already compromised.