tn5250j/src/org/tn5250j/framework/transport/SSL X509CertificateTrustManager.java,1.1,1.2 SSLImplementation.java,1.1,1.2

Steve Kennedy <[email protected]> Fri, 25 Feb 2005 09:12:06 +0000
Newsgroups gmane.comp.java.tn5250j.cvs
Message-ID <[email protected]>
Update of /cvsroot/tn5250j/tn5250j/src/org/tn5250j/framework/transport/SSL
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6828/src/org/tn5250j/framework/transport/SSL

Modified Files:
	X509CertificateTrustManager.java SSLImplementation.java 
Log Message:
SSL Updates

Index: X509CertificateTrustManager.java
===================================================================
RCS file: /cvsroot/tn5250j/tn5250j/src/org/tn5250j/framework/transport/SSL/X509CertificateTrustManager.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** X509CertificateTrustManager.java	1 Aug 2004 14:36:28 -0000	1.1
--- X509CertificateTrustManager.java	25 Feb 2005 09:12:03 -0000	1.2
***************
*** 28,31 ****
--- 28,40 ----
  import javax.swing.JOptionPane;
  
+ /**
+  * This class is used to trust certificates exchanged during an SSL socket
+  * handshake.  It allows the user to accept the certificate so that connections
+  * can be made without requiring the server to have a certificate signed by a
+  * CA (Verisign, Thawte, etc.).
+  *  
+  * @author Stephen M. Kennedy <[email protected]>
+  *
+  */
  public class X509CertificateTrustManager implements X509TrustManager {
  
***************
*** 38,45 ****
    }
  
!   public void checkClientTrusted(X509Certificate[] chain, String type) throws java.security.cert.CertificateException {
! 
    }
!   public void checkServerTrusted(X509Certificate[] chain, String type) throws java.security.cert.CertificateException {
      try {
        trustManager.checkServerTrusted(chain,type);
--- 47,61 ----
    }
  
!   public void checkClientTrusted(X509Certificate[] chain, String type) throws CertificateException {
!   	throw new SecurityException("checkClientTrusted unsupported");
    }
!   
!   
!   /**
!    * Checks the server certificate.  If it isn't trusted by the trust manager
!    * passed to the constructor, then the user will be prompted to accept the
!    * certificate.
!    */
!   public void checkServerTrusted(X509Certificate[] chain, String type) throws CertificateException {
      try {
        trustManager.checkServerTrusted(chain,type);

Index: SSLImplementation.java
===================================================================
RCS file: /cvsroot/tn5250j/tn5250j/src/org/tn5250j/framework/transport/SSL/SSLImplementation.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** SSLImplementation.java	1 Aug 2004 14:36:28 -0000	1.1
--- SSLImplementation.java	25 Feb 2005 09:12:04 -0000	1.2
***************
*** 3,7 ****
  /*
   * @(#)SSLImplementation.java
!  * @author Steve Kennedy
   *
   * Copyright:    Copyright (c) 2001
--- 3,7 ----
  /*
   * @(#)SSLImplementation.java
!  * @author Stephen M. Kennedy
   *
   * Copyright:    Copyright (c) 2001
***************
*** 31,42 ****
  import javax.net.ssl.X509TrustManager;
  import javax.net.ssl.TrustManagerFactory;
- import javax.net.ssl.HandshakeCompletedListener;
- import javax.net.ssl.HandshakeCompletedEvent;
  
  import java.security.KeyStore;
  import java.security.SecureRandom;
  import org.tn5250j.framework.transport.SSLInterface;
- import org.tn5250j.framework.transport.SSLConstants;
  
  public class SSLImplementation implements SSLInterface {
  
--- 31,54 ----
  import javax.net.ssl.X509TrustManager;
  import javax.net.ssl.TrustManagerFactory;
  
  import java.security.KeyStore;
  import java.security.SecureRandom;
  import org.tn5250j.framework.transport.SSLInterface;
  
+ import org.tn5250j.tools.logging.TN5250jLogFactory;
+ import org.tn5250j.tools.logging.TN5250jLogger;
+ 
+ /**
+  * <p>
+  * This class implements the SSLInterface and is used to create SSL socket
+  * instances.
+  * </p>
+  * <p>
+  * This class uses an X509CertificateTrustManager instance to perform
+  * certificate validation during handshaking.
+  * </p> 
+  * @author Stephen M. Kennedy <[email protected]>
+  *
+  */
  public class SSLImplementation implements SSLInterface {
  
***************
*** 49,58 ****
    String sslType = null;
  
    public SSLImplementation () {
! 
    }
  
    public SSLImplementation (String sslType) {
! 
      this.sslType = sslType;
    }
--- 61,74 ----
    String sslType = null;
  
+   private char[] keystorePassword = "changeit".toCharArray();
+   
+   TN5250jLogger logger;
+   
    public SSLImplementation () {
!   	logger = TN5250jLogFactory.getLogger(getClass());
    }
  
    public SSLImplementation (String sslType) {
!   	this();
      this.sslType = sslType;
    }
***************
*** 62,94 ****
    }
  
    private void initKeyStore () {
      try {
!       ks = KeyStore.getInstance("JKS");
!       System.out.println("Loading Keystore...");
        String seperator=System.getProperty("file.separator","/");
        ks.load(new java.io.FileInputStream(System.getProperty("java.home")+
                  seperator+"lib"+seperator+"security"+seperator+"cacerts"),
!                 "changeit".toCharArray());
      }
      catch (Exception e) {
!       System.err.println("MySSLFactory: " + e.getMessage());
      }
    }
  
    private void initKeyManagerFactory() {
      try {
!       System.out.println("Initializing KeyManagerFactory...");
!       kmf = KeyManagerFactory.getInstance("SunX509");
!       kmf.init(ks,"changeit".toCharArray());
      }
      catch (Exception e) {
!       System.err.println("MySSLFactory: " + e.getMessage());
      }
    }
  
    private void initTrustManagers() {
      try {
!       System.out.println("Instantiating TrustManager...");
!       tmf = TrustManagerFactory.getInstance("SunX509");
        tmf.init(ks);
        trustManagers = tmf.getTrustManagers();
--- 78,126 ----
    }
  
+   /**
+    * Initialize the keystore where certificates are loaded from and stored to.
+    *
+    */
    private void initKeyStore () {
      try {
!       ks = KeyStore.getInstance(KeyStore.getDefaultType());
!       logger.debug("Loading Keystore...");
!       
        String seperator=System.getProperty("file.separator","/");
+       
        ks.load(new java.io.FileInputStream(System.getProperty("java.home")+
                  seperator+"lib"+seperator+"security"+seperator+"cacerts"),
!                 keystorePassword);
!       
      }
      catch (Exception e) {
!     	logger.error("Failed Initializing Keystore ["+e.getMessage()+"]");
      }
    }
  
+   
+   /**
+    * Initialize the key manager factory  
+    *
+    */
    private void initKeyManagerFactory() {
      try {
!     	logger.debug("Initializing KeyManagerFactory...");
!       kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
!       kmf.init(ks,keystorePassword);
      }
      catch (Exception e) {
!     	logger.error("Failed initializing Key Manager Factory ["+e.getMessage()+"]");
      }
    }
  
+   /**
+    * Initialize the trust managers
+    *
+    */
    private void initTrustManagers() {
      try {
!     	logger.debug("Instantiating TrustManager...");
!       tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(ks);
        trustManagers = tmf.getTrustManagers();
***************
*** 100,110 ****
      }
      catch (Exception e) {
!       System.err.println("My5250SocketFactory: initTrustManager: " +
!         e.getMessage());
      }
    }
  
    private void initPrng() {
!     System.out.println("Initializing PRNG...");
      SecureRandom prng = new SecureRandom();
      prng.nextInt();
--- 132,142 ----
      }
      catch (Exception e) {
!     	logger.error("Failed initializing Trust Managers ["+e.getMessage()+"]");
      }
    }
  
+   
    private void initPrng() {
!     logger.debug("Initializing PRNG...");
      SecureRandom prng = new SecureRandom();
      prng.nextInt();
***************
*** 113,122 ****
    private void initSSLContext(String type) {
      try {
!       System.out.println("Creating and Initializing SSL Context...");
        sslctx = SSLContext.getInstance(type);
        sslctx.init(kmf.getKeyManagers(),trustManagers,prng);
      }
      catch (Exception e) {
!       System.err.println("MySSLFactory: " + e.getMessage());
      }
  
--- 145,154 ----
    private void initSSLContext(String type) {
      try {
!     	logger.debug("Creating and Initializing SSL Context...");
        sslctx = SSLContext.getInstance(type);
        sslctx.init(kmf.getKeyManagers(),trustManagers,prng);
      }
      catch (Exception e) {
!     	logger.error("Failed initializing SSL Context ["+e.getMessage()+"]");
      }
  
***************
*** 124,129 ****
  
    public Socket createSSLSocket(String destination, int port) {
      try {
- 
        //Using SSL Socket
        initKeyStore();
--- 156,161 ----
  
    public Socket createSSLSocket(String destination, int port) {
+   	SSLSocket socket = null;
      try {
        //Using SSL Socket
        initKeyStore();
***************
*** 131,157 ****
        initTrustManagers();
        initPrng();
!       System.out.println("Creating Secure Socket");
!       if (sslType.equals(SSLConstants.SSL_TYPE_SSLv2)) {
!         initSSLContext("SSL");
!       }
!       else if (sslType.equals(SSLConstants.SSL_TYPE_TLS)) {
!         initSSLContext("TLS");
!       }
!       else {
!         System.err.println("SSL Type not Supported");
!         return null;
!       }
!       SSLSocket sslsock = (SSLSocket)sslctx.getSocketFactory().createSocket(destination,port);
!         sslsock.addHandshakeCompletedListener(new HandshakeCompletedListener() {
!           public void handshakeCompleted(HandshakeCompletedEvent hsce) {
!             System.out.println("Handshake Successful: " + hsce.getCipherSuite());
!           }
!       });
!       return sslsock;
      }
      catch (Exception e) {
!       System.err.println("MySSLFactory: createSocket: " + e.getMessage());
      }
!     return null;
    }
  }
\ No newline at end of file
--- 163,173 ----
        initTrustManagers();
        initPrng();
!       initSSLContext(sslType);
!       socket = (SSLSocket)sslctx.getSocketFactory().createSocket(destination,port);
      }
      catch (Exception e) {
!     	logger.error("Error creating ssl socket ["+e.getMessage()+"]");
      }
!     return socket;
    }
  }
\ No newline at end of file



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click