brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: openssl can tell you if a private key belongs to a certificate

Openssl
~/tips/tip-openssl-key-matches-cert

openssl x509 -in site.pem -noout -pubkey | openssl sha256

Compare the public key inside a certificate with the one derived from a private key.

A web server that refuses to start with a message like key values mismatch usually got the wrong key file. Two commands with OpenSSL tell you which files go together.

Hash the public key from the certificate and the public key from the private key:

openssl x509 -in site.pem -noout -pubkey | openssl sha256
openssl pkey -in site.key -pubout | openssl sha256

If both hashes are the same, the key belongs to the certificate. If not, look for another key file. A CSR has the same public key too, so openssl req -in site.csr -noout -pubkey | openssl sha256 joins the comparison when you have a pile of files from an older renewal.

Older guides compare -modulus output instead. That only works for RSA keys. Comparing the whole public key works for RSA, EC and Ed25519 alike, which is why I prefer it.

The private key never leaves your machine here, and only its public half is printed and hashed, so the output is safe to paste in a ticket. Still, run it on the server itself and do not copy the key around to compare it somewhere else. If the key has a passphrase, pkey asks for it first.

Further reading: OpenSSL: openssl-pkey, OpenSSL: openssl-x509

Going deeper: Run degoog on Unraid as your own search page, compared with SearXNG and 4get

More Openssl tips openssl s_client -connect 203.0.113.10:443 -servername example.com </dev/null | openssl x509 -noout -subject openssl x509 -in cert.pem -days 365 -signkey key.pem -out cert-new.pem printf %s hello | openssl dgst -sha256 -hmac secret openssl pkcs12 -legacy -in old.pfx -nokeys echo 'aGVsbG8gd29ybGQ=' | openssl base64 -d All Openssl tips (34) All topics

Link