How to validate each component of a certificate PEM file, assuming that:
Use command below to output certificate information useful for validate certificate components, either on a certificate chain or single certificate files.
awk -F'\n' '
BEGIN { showcert = "openssl x509 -noout -dates -fingerprint -issuer -subject -ext subjectKeyIdentifier,authorityKeyIdentifier,basicConstraints" }
/-----BEGIN CERTIFICATE-----/ {printf "\nCertificate %d: \n", i}
{printf $0"\n" | showcert}
/-----END CERTIFICATE-----/ {close(showcert) i++}' <certificate_file>
Replace <certificate_file>
with the name of the certificate chain or single certificate file, without the <> brackets. Ensure sure that you copy all the content in the above box, this is one single command rather than several lines of commands.
The following example was created based on a certificate chain that contained the following certificates/key, in the following order:
root@localhost [ ~/ca ]# awk -F'\n' '
BEGIN { showcert = "openssl x509 -noout -dates -fingerprint -issuer -subject -ext subjectKeyIdentifier,authorityKeyIdentifier,basicConstraints" }
/-----BEGIN CERTIFICATE-----/ {printf "\nCertificate %d: \n", i}
{printf $0"\n" | showcert}
/-----END CERTIFICATE-----/ {close(showcert) i++}' test.pem
Certificate 0:
notBefore=Jun 3 15:57:15 2024 GMT
notAfter=Jun 13 15:57:15 2025 GMT
SHA1 Fingerprint=DC:C1:CA:5C:09:CC:05:3F:AF:47:67:29:E9:5A:DD:FF:30:A7:4D:04
issuer=C = IE, ST = Cork, O = VMware, OU = CMBU, CN = Root Sandbox Intermediate CA, emailAddress = [email protected]
subject=C = IE, ST = Cork, L = Cork, O = VMware, OU = CMBU, CN = web.example.com, emailAddress = [email protected]
X509v3 Basic Constraints:
CA:FALSE
X509v3 Subject Key Identifier:
74:C3:28:E6:26:4D:34:03:DD:CD:38:3B:19:64:0F:89:C9:51:FE:F5
X509v3 Authority Key Identifier:
keyid:1F:16:56:79:13:F6:7F:4A:9F:E9:E8:3B:46:C8:42:A2:6F:54:30:AD
DirName:/CN=Repro Sandbox CA/C=IE/ST=Cork/L=Cork/O=VMware/OU=CMBU/[email protected]
serial:10:00
Certificate 1:
notBefore=Jun 3 15:47:05 2024 GMT
notAfter=Jun 1 15:47:05 2034 GMT
SHA1 Fingerprint=AE:9D:8E:E9:AB:FA:DD:6F:65:B4:4A:3D:39:86:1A:43:1A:5B:95:C8
issuer=CN = Repro Sandbox CA, C = IE, ST = Cork, L = Cork, O = VMware, OU = CMBU, emailAddress = [email protected]
subject=C = IE, ST = Cork, O = VMware, OU = CMBU, CN = Root Sandbox Intermediate CA, emailAddress = [email protected]
X509v3 Subject Key Identifier:
1F:16:56:79:13:F6:7F:4A:9F:E9:E8:3B:46:C8:42:A2:6F:54:30:AD
X509v3 Authority Key Identifier:
5C:97:A2:91:56:6C:0B:37:1A:41:30:82:6C:27:FC:49:A2:84:24:0F
X509v3 Basic Constraints: critical
CA:TRUE, pathlen:0
Certificate 2:
notBefore=Jun 3 15:42:35 2024 GMT
notAfter=May 29 15:42:35 2044 GMT
SHA1 Fingerprint=99:41:FB:3B:BA:3B:BE:88:6E:8A:C3:1C:DF:16:97:12:38:41:F1:9C
issuer=CN = Repro Sandbox CA, C = IE, ST = Cork, L = Cork, O = VMware, OU = CMBU, emailAddress = [email protected]
subject=CN = Repro Sandbox CA, C = IE, ST = Cork, L = Cork, O = VMware, OU = CMBU, emailAddress = [email protected]
X509v3 Subject Key Identifier:
5C:97:A2:91:56:6C:0B:37:1A:41:30:82:6C:27:FC:49:A2:84:24:0F
X509v3 Authority Key Identifier:
5C:97:A2:91:56:6C:0B:37:1A:41:30:82:6C:27:FC:49:A2:84:24:0F
X509v3 Basic Constraints: critical
CA:TRUE
How to interpret the output from the example:
Root certificate (certificate 2)
This is a clear indication that this certificate is a root certificate. When verifying a root certificate look at the subjectKeyIdentifier and authorityKeyIdentifier. If these do not have the same value, that means that this is not a root certificate.
Intermediate certificate (certificate 1)
This indicates that the certificate is an intermediate certificate. It's important to note that for intermediate certificate, the following is expected:
Those two alone will indicate whether this certificate is a root or intermediate certificate, depending on whether subjectKeyIdentifier and authorityKeyIdentifier are identical or not.
Server/web/endpoint certificate (certificate 0)
As Basic Constraints is set to CA:FALSE, this indicates that this is a server/web/endpoint certificate, we also see that authorityKeyIdentifier for certificate 0 matches subjectKeyIdentifier for intermediate certificate (certificate 1).
Key rules for checking certificate chain:
To validate that the key and server certificate matches use these two commands:
openssl x509 –noout –modulus –in <certificate_file> | openssl md5
openssl rsa –noout –modulus –in <key_file> | openssl md5
Note that it is easiest to validate these prior to assembling a PEM file with certificate chain that includes the private key.
If the PEM chain only contains a single key, use the following command to extract the key to a single file:
openssl storeutl -keys <certificate_file> > extracted.key
Replace <certificate_file>
with the name of the certificate chain, without the <> brackets.
After extracting the key from the chain, or use key file used when assembling PEM chain, run the two commands above against the chain and the extracted key
root@localhost [ ~/ca ]# openssl x509 –noout –modulus –in extracted.key | openssl md5
x509: Use -help for summary.
MD5(stdin)= d41d8cd98f00b204e9800998ecf8427e
root@localhost [ ~/ca ]# openssl x509 –noout –modulus –in test.pem | openssl md5
x509: Use -help for summary.
MD5(stdin)= d41d8cd98f00b204e9800998ecf8427e
Notice that the MD5 sum for key and certificate matches, which means that the key used with the server/web/endpoint certificate is correct. If these do not match, the incorrect key has been used.
-----BEGIN CERTIFICATE-----
<Base64 encoded certificate>
-----END CERTIFICATE-----