Importing a self signed certificate to java cacert trust store

The following steps to be used only in non production environments.

Self signed certificates could be used in some organisations to make server to server calls between java programs on SSL.  During these situations, we can import the certificate from the server jvm to client's jvm to fix the SSL handshake issue.

On Server JVM:

Generate a self signed certificate

keytool -genkey -noprompt -alias test.example.com -dname "CN=localhost, OU=Team Name, O=Organisation Name, L=London, S=Greater London, C=UK" -ext "san=dns:test.example.com,dns:test.example.org" -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore /tmp/keystore.p12 -validity 36500 -storepass mypassword -keypass mypassword

List the certificate

keytool -list -keystore /tmp/keystore.p12 -storepass mypassword -storetype PKCS12 -v

Export the certificate

keytool -exportcert -keystore /tmp/keystore.p12 -storetype PKCS12 -storepass mypassword -alias localhost -file /tmp/test.example.com.crt

On Client JVM:

List existing certificate in default trust store (cacerts)

keytool -list -v -keystore $JAVA_HOME/jre/lib/security/cacerts -storepass changeit

Import the server certificate

Prefix the following command with sudo if you get permission denied error.

keytool -import -v -trustcacerts -alias test.example.com -file /tmp/localhost.crt -keystore $JAVA_HOME/jre/lib/security/cacerts -keypass changeit -storepass changeit -noprompt

List the newly imported certificate

keytool -list -v -keystore $JAVA_HOME/jre/lib/security/cacerts -storepass changeit -alias test.example.com

Delete the newly imported certificate (after finishing the test)

Prefix the following command with sudo if you get permission denied error.

keytool -delete -alias test.example.com -keystore $JAVA_HOME/jre/lib/security/cacerts -storepass changeit

Comments

Popular posts from this blog

JSON with curl and jq

Import self signed in Linux for Chrome / Chromium headless testing

Colima - Drop In replacement for Docker Desktop for Mac and Linux