Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export private key from a keystore of self-signed certificate

Tags:

I just created a self-signed certificate on a linux box running tomcat 6.

I created the keys like this, valid for 10 years:

keytool -genkey -alias tomcatorange -keyalg RSA -validity 3650 

and copied the keystore into a folder in tomcat, and updated server.xml to point at the keystore.

Now my network admin is asking for the both the public and private key ( for our load balancer)

I can generate the public key using:

openssl s_client -connect mydomain.com:8443 

But how can I export/retrieve the private key?

like image 378
jeph perro Avatar asked Apr 14 '10 20:04

jeph perro


People also ask

How can I get private key of self-signed certificate?

On Windows servers, the OS manages your certificate files for you in a hidden folder, but you can retrieve the private key by exporting a “. pfx” file that contains the certificate(s) and private key. Open Microsoft Management Console (MMC). In the Console Root expand Certificates (Local Computer).

How do I export my private key?

Go to: Certificates > Personal > Certificates. Right-click on the certificate you wish to export and go to All Tasks and hit Export. Hit Next on the Certificate Export Wizard to begin the process. Select “Yes, export the private key” and hit next.


2 Answers

It is a little tricky. First you can use keytool to put the private key into PKCS12 format, which is more portable/compatible than Java's various keystore formats. Here is an example taking a private key with alias 'mykey' in a Java keystore and copying it into a PKCS12 file named myp12file.p12. [note that on most screens this command extends beyond the right side of the box: you need to scroll right to see it all]

keytool -v -importkeystore -srckeystore .keystore -srcalias mykey -destkeystore myp12file.p12 -deststoretype PKCS12 Enter destination keystore password:   Re-enter new password:  Enter source keystore password:   [Storing myp12file.p12] 

Now the file myp12file.p12 contains the private key in PKCS12 format which may be used directly by many software packages or further processed using the openssl pkcs12 command. For example,

openssl pkcs12 -in myp12file.p12 -nocerts -nodes Enter Import Password: MAC verified OK Bag Attributes     friendlyName: mykey     localKeyID: 54 69 6D 65 20 31 32 37 31 32 37 38 35 37 36 32 35 37  Key Attributes: <No Attributes> -----BEGIN RSA PRIVATE KEY----- MIIC... . . . -----END RSA PRIVATE KEY----- 

Prints out the private key unencrypted.

Note that this is a private key, and you are responsible for appreciating the security implications of removing it from your Java keystore and moving it around.

like image 52
President James K. Polk Avatar answered Oct 02 '22 07:10

President James K. Polk


Use Keystore Explorer gui - http://keystore-explorer.sourceforge.net/ - allows you to extract the private key from a .jks in various formats.

like image 25
user2984392 Avatar answered Oct 02 '22 07:10

user2984392