views:

503

answers:

1

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?

+2  A: 

It is a little tricky. First you can use keytool to put the private key into PKCS12 format, which is 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.

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.

GregS