Overview
- 1. Split your .pem file so you have a different file for the certificate, PK, Root certificate and intermidiate certificates
- 2. Import Root and intermidiate certificates to your keystore:
- 3. Create a .p12 keystore containing your leaf certificate and private key
- 4. Import your .p12 content to your .jks keystore
- 5. Verify the content of yout jks keystore
Not long time ago i needed to change a certificate that expired on a tomcat webserver that used a 2-way-ssl connection type to communicate to another server. I received the new certificate from my privider in a .pem form but i needed to add it to my java keystore. The problem was that I would only add the certificate and the chain (but no private key) to my keystore. By doing so I have created a truststore so my webserver did not serve a propper certificare. After some digging, I found a way to make my truststore a keystore.
I will describe below the steps to create a valid java keystore.
1. Split your .pem file so you have a different file for the certificate, PK, Root certificate and intermidiate certificates
In my case this resulted in 5 files:
You might not have the root and intermidiate certificates, as they might be specified in a separate trustore. If thats the case please skip steps 2/3/4
2. Import Root and intermidiate certificates to your keystore:
You will be asked for a passkey to protect your new keystore. Please enter twice your desired key.Also confirm you want to add the cert to your keystore by typing “yes” and ENTER:
Follow the same steps for adding the intermidiate certificates(if needed):
3. Create a .p12 keystore containing your leaf certificate and private key
You will need to specify your PK password. this is usually provided by your certificate provider and might not be set. Also you will need to enter and confirm your .p12 keystore password.
Replace
4. Import your .p12 content to your .jks keystore
You will be propted for the .p12 and .jks passwords.
NOTE: You can add many certificates + private keys (PrivateKeyEntries) into a jks
keystore, but it might not work when reading them.
5. Verify the content of yout jks keystore
In case of everything worked well you should see 4 entries in your truststore. 3 of the woud be of trustedCertEntry type and one will be PrivateKeyEntry.