0N/A/*
2998N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. 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
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle 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,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * 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.*;
2998N/Aimport java.util.Locale;
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(
2998N/A new GetPropertyAction("javax.net.debug", "")).toLowerCase(
2998N/A Locale.ENGLISH);
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;
0N/A }
0N/A });
0N/A }
0N/A
0N/A /**
0N/A * Returns the list of cipher suites which are enabled by default.
0N/A * Unless a different list is enabled, handshaking on an SSL connection
0N/A * will use one of these cipher suites. The minimum quality of service
0N/A * for these defaults requires confidentiality protection and server
0N/A * authentication (that is, no anonymous cipher suites).
0N/A *
0N/A * @see #getSupportedCipherSuites()
0N/A * @return array of the cipher suites enabled by default
0N/A */
0N/A public abstract String [] getDefaultCipherSuites();
0N/A
0N/A /**
0N/A * Returns the names of the cipher suites which could be enabled for use
0N/A * on an SSL connection. Normally, only a subset of these will actually
0N/A * be enabled by default, since this list may include cipher suites which
0N/A * do not meet quality of service requirements for those defaults. Such
0N/A * cipher suites are useful in specialized applications.
0N/A *
0N/A * @see #getDefaultCipherSuites()
0N/A * @return an array of cipher suite names
0N/A */
0N/A public abstract String [] getSupportedCipherSuites();
0N/A
0N/A /**
0N/A * Returns a socket layered over an existing socket connected to the named
0N/A * host, at the given port. This constructor can be used when tunneling SSL
0N/A * through a proxy or when negotiating the use of SSL over an existing
0N/A * socket. The host and port refer to the logical peer destination.
0N/A * This socket is configured using the socket options established for
0N/A * this factory.
0N/A *
0N/A * @param s the existing socket
0N/A * @param host the server host
0N/A * @param port the server port
0N/A * @param autoClose close the underlying socket when this socket is closed
0N/A * @return a socket connected to the specified host and port
0N/A * @throws IOException if an I/O error occurs when creating the socket
0N/A * @throws NullPointerException if the parameter s is null
0N/A */
0N/A public abstract Socket createSocket(Socket s, String host,
0N/A int port, boolean autoClose)
0N/A throws IOException;
0N/A}
0N/A
0N/A
0N/A// file private
0N/Aclass DefaultSSLSocketFactory extends SSLSocketFactory
0N/A{
0N/A private Exception reason;
0N/A
0N/A DefaultSSLSocketFactory(Exception reason) {
0N/A this.reason = reason;
0N/A }
0N/A
0N/A private Socket throwException() throws SocketException {
0N/A throw (SocketException)
0N/A new SocketException(reason.toString()).initCause(reason);
0N/A }
0N/A
0N/A public Socket createSocket()
0N/A throws IOException
0N/A {
0N/A return throwException();
0N/A }
0N/A
0N/A public Socket createSocket(String host, int port)
0N/A throws IOException
0N/A {
0N/A return throwException();
0N/A }
0N/A
0N/A public Socket createSocket(Socket s, String host,
0N/A int port, boolean autoClose)
0N/A throws IOException
0N/A {
0N/A return throwException();
0N/A }
0N/A
0N/A public Socket createSocket(InetAddress address, int port)
0N/A throws IOException
0N/A {
0N/A return throwException();
0N/A }
0N/A
0N/A public Socket createSocket(String host, int port,
0N/A InetAddress clientAddress, int clientPort)
0N/A throws IOException
0N/A {
0N/A return throwException();
0N/A }
0N/A
0N/A public Socket createSocket(InetAddress address, int port,
0N/A InetAddress clientAddress, int clientPort)
0N/A throws IOException
0N/A {
0N/A return throwException();
0N/A }
0N/A
0N/A public String [] getDefaultCipherSuites() {
0N/A return new String[0];
0N/A }
0N/A
0N/A public String [] getSupportedCipherSuites() {
0N/A return new String[0];
0N/A }
0N/A}