author | mchung |
Tue, 06 Nov 2018 10:01:16 -0800 | |
changeset 52427 | 3c6aa484536c |
parent 51986 | c1db377f6300 |
child 53018 | 8bf9268df0e2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
50768 | 2 |
* Copyright (c) 1996, 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 |
||
50768 | 28 |
import java.io.EOFException; |
29 |
import java.io.IOException; |
|
30 |
import java.io.InputStream; |
|
31 |
import java.io.InterruptedIOException; |
|
32 |
import java.io.OutputStream; |
|
33 |
import java.net.InetAddress; |
|
34 |
import java.net.InetSocketAddress; |
|
35 |
import java.net.Socket; |
|
36 |
import java.net.SocketAddress; |
|
37 |
import java.net.SocketException; |
|
38 |
import java.net.UnknownHostException; |
|
39 |
import java.nio.ByteBuffer; |
|
40 |
import java.util.List; |
|
42706
796cf076d69b
8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents:
38865
diff
changeset
|
41 |
import java.util.function.BiFunction; |
50768 | 42 |
import javax.net.ssl.HandshakeCompletedListener; |
43 |
import javax.net.ssl.SSLException; |
|
44 |
import javax.net.ssl.SSLHandshakeException; |
|
45 |
import javax.net.ssl.SSLParameters; |
|
46 |
import javax.net.ssl.SSLProtocolException; |
|
47 |
import javax.net.ssl.SSLServerSocket; |
|
48 |
import javax.net.ssl.SSLSession; |
|
49 |
import javax.net.ssl.SSLSocket; |
|
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
51986
diff
changeset
|
50 |
import jdk.internal.access.JavaNetInetAddressAccess; |
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
51986
diff
changeset
|
51 |
import jdk.internal.access.SharedSecrets; |
31687 | 52 |
|
2 | 53 |
/** |
50768 | 54 |
* Implementation of an SSL socket. |
55 |
* <P> |
|
56 |
* This is a normal connection type socket, implementing SSL over some lower |
|
57 |
* level socket, such as TCP. Because it is layered over some lower level |
|
58 |
* socket, it MUST override all default socket methods. |
|
59 |
* <P> |
|
60 |
* This API offers a non-traditional option for establishing SSL |
|
2 | 61 |
* connections. You may first establish the connection directly, then pass |
62 |
* that connection to the SSL socket constructor with a flag saying which |
|
63 |
* role should be taken in the handshake protocol. (The two ends of the |
|
64 |
* connection must not choose the same role!) This allows setup of SSL |
|
65 |
* proxying or tunneling, and also allows the kind of "role reversal" |
|
66 |
* that is required for most FTP data transfers. |
|
67 |
* |
|
68 |
* @see javax.net.ssl.SSLSocket |
|
69 |
* @see SSLServerSocket |
|
70 |
* |
|
71 |
* @author David Brownell |
|
72 |
*/ |
|
50768 | 73 |
public final class SSLSocketImpl |
74 |
extends BaseSSLSocketImpl implements SSLTransport { |
|
2 | 75 |
|
50768 | 76 |
final SSLContextImpl sslContext; |
77 |
final TransportContext conContext; |
|
34380
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
78 |
|
50768 | 79 |
private final AppInputStream appInput = new AppInputStream(); |
80 |
private final AppOutputStream appOutput = new AppOutputStream(); |
|
2 | 81 |
|
50768 | 82 |
private String peerHost; |
83 |
private boolean autoClose; |
|
84 |
private boolean isConnected = false; |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
85 |
private volatile boolean tlsIsClosed = false; |
30904 | 86 |
|
31687 | 87 |
/* |
88 |
* Is the local name service trustworthy? |
|
89 |
* |
|
90 |
* If the local name service is not trustworthy, reverse host name |
|
91 |
* resolution should not be performed for endpoint identification. |
|
92 |
*/ |
|
50768 | 93 |
private static final boolean trustNameService = |
94 |
Utilities.getBooleanProperty("jdk.tls.trustNameService", false); |
|
31687 | 95 |
|
50768 | 96 |
/** |
97 |
* Package-private constructor used to instantiate an unconnected |
|
98 |
* socket. |
|
99 |
* |
|
100 |
* This instance is meant to set handshake state to use "client mode". |
|
101 |
*/ |
|
102 |
SSLSocketImpl(SSLContextImpl sslContext) { |
|
103 |
super(); |
|
104 |
this.sslContext = sslContext; |
|
105 |
HandshakeHash handshakeHash = new HandshakeHash(); |
|
106 |
this.conContext = new TransportContext(sslContext, this, |
|
107 |
new SSLSocketInputRecord(handshakeHash), |
|
108 |
new SSLSocketOutputRecord(handshakeHash), true); |
|
109 |
} |
|
2 | 110 |
|
111 |
/** |
|
50768 | 112 |
* Package-private constructor used to instantiate a server socket. |
2 | 113 |
* |
50768 | 114 |
* This instance is meant to set handshake state to use "server mode". |
2 | 115 |
*/ |
50768 | 116 |
SSLSocketImpl(SSLContextImpl sslContext, SSLConfiguration sslConfig) { |
2 | 117 |
super(); |
50768 | 118 |
this.sslContext = sslContext; |
119 |
HandshakeHash handshakeHash = new HandshakeHash(); |
|
120 |
this.conContext = new TransportContext(sslContext, this, sslConfig, |
|
121 |
new SSLSocketInputRecord(handshakeHash), |
|
122 |
new SSLSocketOutputRecord(handshakeHash)); |
|
2 | 123 |
} |
124 |
||
125 |
/** |
|
50768 | 126 |
* Constructs an SSL connection to a named host at a specified |
127 |
* port, using the authentication context provided. |
|
2 | 128 |
* |
50768 | 129 |
* This endpoint acts as the client, and may rejoin an existing SSL session |
130 |
* if appropriate. |
|
2 | 131 |
*/ |
50768 | 132 |
SSLSocketImpl(SSLContextImpl sslContext, String peerHost, |
133 |
int peerPort) throws IOException, UnknownHostException { |
|
2 | 134 |
super(); |
50768 | 135 |
this.sslContext = sslContext; |
136 |
HandshakeHash handshakeHash = new HandshakeHash(); |
|
137 |
this.conContext = new TransportContext(sslContext, this, |
|
138 |
new SSLSocketInputRecord(handshakeHash), |
|
139 |
new SSLSocketOutputRecord(handshakeHash), true); |
|
140 |
this.peerHost = peerHost; |
|
141 |
SocketAddress socketAddress = |
|
142 |
peerHost != null ? new InetSocketAddress(peerHost, peerPort) : |
|
143 |
new InetSocketAddress(InetAddress.getByName(null), peerPort); |
|
2 | 144 |
connect(socketAddress, 0); |
145 |
} |
|
146 |
||
147 |
/** |
|
50768 | 148 |
* Constructs an SSL connection to a server at a specified |
149 |
* address, and TCP port, using the authentication context |
|
150 |
* provided. |
|
2 | 151 |
* |
50768 | 152 |
* This endpoint acts as the client, and may rejoin an existing SSL |
153 |
* session if appropriate. |
|
2 | 154 |
*/ |
50768 | 155 |
SSLSocketImpl(SSLContextImpl sslContext, |
156 |
InetAddress address, int peerPort) throws IOException { |
|
2 | 157 |
super(); |
50768 | 158 |
this.sslContext = sslContext; |
159 |
HandshakeHash handshakeHash = new HandshakeHash(); |
|
160 |
this.conContext = new TransportContext(sslContext, this, |
|
161 |
new SSLSocketInputRecord(handshakeHash), |
|
162 |
new SSLSocketOutputRecord(handshakeHash), true); |
|
2 | 163 |
|
50768 | 164 |
SocketAddress socketAddress = new InetSocketAddress(address, peerPort); |
2 | 165 |
connect(socketAddress, 0); |
166 |
} |
|
167 |
||
50768 | 168 |
/** |
169 |
* Constructs an SSL connection to a named host at a specified |
|
170 |
* port, using the authentication context provided. |
|
171 |
* |
|
172 |
* This endpoint acts as the client, and may rejoin an existing SSL |
|
173 |
* session if appropriate. |
|
2 | 174 |
*/ |
50768 | 175 |
SSLSocketImpl(SSLContextImpl sslContext, |
176 |
String peerHost, int peerPort, InetAddress localAddr, |
|
177 |
int localPort) throws IOException, UnknownHostException { |
|
2 | 178 |
super(); |
50768 | 179 |
this.sslContext = sslContext; |
180 |
HandshakeHash handshakeHash = new HandshakeHash(); |
|
181 |
this.conContext = new TransportContext(sslContext, this, |
|
182 |
new SSLSocketInputRecord(handshakeHash), |
|
183 |
new SSLSocketOutputRecord(handshakeHash), true); |
|
184 |
this.peerHost = peerHost; |
|
2 | 185 |
|
50768 | 186 |
bind(new InetSocketAddress(localAddr, localPort)); |
187 |
SocketAddress socketAddress = |
|
188 |
peerHost != null ? new InetSocketAddress(peerHost, peerPort) : |
|
189 |
new InetSocketAddress(InetAddress.getByName(null), peerPort); |
|
190 |
connect(socketAddress, 0); |
|
2 | 191 |
} |
192 |
||
193 |
/** |
|
50768 | 194 |
* Constructs an SSL connection to a server at a specified |
195 |
* address, and TCP port, using the authentication context |
|
196 |
* provided. |
|
2 | 197 |
* |
50768 | 198 |
* This endpoint acts as the client, and may rejoin an existing SSL |
199 |
* session if appropriate. |
|
2 | 200 |
*/ |
50768 | 201 |
SSLSocketImpl(SSLContextImpl sslContext, |
202 |
InetAddress peerAddr, int peerPort, |
|
203 |
InetAddress localAddr, int localPort) throws IOException { |
|
204 |
super(); |
|
205 |
this.sslContext = sslContext; |
|
206 |
HandshakeHash handshakeHash = new HandshakeHash(); |
|
207 |
this.conContext = new TransportContext(sslContext, this, |
|
208 |
new SSLSocketInputRecord(handshakeHash), |
|
209 |
new SSLSocketOutputRecord(handshakeHash), true); |
|
210 |
||
211 |
bind(new InetSocketAddress(localAddr, localPort)); |
|
212 |
SocketAddress socketAddress = new InetSocketAddress(peerAddr, peerPort); |
|
213 |
connect(socketAddress, 0); |
|
2 | 214 |
} |
215 |
||
216 |
/** |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
217 |
* Creates a server mode {@link Socket} layered over an |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
218 |
* existing connected socket, and is able to read data which has |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
219 |
* already been consumed/removed from the {@link Socket}'s |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
220 |
* underlying {@link InputStream}. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
221 |
*/ |
50768 | 222 |
SSLSocketImpl(SSLContextImpl sslContext, Socket sock, |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
223 |
InputStream consumed, boolean autoClose) throws IOException { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
224 |
super(sock, consumed); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
225 |
// We always layer over a connected socket |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
226 |
if (!sock.isConnected()) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
227 |
throw new SocketException("Underlying socket is not connected"); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
228 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
229 |
|
50768 | 230 |
this.sslContext = sslContext; |
231 |
HandshakeHash handshakeHash = new HandshakeHash(); |
|
232 |
this.conContext = new TransportContext(sslContext, this, |
|
233 |
new SSLSocketInputRecord(handshakeHash), |
|
234 |
new SSLSocketOutputRecord(handshakeHash), false); |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
235 |
this.autoClose = autoClose; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
236 |
doneConnect(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
237 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
238 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
239 |
/** |
50768 | 240 |
* Layer SSL traffic over an existing connection, rather than |
241 |
* creating a new connection. |
|
242 |
* |
|
243 |
* The existing connection may be used only for SSL traffic (using this |
|
244 |
* SSLSocket) until the SSLSocket.close() call returns. However, if a |
|
245 |
* protocol error is detected, that existing connection is automatically |
|
246 |
* closed. |
|
247 |
* <p> |
|
248 |
* This particular constructor always uses the socket in the |
|
249 |
* role of an SSL client. It may be useful in cases which start |
|
250 |
* using SSL after some initial data transfers, for example in some |
|
251 |
* SSL tunneling applications or as part of some kinds of application |
|
252 |
* protocols which negotiate use of a SSL based security. |
|
2 | 253 |
*/ |
50768 | 254 |
SSLSocketImpl(SSLContextImpl sslContext, Socket sock, |
255 |
String peerHost, int port, boolean autoClose) throws IOException { |
|
256 |
super(sock); |
|
257 |
// We always layer over a connected socket |
|
258 |
if (!sock.isConnected()) { |
|
259 |
throw new SocketException("Underlying socket is not connected"); |
|
260 |
} |
|
2 | 261 |
|
50768 | 262 |
this.sslContext = sslContext; |
263 |
HandshakeHash handshakeHash = new HandshakeHash(); |
|
264 |
this.conContext = new TransportContext(sslContext, this, |
|
265 |
new SSLSocketInputRecord(handshakeHash), |
|
266 |
new SSLSocketOutputRecord(handshakeHash), true); |
|
267 |
this.peerHost = peerHost; |
|
268 |
this.autoClose = autoClose; |
|
269 |
doneConnect(); |
|
2 | 270 |
} |
271 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
272 |
@Override |
50768 | 273 |
public void connect(SocketAddress endpoint, |
274 |
int timeout) throws IOException { |
|
2 | 275 |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
276 |
if (isLayered()) { |
2 | 277 |
throw new SocketException("Already connected"); |
278 |
} |
|
279 |
||
280 |
if (!(endpoint instanceof InetSocketAddress)) { |
|
281 |
throw new SocketException( |
|
50768 | 282 |
"Cannot handle non-Inet socket addresses."); |
2 | 283 |
} |
284 |
||
285 |
super.connect(endpoint, timeout); |
|
50768 | 286 |
doneConnect(); |
287 |
} |
|
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
288 |
|
50768 | 289 |
@Override |
290 |
public String[] getSupportedCipherSuites() { |
|
291 |
return CipherSuite.namesOf(sslContext.getSupportedCipherSuites()); |
|
292 |
} |
|
293 |
||
294 |
@Override |
|
295 |
public synchronized String[] getEnabledCipherSuites() { |
|
296 |
return CipherSuite.namesOf(conContext.sslConfig.enabledCipherSuites); |
|
297 |
} |
|
298 |
||
299 |
@Override |
|
300 |
public synchronized void setEnabledCipherSuites(String[] suites) { |
|
301 |
conContext.sslConfig.enabledCipherSuites = |
|
302 |
CipherSuite.validValuesOf(suites); |
|
303 |
} |
|
304 |
||
305 |
@Override |
|
306 |
public String[] getSupportedProtocols() { |
|
307 |
return ProtocolVersion.toStringArray( |
|
308 |
sslContext.getSupportedProtocolVersions()); |
|
309 |
} |
|
310 |
||
311 |
@Override |
|
312 |
public synchronized String[] getEnabledProtocols() { |
|
313 |
return ProtocolVersion.toStringArray( |
|
314 |
conContext.sslConfig.enabledProtocols); |
|
315 |
} |
|
316 |
||
317 |
@Override |
|
318 |
public synchronized void setEnabledProtocols(String[] protocols) { |
|
319 |
if (protocols == null) { |
|
320 |
throw new IllegalArgumentException("Protocols cannot be null"); |
|
321 |
} |
|
322 |
||
323 |
conContext.sslConfig.enabledProtocols = |
|
324 |
ProtocolVersion.namesOf(protocols); |
|
325 |
} |
|
326 |
||
327 |
@Override |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
328 |
public SSLSession getSession() { |
50768 | 329 |
try { |
330 |
// start handshaking, if failed, the connection will be closed. |
|
331 |
ensureNegotiated(); |
|
332 |
} catch (IOException ioe) { |
|
333 |
if (SSLLogger.isOn && SSLLogger.isOn("handshake")) { |
|
334 |
SSLLogger.severe("handshake failed", ioe); |
|
335 |
} |
|
336 |
||
337 |
return SSLSessionImpl.nullSession; |
|
338 |
} |
|
339 |
||
340 |
return conContext.conSession; |
|
341 |
} |
|
342 |
||
343 |
@Override |
|
344 |
public synchronized SSLSession getHandshakeSession() { |
|
345 |
if (conContext.handshakeContext != null) { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
346 |
synchronized (this) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
347 |
if (conContext.handshakeContext != null) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
348 |
return conContext.handshakeContext.handshakeSession; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
349 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
350 |
} |
50768 | 351 |
} |
352 |
||
353 |
return null; |
|
354 |
} |
|
355 |
||
356 |
@Override |
|
357 |
public synchronized void addHandshakeCompletedListener( |
|
358 |
HandshakeCompletedListener listener) { |
|
359 |
if (listener == null) { |
|
360 |
throw new IllegalArgumentException("listener is null"); |
|
361 |
} |
|
362 |
||
363 |
conContext.sslConfig.addHandshakeCompletedListener(listener); |
|
364 |
} |
|
365 |
||
366 |
@Override |
|
367 |
public synchronized void removeHandshakeCompletedListener( |
|
368 |
HandshakeCompletedListener listener) { |
|
369 |
if (listener == null) { |
|
370 |
throw new IllegalArgumentException("listener is null"); |
|
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
371 |
} |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
372 |
|
50768 | 373 |
conContext.sslConfig.removeHandshakeCompletedListener(listener); |
374 |
} |
|
375 |
||
376 |
@Override |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
377 |
public void startHandshake() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
378 |
if (!isConnected) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
379 |
throw new SocketException("Socket is not connected"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
380 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
381 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
382 |
if (conContext.isBroken || conContext.isInboundClosed() || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
383 |
conContext.isOutboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
384 |
throw new SocketException("Socket has been closed or broken"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
385 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
386 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
387 |
synchronized (conContext) { // handshake lock |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
388 |
// double check the context status |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
389 |
if (conContext.isBroken || conContext.isInboundClosed() || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
390 |
conContext.isOutboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
391 |
throw new SocketException("Socket has been closed or broken"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
392 |
} |
50768 | 393 |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
394 |
try { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
395 |
conContext.kickstart(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
396 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
397 |
// All initial handshaking goes through this operation until we |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
398 |
// have a valid SSL connection. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
399 |
// |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
400 |
// Handle handshake messages only, need no application data. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
401 |
if (!conContext.isNegotiated) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
402 |
readHandshakeRecord(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
403 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
404 |
} catch (IOException ioe) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
405 |
conContext.fatal(Alert.HANDSHAKE_FAILURE, |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
406 |
"Couldn't kickstart handshaking", ioe); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
407 |
} catch (Exception oe) { // including RuntimeException |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
408 |
handleException(oe); |
50768 | 409 |
} |
410 |
} |
|
411 |
} |
|
412 |
||
413 |
@Override |
|
414 |
public synchronized void setUseClientMode(boolean mode) { |
|
415 |
conContext.setUseClientMode(mode); |
|
416 |
} |
|
417 |
||
418 |
@Override |
|
419 |
public synchronized boolean getUseClientMode() { |
|
420 |
return conContext.sslConfig.isClientMode; |
|
421 |
} |
|
422 |
||
423 |
@Override |
|
424 |
public synchronized void setNeedClientAuth(boolean need) { |
|
425 |
conContext.sslConfig.clientAuthType = |
|
426 |
(need ? ClientAuthType.CLIENT_AUTH_REQUIRED : |
|
427 |
ClientAuthType.CLIENT_AUTH_NONE); |
|
428 |
} |
|
429 |
||
430 |
@Override |
|
431 |
public synchronized boolean getNeedClientAuth() { |
|
432 |
return (conContext.sslConfig.clientAuthType == |
|
433 |
ClientAuthType.CLIENT_AUTH_REQUIRED); |
|
434 |
} |
|
435 |
||
436 |
@Override |
|
437 |
public synchronized void setWantClientAuth(boolean want) { |
|
438 |
conContext.sslConfig.clientAuthType = |
|
439 |
(want ? ClientAuthType.CLIENT_AUTH_REQUESTED : |
|
440 |
ClientAuthType.CLIENT_AUTH_NONE); |
|
441 |
} |
|
442 |
||
443 |
@Override |
|
444 |
public synchronized boolean getWantClientAuth() { |
|
445 |
return (conContext.sslConfig.clientAuthType == |
|
446 |
ClientAuthType.CLIENT_AUTH_REQUESTED); |
|
447 |
} |
|
448 |
||
449 |
@Override |
|
450 |
public synchronized void setEnableSessionCreation(boolean flag) { |
|
451 |
conContext.sslConfig.enableSessionCreation = flag; |
|
452 |
} |
|
453 |
||
454 |
@Override |
|
455 |
public synchronized boolean getEnableSessionCreation() { |
|
456 |
return conContext.sslConfig.enableSessionCreation; |
|
457 |
} |
|
458 |
||
459 |
@Override |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
460 |
public boolean isClosed() { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
461 |
return tlsIsClosed; |
50768 | 462 |
} |
463 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
464 |
// Please don't synchronized this method. Otherwise, the read and close |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
465 |
// locks may be deadlocked. |
50768 | 466 |
@Override |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
467 |
public void close() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
468 |
if (tlsIsClosed) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
469 |
return; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
470 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
471 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
472 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
473 |
SSLLogger.fine("duplex close of SSLSocket"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
474 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
475 |
|
50768 | 476 |
try { |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
477 |
// shutdown output bound, which may have been closed previously. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
478 |
if (!isOutputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
479 |
duplexCloseOutput(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
480 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
481 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
482 |
// shutdown input bound, which may have been closed previously. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
483 |
if (!isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
484 |
duplexCloseInput(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
485 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
486 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
487 |
if (!isClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
488 |
// close the connection directly |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
489 |
closeSocket(false); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
490 |
} |
50768 | 491 |
} catch (IOException ioe) { |
492 |
// ignore the exception |
|
493 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
494 |
SSLLogger.warning("SSLSocket duplex close failed", ioe); |
50768 | 495 |
} |
496 |
} finally { |
|
497 |
tlsIsClosed = true; |
|
498 |
} |
|
499 |
} |
|
500 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
501 |
/** |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
502 |
* Duplex close, start from closing outbound. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
503 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
504 |
* For TLS 1.2 [RFC 5246], unless some other fatal alert has been |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
505 |
* transmitted, each party is required to send a close_notify alert |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
506 |
* before closing the write side of the connection. The other party |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
507 |
* MUST respond with a close_notify alert of its own and close down |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
508 |
* the connection immediately, discarding any pending writes. It is |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
509 |
* not required for the initiator of the close to wait for the responding |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
510 |
* close_notify alert before closing the read side of the connection. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
511 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
512 |
* For TLS 1.3, Each party MUST send a close_notify alert before |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
513 |
* closing its write side of the connection, unless it has already sent |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
514 |
* some error alert. This does not have any effect on its read side of |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
515 |
* the connection. Both parties need not wait to receive a close_notify |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
516 |
* alert before closing their read side of the connection, though doing |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
517 |
* so would introduce the possibility of truncation. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
518 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
519 |
* In order to support user initiated duplex-close for TLS 1.3 connections, |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
520 |
* the user_canceled alert is used together with the close_notify alert. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
521 |
*/ |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
522 |
private void duplexCloseOutput() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
523 |
boolean useUserCanceled = false; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
524 |
boolean hasCloseReceipt = false; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
525 |
if (conContext.isNegotiated) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
526 |
if (!conContext.protocolVersion.useTLS13PlusSpec()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
527 |
hasCloseReceipt = true; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
528 |
} else { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
529 |
// Use a user_canceled alert for TLS 1.3 duplex close. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
530 |
useUserCanceled = true; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
531 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
532 |
} else if (conContext.handshakeContext != null) { // initial handshake |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
533 |
// Use user_canceled alert regardless the protocol versions. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
534 |
useUserCanceled = true; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
535 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
536 |
// The protocol version may have been negotiated. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
537 |
ProtocolVersion pv = conContext.handshakeContext.negotiatedProtocol; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
538 |
if (pv == null || (!pv.useTLS13PlusSpec())) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
539 |
hasCloseReceipt = true; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
540 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
541 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
542 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
543 |
// Need a lock here so that the user_canceled alert and the |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
544 |
// close_notify alert can be delivered together. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
545 |
try { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
546 |
synchronized (conContext.outputRecord) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
547 |
// send a user_canceled alert if needed. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
548 |
if (useUserCanceled) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
549 |
conContext.warning(Alert.USER_CANCELED); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
550 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
551 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
552 |
// send a close_notify alert |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
553 |
conContext.warning(Alert.CLOSE_NOTIFY); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
554 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
555 |
} finally { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
556 |
if (!conContext.isOutboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
557 |
conContext.outputRecord.close(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
558 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
559 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
560 |
if ((autoClose || !isLayered()) && !super.isOutputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
561 |
super.shutdownOutput(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
562 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
563 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
564 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
565 |
if (!isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
566 |
bruteForceCloseInput(hasCloseReceipt); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
567 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
568 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
569 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
570 |
/** |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
571 |
* Duplex close, start from closing inbound. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
572 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
573 |
* This method should only be called when the outbound has been closed, |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
574 |
* but the inbound is still open. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
575 |
*/ |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
576 |
private void duplexCloseInput() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
577 |
boolean hasCloseReceipt = false; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
578 |
if (conContext.isNegotiated && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
579 |
!conContext.protocolVersion.useTLS13PlusSpec()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
580 |
hasCloseReceipt = true; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
581 |
} // No close receipt if handshake has no completed. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
582 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
583 |
bruteForceCloseInput(hasCloseReceipt); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
584 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
585 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
586 |
/** |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
587 |
* Brute force close the input bound. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
588 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
589 |
* This method should only be called when the outbound has been closed, |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
590 |
* but the inbound is still open. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
591 |
*/ |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
592 |
private void bruteForceCloseInput( |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
593 |
boolean hasCloseReceipt) throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
594 |
if (hasCloseReceipt) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
595 |
// It is not required for the initiator of the close to wait for |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
596 |
// the responding close_notify alert before closing the read side |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
597 |
// of the connection. However, if the application protocol using |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
598 |
// TLS provides that any data may be carried over the underlying |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
599 |
// transport after the TLS connection is closed, the TLS |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
600 |
// implementation MUST receive a "close_notify" alert before |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
601 |
// indicating end-of-data to the application-layer. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
602 |
try { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
603 |
this.shutdown(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
604 |
} finally { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
605 |
if (!isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
606 |
shutdownInput(false); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
607 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
608 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
609 |
} else { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
610 |
if (!conContext.isInboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
611 |
conContext.inputRecord.close(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
612 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
613 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
614 |
if ((autoClose || !isLayered()) && !super.isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
615 |
super.shutdownInput(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
616 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
617 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
618 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
619 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
620 |
// Please don't synchronized this method. Otherwise, the read and close |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
621 |
// locks may be deadlocked. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
622 |
@Override |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
623 |
public void shutdownInput() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
624 |
shutdownInput(true); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
625 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
626 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
627 |
// It is not required to check the close_notify receipt unless an |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
628 |
// application call shutdownInput() explicitly. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
629 |
private void shutdownInput( |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
630 |
boolean checkCloseNotify) throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
631 |
if (isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
632 |
return; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
633 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
634 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
635 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
636 |
SSLLogger.fine("close inbound of SSLSocket"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
637 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
638 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
639 |
// Is it ready to close inbound? |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
640 |
// |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
641 |
// No need to throw exception if the initial handshake is not started. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
642 |
if (checkCloseNotify && !conContext.isInputCloseNotified && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
643 |
(conContext.isNegotiated || conContext.handshakeContext != null)) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
644 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
645 |
conContext.fatal(Alert.INTERNAL_ERROR, |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
646 |
"closing inbound before receiving peer's close_notify"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
647 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
648 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
649 |
conContext.closeInbound(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
650 |
if ((autoClose || !isLayered()) && !super.isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
651 |
super.shutdownInput(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
652 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
653 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
654 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
655 |
@Override |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
656 |
public boolean isInputShutdown() { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
657 |
return conContext.isInboundClosed() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
658 |
((autoClose || !isLayered()) ? super.isInputShutdown(): true); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
659 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
660 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
661 |
// Please don't synchronized this method. Otherwise, the read and close |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
662 |
// locks may be deadlocked. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
663 |
@Override |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
664 |
public void shutdownOutput() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
665 |
if (isOutputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
666 |
return; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
667 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
668 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
669 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
670 |
SSLLogger.fine("close outbound of SSLSocket"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
671 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
672 |
conContext.closeOutbound(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
673 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
674 |
if ((autoClose || !isLayered()) && !super.isOutputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
675 |
super.shutdownOutput(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
676 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
677 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
678 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
679 |
@Override |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
680 |
public boolean isOutputShutdown() { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
681 |
return conContext.isOutboundClosed() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
682 |
((autoClose || !isLayered()) ? super.isOutputShutdown(): true); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
683 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
684 |
|
50768 | 685 |
@Override |
686 |
public synchronized InputStream getInputStream() throws IOException { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
687 |
if (isClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
688 |
throw new SocketException("Socket is closed"); |
50768 | 689 |
} |
690 |
||
691 |
if (!isConnected) { |
|
692 |
throw new SocketException("Socket is not connected"); |
|
693 |
} |
|
694 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
695 |
if (conContext.isInboundClosed() || isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
696 |
throw new SocketException("Socket input is already shutdown"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
697 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
698 |
|
50768 | 699 |
return appInput; |
700 |
} |
|
701 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
702 |
private void ensureNegotiated() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
703 |
if (conContext.isNegotiated || conContext.isBroken || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
704 |
conContext.isInboundClosed() || conContext.isOutboundClosed()) { |
50768 | 705 |
return; |
706 |
} |
|
707 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
708 |
synchronized (conContext) { // handshake lock |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
709 |
// double check the context status |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
710 |
if (conContext.isNegotiated || conContext.isBroken || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
711 |
conContext.isInboundClosed() || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
712 |
conContext.isOutboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
713 |
return; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
714 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
715 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
716 |
startHandshake(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
717 |
} |
2 | 718 |
} |
719 |
||
720 |
/** |
|
50768 | 721 |
* InputStream for application data as returned by |
722 |
* SSLSocket.getInputStream(). |
|
2 | 723 |
*/ |
50768 | 724 |
private class AppInputStream extends InputStream { |
725 |
// One element array used to implement the single byte read() method |
|
726 |
private final byte[] oneByte = new byte[1]; |
|
727 |
||
728 |
// the temporary buffer used to read network |
|
729 |
private ByteBuffer buffer; |
|
730 |
||
731 |
// Is application data available in the stream? |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
732 |
private volatile boolean appDataIsAvailable; |
50768 | 733 |
|
734 |
AppInputStream() { |
|
735 |
this.appDataIsAvailable = false; |
|
736 |
this.buffer = ByteBuffer.allocate(4096); |
|
737 |
} |
|
738 |
||
739 |
/** |
|
740 |
* Return the minimum number of bytes that can be read |
|
741 |
* without blocking. |
|
742 |
*/ |
|
743 |
@Override |
|
744 |
public int available() throws IOException { |
|
745 |
// Currently not synchronized. |
|
746 |
if ((!appDataIsAvailable) || checkEOF()) { |
|
747 |
return 0; |
|
748 |
} |
|
749 |
||
750 |
return buffer.remaining(); |
|
751 |
} |
|
752 |
||
753 |
/** |
|
754 |
* Read a single byte, returning -1 on non-fault EOF status. |
|
2 | 755 |
*/ |
50768 | 756 |
@Override |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
757 |
public int read() throws IOException { |
50768 | 758 |
int n = read(oneByte, 0, 1); |
759 |
if (n <= 0) { // EOF |
|
760 |
return -1; |
|
761 |
} |
|
762 |
||
763 |
return oneByte[0] & 0xFF; |
|
764 |
} |
|
2 | 765 |
|
50768 | 766 |
/** |
767 |
* Reads up to {@code len} bytes of data from the input stream |
|
768 |
* into an array of bytes. |
|
769 |
* |
|
770 |
* An attempt is made to read as many as {@code len} bytes, but a |
|
771 |
* smaller number may be read. The number of bytes actually read |
|
772 |
* is returned as an integer. |
|
773 |
* |
|
774 |
* If the layer above needs more data, it asks for more, so we |
|
775 |
* are responsible only for blocking to fill at most one buffer, |
|
776 |
* and returning "-1" on non-fault EOF status. |
|
777 |
*/ |
|
778 |
@Override |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
779 |
public int read(byte[] b, int off, int len) |
50768 | 780 |
throws IOException { |
781 |
if (b == null) { |
|
782 |
throw new NullPointerException("the target buffer is null"); |
|
783 |
} else if (off < 0 || len < 0 || len > b.length - off) { |
|
784 |
throw new IndexOutOfBoundsException( |
|
785 |
"buffer length: " + b.length + ", offset; " + off + |
|
786 |
", bytes to read:" + len); |
|
787 |
} else if (len == 0) { |
|
788 |
return 0; |
|
789 |
} |
|
790 |
||
791 |
if (checkEOF()) { |
|
792 |
return -1; |
|
793 |
} |
|
794 |
||
795 |
// start handshaking if the connection has not been negotiated. |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
796 |
if (!conContext.isNegotiated && !conContext.isBroken && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
797 |
!conContext.isInboundClosed() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
798 |
!conContext.isOutboundClosed()) { |
50768 | 799 |
ensureNegotiated(); |
800 |
} |
|
801 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
802 |
// Check if the Socket is invalid (error or closed). |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
803 |
if (!conContext.isNegotiated || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
804 |
conContext.isBroken || conContext.isInboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
805 |
throw new SocketException("Connection or inbound has closed"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
806 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
807 |
|
50768 | 808 |
// Read the available bytes at first. |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
809 |
// |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
810 |
// Note that the receiving and processing of post-handshake message |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
811 |
// are also synchronized with the read lock. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
812 |
synchronized (this) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
813 |
int remains = available(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
814 |
if (remains > 0) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
815 |
int howmany = Math.min(remains, len); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
816 |
buffer.get(b, off, howmany); |
30904 | 817 |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
818 |
return howmany; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
819 |
} |
50768 | 820 |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
821 |
appDataIsAvailable = false; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
822 |
try { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
823 |
ByteBuffer bb = readApplicationRecord(buffer); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
824 |
if (bb == null) { // EOF |
50768 | 825 |
return -1; |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
826 |
} else { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
827 |
// The buffer may be reallocated for bigger capacity. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
828 |
buffer = bb; |
50768 | 829 |
} |
830 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
831 |
bb.flip(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
832 |
int volume = Math.min(len, bb.remaining()); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
833 |
buffer.get(b, off, volume); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
834 |
appDataIsAvailable = true; |
50768 | 835 |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
836 |
return volume; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
837 |
} catch (Exception e) { // including RuntimeException |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
838 |
// shutdown and rethrow (wrapped) exception as appropriate |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
839 |
handleException(e); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
840 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
841 |
// dummy for compiler |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
842 |
return -1; |
50768 | 843 |
} |
844 |
} |
|
845 |
} |
|
2 | 846 |
|
50768 | 847 |
/** |
848 |
* Skip n bytes. |
|
849 |
* |
|
850 |
* This implementation is somewhat less efficient than possible, but |
|
851 |
* not badly so (redundant copy). We reuse the read() code to keep |
|
51986
c1db377f6300
8200381: Typos in javadoc - missing verb "be" and alike
igerasim
parents:
51407
diff
changeset
|
852 |
* things simpler. |
50768 | 853 |
*/ |
854 |
@Override |
|
855 |
public synchronized long skip(long n) throws IOException { |
|
856 |
// dummy array used to implement skip() |
|
857 |
byte[] skipArray = new byte[256]; |
|
2 | 858 |
|
50768 | 859 |
long skipped = 0; |
860 |
while (n > 0) { |
|
861 |
int len = (int)Math.min(n, skipArray.length); |
|
862 |
int r = read(skipArray, 0, len); |
|
863 |
if (r <= 0) { |
|
864 |
break; |
|
865 |
} |
|
866 |
n -= r; |
|
867 |
skipped += r; |
|
868 |
} |
|
869 |
||
870 |
return skipped; |
|
871 |
} |
|
872 |
||
873 |
@Override |
|
874 |
public void close() throws IOException { |
|
875 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
876 |
SSLLogger.finest("Closing input stream"); |
|
877 |
} |
|
878 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
879 |
try { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
880 |
shutdownInput(false); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
881 |
} catch (IOException ioe) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
882 |
// ignore the exception |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
883 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
884 |
SSLLogger.warning("input stream close failed", ioe); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
885 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
886 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
887 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
888 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
889 |
/** |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
890 |
* Return whether we have reached end-of-file. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
891 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
892 |
* If the socket is not connected, has been shutdown because of an error |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
893 |
* or has been closed, throw an Exception. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
894 |
*/ |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
895 |
private boolean checkEOF() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
896 |
if (conContext.isInboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
897 |
return true; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
898 |
} else if (conContext.isInputCloseNotified || conContext.isBroken) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
899 |
if (conContext.closeReason == null) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
900 |
return true; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
901 |
} else { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
902 |
throw new SSLException( |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
903 |
"Connection has closed: " + conContext.closeReason, |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
904 |
conContext.closeReason); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
905 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
906 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
907 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
908 |
return false; |
50768 | 909 |
} |
2 | 910 |
} |
911 |
||
50768 | 912 |
@Override |
913 |
public synchronized OutputStream getOutputStream() throws IOException { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
914 |
if (isClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
915 |
throw new SocketException("Socket is closed"); |
50768 | 916 |
} |
2 | 917 |
|
50768 | 918 |
if (!isConnected) { |
919 |
throw new SocketException("Socket is not connected"); |
|
920 |
} |
|
921 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
922 |
if (conContext.isOutboundDone() || isOutputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
923 |
throw new SocketException("Socket output is already shutdown"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
924 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
925 |
|
50768 | 926 |
return appOutput; |
927 |
} |
|
928 |
||
929 |
||
930 |
/** |
|
931 |
* OutputStream for application data as returned by |
|
932 |
* SSLSocket.getOutputStream(). |
|
12428 | 933 |
*/ |
50768 | 934 |
private class AppOutputStream extends OutputStream { |
935 |
// One element array used to implement the write(byte) method |
|
936 |
private final byte[] oneByte = new byte[1]; |
|
937 |
||
938 |
@Override |
|
939 |
public void write(int i) throws IOException { |
|
940 |
oneByte[0] = (byte)i; |
|
941 |
write(oneByte, 0, 1); |
|
942 |
} |
|
943 |
||
944 |
@Override |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
945 |
public void write(byte[] b, |
50768 | 946 |
int off, int len) throws IOException { |
947 |
if (b == null) { |
|
948 |
throw new NullPointerException("the source buffer is null"); |
|
949 |
} else if (off < 0 || len < 0 || len > b.length - off) { |
|
950 |
throw new IndexOutOfBoundsException( |
|
951 |
"buffer length: " + b.length + ", offset; " + off + |
|
952 |
", bytes to read:" + len); |
|
953 |
} else if (len == 0) { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
954 |
// |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
955 |
// Don't bother to really write empty records. We went this |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
956 |
// far to drive the handshake machinery, for correctness; not |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
957 |
// writing empty records improves performance by cutting CPU |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
958 |
// time and network resource usage. However, some protocol |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
959 |
// implementations are fragile and don't like to see empty |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
960 |
// records, so this also increases robustness. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
961 |
// |
50768 | 962 |
return; |
963 |
} |
|
964 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
965 |
// Start handshaking if the connection has not been negotiated. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
966 |
if (!conContext.isNegotiated && !conContext.isBroken && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
967 |
!conContext.isInboundClosed() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
968 |
!conContext.isOutboundClosed()) { |
50768 | 969 |
ensureNegotiated(); |
970 |
} |
|
971 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
972 |
// Check if the Socket is invalid (error or closed). |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
973 |
if (!conContext.isNegotiated || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
974 |
conContext.isBroken || conContext.isOutboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
975 |
throw new SocketException("Connection or outbound has closed"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
976 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
977 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
978 |
// |
2 | 979 |
|
50768 | 980 |
// Delegate the writing to the underlying socket. |
981 |
try { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
982 |
conContext.outputRecord.deliver(b, off, len); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
983 |
} catch (SSLHandshakeException she) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
984 |
// may be record sequence number overflow |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
985 |
conContext.fatal(Alert.HANDSHAKE_FAILURE, she); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
986 |
} catch (IOException e) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
987 |
conContext.fatal(Alert.UNEXPECTED_MESSAGE, e); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
988 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
989 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
990 |
// Is the sequence number is nearly overflow, or has the key usage |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
991 |
// limit been reached? |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
992 |
if (conContext.outputRecord.seqNumIsHuge() || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
993 |
conContext.outputRecord.writeCipher.atKeyLimit()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
994 |
tryKeyUpdate(); |
50768 | 995 |
} |
996 |
} |
|
2 | 997 |
|
50768 | 998 |
@Override |
999 |
public void close() throws IOException { |
|
1000 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
1001 |
SSLLogger.finest("Closing output stream"); |
|
1002 |
} |
|
1003 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1004 |
try { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1005 |
shutdownOutput(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1006 |
} catch (IOException ioe) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1007 |
// ignore the exception |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1008 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1009 |
SSLLogger.warning("output stream close failed", ioe); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1010 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1011 |
} |
50768 | 1012 |
} |
1013 |
} |
|
1014 |
||
1015 |
@Override |
|
1016 |
public synchronized SSLParameters getSSLParameters() { |
|
1017 |
return conContext.sslConfig.getSSLParameters(); |
|
1018 |
} |
|
1019 |
||
1020 |
@Override |
|
1021 |
public synchronized void setSSLParameters(SSLParameters params) { |
|
1022 |
conContext.sslConfig.setSSLParameters(params); |
|
2 | 1023 |
|
50768 | 1024 |
if (conContext.sslConfig.maximumPacketSize != 0) { |
1025 |
conContext.outputRecord.changePacketSize( |
|
1026 |
conContext.sslConfig.maximumPacketSize); |
|
1027 |
} |
|
1028 |
} |
|
1029 |
||
1030 |
@Override |
|
1031 |
public synchronized String getApplicationProtocol() { |
|
1032 |
return conContext.applicationProtocol; |
|
1033 |
} |
|
1034 |
||
1035 |
@Override |
|
1036 |
public synchronized String getHandshakeApplicationProtocol() { |
|
1037 |
if (conContext.handshakeContext != null) { |
|
1038 |
return conContext.handshakeContext.applicationProtocol; |
|
1039 |
} |
|
2 | 1040 |
|
50768 | 1041 |
return null; |
1042 |
} |
|
1043 |
||
1044 |
@Override |
|
1045 |
public synchronized void setHandshakeApplicationProtocolSelector( |
|
1046 |
BiFunction<SSLSocket, List<String>, String> selector) { |
|
1047 |
conContext.sslConfig.socketAPSelector = selector; |
|
1048 |
} |
|
2 | 1049 |
|
50768 | 1050 |
@Override |
1051 |
public synchronized BiFunction<SSLSocket, List<String>, String> |
|
1052 |
getHandshakeApplicationProtocolSelector() { |
|
1053 |
return conContext.sslConfig.socketAPSelector; |
|
1054 |
} |
|
1055 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1056 |
/** |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1057 |
* Read the initial handshake records. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1058 |
*/ |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1059 |
private int readHandshakeRecord() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1060 |
while (!conContext.isInboundClosed()) { |
50768 | 1061 |
try { |
1062 |
Plaintext plainText = decode(null); |
|
1063 |
if ((plainText.contentType == ContentType.HANDSHAKE.id) && |
|
1064 |
conContext.isNegotiated) { |
|
1065 |
return 0; |
|
1066 |
} |
|
1067 |
} catch (SSLException ssle) { |
|
1068 |
throw ssle; |
|
1069 |
} catch (IOException ioe) { |
|
1070 |
if (!(ioe instanceof SSLException)) { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1071 |
throw new SSLException("readHandshakeRecord", ioe); |
50768 | 1072 |
} else { |
1073 |
throw ioe; |
|
1074 |
} |
|
1075 |
} |
|
2 | 1076 |
} |
30904 | 1077 |
|
50768 | 1078 |
return -1; |
30904 | 1079 |
} |
1080 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1081 |
/** |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1082 |
* Read application data record. Used by AppInputStream only, but defined |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1083 |
* here so as to use the socket level synchronization. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1084 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1085 |
* Note that the connection guarantees that handshake, alert, and change |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1086 |
* cipher spec data streams are handled as they arrive, so we never see |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1087 |
* them here. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1088 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1089 |
* Note: Please be careful about the synchronization, and don't use this |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1090 |
* method other than in the AppInputStream class! |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1091 |
*/ |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1092 |
private ByteBuffer readApplicationRecord( |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1093 |
ByteBuffer buffer) throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1094 |
while (!conContext.isInboundClosed()) { |
50768 | 1095 |
/* |
1096 |
* clean the buffer and check if it is too small, e.g. because |
|
1097 |
* the AppInputStream did not have the chance to see the |
|
1098 |
* current packet length but rather something like that of the |
|
1099 |
* handshake before. In that case we return 0 at this point to |
|
1100 |
* give the caller the chance to adjust the buffer. |
|
1101 |
*/ |
|
1102 |
buffer.clear(); |
|
1103 |
int inLen = conContext.inputRecord.bytesInCompletePacket(); |
|
1104 |
if (inLen < 0) { // EOF |
|
1105 |
handleEOF(null); |
|
2 | 1106 |
|
50768 | 1107 |
// if no exception thrown |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1108 |
return null; |
50768 | 1109 |
} |
30904 | 1110 |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1111 |
// Is this packet bigger than SSL/TLS normally allows? |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1112 |
if (inLen > SSLRecord.maxLargeRecordSize) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1113 |
throw new SSLProtocolException( |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1114 |
"Illegal packet size: " + inLen); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1115 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1116 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1117 |
if (inLen > buffer.remaining()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1118 |
buffer = ByteBuffer.allocate(inLen); |
50768 | 1119 |
} |
7039 | 1120 |
|
50768 | 1121 |
try { |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1122 |
Plaintext plainText; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1123 |
synchronized (this) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1124 |
plainText = decode(buffer); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1125 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1126 |
if (plainText.contentType == ContentType.APPLICATION_DATA.id && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1127 |
buffer.position() > 0) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1128 |
return buffer; |
30904 | 1129 |
} |
50768 | 1130 |
} catch (SSLException ssle) { |
1131 |
throw ssle; |
|
1132 |
} catch (IOException ioe) { |
|
1133 |
if (!(ioe instanceof SSLException)) { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1134 |
throw new SSLException("readApplicationRecord", ioe); |
50768 | 1135 |
} else { |
1136 |
throw ioe; |
|
30904 | 1137 |
} |
50768 | 1138 |
} |
7039 | 1139 |
} |
1140 |
||
50768 | 1141 |
// |
1142 |
// couldn't read, due to some kind of error |
|
1143 |
// |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1144 |
return null; |
50768 | 1145 |
} |
1146 |
||
1147 |
private Plaintext decode(ByteBuffer destination) throws IOException { |
|
1148 |
Plaintext plainText; |
|
1149 |
try { |
|
1150 |
if (destination == null) { |
|
1151 |
plainText = SSLTransport.decode(conContext, |
|
1152 |
null, 0, 0, null, 0, 0); |
|
1153 |
} else { |
|
1154 |
plainText = SSLTransport.decode(conContext, |
|
1155 |
null, 0, 0, new ByteBuffer[]{destination}, 0, 1); |
|
7039 | 1156 |
} |
50768 | 1157 |
} catch (EOFException eofe) { |
1158 |
// EOFException is special as it is related to close_notify. |
|
1159 |
plainText = handleEOF(eofe); |
|
7039 | 1160 |
} |
30904 | 1161 |
|
50768 | 1162 |
// Is the sequence number is nearly overflow? |
1163 |
if (plainText != Plaintext.PLAINTEXT_NULL && |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1164 |
(conContext.inputRecord.seqNumIsHuge() || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1165 |
conContext.inputRecord.readCipher.atKeyLimit())) { |
50768 | 1166 |
tryKeyUpdate(); |
1167 |
} |
|
30904 | 1168 |
|
50768 | 1169 |
return plainText; |
2 | 1170 |
} |
1171 |
||
1172 |
/** |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1173 |
* Try key update for sequence number wrap or key usage limit. |
50768 | 1174 |
* |
1175 |
* Note that in order to maintain the handshake status properly, we check |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1176 |
* the sequence number and key usage limit after the last record |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1177 |
* reading/writing process. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1178 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1179 |
* As we request renegotiation or close the connection for wrapped sequence |
50768 | 1180 |
* number when there is enough sequence number space left to handle a few |
1181 |
* more records, so the sequence number of the last record cannot be |
|
1182 |
* wrapped. |
|
2 | 1183 |
*/ |
50768 | 1184 |
private void tryKeyUpdate() throws IOException { |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1185 |
// Don't bother to kickstart if handshaking is in progress, or if the |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1186 |
// connection is not duplex-open. |
50768 | 1187 |
if ((conContext.handshakeContext == null) && |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1188 |
!conContext.isOutboundClosed() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1189 |
!conContext.isInboundClosed() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1190 |
!conContext.isBroken) { |
50768 | 1191 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1192 |
SSLLogger.finest("trigger key update"); |
50768 | 1193 |
} |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1194 |
startHandshake(); |
2 | 1195 |
} |
1196 |
} |
|
1197 |
||
1198 |
/** |
|
50768 | 1199 |
* Initialize the handshaker and socket streams. |
2 | 1200 |
* |
50768 | 1201 |
* Called by connect, the layered constructor, and SSLServerSocket. |
2 | 1202 |
*/ |
50768 | 1203 |
synchronized void doneConnect() throws IOException { |
1204 |
// In server mode, it is not necessary to set host and serverNames. |
|
1205 |
// Otherwise, would require a reverse DNS lookup to get the hostname. |
|
1206 |
if ((peerHost == null) || (peerHost.length() == 0)) { |
|
1207 |
boolean useNameService = |
|
1208 |
trustNameService && conContext.sslConfig.isClientMode; |
|
1209 |
useImplicitHost(useNameService); |
|
1210 |
} else { |
|
1211 |
conContext.sslConfig.serverNames = |
|
1212 |
Utilities.addToSNIServerNameList( |
|
1213 |
conContext.sslConfig.serverNames, peerHost); |
|
2 | 1214 |
} |
1215 |
||
50768 | 1216 |
InputStream sockInput = super.getInputStream(); |
1217 |
conContext.inputRecord.setReceiverStream(sockInput); |
|
2 | 1218 |
|
50768 | 1219 |
OutputStream sockOutput = super.getOutputStream(); |
1220 |
conContext.inputRecord.setDeliverStream(sockOutput); |
|
1221 |
conContext.outputRecord.setDeliverStream(sockOutput); |
|
2 | 1222 |
|
50768 | 1223 |
this.isConnected = true; |
2 | 1224 |
} |
1225 |
||
50768 | 1226 |
private void useImplicitHost(boolean useNameService) { |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1227 |
// Note: If the local name service is not trustworthy, reverse |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1228 |
// host name resolution should not be performed for endpoint |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1229 |
// identification. Use the application original specified |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1230 |
// hostname or IP address instead. |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1231 |
|
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
51986
diff
changeset
|
1232 |
// Get the original hostname via jdk.internal.access.SharedSecrets |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1233 |
InetAddress inetAddress = getInetAddress(); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1234 |
if (inetAddress == null) { // not connected |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1235 |
return; |
31687 | 1236 |
} |
1237 |
||
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1238 |
JavaNetInetAddressAccess jna = |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1239 |
SharedSecrets.getJavaNetInetAddressAccess(); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1240 |
String originalHostname = jna.getOriginalHostName(inetAddress); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1241 |
if ((originalHostname != null) && |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1242 |
(originalHostname.length() != 0)) { |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1243 |
|
50768 | 1244 |
this.peerHost = originalHostname; |
1245 |
if (conContext.sslConfig.serverNames.isEmpty() && |
|
1246 |
!conContext.sslConfig.noSniExtension) { |
|
1247 |
conContext.sslConfig.serverNames = |
|
1248 |
Utilities.addToSNIServerNameList( |
|
1249 |
conContext.sslConfig.serverNames, peerHost); |
|
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1250 |
} |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1251 |
|
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1252 |
return; |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1253 |
} |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1254 |
|
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1255 |
// No explicitly specified hostname, no server name indication. |
50768 | 1256 |
if (!useNameService) { |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1257 |
// The local name service is not trustworthy, use IP address. |
50768 | 1258 |
this.peerHost = inetAddress.getHostAddress(); |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1259 |
} else { |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1260 |
// Use the underlying reverse host name resolution service. |
50768 | 1261 |
this.peerHost = getInetAddress().getHostName(); |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1262 |
} |
31687 | 1263 |
} |
1264 |
||
5162
0dbedf4fdb8c
6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents:
2068
diff
changeset
|
1265 |
// ONLY used by HttpsClient to setup the URI specified hostname |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1266 |
// |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1267 |
// Please NOTE that this method MUST be called before calling to |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1268 |
// SSLSocket.setSSLParameters(). Otherwise, the {@code host} parameter |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1269 |
// may override SNIHostName in the customized server name indication. |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
1270 |
public synchronized void setHost(String host) { |
50768 | 1271 |
this.peerHost = host; |
1272 |
this.conContext.sslConfig.serverNames = |
|
1273 |
Utilities.addToSNIServerNameList( |
|
1274 |
conContext.sslConfig.serverNames, host); |
|
2 | 1275 |
} |
1276 |
||
1277 |
/** |
|
50768 | 1278 |
* Handle an exception. |
1279 |
* |
|
1280 |
* This method is called by top level exception handlers (in read(), |
|
1281 |
* write()) to make sure we always shutdown the connection correctly |
|
1282 |
* and do not pass runtime exception to the application. |
|
2 | 1283 |
* |
50768 | 1284 |
* This method never returns normally, it always throws an IOException. |
2 | 1285 |
*/ |
50768 | 1286 |
private void handleException(Exception cause) throws IOException { |
1287 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
1288 |
SSLLogger.warning("handling exception", cause); |
|
1289 |
} |
|
1290 |
||
1291 |
// Don't close the Socket in case of timeouts or interrupts. |
|
1292 |
if (cause instanceof InterruptedIOException) { |
|
1293 |
throw (IOException)cause; |
|
1294 |
} |
|
2 | 1295 |
|
50768 | 1296 |
// need to perform error shutdown |
1297 |
boolean isSSLException = (cause instanceof SSLException); |
|
1298 |
Alert alert; |
|
1299 |
if (isSSLException) { |
|
1300 |
if (cause instanceof SSLHandshakeException) { |
|
1301 |
alert = Alert.HANDSHAKE_FAILURE; |
|
1302 |
} else { |
|
1303 |
alert = Alert.UNEXPECTED_MESSAGE; |
|
1304 |
} |
|
1305 |
} else { |
|
1306 |
if (cause instanceof IOException) { |
|
1307 |
alert = Alert.UNEXPECTED_MESSAGE; |
|
1308 |
} else { |
|
1309 |
// RuntimeException |
|
1310 |
alert = Alert.INTERNAL_ERROR; |
|
1311 |
} |
|
2 | 1312 |
} |
50768 | 1313 |
conContext.fatal(alert, cause); |
2 | 1314 |
} |
1315 |
||
50768 | 1316 |
private Plaintext handleEOF(EOFException eofe) throws IOException { |
1317 |
if (requireCloseNotify || conContext.handshakeContext != null) { |
|
1318 |
SSLException ssle; |
|
1319 |
if (conContext.handshakeContext != null) { |
|
1320 |
ssle = new SSLHandshakeException( |
|
1321 |
"Remote host terminated the handshake"); |
|
1322 |
} else { |
|
1323 |
ssle = new SSLProtocolException( |
|
1324 |
"Remote host terminated the connection"); |
|
1325 |
} |
|
2 | 1326 |
|
50768 | 1327 |
if (eofe != null) { |
1328 |
ssle.initCause(eofe); |
|
1329 |
} |
|
1330 |
throw ssle; |
|
1331 |
} else { |
|
1332 |
// treat as if we had received a close_notify |
|
1333 |
conContext.isInputCloseNotified = true; |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1334 |
shutdownInput(); |
2 | 1335 |
|
50768 | 1336 |
return Plaintext.PLAINTEXT_NULL; |
2 | 1337 |
} |
1338 |
} |
|
1339 |
||
1340 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1341 |
@Override |
50768 | 1342 |
public String getPeerHost() { |
1343 |
return peerHost; |
|
2 | 1344 |
} |
1345 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1346 |
@Override |
50768 | 1347 |
public int getPeerPort() { |
1348 |
return getPort(); |
|
2 | 1349 |
} |
1350 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1351 |
@Override |
50768 | 1352 |
public boolean useDelegatedTask() { |
1353 |
return false; |
|
2 | 1354 |
} |
1355 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1356 |
@Override |
50768 | 1357 |
public void shutdown() throws IOException { |
1358 |
if (!isClosed()) { |
|
1359 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
1360 |
SSLLogger.fine("close the underlying socket"); |
|
1361 |
} |
|
2 | 1362 |
|
50768 | 1363 |
try { |
1364 |
if (conContext.isInputCloseNotified) { |
|
1365 |
// Close the connection, no wait for more peer response. |
|
1366 |
closeSocket(false); |
|
1367 |
} else { |
|
1368 |
// Close the connection, may wait for peer close_notify. |
|
1369 |
closeSocket(true); |
|
1370 |
} |
|
1371 |
} finally { |
|
1372 |
tlsIsClosed = true; |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1373 |
} |
7043 | 1374 |
} |
2 | 1375 |
} |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1376 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1377 |
private void closeSocket(boolean selfInitiated) throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1378 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1379 |
SSLLogger.fine("close the SSL connection " + |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1380 |
(selfInitiated ? "(initiative)" : "(passive)")); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1381 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1382 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1383 |
if (autoClose || !isLayered()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1384 |
super.close(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1385 |
} else if (selfInitiated) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1386 |
if (!conContext.isInboundClosed() && !isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1387 |
// wait for close_notify alert to clear input stream. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1388 |
waitForClose(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1389 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1390 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1391 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1392 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1393 |
/** |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1394 |
* Wait for close_notify alert for a graceful closure. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1395 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1396 |
* [RFC 5246] If the application protocol using TLS provides that any |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1397 |
* data may be carried over the underlying transport after the TLS |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1398 |
* connection is closed, the TLS implementation must receive the responding |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1399 |
* close_notify alert before indicating to the application layer that |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1400 |
* the TLS connection has ended. If the application protocol will not |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1401 |
* transfer any additional data, but will only close the underlying |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1402 |
* transport connection, then the implementation MAY choose to close the |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1403 |
* transport without waiting for the responding close_notify. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1404 |
*/ |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1405 |
private void waitForClose() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1406 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1407 |
SSLLogger.fine("wait for close_notify or alert"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1408 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1409 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1410 |
while (!conContext.isInboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1411 |
try { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1412 |
Plaintext plainText = decode(null); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1413 |
// discard and continue |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1414 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1415 |
SSLLogger.finest( |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1416 |
"discard plaintext while waiting for close", plainText); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1417 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1418 |
} catch (Exception e) { // including RuntimeException |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1419 |
handleException(e); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1420 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1421 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1422 |
} |
2 | 1423 |
} |