Using the JSSE API

The toolkit implements the Java Secure Sockets Extension (JSSE) with cryptographic capabilities, full key management support, and RFC5280 support. See below for how to install the toolkit's JSSE provider and create an SSL socket.

Logging in the user

Instantiate and log in a user.

FileInputStream credentials = new FileInputStream (<credentials_location>);
SecureStringBuffer password = new SecureStringBuffer(new StringBuffer(<user's password>));
User user = new User();
if (<IP address> != null)
{
JNDIDirectory dir = new JNDIDirectory (<ip>, <port>);
user.setConnections(dir, null);
}
CredentialReader credReader = new StreamProfileReader(credentials);
user.login(credReader, password);

Creating an SSLContext

Create an SSLContext object that implements the TLS protocol from the toolkit's provider.

SSLContext context = SSLContext.getInstance("TLS");

Creating the key manager

Generate and initialize a KeyManagerFactory instance to implement the SunX509 key management algorithm.

KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
EntrustKeyStore eks = new EntrustKeyStore(user, true);
eks.load(null,null);
kmf.init(eks, pw.toCharArray());

Creating the trust manager

Create and initialize a TrustManagerFactory object and initialize it with the Entrust key store.

TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(eks);

Initializing the SSL context

Initialize the SSLContext object using the key and trust managers.

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

Creating an SSL socket

Create an SSL socket.

SSLSocket socket = (SSLSocket)
context.getSocketFactory().createSocket(<IP address of host>, <port number>);

Use the socket to send and receive encrypted data as you would normally. This simple code fragment sends a message and writes the response to the standard output stream for the system.

PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
out.println("GET / HTTP/1.0");
out.println();
out.flush();
InputStream is = socket.getInputStream();
int r = is.read();
while(r!=-1)
{
System.out.print((char)r);
r=is.read();
}