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