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