Configuration of SSL

Prepare the Certificate Keystore

Tomcat currently operates only on JKS, PKCS11 or PKCS12 format keystores. The JKS format is Java’s standard “Java KeyStore” format, and is the format created by the keytool command-line utility. This tool is included in the JDK. The PKCS12 format is an internet standard, and can be manipulated via (among other things) OpenSSL and Microsoft’s Key-Manager.

Each entry in a keystore is identified by an alias string. Whilst many keystore implementations treat aliases in a case insensitive manner, case sensitive implementations are available. The PKCS11 specification, for example, requires that aliases are case sensitive. To avoid issues related to the case sensitivity of aliases, it is not recommended to use aliases that differ only in case.

To import an existing certificate into a JKS keystore, please read the documentation (in your JDK documentation package) about keytool. Note that OpenSSL often adds readable comments before the key, keytool does not support that, so remove the OpenSSL comments if they exist before importing the key using keytool.

To import an existing certificate signed by your own CA into a PKCS12 keystore using OpenSSL you would execute a command like:

 

openssl pkcs12 -export -in mycert.crt -inkey mykey.key \

-out mycert.p12 -name tomcat -CAfile myCA.crt \

-caname root -chain

For more advanced cases, consult the OpenSSL documentation.

To create a new keystore from scratch, containing a single self-signed Certificate, execute the following from a terminal command line:

Windows:

%JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RSA

Unix:

$JAVA_HOME/bin/keytool -genkey -alias tomcat -keyalg RSA

(The RSA algorithm should be preferred as a secure algorithm, and this also ensures general compatibility with other servers and components.)

This command will create a new file, in the home directory of the user under which you run it, named “.keystore”. To specify a different location or filename, add the -keystore parameter, followed by the complete pathname to your keystore file, to the keytool command shown above. You will also need to reflect this new location in the server.xml configuration file, as described later. For example:

Windows:

%JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RSA \

-keystore \path\to\my\keystore

Unix:

$JAVA_HOME/bin/keytool -genkey -alias tomcat -keyalg RSA \

-keystore /path/to/my/keystore

After executing this command, you will first be prompted for the keystore password. The default password used by Tomcat is “changeit” (all lower case), although you can specify a custom password if you like. You will also need to specify the custom password in the server.xml configuration file, as described later.

Next, you will be prompted for general information about this Certificate, such as company, contact name, and so on. This information will be displayed to users who attempt to access a secure page in your application, so make sure that the information provided here matches what they will expect.

Finally, you will be prompted for the key password, which is the password specifically for this Certificate (as opposed to any other Certificates stored in the same keystore file). The keytool prompt will tell you that pressing the ENTER key automatically uses the same password for the key as the keystore. You are free to use the same password or to select a custom one. If you select a different password to the keystore password, you will also need to specify the custom password in the server.xml configuration file.

If everything was successful, you now have a keystore file with a Certificate that can be used by your server.

Edit the Tomcat Configuration File

Tomcat can use two different implementations of SSL:

  • the JSSE implementation provided as part of the Java runtime (since 1.4)
  • the APR implementation, which uses the OpenSSL engine by default.

The exact configuration details depend on which implementation is being used. The implementation used by Tomcat is chosen automatically unless it is overridden as described below. If the installation uses APR – i.e. you have installed the Tomcat native library – then it will use the APR SSL implementation, otherwise it will use the Java JSSE implementation.

To avoid auto configuration you can define which implementation to use by specifying a classname in the protocol attribute of the Connector.

To define a Java (JSSE) connector, regardless of whether the APR library is loaded or not do:

 

<!– Define a blocking Java SSL Coyote HTTP/1.1 Connector on port 8443 –>

<Connector protocol=”org.apache.coyote.http11.Http11Protocol”

port=”8443″ …/>

 

<!– Define a non-blocking Java SSL Coyote HTTP/1.1 Connector on port 8443 –>

<Connector protocol=”org.apache.coyote.http11.Http11NioProtocol”

port=”8443″ …/>

 

Alternatively, to specify an APR connector (the APR library must be available) use:

 

<!– Define a APR SSL Coyote HTTP/1.1 Connector on port 8443 –>

<Connector protocol=”org.apache.coyote.http11.Http11AprProtocol”

port=”8443″ …/>

 

If you are using APR, you have the option of configuring an alternative engine to OpenSSL.

 

<Listener className=”org.apache.catalina.core.AprLifecycleListener”

SSLEngine=”someengine” SSLRandomSeed=”somedevice” />

The default value is

<Listener className=”org.apache.catalina.core.AprLifecycleListener”

SSLEngine=”on” SSLRandomSeed=”builtin” />

So to use SSL under APR, make sure the SSLEngine attribute is set to something other than off. The default value is on and if you specify another value, it has to be a valid engine name.

If you haven’t compiled in SSL support into your Tomcat Native library, then you can turn this initialization off

 

<Listener className=”org.apache.catalina.core.AprLifecycleListener”

SSLEngine=”off” />

SSLRandomSeed allows to specify a source of entropy. Productive system needs a reliable source of entropy but entropy may need a lot of time to be collected therefore test systems could use no blocking entropy sources like “/dev/urandom” that will allow quicker starts of Tomcat.

The final step is to configure the Connector in the $CATALINA_BASE/conf/server.xml file, where$CATALINA_BASE represents the base directory for the Tomcat instance. An example <Connector> element for an SSL connector is included in the default server.xml file installed with Tomcat. To configure an SSL connector that uses JSSE, you will need to remove the comments and edit it so it looks something like this:

 

<!– Define a SSL Coyote HTTP/1.1 Connector on port 8443 –>

<Connector

protocol=”HTTP/1.1″

port=”8443″ maxThreads=”200″

scheme=”https” secure=”true” SSLEnabled=”true”

keystoreFile=”${user.home}/.keystore” keystorePass=”changeit”

clientAuth=”false” sslProtocol=”TLS”/>

The example above will throw an error if you have the APR and the Tomcat Native libraries in your path, as Tomcat will try to use the APR connector. The APR connector uses different attributes for many SSL settings, particularly keys and certificates. An example of an APR configuration is:

 

<!– Define a SSL Coyote HTTP/1.1 Connector on port 8443 –>

 

<Connector

protocol=”HTTP/1.1″

port=”8443″ maxThreads=”200″

scheme=”https” secure=”true” SSLEnabled=”true”

SSLCertificateFile=”/usr/local/ssl/server.crt”

SSLCertificateKeyFile=”/usr/local/ssl/server.pem”

SSLVerifyClient=”optional” SSLProtocol=”TLSv1″/>

 

You will note that the example SSL connector elements are commented out by default. You can either remove the comment tags from around the example SSL connector you wish to use or add a new Connector element of your own. In either case, you will need to configure the SSL Connector for your requirements and environment. The configuration options and information on which attributes are mandatory, are documented in the SSL Support section of the HTTP connector configuration reference. Make sure that you use the correct attributes for the connector you are using. The BIO and NIO connectors use JSSE whereas the APR/native connector uses APR.

The port attribute (default value is 8443) is the TCP/IP port number on which Tomcat will listen for secure connections. You can change this to any port number you wish (such as to the default port for https communications, which is 443). However, special setup (outside the scope of this document) is necessary to run Tomcat on port numbers lower than 1024 on many operating systems.

If you change the port number here, you should also change the value specified for the redirectPort attribute on the non-SSL connector. This allows Tomcat to automatically redirect users who attempt to access a page with a security constraint specifying that SSL is required, as required by the Servlet Specification.

After completing these configuration changes, you must restart Tomcat as you normally do, and you should be in business. You should be able to access any web application supported by Tomcat via SSL. For example, try:

 

https://localhost:8443

 

and you should see the usual Tomcat splash page (unless you have modified the ROOT web application). If this does not work, the following section contains some troubleshooting tips.

Share this post
[social_warfare]
General Tips on Running SSL
Installing a Certificate from Certificate Authority

Get industry recognized certification – Contact us

keyboard_arrow_up