Some Secure Code


The Calls


$JAVA_HOME/bin/java -Djavax.net.ssl.trustStore=server.ks -Djavax.net.ssl.trustStoreType=JKS -Djavax.net.ssl.keyStore=server.ks -Djavax.net.ssl.keyStoreType=JKS -Djavax.net.ssl.keyStorePassword=changeit -Djavax.net.ssl.TrustStorePassword=changeit BobtheServer


$JAVA_HOME/bin/java -Djavax.net.ssl.trustStore=client.ks -Djavax.net.ssl.trustStoreType=JKS -Djavax.net.ssl.keyStore=client.ks -Djavax.net.ssl.keyStoreType=JKS -Djavax.net.ssl.keyStorePassword=changeit -Djavax.net.ssl.TrustStorePassword=changeit AlicetheClient


The Code


BobtheServer

/*
 * @(#) $Id: EchoServer.java,v 1.1 2003/10/25 17:51:05 pankaj_kumar Exp $
 *
 * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net).
 * All rights reserved.
 *
 * The license governing the use of this file can be found in the
 * root directory of the containing software.
 */
 
 /*
 This software is copied and was modified under the terms of the above copyright, in particular Open Source License v. 2.0. The
full text of this license can be found at http://www.opensource.org/licenses/osl-2.0.txt.
The sole purpose of the modification is to present an in-class example . 
 */

import javax.net.ServerSocketFactory;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLServerSocket;
import java.net.ServerSocket;
import java.net.Socket;

public class BobtheServer {
       
	public;static void main(String[] args) throws Exception {
		ServerSocketFactory ssf = SSLServerSocketFactory.getDefault();
		ServerSocket ss = ssf.createServerSocket(2952);

		if (args.length > 0){
			SSLServerSocket sss = (SSLServerSocket)ss;
			if ("-needClientAuth".equalsIgnoreCase(args[0])){
				sss.setNeedClientAuth(true);
			} else if ("-wantClientAuth".equalsIgnoreCase(args[0])){
				sss.setWantClientAuth(true);
			}
		}
        int nread = 0;
                byte[] buf = new byte[1024];
                byte[] hi ={'H','i',' ','A','l','i','c','e','.'};
                byte[] huh= {'W','h','o',' ','i','s',' ','i','t','?'};
                boolean stopit =false;
		while (!stopit){
                        stopit=true;
			System.out.print("Waiting for connection ...");
   			System.out.flush();
			Socket socket = ss.accept();
			System.out.println(" ... Connection accepted.");
			java.io.InputStream is = socket.getInputStream();
			java.io.OutputStream os = socket.getOutputStream();
			while ((nread = is.read(buf)) != -1){
                            if(new String(buf,0,5).equals ("Alice"))
                                  os.write(hi,0,9);
                            else
                                 os.write(huh,0,10);
                           
			} // inner while
		} // while (true)
                ss.close();
	} // main()
}

AlicetheClient


/*
 * @(#) $Id: EchoClient.java,v 1.1 2003/10/25 17:51:05 pankaj_kumar Exp $
 *
 * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net).
 * All rights reserved.
 *
 * The license governing the use of this file can be found in the
 * root directory of the containing software.
 */
 
 /*
 This software is copied and was modified under the terms of the above copyright, in particular Open Source License v. 2.0. The
full text of this license can be found at http://www.opensource.org/licenses/osl-2.0.txt.
The sole purpose of the modification is to present an in-class example . 
 */
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.SSLSocket;
import java.net.ServerSocket;
import java.net.Socket;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class AlicetheClient {
	public static void main(String[] args) throws Exception {
		String hostname = "localhost";
		if (args.length > 0)
			hostname = args[0];
		SocketFactory sf = SSLSocketFactory.getDefault();
		Socket socket = sf.createSocket(hostname, 2952);
		System.out.println("Connection established.");

		java.io.InputStream is = socket.getInputStream();
		java.io.OutputStream os = socket.getOutputStream();
		byte[] buf = new byte[1024];
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		while (true){
			System.out.print("Enter Message (Type \"quit\" to exit): ");
			System.out.flush();
			String inp = br.readLine();
			if (inp.equalsIgnoreCase("quit"))
				break;
			os.write(inp.getBytes());
			int n = is.read(buf);
			System.out.println("Server Returned: " + new String(buf, 0, n));
		} // while (true)
		socket.close();
		System.out.println("Connection closed.");
	} // main()
}

sslclient.java -no command line arguements



import javax.net.ssl.*;
import java.io.*;
import java.net.*;
import javax.net.*;
import java.security.*;
import javax.security.cert.X509Certificate;

public class sslclient {

    public static void main(String[] args){
        int port = 443;
        String host = "louie.umsl.edu";
        SSLSocketFactory factory=null;

        try {
            try {
                SSLContext ctx;
                KeyManagerFactory kmf;
                TrustManagerFactory tmf;
                KeyStore ks;
                char[] passphrase = "changeit".toCharArray();

                ctx = SSLContext.getInstance("TLS");
                kmf = KeyManagerFactory.getInstance("SunX509");
                tmf = TrustManagerFactory.getInstance("SunX509");
                ks = KeyStore.getInstance("JKS");

                ks.load(new FileInputStream("client.ks"), passphrase);

                kmf.init(ks, passphrase);
                tmf.init(ks);
                ctx.init(kmf.getKeyManagers(),tmf.getTrustManagers(), null);

                factory = ctx.getSocketFactory();
            } catch (Exception e) {
                throw new IOException(e.getMessage());
            }

            SSLSocket c =
            (SSLSocket)factory.createSocket(host, port);
            BufferedReader inStream=new BufferedReader(new InputStreamReader(c.getInputStream()));
            PrintWriter outStream=new PrintWriter(c.getOutputStream());
            outStream.println("Possible Intrusion from:  "+args[0]+" At: "+args[1]);
            outStream.flush ();
        }

        catch (IOException e) {
        }
    }
}