author | xuelei |
Sat, 24 Nov 2012 04:09:19 -0800 | |
changeset 14664 | e71aa0962e70 |
parent 14194 | 971f46db533d |
child 14665 | 8caa5add16ed |
permissions | -rw-r--r-- |
2 | 1 |
/* |
13815 | 2 |
* Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
||
7043 | 33 |
import java.security.AlgorithmConstraints; |
34 |
||
2 | 35 |
import java.util.*; |
36 |
||
37 |
import javax.net.ssl.SSLException; |
|
38 |
import javax.net.ssl.SSLServerSocket; |
|
7043 | 39 |
import javax.net.ssl.SSLParameters; |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
40 |
import javax.net.ssl.SNIMatcher; |
2 | 41 |
|
42 |
||
43 |
/** |
|
44 |
* This class provides a simple way for servers to support conventional |
|
45 |
* use of the Secure Sockets Layer (SSL). Application code uses an |
|
46 |
* SSLServerSocketImpl exactly like it uses a regular TCP ServerSocket; the |
|
47 |
* difference is that the connections established are secured using SSL. |
|
48 |
* |
|
49 |
* <P> Also, the constructors take an explicit authentication context |
|
50 |
* parameter, giving flexibility with respect to how the server socket |
|
51 |
* authenticates itself. That policy flexibility is not exposed through |
|
52 |
* the standard SSLServerSocketFactory API. |
|
53 |
* |
|
54 |
* <P> System security defaults prevent server sockets from accepting |
|
55 |
* connections if they the authentication context has not been given |
|
56 |
* a certificate chain and its matching private key. If the clients |
|
57 |
* of your application support "anonymous" cipher suites, you may be |
|
58 |
* able to configure a server socket to accept those suites. |
|
59 |
* |
|
60 |
* @see SSLSocketImpl |
|
61 |
* @see SSLServerSocketFactoryImpl |
|
62 |
* |
|
63 |
* @author David Brownell |
|
64 |
*/ |
|
65 |
final |
|
66 |
class SSLServerSocketImpl extends SSLServerSocket |
|
67 |
{ |
|
68 |
private SSLContextImpl sslContext; |
|
69 |
||
70 |
/* Do newly accepted connections require clients to authenticate? */ |
|
71 |
private byte doClientAuth = SSLEngineImpl.clauth_none; |
|
72 |
||
73 |
/* Do new connections created here use the "server" mode of SSL? */ |
|
74 |
private boolean useServerMode = true; |
|
75 |
||
76 |
/* Can new connections created establish new sessions? */ |
|
77 |
private boolean enableSessionCreation = true; |
|
78 |
||
79 |
/* what cipher suites to use by default */ |
|
80 |
private CipherSuiteList enabledCipherSuites = null; |
|
81 |
||
82 |
/* which protocol to use by default */ |
|
83 |
private ProtocolList enabledProtocols = null; |
|
84 |
||
85 |
/* could enabledCipherSuites ever complete handshaking? */ |
|
86 |
private boolean checkedEnabled = false; |
|
87 |
||
7043 | 88 |
// the endpoint identification protocol to use by default |
89 |
private String identificationProtocol = null; |
|
90 |
||
91 |
// The cryptographic algorithm constraints |
|
92 |
private AlgorithmConstraints algorithmConstraints = null; |
|
93 |
||
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
94 |
// The server name indication |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
95 |
Collection<SNIMatcher> sniMatchers = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
96 |
Collections.<SNIMatcher>emptyList(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
97 |
|
2 | 98 |
/** |
99 |
* Create an SSL server socket on a port, using a non-default |
|
100 |
* authentication context and a specified connection backlog. |
|
101 |
* |
|
102 |
* @param port the port on which to listen |
|
103 |
* @param backlog how many connections may be pending before |
|
104 |
* the system should start rejecting new requests |
|
105 |
* @param context authentication context for this server |
|
106 |
*/ |
|
107 |
SSLServerSocketImpl(int port, int backlog, SSLContextImpl context) |
|
108 |
throws IOException, SSLException |
|
109 |
{ |
|
110 |
super(port, backlog); |
|
111 |
initServer(context); |
|
112 |
} |
|
113 |
||
114 |
||
115 |
/** |
|
116 |
* Create an SSL server socket on a port, using a specified |
|
117 |
* authentication context and a specified backlog of connections |
|
118 |
* as well as a particular specified network interface. This |
|
119 |
* constructor is used on multihomed hosts, such as those used |
|
120 |
* for firewalls or as routers, to control through which interface |
|
121 |
* a network service is provided. |
|
122 |
* |
|
123 |
* @param port the port on which to listen |
|
124 |
* @param backlog how many connections may be pending before |
|
125 |
* the system should start rejecting new requests |
|
126 |
* @param address the address of the network interface through |
|
127 |
* which connections will be accepted |
|
128 |
* @param context authentication context for this server |
|
129 |
*/ |
|
130 |
SSLServerSocketImpl( |
|
131 |
int port, |
|
132 |
int backlog, |
|
133 |
InetAddress address, |
|
134 |
SSLContextImpl context) |
|
135 |
throws IOException |
|
136 |
{ |
|
137 |
super(port, backlog, address); |
|
138 |
initServer(context); |
|
139 |
} |
|
140 |
||
141 |
||
142 |
/** |
|
143 |
* Creates an unbound server socket. |
|
144 |
*/ |
|
145 |
SSLServerSocketImpl(SSLContextImpl context) throws IOException { |
|
146 |
super(); |
|
147 |
initServer(context); |
|
148 |
} |
|
149 |
||
150 |
||
151 |
/** |
|
152 |
* Initializes the server socket. |
|
153 |
*/ |
|
154 |
private void initServer(SSLContextImpl context) throws SSLException { |
|
155 |
if (context == null) { |
|
156 |
throw new SSLException("No Authentication context given"); |
|
157 |
} |
|
158 |
sslContext = context; |
|
9246
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
159 |
enabledCipherSuites = sslContext.getDefaultCipherSuiteList(true); |
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
160 |
enabledProtocols = sslContext.getDefaultProtocolList(true); |
2 | 161 |
} |
162 |
||
163 |
/** |
|
164 |
* Returns the names of the cipher suites which could be enabled for use |
|
165 |
* on an SSL connection. Normally, only a subset of these will actually |
|
166 |
* be enabled by default, since this list may include cipher suites which |
|
167 |
* do not support the mutual authentication of servers and clients, or |
|
168 |
* which do not protect data confidentiality. Servers may also need |
|
169 |
* certain kinds of certificates to use certain cipher suites. |
|
170 |
* |
|
171 |
* @return an array of cipher suite names |
|
172 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
173 |
@Override |
2 | 174 |
public String[] getSupportedCipherSuites() { |
13815 | 175 |
return sslContext.getSupportedCipherSuiteList().toStringArray(); |
2 | 176 |
} |
177 |
||
178 |
/** |
|
179 |
* Returns the list of cipher suites which are currently enabled |
|
180 |
* for use by newly accepted connections. A null return indicates |
|
181 |
* that the system defaults are in effect. |
|
182 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
183 |
@Override |
2 | 184 |
synchronized public String[] getEnabledCipherSuites() { |
185 |
return enabledCipherSuites.toStringArray(); |
|
186 |
} |
|
187 |
||
188 |
/** |
|
189 |
* Controls which particular SSL cipher suites are enabled for use |
|
190 |
* by accepted connections. |
|
191 |
* |
|
192 |
* @param suites Names of all the cipher suites to enable; null |
|
193 |
* means to accept system defaults. |
|
194 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
195 |
@Override |
2 | 196 |
synchronized public void setEnabledCipherSuites(String[] suites) { |
197 |
enabledCipherSuites = new CipherSuiteList(suites); |
|
198 |
checkedEnabled = false; |
|
199 |
} |
|
200 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
201 |
@Override |
2 | 202 |
public String[] getSupportedProtocols() { |
9246
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
203 |
return sslContext.getSuportedProtocolList().toStringArray(); |
2 | 204 |
} |
205 |
||
206 |
/** |
|
207 |
* Controls which protocols are enabled for use. |
|
208 |
* The protocols must have been listed by |
|
209 |
* getSupportedProtocols() as being supported. |
|
210 |
* |
|
211 |
* @param protocols protocols to enable. |
|
212 |
* @exception IllegalArgumentException when one of the protocols |
|
213 |
* named by the parameter is not supported. |
|
214 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
215 |
@Override |
2 | 216 |
synchronized public void setEnabledProtocols(String[] protocols) { |
217 |
enabledProtocols = new ProtocolList(protocols); |
|
218 |
} |
|
219 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
220 |
@Override |
2 | 221 |
synchronized public String[] getEnabledProtocols() { |
222 |
return enabledProtocols.toStringArray(); |
|
223 |
} |
|
224 |
||
225 |
/** |
|
226 |
* Controls whether the connections which are accepted must include |
|
227 |
* client authentication. |
|
228 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
229 |
@Override |
2 | 230 |
public void setNeedClientAuth(boolean flag) { |
231 |
doClientAuth = (flag ? |
|
232 |
SSLEngineImpl.clauth_required : SSLEngineImpl.clauth_none); |
|
233 |
} |
|
234 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
235 |
@Override |
2 | 236 |
public boolean getNeedClientAuth() { |
237 |
return (doClientAuth == SSLEngineImpl.clauth_required); |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* Controls whether the connections which are accepted should request |
|
242 |
* client authentication. |
|
243 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
244 |
@Override |
2 | 245 |
public void setWantClientAuth(boolean flag) { |
246 |
doClientAuth = (flag ? |
|
247 |
SSLEngineImpl.clauth_requested : SSLEngineImpl.clauth_none); |
|
248 |
} |
|
249 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
250 |
@Override |
2 | 251 |
public boolean getWantClientAuth() { |
252 |
return (doClientAuth == SSLEngineImpl.clauth_requested); |
|
253 |
} |
|
254 |
||
255 |
/** |
|
256 |
* Makes the returned sockets act in SSL "client" mode, not the usual |
|
257 |
* server mode. The canonical example of why this is needed is for |
|
258 |
* FTP clients, which accept connections from servers and should be |
|
259 |
* rejoining the already-negotiated SSL connection. |
|
260 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
261 |
@Override |
2 | 262 |
public void setUseClientMode(boolean flag) { |
7039 | 263 |
/* |
264 |
* If we need to change the socket mode and the enabled |
|
265 |
* protocols haven't specifically been set by the user, |
|
266 |
* change them to the corresponding default ones. |
|
267 |
*/ |
|
268 |
if (useServerMode != (!flag) && |
|
9246
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
269 |
sslContext.isDefaultProtocolList(enabledProtocols)) { |
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
270 |
enabledProtocols = sslContext.getDefaultProtocolList(!flag); |
7039 | 271 |
} |
272 |
||
2 | 273 |
useServerMode = !flag; |
274 |
} |
|
275 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
276 |
@Override |
2 | 277 |
public boolean getUseClientMode() { |
278 |
return !useServerMode; |
|
279 |
} |
|
280 |
||
281 |
||
282 |
/** |
|
283 |
* Controls whether new connections may cause creation of new SSL |
|
284 |
* sessions. |
|
285 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
286 |
@Override |
2 | 287 |
public void setEnableSessionCreation(boolean flag) { |
288 |
enableSessionCreation = flag; |
|
289 |
} |
|
290 |
||
291 |
/** |
|
292 |
* Returns true if new connections may cause creation of new SSL |
|
293 |
* sessions. |
|
294 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
295 |
@Override |
2 | 296 |
public boolean getEnableSessionCreation() { |
297 |
return enableSessionCreation; |
|
298 |
} |
|
299 |
||
300 |
/** |
|
7043 | 301 |
* Returns the SSLParameters in effect for newly accepted connections. |
302 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
303 |
@Override |
7043 | 304 |
synchronized public SSLParameters getSSLParameters() { |
305 |
SSLParameters params = super.getSSLParameters(); |
|
306 |
||
307 |
// the super implementation does not handle the following parameters |
|
308 |
params.setEndpointIdentificationAlgorithm(identificationProtocol); |
|
309 |
params.setAlgorithmConstraints(algorithmConstraints); |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
310 |
params.setSNIMatchers(sniMatchers); |
7043 | 311 |
|
312 |
return params; |
|
313 |
} |
|
314 |
||
315 |
/** |
|
316 |
* Applies SSLParameters to newly accepted connections. |
|
317 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
318 |
@Override |
7043 | 319 |
synchronized public void setSSLParameters(SSLParameters params) { |
320 |
super.setSSLParameters(params); |
|
321 |
||
322 |
// the super implementation does not handle the following parameters |
|
323 |
identificationProtocol = params.getEndpointIdentificationAlgorithm(); |
|
324 |
algorithmConstraints = params.getAlgorithmConstraints(); |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
325 |
Collection<SNIMatcher> matchers = params.getSNIMatchers(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
326 |
if (matchers != null) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
327 |
sniMatchers = params.getSNIMatchers(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
328 |
} |
7043 | 329 |
} |
330 |
||
331 |
/** |
|
2 | 332 |
* Accept a new SSL connection. This server identifies itself with |
333 |
* information provided in the authentication context which was |
|
334 |
* presented during construction. |
|
335 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
336 |
@Override |
2 | 337 |
public Socket accept() throws IOException { |
338 |
SSLSocketImpl s = new SSLSocketImpl(sslContext, useServerMode, |
|
339 |
enabledCipherSuites, doClientAuth, enableSessionCreation, |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
340 |
enabledProtocols, identificationProtocol, algorithmConstraints, |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
341 |
sniMatchers); |
2 | 342 |
|
343 |
implAccept(s); |
|
344 |
s.doneConnect(); |
|
345 |
return s; |
|
346 |
} |
|
347 |
||
348 |
/** |
|
349 |
* Provides a brief description of this SSL socket. |
|
350 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
351 |
@Override |
2 | 352 |
public String toString() { |
353 |
return "[SSL: "+ super.toString() + "]"; |
|
354 |
} |
|
355 |
} |