[Humbledown highlights] Generating Tomcat Keystore from Key, Cert, and CA Bundle

Originally published by myself on humbledown.org at Sun Jun 12 06:05:32 NZST 2011
and since recovered to this location. It has not been tested since its original publication.

I had to configure a new Tomcat installation yesterday with for SSL. The server also has an Apache (httpd) installation, and the SSL certificate was generated using the OpenSSL command (umask 077; cd /etc/pki/tls/private; openssl req -nodes -newkey rsa:2048 -keyout servername.key -out servername.csr. This generates the private key and a certificate signing request, which gets sent to the Certificate Authority (CA). The response from the CA includes a servername.crt and servername.ca-bundle, which contains a list of PEM-encoded x509 certificates, that are used in establishing the chain of trust from your certificate to a root certificate.

That process works completely fine fine for the httpd server, but Tomcat expects its key to be generated using the keytool command, and the resulting certificate and CA bundle to be imported using keytool. Tomcat (or rather, the Java Security libraries) also default to using the Java Key Store (JKS) format. This is annoying, because it means the keystore will not include the private key (which you generated using openssl req ....

Although you can still combine them together into a JKS keystore, it does require that you compile and use a small Java utility from the agentbob website. However, I prefer not to use third-party utilities to maintain a JKS keystore when the Tomcat and OpenSSL can maintain a PKCS#12 formatted keystore.

Here is a script that will take a PEM-encoded SSL private key foo.key, certificate foo.crt and CA chain foo.ca-bundle files [NOTE BIG ASSUMPTION -- this is a site-local convention], and combine them into a PKCS#12-formatted keystore that can be used with Tomcat. It expects, though doesn't require, that the (private) key is under /etc/pki/tls/private/, and the crt and ca-bundle files are under /etc/pki/tls/certs/

File: generate-tomcat-keystore-pkcs12

#!/bin/bash

# Why a regex? Globs are painful to expand
#
newest_file_in_dir_matching_regex()
{
    local dir="$1"
    local regex="$2"

    echo "$dir/$(ls -t "$dir" | egrep "$regex" | head -1)"
}

read_with_default()
{
    local prompt="$1"
    local default="$2"

    local response=""
    read -e -p "$prompt [$default]: " response

    if [ -z "$response" ]; then
        response="$default"
    fi

    echo "$response"
}

friendly_name='tomcat'

keystore_filename=$(read_with_default 'New keystore filename' \
        '/etc/tomcat5/tomcat.keystore.pkcs12')

key_filename=$(read_with_default 'Private key filename' \
        $(newest_file_in_dir_matching_regex /etc/pki/tls/private '\.key$'))

cert_filename=$(read_with_default 'Certificate filename' \
        $(newest_file_in_dir_matching_regex /etc/pki/tls/certs '\.crt$'))

cabundle_filename=$(read_with_default 'CA Bundle filename' \
        $(newest_file_in_dir_matching_regex /etc/pki/tls/certs '\.ca-bundle$'))

cat "$key_filename" "$cert_filename" "$cabundle_filename" | \
openssl pkcs12 -export -nodes \
        -name      "$friendly_name" \
        -out       "$keystore_filename"

echo "Now you can configure Tomcat with the following connector:"
echo ""
echo "    "
echo "    "

Don't forget to mark it executable, and to backup any old keystore before running this.

Comments

Popular posts from this blog

ORA-12170: TNS:Connect timeout — resolved

Getting MySQL server to run with SSL

From DNS Packet Capture to analysis in Kibana