Are the client and server using http(s) to communicate with one another? If not, can you post the client code and the server code? Have you gotten the client and server to work without SSLSockets?
Main Topics
Browse All Topics
Hi i have a servlet that is deployed on tomcat designed to communicate with server written as a plain java class. Both servlet and server are to communicate using SSL mutual authentication.
When i am running server on port 9000
Server starting...
Server started on port 9000
Waiting for clients...
Connected. Cipher Suite: Unknown 0x0:0x0
Request from client received...
javax.net.ssl.SSLException
hakeException: Received fatal alert: certificate_unknown
And the servlet running on the tomcat is giving this exception:
javax.net.ssl.SSLException
Both servlet and server exchange certificates for authentication. I am creating certificates using OpenSSL.These are the steps i am following to create keystore and truststore on both sides:
1. creating a server keystore:
keytool -genkey -alias server -keystore server_keystore
2. creating a client keystore
keytool -genkey -alias client -keystore client_keystore
3. creating cert requests from both the keystores
4. create a 1024-bit private key to use when creating CA.:
C:\ssl>openssl genrsa -des3 -out keys/ca.key 1024
5.create a master certificate based on this key, to use when signing other certificates:
C:\ssl>openssl req -config openssl.conf -new -x509 -days 1001 -key keys/ca.key -out certs/ca.cer
6.Sign both the requests as follows:
C:\ssl>openssl ca -policy policy_anything -config openssl.conf -cert certs/ca.cer -in requests/certreq.txt -keyfile keys/ca.key -days 360 -out certs/iis.cer
7.Converting the signed certificate into x509 format for use with IIS:(for both the client and server request certs)
C:\ssl>openssl x509 -in certs/iis.cer -out certs/iisx509.cer
8. storing the signed server cert in server keystore and client truststore using keytool import
9. storing the signed client cert in client keystore and server truststore using keytool import.
Placing the client keystore and truststore in tomcat and placing the server keystore and truststore in folder where server is running.
By seeing the steps mentioned above am i missing anything. Please suggest me solution this is very urgent
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Both Servlet client and server are involved in SSL mutual authentication
Servlet Client code:
import java.io.InputStream;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.security.KeyStore;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager
import javax.net.ssl.KeyManagerFa
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFac
import javax.servlet.ServletConfi
import javax.servlet.ServletExcep
import javax.servlet.ServletOutpu
import javax.servlet.http.HttpSer
import javax.servlet.http.HttpSer
import javax.servlet.http.HttpSer
public class SSLSecretNumberServletWith
extends HttpServlet
{
private String server;
private int port;
public void init(ServletConfig config)
throws ServletException
{
super.init(config);
try
{
port = (new Integer(getInitParameter("
intValue();
}
catch (NumberFormatException nfe)
{
log("port must be a parsable integer");
throw new ServletException(nfe.getMe
}
String server = getInitParameter("server")
}
/**
* Opens a secure socket connection with the external
* server, retrieves the secret number generated from
* the external server, and communicates it to the
* client. This method is executed each time a client
* invokes the servlet.
*
* @param request an HttpServletRequest object
* containing information about the client's
* request.
* @param response an HttpServletResponse object in
* which the servlet stores information to be
* sent to the client.
* @throws ServletException if an exception occurs that
* interferes with the servlet's normal
* operation.
* @throws IOException if an I/O exception occurs.
*/
public void service(HttpServletRequest
HttpServletResponse response)
throws ServletException, IOException
{
log("Requesting connection from " + server +
" on port " + port + "...");
// The truststore and keystore passwords are
// both hardcoded
char[] passwd = "xyz123".toCharArray();
char[] passwd2 = "client123".toCharArray();
SSLSocket s = null;
try
{
// Create an SSLContext instance implementing
// the TLS protocol.
SSLContext ctx = SSLContext.getInstance("TL
// Create a TrustManagerFactory implementing the
// X.509 key management algorithm.
TrustManagerFactory tmf =
TrustManagerFactory.getIns
// Create a KeyStore instance implementing the
// Java KeyStore (JKS) algorithm.
KeyStore ks = KeyStore.getInstance("JKS"
// Load the KeyStore file keyStoreFile.
ks.load(new
FileInputStream("trustStor
// Initialize the TrustManagerFactory object with
// the KeyStore.
tmf.init(ks);
// Since the server requires client authentication
// the client must present its own certificate to
// the server.
// Create a KeyManagerFactory implementing the
// X.509 key management algorithm.
KeyManagerFactory kmf =
KeyManagerFactory.getInsta
// Create a KeyStore instance implementing the
// Java KeyStore (JKS) algorithm.
KeyStore ks2 = KeyStore.getInstance("JKS"
// Load the KeyStore file keyClientStore
ks2.load(new FileInputStream("keyClient
passwd2);
// Initialize the KeyManagerFactory object with
// the KeyStore.
kmf.init(ks2, passwd2);
// Initialize the SSLContext with the
// KeyManagerFactory and TrustManagerFactory.
ctx.init(kmf.getKeyManager
tmf.getTrustManagers(), null);
// Create an SSLSocketFactory instance from the
// SSLContext and generate an SSLSocket from it.
SSLSocketFactory sslFact = ctx.getSocketFactory();
s = (SSLSocket) sslFact.createSocket
(server, port);
}
catch (Exception e)
{
// Catch any Exception and turn it into a
// ServletException
throw new ServletException(e.getMess
}
SSLSession session = s.getSession();
log("Connected to server " + server + "\nCipher suite:"
+ session.getCipherSuite());
// Get the secret number from the external server.
InputStream in = s.getInputStream();
DataInputStream dis = new DataInputStream(in);
int secretNumber = dis.readInt();
// Send the response to the client in HTML format.
ServletOutputStream out = response.getOutputStream()
response.setContentType("t
out.println("<HTML><HEAD><
"Secret Number Page</TITLE></HEAD></HTML>
out.println("<BODY><H1>Sec
secretNumber + "</H1></BODY>");
out.println("</HTML>");
dis.close();
in.close();
s.close();
}
}
Server code:
import java.io.OutputStream;
import java.io.IOException;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.util.Random;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLServerSoc
import javax.net.ssl.SSLServerSoc
import javax.net.ssl.SSLContext;
import javax.net.ssl.KeyManagerFa
import javax.net.ssl.TrustManager
import java.security.KeyStore;
/**
* Server class for random number generation. This server
* runs as a process that external client applications can
* contact by opening an appropriate socket. Every time a
* client contacts the server application, a new Thread is
* generated. The server application produces a new random
* number, communicates it to the client, and closes the
* connection with the client. The communication between
* the server and client is protected by SSL. This server
* supports server authentication and requires client
* authentication.
*/
public class SSLServerWithServerAndClie
{
private SSLSocket client;
private static Random randomGenerator = new Random();
/**
* Public constructor. Initializes the server by setting
* the Socket to communicate with the client.
*
* @param client an SSLSocket object representing the
* client application this server is
* communicating with.
*/
public SSLServerWithServerAndClie
{
this.client = c;
}
/**
* Generates a random number and communicates it to the
* the client. The communication is protected by SSL.
*/
public void run()
{
int secretNumber = randomGenerator.nextInt();
System.out.println("Secret
try
{
OutputStream out = client.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(secretNumber)
dos.flush();
dos.close();
out.close();
client.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
/**
* Launches the server application. This method expects
* the port number to be passed on the command line.
*
* @param args a String[] array whose first element
* must represent the port number this server
* will be listening on. The port number is
* expected to be an integer between 1025 and
* 65536. Values not in this range will cause the
* application to quit.
*/
public static void main(String args[])
{
if (args.length == 0)
{
System.out.println("Usage:
System.exit(0);
}
int port = 0;
try
{
port = (new Integer(args[0])).intValue
}
catch (NumberFormatException nfe)
{
System.out.println("Usage:
System.out.println
("<port> must be a parsable integer");
System.exit(0);
}
if (port <= 1024 || port > 65536)
{
System.out.println("Usage:
System.out.println
("<port> must be an integer in the range " +
"1025-65536");
System.exit(0);
}
// The keystore and truststore passwords are
// both hardcoded
char[] passwd = "abc123".toCharArray();
char[] passwd2 = "trust123".toCharArray();
SSLContext ctx = null;
try
{
// Create an SSLContext instance implementing
// the TLS protocol.
ctx = SSLContext.getInstance("TL
// Create a KeyManagerFactory implementing the
// X.509 key management algorithm.
KeyManagerFactory kmf =
KeyManagerFactory.getInsta
// Open up the KeyStore in order to present the
// Server's certificates to the client
KeyStore ks = KeyStore.getInstance("JKS"
// Load the KeyStore file keyStoreFile
ks.load (new FileInputStream("keyStoreF
passwd);
// Initialize the KeyManagerFactory object with
// the KeyStore.
kmf.init(ks, passwd);
// Create a TrustManagerFactory implementing the
// X.509 key management algorithm.
TrustManagerFactory tmf =
TrustManagerFactory.getIns
// Since client authentication will be requested,
// the server must be able to trust the client.
KeyStore ts = KeyStore.getInstance("JKS"
ts.load (new FileInputStream("trustServ
passwd2);
// Initialize the TrustManagerFactory object with
// the TrustStore
tmf.init(ts);
// Initialize the SSLContext with the
// KeyManagerFactory and TrustManagerFactory
ctx.init(kmf.getKeyManager
tmf.getTrustManagers(), null);
}
catch (Exception e)
{
System.out.println
("Unable to initialize SSLContext " +
e.getMessage());
System.exit(0);
}
System.out.println("Server
// Create an SSLServerSocketFactory instance from the
// SSLContext and generate an SSLServerSocket from it.
SSLServerSocketFactory sslSrvFact =
ctx.getServerSocketFactory
SSLServerSocket ss = null;
// Try to start the server. At this point, problems
// may arise if another process is already listening
// on the selected port.
try
{
ss = (SSLServerSocket)
sslSrvFact.createServerSoc
// Require client authentication
ss.setNeedClientAuth(true)
}
catch (IOException ioe)
{
System.out.println
("There is already a server running on port "
+ port + "\n" + ioe);
System.exit(0);
}
System.out.println("Server
System.out.println("Waitin
// Start an endless loop in which the server is
// constantly waiting for clients to connect.
while (true)
{
SSLSocket client = null;
try
{
client = (SSLSocket)ss.accept();
SSLSession session = client.getSession();
System.out.println("Connec
session.getCipherSuite());
}
catch (IOException ioe)
{
System.out.println("Unable
"connection from client\n" + ioe);
System.exit(0);
}
System.out.println
("Request from client received...");
SSLServerWithServerAndClie
new SSLServerWithServerAndClie
server.start();
}
}
}
After you verify the proggie works sans SSL, I would next implement the "standard" SSL protocol (instead of TLS and the IBM/JKS stuff). You should only need keytool to get this done..
Just out of curiosity, why aren't you using regular https and using a java httpclient as the client and a servlet as the server?
Business Accounts
Answer for Membership
by: dhulipalaPosted on 2005-10-09 at 13:00:07ID: 15048688
Anybody please it is very urgent.
Thanks,
ravi