Igor's Techno Club

Hot To Use Java Keystore with Custom SSL Certificates

Screenshot 2024-06-30 at 23

Certificates play a critical role in establishing trust between clients and servers. Managing these certificates typically involves generating them, signing them, and storing them in a keystore. This article provides a comprehensive guide to generating custom SSL certificates and integrating them into a Java Keystore

Introduction to Java Keytool and Keystore

Java Keytool is a command-line utility provided with Java Development Kit (JDK) (I used JDK 17 for this post) that allows users to create and manage their own keystores (collections of security certificates either in public-key pairs or trusted certificates/certificate chains). A keystore is a binary file used by Java applications for encryption, authentication, and verifiable transmission of data.

Java Keytool and keystores support various keystore types including JKS (Java KeyStore), PKCS12, and others. By managing these certs and keystores, applications can ensure secure communications and operations. Lets follow the example on how we can use a keystore for storing our custom certificates.

Generating Custom Certificates and Configurations

Below are the series of commands used to generate custom certificates, and we will describe each command and the outputs they produce:

Generate CA Certificate and Key

The first step in our journey is creating our own Root Certificate Authority (CA). This self-signed certificate will act as the trusted anchor for all subsequent certificates we generate.

openssl req -new -nodes -x509 -days 3650 -newkey rsa:2048 -keyout certs/ca.key -out certs/ca.crt -config ./cnf/ca.cnf

Output files:


For more content like this, subscribe to the blog



Combine CA Certificate and Key

cat certs/ca.crt certs/ca.key > certs/ca.pem

This command combines the CA certificate and key into a single ca.pem file for convenience in later steps.

Generate Server Certificate Signing Request (CSR) and Private Key

Next, we'll generate a server certificate that will be signed by our Root CA, establishing a chain of trust.

openssl req -new -newkey rsa:2048 -keyout certs/server.key -out certs/server.csr -config ./cnf/server.cnf -nodes

Output files:

Generate Server Certificate

openssl x509 -req -days 3650 -in certs/server.csr -CA certs/ca.crt -CAkey certs/ca.key -CAcreateserial -out certs/server.crt -extfile ./cnf/server.cnf -extensions v3_req

Output files:

Create PKCS#12 Archive

To facilitate convenient importing into the Java Keystore, we'll package the server certificate and its private key into a PKCS#12 file.

openssl pkcs12 -legacy -export -in certs/server.crt -inkey certs/server.key -chain -CAfile certs/ca.pem -name your-domain.com -out certs/server.p12 -password pass:test1234

Output file:

Import PKCS#12 into Java KeyStore

Finally, we'll import the contents of the PKCS#12 file into a Java Keystore.

keytool -importkeystore -deststorepass test1234 -destkeystore certs/server.keystore.jks \
    -srckeystore certs/server.p12 \
    -deststoretype PKCS12 \
    -srcstoretype PKCS12 \
    -noprompt \
    -srcstorepass test1234 \
    -deststoretype JKS

Output files:

Import CA Certificate into Truststore

To ensure that your Java application trusts certificates signed by your Root CA, you must add the Root CA certificate to a truststore.

keytool -keystore certs/truststore.jks -alias CARoot -import -file certs/ca.crt -storepass test1234 -trustcacerts -noprompt -storetype PKCS12

Output files:

Useful Keytool Commands

Keytool List Certs

The keytool -list command is used to display the contents of a keystore.

keytool -list -keystore PATH_TO_KEYSTORE

This command enables you to see all the entries in the specified keystore, including aliases and certificate types.

Keytool Delete Alias

The keytool -delete command allows you to delete an entry identified by its alias from the keystore.

keytool -delete -alias ALIAS -keystore PATH_TO_KEYSTORE

This command is useful for removing outdated or compromised certificates from the keystore.

Keytool List Cacerts

To view the default Java truststore, use the following command targeting cacerts:

keytool -list -keystore ${JAVA_HOME}/lib/security/cacerts

The cacerts file contains trusted CA certificates. It's crucial for verifying SSL/TLS connections in Java applications.

Keytool Change Alias

If you need to rename an alias in the keystore, the keytool -changealias command is what you need:

keytool -changealias -alias OLD_ALIAS -destalias NEW_ALIAS -keystore PATH_TO_KEYSTORE

This command is particularly useful when you need to update the naming conventions of your keystore entries.

Default Java Keystore Password

The default password for Java's default keystore is often changeit. This is applicable mostly for the cacerts file found under ${JAVA_HOME}/lib/security/cacerts. Changing this default password is a good security practice.

keytool -storepasswd -new NEW_PASSWORD -keystore ${JAVA_HOME}/lib/security/cacerts

Keytool Export Private Key

Exporting a private key from a Java keystore requires a bit of workaround since keytool does not directly support this. Here's an approach using openssl to accomplish this task.

First, export the certificate and private key into a PKCS#12 file:

keytool -importkeystore -srckeystore PATH_TO_KEYSTORE -destkeystore OUTPUT.p12 -srcalias ALIAS -deststoretype PKCS12

Then, use openssl to extract the private key:

openssl pkcs12 -in OUTPUT.p12 -nocerts -out private_key.pem -nodes

This multi-step process allows you to export a private key if absolutely necessary, although it's generally advised to protect private keys carefully within the keystore.

Conclusion

You've successfully created a Java Keystore containing your custom SSL certificates! By following the steps outlined in this article, you've gained a deeper understanding of the certificate generation process, the importance of keystores, and how to seamlessly integrate these elements into your Java projects for enhanced security. Remember to replace placeholder values with your specific information and keep your private keys secure.

#java #keytool #ssl