Source for javax.net.ssl.SSLServerSocketFactory

   1: /* SSLServerSocketFactory.java -- factory for SSL server sockets.
   2:    Copyright (C) 2004 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.net.ssl;
  40: 
  41: import java.security.KeyStore;
  42: import java.security.Security;
  43: 
  44: import javax.net.ServerSocketFactory;
  45: 
  46: /**
  47:  * A server socket factory for <i>Secure Socket Layer</i> (<b>SSL</b>)
  48:  * server sockets.
  49:  */
  50: public abstract class SSLServerSocketFactory extends ServerSocketFactory
  51: {
  52:   // Field.
  53:   // -------------------------------------------------------------------------
  54: 
  55:   private static SSLContext context;
  56: 
  57:   // Constructor.
  58:   // -------------------------------------------------------------------------
  59: 
  60:   protected SSLServerSocketFactory()
  61:   {
  62:     super();
  63:   }
  64: 
  65:   // Class methods.
  66:   // -------------------------------------------------------------------------
  67: 
  68:   /**
  69:    * Returns a default implementation of a SSL server socket factory.
  70:    *
  71:    * <p>To control the class that gets returned by this method, set the
  72:    * security property "ssl.ServerSocketFactory.provider" to the class
  73:    * name of a concrete implementation of this class. If not set, a
  74:    * system-dependent implementation will be used.</p>
  75:    *
  76:    * <p>The implementation returned is created by the first implementation
  77:    * of the {@link SSLContext} class found, which is initialized with
  78:    * default parameters. To control the key and trust manager factory
  79:    * algorithms used as defaults, set the security properties
  80:    * "ssl.keyManagerFactory.algorithm" and "ssl.trustManagerFactory.algorithm"
  81:    * to the appropriate names.</p>
  82:    *
  83:    * <p>Using this method is not recommended. Instead, use the methods of
  84:    * {@link SSLContext}, which provide much better control over the
  85:    * creation of server socket factories.</p>
  86:    *
  87:    * @return The default server socket factory.
  88:    * @throws RuntimeException If no default can be created.
  89:    */
  90:   public static synchronized ServerSocketFactory getDefault()
  91:   {
  92:     try
  93:       {
  94:         String s = Security.getProperty("ssl.ServerSocketFactory.provider");
  95:         ClassLoader cl = ClassLoader.getSystemClassLoader();
  96:         if (s != null && cl != null)
  97:           {
  98:             return (ServerSocketFactory) cl.loadClass(s).newInstance();
  99:           }
 100:       }
 101:     catch (Exception e)
 102:       {
 103:       }
 104:     if (context == null)
 105:       {
 106:         KeyManager[] km = null;
 107:         TrustManager[] tm = null;
 108: 
 109:         // 1. Determine which algorithms to use for the key and trust
 110:         // manager factories.
 111:         String kmAlg = KeyManagerFactory.getDefaultAlgorithm();
 112:         String tmAlg = TrustManagerFactory.getDefaultAlgorithm();
 113:         // 2. Try to initialize the factories with default parameters.
 114:         try
 115:           {
 116:             KeyManagerFactory kmf = KeyManagerFactory.getInstance(kmAlg);
 117:             kmf.init(null, null);
 118:             km = kmf.getKeyManagers();
 119:           }
 120:         catch (Exception ex)
 121:           {
 122:           }
 123:         try
 124:           {
 125:             TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmAlg);
 126:             tmf.init((KeyStore) null);
 127:             tm = tmf.getTrustManagers();
 128:           }
 129:         catch (Exception ex)
 130:           {
 131:           }
 132: 
 133:         // 3. Create and initialize a context.
 134:         try
 135:           {
 136:             context = SSLContext.getInstance("SSLv3");
 137:             context.init(km, tm, null);
 138:           }
 139:         catch (Exception ex)
 140:           {
 141:             throw new RuntimeException("error instantiating default server socket factory: "
 142:                                        + ex.toString());
 143:           }
 144:       }
 145:     try
 146:       {
 147:         return context.getServerSocketFactory();
 148:       }
 149:     catch (Exception e)
 150:       {
 151:       }
 152:     throw new RuntimeException("no SSLSocketFactory implementation available");
 153:   }
 154: 
 155:   // Abstract methods.
 156:   // -------------------------------------------------------------------------
 157: 
 158:   /**
 159:    * Returns the list of cipher suites that will be enabled in server sockets
 160:    * created by this factory.
 161:    *
 162:    * @return The default cipher suites.
 163:    */
 164:   public abstract String[] getDefaultCipherSuites();
 165: 
 166:   /**
 167:    * Returns the list of all cipher suites supported by this factory.
 168:    *
 169:    * @return The list of supported cipher suites.
 170:    */
 171:   public abstract String[] getSupportedCipherSuites();
 172: }