Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I know if a .pem file contains public or private key?

I have a .pem file (base64-encoded cryptography information). What OpenSSL command-line should I use to detect whether it contains a public key or a private key?

As explained in this StackOverflow question, a .pem can contain both.

Since the contents of the file are juste garbled base64, when sending a .pem certificate to some other machine, I would like to make sure I'm exporting the public key and not giving out my private key.

like image 525
Suzanne Soy Avatar asked Feb 17 '15 19:02

Suzanne Soy


People also ask

How do I know if my PEM has private key?

In the Certificate windows that appears, you should see a note with a key symbol underneath the Valid from field that says, "You have a private key that corresponds to this certificate." If you do not see this, then your private key is not attached to this certificate, indicating a certificate installation issue.


1 Answers

As a practical approximation that should catch most (all?) human mistakes I would recommend just file and grep:

(file $KEYFILE | grep -i private >/dev/null) && echo Private || echo Public-or-unknown

To screen some key material before it goes out:

 for f in *; do (file $f | grep -i private >/dev/null) && file $f; done

Empty output = hopefully no secrets, can proceed.

like image 138
Dawid Toton Avatar answered Sep 22 '22 09:09

Dawid Toton