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