1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 package javax.net.ssl;
  28 
  29 import java.io.IOException;
  30 import java.net.InetAddress;
  31 import java.net.ServerSocket;
  32 import java.net.SocketException;
  33 import javax.net.ServerSocketFactory;
  34 import java.security.*;
  35 
  36 /**
  37  * <code>SSLServerSocketFactory</code>s create
  38  * <code>SSLServerSocket</code>s.
  39  *
  40  * @since 1.4
  41  * @see SSLSocket
  42  * @see SSLServerSocket
  43  * @author David Brownell
  44  */
  45 public abstract class SSLServerSocketFactory extends ServerSocketFactory
  46 {
  47     private static SSLServerSocketFactory theFactory;
  48 
  49     private static boolean propertyChecked;
  50 
  51     private static void log(String msg) {
  52         if (SSLSocketFactory.DEBUG) {
  53             System.out.println(msg);
  54         }
  55     }
  56 
  57     /**
  58      * Constructor is used only by subclasses.
  59      */
  60     protected SSLServerSocketFactory() { /* NOTHING */ }
  61 
  62     /**
  63      * Returns the default SSL server socket factory.
  64      *
  65      * <p>The first time this method is called, the security property
  66      * "ssl.ServerSocketFactory.provider" is examined. If it is non-null, a
  67      * class by that name is loaded and instantiated. If that is successful and
  68      * the object is an instance of SSLServerSocketFactory, it is made the
  69      * default SSL server socket factory.
  70      *
  71      * <p>Otherwise, this method returns
  72      * <code>SSLContext.getDefault().getServerSocketFactory()</code>. If that
  73      * call fails, an inoperative factory is returned.
  74      *
  75      * @return the default <code>ServerSocketFactory</code>
  76      * @see SSLContext#getDefault
  77      */
  78     public static synchronized ServerSocketFactory getDefault() {
  79         if (theFactory != null) {
  80             return theFactory;
  81         }
  82 
  83         if (propertyChecked == false) {
  84             propertyChecked = true;
  85             String clsName = SSLSocketFactory.getSecurityProperty
  86                                         ("ssl.ServerSocketFactory.provider");
  87             if (clsName != null) {
  88                 log("setting up default SSLServerSocketFactory");
  89                 try {
  90                     Class<?> cls = null;
  91                     try {
  92                         cls = Class.forName(clsName);
  93                     } catch (ClassNotFoundException e) {
  94                         ClassLoader cl = ClassLoader.getSystemClassLoader();
  95                         if (cl != null) {
  96                             cls = cl.loadClass(clsName);
  97                         }
  98                     }
  99                     log("class " + clsName + " is loaded");
 100                     @SuppressWarnings("deprecation")
 101                     SSLServerSocketFactory fac = (SSLServerSocketFactory)cls.newInstance();
 102                     log("instantiated an instance of class " + clsName);
 103                     theFactory = fac;
 104                     return fac;
 105                 } catch (Exception e) {
 106                     log("SSLServerSocketFactory instantiation failed: " + e);
 107                     theFactory = new DefaultSSLServerSocketFactory(e);
 108                     return theFactory;
 109                 }
 110             }
 111         }
 112 
 113         try {
 114             return SSLContext.getDefault().getServerSocketFactory();
 115         } catch (NoSuchAlgorithmException e) {
 116             return new DefaultSSLServerSocketFactory(e);
 117         }
 118     }
 119 
 120     /**
 121      * Returns the list of cipher suites which are enabled by default.
 122      * Unless a different list is enabled, handshaking on an SSL connection
 123      * will use one of these cipher suites.  The minimum quality of service
 124      * for these defaults requires confidentiality protection and server
 125      * authentication (that is, no anonymous cipher suites).
 126      *
 127      * @see #getSupportedCipherSuites()
 128      * @return array of the cipher suites enabled by default
 129      */
 130     public abstract String [] getDefaultCipherSuites();
 131 
 132 
 133     /**
 134      * Returns the names of the cipher suites which could be enabled for use
 135      * on an SSL connection created by this factory.
 136      * Normally, only a subset of these will actually
 137      * be enabled by default, since this list may include cipher suites which
 138      * do not meet quality of service requirements for those defaults.  Such
 139      * cipher suites are useful in specialized applications.
 140      *
 141      * @return an array of cipher suite names
 142      * @see #getDefaultCipherSuites()
 143      */
 144     public abstract String [] getSupportedCipherSuites();
 145 }
 146 
 147 
 148 //
 149 // The default factory does NOTHING.
 150 //
 151 class DefaultSSLServerSocketFactory extends SSLServerSocketFactory {
 152 
 153     private final Exception reason;
 154 
 155     DefaultSSLServerSocketFactory(Exception reason) {
 156         this.reason = reason;
 157     }
 158 
 159     private ServerSocket throwException() throws SocketException {
 160         throw (SocketException)
 161             new SocketException(reason.toString()).initCause(reason);
 162     }
 163 
 164     @Override
 165     public ServerSocket createServerSocket() throws IOException {
 166         return throwException();
 167     }
 168 
 169 
 170     @Override
 171     public ServerSocket createServerSocket(int port)
 172     throws IOException
 173     {
 174         return throwException();
 175     }
 176 
 177     @Override
 178     public ServerSocket createServerSocket(int port, int backlog)
 179     throws IOException
 180     {
 181         return throwException();
 182     }
 183 
 184     @Override
 185     public ServerSocket
 186     createServerSocket(int port, int backlog, InetAddress ifAddress)
 187     throws IOException
 188     {
 189         return throwException();
 190     }
 191 
 192     @Override
 193     public String [] getDefaultCipherSuites() {
 194         return new String[0];
 195     }
 196 
 197     @Override
 198     public String [] getSupportedCipherSuites() {
 199         return new String[0];
 200     }
 201 }