author | ascarpino |
Fri, 18 May 2018 19:07:49 -0700 | |
branch | JDK-8145252-TLS13-branch |
changeset 56573 | 4a53dd94403e |
parent 56561 | 5f23e0400f27 |
child 56594 | 99e0f3f3f0e4 |
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 |
||
217 |
// initialize the handshaker if necessary |
|
218 |
if (handshakeContext == null) { |
|
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
219 |
// TLS1.3 post-handshake |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
220 |
if (isNegotiated && protocolVersion.useTLS13PlusSpec()) { |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
221 |
handshakeContext = new PostHandshakeContext(this); |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
222 |
} else { |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
223 |
handshakeContext = sslConfig.isClientMode ? |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
224 |
new ClientHandshakeContext(sslContext, this) : |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
225 |
new ServerHandshakeContext(sslContext, this); |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
226 |
outputRecord.initHandshaker(); |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
227 |
} |
56542 | 228 |
} |
229 |
||
230 |
// kickstart the handshake if needed |
|
231 |
// |
|
232 |
// Need no kickstart message on server side unless the connection |
|
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
233 |
// has been established. |
56542 | 234 |
if(isNegotiated || sslConfig.isClientMode) { |
235 |
handshakeContext.kickstart(); |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
void keyUpdate() throws IOException { |
|
240 |
kickstart(); |
|
241 |
} |
|
242 |
||
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
243 |
final static byte PRE = 1; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
244 |
final static byte POST = 2; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
245 |
|
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
246 |
HandshakeContext getHandshakeContext(byte type) { |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
247 |
if (handshakeContext == null) { |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
248 |
return null; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
249 |
} |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
250 |
|
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
251 |
if (type == PRE && |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
252 |
(handshakeContext instanceof ClientHandshakeContext || |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
253 |
handshakeContext instanceof ServerHandshakeContext)) { |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
254 |
return handshakeContext; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
255 |
} |
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 |
if (type == POST && handshakeContext instanceof PostHandshakeContext) { |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
258 |
return handshakeContext; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
259 |
} |
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 |
return null; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
262 |
} |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
263 |
|
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
264 |
// Note: close_notify is delivered as a warning alert. |
56542 | 265 |
void warning(Alert alert) { |
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
266 |
// For initial handshaking, don't send a warning alert message to peer |
56542 | 267 |
// if handshaker has not started. |
268 |
if (isNegotiated || handshakeContext != null) { |
|
269 |
try { |
|
270 |
outputRecord.encodeAlert(Alert.Level.WARNING.level, alert.id); |
|
271 |
} catch (IOException ioe) { |
|
272 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
273 |
SSLLogger.warning( |
|
274 |
"Warning: failed to send warning alert " + alert, ioe); |
|
275 |
} |
|
276 |
} |
|
277 |
} |
|
278 |
} |
|
279 |
||
280 |
void fatal(Alert alert, |
|
281 |
String diagnostic) throws SSLException { |
|
282 |
fatal(alert, diagnostic, null); |
|
283 |
} |
|
284 |
||
285 |
void fatal(Alert alert, Throwable cause) throws SSLException { |
|
286 |
fatal(alert, null, cause); |
|
287 |
} |
|
288 |
||
289 |
void fatal(Alert alert, |
|
290 |
String diagnostic, Throwable cause) throws SSLException { |
|
291 |
fatal(alert, diagnostic, false, cause); |
|
292 |
} |
|
293 |
||
294 |
// Note: close_notify is not delivered via fatal() methods. |
|
295 |
void fatal(Alert alert, String diagnostic, |
|
296 |
boolean recvFatalAlert, Throwable cause) throws SSLException { |
|
297 |
// If we've already shutdown because of an error, there is nothing we |
|
298 |
// can do except rethrow the exception. |
|
299 |
// |
|
300 |
// Most exceptions seen here will be SSLExceptions. We may find the |
|
301 |
// occasional Exception which hasn't been converted to a SSLException, |
|
302 |
// so we'll do it here. |
|
303 |
if (closeReason != null) { |
|
304 |
if (cause == null) { |
|
305 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
306 |
SSLLogger.warning( |
|
307 |
"Closed transport, general or untracked problem"); |
|
308 |
} |
|
309 |
throw alert.createSSLException( |
|
310 |
"Closed transport, general or untracked problem"); |
|
311 |
} |
|
312 |
||
313 |
if (cause instanceof SSLException) { |
|
314 |
throw (SSLException)cause; |
|
315 |
} else { // unlikely, but just in case. |
|
316 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
317 |
SSLLogger.warning( |
|
318 |
"Closed transport, rethrowing (unexpected)", cause); |
|
319 |
} |
|
320 |
throw alert.createSSLException("Unexpected rethrowing", cause); |
|
321 |
} |
|
322 |
} |
|
323 |
||
324 |
// If we have no further information, make a general-purpose |
|
325 |
// message for folks to see. We generally have one or the other. |
|
326 |
if (diagnostic == null) { |
|
327 |
if (cause == null) { |
|
328 |
diagnostic = "General/Untracked problem"; |
|
329 |
} else { |
|
330 |
diagnostic = cause.getMessage(); |
|
331 |
} |
|
332 |
} |
|
333 |
||
334 |
if (cause == null) { |
|
335 |
cause = alert.createSSLException(diagnostic); |
|
336 |
} |
|
337 |
||
338 |
// shutdown the transport |
|
339 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
340 |
SSLLogger.severe("Fatal (" + alert + "): " + diagnostic, cause); |
|
341 |
} |
|
342 |
||
343 |
// remember the close reason |
|
344 |
if (cause instanceof SSLException) { |
|
345 |
closeReason = (SSLException)cause; |
|
346 |
} else { |
|
347 |
// Including RuntimeException, but we'll throw those down below. |
|
348 |
closeReason = alert.createSSLException(diagnostic, cause); |
|
349 |
} |
|
350 |
||
351 |
// close inbound |
|
352 |
try { |
|
353 |
inputRecord.close(); |
|
354 |
} catch (IOException ioe) { |
|
355 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
356 |
SSLLogger.warning("Fatal: input record closure failed", ioe); |
|
357 |
} |
|
358 |
} |
|
359 |
||
360 |
// invalidate the session |
|
361 |
if (conSession != null) { |
|
362 |
conSession.invalidate(); |
|
363 |
} |
|
364 |
||
365 |
if (handshakeContext != null && |
|
366 |
handshakeContext.handshakeSession != null) { |
|
367 |
handshakeContext.handshakeSession.invalidate(); |
|
368 |
} |
|
369 |
||
370 |
// send fatal alert |
|
371 |
// |
|
372 |
// If we haven't even started handshaking yet, or we are the recipient |
|
373 |
// of a fatal alert, no need to generate a fatal close alert. |
|
374 |
if (!recvFatalAlert && !isOutboundDone() && !isBroken && |
|
375 |
(isNegotiated || handshakeContext != null)) { |
|
376 |
try { |
|
377 |
outputRecord.encodeAlert(Alert.Level.FATAL.level, alert.id); |
|
378 |
} catch (IOException ioe) { |
|
379 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
380 |
SSLLogger.warning( |
|
381 |
"Fatal: failed to send fatal alert " + alert, ioe); |
|
382 |
} |
|
383 |
} |
|
384 |
} |
|
385 |
||
386 |
// close outbound |
|
387 |
try { |
|
388 |
outputRecord.close(); |
|
389 |
} catch (IOException ioe) { |
|
390 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
391 |
SSLLogger.warning("Fatal: ouput record closure failed", ioe); |
|
392 |
} |
|
393 |
} |
|
394 |
||
395 |
// terminal handshake context |
|
396 |
if (handshakeContext != null) { |
|
397 |
handshakeContext = null; |
|
398 |
} |
|
399 |
||
400 |
// terminal the transport |
|
401 |
try { |
|
402 |
transport.shutdown(); |
|
403 |
} catch (IOException ioe) { |
|
404 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
405 |
SSLLogger.warning("Fatal: transport closure failed", ioe); |
|
406 |
} |
|
407 |
} finally { |
|
408 |
isBroken = true; |
|
409 |
} |
|
410 |
||
411 |
if (closeReason instanceof SSLException) { |
|
412 |
throw (SSLException)closeReason; |
|
413 |
} else { |
|
414 |
throw (RuntimeException)closeReason; |
|
415 |
} |
|
416 |
} |
|
417 |
||
418 |
void setUseClientMode(boolean useClientMode) { |
|
419 |
/* |
|
420 |
* If we need to change the client mode and the enabled |
|
421 |
* protocols and cipher suites haven't specifically been |
|
422 |
* set by the user, change them to the corresponding |
|
423 |
* default ones. |
|
424 |
*/ |
|
425 |
if (sslConfig.isClientMode != useClientMode) { |
|
426 |
// Once handshaking has begun, the mode can not be reset for the |
|
427 |
// life of this engine. |
|
428 |
if (handshakeContext != null || isNegotiated) { |
|
429 |
throw new IllegalArgumentException( |
|
430 |
"Cannot change mode after SSL traffic has started"); |
|
431 |
} |
|
432 |
||
433 |
if (sslContext.isDefaultProtocolVesions( |
|
434 |
sslConfig.enabledProtocols)) { |
|
435 |
sslConfig.enabledProtocols = |
|
436 |
sslContext.getDefaultProtocolVersions(!useClientMode); |
|
437 |
} |
|
438 |
||
439 |
if (sslContext.isDefaultCipherSuiteList( |
|
440 |
sslConfig.enabledCipherSuites)) { |
|
441 |
sslConfig.enabledCipherSuites = |
|
442 |
sslContext.getDefaultCipherSuites(!useClientMode); |
|
443 |
} |
|
444 |
||
445 |
sslConfig.isClientMode = useClientMode; |
|
446 |
} |
|
447 |
||
448 |
isUnsureMode = false; |
|
449 |
} |
|
450 |
||
451 |
boolean isOutboundDone() { |
|
56573 | 452 |
return outputRecord.isClosed() && outputRecord.isEmpty(); |
56542 | 453 |
} |
454 |
||
455 |
boolean isInboundDone() { |
|
456 |
return inputRecord.isClosed(); |
|
457 |
} |
|
458 |
||
459 |
boolean isClosed() { |
|
460 |
return isOutboundDone() && isInboundDone(); |
|
461 |
} |
|
462 |
||
463 |
@Override |
|
464 |
public void close() throws IOException { |
|
465 |
if (!isOutboundDone()) { |
|
466 |
closeOutbound(); |
|
467 |
} |
|
468 |
||
469 |
if (!isInboundDone()) { |
|
470 |
closeInbound(); |
|
471 |
} |
|
472 |
} |
|
473 |
||
474 |
void closeInbound() throws SSLException { |
|
475 |
if (isInboundDone()) { |
|
476 |
return; |
|
477 |
} |
|
478 |
||
479 |
try { |
|
480 |
if (isInputCloseNotified) { // passive close |
|
481 |
passiveInboundClose(); |
|
482 |
} else { // initiative close |
|
483 |
initiateInboundClose(); |
|
484 |
} |
|
485 |
} catch (IOException ioe) { |
|
486 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
487 |
SSLLogger.warning("inbound closure failed", ioe); |
|
488 |
} |
|
489 |
} |
|
490 |
} |
|
491 |
||
492 |
void closeOutbound() { |
|
493 |
if (isOutboundDone()) { |
|
494 |
return; |
|
495 |
} |
|
496 |
||
497 |
try { |
|
498 |
initiateOutboundClose(); |
|
499 |
} catch (IOException ioe) { |
|
500 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
501 |
SSLLogger.warning("outbound closure failed", ioe); |
|
502 |
} |
|
503 |
} |
|
504 |
} |
|
505 |
||
506 |
// Close the connection passively. The closure could be kickoff by |
|
507 |
// receiving a close_notify alert or reaching end_of_file of the socket. |
|
508 |
private void passiveInboundClose() throws IOException { |
|
509 |
if (!isInboundDone()) { |
|
510 |
inputRecord.close(); |
|
511 |
} |
|
512 |
||
513 |
// For TLS 1.3, output closure is independent from input closure. |
|
514 |
// if (isNegotiated && protocolVersion.useTLS13PlusSpec()) { |
|
515 |
// return; |
|
516 |
// } |
|
517 |
||
518 |
// For TLS 1.2 and prior version, it is required to respond with |
|
519 |
// a close_notify alert of its own and close down the connection |
|
520 |
// immediately, discarding any pending writes. |
|
521 |
if (!isOutboundDone() && !isOutputCloseNotified) { |
|
522 |
try { |
|
523 |
// send a close_notify alert |
|
524 |
warning(Alert.CLOSE_NOTIFY); |
|
525 |
} finally { |
|
526 |
// any data received after a closure alert is ignored. |
|
527 |
isOutputCloseNotified = true; |
|
528 |
outputRecord.close(); |
|
529 |
} |
|
530 |
} |
|
531 |
||
532 |
transport.shutdown(); |
|
533 |
} |
|
534 |
||
535 |
// Initiate a close by sending a close_notify alert. |
|
536 |
private void initiateInboundClose() throws IOException { |
|
537 |
// TLS 1.3 does not define how to initiate and close a TLS connection |
|
538 |
// gracefully. We will always send a close_notify alert, and close |
|
539 |
// the underlying transportation layer if needed. |
|
56573 | 540 |
if (!isInboundDone() && !isInputCloseNotified) { |
56542 | 541 |
try { |
542 |
// send a close_notify alert |
|
543 |
warning(Alert.CLOSE_NOTIFY); |
|
544 |
} finally { |
|
545 |
// any data received after a closure alert is ignored. |
|
56573 | 546 |
isInputCloseNotified = true; |
547 |
inputRecord.close(); |
|
56542 | 548 |
} |
549 |
} |
|
550 |
||
551 |
// For TLS 1.3, input closure is independent from output closure. Both |
|
552 |
// parties need not wait to receive a "close_notify" alert before |
|
553 |
// closing their read side of the connection. |
|
554 |
// |
|
555 |
// For TLS 1.2 and prior version, it is not required for the initiator |
|
556 |
// of the close to wait for the responding close_notify alert before |
|
557 |
// closing the read side of the connection. |
|
558 |
try { |
|
559 |
transport.shutdown(); |
|
560 |
} finally { |
|
56573 | 561 |
if (!isOutboundDone()) { |
562 |
outputRecord.close(); |
|
56542 | 563 |
} |
564 |
} |
|
565 |
} |
|
566 |
||
567 |
// Initiate a close by sending a close_notify alert. |
|
568 |
private void initiateOutboundClose() throws IOException { |
|
569 |
if (!isOutboundDone() && !isOutputCloseNotified) { |
|
570 |
try { // close outputRecord |
|
571 |
// send a close_notify alert |
|
572 |
warning(Alert.CLOSE_NOTIFY); |
|
573 |
} finally { |
|
574 |
// any data received after a closure alert is ignored. |
|
575 |
isOutputCloseNotified = true; |
|
576 |
outputRecord.close(); |
|
577 |
} |
|
578 |
} |
|
579 |
||
580 |
// For TLS 1.3, output closure is independent from input closure. |
|
581 |
// |
|
582 |
// if (isNegotiated && protocolVersion.useTLS13PlusSpec()) { |
|
583 |
// return; |
|
584 |
// } |
|
585 |
// |
|
586 |
||
587 |
// It is not required for the initiator of the close to wait for the |
|
588 |
// responding close_notify alert before closing the read side of the |
|
589 |
// connection. However, if the application protocol using TLS |
|
590 |
// provides that any data may be carried over the underlying transport |
|
591 |
// after the TLS connection is closed, the TLS implementation MUST |
|
592 |
// receive a "close_notify" alert before indicating end-of-data to the |
|
593 |
// application-layer. |
|
594 |
try { |
|
595 |
transport.shutdown(); |
|
596 |
} finally { |
|
597 |
if (!isInboundDone()) { |
|
598 |
inputRecord.close(); |
|
599 |
} |
|
600 |
} |
|
601 |
} |
|
602 |
||
603 |
// Note; HandshakeStatus.FINISHED status is retrieved in other places. |
|
604 |
HandshakeStatus getHandshakeStatus() { |
|
605 |
if (!outputRecord.isEmpty()) { |
|
606 |
// If no handshaking, special case to wrap alters. |
|
607 |
return HandshakeStatus.NEED_WRAP; |
|
608 |
} else if (handshakeContext != null) { |
|
609 |
if (!handshakeContext.delegatedActions.isEmpty()) { |
|
610 |
return HandshakeStatus.NEED_TASK; |
|
611 |
} else if (sslContext.isDTLS() && |
|
612 |
!inputRecord.isEmpty()) { |
|
613 |
return HandshakeStatus.NEED_UNWRAP_AGAIN; |
|
614 |
} else { |
|
615 |
return HandshakeStatus.NEED_UNWRAP; |
|
616 |
} |
|
617 |
} else if (isOutboundDone() && !isInboundDone()) { |
|
618 |
/* |
|
619 |
* Special case where we're closing, but |
|
620 |
* still need the close_notify before we |
|
621 |
* can officially be closed. |
|
622 |
* |
|
623 |
* Note isOutboundDone is taken care of by |
|
624 |
* hasOutboundData() above. |
|
625 |
*/ |
|
626 |
return HandshakeStatus.NEED_UNWRAP; |
|
627 |
} |
|
628 |
||
629 |
return HandshakeStatus.NOT_HANDSHAKING; |
|
630 |
} |
|
631 |
||
632 |
HandshakeStatus finishHandshake() { |
|
633 |
if (protocolVersion.useTLS13PlusSpec()) { |
|
634 |
outputRecord.tc = this; |
|
635 |
cipherSuite = handshakeContext.negotiatedCipherSuite; |
|
636 |
inputRecord.readCipher.baseSecret = handshakeContext.baseReadSecret; |
|
637 |
outputRecord.writeCipher.baseSecret = handshakeContext.baseWriteSecret; |
|
638 |
} |
|
639 |
handshakeContext = null; |
|
640 |
// inputRecord and outputRecord shares the same handshakeHash |
|
641 |
// inputRecord.handshakeHash.finish(); |
|
642 |
outputRecord.handshakeHash.finish(); |
|
643 |
inputRecord.finishHandshake(); |
|
644 |
outputRecord.finishHandshake(); |
|
645 |
isNegotiated = true; |
|
646 |
||
647 |
// Tell folk about handshake completion, but do it in a separate thread. |
|
648 |
if (transport instanceof SSLSocket && |
|
649 |
sslConfig.handshakeListeners != null && |
|
650 |
!sslConfig.handshakeListeners.isEmpty()) { |
|
651 |
HandshakeCompletedEvent hce = |
|
652 |
new HandshakeCompletedEvent((SSLSocket)transport, conSession); |
|
653 |
Thread thread = new Thread( |
|
654 |
null, |
|
655 |
new NotifyHandshake(sslConfig.handshakeListeners, hce), |
|
656 |
"HandshakeCompletedNotify-Thread", |
|
657 |
0, |
|
658 |
false); |
|
659 |
thread.start(); |
|
660 |
} |
|
661 |
return HandshakeStatus.FINISHED; |
|
662 |
} |
|
663 |
||
664 |
// A separate thread is allocated to deliver handshake completion |
|
665 |
// events. |
|
666 |
private static class NotifyHandshake implements Runnable { |
|
667 |
private Set<Map.Entry<HandshakeCompletedListener, |
|
668 |
AccessControlContext>> targets; // who gets notified |
|
669 |
private HandshakeCompletedEvent event; // the notification |
|
670 |
||
671 |
NotifyHandshake( |
|
672 |
Map<HandshakeCompletedListener,AccessControlContext> listeners, |
|
673 |
HandshakeCompletedEvent event) { |
|
674 |
this.targets = new HashSet<>(listeners.entrySet()); // clone |
|
675 |
this.event = event; |
|
676 |
} |
|
677 |
||
678 |
@Override |
|
679 |
public void run() { |
|
680 |
// Don't need to synchronize, as it only runs in one thread. |
|
681 |
for (Map.Entry<HandshakeCompletedListener, |
|
682 |
AccessControlContext> entry : targets) { |
|
683 |
final HandshakeCompletedListener listener = entry.getKey(); |
|
684 |
AccessControlContext acc = entry.getValue(); |
|
685 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
|
686 |
@Override |
|
687 |
public Void run() { |
|
688 |
listener.handshakeCompleted(event); |
|
689 |
return null; |
|
690 |
} |
|
691 |
}, acc); |
|
692 |
} |
|
693 |
} |
|
694 |
} |
|
695 |
} |