author | martin |
Tue, 15 Sep 2015 21:56:04 -0700 | |
changeset 32649 | 2ee9017c7597 |
parent 30904 | ec0224270f90 |
child 34380 | 2b2609379881 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
14665
diff
changeset
|
2 |
* Copyright (c) 1996, 2013, 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? */ |
|
30904 | 71 |
private ClientAuthType clientAuthType = ClientAuthType.CLIENT_AUTH_NONE; |
2 | 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 |
||
7043 | 85 |
// the endpoint identification protocol to use by default |
86 |
private String identificationProtocol = null; |
|
87 |
||
88 |
// The cryptographic algorithm constraints |
|
89 |
private AlgorithmConstraints algorithmConstraints = null; |
|
90 |
||
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
91 |
// The server name indication |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
92 |
Collection<SNIMatcher> sniMatchers = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
93 |
Collections.<SNIMatcher>emptyList(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
94 |
|
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
14665
diff
changeset
|
95 |
/* |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
14665
diff
changeset
|
96 |
* Whether local cipher suites preference in server side should be |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
14665
diff
changeset
|
97 |
* honored during handshaking? |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
14665
diff
changeset
|
98 |
*/ |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
14665
diff
changeset
|
99 |
private boolean preferLocalCipherSuites = false; |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
14665
diff
changeset
|
100 |
|
2 | 101 |
/** |
102 |
* Create an SSL server socket on a port, using a non-default |
|
103 |
* authentication context and a specified connection backlog. |
|
104 |
* |
|
105 |
* @param port the port on which to listen |
|
106 |
* @param backlog how many connections may be pending before |
|
107 |
* the system should start rejecting new requests |
|
108 |
* @param context authentication context for this server |
|
109 |
*/ |
|
110 |
SSLServerSocketImpl(int port, int backlog, SSLContextImpl context) |
|
111 |
throws IOException, SSLException |
|
112 |
{ |
|
113 |
super(port, backlog); |
|
114 |
initServer(context); |
|
115 |
} |
|
116 |
||
117 |
||
118 |
/** |
|
119 |
* Create an SSL server socket on a port, using a specified |
|
120 |
* authentication context and a specified backlog of connections |
|
121 |
* as well as a particular specified network interface. This |
|
122 |
* constructor is used on multihomed hosts, such as those used |
|
123 |
* for firewalls or as routers, to control through which interface |
|
124 |
* a network service is provided. |
|
125 |
* |
|
126 |
* @param port the port on which to listen |
|
127 |
* @param backlog how many connections may be pending before |
|
128 |
* the system should start rejecting new requests |
|
129 |
* @param address the address of the network interface through |
|
130 |
* which connections will be accepted |
|
131 |
* @param context authentication context for this server |
|
132 |
*/ |
|
133 |
SSLServerSocketImpl( |
|
134 |
int port, |
|
135 |
int backlog, |
|
136 |
InetAddress address, |
|
137 |
SSLContextImpl context) |
|
138 |
throws IOException |
|
139 |
{ |
|
140 |
super(port, backlog, address); |
|
141 |
initServer(context); |
|
142 |
} |
|
143 |
||
144 |
||
145 |
/** |
|
146 |
* Creates an unbound server socket. |
|
147 |
*/ |
|
148 |
SSLServerSocketImpl(SSLContextImpl context) throws IOException { |
|
149 |
super(); |
|
150 |
initServer(context); |
|
151 |
} |
|
152 |
||
153 |
||
154 |
/** |
|
155 |
* Initializes the server socket. |
|
156 |
*/ |
|
157 |
private void initServer(SSLContextImpl context) throws SSLException { |
|
158 |
if (context == null) { |
|
159 |
throw new SSLException("No Authentication context given"); |
|
160 |
} |
|
161 |
sslContext = context; |
|
9246
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
162 |
enabledCipherSuites = sslContext.getDefaultCipherSuiteList(true); |
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
163 |
enabledProtocols = sslContext.getDefaultProtocolList(true); |
2 | 164 |
} |
165 |
||
166 |
/** |
|
167 |
* Returns the names of the cipher suites which could be enabled for use |
|
168 |
* on an SSL connection. Normally, only a subset of these will actually |
|
169 |
* be enabled by default, since this list may include cipher suites which |
|
170 |
* do not support the mutual authentication of servers and clients, or |
|
171 |
* which do not protect data confidentiality. Servers may also need |
|
172 |
* certain kinds of certificates to use certain cipher suites. |
|
173 |
* |
|
174 |
* @return an array of cipher suite names |
|
175 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
176 |
@Override |
2 | 177 |
public String[] getSupportedCipherSuites() { |
13815 | 178 |
return sslContext.getSupportedCipherSuiteList().toStringArray(); |
2 | 179 |
} |
180 |
||
181 |
/** |
|
182 |
* Returns the list of cipher suites which are currently enabled |
|
183 |
* for use by newly accepted connections. A null return indicates |
|
184 |
* that the system defaults are in effect. |
|
185 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
186 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30904
diff
changeset
|
187 |
public synchronized String[] getEnabledCipherSuites() { |
2 | 188 |
return enabledCipherSuites.toStringArray(); |
189 |
} |
|
190 |
||
191 |
/** |
|
192 |
* Controls which particular SSL cipher suites are enabled for use |
|
193 |
* by accepted connections. |
|
194 |
* |
|
195 |
* @param suites Names of all the cipher suites to enable; null |
|
196 |
* means to accept system defaults. |
|
197 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
198 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30904
diff
changeset
|
199 |
public synchronized void setEnabledCipherSuites(String[] suites) { |
2 | 200 |
enabledCipherSuites = new CipherSuiteList(suites); |
201 |
} |
|
202 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
203 |
@Override |
2 | 204 |
public String[] getSupportedProtocols() { |
9246
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
205 |
return sslContext.getSuportedProtocolList().toStringArray(); |
2 | 206 |
} |
207 |
||
208 |
/** |
|
209 |
* Controls which protocols are enabled for use. |
|
210 |
* The protocols must have been listed by |
|
211 |
* getSupportedProtocols() as being supported. |
|
212 |
* |
|
213 |
* @param protocols protocols to enable. |
|
214 |
* @exception IllegalArgumentException when one of the protocols |
|
215 |
* named by the parameter is not supported. |
|
216 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
217 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30904
diff
changeset
|
218 |
public synchronized void setEnabledProtocols(String[] protocols) { |
2 | 219 |
enabledProtocols = new ProtocolList(protocols); |
220 |
} |
|
221 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
222 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30904
diff
changeset
|
223 |
public synchronized String[] getEnabledProtocols() { |
2 | 224 |
return enabledProtocols.toStringArray(); |
225 |
} |
|
226 |
||
227 |
/** |
|
228 |
* Controls whether the connections which are accepted must include |
|
229 |
* client authentication. |
|
230 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
231 |
@Override |
2 | 232 |
public void setNeedClientAuth(boolean flag) { |
30904 | 233 |
clientAuthType = (flag ? ClientAuthType.CLIENT_AUTH_REQUIRED : |
234 |
ClientAuthType.CLIENT_AUTH_NONE); |
|
2 | 235 |
} |
236 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
237 |
@Override |
2 | 238 |
public boolean getNeedClientAuth() { |
30904 | 239 |
return (clientAuthType == ClientAuthType.CLIENT_AUTH_REQUIRED); |
2 | 240 |
} |
241 |
||
242 |
/** |
|
243 |
* Controls whether the connections which are accepted should request |
|
244 |
* client authentication. |
|
245 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
246 |
@Override |
2 | 247 |
public void setWantClientAuth(boolean flag) { |
30904 | 248 |
clientAuthType = (flag ? ClientAuthType.CLIENT_AUTH_REQUESTED : |
249 |
ClientAuthType.CLIENT_AUTH_NONE); |
|
2 | 250 |
} |
251 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
252 |
@Override |
2 | 253 |
public boolean getWantClientAuth() { |
30904 | 254 |
return (clientAuthType == ClientAuthType.CLIENT_AUTH_REQUESTED); |
2 | 255 |
} |
256 |
||
257 |
/** |
|
258 |
* Makes the returned sockets act in SSL "client" mode, not the usual |
|
259 |
* server mode. The canonical example of why this is needed is for |
|
260 |
* FTP clients, which accept connections from servers and should be |
|
261 |
* rejoining the already-negotiated SSL connection. |
|
262 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
263 |
@Override |
2 | 264 |
public void setUseClientMode(boolean flag) { |
7039 | 265 |
/* |
266 |
* If we need to change the socket mode and the enabled |
|
267 |
* protocols haven't specifically been set by the user, |
|
268 |
* change them to the corresponding default ones. |
|
269 |
*/ |
|
270 |
if (useServerMode != (!flag) && |
|
9246
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
271 |
sslContext.isDefaultProtocolList(enabledProtocols)) { |
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
272 |
enabledProtocols = sslContext.getDefaultProtocolList(!flag); |
7039 | 273 |
} |
274 |
||
2 | 275 |
useServerMode = !flag; |
276 |
} |
|
277 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
278 |
@Override |
2 | 279 |
public boolean getUseClientMode() { |
280 |
return !useServerMode; |
|
281 |
} |
|
282 |
||
283 |
||
284 |
/** |
|
285 |
* Controls whether new connections may cause creation of new SSL |
|
286 |
* sessions. |
|
287 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
288 |
@Override |
2 | 289 |
public void setEnableSessionCreation(boolean flag) { |
290 |
enableSessionCreation = flag; |
|
291 |
} |
|
292 |
||
293 |
/** |
|
294 |
* Returns true if new connections may cause creation of new SSL |
|
295 |
* sessions. |
|
296 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
297 |
@Override |
2 | 298 |
public boolean getEnableSessionCreation() { |
299 |
return enableSessionCreation; |
|
300 |
} |
|
301 |
||
302 |
/** |
|
7043 | 303 |
* Returns the SSLParameters in effect for newly accepted connections. |
304 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
305 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30904
diff
changeset
|
306 |
public synchronized SSLParameters getSSLParameters() { |
7043 | 307 |
SSLParameters params = super.getSSLParameters(); |
308 |
||
309 |
// the super implementation does not handle the following parameters |
|
310 |
params.setEndpointIdentificationAlgorithm(identificationProtocol); |
|
311 |
params.setAlgorithmConstraints(algorithmConstraints); |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
312 |
params.setSNIMatchers(sniMatchers); |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
14665
diff
changeset
|
313 |
params.setUseCipherSuitesOrder(preferLocalCipherSuites); |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
14665
diff
changeset
|
314 |
|
7043 | 315 |
|
316 |
return params; |
|
317 |
} |
|
318 |
||
319 |
/** |
|
320 |
* Applies SSLParameters to newly accepted connections. |
|
321 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
322 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30904
diff
changeset
|
323 |
public synchronized void setSSLParameters(SSLParameters params) { |
7043 | 324 |
super.setSSLParameters(params); |
325 |
||
326 |
// the super implementation does not handle the following parameters |
|
327 |
identificationProtocol = params.getEndpointIdentificationAlgorithm(); |
|
328 |
algorithmConstraints = params.getAlgorithmConstraints(); |
|
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
14665
diff
changeset
|
329 |
preferLocalCipherSuites = params.getUseCipherSuitesOrder(); |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
330 |
Collection<SNIMatcher> matchers = params.getSNIMatchers(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
331 |
if (matchers != null) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
332 |
sniMatchers = params.getSNIMatchers(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
333 |
} |
7043 | 334 |
} |
335 |
||
336 |
/** |
|
2 | 337 |
* Accept a new SSL connection. This server identifies itself with |
338 |
* information provided in the authentication context which was |
|
339 |
* presented during construction. |
|
340 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
341 |
@Override |
2 | 342 |
public Socket accept() throws IOException { |
343 |
SSLSocketImpl s = new SSLSocketImpl(sslContext, useServerMode, |
|
30904 | 344 |
enabledCipherSuites, clientAuthType, enableSessionCreation, |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
345 |
enabledProtocols, identificationProtocol, algorithmConstraints, |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
14665
diff
changeset
|
346 |
sniMatchers, preferLocalCipherSuites); |
2 | 347 |
|
348 |
implAccept(s); |
|
349 |
s.doneConnect(); |
|
350 |
return s; |
|
351 |
} |
|
352 |
||
353 |
/** |
|
354 |
* Provides a brief description of this SSL socket. |
|
355 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
356 |
@Override |
2 | 357 |
public String toString() { |
358 |
return "[SSL: "+ super.toString() + "]"; |
|
359 |
} |
|
360 |
} |