toolhq.io

All posts
6 min readby Jameel Haider

Does my private key match my SSL certificate? How to check

You install a new certificate, restart the server, and it refuses to start with a key values mismatch error. It means the certificate and the private key you paired it with do not belong together. Understanding why makes the fix quick.

Why they have to match

A certificate contains a public key. Somewhere on your server sits the matching private key. The two are a mathematical pair: data encrypted to the public key can only be unlocked by its private partner, and the TLS handshake relies on the server proving it holds that private key. If the certificate's public key and your private key are not partners, the server cannot complete the handshake, so it refuses to serve at all.

This happens most often when a certificate is reissued or renewed and the wrong key file is left in place, or when several domains' keys live in the same directory and the paths get crossed.

What the error looks like

Each server words it differently, which is why it is worth recognizing them:

  • nginx: SSL_CTX_use_PrivateKey("...") failed ... key values mismatch
  • Apache: Unable to configure RSA server private key ... certificate and private key do not match
  • HAProxy and others: a load error naming the certificate and key together

All three mean the same thing, and none of them is fixed by regenerating the certificate; they are fixed by pairing it with the correct key.

The check that proves a match

For RSA keys, both the certificate and the private key embed the same modulus, a very large shared number. If the moduli are identical, the pair matches. The classic command-line check hashes the modulus from each and compares:

openssl x509 -noout -modulus -in cert.pem   | openssl md5
openssl rsa  -noout -modulus -in key.pem     | openssl md5
openssl req  -noout -modulus -in request.csr | openssl md5

All three hashes should be identical. The CSR line is optional but useful: if the certificate matches the CSR but not the key on the server, you know the certificate was issued from a different key than the one deployed. For elliptic-curve keys there is no modulus, so the same idea applies to the public point instead: derive the public key from both the certificate and the private key (openssl pkey -pubout) and confirm they are byte-for-byte equal.

If they genuinely do not match

Sometimes the check confirms the worst: the key that generated the certificate is lost. There is no way to derive it, and no amount of converting formats will help. The only fix is to generate a new key and reissue the certificate from a fresh CSR. This is also the moment to treat the old key as compromised if you are unsure where it went, and to have the old certificate revoked.

Doing it without exposing the key

The catch is that pasting a private key into a random website is exactly the kind of mistake that compromises a server. The check does not require sending the key anywhere; it can be done entirely on your own machine. The Key Matcher runs fully in your browser and never transmits what you paste, deriving and comparing the public keys locally. Once the pair is confirmed, reading the certificate's own fields confirms it is the right cert for the right host, and if you also need to re-encode the key, see RSA key formats.