https connection

Dear all,

i am sending one problem that I want to retrieve the pages and sending information to the https connection using java client(SWING).Her we do not run TOMCAT,but a rich graphics client can talk to the server send and recieve information.In this appication i want to explore how to use the crypto library like JSSE.Here we do not use the standard browser, we r using our client application..

This is what as a sample i want to do

1.Create a small java application using SWING and JSSE(clearly not running in a browser,should be a dll or exe file)

2.Prompt login ID and password.

3.Connect to Server and authenticate.

4.Display the results.

i am a beganer

Does any one can suggest me a sample code or related links?

thanks in advance

Shalini

[817 byte] By [shalini97a] at [2007-11-26 16:11:16]
# 1

You have an HTTPS server and want to write a client to it in Java?

The standard API supports this, the following classes are worth studiyng:

java.net.URL

java.net.URLConnection

java.net.HttpURLConnection (also for https)

Java puts emphasis on having a valid https certificate which can be checked. If this is not the case, you have to switch off https certificate checking somehow like this:

http://forum.java.sun.com/thread.jspa?threadID=5128742&messageID=9460026#9460026

BIJ001a at 2007-7-8 22:33:45 > top of Java-index,Core,Core APIs...
# 2

Hello dear,

Thanks 4 ur reply.

I dont have any https server. I have to create that also.

I Am developing a Appication of Secure Socket with JSSE.I have created necessary certificate for client and server using Keytool.When i run server code(SecureServer.java) it is ready to accept connection.But when i run Client it throw Exception...and server also stop and returns to command prompt..I have created certificate of Client and Server.And self certified it.Please help me to solve the problem.

This is secure Server::

import java.io.*;

import java.net.*;

import javax.net.ssl.KeyManagerFactory;

import javax.net.ssl.KeyManager;

import javax.net.ssl.TrustManagerFactory;

import javax.net.ssl.TrustManager;

import javax.net.ssl.SSLContext;

import javax.net.ServerSocketFactory;

import java.security.KeyStore;

public class SecureServer implements Runnable{

public static final int PORT = 5555;

public static final String HOST = "localhost";

public static final String QUESTION = "Knock, knock.";

public static final String ANSWER = "Who's there?";

// The new constants that are used during setup.

public static final String KEYSTORE_FILE = "server_keystore";

public static final String ALGORITHM = "sunx509";

public static final String PASSWORD = "churchillobjects";

public static void main(String[] args){

new Thread(new ClearServer()).start();

}

public void run(){

ServerSocket ss = null;

try {

// Local references used for clarity. Their presence

// here is part of the reason we need to import

// so many classes.

KeyManagerFactory kmf;

KeyManager[] km;

KeyStore ks;

TrustManagerFactory tmf;

TrustManager[] tm;

SSLContext sslc;

// Create a keystore that will read the JKS (Java KeyStore)

// file format which was created by the keytool utility.

ks = KeyStore.getInstance("JKS");

// Load the keystore object with the binary keystore file and

// a byte array representing its password.

ks.load(new FileInputStream(KEYSTORE_FILE), PASSWORD.toCharArray());

// Gives us a factory for key managers that will let

// us handle the asymetric keys we created earlier.

kmf = KeyManagerFactory.getInstance(ALGORITHM);

// Initialize the key manager factory with the keystore object,

// again using the same password for security since it is going to

// access the private key.

kmf.init(ks, PASSWORD.toCharArray());

// Now we can get the key managers from the factory, since it knows

// what type we are using now.

km = kmf.getKeyManagers();

// Next, create a trust manager factory using the same algorithm.

// This is to avoid using the certificates in cacerts that

// represent an authentication security risk.

tmf = TrustManagerFactory.getInstance(ALGORITHM);

// ...then initialize it with the keystore object. This time we don't

// need the keystore password. This is because trusted certificates

// are not a sensitive element in the keystore, unlike the

// private keys.

tmf.init(ks);

// Once that's initialized, get the trust managers from the factory.

tm = tmf.getTrustManagers();

// Almost done, we need a context object that will get our

// server socket factory. We specify TLS to indicate that we will

// need a server socket factory that supports SSL.

sslc = SSLContext.getInstance("TLS");

// Initialize the context object with the key managers and trust

// managers we got earlier. The third parameter is an optional

// SecureRandom object. By passing in null, we are letting the

// context object create its own.

sslc.init(km, tm, null);

// Finally, we get the ordinary-looking server socket factory

// from the context object.

ServerSocketFactory ssf = sslc.getServerSocketFactory();

// From the factory, we simply ask for an ordinary-looking

// server socket on the port we wish.

ss = ssf.createServerSocket(PORT);

listen(ss);

}

catch(Exception e){

e.printStackTrace();

}

finally{

if(ss!=null){

try{

ss.close();

}

catch(IOException e){

// oh, well...

}

}

System.exit(0);

}

}

static void listen(ServerSocket ss) throws Exception{

System.out.println("Ready for connections.");

while(true){

Socket s = ss.accept();

BufferedWriter bw = new BufferedWriter(

new OutputStreamWriter(s.getOutputStream()));

BufferedReader br = new BufferedReader(

new InputStreamReader(s.getInputStream()));

String q = br.readLine();

if(!QUESTION.equals(q)){

throw new RuntimeException("Wrong question: \"" + q + "\"");

}

System.out.println("Question: \"" + q + "\"");

bw.write(ANSWER+"\n");

bw.flush();

s.close();

}

}

}

ERROR::

d:\JSSE>javac SecureServer.java

d:\JSSE>java SecureServer

Ready for connections.

java.lang.RuntimeException: Wrong question: "€b☺⒕☺ 9♦☺ € ⒗ / 3 2 "

at ClearServer.listen(ClearServer.java:47)

at ClearServer.run(ClearServer.java:19)

at java.lang.Thread.run(Thread.java:595)

d:\JSSE>

And The SECURECLENT is::

import java.io.*;

import java.net.*;

import javax.net.ssl.KeyManagerFactory;

import javax.net.ssl.TrustManagerFactory;

import javax.net.ssl.SSLContext;

import java.security.KeyStore;

import javax.net.SocketFactory;

public class SecureClient implements Runnable{

public static final int PORT = 5555;

public static final String HOST = "localhost";

public static final String KEYSTORE_FILE = "client_keystore";

public static final String ALGORITHM = "sunx509";

public static final String PASSWORD = "churchillobjects";

public static final String QUESTION = "Knock, knock.";

public static final String ANSWER = "Who's there?";

public static void main(String[] args){

new Thread(new SecureClient()).start();

}

public void run(){

Socket socket = null;

try{

KeyManagerFactory kmf;

KeyStore ks;

TrustManagerFactory tmf;

SSLContext sslc;

kmf = KeyManagerFactory.getInstance(ALGORITHM);

ks = KeyStore.getInstance( "JKS" );

ks.load(new FileInputStream(KEYSTORE_FILE), PASSWORD.toCharArray());

kmf.init(ks, PASSWORD.toCharArray());

tmf = TrustManagerFactory.getInstance(ALGORITHM);

tmf.init(ks);

sslc = SSLContext.getInstance("TLS");

sslc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

// The process is different from here on the client. Instead of

// getting a ServerSocketFactory, we ask for a SocketFactory from

// the SSL context.

SocketFactory sf = sslc.getSocketFactory();

// Then we get the socket from the factory and treat it

// as if it were a standard (plain) socket.

socket = sf.createSocket(HOST, PORT);

doQuery(socket);

}

catch(Exception e){

e.printStackTrace();

}

finally{

if(socket!=null){

try{

socket.close();

}

catch(IOException e){

// oh, well...

}

}

System.exit(0);

}

}

private void doQuery(Socket s) throws Exception{

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));

bw.write(QUESTION+"\n");

bw.flush();

String response = br.readLine();

if(!ANSWER.equals(response)){

throw new RuntimeException("Wrong answer: \"" + response + "\"");

}

System.out.println("Got the right answer: \"" + response + "\"");

}

}

ERROR IS::

d:\JSSE>javac SecureClient.java

d:\JSSE>java SecureClient

java.net.SocketException: Connection reset

at java.net.SocketInputStream.read(SocketInputStream.java:168)

at com.sun.net.ssl.internal.ssl.InputRecord.readFully(InputRecord.java:2

84)

at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:319)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.j

ava:720)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SS

LSocketImpl.java:1025)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.

java:619)

at com.sun.net.ssl.internal.ssl.AppOutputStream.write(AppOutputStream.ja

va:59)

at sun.nio.cs.StreamEncoder$CharsetSE.writeBytes(StreamEncoder.java:336)

at sun.nio.cs.StreamEncoder$CharsetSE.implFlushBuffer(StreamEncoder.java

:404)

at sun.nio.cs.StreamEncoder$CharsetSE.implFlush(StreamEncoder.java:408)

at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:152)

at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:213)

at java.io.BufferedWriter.flush(BufferedWriter.java:236)

at SecureClient.doQuery(SecureClient.java:71)

at SecureClient.run(SecureClient.java:49)

at java.lang.Thread.run(Thread.java:59

Client and server are using two files

ClearServer::

import java.io.*;

import java.net.*;

public class ClearServer implements Runnable{

public static final int PORT = 5555;

public static final String HOST = "localhost";

public static final String QUESTION = "Knock, knock.";

public static final String ANSWER = "Who's there?";

public static void main(String[] args){

new Thread(new ClearServer()).start();

}

public void run(){

ServerSocket ss = null;

try {

ss = new ServerSocket(PORT);

listen(ss);

}

catch(Exception e){

e.printStackTrace();

}

finally{

if(ss!=null){

try{

ss.close();

}

catch(IOException e){

// oh, well...

}

}

System.exit(0);

}

}

static void listen(ServerSocket ss) throws Exception{

System.out.println("Ready for connections.");

while(true){

Socket s = ss.accept();

BufferedWriter bw = new BufferedWriter(

new OutputStreamWriter(s.getOutputStream()));

BufferedReader br = new BufferedReader(

new InputStreamReader(s.getInputStream()));

String q = br.readLine();

if(!QUESTION.equals(q)){

throw new RuntimeException("Wrong question: \"" + q + "\"");

}

System.out.println("Question: \"" + q + "\"");

bw.write(ANSWER+"\n");

bw.flush();

s.close();

}

}

}

ClearClient::

import java.io.*;

import java.net.*;

public class ClearClient implements Runnable{

public static final int PORT = 5555;

public static final String HOST = "localhost";

public static final String KEYSTORE_FILE = "client_keystore";

public static final String ALGORITHM = "sunx509";

public static final String PASSWORD = "churchillobjects";

public static final String QUESTION = "Knock, knock.";

public static final String ANSWER = "Who's there?";

public static void main(String[] args){

new Thread(new ClearClient()).start();

}

public void run(){

Socket socket = null;

try{

socket = new Socket(HOST, PORT);

doQuery(socket);

}

catch(Exception e){

e.printStackTrace();

}

finally{

if(socket!=null){

try{

socket.close();

}

catch(IOException e){

// oh, well...

}

}

System.exit(0);

}

}

private void doQuery(Socket s) throws Exception{

BufferedWriter bw = new BufferedWriter(

new OutputStreamWriter(s.getOutputStream()));

BufferedReader br = new BufferedReader(

new InputStreamReader(s.getInputStream()));

bw.write(QUESTION+"\n");

bw.flush();

String response = br.readLine();

if(!ANSWER.equals(response)){

throw new RuntimeException("Wrong answer: \"" + response + "\"");

}

System.out.println("Got the right answer: \"" + response + "\"");

}

}

Both the ClearServer and ClearServer code is working perfectly::

Thanks in advance

Regards

Shalini

shalini99a at 2007-7-8 22:33:46 > top of Java-index,Core,Core APIs...