I ran into this problem the other day. My SSL Certificate expired for my dev domain. I went to Network Solutions, snagged a new Certificate using my old Certificate Signing Request (CSR), and tried to import it into my keystore.
In the past, I’ve always generated my private key and CSR, and then imported a new certificate along with the complete chain. Since the old expired certificate was still in the keystore, I thought it made sense to blow away the appropriate alias in the keystore with the following command.
What I didn’t realize is when you blow away your alias in the keystore, it not only blows away the certificate, but the private key as well. Oops.
Luckily, I still had my private key. I was hopeful that I could just import this private key into my keystore. Strike 2!
The Java keytool does not allow you to import an existing private key. Crap. I didn’t want to go back to Network Solutions to generate a new certificate. That’s a pain. There has to be a way to get a private key into my keystore.
Luckily, there is. You can’t import a private key directly into your keystore, but you can merge a private-cert pair into an existing keystore if the private-cert pair is in PKCS12 format. How’s that for fun?
To start, you need to use openssl to generate a PKCS12 file from your pem file. The pem file needs to be a combination of first your private key followed by your crt file for the domain.
Next you need to run openssl with the -in parameter to generate the PKCS12 fle.
Now, with the PKCS12 file, you need to do a keystore merge with your existing keystore.
After doing the merge, when you look into your keystore, you will see that this entry has been given an alias of 1.
You will want to change this alias to something usable,
Now, you will also want to import other portions of the certificate chain.
Now, when I start up my tomcat instance referencing my tomcat_ssl alias, I’m back in business!