Certificate Pinning
A technique that associates a host with its expected cryptographic identity to prevent man-in-the-middle attacks using forged certificates.
Description
Certificate pinning is a security mechanism that binds a specific cryptographic identity (a certificate or public key) to a particular host, rejecting all other certificates even if they are signed by a trusted certificate authority (CA). This defends against sophisticated man-in-the-middle attacks where an attacker obtains a fraudulent but CA-signed certificate for the target domain -- whether through CA compromise, CA coercion by a state actor, or misuse of enterprise CA certificates installed on managed devices.
There are several approaches to certificate pinning: pinning the leaf certificate (most specific but requires updates on every certificate renewal), pinning the public key (survives certificate renewals as long as the key pair is reused), pinning an intermediate CA certificate (provides a balance between security and operational flexibility), and using backup pins to prevent lockouts during planned or emergency certificate rotations. The HTTP Public Key Pinning (HPKP) header was deprecated by browsers due to the risk of self-inflicted denial of service, but pinning remains important in mobile apps and server-to-server communication.
In practice, certificate pinning is most commonly implemented in mobile applications (iOS Trust Evaluation API, Android Network Security Configuration) and in server-to-server communication for critical API integrations. For web applications, Certificate Transparency (CT) logs and the Expect-CT header have largely replaced browser-side pinning. Server-side implementations in Node.js can use the checkServerIdentity option in TLS connections or the ca option in HTTPS agents to pin specific certificates.
Prompt Snippet
For mobile apps, implement certificate pinning using Android Network Security Configuration (pin-set with backup pins) and iOS URLSession delegate with SecTrustEvaluateWithError. Pin the public key hash (SPKI SHA-256) rather than the full certificate to survive renewals: openssl x509 -pubkey -noout -in cert.pem | openssl pkey -pubin -outform DER | openssl dgst -sha256 -binary | base64. For Node.js server-to-server communication, pin certificates via the https.Agent ca option or checkServerIdentity callback. Always include at least one backup pin for rotation. Monitor Certificate Transparency logs (crt.sh, Facebook CT monitor) for unauthorized certificate issuance for your domains.
Tags
Related Terms
Encryption in Transit (TLS)
Encrypting data as it moves between systems using TLS to prevent eavesdropping, tampering, and man-in-the-middle attacks.
Cryptographic Key Management
The practices and systems for generating, storing, distributing, rotating, and retiring cryptographic keys throughout their lifecycle.
HTTPS Enforcement (HSTS)
Mechanisms that ensure all communication occurs over encrypted HTTPS connections, preventing protocol downgrade attacks.