author | ascarpino |
Fri, 25 May 2018 12:43:45 -0700 | |
branch | JDK-8145252-TLS13-branch |
changeset 56611 | f8f7e604e1f8 |
parent 56603 | f103e0c2be1e |
child 56646 | e57205a6e4ee |
permissions | -rw-r--r-- |
2 | 1 |
/* |
56542 | 2 |
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.ssl; |
|
27 |
||
56542 | 28 |
import java.io.IOException; |
29 |
import java.nio.ByteBuffer; |
|
30 |
import java.nio.ReadOnlyBufferException; |
|
31 |
import java.security.AccessController; |
|
32 |
import java.security.PrivilegedActionException; |
|
33 |
import java.security.PrivilegedExceptionAction; |
|
34 |
import java.util.List; |
|
35 |
import java.util.Map; |
|
42706
796cf076d69b
8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents:
41910
diff
changeset
|
36 |
import java.util.function.BiFunction; |
56542 | 37 |
import javax.net.ssl.SSLEngine; |
38 |
import javax.net.ssl.SSLEngineResult; |
|
39 |
import javax.net.ssl.SSLEngineResult.HandshakeStatus; |
|
40 |
import javax.net.ssl.SSLEngineResult.Status; |
|
41 |
import javax.net.ssl.SSLException; |
|
42 |
import javax.net.ssl.SSLHandshakeException; |
|
43 |
import javax.net.ssl.SSLKeyException; |
|
44 |
import javax.net.ssl.SSLParameters; |
|
45 |
import javax.net.ssl.SSLPeerUnverifiedException; |
|
46 |
import javax.net.ssl.SSLProtocolException; |
|
47 |
import javax.net.ssl.SSLSession; |
|
2 | 48 |
|
49 |
/** |
|
50 |
* Implementation of an non-blocking SSLEngine. |
|
51 |
* |
|
52 |
* @author Brad Wetmore |
|
53 |
*/ |
|
56542 | 54 |
final class SSLEngineImpl extends SSLEngine implements SSLTransport { |
55 |
private final SSLContextImpl sslContext; |
|
56 |
final TransportContext conContext; |
|
2 | 57 |
|
58 |
/** |
|
59 |
* Constructor for an SSLEngine from SSLContext, without |
|
56542 | 60 |
* host/port hints. |
61 |
* |
|
62 |
* This Engine will not be able to cache sessions, but must renegotiate |
|
63 |
* everything by hand. |
|
2 | 64 |
*/ |
56542 | 65 |
SSLEngineImpl(SSLContextImpl sslContext) { |
66 |
this(sslContext, null, -1); |
|
2 | 67 |
} |
68 |
||
69 |
/** |
|
70 |
* Constructor for an SSLEngine from SSLContext. |
|
71 |
*/ |
|
56542 | 72 |
SSLEngineImpl(SSLContextImpl sslContext, |
73 |
String host, int port) { |
|
2 | 74 |
super(host, port); |
56542 | 75 |
this.sslContext = sslContext; |
76 |
HandshakeHash handshakeHash = new HandshakeHash(); |
|
77 |
if (sslContext.isDTLS()) { |
|
78 |
this.conContext = new TransportContext(sslContext, this, |
|
79 |
new DTLSInputRecord(handshakeHash), |
|
80 |
new DTLSOutputRecord(handshakeHash)); |
|
81 |
} else { |
|
82 |
this.conContext = new TransportContext(sslContext, this, |
|
83 |
new SSLEngineInputRecord(handshakeHash), |
|
84 |
new SSLEngineOutputRecord(handshakeHash)); |
|
85 |
} |
|
86 |
||
87 |
// Server name indication is a connection scope extension. |
|
88 |
if (host != null) { |
|
89 |
this.conContext.sslConfig.serverNames = |
|
90 |
Utilities.addToSNIServerNameList( |
|
91 |
conContext.sslConfig.serverNames, host); |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
@Override |
|
96 |
public synchronized void beginHandshake() throws SSLException { |
|
97 |
if (conContext.isUnsureMode) { |
|
98 |
throw new IllegalStateException( |
|
99 |
"Client/Server mode has not yet been set."); |
|
100 |
} |
|
101 |
||
102 |
try { |
|
103 |
conContext.kickstart(); |
|
104 |
} catch (IOException ioe) { |
|
105 |
conContext.fatal(Alert.HANDSHAKE_FAILURE, |
|
106 |
"Couldn't kickstart handshaking", ioe); |
|
107 |
} catch (Exception ex) { // including RuntimeException |
|
108 |
conContext.fatal(Alert.INTERNAL_ERROR, |
|
109 |
"Fail to begin handshake", ex); |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
@Override |
|
114 |
public synchronized SSLEngineResult wrap(ByteBuffer[] appData, |
|
115 |
int offset, int length, ByteBuffer netData) throws SSLException { |
|
116 |
return wrap( |
|
117 |
appData, offset, length, new ByteBuffer[]{ netData }, 0, 1); |
|
2 | 118 |
} |
119 |
||
56542 | 120 |
// @Override |
121 |
public synchronized SSLEngineResult wrap( |
|
122 |
ByteBuffer[] srcs, int srcsOffset, int srcsLength, |
|
123 |
ByteBuffer[] dsts, int dstsOffset, int dstsLength) throws SSLException { |
|
124 |
||
125 |
if (conContext.isUnsureMode) { |
|
126 |
throw new IllegalStateException( |
|
127 |
"Client/Server mode has not yet been set."); |
|
128 |
} |
|
129 |
||
130 |
// See if the handshaker needs to report back some SSLException. |
|
131 |
if (conContext.outputRecord.isEmpty()) { |
|
132 |
checkTaskThrown(); |
|
133 |
} // Otherwise, deliver cached records before throwing task exception. |
|
134 |
||
135 |
// check parameters |
|
136 |
checkParams(srcs, srcsOffset, srcsLength, dsts, dstsOffset, dstsLength); |
|
137 |
||
138 |
try { |
|
139 |
return writeRecord( |
|
140 |
srcs, srcsOffset, srcsLength, dsts, dstsOffset, dstsLength); |
|
141 |
} catch (SSLProtocolException spe) { |
|
142 |
// may be an unexpected handshake message |
|
143 |
conContext.fatal(Alert.UNEXPECTED_MESSAGE, spe); |
|
144 |
} catch (IOException ioe) { |
|
145 |
conContext.fatal(Alert.INTERNAL_ERROR, |
|
146 |
"problem wrapping app data", ioe); |
|
147 |
} catch (Exception ex) { // including RuntimeException |
|
148 |
conContext.fatal(Alert.INTERNAL_ERROR, |
|
149 |
"Fail to wrap application data", ex); |
|
2 | 150 |
} |
151 |
||
56542 | 152 |
return null; // make compiler happy |
153 |
} |
|
154 |
||
155 |
private SSLEngineResult writeRecord( |
|
156 |
ByteBuffer[] srcs, int srcsOffset, int srcsLength, |
|
157 |
ByteBuffer[] dsts, int dstsOffset, int dstsLength) throws IOException { |
|
158 |
||
159 |
if (isOutboundDone()) { |
|
160 |
return new SSLEngineResult( |
|
161 |
Status.CLOSED, getHandshakeStatus(), 0, 0); |
|
162 |
} |
|
163 |
||
164 |
HandshakeContext hc = conContext.handshakeContext; |
|
165 |
HandshakeStatus hsStatus = null; |
|
56594 | 166 |
if (!conContext.isNegotiated && |
167 |
!conContext.isClosed() && !conContext.isBroken) { |
|
56542 | 168 |
conContext.kickstart(); |
169 |
||
170 |
hsStatus = getHandshakeStatus(); |
|
171 |
if (hsStatus == HandshakeStatus.NEED_UNWRAP) { |
|
172 |
/* |
|
173 |
* For DTLS, if the handshake state is |
|
174 |
* HandshakeStatus.NEED_UNWRAP, a call to SSLEngine.wrap() |
|
175 |
* means that the previous handshake packets (if delivered) |
|
176 |
* get lost, and need retransmit the handshake messages. |
|
177 |
*/ |
|
178 |
if (!sslContext.isDTLS() || hc == null || |
|
179 |
!hc.sslConfig.enableRetransmissions || |
|
180 |
conContext.outputRecord.firstMessage) { |
|
181 |
||
182 |
return new SSLEngineResult(Status.OK, hsStatus, 0, 0); |
|
183 |
} // otherwise, need retransmission |
|
184 |
} |
|
185 |
} |
|
186 |
||
187 |
if (hsStatus == null) { |
|
188 |
hsStatus = getHandshakeStatus(); |
|
189 |
} |
|
2 | 190 |
|
191 |
/* |
|
56542 | 192 |
* If we have a task outstanding, this *MUST* be done before |
193 |
* doing any more wrapping, because we could be in the middle |
|
194 |
* of receiving a handshake message, for example, a finished |
|
195 |
* message which would change the ciphers. |
|
2 | 196 |
*/ |
56542 | 197 |
if (hsStatus == HandshakeStatus.NEED_TASK) { |
198 |
return new SSLEngineResult(Status.OK, hsStatus, 0, 0); |
|
199 |
} |
|
200 |
||
201 |
int dstsRemains = 0; |
|
202 |
for (int i = dstsOffset; i < dstsOffset + dstsLength; i++) { |
|
203 |
dstsRemains += dsts[i].remaining(); |
|
204 |
} |
|
2 | 205 |
|
56542 | 206 |
// Check destination buffer size. |
207 |
// |
|
208 |
// We can be smarter about using smaller buffer sizes later. For |
|
209 |
// now, force it to be large enough to handle any valid record. |
|
210 |
if (dstsRemains < conContext.conSession.getPacketBufferSize()) { |
|
211 |
return new SSLEngineResult( |
|
212 |
Status.BUFFER_OVERFLOW, getHandshakeStatus(), 0, 0); |
|
213 |
} |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
214 |
|
56542 | 215 |
int srcsRemains = 0; |
216 |
for (int i = srcsOffset; i < srcsOffset + srcsLength; i++) { |
|
217 |
srcsRemains += srcs[i].remaining(); |
|
218 |
} |
|
6856 | 219 |
|
56542 | 220 |
Ciphertext ciphertext = null; |
221 |
try { |
|
222 |
// Acquire the buffered to-be-delivered records or retransmissions. |
|
223 |
// |
|
224 |
// May have buffered records, or need retransmission if handshaking. |
|
225 |
if (!conContext.outputRecord.isEmpty() || (hc != null && |
|
226 |
hc.sslConfig.enableRetransmissions && |
|
227 |
hc.sslContext.isDTLS() && |
|
228 |
hsStatus == HandshakeStatus.NEED_UNWRAP)) { |
|
229 |
ciphertext = encode(null, 0, 0, |
|
230 |
dsts, dstsOffset, dstsLength); |
|
231 |
} |
|
2 | 232 |
|
56542 | 233 |
if (ciphertext == null && srcsRemains != 0) { |
234 |
ciphertext = encode(srcs, srcsOffset, srcsLength, |
|
235 |
dsts, dstsOffset, dstsLength); |
|
236 |
} |
|
237 |
} catch (IOException ioe) { |
|
238 |
if (ioe instanceof SSLException) { |
|
239 |
throw ioe; |
|
240 |
} else { |
|
241 |
throw new SSLException("Write problems", ioe); |
|
242 |
} |
|
243 |
} |
|
2 | 244 |
|
245 |
/* |
|
56542 | 246 |
* Check for status. |
2 | 247 |
*/ |
56542 | 248 |
Status status = (isOutboundDone() ? Status.CLOSED : Status.OK); |
249 |
if (ciphertext != null && ciphertext.handshakeStatus != null) { |
|
250 |
hsStatus = ciphertext.handshakeStatus; |
|
251 |
} else { |
|
252 |
hsStatus = getHandshakeStatus(); |
|
253 |
} |
|
254 |
||
255 |
int deltaSrcs = srcsRemains; |
|
256 |
for (int i = srcsOffset; i < srcsOffset + srcsLength; i++) { |
|
257 |
deltaSrcs -= srcs[i].remaining(); |
|
258 |
} |
|
259 |
||
260 |
int deltaDsts = dstsRemains; |
|
261 |
for (int i = dstsOffset; i < dstsOffset + dstsLength; i++) { |
|
262 |
deltaDsts -= dsts[i].remaining(); |
|
263 |
} |
|
2 | 264 |
|
56542 | 265 |
return new SSLEngineResult(status, hsStatus, deltaSrcs, deltaDsts, |
266 |
ciphertext != null ? ciphertext.recordSN : -1L); |
|
267 |
} |
|
268 |
||
269 |
private Ciphertext encode( |
|
270 |
ByteBuffer[] srcs, int srcsOffset, int srcsLength, |
|
271 |
ByteBuffer[] dsts, int dstsOffset, int dstsLength) throws IOException { |
|
30904 | 272 |
|
56542 | 273 |
Ciphertext ciphertext = null; |
274 |
try { |
|
275 |
ciphertext = conContext.outputRecord.encode( |
|
276 |
srcs, srcsOffset, srcsLength, dsts, dstsOffset, dstsLength); |
|
277 |
} catch (SSLHandshakeException she) { |
|
278 |
// may be record sequence number overflow |
|
279 |
conContext.fatal(Alert.HANDSHAKE_FAILURE, she); |
|
280 |
} catch (IOException e) { |
|
281 |
conContext.fatal(Alert.UNEXPECTED_MESSAGE, e); |
|
282 |
} |
|
2 | 283 |
|
56542 | 284 |
if (ciphertext == null) { |
285 |
return Ciphertext.CIPHERTEXT_NULL; |
|
30904 | 286 |
} |
287 |
||
56542 | 288 |
// Is the handshake completed? |
289 |
boolean needRetransmission = |
|
290 |
conContext.sslContext.isDTLS() && |
|
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
291 |
conContext.getHandshakeContext(TransportContext.PRE) != null && |
56542 | 292 |
conContext.handshakeContext.sslConfig.enableRetransmissions; |
293 |
HandshakeStatus hsStatus = |
|
294 |
tryToFinishHandshake(ciphertext.contentType); |
|
295 |
if (needRetransmission && |
|
296 |
hsStatus == HandshakeStatus.FINISHED && |
|
297 |
conContext.sslContext.isDTLS() && |
|
298 |
ciphertext.handshakeType == SSLHandshake.FINISHED.id) { |
|
299 |
// Retransmit the last flight for DTLS. |
|
300 |
// |
|
301 |
// The application data transactions may begin immediately |
|
302 |
// after the last flight. If the last flight get lost, the |
|
303 |
// application data may be discarded accordingly. As could |
|
304 |
// be an issue for some applications. This impact can be |
|
305 |
// mitigated by sending the last fligth twice. |
|
306 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,verbose")) { |
|
307 |
SSLLogger.finest("retransmit the last flight messages"); |
|
308 |
} |
|
309 |
||
310 |
conContext.outputRecord.launchRetransmission(); |
|
311 |
hsStatus = HandshakeStatus.NEED_WRAP; |
|
312 |
} |
|
313 |
||
314 |
if (hsStatus == null) { |
|
315 |
hsStatus = conContext.getHandshakeStatus(); |
|
316 |
} |
|
317 |
||
318 |
// Is the sequence number is nearly overflow? |
|
319 |
if (conContext.outputRecord.seqNumIsHuge()) { |
|
320 |
hsStatus = tryKeyUpdate(hsStatus); |
|
321 |
} |
|
322 |
||
323 |
// update context status |
|
324 |
ciphertext.handshakeStatus = hsStatus; |
|
325 |
||
326 |
return ciphertext; |
|
327 |
} |
|
328 |
||
329 |
private HandshakeStatus tryToFinishHandshake(byte contentType) { |
|
330 |
HandshakeStatus hsStatus = null; |
|
331 |
if ((contentType == ContentType.HANDSHAKE.id) && |
|
332 |
conContext.outputRecord.isEmpty()) { |
|
333 |
if (conContext.handshakeContext == null) { |
|
334 |
hsStatus = HandshakeStatus.FINISHED; |
|
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
335 |
} else if (conContext.getHandshakeContext(TransportContext.POST) != null) { |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
336 |
return null; |
56542 | 337 |
} else if (conContext.handshakeContext.handshakeFinished) { |
338 |
hsStatus = conContext.finishHandshake(); |
|
339 |
} |
|
340 |
} // Otherwise, the followed call to getHSStatus() will help. |
|
341 |
||
342 |
return hsStatus; |
|
2 | 343 |
} |
344 |
||
345 |
/** |
|
56542 | 346 |
* Try renegotiation or key update for sequence number wrap. |
2 | 347 |
* |
56542 | 348 |
* Note that in order to maintain the handshake status properly, we check |
349 |
* the sequence number after the last record reading/writing process. As |
|
350 |
* we request renegotiation or close the connection for wrapped sequence |
|
351 |
* number when there is enough sequence number space left to handle a few |
|
352 |
* more records, so the sequence number of the last record cannot be |
|
353 |
* wrapped. |
|
2 | 354 |
*/ |
56542 | 355 |
private HandshakeStatus tryKeyUpdate( |
356 |
HandshakeStatus currentHandshakeStatus) throws IOException { |
|
357 |
// Don't bother to kickstart the renegotiation or key update when the |
|
358 |
// local is asking for it. |
|
359 |
if ((conContext.handshakeContext == null) && |
|
360 |
!conContext.isClosed() && !conContext.isBroken) { |
|
361 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
362 |
SSLLogger.finest("key update to wrap sequence number"); |
|
363 |
} |
|
364 |
conContext.keyUpdate(); |
|
365 |
return conContext.getHandshakeStatus(); |
|
2 | 366 |
} |
34380
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
367 |
|
56542 | 368 |
return currentHandshakeStatus; |
2 | 369 |
} |
370 |
||
56542 | 371 |
private static void checkParams( |
372 |
ByteBuffer[] srcs, int srcsOffset, int srcsLength, |
|
373 |
ByteBuffer[] dsts, int dstsOffset, int dstsLength) { |
|
2 | 374 |
|
56542 | 375 |
if ((srcs == null) || (dsts == null)) { |
376 |
throw new IllegalArgumentException( |
|
377 |
"source or destination buffer is null"); |
|
378 |
} |
|
2 | 379 |
|
56542 | 380 |
if ((srcsOffset < 0) || (srcsLength < 0) || |
381 |
(srcsOffset > srcs.length - srcsLength)) { |
|
382 |
throw new IndexOutOfBoundsException( |
|
383 |
"index out of bound of the source buffers"); |
|
2 | 384 |
} |
1763
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
385 |
|
56542 | 386 |
if ((dstsOffset < 0) || (dstsLength < 0) || |
387 |
(dstsOffset > dsts.length - dstsLength)) { |
|
388 |
throw new IndexOutOfBoundsException( |
|
389 |
"index out of bound of the destination buffers"); |
|
390 |
} |
|
2 | 391 |
|
56542 | 392 |
for (int i = srcsOffset; i < srcsOffset + srcsLength; i++) { |
393 |
if (srcs[i] == null) { |
|
394 |
throw new IllegalArgumentException( |
|
395 |
"source buffer[" + i + "] == null"); |
|
396 |
} |
|
397 |
} |
|
2 | 398 |
|
56542 | 399 |
for (int i = dstsOffset; i < dstsOffset + dstsLength; i++) { |
400 |
if (dsts[i] == null) { |
|
401 |
throw new IllegalArgumentException( |
|
402 |
"destination buffer[" + i + "] == null"); |
|
6856 | 403 |
} |
404 |
||
56542 | 405 |
/* |
406 |
* Make sure the destination bufffers are writable. |
|
407 |
*/ |
|
408 |
if (dsts[i].isReadOnly()) { |
|
409 |
throw new ReadOnlyBufferException(); |
|
2 | 410 |
} |
411 |
} |
|
412 |
} |
|
413 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
414 |
@Override |
56542 | 415 |
public synchronized SSLEngineResult unwrap(ByteBuffer src, |
416 |
ByteBuffer[] dsts, int offset, int length) throws SSLException { |
|
417 |
return unwrap( |
|
418 |
new ByteBuffer[]{src}, 0, 1, dsts, offset, length); |
|
2 | 419 |
} |
420 |
||
56542 | 421 |
// @Override |
422 |
public synchronized SSLEngineResult unwrap( |
|
423 |
ByteBuffer[] srcs, int srcsOffset, int srcsLength, |
|
424 |
ByteBuffer[] dsts, int dstsOffset, int dstsLength) throws SSLException { |
|
2 | 425 |
|
56542 | 426 |
if (conContext.isUnsureMode) { |
427 |
throw new IllegalStateException( |
|
428 |
"Client/Server mode has not yet been set."); |
|
429 |
} |
|
2 | 430 |
|
56542 | 431 |
// See if the handshaker needs to report back some SSLException. |
432 |
checkTaskThrown(); |
|
433 |
||
434 |
// check parameters |
|
435 |
checkParams(srcs, srcsOffset, srcsLength, dsts, dstsOffset, dstsLength); |
|
2 | 436 |
|
437 |
try { |
|
56542 | 438 |
return readRecord( |
439 |
srcs, srcsOffset, srcsLength, dsts, dstsOffset, dstsLength); |
|
30904 | 440 |
} catch (SSLProtocolException spe) { |
441 |
// may be an unexpected handshake message |
|
56542 | 442 |
conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
443 |
spe.getMessage(), spe); |
|
444 |
} catch (IOException ioe) { |
|
2 | 445 |
/* |
446 |
* Don't reset position so it looks like we didn't |
|
447 |
* consume anything. We did consume something, and it |
|
448 |
* got us into this situation, so report that much back. |
|
449 |
* Our days of consuming are now over anyway. |
|
450 |
*/ |
|
56542 | 451 |
conContext.fatal(Alert.INTERNAL_ERROR, |
452 |
"problem unwrapping net record", ioe); |
|
453 |
} catch (Exception ex) { // including RuntimeException |
|
454 |
conContext.fatal(Alert.INTERNAL_ERROR, |
|
455 |
"Fail to unwrap network record", ex); |
|
30904 | 456 |
} |
457 |
||
56542 | 458 |
return null; // make compiler happy |
2 | 459 |
} |
460 |
||
56542 | 461 |
private SSLEngineResult readRecord( |
462 |
ByteBuffer[] srcs, int srcsOffset, int srcsLength, |
|
463 |
ByteBuffer[] dsts, int dstsOffset, int dstsLength) throws IOException { |
|
2 | 464 |
|
465 |
/* |
|
466 |
* Check if we are closing/closed. |
|
467 |
*/ |
|
468 |
if (isInboundDone()) { |
|
56542 | 469 |
return new SSLEngineResult( |
470 |
Status.CLOSED, getHandshakeStatus(), 0, 0); |
|
2 | 471 |
} |
472 |
||
56542 | 473 |
HandshakeStatus hsStatus = null; |
56594 | 474 |
if (!conContext.isNegotiated && |
475 |
!conContext.isClosed() && !conContext.isBroken) { |
|
56542 | 476 |
conContext.kickstart(); |
2 | 477 |
|
56542 | 478 |
/* |
479 |
* If there's still outbound data to flush, we |
|
480 |
* can return without trying to unwrap anything. |
|
481 |
*/ |
|
482 |
hsStatus = getHandshakeStatus(); |
|
483 |
if (hsStatus == HandshakeStatus.NEED_WRAP) { |
|
484 |
return new SSLEngineResult(Status.OK, hsStatus, 0, 0); |
|
2 | 485 |
} |
486 |
} |
|
487 |
||
488 |
if (hsStatus == null) { |
|
56542 | 489 |
hsStatus = getHandshakeStatus(); |
2 | 490 |
} |
491 |
||
492 |
/* |
|
493 |
* If we have a task outstanding, this *MUST* be done before |
|
494 |
* doing any more unwrapping, because we could be in the middle |
|
495 |
* of receiving a handshake message, for example, a finished |
|
496 |
* message which would change the ciphers. |
|
497 |
*/ |
|
498 |
if (hsStatus == HandshakeStatus.NEED_TASK) { |
|
30904 | 499 |
return new SSLEngineResult(Status.OK, hsStatus, 0, 0); |
500 |
} |
|
501 |
||
502 |
if (hsStatus == SSLEngineResult.HandshakeStatus.NEED_UNWRAP_AGAIN) { |
|
503 |
Plaintext plainText = null; |
|
504 |
try { |
|
56542 | 505 |
plainText = decode(null, 0, 0, |
506 |
dsts, dstsOffset, dstsLength); |
|
507 |
} catch (IOException ioe) { |
|
508 |
if (ioe instanceof SSLException) { |
|
509 |
throw ioe; |
|
510 |
} else { |
|
511 |
throw new SSLException("readRecord", ioe); |
|
512 |
} |
|
30904 | 513 |
} |
514 |
||
56542 | 515 |
Status status = (isInboundDone() ? Status.CLOSED : Status.OK); |
516 |
if (plainText.handshakeStatus != null) { |
|
517 |
hsStatus = plainText.handshakeStatus; |
|
518 |
} else { |
|
519 |
hsStatus = getHandshakeStatus(); |
|
520 |
} |
|
30904 | 521 |
|
2 | 522 |
return new SSLEngineResult( |
30904 | 523 |
status, hsStatus, 0, 0, plainText.recordSN); |
2 | 524 |
} |
525 |
||
56542 | 526 |
int srcsRemains = 0; |
527 |
for (int i = srcsOffset; i < srcsOffset + srcsLength; i++) { |
|
528 |
srcsRemains += srcs[i].remaining(); |
|
529 |
} |
|
530 |
||
531 |
if (srcsRemains == 0) { |
|
532 |
return new SSLEngineResult(Status.OK, getHandshakeStatus(), 0, 0); |
|
533 |
} |
|
534 |
||
2 | 535 |
/* |
536 |
* Check the packet to make sure enough is here. |
|
537 |
* This will also indirectly check for 0 len packets. |
|
538 |
*/ |
|
30904 | 539 |
int packetLen = 0; |
540 |
try { |
|
56542 | 541 |
packetLen = conContext.inputRecord.bytesInCompletePacket( |
542 |
srcs, srcsOffset, srcsLength); |
|
30904 | 543 |
} catch (SSLException ssle) { |
544 |
// Need to discard invalid records for DTLS protocols. |
|
56542 | 545 |
if (sslContext.isDTLS()) { |
546 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,verbose")) { |
|
547 |
SSLLogger.finest("Discard invalid DTLS records", ssle); |
|
30904 | 548 |
} |
549 |
||
550 |
// invalid, discard the entire data [section 4.1.2.7, RFC 6347] |
|
56542 | 551 |
int deltaNet = 0; |
552 |
// int deltaNet = netData.remaining(); |
|
553 |
// netData.position(netData.limit()); |
|
30904 | 554 |
|
56542 | 555 |
Status status = (isInboundDone() ? Status.CLOSED : Status.OK); |
556 |
if (hsStatus == null) { |
|
557 |
hsStatus = getHandshakeStatus(); |
|
558 |
} |
|
30904 | 559 |
|
560 |
return new SSLEngineResult(status, hsStatus, deltaNet, 0, -1L); |
|
561 |
} else { |
|
562 |
throw ssle; |
|
563 |
} |
|
564 |
} |
|
2 | 565 |
|
566 |
// Is this packet bigger than SSL/TLS normally allows? |
|
56542 | 567 |
if (packetLen > conContext.conSession.getPacketBufferSize()) { |
568 |
int largestRecordSize = sslContext.isDTLS() ? |
|
30904 | 569 |
DTLSRecord.maxRecordSize : SSLRecord.maxLargeRecordSize; |
56542 | 570 |
if ((packetLen <= largestRecordSize) && !sslContext.isDTLS()) { |
2 | 571 |
// Expand the expected maximum packet/application buffer |
572 |
// sizes. |
|
30904 | 573 |
// |
574 |
// Only apply to SSL/TLS protocols. |
|
575 |
||
576 |
// Old behavior: shall we honor the System Property |
|
577 |
// "jsse.SSLEngine.acceptLargeFragments" if it is "false"? |
|
56542 | 578 |
conContext.conSession.expandBufferSizes(); |
2 | 579 |
} |
30904 | 580 |
|
581 |
// check the packet again |
|
56542 | 582 |
largestRecordSize = conContext.conSession.getPacketBufferSize(); |
30904 | 583 |
if (packetLen > largestRecordSize) { |
584 |
throw new SSLProtocolException( |
|
585 |
"Input record too big: max = " + |
|
586 |
largestRecordSize + " len = " + packetLen); |
|
587 |
} |
|
588 |
} |
|
589 |
||
2 | 590 |
/* |
591 |
* Check for OVERFLOW. |
|
592 |
* |
|
30904 | 593 |
* Delay enforcing the application buffer free space requirement |
594 |
* until after the initial handshaking. |
|
2 | 595 |
*/ |
56542 | 596 |
int dstsRemains = 0; |
597 |
for (int i = dstsOffset; i < dstsOffset + dstsLength; i++) { |
|
598 |
dstsRemains += dsts[i].remaining(); |
|
599 |
} |
|
30904 | 600 |
|
56542 | 601 |
if (conContext.isNegotiated) { |
602 |
int FragLen = |
|
603 |
conContext.inputRecord.estimateFragmentSize(packetLen); |
|
604 |
if (FragLen > dstsRemains) { |
|
30904 | 605 |
return new SSLEngineResult( |
606 |
Status.BUFFER_OVERFLOW, hsStatus, 0, 0); |
|
607 |
} |
|
2 | 608 |
} |
609 |
||
610 |
// check for UNDERFLOW. |
|
56542 | 611 |
if ((packetLen == -1) || (srcsRemains < packetLen)) { |
30904 | 612 |
return new SSLEngineResult(Status.BUFFER_UNDERFLOW, hsStatus, 0, 0); |
2 | 613 |
} |
614 |
||
615 |
/* |
|
616 |
* We're now ready to actually do the read. |
|
617 |
*/ |
|
30904 | 618 |
Plaintext plainText = null; |
2 | 619 |
try { |
56542 | 620 |
plainText = decode(srcs, srcsOffset, srcsLength, |
621 |
dsts, dstsOffset, dstsLength); |
|
622 |
} catch (IOException ioe) { |
|
623 |
if (ioe instanceof SSLException) { |
|
624 |
throw ioe; |
|
625 |
} else { |
|
626 |
throw new SSLException("readRecord", ioe); |
|
627 |
} |
|
2 | 628 |
} |
629 |
||
630 |
/* |
|
631 |
* Check the various condition that we could be reporting. |
|
632 |
* |
|
633 |
* It's *possible* something might have happened between the |
|
634 |
* above and now, but it was better to minimally lock "this" |
|
635 |
* during the read process. We'll return the current |
|
636 |
* status, which is more representative of the current state. |
|
637 |
* |
|
638 |
* status above should cover: FINISHED, NEED_TASK |
|
639 |
*/ |
|
56542 | 640 |
Status status = (isInboundDone() ? Status.CLOSED : Status.OK); |
641 |
if (plainText.handshakeStatus != null) { |
|
642 |
hsStatus = plainText.handshakeStatus; |
|
643 |
} else { |
|
644 |
hsStatus = getHandshakeStatus(); |
|
645 |
} |
|
30904 | 646 |
|
56542 | 647 |
int deltaNet = srcsRemains; |
648 |
for (int i = srcsOffset; i < srcsOffset + srcsLength; i++) { |
|
649 |
deltaNet -= srcs[i].remaining(); |
|
650 |
} |
|
651 |
||
652 |
int deltaApp = dstsRemains; |
|
653 |
for (int i = dstsOffset; i < dstsOffset + dstsLength; i++) { |
|
654 |
deltaApp -= dsts[i].remaining(); |
|
30904 | 655 |
} |
2 | 656 |
|
30904 | 657 |
return new SSLEngineResult( |
658 |
status, hsStatus, deltaNet, deltaApp, plainText.recordSN); |
|
659 |
} |
|
660 |
||
56542 | 661 |
private Plaintext decode( |
662 |
ByteBuffer[] srcs, int srcsOffset, int srcsLength, |
|
663 |
ByteBuffer[] dsts, int dstsOffset, int dstsLength) throws IOException { |
|
2 | 664 |
|
56542 | 665 |
Plaintext pt = SSLTransport.decode(conContext, |
666 |
srcs, srcsOffset, srcsLength, |
|
667 |
dsts, dstsOffset, dstsLength); |
|
2 | 668 |
|
56542 | 669 |
// Is the handshake completed? |
670 |
if (pt != Plaintext.PLAINTEXT_NULL) { |
|
671 |
HandshakeStatus hsStatus = tryToFinishHandshake(pt.contentType); |
|
672 |
if (hsStatus == null) { |
|
673 |
pt.handshakeStatus = conContext.getHandshakeStatus(); |
|
674 |
} else { |
|
675 |
pt.handshakeStatus = hsStatus; |
|
30904 | 676 |
} |
677 |
||
56542 | 678 |
// Is the sequence number is nearly overflow? |
679 |
if (conContext.inputRecord.seqNumIsHuge()) { |
|
680 |
pt.handshakeStatus = |
|
681 |
tryKeyUpdate(pt.handshakeStatus); |
|
2 | 682 |
} |
683 |
} |
|
684 |
||
56542 | 685 |
return pt; |
2 | 686 |
} |
687 |
||
56542 | 688 |
@Override |
689 |
public synchronized Runnable getDelegatedTask() { |
|
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
690 |
if (conContext.handshakeContext != null && // PRE or POST handshake |
56542 | 691 |
!conContext.handshakeContext.taskDelegated && |
692 |
!conContext.handshakeContext.delegatedActions.isEmpty()) { |
|
693 |
conContext.handshakeContext.taskDelegated = true; |
|
694 |
return new DelegatedTask(this); |
|
30904 | 695 |
} |
696 |
||
56542 | 697 |
return null; |
30904 | 698 |
} |
7039 | 699 |
|
56542 | 700 |
@Override |
701 |
public synchronized void closeInbound() throws SSLException { |
|
702 |
conContext.closeInbound(); |
|
703 |
} |
|
2 | 704 |
|
56542 | 705 |
@Override |
706 |
public synchronized boolean isInboundDone() { |
|
707 |
return conContext.isInboundDone(); |
|
2 | 708 |
} |
709 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
710 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30904
diff
changeset
|
711 |
public synchronized void closeOutbound() { |
56542 | 712 |
conContext.closeOutbound(); |
2 | 713 |
} |
714 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
715 |
@Override |
56542 | 716 |
public synchronized boolean isOutboundDone() { |
717 |
return conContext.isOutboundDone(); |
|
2 | 718 |
} |
719 |
||
56542 | 720 |
@Override |
721 |
public String[] getSupportedCipherSuites() { |
|
722 |
return CipherSuite.namesOf(sslContext.getSupportedCipherSuites()); |
|
723 |
} |
|
2 | 724 |
|
56542 | 725 |
@Override |
726 |
public synchronized String[] getEnabledCipherSuites() { |
|
727 |
return CipherSuite.namesOf(conContext.sslConfig.enabledCipherSuites); |
|
2 | 728 |
} |
729 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
730 |
@Override |
56542 | 731 |
public synchronized void setEnabledCipherSuites(String[] suites) { |
732 |
if (suites == null) { |
|
733 |
throw new IllegalArgumentException("CipherSuites cannot be null"); |
|
2 | 734 |
} |
735 |
||
56542 | 736 |
conContext.sslConfig.enabledCipherSuites = |
737 |
CipherSuite.validValuesOf(suites); |
|
738 |
} |
|
739 |
||
740 |
@Override |
|
741 |
public String[] getSupportedProtocols() { |
|
742 |
return ProtocolVersion.toStringArray( |
|
56611 | 743 |
sslContext.getSupportedProtocolVersions()); |
2 | 744 |
} |
745 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
746 |
@Override |
56542 | 747 |
public synchronized String[] getEnabledProtocols() { |
748 |
return ProtocolVersion.toStringArray( |
|
749 |
conContext.sslConfig.enabledProtocols); |
|
2 | 750 |
} |
751 |
||
56542 | 752 |
@Override |
753 |
public synchronized void setEnabledProtocols(String[] protocols) { |
|
754 |
if (protocols == null) { |
|
755 |
throw new IllegalArgumentException("Protocols cannot be null"); |
|
756 |
} |
|
2 | 757 |
|
56542 | 758 |
conContext.sslConfig.enabledProtocols = |
759 |
ProtocolVersion.namesOf(protocols); |
|
760 |
} |
|
761 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
762 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30904
diff
changeset
|
763 |
public synchronized SSLSession getSession() { |
56542 | 764 |
return conContext.conSession; |
2 | 765 |
} |
766 |
||
7043 | 767 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30904
diff
changeset
|
768 |
public synchronized SSLSession getHandshakeSession() { |
56542 | 769 |
return conContext.handshakeContext == null ? |
770 |
null : conContext.handshakeContext.handshakeSession; |
|
2 | 771 |
} |
772 |
||
56542 | 773 |
@Override |
774 |
public synchronized SSLEngineResult.HandshakeStatus getHandshakeStatus() { |
|
775 |
return conContext.getHandshakeStatus(); |
|
2 | 776 |
} |
777 |
||
56542 | 778 |
@Override |
779 |
public synchronized void setUseClientMode(boolean mode) { |
|
780 |
conContext.setUseClientMode(mode); |
|
2 | 781 |
} |
782 |
||
56542 | 783 |
@Override |
784 |
public synchronized boolean getUseClientMode() { |
|
785 |
return conContext.sslConfig.isClientMode; |
|
2 | 786 |
} |
787 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
788 |
@Override |
56542 | 789 |
public synchronized void setNeedClientAuth(boolean need) { |
790 |
conContext.sslConfig.clientAuthType = |
|
791 |
(need ? ClientAuthType.CLIENT_AUTH_REQUIRED : |
|
792 |
ClientAuthType.CLIENT_AUTH_NONE); |
|
2 | 793 |
} |
794 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
795 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30904
diff
changeset
|
796 |
public synchronized boolean getNeedClientAuth() { |
56542 | 797 |
return (conContext.sslConfig.clientAuthType == |
798 |
ClientAuthType.CLIENT_AUTH_REQUIRED); |
|
2 | 799 |
} |
800 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
801 |
@Override |
56542 | 802 |
public synchronized void setWantClientAuth(boolean want) { |
803 |
conContext.sslConfig.clientAuthType = |
|
804 |
(want ? ClientAuthType.CLIENT_AUTH_REQUESTED : |
|
805 |
ClientAuthType.CLIENT_AUTH_NONE); |
|
2 | 806 |
} |
807 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
808 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30904
diff
changeset
|
809 |
public synchronized boolean getWantClientAuth() { |
56542 | 810 |
return (conContext.sslConfig.clientAuthType == |
811 |
ClientAuthType.CLIENT_AUTH_REQUESTED); |
|
2 | 812 |
} |
813 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
814 |
@Override |
56542 | 815 |
public synchronized void setEnableSessionCreation(boolean flag) { |
816 |
conContext.sslConfig.enableSessionCreation = flag; |
|
2 | 817 |
} |
818 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
819 |
@Override |
56542 | 820 |
public synchronized boolean getEnableSessionCreation() { |
821 |
return conContext.sslConfig.enableSessionCreation; |
|
2 | 822 |
} |
823 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
824 |
@Override |
56542 | 825 |
public synchronized SSLParameters getSSLParameters() { |
826 |
return conContext.sslConfig.getSSLParameters(); |
|
2 | 827 |
} |
828 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
829 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30904
diff
changeset
|
830 |
public synchronized void setSSLParameters(SSLParameters params) { |
56542 | 831 |
conContext.sslConfig.setSSLParameters(params); |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
832 |
|
56542 | 833 |
if (conContext.sslConfig.maximumPacketSize != 0) { |
834 |
conContext.outputRecord.changePacketSize( |
|
835 |
conContext.sslConfig.maximumPacketSize); |
|
7043 | 836 |
} |
2 | 837 |
} |
838 |
||
34380
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
839 |
@Override |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
840 |
public synchronized String getApplicationProtocol() { |
56542 | 841 |
return conContext.applicationProtocol; |
34380
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
842 |
} |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
843 |
|
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
844 |
@Override |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
845 |
public synchronized String getHandshakeApplicationProtocol() { |
56542 | 846 |
return conContext.handshakeContext == null ? |
847 |
null : conContext.handshakeContext.applicationProtocol; |
|
34380
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
848 |
} |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
849 |
|
42706
796cf076d69b
8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents:
41910
diff
changeset
|
850 |
@Override |
796cf076d69b
8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents:
41910
diff
changeset
|
851 |
public synchronized void setHandshakeApplicationProtocolSelector( |
56542 | 852 |
BiFunction<SSLEngine, List<String>, String> selector) { |
853 |
conContext.sslConfig.engineAPSelector = selector; |
|
42706
796cf076d69b
8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents:
41910
diff
changeset
|
854 |
} |
796cf076d69b
8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents:
41910
diff
changeset
|
855 |
|
796cf076d69b
8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents:
41910
diff
changeset
|
856 |
@Override |
796cf076d69b
8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents:
41910
diff
changeset
|
857 |
public synchronized BiFunction<SSLEngine, List<String>, String> |
56542 | 858 |
getHandshakeApplicationProtocolSelector() { |
859 |
return conContext.sslConfig.engineAPSelector; |
|
860 |
} |
|
861 |
||
862 |
@Override |
|
863 |
public boolean useDelegatedTask() { |
|
864 |
return true; |
|
865 |
} |
|
866 |
||
867 |
private synchronized void checkTaskThrown() throws SSLException { |
|
868 |
HandshakeContext hc = conContext.handshakeContext; |
|
869 |
if (hc != null && hc.delegatedThrown != null) { |
|
870 |
try { |
|
871 |
throw getTaskThrown(hc.delegatedThrown); |
|
872 |
} finally { |
|
873 |
hc.delegatedThrown = null; |
|
874 |
} |
|
875 |
} |
|
876 |
||
877 |
if (conContext.isBroken && conContext.closeReason != null) { |
|
878 |
throw getTaskThrown(conContext.closeReason); |
|
879 |
} |
|
880 |
} |
|
881 |
||
882 |
private static SSLException getTaskThrown(Exception taskThrown) { |
|
883 |
String msg = taskThrown.getMessage(); |
|
884 |
||
885 |
if (msg == null) { |
|
886 |
msg = "Delegated task threw Exception or Error"; |
|
887 |
} |
|
888 |
||
889 |
if (taskThrown instanceof RuntimeException) { |
|
890 |
throw new RuntimeException(msg, taskThrown); |
|
891 |
} else if (taskThrown instanceof SSLHandshakeException) { |
|
892 |
return (SSLHandshakeException) |
|
893 |
new SSLHandshakeException(msg).initCause(taskThrown); |
|
894 |
} else if (taskThrown instanceof SSLKeyException) { |
|
895 |
return (SSLKeyException) |
|
896 |
new SSLKeyException(msg).initCause(taskThrown); |
|
897 |
} else if (taskThrown instanceof SSLPeerUnverifiedException) { |
|
898 |
return (SSLPeerUnverifiedException) |
|
899 |
new SSLPeerUnverifiedException(msg).initCause(taskThrown); |
|
900 |
} else if (taskThrown instanceof SSLProtocolException) { |
|
901 |
return (SSLProtocolException) |
|
902 |
new SSLProtocolException(msg).initCause(taskThrown); |
|
903 |
} else if (taskThrown instanceof SSLException) { |
|
904 |
return (SSLException)taskThrown; |
|
905 |
} else { |
|
906 |
return new SSLException(msg, taskThrown); |
|
907 |
} |
|
42706
796cf076d69b
8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents:
41910
diff
changeset
|
908 |
} |
796cf076d69b
8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents:
41910
diff
changeset
|
909 |
|
2 | 910 |
/** |
56542 | 911 |
* Implement a simple task delegator. |
2 | 912 |
*/ |
56542 | 913 |
private static class DelegatedTask implements Runnable { |
914 |
private final SSLEngineImpl engine; |
|
915 |
||
916 |
DelegatedTask(SSLEngineImpl engineInstance) { |
|
917 |
this.engine = engineInstance; |
|
918 |
} |
|
919 |
||
920 |
@Override |
|
921 |
public void run() { |
|
922 |
synchronized (engine) { |
|
923 |
HandshakeContext hc = engine.conContext.handshakeContext; |
|
924 |
if (hc == null || hc.delegatedActions.isEmpty()) { |
|
925 |
return; |
|
926 |
} |
|
2 | 927 |
|
56542 | 928 |
try { |
929 |
AccessController.doPrivileged( |
|
930 |
new DelegatedAction(hc), engine.conContext.acc); |
|
931 |
} catch (PrivilegedActionException pae) { |
|
932 |
// Get the handshake context again in case the |
|
933 |
// handshaking has completed. |
|
934 |
hc = engine.conContext.handshakeContext; |
|
935 |
if (hc != null) { |
|
936 |
hc.delegatedThrown = pae.getException(); |
|
937 |
} else if (engine.conContext.closeReason != null) { |
|
938 |
engine.conContext.closeReason = |
|
939 |
getTaskThrown(pae.getException()); |
|
940 |
} |
|
941 |
} catch (RuntimeException rte) { |
|
942 |
// Get the handshake context again in case the |
|
943 |
// handshaking has completed. |
|
944 |
hc = engine.conContext.handshakeContext; |
|
945 |
if (hc != null) { |
|
946 |
hc.delegatedThrown = rte; |
|
947 |
} else if (engine.conContext.closeReason != null) { |
|
948 |
engine.conContext.closeReason = rte; |
|
949 |
} |
|
950 |
} |
|
2 | 951 |
|
56542 | 952 |
// Get the handshake context again in case the |
953 |
// handshaking has completed. |
|
954 |
hc = engine.conContext.handshakeContext; |
|
955 |
if (hc != null) { |
|
956 |
hc.taskDelegated = false; |
|
957 |
} |
|
958 |
} |
|
959 |
} |
|
960 |
||
961 |
private static class DelegatedAction |
|
962 |
implements PrivilegedExceptionAction<Void> { |
|
963 |
final HandshakeContext context; |
|
964 |
DelegatedAction(HandshakeContext context) { |
|
965 |
this.context = context; |
|
966 |
} |
|
967 |
||
968 |
@Override |
|
969 |
public Void run() throws Exception { |
|
970 |
while (!context.delegatedActions.isEmpty()) { |
|
971 |
// Report back the task SSLException |
|
972 |
if (context.delegatedThrown != null) { |
|
973 |
Exception delegatedThrown = context.delegatedThrown; |
|
974 |
context.delegatedThrown = null; |
|
975 |
throw getTaskThrown(delegatedThrown); |
|
976 |
} |
|
977 |
||
978 |
Map.Entry<Byte, ByteBuffer> me = |
|
979 |
context.delegatedActions.poll(); |
|
980 |
if (me != null) { |
|
981 |
context.dispatch(me.getKey(), me.getValue()); |
|
982 |
} |
|
983 |
} |
|
984 |
return null; |
|
985 |
} |
|
986 |
} |
|
2 | 987 |
} |
988 |
} |