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