The Java Secure Socket Extensions 
by Kirby W. Angell

Listing One
static void sendData( Socket server, String cc, String amnt ) {
    try { 
        BufferedWriter out = new BufferedWriter( 
            new OutputStreamWriter( server.getOutputStream() ) );
        BufferedReader in = new BufferedReader( 
            new InputStreamReader( server.getInputStream() ) );
        boolean success = false;
        String s = in.readLine();
        if ( s.equals( "HELO CC-SERVER" ) ) {
            System.out.println( "Connected to server." );
            println( out, "HELO CC-CLIENT" );
            s = in.readLine();
            if ( s.equals( "READY" ) ) {
                System.out.println( "Server ready." );
                println( out, cc + "\t" + amnt );
                s = in.readLine();
                if ( s.equals( "OK" ) ) {
                    success = true;
                } // if
            } // if
        } // if
        if ( success ) 
            System.out.println( "Data sent." );
        else
            System.out.println( "Protocal error. -- " + s );
        server.close();
    } catch ( Exception e ) {
            System.out.println( "Error: " + e.getMessage() );
            e.printStackTrace();
    } // catch
} // send data

Listing Two
class ClientProcessor implements Runnable {
    Socket socket = null;
    ClientProcessor( Socket client ) {
        socket = client;
    } // constructor
    public void run() {
        try {
            BufferedWriter out = new BufferedWriter( 
                new OutputStreamWriter( socket.getOutputStream() ) );
            BufferedReader in = new BufferedReader( 
                new InputStreamReader( socket.getInputStream() ) );
            println( out, "HELO CC-SERVER" );
            String s = in.readLine();
            if ( s.equals( "HELO CC-CLIENT" ) ) {
                System.out.println( "Client connected." );
                println( out, "READY" );
                s = in.readLine();
                writeTransaction( s );
                System.out.println( "Credit card authorized." );
                println( out, "OK" );
            } else {
                println( out, "IHATEYOU" );
            } // else
        } catch ( Exception e ) {
            System.out.println("Error: " + e.getMessage());
            e.printStackTrace();
        } // catch
        try {
            socket.close();
        } catch ( Exception ex ) {}
    } // run
   void writeTransaction( String s ) throws IOException {
        FileWriter fw = new FileWriter( "transactions.log", true );
        PrintWriter out = new PrintWriter( fw, true );
        out.println( s );
        out.flush();
        out.close();
        fw.close();
    } // writeTransaction
    static void println( BufferedWriter bw, String s ) throws IOException {
        bw.write( s );
        bw.newLine();
        bw.flush();
    } // println
} // ClientProcessor


Listing Three
public static void main( String[] args ) throws Exception {
    String type = args[0];
    String host = args[1];
    String cc = args[2];
    String amnt = args[3];
    Socket socket = null;
1:  if ( type.equals( "TLS" ) ) {
        SSLSocketFactory factory = null;
2:      SSLContext ctx = SSLContext.getInstance( "TLS" );
3:      KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
4:      KeyStore ks = KeyStore.getInstance( "JKS") ;
5:      char[] passphrase = "passphrase".toCharArray();
6:      ks.load(new FileInputStream("testkeys"), passphrase);
7:      kmf.init(ks, passphrase);
8:      TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
9:      tmf.init( ks );
10:     ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
11:     factory = ctx.getSocketFactory();
12:     socket = factory.createSocket(host, DEFAULT_PORT);
    } else {
13:     socket = new Socket( host, DEFAULT_PORT );
    } // else
14: sendData( socket, cc, amnt );
} // main


Listing Four
public static void main(String args[]) {
   String type = "plain";
    if ( args.length > 0 ) {
        type = args[0];
    } // if
    try {
1:      ServerSocketFactory ssf = server.createServerSocketFactory( type );
2:      ServerSocket ss = ssf.createServerSocket( DEFAULT_PORT );
3:      if ( type.equals( "TLS" ) ) 
4:          ((SSLServerSocket)ss).setNeedClientAuth( true );
5:      acceptConnections( ss );
    } catch (IOException e) {
        System.out.println( "Error: " + e.getMessage() );
        e.printStackTrace();
    } // try
} // main
private static ServerSocketFactory createServerSocketFactory( String type ) {
    if ( type.equals( "TLS" ) ) {
        SSLServerSocketFactory ssf = null;
        try {
            // set up key manager to do server authentication
6:          KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
7:          KeyStore ks = KeyStore.getInstance( "JKS" );
8:          char[] passphrase = "passphrase".toCharArray();
9:          ks.load(new FileInputStream("testkeys"), passphrase);
10:         kmf.init(ks, passphrase);
11:         TrustManagerFactory tmf = 
                 TrustManagerFactory.getInstance("SunX509");
12:         tmf.init( ks );
13:         SSLContext ctx = SSLContext.getInstance( "TLS" );
14:         ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
15:         ssf = ctx.getServerSocketFactory();
16:         return ssf;
        } catch (Exception e) {
            System.out.println( "Error: " + e.getMessage() );
            e.printStackTrace();
        } // try
    } else {
        return ServerSocketFactory.getDefault();
    } // if
    return null;
} // createServerSocketFactory
static void acceptConnections( ServerSocket server ) {
17: Socket socket = null;
    while( true ) {
        // accept a connection
        try {
18:         socket = server.accept();
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
            e.printStackTrace();
           return;
        } // catch
        // create a new thread to accept the next connection
19:     ClientProcessor cp = new ClientProcessor( socket );
20:     (new Thread( cp )).start();
    } // while
} // run





3

