295

I am using Java keytool. I have exported a self-signed .pem certificate from my keystore. Is there a command to view the certificate details directly from the .pem file (not of the certificate in the keystore)?

1

4 Answers 4

545

An alternative to using keytool, you can use the command

openssl x509 -in certificate.pem -text

This should work for any x509 .pem file provided you have openssl installed.

7
  • 7
    Actually, keytool errored out with java.lang.Exception: Failed to parse input for some pems, but this worked for all of them
    – Csaba Toth
    Apr 13, 2018 at 18:23
  • 10
    In my case I had to change "x509" with "rsa" so I guess it depends on the .pem contents. I used file command to know that it was "rsa" and not "x509" (e.g. file xyz.pem).
    – MegaTux
    May 22, 2019 at 19:40
  • 11
    @megatux a PEM file can contain a few different types of data x509 is the format for certificates, rsa is the format for a public/private key pair.
    – alfwatt
    Jun 7, 2019 at 22:46
  • 8
    For shorter text-output try: openssl x509 -in certificate.pem -text -noout - This will omit the last ~ 40 lines of text from the output ( BEGIN CERTIFICATE ... END CERTIFICATE stuff)
    – knb
    Oct 22, 2020 at 12:28
  • 2
    to get only the subject: openssl x509 -noout -subject -in file.pem
    – user2053904
    Oct 23, 2020 at 7:23
254

Use the -printcert command like this:

keytool -printcert -file certificate.pem
4
  • 44
    I am getting the error java.lang.Exception: Failed to parse input
    – maxisme
    Jun 6, 2014 at 0:19
  • 15
    @Maximilian it may happen on APNS certificates, which combines private key & certificate into one .pem. Separate them into 2 files using text editor and the above command will work. (Hint: copy -- BEGIN CERTIFICATE -- line to -- END CERTIFICATE -- line to new file)
    – Raptor
    Jan 2, 2015 at 4:13
  • 3
    needs java (jdk or jre)
    – Pieter
    Nov 14, 2016 at 1:57
  • Check the name of your pem file.
    – tksilicon
    Feb 16, 2020 at 6:03
3

In Windows, no external tools needed, just powershell:

Import cert file to variable $cert

$fpath = "path-to-file"
$cert = New-Object Security.Cryptography.X509Certificates.X509Certificate2([string]$fpath)

To view all content of certificate, type

$cert | select *

Should work for other cert extensions as well.

2
  • Screenshot would be nice. Is it console or GUI output? May 11, 2023 at 16:38
  • @PaulVerest Console output. Jun 5, 2023 at 22:30
0

In Powershell it is sufficient to go to the folder with the crt creticifate. Type in the name of the certificate. Hit enter. Next you will see a dialog box with details of the certificate/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.