2
|
1 |
/*
|
|
2 |
* Copyright 1996-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 sun.security.ssl;
|
|
28 |
|
|
29 |
import java.io.IOException;
|
|
30 |
import java.net.InetAddress;
|
|
31 |
import java.net.Socket;
|
|
32 |
import java.net.ServerSocket;
|
|
33 |
|
|
34 |
import java.util.*;
|
|
35 |
|
|
36 |
import javax.net.ServerSocketFactory;
|
|
37 |
import javax.net.ssl.SSLException;
|
|
38 |
import javax.net.ssl.SSLServerSocket;
|
|
39 |
|
|
40 |
|
|
41 |
/**
|
|
42 |
* This class provides a simple way for servers to support conventional
|
|
43 |
* use of the Secure Sockets Layer (SSL). Application code uses an
|
|
44 |
* SSLServerSocketImpl exactly like it uses a regular TCP ServerSocket; the
|
|
45 |
* difference is that the connections established are secured using SSL.
|
|
46 |
*
|
|
47 |
* <P> Also, the constructors take an explicit authentication context
|
|
48 |
* parameter, giving flexibility with respect to how the server socket
|
|
49 |
* authenticates itself. That policy flexibility is not exposed through
|
|
50 |
* the standard SSLServerSocketFactory API.
|
|
51 |
*
|
|
52 |
* <P> System security defaults prevent server sockets from accepting
|
|
53 |
* connections if they the authentication context has not been given
|
|
54 |
* a certificate chain and its matching private key. If the clients
|
|
55 |
* of your application support "anonymous" cipher suites, you may be
|
|
56 |
* able to configure a server socket to accept those suites.
|
|
57 |
*
|
|
58 |
* @see SSLSocketImpl
|
|
59 |
* @see SSLServerSocketFactoryImpl
|
|
60 |
*
|
|
61 |
* @author David Brownell
|
|
62 |
*/
|
|
63 |
final
|
|
64 |
class SSLServerSocketImpl extends SSLServerSocket
|
|
65 |
{
|
|
66 |
private SSLContextImpl sslContext;
|
|
67 |
|
|
68 |
/* Do newly accepted connections require clients to authenticate? */
|
|
69 |
private byte doClientAuth = SSLEngineImpl.clauth_none;
|
|
70 |
|
|
71 |
/* Do new connections created here use the "server" mode of SSL? */
|
|
72 |
private boolean useServerMode = true;
|
|
73 |
|
|
74 |
/* Can new connections created establish new sessions? */
|
|
75 |
private boolean enableSessionCreation = true;
|
|
76 |
|
|
77 |
/* what cipher suites to use by default */
|
|
78 |
private CipherSuiteList enabledCipherSuites = null;
|
|
79 |
|
|
80 |
/* which protocol to use by default */
|
|
81 |
private ProtocolList enabledProtocols = null;
|
|
82 |
|
|
83 |
/* could enabledCipherSuites ever complete handshaking? */
|
|
84 |
private boolean checkedEnabled = false;
|
|
85 |
|
|
86 |
/**
|
|
87 |
* Create an SSL server socket on a port, using a non-default
|
|
88 |
* authentication context and a specified connection backlog.
|
|
89 |
*
|
|
90 |
* @param port the port on which to listen
|
|
91 |
* @param backlog how many connections may be pending before
|
|
92 |
* the system should start rejecting new requests
|
|
93 |
* @param context authentication context for this server
|
|
94 |
*/
|
|
95 |
SSLServerSocketImpl(int port, int backlog, SSLContextImpl context)
|
|
96 |
throws IOException, SSLException
|
|
97 |
{
|
|
98 |
super(port, backlog);
|
|
99 |
initServer(context);
|
|
100 |
}
|
|
101 |
|
|
102 |
|
|
103 |
/**
|
|
104 |
* Create an SSL server socket on a port, using a specified
|
|
105 |
* authentication context and a specified backlog of connections
|
|
106 |
* as well as a particular specified network interface. This
|
|
107 |
* constructor is used on multihomed hosts, such as those used
|
|
108 |
* for firewalls or as routers, to control through which interface
|
|
109 |
* a network service is provided.
|
|
110 |
*
|
|
111 |
* @param port the port on which to listen
|
|
112 |
* @param backlog how many connections may be pending before
|
|
113 |
* the system should start rejecting new requests
|
|
114 |
* @param address the address of the network interface through
|
|
115 |
* which connections will be accepted
|
|
116 |
* @param context authentication context for this server
|
|
117 |
*/
|
|
118 |
SSLServerSocketImpl(
|
|
119 |
int port,
|
|
120 |
int backlog,
|
|
121 |
InetAddress address,
|
|
122 |
SSLContextImpl context)
|
|
123 |
throws IOException
|
|
124 |
{
|
|
125 |
super(port, backlog, address);
|
|
126 |
initServer(context);
|
|
127 |
}
|
|
128 |
|
|
129 |
|
|
130 |
/**
|
|
131 |
* Creates an unbound server socket.
|
|
132 |
*/
|
|
133 |
SSLServerSocketImpl(SSLContextImpl context) throws IOException {
|
|
134 |
super();
|
|
135 |
initServer(context);
|
|
136 |
}
|
|
137 |
|
|
138 |
|
|
139 |
/**
|
|
140 |
* Initializes the server socket.
|
|
141 |
*/
|
|
142 |
private void initServer(SSLContextImpl context) throws SSLException {
|
|
143 |
if (context == null) {
|
|
144 |
throw new SSLException("No Authentication context given");
|
|
145 |
}
|
|
146 |
sslContext = context;
|
|
147 |
enabledCipherSuites = CipherSuiteList.getDefault();
|
|
148 |
enabledProtocols = ProtocolList.getDefault();
|
|
149 |
}
|
|
150 |
|
|
151 |
/**
|
|
152 |
* Returns the names of the cipher suites which could be enabled for use
|
|
153 |
* on an SSL connection. Normally, only a subset of these will actually
|
|
154 |
* be enabled by default, since this list may include cipher suites which
|
|
155 |
* do not support the mutual authentication of servers and clients, or
|
|
156 |
* which do not protect data confidentiality. Servers may also need
|
|
157 |
* certain kinds of certificates to use certain cipher suites.
|
|
158 |
*
|
|
159 |
* @return an array of cipher suite names
|
|
160 |
*/
|
|
161 |
public String[] getSupportedCipherSuites() {
|
|
162 |
CipherSuiteList.clearAvailableCache();
|
|
163 |
return CipherSuiteList.getSupported().toStringArray();
|
|
164 |
}
|
|
165 |
|
|
166 |
/**
|
|
167 |
* Returns the list of cipher suites which are currently enabled
|
|
168 |
* for use by newly accepted connections. A null return indicates
|
|
169 |
* that the system defaults are in effect.
|
|
170 |
*/
|
|
171 |
synchronized public String[] getEnabledCipherSuites() {
|
|
172 |
return enabledCipherSuites.toStringArray();
|
|
173 |
}
|
|
174 |
|
|
175 |
/**
|
|
176 |
* Controls which particular SSL cipher suites are enabled for use
|
|
177 |
* by accepted connections.
|
|
178 |
*
|
|
179 |
* @param suites Names of all the cipher suites to enable; null
|
|
180 |
* means to accept system defaults.
|
|
181 |
*/
|
|
182 |
synchronized public void setEnabledCipherSuites(String[] suites) {
|
|
183 |
enabledCipherSuites = new CipherSuiteList(suites);
|
|
184 |
checkedEnabled = false;
|
|
185 |
}
|
|
186 |
|
|
187 |
public String[] getSupportedProtocols() {
|
|
188 |
return ProtocolList.getSupported().toStringArray();
|
|
189 |
}
|
|
190 |
|
|
191 |
/**
|
|
192 |
* Controls which protocols are enabled for use.
|
|
193 |
* The protocols must have been listed by
|
|
194 |
* getSupportedProtocols() as being supported.
|
|
195 |
*
|
|
196 |
* @param protocols protocols to enable.
|
|
197 |
* @exception IllegalArgumentException when one of the protocols
|
|
198 |
* named by the parameter is not supported.
|
|
199 |
*/
|
|
200 |
synchronized public void setEnabledProtocols(String[] protocols) {
|
|
201 |
enabledProtocols = new ProtocolList(protocols);
|
|
202 |
}
|
|
203 |
|
|
204 |
synchronized public String[] getEnabledProtocols() {
|
|
205 |
return enabledProtocols.toStringArray();
|
|
206 |
}
|
|
207 |
|
|
208 |
/**
|
|
209 |
* Controls whether the connections which are accepted must include
|
|
210 |
* client authentication.
|
|
211 |
*/
|
|
212 |
public void setNeedClientAuth(boolean flag) {
|
|
213 |
doClientAuth = (flag ?
|
|
214 |
SSLEngineImpl.clauth_required : SSLEngineImpl.clauth_none);
|
|
215 |
}
|
|
216 |
|
|
217 |
public boolean getNeedClientAuth() {
|
|
218 |
return (doClientAuth == SSLEngineImpl.clauth_required);
|
|
219 |
}
|
|
220 |
|
|
221 |
/**
|
|
222 |
* Controls whether the connections which are accepted should request
|
|
223 |
* client authentication.
|
|
224 |
*/
|
|
225 |
public void setWantClientAuth(boolean flag) {
|
|
226 |
doClientAuth = (flag ?
|
|
227 |
SSLEngineImpl.clauth_requested : SSLEngineImpl.clauth_none);
|
|
228 |
}
|
|
229 |
|
|
230 |
public boolean getWantClientAuth() {
|
|
231 |
return (doClientAuth == SSLEngineImpl.clauth_requested);
|
|
232 |
}
|
|
233 |
|
|
234 |
/**
|
|
235 |
* Makes the returned sockets act in SSL "client" mode, not the usual
|
|
236 |
* server mode. The canonical example of why this is needed is for
|
|
237 |
* FTP clients, which accept connections from servers and should be
|
|
238 |
* rejoining the already-negotiated SSL connection.
|
|
239 |
*/
|
|
240 |
public void setUseClientMode(boolean flag) {
|
|
241 |
useServerMode = !flag;
|
|
242 |
}
|
|
243 |
|
|
244 |
public boolean getUseClientMode() {
|
|
245 |
return !useServerMode;
|
|
246 |
}
|
|
247 |
|
|
248 |
|
|
249 |
/**
|
|
250 |
* Controls whether new connections may cause creation of new SSL
|
|
251 |
* sessions.
|
|
252 |
*/
|
|
253 |
public void setEnableSessionCreation(boolean flag) {
|
|
254 |
enableSessionCreation = flag;
|
|
255 |
}
|
|
256 |
|
|
257 |
/**
|
|
258 |
* Returns true if new connections may cause creation of new SSL
|
|
259 |
* sessions.
|
|
260 |
*/
|
|
261 |
public boolean getEnableSessionCreation() {
|
|
262 |
return enableSessionCreation;
|
|
263 |
}
|
|
264 |
|
|
265 |
|
|
266 |
/**
|
|
267 |
* Accept a new SSL connection. This server identifies itself with
|
|
268 |
* information provided in the authentication context which was
|
|
269 |
* presented during construction.
|
|
270 |
*/
|
|
271 |
public Socket accept() throws IOException {
|
|
272 |
checkEnabledSuites();
|
|
273 |
|
|
274 |
SSLSocketImpl s = new SSLSocketImpl(sslContext, useServerMode,
|
|
275 |
enabledCipherSuites, doClientAuth, enableSessionCreation,
|
|
276 |
enabledProtocols);
|
|
277 |
|
|
278 |
implAccept(s);
|
|
279 |
s.doneConnect();
|
|
280 |
return s;
|
|
281 |
}
|
|
282 |
|
|
283 |
|
|
284 |
/*
|
|
285 |
* This is a sometimes helpful diagnostic check that is performed
|
|
286 |
* once for each ServerSocket to verify that the initial set of
|
|
287 |
* enabled suites are capable of supporting a successful handshake.
|
|
288 |
*/
|
|
289 |
private void checkEnabledSuites() throws IOException {
|
|
290 |
//
|
|
291 |
// We want to report an error if no cipher suites were actually
|
|
292 |
// enabled, since this is an error users are known to make. Then
|
|
293 |
// they get vastly confused by having clients report an error!
|
|
294 |
//
|
|
295 |
synchronized (this) {
|
|
296 |
if (checkedEnabled) {
|
|
297 |
return;
|
|
298 |
}
|
|
299 |
if (useServerMode == false) {
|
|
300 |
return;
|
|
301 |
}
|
|
302 |
|
|
303 |
SSLSocketImpl tmp = new SSLSocketImpl(sslContext, useServerMode,
|
|
304 |
enabledCipherSuites, doClientAuth,
|
|
305 |
enableSessionCreation, enabledProtocols);
|
|
306 |
|
|
307 |
ServerHandshaker handshaker = tmp.getServerHandshaker();
|
|
308 |
|
|
309 |
for (Iterator t = enabledCipherSuites.iterator(); t.hasNext(); ) {
|
|
310 |
CipherSuite suite = (CipherSuite)t.next();
|
|
311 |
if (handshaker.trySetCipherSuite(suite)) {
|
|
312 |
checkedEnabled = true;
|
|
313 |
return;
|
|
314 |
}
|
|
315 |
}
|
|
316 |
|
|
317 |
//
|
|
318 |
// diagnostic text here is currently appropriate
|
|
319 |
// since it's only certificate unavailability that can
|
|
320 |
// cause such problems ... but that might change someday.
|
|
321 |
//
|
|
322 |
throw new SSLException("No available certificate or key corresponds"
|
|
323 |
+ " to the SSL cipher suites which are enabled.");
|
|
324 |
}
|
|
325 |
}
|
|
326 |
|
|
327 |
|
|
328 |
/**
|
|
329 |
* Provides a brief description of this SSL socket.
|
|
330 |
*/
|
|
331 |
public String toString() {
|
|
332 |
return "[SSL: "+ super.toString() + "]";
|
|
333 |
}
|
|
334 |
}
|