One of the most frequent causes of web server startup failures—such as Nginx's notorious SSL_CTX_use_PrivateKey_file() failed (SSL: error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch)—is attempting to load a private key that does not belong to the issued certificate.
1. The Mathematics of Key Matching
Asymmetric cryptography is predicated on mathematical pairs. The method for verifying that a private key, CSR, and certificate correspond depends on the underlying public-key algorithm:
RSA Key Matching
An RSA key pair consists of a public key $(n, e)$ and a private key $(n, d)$, where $n = p \times q$ is the modulus. Notice that the modulus $n$ is identical in both the public key (embedded in the CSR and certificate) and the private key.
Therefore, to verify that an RSA certificate, private key, and CSR belong together, it is mathematically sufficient to compute the cryptographic hash (such as SHA-256 or MD5) of their modulus $n$. If the hashes match, the triplet is guaranteed to belong to the same cryptographic key pair.
ECDSA (Elliptic Curve) Key Matching
In Elliptic Curve Cryptography (e.g., NIST P-256, P-384, or SECP256K1), there is no composite modulus $n$. Instead, the private key is a secret scalar $d$, and the public key is a point on the elliptic curve $Q = d \times G$, where $G$ is the standard curve base generator.
For ECDSA, matching requires extracting the Subject Public Key Info (SPKI) point coordinates $(x, y)$ from the private key and verifying that they are identical to the public key point encoded in the CSR and certificate.
2. Manual Verification Using OpenSSL
You can test your files locally using standard OpenSSL commands. Notice that we pipe the extracted modulus into a SHA-256 hash function for instant visual comparison:
# 1. Compute the SHA-256 hash of the Private Key modulus:
openssl rsa -noout -modulus -in server.key | openssl sha256
# 2. Compute the SHA-256 hash of the Certificate modulus:
openssl x509 -noout -modulus -in server.crt | openssl sha256
# 3. Compute the SHA-256 hash of the CSR modulus:
openssl req -noout -modulus -in server.csr | openssl sha256
(stdin)= a3f92b7c4d5e8910111213141516171819202122232425262728293031323334
(stdin)= a3f92b7c4d5e8910111213141516171819202122232425262728293031323334
(stdin)= a3f92b7c4d5e8910111213141516171819202122232425262728293031323334
If all three output hashes are character-for-character identical, your private key, certificate, and CSR match perfectly.
3. Verifying the CSR Signature
A Certificate Signing Request (CSR) is not merely a public key wrapper; it is also a self-signed proof of possession (PoP). When generating a CSR, the client signs the request using the private key. This proves to the Certificate Authority that the requester owns the corresponding private key without disclosing it.
You can verify that a CSR's internal signature is cryptographically valid with:
openssl req -in server.csr -noout -verify
If OpenSSL prints verify OK, the CSR's internal signature was verified using the embedded public key.
4. Common Pitfalls during Certificate Renewal
- Overwriting the private key: When generating a new CSR for renewal, engineers often overwrite
server.keywith a new key pair. If the CA hasn't issued the certificate yet, the live web server will crash on reload because the old certificate no longer matches the new key. - Wrong file extensions: Some automation scripts save the certificate chain as
.crtwhile the private key is also saved as.crtor.pem, leading to confusion during deployment. - Mismatched Passphrases: If an encrypted private key has a passphrase, web servers will pause on startup awaiting terminal input unless an unencrypted key or secret management system is configured.
Put This Guide into Practice
Test your certificates, keys, or tokens in-memory with our zero-disk developer tool.