1 /*
2 * Copyright 1997-2007 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any 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 SSLServerSocketFactory fac = (SSLServerSocketFactory)cls.newInstance();
101 log("instantiated an instance of class " + clsName);
102 theFactory = fac;
103 return fac;
104 } catch (Exception e) {
105 log("SSLServerSocketFactory instantiation failed: " + e);
106 theFactory = new DefaultSSLServerSocketFactory(e);
107 return theFactory;
108 }
109 }
110 }
111
112 try {
113 return SSLContext.getDefault().getServerSocketFactory();
114 } catch (NoSuchAlgorithmException e) {
115 return new DefaultSSLServerSocketFactory(e);
116 }
117 }
118
119 /**
120 * Returns the list of cipher suites which are enabled by default.
121 * Unless a different list is enabled, handshaking on an SSL connection
122 * will use one of these cipher suites. The minimum quality of service
123 * for these defaults requires confidentiality protection and server
124 * authentication (that is, no anonymous cipher suites).
125 *
126 * @see #getSupportedCipherSuites()
127 * @return array of the cipher suites enabled by default
128 */
129 public abstract String [] getDefaultCipherSuites();
130
131
132 /**
133 * Returns the names of the cipher suites which could be enabled for use
134 * on an SSL connection created by this factory.
135 * Normally, only a subset of these will actually
136 * be enabled by default, since this list may include cipher suites which
137 * do not meet quality of service requirements for those defaults. Such
138 * cipher suites are useful in specialized applications.
139 *
140 * @return an array of cipher suite names
141 * @see #getDefaultCipherSuites()
142 */
143 public abstract String [] getSupportedCipherSuites();
144 }
145
146
147 //
148 // The default factory does NOTHING.
149 //
150 class DefaultSSLServerSocketFactory extends SSLServerSocketFactory {
151
152 private final Exception reason;
153
154 DefaultSSLServerSocketFactory(Exception reason) {
155 this.reason = reason;
156 }
157
158 private ServerSocket throwException() throws SocketException {
159 throw (SocketException)
160 new SocketException(reason.toString()).initCause(reason);
161 }
162
163 public ServerSocket createServerSocket() throws IOException {
164 return throwException();
165 }
166
167
168 public ServerSocket createServerSocket(int port)
169 throws IOException
170 {
171 return throwException();
172 }
173
174 public ServerSocket createServerSocket(int port, int backlog)
175 throws IOException
176 {
177 return throwException();
178 }
179
180 public ServerSocket
181 createServerSocket(int port, int backlog, InetAddress ifAddress)
182 throws IOException
183 {
184 return throwException();
185 }
186
187 public String [] getDefaultCipherSuites() {
188 return new String[0];
189 }
190
191 public String [] getSupportedCipherSuites() {
192 return new String[0];
193 }
194 }