author | xuelei |
Tue, 02 Jun 2015 04:01:04 +0000 | |
changeset 30904 | ec0224270f90 |
parent 29923 | e3ee0996bedb |
child 31687 | d6eb4f028c60 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
28543 | 2 |
* Copyright (c) 1996, 2014, 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.*; |
|
30904 | 30 |
import java.nio.*; |
2 | 31 |
import java.net.*; |
32 |
import java.security.GeneralSecurityException; |
|
33 |
import java.security.AccessController; |
|
34 |
import java.security.AccessControlContext; |
|
35 |
import java.security.PrivilegedAction; |
|
7043 | 36 |
import java.security.AlgorithmConstraints; |
2 | 37 |
import java.util.*; |
100
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
38 |
import java.util.concurrent.TimeUnit; |
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
39 |
import java.util.concurrent.locks.ReentrantLock; |
2 | 40 |
|
41 |
import javax.crypto.BadPaddingException; |
|
42 |
import javax.net.ssl.*; |
|
29923
e3ee0996bedb
8042332: Enhance thread contexts in security libraries
valeriep
parents:
28550
diff
changeset
|
43 |
import sun.misc.ManagedLocalsThread; |
2 | 44 |
|
45 |
/** |
|
46 |
* Implementation of an SSL socket. This is a normal connection type |
|
47 |
* socket, implementing SSL over some lower level socket, such as TCP. |
|
48 |
* Because it is layered over some lower level socket, it MUST override |
|
49 |
* all default socket methods. |
|
50 |
* |
|
51 |
* <P> This API offers a non-traditional option for establishing SSL |
|
52 |
* connections. You may first establish the connection directly, then pass |
|
53 |
* that connection to the SSL socket constructor with a flag saying which |
|
54 |
* role should be taken in the handshake protocol. (The two ends of the |
|
55 |
* connection must not choose the same role!) This allows setup of SSL |
|
56 |
* proxying or tunneling, and also allows the kind of "role reversal" |
|
57 |
* that is required for most FTP data transfers. |
|
58 |
* |
|
59 |
* @see javax.net.ssl.SSLSocket |
|
60 |
* @see SSLServerSocket |
|
61 |
* |
|
62 |
* @author David Brownell |
|
63 |
*/ |
|
64 |
final public class SSLSocketImpl extends BaseSSLSocketImpl { |
|
65 |
||
66 |
/* |
|
67 |
* ERROR HANDLING GUIDELINES |
|
68 |
* (which exceptions to throw and catch and which not to throw and catch) |
|
69 |
* |
|
70 |
* . if there is an IOException (SocketException) when accessing the |
|
71 |
* underlying Socket, pass it through |
|
72 |
* |
|
73 |
* . do not throw IOExceptions, throw SSLExceptions (or a subclass) |
|
74 |
* |
|
75 |
* . for internal errors (things that indicate a bug in JSSE or a |
|
76 |
* grossly misconfigured J2RE), throw either an SSLException or |
|
77 |
* a RuntimeException at your convenience. |
|
78 |
* |
|
79 |
* . handshaking code (Handshaker or HandshakeMessage) should generally |
|
80 |
* pass through exceptions, but can handle them if they know what to |
|
81 |
* do. |
|
82 |
* |
|
83 |
* . exception chaining should be used for all new code. If you happen |
|
84 |
* to touch old code that does not use chaining, you should change it. |
|
85 |
* |
|
86 |
* . there is a top level exception handler that sits at all entry |
|
87 |
* points from application code to SSLSocket read/write code. It |
|
88 |
* makes sure that all errors are handled (see handleException()). |
|
89 |
* |
|
90 |
* . JSSE internal code should generally not call close(), call |
|
91 |
* closeInternal(). |
|
92 |
*/ |
|
93 |
||
94 |
/* |
|
95 |
* There's a state machine associated with each connection, which |
|
96 |
* among other roles serves to negotiate session changes. |
|
97 |
* |
|
98 |
* - START with constructor, until the TCP connection's around. |
|
99 |
* - HANDSHAKE picks session parameters before allowing traffic. |
|
100 |
* There are many substates due to sequencing requirements |
|
101 |
* for handshake messages. |
|
102 |
* - DATA may be transmitted. |
|
103 |
* - RENEGOTIATE state allows concurrent data and handshaking |
|
104 |
* traffic ("same" substates as HANDSHAKE), and terminates |
|
105 |
* in selection of new session (and connection) parameters |
|
106 |
* - ERROR state immediately precedes abortive disconnect. |
|
107 |
* - SENT_CLOSE sent a close_notify to the peer. For layered, |
|
108 |
* non-autoclose socket, must now read close_notify |
|
109 |
* from peer before closing the connection. For nonlayered or |
|
110 |
* non-autoclose socket, close connection and go onto |
|
111 |
* cs_CLOSED state. |
|
112 |
* - CLOSED after sending close_notify alert, & socket is closed. |
|
113 |
* SSL connection objects are not reused. |
|
114 |
* - APP_CLOSED once the application calls close(). Then it behaves like |
|
115 |
* a closed socket, e.g.. getInputStream() throws an Exception. |
|
116 |
* |
|
117 |
* State affects what SSL record types may legally be sent: |
|
118 |
* |
|
119 |
* - Handshake ... only in HANDSHAKE and RENEGOTIATE states |
|
120 |
* - App Data ... only in DATA and RENEGOTIATE states |
|
121 |
* - Alert ... in HANDSHAKE, DATA, RENEGOTIATE |
|
122 |
* |
|
123 |
* Re what may be received: same as what may be sent, except that |
|
124 |
* HandshakeRequest handshaking messages can come from servers even |
|
125 |
* in the application data state, to request entry to RENEGOTIATE. |
|
126 |
* |
|
127 |
* The state machine within HANDSHAKE and RENEGOTIATE states controls |
|
128 |
* the pending session, not the connection state, until the change |
|
129 |
* cipher spec and "Finished" handshake messages are processed and |
|
130 |
* make the "new" session become the current one. |
|
131 |
* |
|
132 |
* NOTE: details of the SMs always need to be nailed down better. |
|
133 |
* The text above illustrates the core ideas. |
|
134 |
* |
|
135 |
* +---->-------+------>--------->-------+ |
|
136 |
* | | | |
|
137 |
* <-----< ^ ^ <-----< v |
|
138 |
*START>----->HANDSHAKE>----->DATA>----->RENEGOTIATE SENT_CLOSE |
|
139 |
* v v v | | |
|
140 |
* | | | | v |
|
141 |
* +------------+---------------+ v ERROR |
|
142 |
* | | | |
|
143 |
* v | | |
|
144 |
* ERROR>------>----->CLOSED<--------<----+-- + |
|
145 |
* | |
|
146 |
* v |
|
147 |
* APP_CLOSED |
|
148 |
* |
|
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
25859
diff
changeset
|
149 |
* ALSO, note that the purpose of handshaking (renegotiation is |
2 | 150 |
* included) is to assign a different, and perhaps new, session to |
151 |
* the connection. The SSLv3 spec is a bit confusing on that new |
|
152 |
* protocol feature. |
|
153 |
*/ |
|
154 |
private static final int cs_START = 0; |
|
155 |
private static final int cs_HANDSHAKE = 1; |
|
156 |
private static final int cs_DATA = 2; |
|
157 |
private static final int cs_RENEGOTIATE = 3; |
|
158 |
private static final int cs_ERROR = 4; |
|
30904 | 159 |
private static final int cs_SENT_CLOSE = 5; |
2 | 160 |
private static final int cs_CLOSED = 6; |
161 |
private static final int cs_APP_CLOSED = 7; |
|
162 |
||
163 |
/* |
|
164 |
* Drives the protocol state machine. |
|
165 |
*/ |
|
19223
e27cda06fe6a
8013809: deadlock in SSLSocketImpl between between write and close
xuelei
parents:
16913
diff
changeset
|
166 |
private volatile int connectionState; |
2 | 167 |
|
168 |
/* |
|
169 |
* Flag indicating if the next record we receive MUST be a Finished |
|
170 |
* message. Temporarily set during the handshake to ensure that |
|
171 |
* a change cipher spec message is followed by a finished message. |
|
172 |
*/ |
|
173 |
private boolean expectingFinished; |
|
174 |
||
175 |
/* |
|
176 |
* For improved diagnostics, we detail connection closure |
|
177 |
* If the socket is closed (connectionState >= cs_ERROR), |
|
178 |
* closeReason != null indicates if the socket was closed |
|
179 |
* because of an error or because or normal shutdown. |
|
180 |
*/ |
|
181 |
private SSLException closeReason; |
|
182 |
||
183 |
/* |
|
184 |
* Per-connection private state that doesn't change when the |
|
185 |
* session is changed. |
|
186 |
*/ |
|
30904 | 187 |
private ClientAuthType doClientAuth = |
188 |
ClientAuthType.CLIENT_AUTH_NONE; |
|
2 | 189 |
private boolean roleIsServer; |
190 |
private boolean enableSessionCreation = true; |
|
191 |
private String host; |
|
192 |
private boolean autoClose = true; |
|
193 |
private AccessControlContext acc; |
|
194 |
||
7039 | 195 |
// The cipher suites enabled for use on this connection. |
196 |
private CipherSuiteList enabledCipherSuites; |
|
197 |
||
7043 | 198 |
// The endpoint identification protocol |
199 |
private String identificationProtocol = null; |
|
200 |
||
201 |
// The cryptographic algorithm constraints |
|
202 |
private AlgorithmConstraints algorithmConstraints = null; |
|
2 | 203 |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
204 |
// The server name indication and matchers |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
205 |
List<SNIServerName> serverNames = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
206 |
Collections.<SNIServerName>emptyList(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
207 |
Collection<SNIMatcher> sniMatchers = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
208 |
Collections.<SNIMatcher>emptyList(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
209 |
|
2 | 210 |
/* |
211 |
* READ ME * READ ME * READ ME * READ ME * READ ME * READ ME * |
|
212 |
* IMPORTANT STUFF TO UNDERSTANDING THE SYNCHRONIZATION ISSUES. |
|
213 |
* READ ME * READ ME * READ ME * READ ME * READ ME * READ ME * |
|
214 |
* |
|
215 |
* There are several locks here. |
|
216 |
* |
|
217 |
* The primary lock is the per-instance lock used by |
|
218 |
* synchronized(this) and the synchronized methods. It controls all |
|
219 |
* access to things such as the connection state and variables which |
|
220 |
* affect handshaking. If we are inside a synchronized method, we |
|
221 |
* can access the state directly, otherwise, we must use the |
|
222 |
* synchronized equivalents. |
|
223 |
* |
|
224 |
* The handshakeLock is used to ensure that only one thread performs |
|
225 |
* the *complete initial* handshake. If someone is handshaking, any |
|
226 |
* stray application or startHandshake() requests who find the |
|
227 |
* connection state is cs_HANDSHAKE will stall on handshakeLock |
|
228 |
* until handshaking is done. Once the handshake is done, we either |
|
229 |
* succeeded or failed, but we can never go back to the cs_HANDSHAKE |
|
230 |
* or cs_START state again. |
|
231 |
* |
|
232 |
* Note that the read/write() calls here in SSLSocketImpl are not |
|
233 |
* obviously synchronized. In fact, it's very nonintuitive, and |
|
234 |
* requires careful examination of code paths. Grab some coffee, |
|
235 |
* and be careful with any code changes. |
|
236 |
* |
|
237 |
* There can be only three threads active at a time in the I/O |
|
238 |
* subsection of this class. |
|
239 |
* 1. startHandshake |
|
240 |
* 2. AppInputStream |
|
241 |
* 3. AppOutputStream |
|
242 |
* One thread could call startHandshake(). |
|
243 |
* AppInputStream/AppOutputStream read() and write() calls are each |
|
244 |
* synchronized on 'this' in their respective classes, so only one |
|
245 |
* app. thread will be doing a SSLSocketImpl.read() or .write()'s at |
|
246 |
* a time. |
|
247 |
* |
|
248 |
* If handshaking is required (state cs_HANDSHAKE), and |
|
249 |
* getConnectionState() for some/all threads returns cs_HANDSHAKE, |
|
250 |
* only one can grab the handshakeLock, and the rest will stall |
|
251 |
* either on getConnectionState(), or on the handshakeLock if they |
|
252 |
* happen to successfully race through the getConnectionState(). |
|
253 |
* |
|
254 |
* If a writer is doing the initial handshaking, it must create a |
|
255 |
* temporary reader to read the responses from the other side. As a |
|
256 |
* side-effect, the writer's reader will have priority over any |
|
257 |
* other reader. However, the writer's reader is not allowed to |
|
258 |
* consume any application data. When handshakeLock is finally |
|
259 |
* released, we either have a cs_DATA connection, or a |
|
260 |
* cs_CLOSED/cs_ERROR socket. |
|
261 |
* |
|
262 |
* The writeLock is held while writing on a socket connection and |
|
263 |
* also to protect the MAC and cipher for their direction. The |
|
264 |
* writeLock is package private for Handshaker which holds it while |
|
265 |
* writing the ChangeCipherSpec message. |
|
266 |
* |
|
267 |
* To avoid the problem of a thread trying to change operational |
|
268 |
* modes on a socket while handshaking is going on, we synchronize |
|
269 |
* on 'this'. If handshaking has not started yet, we tell the |
|
270 |
* handshaker to change its mode. If handshaking has started, |
|
271 |
* we simply store that request until the next pending session |
|
272 |
* is created, at which time the new handshaker's state is set. |
|
273 |
* |
|
274 |
* The readLock is held during readRecord(), which is responsible |
|
30904 | 275 |
* for reading an SSLInputRecord, decrypting it, and processing it. |
2 | 276 |
* The readLock ensures that these three steps are done atomically |
30904 | 277 |
* and that once started, no other thread can block on SSLInputRecord.read. |
2 | 278 |
* This is necessary so that processing of close_notify alerts |
279 |
* from the peer are handled properly. |
|
280 |
*/ |
|
6856 | 281 |
final private Object handshakeLock = new Object(); |
282 |
final ReentrantLock writeLock = new ReentrantLock(); |
|
283 |
final private Object readLock = new Object(); |
|
2 | 284 |
|
30904 | 285 |
InputRecord inputRecord; |
286 |
OutputRecord outputRecord; |
|
2 | 287 |
|
288 |
/* |
|
6856 | 289 |
* security parameters for secure renegotiation. |
290 |
*/ |
|
291 |
private boolean secureRenegotiation; |
|
292 |
private byte[] clientVerifyData; |
|
293 |
private byte[] serverVerifyData; |
|
294 |
||
295 |
/* |
|
2 | 296 |
* The authentication context holds all information used to establish |
297 |
* who this end of the connection is (certificate chains, private keys, |
|
298 |
* etc) and who is trusted (e.g. as CAs or websites). |
|
299 |
*/ |
|
300 |
private SSLContextImpl sslContext; |
|
301 |
||
302 |
||
303 |
/* |
|
304 |
* This connection is one of (potentially) many associated with |
|
305 |
* any given session. The output of the handshake protocol is a |
|
306 |
* new session ... although all the protocol description talks |
|
307 |
* about changing the cipher spec (and it does change), in fact |
|
308 |
* that's incidental since it's done by changing everything that |
|
309 |
* is associated with a session at the same time. (TLS/IETF may |
|
310 |
* change that to add client authentication w/o new key exchg.) |
|
311 |
*/ |
|
7043 | 312 |
private Handshaker handshaker; |
313 |
private SSLSessionImpl sess; |
|
314 |
private volatile SSLSessionImpl handshakeSession; |
|
2 | 315 |
|
316 |
||
317 |
/* |
|
318 |
* If anyone wants to get notified about handshake completions, |
|
319 |
* they'll show up on this list. |
|
320 |
*/ |
|
321 |
private HashMap<HandshakeCompletedListener, AccessControlContext> |
|
322 |
handshakeListeners; |
|
323 |
||
324 |
/* |
|
325 |
* Reuse the same internal input/output streams. |
|
326 |
*/ |
|
327 |
private InputStream sockInput; |
|
328 |
private OutputStream sockOutput; |
|
329 |
||
330 |
||
331 |
/* |
|
332 |
* These input and output streams block their data in SSL records, |
|
333 |
* and usually arrange integrity and privacy protection for those |
|
334 |
* records. The guts of the SSL protocol are wrapped up in these |
|
335 |
* streams, and in the handshaking that establishes the details of |
|
336 |
* that integrity and privacy protection. |
|
337 |
*/ |
|
338 |
private AppInputStream input; |
|
339 |
private AppOutputStream output; |
|
340 |
||
341 |
/* |
|
7039 | 342 |
* The protocol versions enabled for use on this connection. |
343 |
* |
|
344 |
* Note: we support a pseudo protocol called SSLv2Hello which when |
|
345 |
* set will result in an SSL v2 Hello being sent with SSL (version 3.0) |
|
346 |
* or TLS (version 3.1, 3.2, etc.) version info. |
|
2 | 347 |
*/ |
348 |
private ProtocolList enabledProtocols; |
|
349 |
||
350 |
/* |
|
351 |
* The SSL version associated with this connection. |
|
352 |
*/ |
|
30904 | 353 |
private ProtocolVersion protocolVersion = ProtocolVersion.DEFAULT_TLS; |
2 | 354 |
|
355 |
/* Class and subclass dynamic debugging support */ |
|
356 |
private static final Debug debug = Debug.getInstance("ssl"); |
|
357 |
||
10915 | 358 |
/* |
359 |
* Is it the first application record to write? |
|
360 |
*/ |
|
361 |
private boolean isFirstAppOutputRecord = true; |
|
362 |
||
12428 | 363 |
/* |
364 |
* If AppOutputStream needs to delay writes of small packets, we |
|
365 |
* will use this to store the data until we actually do the write. |
|
366 |
*/ |
|
367 |
private ByteArrayOutputStream heldRecordBuffer = null; |
|
368 |
||
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
369 |
/* |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
370 |
* Whether local cipher suites preference in server side should be |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
371 |
* honored during handshaking? |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
372 |
*/ |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
373 |
private boolean preferLocalCipherSuites = false; |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
374 |
|
30904 | 375 |
/* |
376 |
* The maximum expected network packet size for SSL/TLS/DTLS records. |
|
377 |
*/ |
|
378 |
private int maximumPacketSize = 0; |
|
379 |
||
2 | 380 |
// |
381 |
// CONSTRUCTORS AND INITIALIZATION CODE |
|
382 |
// |
|
383 |
||
384 |
/** |
|
385 |
* Constructs an SSL connection to a named host at a specified port, |
|
386 |
* using the authentication context provided. This endpoint acts as |
|
387 |
* the client, and may rejoin an existing SSL session if appropriate. |
|
388 |
* |
|
389 |
* @param context authentication context to use |
|
390 |
* @param host name of the host with which to connect |
|
391 |
* @param port number of the server's port |
|
392 |
*/ |
|
393 |
SSLSocketImpl(SSLContextImpl context, String host, int port) |
|
394 |
throws IOException, UnknownHostException { |
|
395 |
super(); |
|
396 |
this.host = host; |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
397 |
this.serverNames = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
398 |
Utilities.addToSNIServerNameList(this.serverNames, this.host); |
2 | 399 |
init(context, false); |
2068
cdbc5929b91e
5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents:
1763
diff
changeset
|
400 |
SocketAddress socketAddress = |
cdbc5929b91e
5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents:
1763
diff
changeset
|
401 |
host != null ? new InetSocketAddress(host, port) : |
cdbc5929b91e
5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents:
1763
diff
changeset
|
402 |
new InetSocketAddress(InetAddress.getByName(null), port); |
2 | 403 |
connect(socketAddress, 0); |
404 |
} |
|
405 |
||
406 |
||
407 |
/** |
|
408 |
* Constructs an SSL connection to a server at a specified address. |
|
409 |
* and TCP port, using the authentication context provided. This |
|
410 |
* endpoint acts as the client, and may rejoin an existing SSL session |
|
411 |
* if appropriate. |
|
412 |
* |
|
413 |
* @param context authentication context to use |
|
414 |
* @param address the server's host |
|
415 |
* @param port its port |
|
416 |
*/ |
|
417 |
SSLSocketImpl(SSLContextImpl context, InetAddress host, int port) |
|
418 |
throws IOException { |
|
419 |
super(); |
|
420 |
init(context, false); |
|
421 |
SocketAddress socketAddress = new InetSocketAddress(host, port); |
|
422 |
connect(socketAddress, 0); |
|
423 |
} |
|
424 |
||
425 |
/** |
|
426 |
* Constructs an SSL connection to a named host at a specified port, |
|
427 |
* using the authentication context provided. This endpoint acts as |
|
428 |
* the client, and may rejoin an existing SSL session if appropriate. |
|
429 |
* |
|
430 |
* @param context authentication context to use |
|
431 |
* @param host name of the host with which to connect |
|
432 |
* @param port number of the server's port |
|
433 |
* @param localAddr the local address the socket is bound to |
|
434 |
* @param localPort the local port the socket is bound to |
|
435 |
*/ |
|
436 |
SSLSocketImpl(SSLContextImpl context, String host, int port, |
|
437 |
InetAddress localAddr, int localPort) |
|
438 |
throws IOException, UnknownHostException { |
|
439 |
super(); |
|
440 |
this.host = host; |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
441 |
this.serverNames = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
442 |
Utilities.addToSNIServerNameList(this.serverNames, this.host); |
2 | 443 |
init(context, false); |
444 |
bind(new InetSocketAddress(localAddr, localPort)); |
|
2068
cdbc5929b91e
5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents:
1763
diff
changeset
|
445 |
SocketAddress socketAddress = |
cdbc5929b91e
5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents:
1763
diff
changeset
|
446 |
host != null ? new InetSocketAddress(host, port) : |
cdbc5929b91e
5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents:
1763
diff
changeset
|
447 |
new InetSocketAddress(InetAddress.getByName(null), port); |
2 | 448 |
connect(socketAddress, 0); |
449 |
} |
|
450 |
||
451 |
||
452 |
/** |
|
453 |
* Constructs an SSL connection to a server at a specified address. |
|
454 |
* and TCP port, using the authentication context provided. This |
|
455 |
* endpoint acts as the client, and may rejoin an existing SSL session |
|
456 |
* if appropriate. |
|
457 |
* |
|
458 |
* @param context authentication context to use |
|
459 |
* @param address the server's host |
|
460 |
* @param port its port |
|
461 |
* @param localAddr the local address the socket is bound to |
|
462 |
* @param localPort the local port the socket is bound to |
|
463 |
*/ |
|
464 |
SSLSocketImpl(SSLContextImpl context, InetAddress host, int port, |
|
465 |
InetAddress localAddr, int localPort) |
|
466 |
throws IOException { |
|
467 |
super(); |
|
468 |
init(context, false); |
|
469 |
bind(new InetSocketAddress(localAddr, localPort)); |
|
470 |
SocketAddress socketAddress = new InetSocketAddress(host, port); |
|
471 |
connect(socketAddress, 0); |
|
472 |
} |
|
473 |
||
474 |
/* |
|
475 |
* Package-private constructor used ONLY by SSLServerSocket. The |
|
476 |
* java.net package accepts the TCP connection after this call is |
|
477 |
* made. This just initializes handshake state to use "server mode", |
|
478 |
* giving control over the use of SSL client authentication. |
|
479 |
*/ |
|
480 |
SSLSocketImpl(SSLContextImpl context, boolean serverMode, |
|
30904 | 481 |
CipherSuiteList suites, ClientAuthType clientAuth, |
7043 | 482 |
boolean sessionCreation, ProtocolList protocols, |
483 |
String identificationProtocol, |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
484 |
AlgorithmConstraints algorithmConstraints, |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
485 |
Collection<SNIMatcher> sniMatchers, |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
486 |
boolean preferLocalCipherSuites) throws IOException { |
7043 | 487 |
|
2 | 488 |
super(); |
489 |
doClientAuth = clientAuth; |
|
490 |
enableSessionCreation = sessionCreation; |
|
7043 | 491 |
this.identificationProtocol = identificationProtocol; |
492 |
this.algorithmConstraints = algorithmConstraints; |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
493 |
this.sniMatchers = sniMatchers; |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
494 |
this.preferLocalCipherSuites = preferLocalCipherSuites; |
2 | 495 |
init(context, serverMode); |
496 |
||
497 |
/* |
|
498 |
* Override what was picked out for us. |
|
499 |
*/ |
|
500 |
enabledCipherSuites = suites; |
|
501 |
enabledProtocols = protocols; |
|
502 |
} |
|
503 |
||
504 |
||
505 |
/** |
|
506 |
* Package-private constructor used to instantiate an unconnected |
|
507 |
* socket. The java.net package will connect it, either when the |
|
508 |
* connect() call is made by the application. This instance is |
|
509 |
* meant to set handshake state to use "client mode". |
|
510 |
*/ |
|
511 |
SSLSocketImpl(SSLContextImpl context) { |
|
512 |
super(); |
|
513 |
init(context, false); |
|
514 |
} |
|
515 |
||
516 |
||
517 |
/** |
|
518 |
* Layer SSL traffic over an existing connection, rather than creating |
|
519 |
* a new connection. The existing connection may be used only for SSL |
|
520 |
* traffic (using this SSLSocket) until the SSLSocket.close() call |
|
521 |
* returns. However, if a protocol error is detected, that existing |
|
522 |
* connection is automatically closed. |
|
523 |
* |
|
524 |
* <P> This particular constructor always uses the socket in the |
|
525 |
* role of an SSL client. It may be useful in cases which start |
|
526 |
* using SSL after some initial data transfers, for example in some |
|
527 |
* SSL tunneling applications or as part of some kinds of application |
|
528 |
* protocols which negotiate use of a SSL based security. |
|
529 |
* |
|
530 |
* @param sock the existing connection |
|
531 |
* @param context the authentication context to use |
|
532 |
*/ |
|
533 |
SSLSocketImpl(SSLContextImpl context, Socket sock, String host, |
|
534 |
int port, boolean autoClose) throws IOException { |
|
535 |
super(sock); |
|
536 |
// We always layer over a connected socket |
|
537 |
if (!sock.isConnected()) { |
|
538 |
throw new SocketException("Underlying socket is not connected"); |
|
539 |
} |
|
540 |
this.host = host; |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
541 |
this.serverNames = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
542 |
Utilities.addToSNIServerNameList(this.serverNames, this.host); |
2 | 543 |
init(context, false); |
544 |
this.autoClose = autoClose; |
|
545 |
doneConnect(); |
|
546 |
} |
|
547 |
||
548 |
/** |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
549 |
* Creates a server mode {@link Socket} layered over an |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
550 |
* existing connected socket, and is able to read data which has |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
551 |
* already been consumed/removed from the {@link Socket}'s |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
552 |
* underlying {@link InputStream}. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
553 |
*/ |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
554 |
SSLSocketImpl(SSLContextImpl context, Socket sock, |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
555 |
InputStream consumed, boolean autoClose) throws IOException { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
556 |
super(sock, consumed); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
557 |
// We always layer over a connected socket |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
558 |
if (!sock.isConnected()) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
559 |
throw new SocketException("Underlying socket is not connected"); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
560 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
561 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
562 |
// In server mode, it is not necessary to set host and serverNames. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
563 |
// Otherwise, would require a reverse DNS lookup to get the hostname. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
564 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
565 |
init(context, true); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
566 |
this.autoClose = autoClose; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
567 |
doneConnect(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
568 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
569 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
570 |
/** |
2 | 571 |
* Initializes the client socket. |
572 |
*/ |
|
573 |
private void init(SSLContextImpl context, boolean isServer) { |
|
574 |
sslContext = context; |
|
575 |
sess = SSLSessionImpl.nullSession; |
|
7043 | 576 |
handshakeSession = null; |
2 | 577 |
|
578 |
/* |
|
579 |
* role is as specified, state is START until after |
|
580 |
* the low level connection's established. |
|
581 |
*/ |
|
582 |
roleIsServer = isServer; |
|
583 |
connectionState = cs_START; |
|
584 |
||
6856 | 585 |
// initial security parameters for secure renegotiation |
586 |
secureRenegotiation = false; |
|
587 |
clientVerifyData = new byte[0]; |
|
588 |
serverVerifyData = new byte[0]; |
|
589 |
||
9246
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
590 |
enabledCipherSuites = |
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
591 |
sslContext.getDefaultCipherSuiteList(roleIsServer); |
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
592 |
enabledProtocols = |
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
593 |
sslContext.getDefaultProtocolList(roleIsServer); |
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
594 |
|
30904 | 595 |
inputRecord = new SSLSocketInputRecord();; |
596 |
outputRecord = new SSLSocketOutputRecord(); |
|
597 |
||
598 |
maximumPacketSize = outputRecord.getMaxPacketSize(); |
|
2 | 599 |
|
600 |
// save the acc |
|
601 |
acc = AccessController.getContext(); |
|
602 |
||
603 |
input = new AppInputStream(this); |
|
604 |
output = new AppOutputStream(this); |
|
605 |
} |
|
606 |
||
607 |
/** |
|
608 |
* Connects this socket to the server with a specified timeout |
|
609 |
* value. |
|
610 |
* |
|
611 |
* This method is either called on an unconnected SSLSocketImpl by the |
|
612 |
* application, or it is called in the constructor of a regular |
|
613 |
* SSLSocketImpl. If we are layering on top on another socket, then |
|
614 |
* this method should not be called, because we assume that the |
|
615 |
* underlying socket is already connected by the time it is passed to |
|
616 |
* us. |
|
617 |
* |
|
618 |
* @param endpoint the <code>SocketAddress</code> |
|
619 |
* @param timeout the timeout value to be used, 0 is no timeout |
|
620 |
* @throws IOException if an error occurs during the connection |
|
621 |
* @throws SocketTimeoutException if timeout expires before connecting |
|
622 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
623 |
@Override |
2 | 624 |
public void connect(SocketAddress endpoint, int timeout) |
625 |
throws IOException { |
|
626 |
||
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
627 |
if (isLayered()) { |
2 | 628 |
throw new SocketException("Already connected"); |
629 |
} |
|
630 |
||
631 |
if (!(endpoint instanceof InetSocketAddress)) { |
|
632 |
throw new SocketException( |
|
633 |
"Cannot handle non-Inet socket addresses."); |
|
634 |
} |
|
635 |
||
636 |
super.connect(endpoint, timeout); |
|
637 |
doneConnect(); |
|
638 |
} |
|
639 |
||
640 |
/** |
|
641 |
* Initialize the handshaker and socket streams. |
|
642 |
* |
|
643 |
* Called by connect, the layered constructor, and SSLServerSocket. |
|
644 |
*/ |
|
645 |
void doneConnect() throws IOException { |
|
646 |
/* |
|
647 |
* Save the input and output streams. May be done only after |
|
648 |
* java.net actually connects using the socket "self", else |
|
649 |
* we get some pretty bizarre failure modes. |
|
650 |
*/ |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
651 |
sockInput = super.getInputStream(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
652 |
sockOutput = super.getOutputStream(); |
2 | 653 |
|
30904 | 654 |
inputRecord.setDeliverStream(sockOutput); |
655 |
outputRecord.setDeliverStream(sockOutput); |
|
656 |
||
2 | 657 |
/* |
658 |
* Move to handshaking state, with pending session initialized |
|
659 |
* to defaults and the appropriate kind of handshaker set up. |
|
660 |
*/ |
|
661 |
initHandshaker(); |
|
662 |
} |
|
663 |
||
664 |
synchronized private int getConnectionState() { |
|
665 |
return connectionState; |
|
666 |
} |
|
667 |
||
668 |
synchronized private void setConnectionState(int state) { |
|
669 |
connectionState = state; |
|
670 |
} |
|
671 |
||
672 |
AccessControlContext getAcc() { |
|
673 |
return acc; |
|
674 |
} |
|
675 |
||
676 |
// |
|
677 |
// READING AND WRITING RECORDS |
|
678 |
// |
|
679 |
||
680 |
/* |
|
30904 | 681 |
* Application data record output. |
12428 | 682 |
* |
30904 | 683 |
* Application data can't be sent until the first handshake establishes |
684 |
* a session. |
|
12428 | 685 |
*/ |
30904 | 686 |
void writeRecord(byte[] source, int offset, int length) throws IOException { |
2 | 687 |
/* |
688 |
* The loop is in case of HANDSHAKE --> ERROR transitions, etc |
|
689 |
*/ |
|
30904 | 690 |
// Don't bother to check the emptiness of source applicatoin data |
691 |
// before the security connection established. |
|
692 |
for (boolean readyForApp = false; !readyForApp;) { |
|
2 | 693 |
/* |
694 |
* Not all states support passing application data. We |
|
695 |
* synchronize access to the connection state, so that |
|
696 |
* synchronous handshakes can complete cleanly. |
|
697 |
*/ |
|
698 |
switch (getConnectionState()) { |
|
699 |
||
30904 | 700 |
/* |
701 |
* We've deferred the initial handshaking till just now, |
|
702 |
* when presumably a thread's decided it's OK to block for |
|
703 |
* longish periods of time for I/O purposes (as well as |
|
704 |
* configured the cipher suites it wants to use). |
|
705 |
*/ |
|
706 |
case cs_HANDSHAKE: |
|
707 |
performInitialHandshake(); |
|
708 |
break; |
|
2 | 709 |
|
30904 | 710 |
case cs_DATA: |
711 |
case cs_RENEGOTIATE: |
|
712 |
readyForApp = true; |
|
713 |
break; |
|
2 | 714 |
|
30904 | 715 |
case cs_ERROR: |
716 |
fatal(Alerts.alert_close_notify, |
|
717 |
"error while writing to socket"); |
|
718 |
break; // dummy |
|
2 | 719 |
|
30904 | 720 |
case cs_SENT_CLOSE: |
721 |
case cs_CLOSED: |
|
722 |
case cs_APP_CLOSED: |
|
723 |
// we should never get here (check in AppOutputStream) |
|
724 |
// this is just a fallback |
|
725 |
if (closeReason != null) { |
|
726 |
throw closeReason; |
|
727 |
} else { |
|
728 |
throw new SocketException("Socket closed"); |
|
729 |
} |
|
2 | 730 |
|
30904 | 731 |
/* |
732 |
* Else something's goofy in this state machine's use. |
|
733 |
*/ |
|
734 |
default: |
|
735 |
throw new SSLProtocolException( |
|
736 |
"State error, send app data"); |
|
2 | 737 |
} |
738 |
} |
|
739 |
||
740 |
// |
|
741 |
// Don't bother to really write empty records. We went this |
|
742 |
// far to drive the handshake machinery, for correctness; not |
|
743 |
// writing empty records improves performance by cutting CPU |
|
744 |
// time and network resource usage. However, some protocol |
|
745 |
// implementations are fragile and don't like to see empty |
|
746 |
// records, so this also increases robustness. |
|
747 |
// |
|
30904 | 748 |
if (length > 0) { |
749 |
writeLock.lock(); |
|
750 |
try { |
|
751 |
outputRecord.deliver(source, offset, length); |
|
752 |
} catch (SSLHandshakeException she) { |
|
753 |
// may be record sequence number overflow |
|
754 |
fatal(Alerts.alert_handshake_failure, she); |
|
755 |
} catch (IOException e) { |
|
756 |
fatal(Alerts.alert_unexpected_message, e); |
|
757 |
} finally { |
|
758 |
writeLock.unlock(); |
|
2 | 759 |
} |
760 |
} |
|
7039 | 761 |
|
762 |
/* |
|
763 |
* Check the sequence number state |
|
764 |
* |
|
765 |
* Note that in order to maintain the connection I/O |
|
766 |
* properly, we check the sequence number after the last |
|
767 |
* record writing process. As we request renegotiation |
|
768 |
* or close the connection for wrapped sequence number |
|
769 |
* when there is enough sequence number space left to |
|
770 |
* handle a few more records, so the sequence number |
|
771 |
* of the last record cannot be wrapped. |
|
30904 | 772 |
* |
773 |
* Don't bother to kickstart the renegotiation when the |
|
774 |
* local is asking for it. |
|
7039 | 775 |
*/ |
30904 | 776 |
if ((connectionState == cs_DATA) && outputRecord.seqNumIsHuge()) { |
777 |
/* |
|
778 |
* Ask for renegotiation when need to renew sequence number. |
|
779 |
* |
|
780 |
* Don't bother to kickstart the renegotiation when the local is |
|
781 |
* asking for it. |
|
782 |
*/ |
|
783 |
if (debug != null && Debug.isOn("ssl")) { |
|
784 |
System.out.println(Thread.currentThread().getName() + |
|
785 |
", request renegotiation " + |
|
786 |
"to avoid sequence number overflow"); |
|
787 |
} |
|
10915 | 788 |
|
30904 | 789 |
startHandshake(); |
10915 | 790 |
} |
100
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
791 |
} |
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
792 |
|
10915 | 793 |
/* |
30904 | 794 |
* Alert record output. |
10915 | 795 |
*/ |
30904 | 796 |
void writeAlert(byte level, byte description) throws IOException { |
797 |
||
798 |
// If the record is a close notify alert, we need to honor |
|
799 |
// socket option SO_LINGER. Note that we will try to send |
|
800 |
// the close notify even if the SO_LINGER set to zero. |
|
801 |
if ((description == Alerts.alert_close_notify) && getSoLinger() >= 0) { |
|
802 |
||
803 |
// keep and clear the current thread interruption status. |
|
804 |
boolean interrupted = Thread.interrupted(); |
|
805 |
try { |
|
806 |
if (writeLock.tryLock(getSoLinger(), TimeUnit.SECONDS)) { |
|
807 |
try { |
|
808 |
outputRecord.encodeAlert(level, description); |
|
809 |
} finally { |
|
810 |
writeLock.unlock(); |
|
811 |
} |
|
812 |
} else { |
|
813 |
SSLException ssle = new SSLException( |
|
814 |
"SO_LINGER timeout," + |
|
815 |
" close_notify message cannot be sent."); |
|
816 |
||
817 |
||
818 |
// For layered, non-autoclose sockets, we are not |
|
819 |
// able to bring them into a usable state, so we |
|
820 |
// treat it as fatal error. |
|
821 |
if (isLayered() && !autoClose) { |
|
822 |
// Note that the alert description is |
|
823 |
// specified as -1, so no message will be send |
|
824 |
// to peer anymore. |
|
825 |
fatal((byte)(-1), ssle); |
|
826 |
} else if ((debug != null) && Debug.isOn("ssl")) { |
|
827 |
System.out.println( |
|
828 |
Thread.currentThread().getName() + |
|
829 |
", received Exception: " + ssle); |
|
830 |
} |
|
831 |
||
832 |
// RFC2246 requires that the session becomes |
|
833 |
// unresumable if any connection is terminated |
|
834 |
// without proper close_notify messages with |
|
835 |
// level equal to warning. |
|
836 |
// |
|
837 |
// RFC4346 no longer requires that a session not be |
|
838 |
// resumed if failure to properly close a connection. |
|
839 |
// |
|
840 |
// We choose to make the session unresumable if |
|
841 |
// failed to send the close_notify message. |
|
842 |
// |
|
843 |
sess.invalidate(); |
|
844 |
} |
|
845 |
} catch (InterruptedException ie) { |
|
846 |
// keep interrupted status |
|
847 |
interrupted = true; |
|
848 |
} |
|
849 |
||
850 |
// restore the interrupted status |
|
851 |
if (interrupted) { |
|
852 |
Thread.currentThread().interrupt(); |
|
853 |
} |
|
854 |
} else { |
|
855 |
writeLock.lock(); |
|
856 |
try { |
|
857 |
outputRecord.encodeAlert(level, description); |
|
858 |
} finally { |
|
859 |
writeLock.unlock(); |
|
860 |
} |
|
861 |
} |
|
862 |
||
863 |
// Don't bother to check sequence number overlap here. If sequence |
|
864 |
// number is huge, there should be enough sequence number space to |
|
865 |
// request renegotiation in next application data read and write. |
|
866 |
} |
|
867 |
||
868 |
||
869 |
int bytesInCompletePacket() throws IOException { |
|
870 |
if (getConnectionState() == cs_HANDSHAKE) { |
|
871 |
performInitialHandshake(); |
|
872 |
} |
|
873 |
||
874 |
synchronized (readLock) { |
|
875 |
int state = getConnectionState(); |
|
876 |
if ((state == cs_CLOSED) || |
|
877 |
(state == cs_ERROR) || (state == cs_APP_CLOSED)) { |
|
878 |
return -1; |
|
879 |
} |
|
880 |
||
881 |
try { |
|
882 |
return inputRecord.bytesInCompletePacket(sockInput); |
|
883 |
} catch (EOFException eofe) { |
|
884 |
boolean handshaking = (connectionState <= cs_HANDSHAKE); |
|
885 |
boolean rethrow = requireCloseNotify || handshaking; |
|
886 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
887 |
System.out.println(Thread.currentThread().getName() + |
|
888 |
", received EOFException: " |
|
889 |
+ (rethrow ? "error" : "ignored")); |
|
890 |
} |
|
891 |
||
892 |
if (!rethrow) { |
|
893 |
// treat as if we had received a close_notify |
|
894 |
closeInternal(false); |
|
895 |
} else { |
|
896 |
SSLException e; |
|
897 |
if (handshaking) { |
|
898 |
e = new SSLHandshakeException( |
|
899 |
"Remote host terminated the handshake"); |
|
900 |
} else { |
|
901 |
e = new SSLProtocolException( |
|
902 |
"Remote host terminated the handshake"); |
|
903 |
} |
|
904 |
e.initCause(eofe); |
|
905 |
throw e; |
|
906 |
} |
|
907 |
} |
|
908 |
||
909 |
return -1; |
|
10915 | 910 |
} |
911 |
} |
|
2 | 912 |
|
30904 | 913 |
// the caller have synchronized readLock |
914 |
void expectingFinishFlight() { |
|
915 |
inputRecord.expectingFinishFlight(); |
|
916 |
} |
|
917 |
||
2 | 918 |
/* |
30904 | 919 |
* Read an application data record. |
920 |
* |
|
921 |
* Alerts and handshake messages are internally handled directly. |
|
2 | 922 |
*/ |
30904 | 923 |
int readRecord(ByteBuffer buffer) throws IOException { |
2 | 924 |
if (getConnectionState() == cs_HANDSHAKE) { |
925 |
performInitialHandshake(); |
|
926 |
} |
|
30904 | 927 |
|
928 |
return readRecord(buffer, true); |
|
2 | 929 |
} |
930 |
||
30904 | 931 |
/* |
932 |
* Read a record, no application data input required. |
|
933 |
* |
|
934 |
* Alerts and handshake messages are internally handled directly. |
|
935 |
*/ |
|
936 |
int readRecord(boolean needAppData) throws IOException { |
|
937 |
return readRecord(null, needAppData); |
|
938 |
} |
|
2 | 939 |
|
940 |
/* |
|
941 |
* Clear the pipeline of records from the peer, optionally returning |
|
942 |
* application data. Caller is responsible for knowing that it's |
|
943 |
* possible to do this kind of clearing, if they don't want app |
|
944 |
* data -- e.g. since it's the initial SSL handshake. |
|
945 |
* |
|
946 |
* Don't synchronize (this) during a blocking read() since it |
|
947 |
* protects data which is accessed on the write side as well. |
|
948 |
*/ |
|
30904 | 949 |
private int readRecord(ByteBuffer buffer, boolean needAppData) |
2 | 950 |
throws IOException { |
951 |
int state; |
|
952 |
||
30904 | 953 |
// readLock protects reading and processing of an SSLInputRecord. |
2 | 954 |
// It keeps the reading from sockInput and processing of the record |
955 |
// atomic so that no two threads can be blocked on the |
|
956 |
// read from the same input stream at the same time. |
|
957 |
// This is required for example when a reader thread is |
|
958 |
// blocked on the read and another thread is trying to |
|
959 |
// close the socket. For a non-autoclose, layered socket, |
|
960 |
// the thread performing the close needs to read the close_notify. |
|
961 |
// |
|
962 |
// Use readLock instead of 'this' for locking because |
|
963 |
// 'this' also protects data accessed during writing. |
|
30904 | 964 |
synchronized (readLock) { |
2 | 965 |
/* |
30904 | 966 |
* Read and handle records ... return application data |
967 |
* ONLY if it's needed. |
|
2 | 968 |
*/ |
30904 | 969 |
Plaintext plainText = null; |
970 |
while (((state = getConnectionState()) != cs_CLOSED) && |
|
971 |
(state != cs_ERROR) && (state != cs_APP_CLOSED)) { |
|
972 |
// clean the buffer |
|
973 |
if (buffer != null) { |
|
974 |
buffer.clear(); |
|
975 |
} |
|
2 | 976 |
|
30904 | 977 |
/* |
978 |
* Read a record ... maybe emitting an alert if we get a |
|
979 |
* comprehensible but unsupported "hello" message during |
|
980 |
* format checking (e.g. V2). |
|
981 |
*/ |
|
982 |
try { |
|
983 |
plainText = inputRecord.decode(sockInput, buffer); |
|
984 |
} catch (BadPaddingException bpe) { |
|
985 |
byte alertType = (state != cs_DATA) ? |
|
986 |
Alerts.alert_handshake_failure : |
|
987 |
Alerts.alert_bad_record_mac; |
|
988 |
fatal(alertType, bpe.getMessage(), bpe); |
|
989 |
} catch (SSLProtocolException spe) { |
|
990 |
try { |
|
991 |
fatal(Alerts.alert_unexpected_message, spe); |
|
992 |
} catch (IOException x) { |
|
993 |
// discard this exception, throw the original exception |
|
994 |
} |
|
995 |
throw spe; |
|
996 |
} catch (SSLHandshakeException she) { |
|
997 |
// may be record sequence number overflow |
|
998 |
fatal(Alerts.alert_handshake_failure, she); |
|
999 |
} catch (EOFException eof) { |
|
1000 |
boolean handshaking = (connectionState <= cs_HANDSHAKE); |
|
1001 |
boolean rethrow = requireCloseNotify || handshaking; |
|
1002 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
1003 |
System.out.println(Thread.currentThread().getName() + |
|
1004 |
", received EOFException: " |
|
1005 |
+ (rethrow ? "error" : "ignored")); |
|
1006 |
} |
|
1007 |
if (rethrow) { |
|
1008 |
SSLException e; |
|
1009 |
if (handshaking) { |
|
1010 |
e = new SSLHandshakeException( |
|
1011 |
"Remote host terminated the handshake"); |
|
1012 |
} else { |
|
1013 |
e = new SSLProtocolException( |
|
1014 |
"Remote host terminated the connection"); |
|
5182 | 1015 |
} |
30904 | 1016 |
e.initCause(eof); |
1017 |
throw e; |
|
1018 |
} else { |
|
1019 |
// treat as if we had received a close_notify |
|
1020 |
closeInternal(false); |
|
2 | 1021 |
continue; |
1022 |
} |
|
30904 | 1023 |
} |
1024 |
||
1025 |
// PlainText should never be null. Process input record. |
|
1026 |
int volume = processInputRecord(plainText, needAppData); |
|
1027 |
||
1028 |
if (plainText.contentType == Record.ct_application_data) { |
|
1029 |
return volume; |
|
1030 |
} |
|
1031 |
||
1032 |
if (plainText.contentType == Record.ct_handshake) { |
|
1033 |
if (!needAppData && connectionState == cs_DATA) { |
|
1034 |
return volume; |
|
1035 |
} // otherwise, need to read more for app data. |
|
1036 |
} |
|
2 | 1037 |
|
30904 | 1038 |
// continue to read more net data |
1039 |
} // while |
|
2 | 1040 |
|
30904 | 1041 |
// |
1042 |
// couldn't read, due to some kind of error |
|
1043 |
// |
|
1044 |
return -1; |
|
1045 |
} // readLock synchronization |
|
1046 |
} |
|
1047 |
||
1048 |
/* |
|
1049 |
* Process the plainText input record. |
|
1050 |
*/ |
|
1051 |
private synchronized int processInputRecord( |
|
1052 |
Plaintext plainText, boolean needAppData) throws IOException { |
|
2 | 1053 |
|
30904 | 1054 |
/* |
1055 |
* Process the record. |
|
1056 |
*/ |
|
1057 |
int volume = 0; // no application data |
|
1058 |
switch (plainText.contentType) { |
|
1059 |
case Record.ct_handshake: |
|
1060 |
/* |
|
1061 |
* Handshake messages always go to a pending session |
|
1062 |
* handshaker ... if there isn't one, create one. This |
|
1063 |
* must work asynchronously, for renegotiation. |
|
1064 |
* |
|
1065 |
* NOTE that handshaking will either resume a session |
|
1066 |
* which was in the cache (and which might have other |
|
1067 |
* connections in it already), or else will start a new |
|
1068 |
* session (new keys exchanged) with just this connection |
|
1069 |
* in it. |
|
1070 |
*/ |
|
1071 |
initHandshaker(); |
|
1072 |
if (!handshaker.activated()) { |
|
1073 |
// prior to handshaking, activate the handshake |
|
1074 |
if (connectionState == cs_RENEGOTIATE) { |
|
1075 |
// don't use SSLv2Hello when renegotiating |
|
1076 |
handshaker.activate(protocolVersion); |
|
1077 |
} else { |
|
1078 |
handshaker.activate(null); |
|
1079 |
} |
|
1080 |
} |
|
2 | 1081 |
|
30904 | 1082 |
/* |
1083 |
* process the handshake record ... may contain just |
|
1084 |
* a partial handshake message or multiple messages. |
|
1085 |
* |
|
1086 |
* The handshaker state machine will ensure that it's |
|
1087 |
* a finished message. |
|
1088 |
*/ |
|
1089 |
handshaker.processRecord(plainText.fragment, expectingFinished); |
|
1090 |
expectingFinished = false; |
|
1091 |
||
1092 |
if (handshaker.invalidated) { |
|
1093 |
handshaker = null; |
|
1094 |
inputRecord.setHandshakeHash(null); |
|
1095 |
outputRecord.setHandshakeHash(null); |
|
1096 |
||
1097 |
// if state is cs_RENEGOTIATE, revert it to cs_DATA |
|
1098 |
if (connectionState == cs_RENEGOTIATE) { |
|
1099 |
connectionState = cs_DATA; |
|
2 | 1100 |
} |
30904 | 1101 |
} else if (handshaker.isDone()) { |
1102 |
// reset the parameters for secure renegotiation. |
|
1103 |
secureRenegotiation = |
|
1104 |
handshaker.isSecureRenegotiation(); |
|
1105 |
clientVerifyData = handshaker.getClientVerifyData(); |
|
1106 |
serverVerifyData = handshaker.getServerVerifyData(); |
|
2 | 1107 |
|
30904 | 1108 |
sess = handshaker.getSession(); |
1109 |
handshakeSession = null; |
|
1110 |
handshaker = null; |
|
1111 |
inputRecord.setHandshakeHash(null); |
|
1112 |
outputRecord.setHandshakeHash(null); |
|
1113 |
connectionState = cs_DATA; |
|
28543 | 1114 |
|
2 | 1115 |
// |
30904 | 1116 |
// Tell folk about handshake completion, but do |
1117 |
// it in a separate thread. |
|
2 | 1118 |
// |
30904 | 1119 |
if (handshakeListeners != null) { |
1120 |
HandshakeCompletedEvent event = |
|
1121 |
new HandshakeCompletedEvent(this, sess); |
|
1122 |
||
1123 |
Thread thread = new ManagedLocalsThread( |
|
1124 |
new NotifyHandshake( |
|
1125 |
handshakeListeners.entrySet(), event), |
|
1126 |
"HandshakeCompletedNotify-Thread"); |
|
1127 |
thread.start(); |
|
1128 |
} |
|
1129 |
} |
|
1130 |
||
1131 |
break; |
|
2 | 1132 |
|
30904 | 1133 |
case Record.ct_application_data: |
1134 |
if (connectionState != cs_DATA |
|
1135 |
&& connectionState != cs_RENEGOTIATE |
|
1136 |
&& connectionState != cs_SENT_CLOSE) { |
|
1137 |
throw new SSLProtocolException( |
|
1138 |
"Data received in non-data state: " + |
|
1139 |
connectionState); |
|
1140 |
} |
|
1141 |
if (expectingFinished) { |
|
1142 |
throw new SSLProtocolException |
|
1143 |
("Expecting finished message, received data"); |
|
1144 |
} |
|
1145 |
if (!needAppData) { |
|
1146 |
throw new SSLException("Discarding app data"); |
|
1147 |
} |
|
1148 |
||
1149 |
volume = plainText.fragment.remaining(); |
|
1150 |
break; |
|
1151 |
||
1152 |
case Record.ct_alert: |
|
1153 |
recvAlert(plainText.fragment); |
|
1154 |
break; |
|
7039 | 1155 |
|
30904 | 1156 |
case Record.ct_change_cipher_spec: |
1157 |
if ((connectionState != cs_HANDSHAKE |
|
1158 |
&& connectionState != cs_RENEGOTIATE)) { |
|
1159 |
// For the CCS message arriving in the wrong state |
|
1160 |
fatal(Alerts.alert_unexpected_message, |
|
1161 |
"illegal change cipher spec msg, conn state = " |
|
1162 |
+ connectionState); |
|
1163 |
} else if (plainText.fragment.remaining() != 1 |
|
1164 |
|| plainText.fragment.get() != 1) { |
|
1165 |
// For structural/content issues with the CCS |
|
1166 |
fatal(Alerts.alert_unexpected_message, |
|
1167 |
"Malformed change cipher spec msg"); |
|
1168 |
} |
|
7039 | 1169 |
|
30904 | 1170 |
// |
1171 |
// The first message after a change_cipher_spec |
|
1172 |
// record MUST be a "Finished" handshake record, |
|
1173 |
// else it's a protocol violation. We force this |
|
1174 |
// to be checked by a minor tweak to the state |
|
1175 |
// machine. |
|
1176 |
// |
|
1177 |
handshaker.receiveChangeCipherSpec(); |
|
2 | 1178 |
|
30904 | 1179 |
CipherBox readCipher; |
1180 |
Authenticator readAuthenticator; |
|
1181 |
try { |
|
1182 |
readCipher = handshaker.newReadCipher(); |
|
1183 |
readAuthenticator = handshaker.newReadAuthenticator(); |
|
1184 |
} catch (GeneralSecurityException e) { |
|
1185 |
// can't happen |
|
1186 |
throw new SSLException("Algorithm missing: ", e); |
|
1187 |
} |
|
1188 |
inputRecord.changeReadCiphers(readAuthenticator, readCipher); |
|
2 | 1189 |
|
30904 | 1190 |
// next message MUST be a finished message |
1191 |
expectingFinished = true; |
|
1192 |
||
1193 |
break; |
|
7039 | 1194 |
|
30904 | 1195 |
default: |
1196 |
// |
|
1197 |
// TLS requires that unrecognized records be ignored. |
|
1198 |
// |
|
1199 |
if (debug != null && Debug.isOn("ssl")) { |
|
1200 |
System.out.println(Thread.currentThread().getName() + |
|
1201 |
", Received record type: " + plainText.contentType); |
|
1202 |
} |
|
1203 |
break; |
|
7039 | 1204 |
} |
1205 |
||
1206 |
/* |
|
30904 | 1207 |
* Check the sequence number state |
1208 |
* |
|
1209 |
* Note that in order to maintain the connection I/O |
|
1210 |
* properly, we check the sequence number after the last |
|
1211 |
* record reading process. As we request renegotiation |
|
1212 |
* or close the connection for wrapped sequence number |
|
1213 |
* when there is enough sequence number space left to |
|
1214 |
* handle a few more records, so the sequence number |
|
1215 |
* of the last record cannot be wrapped. |
|
1216 |
* |
|
1217 |
* Don't bother to kickstart the renegotiation when the |
|
1218 |
* local is asking for it. |
|
7039 | 1219 |
*/ |
30904 | 1220 |
if ((connectionState == cs_DATA) && inputRecord.seqNumIsHuge()) { |
7039 | 1221 |
/* |
30904 | 1222 |
* Ask for renegotiation when need to renew sequence number. |
1223 |
* |
|
1224 |
* Don't bother to kickstart the renegotiation when the local is |
|
1225 |
* asking for it. |
|
7039 | 1226 |
*/ |
1227 |
if (debug != null && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1228 |
System.out.println(Thread.currentThread().getName() + |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1229 |
", request renegotiation " + |
7039 | 1230 |
"to avoid sequence number overflow"); |
1231 |
} |
|
1232 |
||
1233 |
startHandshake(); |
|
1234 |
} |
|
30904 | 1235 |
|
1236 |
return volume; |
|
7039 | 1237 |
} |
1238 |
||
30904 | 1239 |
|
2 | 1240 |
// |
1241 |
// HANDSHAKE RELATED CODE |
|
1242 |
// |
|
1243 |
||
1244 |
/** |
|
1245 |
* Return the AppInputStream. For use by Handshaker only. |
|
1246 |
*/ |
|
1247 |
AppInputStream getAppInputStream() { |
|
1248 |
return input; |
|
1249 |
} |
|
1250 |
||
1251 |
/** |
|
7039 | 1252 |
* Return the AppOutputStream. For use by Handshaker only. |
2 | 1253 |
*/ |
7039 | 1254 |
AppOutputStream getAppOutputStream() { |
1255 |
return output; |
|
2 | 1256 |
} |
1257 |
||
1258 |
/** |
|
1259 |
* Initialize the handshaker object. This means: |
|
1260 |
* |
|
1261 |
* . if a handshake is already in progress (state is cs_HANDSHAKE |
|
1262 |
* or cs_RENEGOTIATE), do nothing and return |
|
1263 |
* |
|
1264 |
* . if the socket is already closed, throw an Exception (internal error) |
|
1265 |
* |
|
1266 |
* . otherwise (cs_START or cs_DATA), create the appropriate handshaker |
|
7039 | 1267 |
* object, and advance the connection state (to cs_HANDSHAKE or |
1268 |
* cs_RENEGOTIATE, respectively). |
|
2 | 1269 |
* |
1270 |
* This method is called right after a new socket is created, when |
|
1271 |
* starting renegotiation, or when changing client/ server mode of the |
|
1272 |
* socket. |
|
1273 |
*/ |
|
1274 |
private void initHandshaker() { |
|
1275 |
switch (connectionState) { |
|
1276 |
||
1277 |
// |
|
1278 |
// Starting a new handshake. |
|
1279 |
// |
|
1280 |
case cs_START: |
|
1281 |
case cs_DATA: |
|
1282 |
break; |
|
1283 |
||
1284 |
// |
|
1285 |
// We're already in the middle of a handshake. |
|
1286 |
// |
|
1287 |
case cs_HANDSHAKE: |
|
1288 |
case cs_RENEGOTIATE: |
|
1289 |
return; |
|
1290 |
||
1291 |
// |
|
1292 |
// Anyone allowed to call this routine is required to |
|
1293 |
// do so ONLY if the connection state is reasonable... |
|
1294 |
// |
|
1295 |
default: |
|
1296 |
throw new IllegalStateException("Internal error"); |
|
1297 |
} |
|
1298 |
||
1299 |
// state is either cs_START or cs_DATA |
|
1300 |
if (connectionState == cs_START) { |
|
1301 |
connectionState = cs_HANDSHAKE; |
|
1302 |
} else { // cs_DATA |
|
1303 |
connectionState = cs_RENEGOTIATE; |
|
1304 |
} |
|
1305 |
if (roleIsServer) { |
|
5182 | 1306 |
handshaker = new ServerHandshaker(this, sslContext, |
6856 | 1307 |
enabledProtocols, doClientAuth, |
1308 |
protocolVersion, connectionState == cs_HANDSHAKE, |
|
1309 |
secureRenegotiation, clientVerifyData, serverVerifyData); |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1310 |
handshaker.setSNIMatchers(sniMatchers); |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
1311 |
handshaker.setUseCipherSuitesOrder(preferLocalCipherSuites); |
2 | 1312 |
} else { |
5182 | 1313 |
handshaker = new ClientHandshaker(this, sslContext, |
6856 | 1314 |
enabledProtocols, |
1315 |
protocolVersion, connectionState == cs_HANDSHAKE, |
|
1316 |
secureRenegotiation, clientVerifyData, serverVerifyData); |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1317 |
handshaker.setSNIServerNames(serverNames); |
2 | 1318 |
} |
30904 | 1319 |
handshaker.setMaximumPacketSize(maximumPacketSize); |
7039 | 1320 |
handshaker.setEnabledCipherSuites(enabledCipherSuites); |
2 | 1321 |
handshaker.setEnableSessionCreation(enableSessionCreation); |
1322 |
} |
|
1323 |
||
1324 |
/** |
|
1325 |
* Synchronously perform the initial handshake. |
|
1326 |
* |
|
1327 |
* If the handshake is already in progress, this method blocks until it |
|
1328 |
* is completed. If the initial handshake has already been completed, |
|
1329 |
* it returns immediately. |
|
1330 |
*/ |
|
1331 |
private void performInitialHandshake() throws IOException { |
|
1332 |
// use handshakeLock and the state check to make sure only |
|
1333 |
// one thread performs the handshake |
|
1334 |
synchronized (handshakeLock) { |
|
1335 |
if (getConnectionState() == cs_HANDSHAKE) { |
|
7039 | 1336 |
kickstartHandshake(); |
1337 |
||
2 | 1338 |
/* |
30904 | 1339 |
* All initial handshaking goes through this operation |
1340 |
* until we have a valid SSL connection. |
|
2 | 1341 |
* |
30904 | 1342 |
* Handle handshake messages only, need no application data. |
2 | 1343 |
*/ |
30904 | 1344 |
readRecord(false); |
2 | 1345 |
} |
1346 |
} |
|
1347 |
} |
|
1348 |
||
1349 |
/** |
|
1350 |
* Starts an SSL handshake on this connection. |
|
1351 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1352 |
@Override |
2 | 1353 |
public void startHandshake() throws IOException { |
1354 |
// start an ssl handshake that could be resumed from timeout exception |
|
1355 |
startHandshake(true); |
|
1356 |
} |
|
1357 |
||
1358 |
/** |
|
1359 |
* Starts an ssl handshake on this connection. |
|
1360 |
* |
|
1361 |
* @param resumable indicates the handshake process is resumable from a |
|
1362 |
* certain exception. If <code>resumable</code>, the socket will |
|
1363 |
* be reserved for exceptions like timeout; otherwise, the socket |
|
1364 |
* will be closed, no further communications could be done. |
|
1365 |
*/ |
|
1366 |
private void startHandshake(boolean resumable) throws IOException { |
|
1367 |
checkWrite(); |
|
1368 |
try { |
|
1369 |
if (getConnectionState() == cs_HANDSHAKE) { |
|
1370 |
// do initial handshake |
|
1371 |
performInitialHandshake(); |
|
1372 |
} else { |
|
1373 |
// start renegotiation |
|
1374 |
kickstartHandshake(); |
|
1375 |
} |
|
1376 |
} catch (Exception e) { |
|
1377 |
// shutdown and rethrow (wrapped) exception as appropriate |
|
1378 |
handleException(e, resumable); |
|
1379 |
} |
|
1380 |
} |
|
1381 |
||
1382 |
/** |
|
1383 |
* Kickstart the handshake if it is not already in progress. |
|
1384 |
* This means: |
|
1385 |
* |
|
1386 |
* . if handshaking is already underway, do nothing and return |
|
1387 |
* |
|
1388 |
* . if the socket is not connected or already closed, throw an |
|
1389 |
* Exception. |
|
1390 |
* |
|
1391 |
* . otherwise, call initHandshake() to initialize the handshaker |
|
1392 |
* object and progress the state. Then, send the initial |
|
1393 |
* handshaking message if appropriate (always on clients and |
|
1394 |
* on servers when renegotiating). |
|
1395 |
*/ |
|
1396 |
private synchronized void kickstartHandshake() throws IOException { |
|
7039 | 1397 |
|
2 | 1398 |
switch (connectionState) { |
1399 |
||
1400 |
case cs_HANDSHAKE: |
|
1401 |
// handshaker already setup, proceed |
|
1402 |
break; |
|
1403 |
||
1404 |
case cs_DATA: |
|
6856 | 1405 |
if (!secureRenegotiation && !Handshaker.allowUnsafeRenegotiation) { |
1406 |
throw new SSLHandshakeException( |
|
1407 |
"Insecure renegotiation is not allowed"); |
|
1408 |
} |
|
1409 |
||
1410 |
if (!secureRenegotiation) { |
|
1411 |
if (debug != null && Debug.isOn("handshake")) { |
|
1412 |
System.out.println( |
|
1413 |
"Warning: Using insecure renegotiation"); |
|
1414 |
} |
|
5182 | 1415 |
} |
1416 |
||
2 | 1417 |
// initialize the handshaker, move to cs_RENEGOTIATE |
1418 |
initHandshaker(); |
|
1419 |
break; |
|
1420 |
||
1421 |
case cs_RENEGOTIATE: |
|
1422 |
// handshaking already in progress, return |
|
1423 |
return; |
|
1424 |
||
1425 |
/* |
|
1426 |
* The only way to get a socket in the state is when |
|
1427 |
* you have an unconnected socket. |
|
1428 |
*/ |
|
1429 |
case cs_START: |
|
1430 |
throw new SocketException( |
|
1431 |
"handshaking attempted on unconnected socket"); |
|
1432 |
||
1433 |
default: |
|
1434 |
throw new SocketException("connection is closed"); |
|
1435 |
} |
|
1436 |
||
1437 |
// |
|
1438 |
// Kickstart handshake state machine if we need to ... |
|
1439 |
// |
|
1440 |
// Note that handshaker.kickstart() writes the message |
|
1441 |
// to its HandshakeOutStream, which calls back into |
|
1442 |
// SSLSocketImpl.writeRecord() to send it. |
|
1443 |
// |
|
7039 | 1444 |
if (!handshaker.activated()) { |
1445 |
// prior to handshaking, activate the handshake |
|
1446 |
if (connectionState == cs_RENEGOTIATE) { |
|
1447 |
// don't use SSLv2Hello when renegotiating |
|
1448 |
handshaker.activate(protocolVersion); |
|
1449 |
} else { |
|
1450 |
handshaker.activate(null); |
|
1451 |
} |
|
1452 |
||
2 | 1453 |
if (handshaker instanceof ClientHandshaker) { |
1454 |
// send client hello |
|
1455 |
handshaker.kickstart(); |
|
1456 |
} else { |
|
1457 |
if (connectionState == cs_HANDSHAKE) { |
|
1458 |
// initial handshake, no kickstart message to send |
|
1459 |
} else { |
|
1460 |
// we want to renegotiate, send hello request |
|
1461 |
handshaker.kickstart(); |
|
1462 |
} |
|
1463 |
} |
|
1464 |
} |
|
1465 |
} |
|
1466 |
||
1467 |
// |
|
1468 |
// CLOSURE RELATED CALLS |
|
1469 |
// |
|
1470 |
||
1471 |
/** |
|
1472 |
* Return whether the socket has been explicitly closed by the application. |
|
1473 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1474 |
@Override |
2 | 1475 |
public boolean isClosed() { |
19223
e27cda06fe6a
8013809: deadlock in SSLSocketImpl between between write and close
xuelei
parents:
16913
diff
changeset
|
1476 |
return connectionState == cs_APP_CLOSED; |
2 | 1477 |
} |
1478 |
||
1479 |
/** |
|
1480 |
* Return whether we have reached end-of-file. |
|
1481 |
* |
|
1482 |
* If the socket is not connected, has been shutdown because of an error |
|
1483 |
* or has been closed, throw an Exception. |
|
1484 |
*/ |
|
1485 |
boolean checkEOF() throws IOException { |
|
1486 |
switch (getConnectionState()) { |
|
1487 |
case cs_START: |
|
1488 |
throw new SocketException("Socket is not connected"); |
|
1489 |
||
1490 |
case cs_HANDSHAKE: |
|
1491 |
case cs_DATA: |
|
1492 |
case cs_RENEGOTIATE: |
|
1493 |
case cs_SENT_CLOSE: |
|
1494 |
return false; |
|
1495 |
||
1496 |
case cs_APP_CLOSED: |
|
1497 |
throw new SocketException("Socket is closed"); |
|
1498 |
||
1499 |
case cs_ERROR: |
|
1500 |
case cs_CLOSED: |
|
1501 |
default: |
|
1502 |
// either closed because of error, or normal EOF |
|
1503 |
if (closeReason == null) { |
|
1504 |
return true; |
|
1505 |
} |
|
1506 |
IOException e = new SSLException |
|
1507 |
("Connection has been shutdown: " + closeReason); |
|
1508 |
e.initCause(closeReason); |
|
1509 |
throw e; |
|
1510 |
||
1511 |
} |
|
1512 |
} |
|
1513 |
||
1514 |
/** |
|
1515 |
* Check if we can write data to this socket. If not, throw an IOException. |
|
1516 |
*/ |
|
1517 |
void checkWrite() throws IOException { |
|
1518 |
if (checkEOF() || (getConnectionState() == cs_SENT_CLOSE)) { |
|
1519 |
// we are at EOF, write must throw Exception |
|
1520 |
throw new SocketException("Connection closed by remote host"); |
|
1521 |
} |
|
1522 |
} |
|
1523 |
||
30904 | 1524 |
private void closeSocket() throws IOException { |
1580 | 1525 |
|
2 | 1526 |
if ((debug != null) && Debug.isOn("ssl")) { |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1527 |
System.out.println(Thread.currentThread().getName() + |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1528 |
", called closeSocket()"); |
2 | 1529 |
} |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1530 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1531 |
super.close(); |
2 | 1532 |
} |
1533 |
||
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1534 |
private void closeSocket(boolean selfInitiated) throws IOException { |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1535 |
if ((debug != null) && Debug.isOn("ssl")) { |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1536 |
System.out.println(Thread.currentThread().getName() + |
11018 | 1537 |
", called closeSocket(" + selfInitiated + ")"); |
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1538 |
} |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1539 |
if (!isLayered() || autoClose) { |
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1540 |
super.close(); |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1541 |
} else if (selfInitiated) { |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1542 |
// layered && non-autoclose |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1543 |
// read close_notify alert to clear input stream |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1544 |
waitForClose(false); |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1545 |
} |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1546 |
} |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1547 |
|
2 | 1548 |
/* |
1549 |
* Closing the connection is tricky ... we can't officially close the |
|
1550 |
* connection until we know the other end is ready to go away too, |
|
1551 |
* and if ever the connection gets aborted we must forget session |
|
1552 |
* state (it becomes invalid). |
|
1553 |
*/ |
|
1554 |
||
1555 |
/** |
|
1556 |
* Closes the SSL connection. SSL includes an application level |
|
1557 |
* shutdown handshake; you should close SSL sockets explicitly |
|
1558 |
* rather than leaving it for finalization, so that your remote |
|
1559 |
* peer does not experience a protocol error. |
|
1560 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1561 |
@Override |
2 | 1562 |
public void close() throws IOException { |
1563 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1564 |
System.out.println(Thread.currentThread().getName() + |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1565 |
", called close()"); |
2 | 1566 |
} |
1567 |
closeInternal(true); // caller is initiating close |
|
30904 | 1568 |
|
1569 |
// Clearup the resources. |
|
1570 |
try { |
|
1571 |
synchronized (readLock) { |
|
1572 |
inputRecord.close(); |
|
1573 |
} |
|
1574 |
||
1575 |
writeLock.lock(); |
|
1576 |
try { |
|
1577 |
outputRecord.close(); |
|
1578 |
} finally { |
|
1579 |
writeLock.unlock(); |
|
1580 |
} |
|
1581 |
} catch (IOException ioe) { |
|
1582 |
// ignore |
|
1583 |
} |
|
1584 |
||
2 | 1585 |
setConnectionState(cs_APP_CLOSED); |
1586 |
} |
|
1587 |
||
1588 |
/** |
|
1589 |
* Don't synchronize the whole method because waitForClose() |
|
1590 |
* (which calls readRecord()) might be called. |
|
1591 |
* |
|
1592 |
* @param selfInitiated Indicates which party initiated the close. |
|
1593 |
* If selfInitiated, this side is initiating a close; for layered and |
|
1594 |
* non-autoclose socket, wait for close_notify response. |
|
1595 |
* If !selfInitiated, peer sent close_notify; we reciprocate but |
|
1596 |
* no need to wait for response. |
|
1597 |
*/ |
|
1598 |
private void closeInternal(boolean selfInitiated) throws IOException { |
|
1599 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1600 |
System.out.println(Thread.currentThread().getName() + |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1601 |
", called closeInternal(" + selfInitiated + ")"); |
2 | 1602 |
} |
1603 |
||
1604 |
int state = getConnectionState(); |
|
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1605 |
boolean closeSocketCalled = false; |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1606 |
Throwable cachedThrowable = null; |
2 | 1607 |
try { |
1608 |
switch (state) { |
|
1609 |
case cs_START: |
|
13370
785cedd026db
7179879: SSLSocket connect times out instead of throwing socket closed exception
coffeys
parents:
12428
diff
changeset
|
1610 |
// unconnected socket or handshaking has not been initialized |
785cedd026db
7179879: SSLSocket connect times out instead of throwing socket closed exception
coffeys
parents:
12428
diff
changeset
|
1611 |
closeSocket(selfInitiated); |
2 | 1612 |
break; |
1613 |
||
1614 |
/* |
|
1615 |
* If we're closing down due to error, we already sent (or else |
|
1616 |
* received) the fatal alert ... no niceties, blow the connection |
|
1617 |
* away as quickly as possible (even if we didn't allocate the |
|
1618 |
* socket ourselves; it's unusable, regardless). |
|
1619 |
*/ |
|
1620 |
case cs_ERROR: |
|
1621 |
closeSocket(); |
|
1622 |
break; |
|
1623 |
||
1624 |
/* |
|
1625 |
* Sometimes close() gets called more than once. |
|
1626 |
*/ |
|
1627 |
case cs_CLOSED: |
|
1628 |
case cs_APP_CLOSED: |
|
1629 |
break; |
|
1630 |
||
1631 |
/* |
|
1632 |
* Otherwise we indicate clean termination. |
|
1633 |
*/ |
|
1634 |
// case cs_HANDSHAKE: |
|
1635 |
// case cs_DATA: |
|
1636 |
// case cs_RENEGOTIATE: |
|
1637 |
// case cs_SENT_CLOSE: |
|
1638 |
default: |
|
1639 |
synchronized (this) { |
|
1640 |
if (((state = getConnectionState()) == cs_CLOSED) || |
|
1641 |
(state == cs_ERROR) || (state == cs_APP_CLOSED)) { |
|
1642 |
return; // connection was closed while we waited |
|
1643 |
} |
|
1644 |
if (state != cs_SENT_CLOSE) { |
|
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1645 |
try { |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1646 |
warning(Alerts.alert_close_notify); |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1647 |
connectionState = cs_SENT_CLOSE; |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1648 |
} catch (Throwable th) { |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1649 |
// we need to ensure socket is closed out |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1650 |
// if we encounter any errors. |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1651 |
connectionState = cs_ERROR; |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1652 |
// cache this for later use |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1653 |
cachedThrowable = th; |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1654 |
closeSocketCalled = true; |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1655 |
closeSocket(selfInitiated); |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1656 |
} |
2 | 1657 |
} |
1658 |
} |
|
1659 |
// If state was cs_SENT_CLOSE before, we don't do the actual |
|
1660 |
// closing since it is already in progress. |
|
1661 |
if (state == cs_SENT_CLOSE) { |
|
1662 |
if (debug != null && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1663 |
System.out.println(Thread.currentThread().getName() + |
2 | 1664 |
", close invoked again; state = " + |
1665 |
getConnectionState()); |
|
1666 |
} |
|
1667 |
if (selfInitiated == false) { |
|
1668 |
// We were called because a close_notify message was |
|
1669 |
// received. This may be due to another thread calling |
|
1670 |
// read() or due to our call to waitForClose() below. |
|
1671 |
// In either case, just return. |
|
1672 |
return; |
|
1673 |
} |
|
1674 |
// Another thread explicitly called close(). We need to |
|
1675 |
// wait for the closing to complete before returning. |
|
1676 |
synchronized (this) { |
|
1677 |
while (connectionState < cs_CLOSED) { |
|
1678 |
try { |
|
1679 |
this.wait(); |
|
1680 |
} catch (InterruptedException e) { |
|
1681 |
// ignore |
|
1682 |
} |
|
1683 |
} |
|
1684 |
} |
|
1685 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1686 |
System.out.println(Thread.currentThread().getName() + |
2 | 1687 |
", after primary close; state = " + |
1688 |
getConnectionState()); |
|
1689 |
} |
|
1690 |
return; |
|
1691 |
} |
|
1692 |
||
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1693 |
if (!closeSocketCalled) { |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1694 |
closeSocketCalled = true; |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1695 |
closeSocket(selfInitiated); |
2 | 1696 |
} |
1697 |
||
1698 |
break; |
|
1699 |
} |
|
1700 |
} finally { |
|
1701 |
synchronized (this) { |
|
1702 |
// Upon exit from this method, the state is always >= cs_CLOSED |
|
1703 |
connectionState = (connectionState == cs_APP_CLOSED) |
|
1704 |
? cs_APP_CLOSED : cs_CLOSED; |
|
1705 |
// notify any threads waiting for the closing to finish |
|
1706 |
this.notifyAll(); |
|
1707 |
} |
|
30904 | 1708 |
|
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1709 |
if (cachedThrowable != null) { |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1710 |
/* |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1711 |
* Rethrow the error to the calling method |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1712 |
* The Throwable caught can only be an Error or RuntimeException |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1713 |
*/ |
30904 | 1714 |
if (cachedThrowable instanceof Error) { |
1715 |
throw (Error)cachedThrowable; |
|
1716 |
} else if (cachedThrowable instanceof RuntimeException) { |
|
1717 |
throw (RuntimeException)cachedThrowable; |
|
1718 |
} // Otherwise, unlikely |
|
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1719 |
} |
2 | 1720 |
} |
1721 |
} |
|
1722 |
||
1723 |
/** |
|
1724 |
* Reads a close_notify or a fatal alert from the input stream. |
|
1725 |
* Keep reading records until we get a close_notify or until |
|
1726 |
* the connection is otherwise closed. The close_notify or alert |
|
1727 |
* might be read by another reader, |
|
1728 |
* which will then process the close and set the connection state. |
|
1729 |
*/ |
|
1730 |
void waitForClose(boolean rethrow) throws IOException { |
|
1731 |
if (debug != null && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1732 |
System.out.println(Thread.currentThread().getName() + |
2 | 1733 |
", waiting for close_notify or alert: state " |
1734 |
+ getConnectionState()); |
|
1735 |
} |
|
1736 |
||
1737 |
try { |
|
1738 |
int state; |
|
1739 |
||
1740 |
while (((state = getConnectionState()) != cs_CLOSED) && |
|
1741 |
(state != cs_ERROR) && (state != cs_APP_CLOSED)) { |
|
1742 |
||
1743 |
// Ask for app data and then throw it away |
|
1744 |
try { |
|
30904 | 1745 |
readRecord(true); |
2 | 1746 |
} catch (SocketTimeoutException e) { |
1747 |
// if time out, ignore the exception and continue |
|
1748 |
} |
|
1749 |
} |
|
1750 |
} catch (IOException e) { |
|
1751 |
if (debug != null && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1752 |
System.out.println(Thread.currentThread().getName() + |
2 | 1753 |
", Exception while waiting for close " +e); |
1754 |
} |
|
1755 |
if (rethrow) { |
|
1756 |
throw e; // pass exception up |
|
1757 |
} |
|
1758 |
} |
|
1759 |
} |
|
1760 |
||
1761 |
// |
|
1762 |
// EXCEPTION AND ALERT HANDLING |
|
1763 |
// |
|
1764 |
||
1765 |
/** |
|
1766 |
* Handle an exception. This method is called by top level exception |
|
1767 |
* handlers (in read(), write()) to make sure we always shutdown the |
|
1768 |
* connection correctly and do not pass runtime exception to the |
|
1769 |
* application. |
|
1770 |
*/ |
|
1771 |
void handleException(Exception e) throws IOException { |
|
1772 |
handleException(e, true); |
|
1773 |
} |
|
1774 |
||
1775 |
/** |
|
1776 |
* Handle an exception. This method is called by top level exception |
|
1777 |
* handlers (in read(), write(), startHandshake()) to make sure we |
|
1778 |
* always shutdown the connection correctly and do not pass runtime |
|
1779 |
* exception to the application. |
|
1780 |
* |
|
1781 |
* This method never returns normally, it always throws an IOException. |
|
1782 |
* |
|
1783 |
* We first check if the socket has already been shutdown because of an |
|
1784 |
* error. If so, we just rethrow the exception. If the socket has not |
|
1785 |
* been shutdown, we sent a fatal alert and remember the exception. |
|
1786 |
* |
|
1787 |
* @param e the Exception |
|
1788 |
* @param resumable indicates the caller process is resumable from the |
|
1789 |
* exception. If <code>resumable</code>, the socket will be |
|
1790 |
* reserved for exceptions like timeout; otherwise, the socket |
|
1791 |
* will be closed, no further communications could be done. |
|
1792 |
*/ |
|
1793 |
synchronized private void handleException(Exception e, boolean resumable) |
|
1794 |
throws IOException { |
|
1795 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1796 |
System.out.println(Thread.currentThread().getName() + |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1797 |
", handling exception: " + e.toString()); |
2 | 1798 |
} |
1799 |
||
1800 |
// don't close the Socket in case of timeouts or interrupts if |
|
1801 |
// the process is resumable. |
|
1802 |
if (e instanceof InterruptedIOException && resumable) { |
|
1803 |
throw (IOException)e; |
|
1804 |
} |
|
1805 |
||
1806 |
// if we've already shutdown because of an error, |
|
1807 |
// there is nothing to do except rethrow the exception |
|
1808 |
if (closeReason != null) { |
|
1809 |
if (e instanceof IOException) { // includes SSLException |
|
1810 |
throw (IOException)e; |
|
1811 |
} else { |
|
1812 |
// this is odd, not an IOException. |
|
1813 |
// normally, this should not happen |
|
1814 |
// if closeReason has been already been set |
|
1815 |
throw Alerts.getSSLException(Alerts.alert_internal_error, e, |
|
1816 |
"Unexpected exception"); |
|
1817 |
} |
|
1818 |
} |
|
1819 |
||
1820 |
// need to perform error shutdown |
|
1821 |
boolean isSSLException = (e instanceof SSLException); |
|
30904 | 1822 |
if ((!isSSLException) && (e instanceof IOException)) { |
2 | 1823 |
// IOException from the socket |
1824 |
// this means the TCP connection is already dead |
|
1825 |
// we call fatal just to set the error status |
|
1826 |
try { |
|
1827 |
fatal(Alerts.alert_unexpected_message, e); |
|
1828 |
} catch (IOException ee) { |
|
1829 |
// ignore (IOException wrapped in SSLException) |
|
1830 |
} |
|
1831 |
// rethrow original IOException |
|
1832 |
throw (IOException)e; |
|
1833 |
} |
|
1834 |
||
1835 |
// must be SSLException or RuntimeException |
|
1836 |
byte alertType; |
|
1837 |
if (isSSLException) { |
|
1838 |
if (e instanceof SSLHandshakeException) { |
|
1839 |
alertType = Alerts.alert_handshake_failure; |
|
1840 |
} else { |
|
1841 |
alertType = Alerts.alert_unexpected_message; |
|
1842 |
} |
|
1843 |
} else { |
|
1844 |
alertType = Alerts.alert_internal_error; |
|
1845 |
} |
|
1846 |
fatal(alertType, e); |
|
1847 |
} |
|
1848 |
||
1849 |
/* |
|
1850 |
* Send a warning alert. |
|
1851 |
*/ |
|
1852 |
void warning(byte description) { |
|
1853 |
sendAlert(Alerts.alert_warning, description); |
|
1854 |
} |
|
1855 |
||
1856 |
synchronized void fatal(byte description, String diagnostic) |
|
1857 |
throws IOException { |
|
1858 |
fatal(description, diagnostic, null); |
|
1859 |
} |
|
1860 |
||
1861 |
synchronized void fatal(byte description, Throwable cause) |
|
1862 |
throws IOException { |
|
1863 |
fatal(description, null, cause); |
|
1864 |
} |
|
1865 |
||
1866 |
/* |
|
1867 |
* Send a fatal alert, and throw an exception so that callers will |
|
1868 |
* need to stand on their heads to accidentally continue processing. |
|
1869 |
*/ |
|
1870 |
synchronized void fatal(byte description, String diagnostic, |
|
1871 |
Throwable cause) throws IOException { |
|
30904 | 1872 |
|
1873 |
// Be care of deadlock. Please don't synchronize readLock. |
|
1874 |
try { |
|
1875 |
inputRecord.close(); |
|
1876 |
} catch (IOException ioe) { |
|
1877 |
// ignore |
|
2 | 1878 |
} |
30904 | 1879 |
|
2 | 1880 |
sess.invalidate(); |
7043 | 1881 |
if (handshakeSession != null) { |
1882 |
handshakeSession.invalidate(); |
|
1883 |
} |
|
2 | 1884 |
|
1885 |
int oldState = connectionState; |
|
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1886 |
if (connectionState < cs_ERROR) { |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1887 |
connectionState = cs_ERROR; |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1888 |
} |
2 | 1889 |
|
1890 |
/* |
|
1891 |
* Has there been an error received yet? If not, remember it. |
|
1892 |
* By RFC 2246, we don't bother waiting for a response. |
|
1893 |
* Fatal errors require immediate shutdown. |
|
1894 |
*/ |
|
1895 |
if (closeReason == null) { |
|
1896 |
/* |
|
1897 |
* Try to clear the kernel buffer to avoid TCP connection resets. |
|
1898 |
*/ |
|
1899 |
if (oldState == cs_HANDSHAKE) { |
|
1900 |
sockInput.skip(sockInput.available()); |
|
1901 |
} |
|
100
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
1902 |
|
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
1903 |
// If the description equals -1, the alert won't be sent to peer. |
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
1904 |
if (description != -1) { |
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
1905 |
sendAlert(Alerts.alert_fatal, description); |
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
1906 |
} |
2 | 1907 |
if (cause instanceof SSLException) { // only true if != null |
1908 |
closeReason = (SSLException)cause; |
|
1909 |
} else { |
|
1910 |
closeReason = |
|
1911 |
Alerts.getSSLException(description, cause, diagnostic); |
|
1912 |
} |
|
1913 |
} |
|
1914 |
||
1915 |
/* |
|
1916 |
* Clean up our side. |
|
1917 |
*/ |
|
1918 |
closeSocket(); |
|
1763
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
1580
diff
changeset
|
1919 |
|
30904 | 1920 |
// Be care of deadlock. Please don't synchronize writeLock. |
1921 |
try { |
|
1922 |
outputRecord.close(); |
|
1923 |
} catch (IOException ioe) { |
|
1924 |
// ignore |
|
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1925 |
} |
1763
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
1580
diff
changeset
|
1926 |
|
2 | 1927 |
throw closeReason; |
1928 |
} |
|
1929 |
||
1930 |
||
1931 |
/* |
|
1932 |
* Process an incoming alert ... caller must already have synchronized |
|
1933 |
* access to "this". |
|
1934 |
*/ |
|
30904 | 1935 |
private void recvAlert(ByteBuffer fragment) throws IOException { |
1936 |
byte level = fragment.get(); |
|
1937 |
byte description = fragment.get(); |
|
1938 |
||
2 | 1939 |
if (description == -1) { // check for short message |
1940 |
fatal(Alerts.alert_illegal_parameter, "Short alert message"); |
|
1941 |
} |
|
1942 |
||
1943 |
if (debug != null && (Debug.isOn("record") || |
|
1944 |
Debug.isOn("handshake"))) { |
|
1945 |
synchronized (System.out) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1946 |
System.out.print(Thread.currentThread().getName()); |
2 | 1947 |
System.out.print(", RECV " + protocolVersion + " ALERT: "); |
1948 |
if (level == Alerts.alert_fatal) { |
|
1949 |
System.out.print("fatal, "); |
|
1950 |
} else if (level == Alerts.alert_warning) { |
|
1951 |
System.out.print("warning, "); |
|
1952 |
} else { |
|
1953 |
System.out.print("<level " + (0x0ff & level) + ">, "); |
|
1954 |
} |
|
1955 |
System.out.println(Alerts.alertDescription(description)); |
|
1956 |
} |
|
1957 |
} |
|
1958 |
||
1959 |
if (level == Alerts.alert_warning) { |
|
1960 |
if (description == Alerts.alert_close_notify) { |
|
1961 |
if (connectionState == cs_HANDSHAKE) { |
|
1962 |
fatal(Alerts.alert_unexpected_message, |
|
1963 |
"Received close_notify during handshake"); |
|
1964 |
} else { |
|
1965 |
closeInternal(false); // reply to close |
|
1966 |
} |
|
1967 |
} else { |
|
1968 |
||
1969 |
// |
|
1970 |
// The other legal warnings relate to certificates, |
|
1971 |
// e.g. no_certificate, bad_certificate, etc; these |
|
1972 |
// are important to the handshaking code, which can |
|
1973 |
// also handle illegal protocol alerts if needed. |
|
1974 |
// |
|
1975 |
if (handshaker != null) { |
|
1976 |
handshaker.handshakeAlert(description); |
|
1977 |
} |
|
1978 |
} |
|
1979 |
} else { // fatal or unknown level |
|
1980 |
String reason = "Received fatal alert: " |
|
1981 |
+ Alerts.alertDescription(description); |
|
1982 |
if (closeReason == null) { |
|
1983 |
closeReason = Alerts.getSSLException(description, reason); |
|
1984 |
} |
|
1985 |
fatal(Alerts.alert_unexpected_message, reason); |
|
1986 |
} |
|
1987 |
} |
|
1988 |
||
1989 |
||
1990 |
/* |
|
1991 |
* Emit alerts. Caller must have synchronized with "this". |
|
1992 |
*/ |
|
1993 |
private void sendAlert(byte level, byte description) { |
|
7039 | 1994 |
// the connectionState cannot be cs_START |
100
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
1995 |
if (connectionState >= cs_SENT_CLOSE) { |
2 | 1996 |
return; |
1997 |
} |
|
1998 |
||
7039 | 1999 |
// For initial handshaking, don't send alert message to peer if |
2000 |
// handshaker has not started. |
|
30904 | 2001 |
// |
2002 |
// Shall we send an fatal alter to terminate the connection gracefully? |
|
2003 |
if (connectionState <= cs_HANDSHAKE && |
|
2004 |
(handshaker == null || !handshaker.started() || |
|
2005 |
!handshaker.activated())) { |
|
7039 | 2006 |
return; |
2007 |
} |
|
2008 |
||
2 | 2009 |
boolean useDebug = debug != null && Debug.isOn("ssl"); |
2010 |
if (useDebug) { |
|
2011 |
synchronized (System.out) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2012 |
System.out.print(Thread.currentThread().getName()); |
2 | 2013 |
System.out.print(", SEND " + protocolVersion + " ALERT: "); |
2014 |
if (level == Alerts.alert_fatal) { |
|
2015 |
System.out.print("fatal, "); |
|
2016 |
} else if (level == Alerts.alert_warning) { |
|
2017 |
System.out.print("warning, "); |
|
2018 |
} else { |
|
2019 |
System.out.print("<level = " + (0x0ff & level) + ">, "); |
|
2020 |
} |
|
2021 |
System.out.println("description = " |
|
2022 |
+ Alerts.alertDescription(description)); |
|
2023 |
} |
|
2024 |
} |
|
2025 |
||
2026 |
try { |
|
30904 | 2027 |
writeAlert(level, description); |
2 | 2028 |
} catch (IOException e) { |
2029 |
if (useDebug) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2030 |
System.out.println(Thread.currentThread().getName() + |
2 | 2031 |
", Exception sending alert: " + e); |
2032 |
} |
|
2033 |
} |
|
2034 |
} |
|
2035 |
||
2036 |
// |
|
2037 |
// VARIOUS OTHER METHODS |
|
2038 |
// |
|
2039 |
||
2040 |
// used by Handshaker |
|
30904 | 2041 |
void changeWriteCiphers() throws IOException { |
2042 |
Authenticator writeAuthenticator; |
|
2043 |
CipherBox writeCipher; |
|
2 | 2044 |
try { |
2045 |
writeCipher = handshaker.newWriteCipher(); |
|
16913 | 2046 |
writeAuthenticator = handshaker.newWriteAuthenticator(); |
2 | 2047 |
} catch (GeneralSecurityException e) { |
2048 |
// "can't happen" |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
10128
diff
changeset
|
2049 |
throw new SSLException("Algorithm missing: ", e); |
2 | 2050 |
} |
30904 | 2051 |
outputRecord.changeWriteCiphers(writeAuthenticator, writeCipher); |
2 | 2052 |
} |
2053 |
||
2054 |
/* |
|
2055 |
* Updates the SSL version associated with this connection. |
|
2056 |
* Called from Handshaker once it has determined the negotiated version. |
|
2057 |
*/ |
|
2058 |
synchronized void setVersion(ProtocolVersion protocolVersion) { |
|
2059 |
this.protocolVersion = protocolVersion; |
|
30904 | 2060 |
outputRecord.setVersion(protocolVersion); |
2 | 2061 |
} |
2062 |
||
2063 |
synchronized String getHost() { |
|
2068
cdbc5929b91e
5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents:
1763
diff
changeset
|
2064 |
// Note that the host may be null or empty for localhost. |
cdbc5929b91e
5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents:
1763
diff
changeset
|
2065 |
if (host == null || host.length() == 0) { |
2 | 2066 |
host = getInetAddress().getHostName(); |
2067 |
} |
|
2068 |
return host; |
|
2069 |
} |
|
2070 |
||
5162
0dbedf4fdb8c
6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents:
2068
diff
changeset
|
2071 |
// ONLY used by HttpsClient to setup the URI specified hostname |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2072 |
// |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2073 |
// Please NOTE that this method MUST be called before calling to |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2074 |
// SSLSocket.setSSLParameters(). Otherwise, the {@code host} parameter |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2075 |
// may override SNIHostName in the customized server name indication. |
5162
0dbedf4fdb8c
6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents:
2068
diff
changeset
|
2076 |
synchronized public void setHost(String host) { |
0dbedf4fdb8c
6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents:
2068
diff
changeset
|
2077 |
this.host = host; |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2078 |
this.serverNames = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2079 |
Utilities.addToSNIServerNameList(this.serverNames, this.host); |
5162
0dbedf4fdb8c
6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents:
2068
diff
changeset
|
2080 |
} |
0dbedf4fdb8c
6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents:
2068
diff
changeset
|
2081 |
|
2 | 2082 |
/** |
2083 |
* Gets an input stream to read from the peer on the other side. |
|
2084 |
* Data read from this stream was always integrity protected in |
|
2085 |
* transit, and will usually have been confidentiality protected. |
|
2086 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2087 |
@Override |
2 | 2088 |
synchronized public InputStream getInputStream() throws IOException { |
2089 |
if (isClosed()) { |
|
2090 |
throw new SocketException("Socket is closed"); |
|
2091 |
} |
|
2092 |
||
2093 |
/* |
|
2094 |
* Can't call isConnected() here, because the Handshakers |
|
2095 |
* do some initialization before we actually connect. |
|
2096 |
*/ |
|
2097 |
if (connectionState == cs_START) { |
|
2098 |
throw new SocketException("Socket is not connected"); |
|
2099 |
} |
|
2100 |
||
2101 |
return input; |
|
2102 |
} |
|
2103 |
||
2104 |
/** |
|
2105 |
* Gets an output stream to write to the peer on the other side. |
|
2106 |
* Data written on this stream is always integrity protected, and |
|
2107 |
* will usually be confidentiality protected. |
|
2108 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2109 |
@Override |
2 | 2110 |
synchronized public OutputStream getOutputStream() throws IOException { |
2111 |
if (isClosed()) { |
|
2112 |
throw new SocketException("Socket is closed"); |
|
2113 |
} |
|
2114 |
||
2115 |
/* |
|
2116 |
* Can't call isConnected() here, because the Handshakers |
|
2117 |
* do some initialization before we actually connect. |
|
2118 |
*/ |
|
2119 |
if (connectionState == cs_START) { |
|
2120 |
throw new SocketException("Socket is not connected"); |
|
2121 |
} |
|
2122 |
||
2123 |
return output; |
|
2124 |
} |
|
2125 |
||
2126 |
/** |
|
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
25859
diff
changeset
|
2127 |
* Returns the SSL Session in use by this connection. These can |
2 | 2128 |
* be long lived, and frequently correspond to an entire login session |
2129 |
* for some user. |
|
2130 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2131 |
@Override |
2 | 2132 |
public SSLSession getSession() { |
2133 |
/* |
|
2134 |
* Force a synchronous handshake, if appropriate. |
|
2135 |
*/ |
|
2136 |
if (getConnectionState() == cs_HANDSHAKE) { |
|
2137 |
try { |
|
2138 |
// start handshaking, if failed, the connection will be closed. |
|
2139 |
startHandshake(false); |
|
2140 |
} catch (IOException e) { |
|
2141 |
// handshake failed. log and return a nullSession |
|
2142 |
if (debug != null && Debug.isOn("handshake")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2143 |
System.out.println(Thread.currentThread().getName() + |
2 | 2144 |
", IOException in getSession(): " + e); |
2145 |
} |
|
2146 |
} |
|
2147 |
} |
|
2148 |
synchronized (this) { |
|
2149 |
return sess; |
|
2150 |
} |
|
2151 |
} |
|
2152 |
||
7043 | 2153 |
@Override |
2154 |
synchronized public SSLSession getHandshakeSession() { |
|
2155 |
return handshakeSession; |
|
2156 |
} |
|
2157 |
||
2158 |
synchronized void setHandshakeSession(SSLSessionImpl session) { |
|
30904 | 2159 |
// update the fragment size, which may be negotiated during handshaking |
2160 |
inputRecord.changeFragmentSize(session.getNegotiatedMaxFragSize()); |
|
2161 |
outputRecord.changeFragmentSize(session.getNegotiatedMaxFragSize()); |
|
2162 |
||
7043 | 2163 |
handshakeSession = session; |
2164 |
} |
|
2165 |
||
2 | 2166 |
/** |
2167 |
* Controls whether new connections may cause creation of new SSL |
|
2168 |
* sessions. |
|
2169 |
* |
|
2170 |
* As long as handshaking has not started, we can change |
|
2171 |
* whether we enable session creations. Otherwise, |
|
2172 |
* we will need to wait for the next handshake. |
|
2173 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2174 |
@Override |
2 | 2175 |
synchronized public void setEnableSessionCreation(boolean flag) { |
2176 |
enableSessionCreation = flag; |
|
2177 |
||
7039 | 2178 |
if ((handshaker != null) && !handshaker.activated()) { |
2 | 2179 |
handshaker.setEnableSessionCreation(enableSessionCreation); |
2180 |
} |
|
2181 |
} |
|
2182 |
||
2183 |
/** |
|
2184 |
* Returns true if new connections may cause creation of new SSL |
|
2185 |
* sessions. |
|
2186 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2187 |
@Override |
2 | 2188 |
synchronized public boolean getEnableSessionCreation() { |
2189 |
return enableSessionCreation; |
|
2190 |
} |
|
2191 |
||
2192 |
||
2193 |
/** |
|
2194 |
* Sets the flag controlling whether a server mode socket |
|
2195 |
* *REQUIRES* SSL client authentication. |
|
2196 |
* |
|
2197 |
* As long as handshaking has not started, we can change |
|
2198 |
* whether client authentication is needed. Otherwise, |
|
2199 |
* we will need to wait for the next handshake. |
|
2200 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2201 |
@Override |
2 | 2202 |
synchronized public void setNeedClientAuth(boolean flag) { |
30904 | 2203 |
doClientAuth = (flag ? ClientAuthType.CLIENT_AUTH_REQUIRED : |
2204 |
ClientAuthType.CLIENT_AUTH_NONE); |
|
2 | 2205 |
|
2206 |
if ((handshaker != null) && |
|
2207 |
(handshaker instanceof ServerHandshaker) && |
|
7039 | 2208 |
!handshaker.activated()) { |
2 | 2209 |
((ServerHandshaker) handshaker).setClientAuth(doClientAuth); |
2210 |
} |
|
2211 |
} |
|
2212 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2213 |
@Override |
2 | 2214 |
synchronized public boolean getNeedClientAuth() { |
30904 | 2215 |
return (doClientAuth == ClientAuthType.CLIENT_AUTH_REQUIRED); |
2 | 2216 |
} |
2217 |
||
2218 |
/** |
|
2219 |
* Sets the flag controlling whether a server mode socket |
|
2220 |
* *REQUESTS* SSL client authentication. |
|
2221 |
* |
|
2222 |
* As long as handshaking has not started, we can change |
|
2223 |
* whether client authentication is requested. Otherwise, |
|
2224 |
* we will need to wait for the next handshake. |
|
2225 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2226 |
@Override |
2 | 2227 |
synchronized public void setWantClientAuth(boolean flag) { |
30904 | 2228 |
doClientAuth = (flag ? ClientAuthType.CLIENT_AUTH_REQUESTED : |
2229 |
ClientAuthType.CLIENT_AUTH_NONE); |
|
2 | 2230 |
|
2231 |
if ((handshaker != null) && |
|
2232 |
(handshaker instanceof ServerHandshaker) && |
|
7039 | 2233 |
!handshaker.activated()) { |
2 | 2234 |
((ServerHandshaker) handshaker).setClientAuth(doClientAuth); |
2235 |
} |
|
2236 |
} |
|
2237 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2238 |
@Override |
2 | 2239 |
synchronized public boolean getWantClientAuth() { |
30904 | 2240 |
return (doClientAuth == ClientAuthType.CLIENT_AUTH_REQUESTED); |
2 | 2241 |
} |
2242 |
||
2243 |
||
2244 |
/** |
|
2245 |
* Sets the flag controlling whether the socket is in SSL |
|
2246 |
* client or server mode. Must be called before any SSL |
|
2247 |
* traffic has started. |
|
2248 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2249 |
@Override |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
10128
diff
changeset
|
2250 |
@SuppressWarnings("fallthrough") |
2 | 2251 |
synchronized public void setUseClientMode(boolean flag) { |
2252 |
switch (connectionState) { |
|
2253 |
||
2254 |
case cs_START: |
|
7039 | 2255 |
/* |
2256 |
* If we need to change the socket mode and the enabled |
|
30904 | 2257 |
* protocols and cipher suites haven't specifically been |
2258 |
* set by the user, change them to the corresponding |
|
2259 |
* default ones. |
|
7039 | 2260 |
*/ |
30904 | 2261 |
if (roleIsServer != (!flag)) { |
2262 |
if (sslContext.isDefaultProtocolList(enabledProtocols)) { |
|
2263 |
enabledProtocols = |
|
2264 |
sslContext.getDefaultProtocolList(!flag); |
|
2265 |
} |
|
2266 |
||
2267 |
if (sslContext.isDefaultCipherSuiteList(enabledCipherSuites)) { |
|
2268 |
enabledCipherSuites = |
|
2269 |
sslContext.getDefaultCipherSuiteList(!flag); |
|
2270 |
} |
|
7039 | 2271 |
} |
30904 | 2272 |
|
2 | 2273 |
roleIsServer = !flag; |
2274 |
break; |
|
2275 |
||
2276 |
case cs_HANDSHAKE: |
|
2277 |
/* |
|
2278 |
* If we have a handshaker, but haven't started |
|
2279 |
* SSL traffic, we can throw away our current |
|
2280 |
* handshaker, and start from scratch. Don't |
|
2281 |
* need to call doneConnect() again, we already |
|
2282 |
* have the streams. |
|
2283 |
*/ |
|
2284 |
assert(handshaker != null); |
|
7039 | 2285 |
if (!handshaker.activated()) { |
2286 |
/* |
|
2287 |
* If we need to change the socket mode and the enabled |
|
30904 | 2288 |
* protocols and cipher suites haven't specifically been |
2289 |
* set by the user, change them to the corresponding |
|
2290 |
* default ones. |
|
7039 | 2291 |
*/ |
30904 | 2292 |
if (roleIsServer != (!flag)) { |
2293 |
if (sslContext.isDefaultProtocolList(enabledProtocols)) { |
|
2294 |
enabledProtocols = |
|
2295 |
sslContext.getDefaultProtocolList(!flag); |
|
2296 |
} |
|
2297 |
||
2298 |
if (sslContext.isDefaultCipherSuiteList( |
|
2299 |
enabledCipherSuites)) { |
|
2300 |
enabledCipherSuites = |
|
2301 |
sslContext.getDefaultCipherSuiteList(!flag); |
|
2302 |
} |
|
7039 | 2303 |
} |
30904 | 2304 |
|
2 | 2305 |
roleIsServer = !flag; |
2306 |
connectionState = cs_START; |
|
2307 |
initHandshaker(); |
|
2308 |
break; |
|
2309 |
} |
|
2310 |
||
2311 |
// If handshake has started, that's an error. Fall through... |
|
2312 |
||
2313 |
default: |
|
2314 |
if (debug != null && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2315 |
System.out.println(Thread.currentThread().getName() + |
2 | 2316 |
", setUseClientMode() invoked in state = " + |
2317 |
connectionState); |
|
2318 |
} |
|
2319 |
throw new IllegalArgumentException( |
|
2320 |
"Cannot change mode after SSL traffic has started"); |
|
2321 |
} |
|
2322 |
} |
|
2323 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2324 |
@Override |
2 | 2325 |
synchronized public boolean getUseClientMode() { |
2326 |
return !roleIsServer; |
|
2327 |
} |
|
2328 |
||
2329 |
||
2330 |
/** |
|
2331 |
* Returns the names of the cipher suites which could be enabled for use |
|
2332 |
* on an SSL connection. Normally, only a subset of these will actually |
|
2333 |
* be enabled by default, since this list may include cipher suites which |
|
2334 |
* do not support the mutual authentication of servers and clients, or |
|
2335 |
* which do not protect data confidentiality. Servers may also need |
|
2336 |
* certain kinds of certificates to use certain cipher suites. |
|
2337 |
* |
|
2338 |
* @return an array of cipher suite names |
|
2339 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2340 |
@Override |
2 | 2341 |
public String[] getSupportedCipherSuites() { |
13815 | 2342 |
return sslContext.getSupportedCipherSuiteList().toStringArray(); |
2 | 2343 |
} |
2344 |
||
2345 |
/** |
|
2346 |
* Controls which particular cipher suites are enabled for use on |
|
2347 |
* this connection. The cipher suites must have been listed by |
|
2348 |
* getCipherSuites() as being supported. Even if a suite has been |
|
2349 |
* enabled, it might never be used if no peer supports it or the |
|
2350 |
* requisite certificates (and private keys) are not available. |
|
2351 |
* |
|
2352 |
* @param suites Names of all the cipher suites to enable. |
|
2353 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2354 |
@Override |
2 | 2355 |
synchronized public void setEnabledCipherSuites(String[] suites) { |
2356 |
enabledCipherSuites = new CipherSuiteList(suites); |
|
7039 | 2357 |
if ((handshaker != null) && !handshaker.activated()) { |
2358 |
handshaker.setEnabledCipherSuites(enabledCipherSuites); |
|
2 | 2359 |
} |
2360 |
} |
|
2361 |
||
2362 |
/** |
|
2363 |
* Returns the names of the SSL cipher suites which are currently enabled |
|
2364 |
* for use on this connection. When an SSL socket is first created, |
|
2365 |
* all enabled cipher suites <em>(a)</em> protect data confidentiality, |
|
2366 |
* by traffic encryption, and <em>(b)</em> can mutually authenticate |
|
2367 |
* both clients and servers. Thus, in some environments, this value |
|
2368 |
* might be empty. |
|
2369 |
* |
|
2370 |
* @return an array of cipher suite names |
|
2371 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2372 |
@Override |
2 | 2373 |
synchronized public String[] getEnabledCipherSuites() { |
2374 |
return enabledCipherSuites.toStringArray(); |
|
2375 |
} |
|
2376 |
||
2377 |
||
2378 |
/** |
|
2379 |
* Returns the protocols that are supported by this implementation. |
|
2380 |
* A subset of the supported protocols may be enabled for this connection |
|
7043 | 2381 |
* @return an array of protocol names. |
2 | 2382 |
*/ |
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2383 |
@Override |
2 | 2384 |
public String[] getSupportedProtocols() { |
9246
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
2385 |
return sslContext.getSuportedProtocolList().toStringArray(); |
2 | 2386 |
} |
2387 |
||
2388 |
/** |
|
2389 |
* Controls which protocols are enabled for use on |
|
2390 |
* this connection. The protocols must have been listed by |
|
2391 |
* getSupportedProtocols() as being supported. |
|
2392 |
* |
|
2393 |
* @param protocols protocols to enable. |
|
2394 |
* @exception IllegalArgumentException when one of the protocols |
|
2395 |
* named by the parameter is not supported. |
|
2396 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2397 |
@Override |
2 | 2398 |
synchronized public void setEnabledProtocols(String[] protocols) { |
2399 |
enabledProtocols = new ProtocolList(protocols); |
|
7039 | 2400 |
if ((handshaker != null) && !handshaker.activated()) { |
2 | 2401 |
handshaker.setEnabledProtocols(enabledProtocols); |
2402 |
} |
|
2403 |
} |
|
2404 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2405 |
@Override |
2 | 2406 |
synchronized public String[] getEnabledProtocols() { |
2407 |
return enabledProtocols.toStringArray(); |
|
2408 |
} |
|
2409 |
||
2410 |
/** |
|
2411 |
* Assigns the socket timeout. |
|
2412 |
* @see java.net.Socket#setSoTimeout |
|
2413 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2414 |
@Override |
2 | 2415 |
public void setSoTimeout(int timeout) throws SocketException { |
2416 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2417 |
System.out.println(Thread.currentThread().getName() + |
2 | 2418 |
", setSoTimeout(" + timeout + ") called"); |
2419 |
} |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2420 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2421 |
super.setSoTimeout(timeout); |
2 | 2422 |
} |
2423 |
||
2424 |
/** |
|
2425 |
* Registers an event listener to receive notifications that an |
|
2426 |
* SSL handshake has completed on this connection. |
|
2427 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2428 |
@Override |
2 | 2429 |
public synchronized void addHandshakeCompletedListener( |
2430 |
HandshakeCompletedListener listener) { |
|
2431 |
if (listener == null) { |
|
2432 |
throw new IllegalArgumentException("listener is null"); |
|
2433 |
} |
|
2434 |
if (handshakeListeners == null) { |
|
2435 |
handshakeListeners = new |
|
2436 |
HashMap<HandshakeCompletedListener, AccessControlContext>(4); |
|
2437 |
} |
|
2438 |
handshakeListeners.put(listener, AccessController.getContext()); |
|
2439 |
} |
|
2440 |
||
2441 |
||
2442 |
/** |
|
2443 |
* Removes a previously registered handshake completion listener. |
|
2444 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2445 |
@Override |
2 | 2446 |
public synchronized void removeHandshakeCompletedListener( |
2447 |
HandshakeCompletedListener listener) { |
|
2448 |
if (handshakeListeners == null) { |
|
2449 |
throw new IllegalArgumentException("no listeners"); |
|
2450 |
} |
|
2451 |
if (handshakeListeners.remove(listener) == null) { |
|
2452 |
throw new IllegalArgumentException("listener not registered"); |
|
2453 |
} |
|
2454 |
if (handshakeListeners.isEmpty()) { |
|
2455 |
handshakeListeners = null; |
|
2456 |
} |
|
2457 |
} |
|
2458 |
||
2459 |
/** |
|
7043 | 2460 |
* Returns the SSLParameters in effect for this SSLSocket. |
2 | 2461 |
*/ |
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2462 |
@Override |
7043 | 2463 |
synchronized public SSLParameters getSSLParameters() { |
2464 |
SSLParameters params = super.getSSLParameters(); |
|
2465 |
||
2466 |
// the super implementation does not handle the following parameters |
|
2467 |
params.setEndpointIdentificationAlgorithm(identificationProtocol); |
|
2468 |
params.setAlgorithmConstraints(algorithmConstraints); |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2469 |
params.setSNIMatchers(sniMatchers); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2470 |
params.setServerNames(serverNames); |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
2471 |
params.setUseCipherSuitesOrder(preferLocalCipherSuites); |
30904 | 2472 |
params.setMaximumPacketSize(maximumPacketSize); |
2473 |
||
2474 |
// DTLS handshake retransmissions parameter does not apply here. |
|
7043 | 2475 |
|
2476 |
return params; |
|
2 | 2477 |
} |
2478 |
||
2479 |
/** |
|
7043 | 2480 |
* Applies SSLParameters to this socket. |
2 | 2481 |
*/ |
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2482 |
@Override |
7043 | 2483 |
synchronized public void setSSLParameters(SSLParameters params) { |
2484 |
super.setSSLParameters(params); |
|
2485 |
||
2486 |
// the super implementation does not handle the following parameters |
|
2487 |
identificationProtocol = params.getEndpointIdentificationAlgorithm(); |
|
2488 |
algorithmConstraints = params.getAlgorithmConstraints(); |
|
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
2489 |
preferLocalCipherSuites = params.getUseCipherSuitesOrder(); |
30904 | 2490 |
maximumPacketSize = params.getMaximumPacketSize(); |
2491 |
||
2492 |
// DTLS handshake retransmissions parameter does not apply here. |
|
2493 |
||
2494 |
if (maximumPacketSize != 0) { |
|
2495 |
outputRecord.changePacketSize(maximumPacketSize); |
|
2496 |
} else { |
|
2497 |
// use the implicit maximum packet size. |
|
2498 |
maximumPacketSize = outputRecord.getMaxPacketSize(); |
|
2499 |
} |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2500 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2501 |
List<SNIServerName> sniNames = params.getServerNames(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2502 |
if (sniNames != null) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2503 |
serverNames = sniNames; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2504 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2505 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2506 |
Collection<SNIMatcher> matchers = params.getSNIMatchers(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2507 |
if (matchers != null) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2508 |
sniMatchers = matchers; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2509 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2510 |
|
7043 | 2511 |
if ((handshaker != null) && !handshaker.started()) { |
2512 |
handshaker.setIdentificationProtocol(identificationProtocol); |
|
2513 |
handshaker.setAlgorithmConstraints(algorithmConstraints); |
|
30904 | 2514 |
handshaker.setMaximumPacketSize(maximumPacketSize); |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2515 |
if (roleIsServer) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2516 |
handshaker.setSNIMatchers(sniMatchers); |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
2517 |
handshaker.setUseCipherSuitesOrder(preferLocalCipherSuites); |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2518 |
} else { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2519 |
handshaker.setSNIServerNames(serverNames); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2520 |
} |
7043 | 2521 |
} |
2 | 2522 |
} |
2523 |
||
2524 |
// |
|
2525 |
// We allocate a separate thread to deliver handshake completion |
|
2526 |
// events. This ensures that the notifications don't block the |
|
2527 |
// protocol state machine. |
|
2528 |
// |
|
29923
e3ee0996bedb
8042332: Enhance thread contexts in security libraries
valeriep
parents:
28550
diff
changeset
|
2529 |
private static class NotifyHandshake implements Runnable { |
2 | 2530 |
|
2531 |
private Set<Map.Entry<HandshakeCompletedListener,AccessControlContext>> |
|
2532 |
targets; // who gets notified |
|
2533 |
private HandshakeCompletedEvent event; // the notification |
|
2534 |
||
29923
e3ee0996bedb
8042332: Enhance thread contexts in security libraries
valeriep
parents:
28550
diff
changeset
|
2535 |
NotifyHandshake( |
2 | 2536 |
Set<Map.Entry<HandshakeCompletedListener,AccessControlContext>> |
2537 |
entrySet, HandshakeCompletedEvent e) { |
|
2538 |
||
10128
f505a8514bd7
7065972: Some race condition may happen in SSLSocketImpl class
xuelei
parents:
9514
diff
changeset
|
2539 |
targets = new HashSet<>(entrySet); // clone the entry set |
2 | 2540 |
event = e; |
2541 |
} |
|
2542 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2543 |
@Override |
2 | 2544 |
public void run() { |
10128
f505a8514bd7
7065972: Some race condition may happen in SSLSocketImpl class
xuelei
parents:
9514
diff
changeset
|
2545 |
// Don't need to synchronize, as it only runs in one thread. |
2 | 2546 |
for (Map.Entry<HandshakeCompletedListener,AccessControlContext> |
2547 |
entry : targets) { |
|
2548 |
||
2549 |
final HandshakeCompletedListener l = entry.getKey(); |
|
2550 |
AccessControlContext acc = entry.getValue(); |
|
2551 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2552 |
@Override |
2 | 2553 |
public Void run() { |
2554 |
l.handshakeCompleted(event); |
|
2555 |
return null; |
|
2556 |
} |
|
2557 |
}, acc); |
|
2558 |
} |
|
2559 |
} |
|
2560 |
} |
|
2561 |
||
2562 |
/** |
|
2563 |
* Returns a printable representation of this end of the connection. |
|
2564 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2565 |
@Override |
2 | 2566 |
public String toString() { |
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
21278
diff
changeset
|
2567 |
StringBuilder retval = new StringBuilder(80); |
2 | 2568 |
|
2569 |
retval.append(Integer.toHexString(hashCode())); |
|
2570 |
retval.append("["); |
|
2571 |
retval.append(sess.getCipherSuite()); |
|
2572 |
retval.append(": "); |
|
2573 |
||
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2574 |
retval.append(super.toString()); |
2 | 2575 |
retval.append("]"); |
2576 |
||
2577 |
return retval.toString(); |
|
2578 |
} |
|
2579 |
} |