SSLSocketFactory.java revision 0
0N/A/*
2362N/A * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
2362N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2362N/A *
2362N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/A
0N/Apackage javax.net.ssl;
0N/A
0N/Aimport java.net.*;
0N/Aimport javax.net.SocketFactory;
0N/Aimport java.io.IOException;
0N/Aimport java.security.*;
0N/A
0N/Aimport sun.security.action.GetPropertyAction;
0N/A
0N/A/**
0N/A * <code>SSLSocketFactory</code>s create <code>SSLSocket</code>s.
0N/A *
0N/A * @since 1.4
0N/A * @see SSLSocket
0N/A * @author David Brownell
0N/A */
0N/Apublic abstract class SSLSocketFactory extends SocketFactory
0N/A{
0N/A private static SSLSocketFactory theFactory;
0N/A
0N/A private static boolean propertyChecked;
0N/A
0N/A static final boolean DEBUG;
0N/A
0N/A static {
0N/A String s = java.security.AccessController.doPrivileged(
0N/A new GetPropertyAction("javax.net.debug", "")).toLowerCase();
0N/A DEBUG = s.contains("all") || s.contains("ssl");
0N/A }
0N/A
0N/A private static void log(String msg) {
0N/A if (DEBUG) {
0N/A System.out.println(msg);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Constructor is used only by subclasses.
0N/A */
0N/A public SSLSocketFactory() {
0N/A }
0N/A
0N/A /**
0N/A * Returns the default SSL socket factory.
0N/A *
0N/A * <p>The first time this method is called, the security property
0N/A * "ssl.SocketFactory.provider" is examined. If it is non-null, a class by
0N/A * that name is loaded and instantiated. If that is successful and the
0N/A * object is an instance of SSLSocketFactory, it is made the default SSL
0N/A * socket factory.
0N/A *
0N/A * <p>Otherwise, this method returns
0N/A * <code>SSLContext.getDefault().getSocketFactory()</code>. If that
0N/A * call fails, an inoperative factory is returned.
0N/A *
0N/A * @return the default <code>SocketFactory</code>
0N/A * @see SSLContext#getDefault
0N/A */
0N/A public static synchronized SocketFactory getDefault() {
0N/A if (theFactory != null) {
0N/A return theFactory;
0N/A }
0N/A
0N/A if (propertyChecked == false) {
0N/A propertyChecked = true;
0N/A String clsName = getSecurityProperty("ssl.SocketFactory.provider");
0N/A if (clsName != null) {
0N/A log("setting up default SSLSocketFactory");
0N/A try {
0N/A Class cls = null;
0N/A try {
0N/A cls = Class.forName(clsName);
0N/A } catch (ClassNotFoundException e) {
0N/A ClassLoader cl = ClassLoader.getSystemClassLoader();
0N/A if (cl != null) {
0N/A cls = cl.loadClass(clsName);
0N/A }
0N/A }
0N/A log("class " + clsName + " is loaded");
0N/A SSLSocketFactory fac = (SSLSocketFactory)cls.newInstance();
0N/A log("instantiated an instance of class " + clsName);
0N/A theFactory = fac;
0N/A return fac;
0N/A } catch (Exception e) {
0N/A log("SSLSocketFactory instantiation failed: " + e.toString());
0N/A theFactory = new DefaultSSLSocketFactory(e);
0N/A return theFactory;
0N/A }
0N/A }
0N/A }
0N/A
0N/A try {
0N/A return SSLContext.getDefault().getSocketFactory();
0N/A } catch (NoSuchAlgorithmException e) {
0N/A return new DefaultSSLSocketFactory(e);
0N/A }
0N/A }
0N/A
0N/A static String getSecurityProperty(final String name) {
0N/A return AccessController.doPrivileged(new PrivilegedAction<String>() {
0N/A public String run() {
0N/A String s = java.security.Security.getProperty(name);
0N/A if (s != null) {
0N/A s = s.trim();
0N/A if (s.length() == 0) {
0N/A s = null;
0N/A }
0N/A }
0N/A return s;
}
});
}
/**
* Returns the list of cipher suites which are enabled by default.
* Unless a different list is enabled, handshaking on an SSL connection
* will use one of these cipher suites. The minimum quality of service
* for these defaults requires confidentiality protection and server
* authentication (that is, no anonymous cipher suites).
*
* @see #getSupportedCipherSuites()
* @return array of the cipher suites enabled by default
*/
public abstract String [] getDefaultCipherSuites();
/**
* Returns the names of the cipher suites which could be enabled for use
* on an SSL connection. Normally, only a subset of these will actually
* be enabled by default, since this list may include cipher suites which
* do not meet quality of service requirements for those defaults. Such
* cipher suites are useful in specialized applications.
*
* @see #getDefaultCipherSuites()
* @return an array of cipher suite names
*/
public abstract String [] getSupportedCipherSuites();
/**
* Returns a socket layered over an existing socket connected to the named
* host, at the given port. This constructor can be used when tunneling SSL
* through a proxy or when negotiating the use of SSL over an existing
* socket. The host and port refer to the logical peer destination.
* This socket is configured using the socket options established for
* this factory.
*
* @param s the existing socket
* @param host the server host
* @param port the server port
* @param autoClose close the underlying socket when this socket is closed
* @return a socket connected to the specified host and port
* @throws IOException if an I/O error occurs when creating the socket
* @throws NullPointerException if the parameter s is null
*/
public abstract Socket createSocket(Socket s, String host,
int port, boolean autoClose)
throws IOException;
}
// file private
class DefaultSSLSocketFactory extends SSLSocketFactory
{
private Exception reason;
DefaultSSLSocketFactory(Exception reason) {
this.reason = reason;
}
private Socket throwException() throws SocketException {
throw (SocketException)
new SocketException(reason.toString()).initCause(reason);
}
public Socket createSocket()
throws IOException
{
return throwException();
}
public Socket createSocket(String host, int port)
throws IOException
{
return throwException();
}
public Socket createSocket(Socket s, String host,
int port, boolean autoClose)
throws IOException
{
return throwException();
}
public Socket createSocket(InetAddress address, int port)
throws IOException
{
return throwException();
}
public Socket createSocket(String host, int port,
InetAddress clientAddress, int clientPort)
throws IOException
{
return throwException();
}
public Socket createSocket(InetAddress address, int port,
InetAddress clientAddress, int clientPort)
throws IOException
{
return throwException();
}
public String [] getDefaultCipherSuites() {
return new String[0];
}
public String [] getSupportedCipherSuites() {
return new String[0];
}
}