author | xuelei |
Tue, 14 Aug 2018 18:16:47 -0700 | |
changeset 51407 | 910f7b56592f |
parent 51141 | 2dd2d73c52f6 |
child 51574 | ed52ea83f830 |
permissions | -rw-r--r-- |
50768 | 1 |
/* |
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
|
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 |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
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 |
* |
|
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. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.security.ssl; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.security.AccessControlContext; |
|
30 |
import java.security.AccessController; |
|
31 |
import java.security.PrivilegedAction; |
|
32 |
import java.util.HashMap; |
|
33 |
import java.util.HashSet; |
|
34 |
import java.util.List; |
|
35 |
import java.util.Map; |
|
36 |
import java.util.Set; |
|
37 |
import javax.net.ssl.HandshakeCompletedEvent; |
|
38 |
import javax.net.ssl.HandshakeCompletedListener; |
|
39 |
import javax.net.ssl.SSLEngineResult.HandshakeStatus; |
|
40 |
import javax.net.ssl.SSLException; |
|
41 |
import javax.net.ssl.SSLSocket; |
|
42 |
import sun.security.ssl.SupportedGroupsExtension.NamedGroup; |
|
43 |
||
44 |
/** |
|
45 |
* SSL/(D)TLS transportation context. |
|
46 |
*/ |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
47 |
class TransportContext implements ConnectionContext { |
50768 | 48 |
final SSLTransport transport; |
49 |
||
50 |
// registered plaintext consumers |
|
51 |
final Map<Byte, SSLConsumer> consumers; |
|
52 |
final AccessControlContext acc; |
|
53 |
||
54 |
final SSLContextImpl sslContext; |
|
55 |
final SSLConfiguration sslConfig; |
|
56 |
final InputRecord inputRecord; |
|
57 |
final OutputRecord outputRecord; |
|
58 |
||
59 |
// connection status |
|
60 |
boolean isUnsureMode; |
|
61 |
boolean isNegotiated = false; |
|
62 |
boolean isBroken = false; |
|
63 |
boolean isInputCloseNotified = false; |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
64 |
boolean peerUserCanceled = false; |
50768 | 65 |
Exception closeReason = null; |
66 |
||
67 |
// negotiated security parameters |
|
68 |
SSLSessionImpl conSession; |
|
69 |
ProtocolVersion protocolVersion; |
|
70 |
String applicationProtocol= null; |
|
71 |
||
72 |
// handshake context |
|
73 |
HandshakeContext handshakeContext = null; |
|
74 |
||
75 |
// connection reserved status for handshake. |
|
76 |
boolean secureRenegotiation = false; |
|
77 |
byte[] clientVerifyData; |
|
78 |
byte[] serverVerifyData; |
|
79 |
||
80 |
// connection sensitive configuration |
|
81 |
List<NamedGroup> serverRequestedNamedGroups; |
|
82 |
||
83 |
CipherSuite cipherSuite; |
|
84 |
private static final byte[] emptyByteArray = new byte[0]; |
|
85 |
||
86 |
// Please never use the transport parameter other than storing a |
|
87 |
// reference to this object. |
|
88 |
// |
|
89 |
// Called by SSLEngineImpl |
|
90 |
TransportContext(SSLContextImpl sslContext, SSLTransport transport, |
|
91 |
InputRecord inputRecord, OutputRecord outputRecord) { |
|
92 |
this(sslContext, transport, new SSLConfiguration(sslContext, true), |
|
93 |
inputRecord, outputRecord, true); |
|
94 |
} |
|
95 |
||
96 |
// Please never use the transport parameter other than storing a |
|
97 |
// reference to this object. |
|
98 |
// |
|
99 |
// Called by SSLSocketImpl |
|
100 |
TransportContext(SSLContextImpl sslContext, SSLTransport transport, |
|
101 |
InputRecord inputRecord, OutputRecord outputRecord, |
|
102 |
boolean isClientMode) { |
|
103 |
this(sslContext, transport, |
|
104 |
new SSLConfiguration(sslContext, isClientMode), |
|
105 |
inputRecord, outputRecord, false); |
|
106 |
} |
|
107 |
||
108 |
// Please never use the transport parameter other than storing a |
|
109 |
// reference to this object. |
|
110 |
// |
|
111 |
// Called by SSLSocketImpl with an existing SSLConfig |
|
112 |
TransportContext(SSLContextImpl sslContext, SSLTransport transport, |
|
113 |
SSLConfiguration sslConfig, |
|
114 |
InputRecord inputRecord, OutputRecord outputRecord) { |
|
115 |
this(sslContext, transport, (SSLConfiguration)sslConfig.clone(), |
|
116 |
inputRecord, outputRecord, false); |
|
117 |
} |
|
118 |
||
119 |
private TransportContext(SSLContextImpl sslContext, SSLTransport transport, |
|
120 |
SSLConfiguration sslConfig, InputRecord inputRecord, |
|
121 |
OutputRecord outputRecord, boolean isUnsureMode) { |
|
122 |
this.transport = transport; |
|
123 |
this.sslContext = sslContext; |
|
124 |
this.inputRecord = inputRecord; |
|
125 |
this.outputRecord = outputRecord; |
|
126 |
this.sslConfig = sslConfig; |
|
127 |
if (this.sslConfig.maximumPacketSize == 0) { |
|
128 |
this.sslConfig.maximumPacketSize = outputRecord.getMaxPacketSize(); |
|
129 |
} |
|
130 |
this.isUnsureMode = isUnsureMode; |
|
131 |
||
132 |
// initial security parameters |
|
133 |
this.conSession = SSLSessionImpl.nullSession; |
|
134 |
this.protocolVersion = this.sslConfig.maximumProtocolVersion; |
|
135 |
this.clientVerifyData = emptyByteArray; |
|
136 |
this.serverVerifyData = emptyByteArray; |
|
137 |
||
138 |
this.acc = AccessController.getContext(); |
|
139 |
this.consumers = new HashMap<>(); |
|
140 |
} |
|
141 |
||
142 |
// Dispatch plaintext to a specific consumer. |
|
143 |
void dispatch(Plaintext plaintext) throws IOException { |
|
144 |
if (plaintext == null) { |
|
145 |
return; |
|
146 |
} |
|
147 |
||
148 |
ContentType ct = ContentType.valueOf(plaintext.contentType); |
|
149 |
if (ct == null) { |
|
150 |
fatal(Alert.UNEXPECTED_MESSAGE, |
|
151 |
"Unknown content type: " + plaintext.contentType); |
|
152 |
return; |
|
153 |
} |
|
154 |
||
155 |
switch (ct) { |
|
156 |
case HANDSHAKE: |
|
157 |
byte type = HandshakeContext.getHandshakeType(this, |
|
158 |
plaintext); |
|
159 |
if (handshakeContext == null) { |
|
160 |
if (type == SSLHandshake.KEY_UPDATE.id || |
|
161 |
type == SSLHandshake.NEW_SESSION_TICKET.id) { |
|
162 |
if (isNegotiated && |
|
163 |
protocolVersion.useTLS13PlusSpec()) { |
|
164 |
handshakeContext = new PostHandshakeContext(this); |
|
165 |
} else { |
|
166 |
fatal(Alert.UNEXPECTED_MESSAGE, |
|
167 |
"Unexpected post-handshake message: " + |
|
168 |
SSLHandshake.nameOf(type)); |
|
169 |
} |
|
170 |
} else { |
|
171 |
handshakeContext = sslConfig.isClientMode ? |
|
172 |
new ClientHandshakeContext(sslContext, this) : |
|
173 |
new ServerHandshakeContext(sslContext, this); |
|
174 |
outputRecord.initHandshaker(); |
|
175 |
} |
|
176 |
} |
|
177 |
handshakeContext.dispatch(type, plaintext); |
|
178 |
break; |
|
179 |
case ALERT: |
|
180 |
Alert.alertConsumer.consume(this, plaintext.fragment); |
|
181 |
break; |
|
182 |
default: |
|
183 |
SSLConsumer consumer = consumers.get(plaintext.contentType); |
|
184 |
if (consumer != null) { |
|
185 |
consumer.consume(this, plaintext.fragment); |
|
186 |
} else { |
|
187 |
fatal(Alert.UNEXPECTED_MESSAGE, |
|
188 |
"Unexpected content: " + plaintext.contentType); |
|
189 |
} |
|
190 |
} |
|
191 |
} |
|
192 |
||
193 |
void kickstart() throws IOException { |
|
194 |
if (isUnsureMode) { |
|
195 |
throw new IllegalStateException("Client/Server mode not yet set."); |
|
196 |
} |
|
197 |
||
198 |
if (outputRecord.isClosed() || inputRecord.isClosed() || isBroken) { |
|
199 |
if (closeReason != null) { |
|
200 |
throw new SSLException( |
|
201 |
"Cannot kickstart, the connection is broken or closed", |
|
202 |
closeReason); |
|
203 |
} else { |
|
204 |
throw new SSLException( |
|
205 |
"Cannot kickstart, the connection is broken or closed"); |
|
206 |
} |
|
207 |
} |
|
208 |
||
209 |
// initialize the handshaker if necessary |
|
210 |
if (handshakeContext == null) { |
|
211 |
// TLS1.3 post-handshake |
|
212 |
if (isNegotiated && protocolVersion.useTLS13PlusSpec()) { |
|
213 |
handshakeContext = new PostHandshakeContext(this); |
|
214 |
} else { |
|
215 |
handshakeContext = sslConfig.isClientMode ? |
|
216 |
new ClientHandshakeContext(sslContext, this) : |
|
217 |
new ServerHandshakeContext(sslContext, this); |
|
218 |
outputRecord.initHandshaker(); |
|
219 |
} |
|
220 |
} |
|
221 |
||
222 |
// kickstart the handshake if needed |
|
223 |
// |
|
224 |
// Need no kickstart message on server side unless the connection |
|
225 |
// has been established. |
|
226 |
if(isNegotiated || sslConfig.isClientMode) { |
|
227 |
handshakeContext.kickstart(); |
|
228 |
} |
|
229 |
} |
|
230 |
||
231 |
boolean isPostHandshakeContext() { |
|
232 |
return handshakeContext != null && |
|
233 |
(handshakeContext instanceof PostHandshakeContext); |
|
234 |
} |
|
235 |
||
236 |
// Note: close_notify is delivered as a warning alert. |
|
237 |
void warning(Alert alert) { |
|
238 |
// For initial handshaking, don't send a warning alert message to peer |
|
239 |
// if handshaker has not started. |
|
240 |
if (isNegotiated || handshakeContext != null) { |
|
241 |
try { |
|
242 |
outputRecord.encodeAlert(Alert.Level.WARNING.level, alert.id); |
|
243 |
} catch (IOException ioe) { |
|
244 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
245 |
SSLLogger.warning( |
|
246 |
"Warning: failed to send warning alert " + alert, ioe); |
|
247 |
} |
|
248 |
} |
|
249 |
} |
|
250 |
} |
|
251 |
||
252 |
void fatal(Alert alert, |
|
253 |
String diagnostic) throws SSLException { |
|
254 |
fatal(alert, diagnostic, null); |
|
255 |
} |
|
256 |
||
257 |
void fatal(Alert alert, Throwable cause) throws SSLException { |
|
258 |
fatal(alert, null, cause); |
|
259 |
} |
|
260 |
||
261 |
void fatal(Alert alert, |
|
262 |
String diagnostic, Throwable cause) throws SSLException { |
|
263 |
fatal(alert, diagnostic, false, cause); |
|
264 |
} |
|
265 |
||
266 |
// Note: close_notify is not delivered via fatal() methods. |
|
267 |
void fatal(Alert alert, String diagnostic, |
|
268 |
boolean recvFatalAlert, Throwable cause) throws SSLException { |
|
269 |
// If we've already shutdown because of an error, there is nothing we |
|
270 |
// can do except rethrow the exception. |
|
271 |
// |
|
272 |
// Most exceptions seen here will be SSLExceptions. We may find the |
|
273 |
// occasional Exception which hasn't been converted to a SSLException, |
|
274 |
// so we'll do it here. |
|
275 |
if (closeReason != null) { |
|
276 |
if (cause == null) { |
|
277 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
278 |
SSLLogger.warning( |
|
279 |
"Closed transport, general or untracked problem"); |
|
280 |
} |
|
281 |
throw alert.createSSLException( |
|
282 |
"Closed transport, general or untracked problem"); |
|
283 |
} |
|
284 |
||
285 |
if (cause instanceof SSLException) { |
|
286 |
throw (SSLException)cause; |
|
287 |
} else { // unlikely, but just in case. |
|
288 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
289 |
SSLLogger.warning( |
|
290 |
"Closed transport, unexpected rethrowing", cause); |
|
291 |
} |
|
292 |
throw alert.createSSLException("Unexpected rethrowing", cause); |
|
293 |
} |
|
294 |
} |
|
295 |
||
296 |
// If we have no further information, make a general-purpose |
|
297 |
// message for folks to see. We generally have one or the other. |
|
298 |
if (diagnostic == null) { |
|
299 |
if (cause == null) { |
|
300 |
diagnostic = "General/Untracked problem"; |
|
301 |
} else { |
|
302 |
diagnostic = cause.getMessage(); |
|
303 |
} |
|
304 |
} |
|
305 |
||
306 |
if (cause == null) { |
|
307 |
cause = alert.createSSLException(diagnostic); |
|
308 |
} |
|
309 |
||
310 |
// shutdown the transport |
|
311 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
312 |
SSLLogger.severe("Fatal (" + alert + "): " + diagnostic, cause); |
|
313 |
} |
|
314 |
||
315 |
// remember the close reason |
|
316 |
if (cause instanceof SSLException) { |
|
317 |
closeReason = (SSLException)cause; |
|
318 |
} else { |
|
319 |
// Including RuntimeException, but we'll throw those down below. |
|
320 |
closeReason = alert.createSSLException(diagnostic, cause); |
|
321 |
} |
|
322 |
||
323 |
// close inbound |
|
324 |
try { |
|
325 |
inputRecord.close(); |
|
326 |
} catch (IOException ioe) { |
|
327 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
328 |
SSLLogger.warning("Fatal: input record closure failed", ioe); |
|
329 |
} |
|
330 |
} |
|
331 |
||
332 |
// invalidate the session |
|
333 |
if (conSession != null) { |
|
334 |
conSession.invalidate(); |
|
335 |
} |
|
336 |
||
337 |
if (handshakeContext != null && |
|
338 |
handshakeContext.handshakeSession != null) { |
|
339 |
handshakeContext.handshakeSession.invalidate(); |
|
340 |
} |
|
341 |
||
342 |
// send fatal alert |
|
343 |
// |
|
344 |
// If we haven't even started handshaking yet, or we are the recipient |
|
345 |
// of a fatal alert, no need to generate a fatal close alert. |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
346 |
if (!recvFatalAlert && !isOutboundClosed() && !isBroken && |
50768 | 347 |
(isNegotiated || handshakeContext != null)) { |
348 |
try { |
|
349 |
outputRecord.encodeAlert(Alert.Level.FATAL.level, alert.id); |
|
350 |
} catch (IOException ioe) { |
|
351 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
352 |
SSLLogger.warning( |
|
353 |
"Fatal: failed to send fatal alert " + alert, ioe); |
|
354 |
} |
|
355 |
} |
|
356 |
} |
|
357 |
||
358 |
// close outbound |
|
359 |
try { |
|
360 |
outputRecord.close(); |
|
361 |
} catch (IOException ioe) { |
|
362 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
363 |
SSLLogger.warning("Fatal: output record closure failed", ioe); |
|
364 |
} |
|
365 |
} |
|
366 |
||
367 |
// terminal handshake context |
|
368 |
if (handshakeContext != null) { |
|
369 |
handshakeContext = null; |
|
370 |
} |
|
371 |
||
372 |
// terminal the transport |
|
373 |
try { |
|
374 |
transport.shutdown(); |
|
375 |
} catch (IOException ioe) { |
|
376 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
377 |
SSLLogger.warning("Fatal: transport closure failed", ioe); |
|
378 |
} |
|
379 |
} finally { |
|
380 |
isBroken = true; |
|
381 |
} |
|
382 |
||
383 |
if (closeReason instanceof SSLException) { |
|
384 |
throw (SSLException)closeReason; |
|
385 |
} else { |
|
386 |
throw (RuntimeException)closeReason; |
|
387 |
} |
|
388 |
} |
|
389 |
||
390 |
void setUseClientMode(boolean useClientMode) { |
|
51141
2dd2d73c52f6
8207250: setUseClientMode post handshake with the same value as before does not throw IAE
weijun
parents:
50768
diff
changeset
|
391 |
// Once handshaking has begun, the mode can not be reset for the |
2dd2d73c52f6
8207250: setUseClientMode post handshake with the same value as before does not throw IAE
weijun
parents:
50768
diff
changeset
|
392 |
// life of this engine. |
2dd2d73c52f6
8207250: setUseClientMode post handshake with the same value as before does not throw IAE
weijun
parents:
50768
diff
changeset
|
393 |
if (handshakeContext != null || isNegotiated) { |
2dd2d73c52f6
8207250: setUseClientMode post handshake with the same value as before does not throw IAE
weijun
parents:
50768
diff
changeset
|
394 |
throw new IllegalArgumentException( |
2dd2d73c52f6
8207250: setUseClientMode post handshake with the same value as before does not throw IAE
weijun
parents:
50768
diff
changeset
|
395 |
"Cannot change mode after SSL traffic has started"); |
2dd2d73c52f6
8207250: setUseClientMode post handshake with the same value as before does not throw IAE
weijun
parents:
50768
diff
changeset
|
396 |
} |
2dd2d73c52f6
8207250: setUseClientMode post handshake with the same value as before does not throw IAE
weijun
parents:
50768
diff
changeset
|
397 |
|
50768 | 398 |
/* |
399 |
* If we need to change the client mode and the enabled |
|
400 |
* protocols and cipher suites haven't specifically been |
|
401 |
* set by the user, change them to the corresponding |
|
402 |
* default ones. |
|
403 |
*/ |
|
404 |
if (sslConfig.isClientMode != useClientMode) { |
|
405 |
if (sslContext.isDefaultProtocolVesions( |
|
406 |
sslConfig.enabledProtocols)) { |
|
407 |
sslConfig.enabledProtocols = |
|
408 |
sslContext.getDefaultProtocolVersions(!useClientMode); |
|
409 |
} |
|
410 |
||
411 |
if (sslContext.isDefaultCipherSuiteList( |
|
412 |
sslConfig.enabledCipherSuites)) { |
|
413 |
sslConfig.enabledCipherSuites = |
|
414 |
sslContext.getDefaultCipherSuites(!useClientMode); |
|
415 |
} |
|
416 |
||
417 |
sslConfig.isClientMode = useClientMode; |
|
418 |
} |
|
419 |
||
420 |
isUnsureMode = false; |
|
421 |
} |
|
422 |
||
423 |
// The OutputRecord is closed and not buffered output record. |
|
424 |
boolean isOutboundDone() { |
|
425 |
return outputRecord.isClosed() && outputRecord.isEmpty(); |
|
426 |
} |
|
427 |
||
428 |
// The OutputRecord is closed, but buffered output record may be still |
|
429 |
// waiting for delivery to the underlying connection. |
|
430 |
boolean isOutboundClosed() { |
|
431 |
return outputRecord.isClosed(); |
|
432 |
} |
|
433 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
434 |
boolean isInboundClosed() { |
50768 | 435 |
return inputRecord.isClosed(); |
436 |
} |
|
437 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
438 |
// Close inbound, no more data should be delivered to the underlying |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
439 |
// transportation connection. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
440 |
void closeInbound() throws SSLException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
441 |
if (isInboundClosed()) { |
50768 | 442 |
return; |
443 |
} |
|
444 |
||
445 |
try { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
446 |
// Important note: check if the initial handshake is started at |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
447 |
// first so that the passiveInboundClose() implementation need not |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
448 |
// to consider the case any more. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
449 |
if (!isInputCloseNotified) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
450 |
// the initial handshake is not started |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
451 |
initiateInboundClose(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
452 |
} else { |
50768 | 453 |
passiveInboundClose(); |
454 |
} |
|
455 |
} catch (IOException ioe) { |
|
456 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
457 |
SSLLogger.warning("inbound closure failed", ioe); |
|
458 |
} |
|
459 |
} |
|
460 |
} |
|
461 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
462 |
// Close the connection passively. The closure could be kickoff by |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
463 |
// receiving a close_notify alert or reaching end_of_file of the socket. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
464 |
// |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
465 |
// Note that this method is called only if the initial handshake has |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
466 |
// started or completed. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
467 |
private void passiveInboundClose() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
468 |
if (!isInboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
469 |
inputRecord.close(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
470 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
471 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
472 |
// For TLS 1.2 and prior version, it is required to respond with |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
473 |
// a close_notify alert of its own and close down the connection |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
474 |
// immediately, discarding any pending writes. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
475 |
if (!isOutboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
476 |
boolean needCloseNotify = SSLConfiguration.acknowledgeCloseNotify; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
477 |
if (!needCloseNotify) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
478 |
if (isNegotiated) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
479 |
if (!protocolVersion.useTLS13PlusSpec()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
480 |
needCloseNotify = true; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
481 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
482 |
} else if (handshakeContext != null) { // initial handshake |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
483 |
ProtocolVersion pv = handshakeContext.negotiatedProtocol; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
484 |
if (pv == null || (!pv.useTLS13PlusSpec())) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
485 |
needCloseNotify = true; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
486 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
487 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
488 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
489 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
490 |
if (needCloseNotify) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
491 |
synchronized (outputRecord) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
492 |
try { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
493 |
// send a close_notify alert |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
494 |
warning(Alert.CLOSE_NOTIFY); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
495 |
} finally { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
496 |
outputRecord.close(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
497 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
498 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
499 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
500 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
501 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
502 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
503 |
// Initiate a inbound close when the handshake is not started. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
504 |
private void initiateInboundClose() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
505 |
if (!isInboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
506 |
inputRecord.close(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
507 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
508 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
509 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
510 |
// Close outbound, no more data should be received from the underlying |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
511 |
// transportation connection. |
50768 | 512 |
void closeOutbound() { |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
513 |
if (isOutboundClosed()) { |
50768 | 514 |
return; |
515 |
} |
|
516 |
||
517 |
try { |
|
518 |
initiateOutboundClose(); |
|
519 |
} catch (IOException ioe) { |
|
520 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
521 |
SSLLogger.warning("outbound closure failed", ioe); |
|
522 |
} |
|
523 |
} |
|
524 |
} |
|
525 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
526 |
// Initiate a close by sending a close_notify alert. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
527 |
private void initiateOutboundClose() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
528 |
boolean useUserCanceled = false; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
529 |
if (!isNegotiated && (handshakeContext != null) && !peerUserCanceled) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
530 |
// initial handshake |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
531 |
useUserCanceled = true; |
50768 | 532 |
} |
533 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
534 |
// Need a lock here so that the user_canceled alert and the |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
535 |
// close_notify alert can be delivered together. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
536 |
synchronized (outputRecord) { |
50768 | 537 |
try { |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
538 |
// send a user_canceled alert if needed. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
539 |
if (useUserCanceled) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
540 |
warning(Alert.USER_CANCELED); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
541 |
} |
50768 | 542 |
|
543 |
// send a close_notify alert |
|
544 |
warning(Alert.CLOSE_NOTIFY); |
|
545 |
} finally { |
|
546 |
outputRecord.close(); |
|
547 |
} |
|
548 |
} |
|
549 |
} |
|
550 |
||
551 |
// Note; HandshakeStatus.FINISHED status is retrieved in other places. |
|
552 |
HandshakeStatus getHandshakeStatus() { |
|
553 |
if (!outputRecord.isEmpty()) { |
|
554 |
// If no handshaking, special case to wrap alters or |
|
555 |
// post-handshake messages. |
|
556 |
return HandshakeStatus.NEED_WRAP; |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
557 |
} else if (isOutboundClosed() && isInboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
558 |
return HandshakeStatus.NOT_HANDSHAKING; |
50768 | 559 |
} else if (handshakeContext != null) { |
560 |
if (!handshakeContext.delegatedActions.isEmpty()) { |
|
561 |
return HandshakeStatus.NEED_TASK; |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
562 |
} else if (!isInboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
563 |
if (sslContext.isDTLS() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
564 |
!inputRecord.isEmpty()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
565 |
return HandshakeStatus.NEED_UNWRAP_AGAIN; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
566 |
} else { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
567 |
return HandshakeStatus.NEED_UNWRAP; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
568 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
569 |
} else if (!isOutboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
570 |
// Special case that the inbound was closed, but outbound open. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
571 |
return HandshakeStatus.NEED_WRAP; |
50768 | 572 |
} |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
573 |
} else if (isOutboundClosed() && !isInboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
574 |
// Special case that the outbound was closed, but inbound open. |
50768 | 575 |
return HandshakeStatus.NEED_UNWRAP; |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
576 |
} else if (!isOutboundClosed() && isInboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
577 |
// Special case that the inbound was closed, but outbound open. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
578 |
return HandshakeStatus.NEED_WRAP; |
50768 | 579 |
} |
580 |
||
581 |
return HandshakeStatus.NOT_HANDSHAKING; |
|
582 |
} |
|
583 |
||
584 |
HandshakeStatus finishHandshake() { |
|
585 |
if (protocolVersion.useTLS13PlusSpec()) { |
|
586 |
outputRecord.tc = this; |
|
587 |
inputRecord.tc = this; |
|
588 |
cipherSuite = handshakeContext.negotiatedCipherSuite; |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
589 |
inputRecord.readCipher.baseSecret = |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
590 |
handshakeContext.baseReadSecret; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
591 |
outputRecord.writeCipher.baseSecret = |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
51141
diff
changeset
|
592 |
handshakeContext.baseWriteSecret; |
50768 | 593 |
} |
594 |
||
595 |
handshakeContext = null; |
|
596 |
outputRecord.handshakeHash.finish(); |
|
597 |
inputRecord.finishHandshake(); |
|
598 |
outputRecord.finishHandshake(); |
|
599 |
isNegotiated = true; |
|
600 |
||
601 |
// Tell folk about handshake completion, but do it in a separate thread. |
|
602 |
if (transport instanceof SSLSocket && |
|
603 |
sslConfig.handshakeListeners != null && |
|
604 |
!sslConfig.handshakeListeners.isEmpty()) { |
|
605 |
HandshakeCompletedEvent hce = |
|
606 |
new HandshakeCompletedEvent((SSLSocket)transport, conSession); |
|
607 |
Thread thread = new Thread( |
|
608 |
null, |
|
609 |
new NotifyHandshake(sslConfig.handshakeListeners, hce), |
|
610 |
"HandshakeCompletedNotify-Thread", |
|
611 |
0, |
|
612 |
false); |
|
613 |
thread.start(); |
|
614 |
} |
|
615 |
||
616 |
return HandshakeStatus.FINISHED; |
|
617 |
} |
|
618 |
||
619 |
HandshakeStatus finishPostHandshake() { |
|
620 |
handshakeContext = null; |
|
621 |
||
622 |
// Note: May need trigger handshake completion even for post-handshake |
|
623 |
// authentication in the future. |
|
624 |
||
625 |
return HandshakeStatus.FINISHED; |
|
626 |
} |
|
627 |
||
628 |
// A separate thread is allocated to deliver handshake completion |
|
629 |
// events. |
|
630 |
private static class NotifyHandshake implements Runnable { |
|
631 |
private final Set<Map.Entry<HandshakeCompletedListener, |
|
632 |
AccessControlContext>> targets; // who gets notified |
|
633 |
private final HandshakeCompletedEvent event; // the notification |
|
634 |
||
635 |
NotifyHandshake( |
|
636 |
Map<HandshakeCompletedListener,AccessControlContext> listeners, |
|
637 |
HandshakeCompletedEvent event) { |
|
638 |
this.targets = new HashSet<>(listeners.entrySet()); // clone |
|
639 |
this.event = event; |
|
640 |
} |
|
641 |
||
642 |
@Override |
|
643 |
public void run() { |
|
644 |
// Don't need to synchronize, as it only runs in one thread. |
|
645 |
for (Map.Entry<HandshakeCompletedListener, |
|
646 |
AccessControlContext> entry : targets) { |
|
647 |
final HandshakeCompletedListener listener = entry.getKey(); |
|
648 |
AccessControlContext acc = entry.getValue(); |
|
649 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
|
650 |
@Override |
|
651 |
public Void run() { |
|
652 |
listener.handshakeCompleted(event); |
|
653 |
return null; |
|
654 |
} |
|
655 |
}, acc); |
|
656 |
} |
|
657 |
} |
|
658 |
} |
|
659 |
} |