Note: Read the Code example disclaimer for important legal information.
//////////////////////////////////////////////////////////////////////////////////
//
// The following server program utilizes an SSLContext object that it
// initializes with a previously created keystore file.
//
// The keystore file has the following name and keystore password:
// File name: /home/keystore.file
// Password: password
//
// The example program needs the keystore file in order to create an
// IbmISeriesKeyStore object. The KeyStore object must specify MY_SERVER_APP as
// the application identifier.
//
// To create the keystore file, you can use the following Qshell command:
//
// java com.ibm.as400.SSLConfiguration -create -keystore /home/keystore.file
// -storepass password -appid MY_SERVER_APP
//
// Command syntax:
// java -Djava.version=1.4 JavaSslServer
//
// Note that "-Djava.version=1.4" is unnecessary when you have configured
// J2SDK version 1. to be used by default.
//
//////////////////////////////////////////////////////////////////////////////////
import java.io.*;
import javax.net.ssl.*;
/**
* Java SSL Server Program using Application ID.
*/
public class JavaSslServer {
/**
* JavaSslServer main method.
*
* @param args the command line arguments (not used)
*/
public static void main(String args[]) {
/*
* Set up to catch any exceptions thrown.
*/
try {
/*
* Allocate and initialize a KeyStore object.
*/
Char[] password = "password".toCharArray();
KeyStore ks = KeyStore.getInstance("IbmISeriesKeyStore");
FileInputStream fis = new FileInputStream("/home/keystore.file"
Ks.load(fis, password);
/*
* Allocate and initialize a KeyManagerFactory.
*/
KeyManagerFactory kmf =
KeyManagerFactory.getInstance("IbmISeriesX509");
Kmf.init(ks, password);
/*
* Allocate and initialize a TrustManagerFactory.
*/
TrustManagerFactory tmf =
TrustManagerFactory.getInstance("IbmISeriesX509");
tmf.init(ks);
/*
* Allocate and initialize an SSLContext.
*/
SSLContext c =
SSLContext.getInstance("SSL", "IbmISeriesSslProvider");
C.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
/*
* Get the an SSLServerSocketFactory from the SSLContext.
*/
SSLServerSocketFactory sf = c.getSSLServerSocketFactory();
/*
* Create an SSLServerSocket.
*/
SSLServerSocket ss =
(SSLServerSocket) sf.createServerSocket(13333);
/*
* Perform an accept() to create an SSLSocket.
*/
SSLSocket s = (SSLSocket) ss.accept();
/*
* Receive a message from the client using the secure session.
*/
InputStream is = s.getInputStream();
byte[] buffer = new byte[1024];
int bytesRead = is.read(buffer);
if (bytesRead == -1)
throw new IOException("Unexpected End-of-file Received");
String received = new String(buffer, 0, bytesRead);
/*
* Write results to screen.
*/
System.out.println("Read " + received.length() + " bytes...");
System.out.println(received);
/*
* Echo the message back to the client using the secure session.
*/
OutputStream os = s.getOutputStream();
os.write(received.getBytes());
/*
* Write results to screen.
*/
System.out.println("Wrote " + received.length() + " bytes...");
System.out.println(received);
} catch (Exception e) {
System.out.println("Unexpected exception caught: " +
e.getMessage());
e.printStackTrace();
}
}
}