Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read common name from .pem file

is there a way to read the common name from a .pem file in my shell?

Thanks

like image 613
Heiko Avatar asked Aug 03 '12 10:08

Heiko


1 Answers

First off, the .pem extension only refers to the type of encoding used in the file.

The common name would be a feature of the Subject or Issuer of a certificate, and can be recognised by the lines

$ grep CERTIFICATE f.pem
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----

and lots of base64 encoded text in between.

If the .pem file contains an x509 certificate, this should do the trick:

openssl x509 -in cacert.pem -noout -text

This will dump the whole certificate. The openssl x509 command has several options to suppress the fields you don't want to see. You find those explained in the man page, under TEXT OPTIONS

You can also choose to get shown just the 'Subject' of the certificate:

openssl x509 -in cacert.pem -noout -subject

Example:

Let's capture the certificate of stackoverflow.com straight from the server

$ : | openssl s_client -connect stackoverflow.com:443 > f.pem 2>& 1 &&
      openssl x509 -in f.pem -noout -subject 2>& 1

Outputs:

Subject: CN = *.stackexchange.com
like image 134
Henk Langeveld Avatar answered Nov 10 '22 00:11

Henk Langeveld