Monday, July 7, 2014

How to generate & add certificates using keytool

Some times you need to use secure http calls depending upon requirements.

At that time if you want to generate a certificate temporarily here are the simple steps for how to generate , put it in JDK & how to configure the same in server (Here i am using tomcat).I got explored a bit while integrating CAS with Liferay.

C:\Documents and Settings\akash>keytool -genkey -alias cas-liferay-infocast -keypass changeit -keyalg RSA
Enter keystore password:
What is your first and last name?
[Unknown]: localhost
What is the name of your organizational unit?
[Unknown]: Information systems
What is the name of your organization?
[Unknown]: FULCRUM
What is the name of your City or Locality?
[Unknown]:PUNE
What is the name of your State or Province?
[Unknown]:MAHARASTRA
What is the two-letter country code for this unit?
[Unknown]: IN
Is CN=localhost, OU=Information systems, O=FULCRUM, L=PUNE , ST=MAHARASTRA, C=IN correct?
[no]: yes


C:\Documents and Settings\akash>keytool -export -alias cas-liferay -keypass changeit -file D:\projects\liferay\project-docs\infocast\server_cas_infocast.crt
Enter keystore password:
Certificate stored in file

C:\Documents and Settings\akash>keytool -import -file D:\projects\liferay\project-docs\infocast\server_cas_infocast.crt -keypass changeit -keystore C:\Java\jdk1.6.0_19\jre\lib\security\cacerts
Enter keystore password:
keytool error: java.lang.Exception: Certificate not imported, alias <mykey> already exists

Some times you will get above error (here mykey is the default alias - it might have taken already). In that case import the certificate with alias name as below
C:\Documents and Settings\akash>keytool -import -alias cas-liferay-infocast -file D:\projects\liferay\project-docs\infocast\server_cas_infocast.crt -keystore C:\Java\jdk1.6.0_19\jre\lib\security\cacerts

Here is my server.xml related change in TOMCAT_HOME/conf

Search for 8443 in that file, you will find some thing like this
<connector clientauth="false" maxthreads="150" port="8443" protocol="HTTP/1.1" scheme="https" secure="true" sslenabled="true" sslprotocol="TLS"></connector>
change above to
<connector clientauth="false" keyalias="cas-liferay-infocast" maxthreads="150" port="8443" protocol="HTTP/1.1" scheme="https" secure="true" sslenabled="true" sslprotocol="TLS"
keystorefile="C:/Documents and Settings/akash/.keystore" keystorepass="changeit"
truststorefile="C:/Java/jdk1.6.0_19/jre/lib/security/cacerts"
></connector>


Reference Link: http://btnkumar.blogspot.in/2010/08/how-to-generate-add-certificates-using.html 

No comments:

Post a Comment