2
votes

I am creating a SSL Server and Client in Java. The point of the program is to mimic a movie theater program. I can establish the connection but when I attempt to "reserve" a seat the program crashes. I get the following error:

Server aborted: javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

This is my Server Code

// SSL Server
import java.net.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.net.ServerSocketFactory;
import javax.net.ssl.SSLServerSocketFactory;

public class SSL_Server {


public static void main(String[] args) {
    int port = 2018;

    System.setProperty("javax.net.ssl.keyStore","mySrvKeystore");
    System.setProperty("javax.net.ssl.keyStorePassword","123456");
    ServerSocketFactory ssocketFactory = SSLServerSocketFactory.getDefault();
    ServerSocket ssocket = null;
    System.out.println("SSL_Server started");

    final ExecutorService threadPool = Executors.newCachedThreadPool();

    try {
        ssocket = ssocketFactory.createServerSocket(port);
        InetAddress myIP =InetAddress.getLocalHost();
        System.out.println(myIP.getHostAddress());

        while(true){
            Socket aClient = ssocket.accept();
            //create a new thread for every client
            threadPool.submit(new SSL_ClientHandler(aClient));
        } 

    } 
    catch(Exception e) {
        System.err.println("Server aborted:" + e);
    } finally {
        try{
            ssocket.close();
        } catch (Exception e){
            System.err.println("could not close connection properly" + e);
        }
    }
    System.out.println("connection was closed successfully");
}
}

The following is my client code

//SSL Client
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.net.ServerSocketFactory;
import javax.net.SocketFactory;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocketFactory;

public class TCP_Client {


public static void main(String[] args) throws IOException{
//  SSL_Client newClient = new SSL_Client();
//  Lock lock = new ReentrantLock();
    boolean validInput = false;

    BufferedReader din;
    PrintStream pout;

    int port = 2018;
    BufferedReader stdinp = new BufferedReader(new InputStreamReader(System.in));
    String line = "done";
    StringTokenizer st;
    String hostname; 
    String task = "done";


    if(args.length>0)
        hostname = args[0];
    else
        hostname = "localhost";

    SocketFactory socketFactory = SSLSocketFactory.getDefault();
    //Socket socket = socketFactory.createSocket(hostname, port);

    while(true)
    {
        try{
            //read input
            while(!validInput)
            {
                System.out.println("Please enter a valid command or 'done' to finish.");
                line = stdinp.readLine();
                st = new StringTokenizer(line);
                task = st.nextToken();
                if(task.equals("reserve") || task.equals("search") || task.equals("delete") || task.equals("getinfo") || task.equals("done"))
                {
                    validInput =true;
                    break;
                }
                System.out.println("Invalid command.  Please enter another command or 'done' to escape.");
            }
            if(task.equals("done"))
            {
                break;
            }
            validInput = false;//reset for next line read in

            //create a new socket every time
            //Socket socket = new Socket(hostname, port);

            Socket socket = socketFactory.createSocket(hostname, port);
            din = new BufferedReader (new InputStreamReader    (socket.getInputStream()));
            pout = new PrintStream (socket.getOutputStream());

            pout.println(line);
            pout.flush();

            //print out response from server
            System.out.println(din.readLine());

        } catch (Exception e){
            System.err.println("Server aborted: " + e);
        }
    }   
}
}
1

1 Answers

3
votes

"Unable to find valid certification path to requested target" means that your truststore doesn't trust the server certificate. Import it into your truststore, or have it signed by a recognized CA.