The Source for Java Technology Collaboration
User: Password:



Evan Summers

Evan Summers's Blog

Java SSL Sockets

Posted by evanx on May 28, 2007 at 10:45 PM | Comments (0)

Let's build a trivial client/server demo using SSLServerSocket and SSLSocket as provided by the Java Secure Socket Extension (JSSE). We create a self-signed public key certificate for the server using keytool, and install this on the client.


Code Snippet

We implement the client as follows.

public class EnigmaClient extends Thread {
    EnigmaSocket enigmaSocket;
    KeyStore keyStore;
    KeyManager[] keyManagers;
    TrustManager[] trustManagers;
    SSLContext sslContext;
    
    public void init() throws Exception {
        initKeyManagers();
        initTrustManagers();
        initSSLContext();
    }
    
    public void connect(String host, int port) throws Exception {
        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        Socket clientSocket = sslSocketFactory.createSocket(host, port);
        this.enigmaSocket = new EnigmaSocket(clientSocket);
    }
    ...
}

where the keystore containing the server's digital certificate eg. created using keytool, is loaded from a resource or file, and a KeyManagerFactory and TrustManagerFactory are initialised with the keyStore instance. Finally, an SSLContext is initialised with key managers and trust managers, and this is used to create SSL socket connections.



Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds