Certificate and Private Key Verification
Preparation
Required files:
- Certificate file (
cert.crt,.cer,.pem,.der,.p7b) or a PKCS#12 bundle (.pfx,.p12). - Private key file (
key.key,.pem,.der), if not inside the bundle. - Plain text file with the key or bundle password (
pass.txt), only for encrypted material.
All commands use openssl pkey instead of openssl rsa, so they work for RSA, EC and Ed25519 keys alike. OpenSSL 1.1.1 or newer is required.
pass.txt at chmod 600 and remove it after the check.Identifying the Format
The file extension says nothing reliable. Check the first line instead:
head -1 cert.crt key.key| First line | Format | -inform |
|---|---|---|
-----BEGIN CERTIFICATE----- |
X.509 certificate, PEM | PEM (default) |
-----BEGIN PKCS7----- |
PKCS#7 bundle (.p7b), PEM |
PEM |
-----BEGIN PRIVATE KEY----- |
PKCS#8 key, unencrypted | PEM |
-----BEGIN ENCRYPTED PRIVATE KEY----- |
PKCS#8 key, encrypted | PEM + -passin |
-----BEGIN RSA PRIVATE KEY----- |
PKCS#1 RSA key (with Proc-Type: 4,ENCRYPTED = encrypted) |
PEM |
-----BEGIN EC PRIVATE KEY----- |
SEC1 EC key | PEM |
| binary data | DER certificate/key, PKCS#12 bundle or DER PKCS#7 | DER |
For a binary file, file cert.der or a trial command tells which it is: openssl x509 -inform DER -in cert.der -noout succeeds only for a DER certificate.
Inspecting the Certificate
# PEM certificate (subject, issuer, validity, SAN)
openssl x509 -in cert.crt -noout -subject -issuer -dates -ext subjectAltName
# DER certificate
openssl x509 -in cert.der -inform DER -noout -subject -issuer -dates -ext subjectAltName
# Expiry check: exit code 0 = valid for at least 30 more days, 1 = expires sooner or already expired
openssl x509 -in cert.crt -noout -checkend 2592000
# PKCS#7 bundle (add -inform DER for a binary .p7b)
openssl pkcs7 -in chain.p7b -print_certs -noout
# PKCS#12 bundle: list the certificates without exporting the key
openssl pkcs12 -in bundle.pfx -passin file:pass.txt -nokeys | openssl x509 -noout -subject -issuer -datesopenssl x509 reads only the first certificate in a file. A full-chain PEM file must have the leaf (server) certificate first, which is also what web servers expect.Checking the Private Key
-check validates the key’s internal consistency (for RSA: the primes and exponents match the modulus). It prints Key is valid on success.
# PEM key, unencrypted
openssl pkey -in key.key -check -noout
# PEM key, encrypted
openssl pkey -in key.key -passin file:pass.txt -check -noout
# DER key
openssl pkey -in key.der -inform DER -check -noout
# Key type and size, without printing the key
openssl pkey -in key.key -pubout | openssl pkey -pubin -noout -text | head -1Matching the Key to the Certificate
A private key contains everything needed to derive its public key. If the pair matches, the public key derived from the private key is identical to the one embedded in the certificate. Both sides are converted to DER and hashed, so the comparison does not depend on PEM line wrapping or the key algorithm.
# Both hashes must be identical
openssl x509 -in cert.crt -noout -pubkey | openssl pkey -pubin -outform DER | sha256sum
openssl pkey -in key.key -pubout -outform DER | sha256sumFor DER input, add -inform DER to the first command of each pipe; for an encrypted key, add -passin file:pass.txt.
Single line with a verdict:
[ "$(openssl x509 -in cert.crt -noout -pubkey | openssl pkey -pubin -outform DER | sha256sum)" = "$(openssl pkey -in key.key -pubout -outform DER | sha256sum)" ] && echo MATCH || echo MISMATCHKey and certificate inside a PKCS#12 bundle:
openssl pkcs12 -in bundle.pfx -passin file:pass.txt -nokeys -clcerts | openssl x509 -noout -pubkey | openssl pkey -pubin -outform DER | sha256sum
openssl pkcs12 -in bundle.pfx -passin file:pass.txt -nocerts -nodes | openssl pkey -pubout -outform DER | sha256sumunsupported algorithm). Add -legacy to the openssl pkcs12 commands for such bundles.Verifying the Certificate Against Its Chain
The pair can match while the certificate is still unusable because the chain is incomplete or wrong. chain.pem holds the intermediate and root CA certificates:
openssl verify -CAfile chain.pem cert.crtThe output must be cert.crt: OK. To check against the system trust store and pass only the intermediates, use openssl verify -untrusted intermediates.pem cert.crt.
Complete Check Script
The script accepts a certificate as PEM or DER X.509, or as a PKCS#7 bundle (PEM or DER, whatever the extension). It reads every certificate in the file, picks the one whose public key matches the key, prints its details and checks the key. Exit code 0 means a matching certificate was found.
#!/usr/bin/env bash
# Usage: ./check-pair.sh <cert> <key> [password-file]
set -euo pipefail
CERT=$1 KEY=$2
PASSIN=(); [ -n "${3:-}" ] && PASSIN=(-passin "file:$3")
KF=$(grep -q -- '-----BEGIN' "$KEY" && echo PEM || echo DER)
# Print all certificates from the file as PEM: X.509 or PKCS#7, PEM or DER
certs() {
local f out
# PKCS#7 first: OpenSSL 3 x509 would silently take only its first certificate
for f in PEM DER; do
out=$(openssl pkcs7 -inform $f -in "$1" -print_certs 2>/dev/null) && [[ $out == *BEGIN* ]] && { echo "$out"; return; }
done
for f in PEM DER; do openssl x509 -inform $f -in "$1" 2>/dev/null && return; done
echo "Unsupported certificate file: $1 (not X.509 or PKCS#7)" >&2; return 1
}
hash() { openssl pkey -pubin -outform DER | sha256sum | cut -d' ' -f1; }
echo "== Private key ($KF)"
openssl pkey -in "$KEY" -inform "$KF" "${PASSIN[@]}" -check -noout
KH=$(openssl pkey -in "$KEY" -inform "$KF" "${PASSIN[@]}" -pubout | hash)
ALL=$(certs "$CERT"); N=0; MATCH=""; BLK=""
while IFS= read -r line; do
line=${line%$'\r'}
BLK+="$line"$'\n'
if [[ $line == "-----END CERTIFICATE-----" ]]; then
N=$((N + 1))
[ "$(openssl x509 -noout -pubkey <<<"$BLK" | hash)" = "$KH" ] && MATCH=$BLK
BLK=""
fi
done <<<"$ALL"
echo "== Certificate file: $N certificate(s)"
[ -n "$MATCH" ] || { echo "Key pubkey SHA256: $KH"; echo "Result: MISMATCH (no certificate in the file matches the key)"; exit 1; }
openssl x509 -noout -subject -issuer -dates -ext subjectAltName <<<"$MATCH"
openssl x509 -noout -checkend 2592000 <<<"$MATCH" >/dev/null \
&& echo "Expiry: OK (more than 30 days left)" || echo "Expiry: WARNING (expires within 30 days or expired)"
echo "Pubkey SHA256: $KH"
echo "Result: MATCH".pfx/.p12 files.Troubleshooting: “Could not find certificate”
Could not find certificate from cert.cer
...DECODER routines:OSSL_DECODER_from_bio:unsupported...No supported data to decode. Input type: DER, Input structure: CertificateOpenSSL found no valid -----BEGIN header and the content is not DER either. A UTF-8 BOM, CRLF line endings and PKCS#7 bundles are all read fine by OpenSSL 3, so the usual causes are:
| Cause | How it happens | file cert.cer shows |
|---|---|---|
| PEM read as DER | -inform DER given for a file starting with -----BEGIN CERTIFICATE-----; drop the flag |
PEM certificate |
| UTF-16 encoding | Saved by Notepad or redirected with > in Windows PowerShell 5.1 |
Unicode text, UTF-16, little-endian |
| Damaged or missing header | Partial copy-paste, missing dashes, extra characters | ASCII text |
| Empty or truncated file | Failed transfer | empty / data |
| Wrong file | A private key or CSR saved as .cer |
... private key, PEM certificate request |
# What is it?
file cert.cer; head -2 cert.cer | od -c | head -5
# Same file as a working copy?
sha256sum cert.cer /path/to/working/cert.cer
# Fix UTF-16
iconv -f UTF-16 -t UTF-8 cert.cer > cert.pemCould not open file or uri for loading certificate ...: No such file or directory or Permission denied is a different error: a wrong path or missing read rights, not a format problem.