Thursday, September 8, 2011

Setting Up SSL on Tomcat

http://techtracer.com/2007/09/12/setting-up-ssl-on-tomcat-in-3-easy-steps/

Setting up SSL on Tomcat is easy and you don’t have to do much for converting your web application to work with the https protocol. But however, the problem you would find to set up SSL is the documentation available over the web. The documentation source is available on the Apache site but it starts off good and ends with a lot of confusion. Especially I was confused on the OpenSSL part where it says to use OpenSSL.
It might be good in a production environment to use OpenSSL but if you just want to test out SSL with Tomcat alone then it is more than enough to just have your JDK and Tomcat setups. So I would make you walk through the same steps which I did while getting SSL up and running and building a secured web app within a matter of minutes.
The things which I have used to setup SSL consists of:
  • JDK 1.6
  • Tomcat 6
Even though I have used the latest version I don’t see any problems which you might face in carrying out the same set of steps for JDK 1.5 which I am about to explain. JDK comes shipped with a keytool executable which is required to generate a keystore. The keytool can be found in the earlier version of JDK too. The 3 steps which would make you to get started with setting up SSL are:
  1. Generating the Keystore file
  2. Configuring Tomcat for using the Keystore file
  3. Configuring your web application to work with SSL
Let’s get this party started now.
1. Generating the KeyStore file
The keystore file is the one which would store the details of the certificates necessary to make the protocol secured. Certificates contain the information as to who is the source from which you are receiving the application data and to authenticate whether it is the intended party or not. To make this keystore you would have to use the keytool. So open command prompt in Windows or the shell in Linux and type:
cd %JAVA_HOME%/bin on Windows
cd $JAVA_HOME/bin on Linux
You would land up in the Java bin directory. Now time to run the keytool command. You have to provide some parameters to the command as follows :

keytool -genkey -alias techtracer -keypass ttadmin -keystore techtracer.bin -storepass ttadmin

The highlighted words are the ones which you would have to change according to your requirements. But keep one thing in mind that both the keypass and storepass passwords should be the same. The .bin file is actually your keystore file. It would now start a questionnaire. So fill in the relevant details accordingly. Look below for a reference as to what to answer for the questions.

What is your first and last name?
[Unknown]: nitin pai
What is the name of your organizational unit?
[Unknown]: home
What is the name of your organization?
[Unknown]: techtracer
What is the name of your City or Locality?
[Unknown]: mumbai
What is the name of your State or Province?
[Unknown]: maharashtra
What is the two-letter country code for this unit?
[Unknown]: IN
Is CN=nitin pai, OU=home, O=techtracer, L=mumbai, ST=maharashtra, C=IN correct?
[no]: yes
The command would then conclude. It would make a .bin file with the name you had provided inside the bin directory itself. In my case it was techtracer.bin which was located in
C:\Program Files\Java\jdk1.6.0_02\bin\
Put the .bin file in the webapps directory of Tomcat. This is required to avoid the need to give an absolute path of the file in the next step.
2. Configuring Tomcat for using the Keystore file
Here we would be making some changes to the server.xml file inside tomcat to tell it to use the keystore which was created in the earlier step for configuring SSL. Open the file server.xml which can be found as:
<CATALINA_HOME>/conf/server.xml
Now you have to modify it. Find the Connector element which has port=”8443″ and uncomment it if already not done. Add two lines. The highlighted lines are the newly added ones.

<Connector port=”8443″
maxThreads=”150″ minSpareThreads=”25″ maxSpareThreads=”75″
enableLookups=”true” disableUploadTimeout=”true”
acceptCount=”100″ debug=”0″ scheme=”https” secure=”true”
clientAuth=”false” sslProtocol=”TLS”
keystoreFile=”../webapps/techtracer.bin”
keystorePass=”ttadmin” />

You can notice that I have given the path to the keystoreFile property as relative to tomcat bin directory because the startup command will look for the .bin file. Now all you have to do is start your server and check the working of SSL by pointing your browser to the URL to:
https://localhost:8443/
Now that you have your tomcat running in the SSL mode you are ready to deploy an application to test its working. You must note that still your tomcat can run in normal mode too at the same time i.e on port 8080 with http. So it is but obvious that any application deployed to the server will be running on http and https at the same time. This is something that we don’t want. We want our application to run only in the secured mode.
3. Configuring your web application to work with SSL
In order to do this for our test, take any application which has already been deployed successfully in Tomcat and first access it through http and https to see if it works fine. If yes, then open the web.xml of that application and just add this XML fragment before web-app ends i.e </web-app>

<security-constraint>
<web-resource-collection>
<web-resource-name>securedapp</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
Explanation of the fragment is beyond the scope of this tutorial but all you should notice is that the /* indicates that now, any resource in your application can be accessed only with https be it Servlets or JSP’s. The term CONFIDENTIAL is the term which tells the server to make the application work on SSL. If you want to turn the SSL mode for this application off then just turn don’t delete the fragment. Just put the value as NONE instead of CONFIDENTIAL. That’s it!
Conclusion
These were the 3 easy steps in which you can make Tomcat to work in the SSL mode and also it tells you how easily you can turn the SSL mode on and off. If you find any difficulty or are not clear on any of the above steps feel free to drop in your queries. If you like this tutorial it would be nice of you to drop in a comment of appreciation or feedback as to how this tutorial can be improved.


http://oldbytes.posterous.com/create-a-ssl-keystore-for-a-tomcat-server-usi

Create a SSL keystore for a Tomcat server using Openssl


An SSL certificate was required for one of our customers. The SSL certificate was to be used with a Tomcat server, but I decided to give the customer the flexibility to re-use this certificate on a different webserver if needed. This meant I used openssl to generate the certificate and then created a pkcs12 keystore.

  • Create the private key and certificate request
    • Create the certificate key
    • # openssl genrsa -des3 -out customercert.key 2048
    • Remove the passphrase from the key
    • # openssl rsa -in customercert.key -out customercert.key.new
      # mv customercert.key.new customercert.key

    • Create the Certificate request
    • # openssl req -new -key customercert.key -out customercert.csr
  • Create the Keystore file for use with tomcat and keytool
I had some trouble getting this to work. This is a very simple procedure when working with certs signed by GoDaddy, but certs from Verisign needed some extra hand-holding. There is some information on how to do this is found at http://conshell.net/wiki/index.php/OpenSSLtoKeytoolConversiontips.
I did not follow the instructions on this site. I ended up creating a keystore in the pkcs12 format instead of the default jks format. This site above does have instructions for converting a pkcs12 keystore to a jks format, if you require. The signed certificate was downloaded to clients.adaptivetcr.com.cer. The Secure Site with EV Root bundle was downloaded to intermediate.crt. When I first attempted to create the keystore file, I received the error below
openssl pkcs12 -export -chain -CAfile intermediate.crt -in customercert.cer -inkey customercert.key -out customercert.keystore -name tomcat -passout pass:changeit
Error unable to get issuer certificate getting chain.


Now the interesting thing about this error is that if you attempt a openssl verify using both cert file and intermediate.crt, it does not complain and gives the “OK” message. After a bit of testing, I found that you need to make a new CAfile to be used, that combines the cacerts file from the openssl distribution and the intermediate.crt file.
#
cat intermediate.crt /etc/ssl/certs/ca-certificates.crt > allcacerts.crt

or:

# cat ica.cer rca.cer /etc/pki/tls/certs/ca-bundle.crt > allcacerts.crt

#
openssl pkcs12 -export -chain -CAfile allcacerts.crt -in customercert.cer -inkey customercert.key -out customercert.keystore -name tomcat -passout pass:changeit

This successfully created the keystore file. You can look at the contents of the keystore by running
keytool -list -keystore customercert.keystore -storetype pkcs12 -v

Notes:
When using MS cert server generate the cert, put both ica and rca cert in the allcacerts.crt
shell> keytool -certreq -keyalg RSA -alias tomcat -file certreq.txt

Get cert from CA.
shell> keytool -import -alias tomcat -file
------
http://www.entrust.net/knowledge-base/technote.cfm?tn=6557

TN 6557 - How do I move my certificate from Apache to Tomcat?
Question:
How do I move my certificate from Apache to Tomcat?
Answer:
Since Jakarta Tomcat 5.x or higher supports the use of PKCS#12 or PFX keystores, you can use OpenSSL to convert the format of your Apache private key and SSL certificate.

Note: Only Tomcat 5 and later support keystores in PKCS#12 format.
Complete the following procedure to move your certificate from Apache to Tomcat.

To move your certificate from Apache to Tomcat:
1. At a command prompt or terminal session, type the following:
openssl pkcs12 -export -in mycert.crt -inkey <mykey.key> -out <mycert.p12> -name tomcat -CAfile <Entrust_SSL_CA.cer> -caname root -chain

Where:
<mykey.key> is your current private key.
<mycert.p12> is your current openssl certificate
<Entrust_SSL_CA.cer> is the Entrust Secure Server Root CA available for download at the following URL https://www.entrust.net/downloads/root_index.cfm

The exported keystore will be in <mycert.p12>
2. Specify in Tomcat that the keystore is in PKCS#12 format by inserting keystoreType="pkcs12" in the ssl configuration.


# openssl genrsa -des3 -out myserver.key.tmp 2048
# openssl rsa -in myserver.key.tmp -out myserver.key
# openssl req -new -key myserver.key -out myserver.csr

# cat ica.cer rca.cer /etc/pki/tls/certs/ca-bundle.crt > allcacerts.crt
# openssl pkcs12 -export -chain -CAfile allcacerts.crt -in myserver.b64.cer -inkey myserver.key -out myserver.p12 -name tomcat -passout pass:mypassword
where myserver.b64.cer is a Base64 encoded X.509 certificates (PEM).
The portion of tomcat server.xml :

<Connector port="443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
keystoreFile="//myserver.p12" keystorePass="mypassword" keystoreType="pkcs12"
clientAuth="false" sslProtocol="TLS" />

Verify ssl cert
# openssl s_client -showcerts -connect myserver:443 (or 636 for ldap default port)
Copy from the "-----BEGIN CERTIFICATE-----" to the "-----END CERTIFICATE-----" , and save it to <mycert.pem> file
# keytool -printcert -file <mycert.pem>
http://shib.kuleuven.be/docs/ssl_commands.shtml
http://www.sslshopper.com/article-most-common-openssl-commands.html

No comments:

Post a Comment