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

"Kenneth J. Pouncey" <[email protected]> Sun, 01 Aug 2004 14:36:30 +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-serv6573/src/org/tn5250j/framework/transport/SSL

Added Files:
	X509CertificateTrustManager.java SSLImplementation.java 
Log Message:
Move transport to framework package

--- NEW FILE: X509CertificateTrustManager.java ---
package org.tn5250j.framework.transport.SSL;

/*
 * @(#)X509CertificateTrustManager.java
 *
 * Copyright:    Copyright (c) 2001
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA
 *
 */
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import javax.swing.JOptionPane;

public class X509CertificateTrustManager implements X509TrustManager {

  KeyStore ks = null;
  X509TrustManager trustManager = null;

  public X509CertificateTrustManager(X509TrustManager manager, KeyStore keyStore) {
    trustManager = manager;
    ks = keyStore;
  }

  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);
      return;
    }
    catch (CertificateException ce) {
      X509Certificate cert = chain[0];
      String certInfo = "Version: " + cert.getVersion() + "\n";
      certInfo = certInfo.concat("Serial Number: " + cert.getSerialNumber()+"\n");
      certInfo = certInfo.concat("Signature Algorithm: " + cert.getSigAlgName()+"\n");
      certInfo = certInfo.concat("Issuer: " + cert.getIssuerDN().getName()+"\n");
      certInfo = certInfo.concat("Valid From: " + cert.getNotBefore()+"\n");
      certInfo = certInfo.concat("Valid To: " + cert.getNotAfter()+"\n");
      certInfo = certInfo.concat("Subject DN: " + cert.getSubjectDN().getName()+"\n");
      certInfo = certInfo.concat("Public Key: " + cert.getPublicKey().getFormat()+"\n");

      int accept = JOptionPane.showConfirmDialog(null,certInfo,
                  "Accept Certificate",javax.swing.JOptionPane.YES_NO_OPTION);
      if (accept != JOptionPane.YES_OPTION) {
        throw new java.security.cert.CertificateException("Certificate Not Accepted");
      }
    }
  }

  public X509Certificate[] getAcceptedIssuers() {
    return trustManager.getAcceptedIssuers();
  }
}
--- NEW FILE: SSLImplementation.java ---
package org.tn5250j.framework.transport.SSL;

/*
 * @(#)SSLImplementation.java
 * @author Steve Kennedy
 *
 * Copyright:    Copyright (c) 2001
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA
 *
 */

import java.net.Socket;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLContext;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManager;
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 {

  SSLContext sslctx = null;
  KeyStore ks = null;
  KeyManagerFactory kmf = null;
  SecureRandom prng = null;
  TrustManagerFactory tmf = null;
  TrustManager[] trustManagers = null;
  String sslType = null;

  public SSLImplementation () {

  }

  public SSLImplementation (String sslType) {

    this.sslType = sslType;
  }

  public void setSSLType(String type) {
    sslType = type;
  }

  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();
      X509TrustManager myTrustManager = new X509CertificateTrustManager(
          ((X509TrustManager)trustManagers[0]),ks);
      TrustManager[] newTrustManagers = new TrustManager[1];
      newTrustManagers[0] = myTrustManager;
      trustManagers = newTrustManagers;
    }
    catch (Exception e) {
      System.err.println("My5250SocketFactory: initTrustManager: " +
        e.getMessage());
    }
  }

  private void initPrng() {
    System.out.println("Initializing PRNG...");
    SecureRandom prng = new SecureRandom();
    prng.nextInt();
  }

  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());
    }

  }

  public Socket createSSLSocket(String destination, int port) {
    try {

      //Using SSL Socket
      initKeyStore();
      initKeyManagerFactory();
      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;
  }
}


-------------------------------------------------------
This SF.Net email is sponsored by OSTG. Have you noticed the changes on
Linux.com, ITManagersJournal and NewsForge in the past few weeks? Now,
one more big change to announce. We are now OSTG- Open Source Technology
Group. Come see the changes on the new OSTG site. www.ostg.com