JsseSSLSupport.java :  » Net » SkunkDAV » HTTPClient » jsse » Java Open Source

Java Open Source » Net » SkunkDAV 
SkunkDAV » HTTPClient » jsse » JsseSSLSupport.java
/*
 *  JsseSSLSupport.java           
 *
 *  Copyright (C) 2000 Jacob Smullyan
 *
 *  This file is a supplement to the HTTPClient package by Ronald Tschalr,
 *  Copyright (C) 1996-1999  Ronald Tschalr, and the same license holds.  It
 *  is based on a jsse patch by RT.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA 02111-1307, USA
 *
 *  For questions, suggestions, bug-reports, enhancement-requests etc.
 *  I may be contacted at:
 *
 *  ronald@innovation.ch
 *
 */
package HTTPClient.jsse;

import HTTPClient.SSLSupport;
import java.io.IOException;
import java.net.Socket;
import java.lang.reflect.Method;
import java.security.Provider;
import java.security.Security;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

/**
 * A wrapper which helps to the HTTPClient package for
 * SSL support with Sun's JSSE while making it possible 
 * use the patched client, sans SSL capability, without 
 * the JSSE jars.
 */
public final class JsseSSLSupport extends SSLSupport
{
    public static final String SSL_PROVIDER_CLASS="com.sun.net.ssl.internal.ssl.Provider";

    static
    {
  try
  {
      Provider provider=(Provider) Class.forName(SSL_PROVIDER_CLASS).newInstance();
      Security.addProvider(provider);
  }
  catch (Exception e)
  {
      e.printStackTrace();
  }
    }

    public Socket createSocket(Socket sock, String host, int port)
  throws IOException
    {
  Socket socket=((SSLSocketFactory)SSLSocketFactory.getDefault()).createSocket(sock, host, port, true);
  //enable all supported cipher suites
  configureCipherSuites(socket);
  checkCertificate(socket, host);
  return socket;
    }

    private void configureCipherSuites(Socket s)
    {
  if (s instanceof SSLSocket)
  {
      SSLSocket sock=(SSLSocket)s;      
      String[] supported=sock.getSupportedCipherSuites();
      sock.setEnabledCipherSuites(supported);
  }
    }

    private void checkCertificate(Socket s, String host)
    {
  try
  {
      if (!((SSLSocket) s).getSession().getPeerHost().equals(host))
      {
    System.out.println("host does not match");
      }

  }
  catch (Exception e)
  {
      e.printStackTrace();
  }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.