author | xuelei |
Fri, 01 Mar 2019 09:42:04 -0800 | |
changeset 53974 | c9f2b71057c1 |
parent 53958 | 196ab0abc685 |
child 54221 | 727ab25a4e24 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
53958
196ab0abc685
8219658: Deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
53067
diff
changeset
|
2 |
* Copyright (c) 1996, 2019, 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) { |
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
53018
diff
changeset
|
405 |
throw conContext.fatal(Alert.HANDSHAKE_FAILURE, |
51407
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()) { |
53067
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
611 |
try (conContext.inputRecord) { |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
612 |
// Try the best to use up the input records and close the |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
613 |
// socket gracefully, without impact the performance too |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
614 |
// much. |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
615 |
appInput.deplete(); |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
616 |
} |
51407
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 |
if ((autoClose || !isLayered()) && !super.isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
620 |
super.shutdownInput(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
621 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
622 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
623 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
624 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
625 |
// 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
|
626 |
// locks may be deadlocked. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
627 |
@Override |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
628 |
public void shutdownInput() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
629 |
shutdownInput(true); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
630 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
631 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
632 |
// 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
|
633 |
// application call shutdownInput() explicitly. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
634 |
private void shutdownInput( |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
635 |
boolean checkCloseNotify) throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
636 |
if (isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
637 |
return; |
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 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
640 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
641 |
SSLLogger.fine("close inbound of SSLSocket"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
642 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
643 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
644 |
// Is it ready to close inbound? |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
645 |
// |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
646 |
// 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
|
647 |
if (checkCloseNotify && !conContext.isInputCloseNotified && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
648 |
(conContext.isNegotiated || conContext.handshakeContext != null)) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
649 |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
53018
diff
changeset
|
650 |
throw conContext.fatal(Alert.INTERNAL_ERROR, |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
651 |
"closing inbound before receiving peer's close_notify"); |
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 |
conContext.closeInbound(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
655 |
if ((autoClose || !isLayered()) && !super.isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
656 |
super.shutdownInput(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
657 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
658 |
} |
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 |
@Override |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
661 |
public boolean isInputShutdown() { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
662 |
return conContext.isInboundClosed() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
663 |
((autoClose || !isLayered()) ? super.isInputShutdown(): true); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
664 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
665 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
666 |
// 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
|
667 |
// locks may be deadlocked. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
668 |
@Override |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
669 |
public void shutdownOutput() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
670 |
if (isOutputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
671 |
return; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
672 |
} |
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 (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
675 |
SSLLogger.fine("close outbound of SSLSocket"); |
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 |
conContext.closeOutbound(); |
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 |
if ((autoClose || !isLayered()) && !super.isOutputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
680 |
super.shutdownOutput(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
681 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
682 |
} |
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 |
@Override |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
685 |
public boolean isOutputShutdown() { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
686 |
return conContext.isOutboundClosed() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
687 |
((autoClose || !isLayered()) ? super.isOutputShutdown(): true); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
688 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
689 |
|
50768 | 690 |
@Override |
691 |
public synchronized InputStream getInputStream() throws IOException { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
692 |
if (isClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
693 |
throw new SocketException("Socket is closed"); |
50768 | 694 |
} |
695 |
||
696 |
if (!isConnected) { |
|
697 |
throw new SocketException("Socket is not connected"); |
|
698 |
} |
|
699 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
700 |
if (conContext.isInboundClosed() || isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
701 |
throw new SocketException("Socket input is already shutdown"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
702 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
703 |
|
50768 | 704 |
return appInput; |
705 |
} |
|
706 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
707 |
private void ensureNegotiated() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
708 |
if (conContext.isNegotiated || conContext.isBroken || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
709 |
conContext.isInboundClosed() || conContext.isOutboundClosed()) { |
50768 | 710 |
return; |
711 |
} |
|
712 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
713 |
synchronized (conContext) { // handshake lock |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
714 |
// double check the context status |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
715 |
if (conContext.isNegotiated || conContext.isBroken || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
716 |
conContext.isInboundClosed() || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
717 |
conContext.isOutboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
718 |
return; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
719 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
720 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
721 |
startHandshake(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
722 |
} |
2 | 723 |
} |
724 |
||
725 |
/** |
|
50768 | 726 |
* InputStream for application data as returned by |
727 |
* SSLSocket.getInputStream(). |
|
2 | 728 |
*/ |
50768 | 729 |
private class AppInputStream extends InputStream { |
730 |
// One element array used to implement the single byte read() method |
|
731 |
private final byte[] oneByte = new byte[1]; |
|
732 |
||
733 |
// the temporary buffer used to read network |
|
734 |
private ByteBuffer buffer; |
|
735 |
||
736 |
// Is application data available in the stream? |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
737 |
private volatile boolean appDataIsAvailable; |
50768 | 738 |
|
739 |
AppInputStream() { |
|
740 |
this.appDataIsAvailable = false; |
|
741 |
this.buffer = ByteBuffer.allocate(4096); |
|
742 |
} |
|
743 |
||
744 |
/** |
|
745 |
* Return the minimum number of bytes that can be read |
|
746 |
* without blocking. |
|
747 |
*/ |
|
748 |
@Override |
|
749 |
public int available() throws IOException { |
|
750 |
// Currently not synchronized. |
|
751 |
if ((!appDataIsAvailable) || checkEOF()) { |
|
752 |
return 0; |
|
753 |
} |
|
754 |
||
755 |
return buffer.remaining(); |
|
756 |
} |
|
757 |
||
758 |
/** |
|
759 |
* Read a single byte, returning -1 on non-fault EOF status. |
|
2 | 760 |
*/ |
50768 | 761 |
@Override |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
762 |
public int read() throws IOException { |
50768 | 763 |
int n = read(oneByte, 0, 1); |
764 |
if (n <= 0) { // EOF |
|
765 |
return -1; |
|
766 |
} |
|
767 |
||
768 |
return oneByte[0] & 0xFF; |
|
769 |
} |
|
2 | 770 |
|
50768 | 771 |
/** |
772 |
* Reads up to {@code len} bytes of data from the input stream |
|
773 |
* into an array of bytes. |
|
774 |
* |
|
775 |
* An attempt is made to read as many as {@code len} bytes, but a |
|
776 |
* smaller number may be read. The number of bytes actually read |
|
777 |
* is returned as an integer. |
|
778 |
* |
|
779 |
* If the layer above needs more data, it asks for more, so we |
|
780 |
* are responsible only for blocking to fill at most one buffer, |
|
781 |
* and returning "-1" on non-fault EOF status. |
|
782 |
*/ |
|
783 |
@Override |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
784 |
public int read(byte[] b, int off, int len) |
50768 | 785 |
throws IOException { |
786 |
if (b == null) { |
|
787 |
throw new NullPointerException("the target buffer is null"); |
|
788 |
} else if (off < 0 || len < 0 || len > b.length - off) { |
|
789 |
throw new IndexOutOfBoundsException( |
|
790 |
"buffer length: " + b.length + ", offset; " + off + |
|
791 |
", bytes to read:" + len); |
|
792 |
} else if (len == 0) { |
|
793 |
return 0; |
|
794 |
} |
|
795 |
||
796 |
if (checkEOF()) { |
|
797 |
return -1; |
|
798 |
} |
|
799 |
||
800 |
// 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
|
801 |
if (!conContext.isNegotiated && !conContext.isBroken && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
802 |
!conContext.isInboundClosed() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
803 |
!conContext.isOutboundClosed()) { |
50768 | 804 |
ensureNegotiated(); |
805 |
} |
|
806 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
807 |
// Check if the Socket is invalid (error or closed). |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
808 |
if (!conContext.isNegotiated || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
809 |
conContext.isBroken || conContext.isInboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
810 |
throw new SocketException("Connection or inbound has closed"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
811 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
812 |
|
50768 | 813 |
// Read the available bytes at first. |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
814 |
// |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
815 |
// 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
|
816 |
// are also synchronized with the read lock. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
817 |
synchronized (this) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
818 |
int remains = available(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
819 |
if (remains > 0) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
820 |
int howmany = Math.min(remains, len); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
821 |
buffer.get(b, off, howmany); |
30904 | 822 |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
823 |
return howmany; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
824 |
} |
50768 | 825 |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
826 |
appDataIsAvailable = false; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
827 |
try { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
828 |
ByteBuffer bb = readApplicationRecord(buffer); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
829 |
if (bb == null) { // EOF |
50768 | 830 |
return -1; |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
831 |
} else { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
832 |
// The buffer may be reallocated for bigger capacity. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
833 |
buffer = bb; |
50768 | 834 |
} |
835 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
836 |
bb.flip(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
837 |
int volume = Math.min(len, bb.remaining()); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
838 |
buffer.get(b, off, volume); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
839 |
appDataIsAvailable = true; |
50768 | 840 |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
841 |
return volume; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
842 |
} catch (Exception e) { // including RuntimeException |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
843 |
// shutdown and rethrow (wrapped) exception as appropriate |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
844 |
handleException(e); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
845 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
846 |
// dummy for compiler |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
847 |
return -1; |
50768 | 848 |
} |
849 |
} |
|
850 |
} |
|
2 | 851 |
|
50768 | 852 |
/** |
853 |
* Skip n bytes. |
|
854 |
* |
|
855 |
* This implementation is somewhat less efficient than possible, but |
|
856 |
* 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
|
857 |
* things simpler. |
50768 | 858 |
*/ |
859 |
@Override |
|
860 |
public synchronized long skip(long n) throws IOException { |
|
861 |
// dummy array used to implement skip() |
|
862 |
byte[] skipArray = new byte[256]; |
|
2 | 863 |
|
50768 | 864 |
long skipped = 0; |
865 |
while (n > 0) { |
|
866 |
int len = (int)Math.min(n, skipArray.length); |
|
867 |
int r = read(skipArray, 0, len); |
|
868 |
if (r <= 0) { |
|
869 |
break; |
|
870 |
} |
|
871 |
n -= r; |
|
872 |
skipped += r; |
|
873 |
} |
|
874 |
||
875 |
return skipped; |
|
876 |
} |
|
877 |
||
878 |
@Override |
|
879 |
public void close() throws IOException { |
|
880 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
881 |
SSLLogger.finest("Closing input stream"); |
|
882 |
} |
|
883 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
884 |
try { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
885 |
shutdownInput(false); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
886 |
} catch (IOException ioe) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
887 |
// ignore the exception |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
888 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
889 |
SSLLogger.warning("input stream close failed", ioe); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
890 |
} |
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 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
893 |
|
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 |
* Return whether we have reached end-of-file. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
896 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
897 |
* 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
|
898 |
* or has been closed, throw an Exception. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
899 |
*/ |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
900 |
private boolean checkEOF() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
901 |
if (conContext.isInboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
902 |
return true; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
903 |
} else if (conContext.isInputCloseNotified || conContext.isBroken) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
904 |
if (conContext.closeReason == null) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
905 |
return true; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
906 |
} else { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
907 |
throw new SSLException( |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
908 |
"Connection has closed: " + conContext.closeReason, |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
909 |
conContext.closeReason); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
910 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
911 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
912 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
913 |
return false; |
50768 | 914 |
} |
53067
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
915 |
|
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
916 |
/** |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
917 |
* Try the best to use up the input records so as to close the |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
918 |
* socket gracefully, without impact the performance too much. |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
919 |
*/ |
53974 | 920 |
private synchronized void deplete() { |
53067
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
921 |
if (!conContext.isInboundClosed()) { |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
922 |
if (!(conContext.inputRecord instanceof SSLSocketInputRecord)) { |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
923 |
return; |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
924 |
} |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
925 |
|
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
926 |
SSLSocketInputRecord socketInputRecord = |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
927 |
(SSLSocketInputRecord)conContext.inputRecord; |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
928 |
try { |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
929 |
socketInputRecord.deplete( |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
930 |
conContext.isNegotiated && (getSoTimeout() > 0)); |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
931 |
} catch (IOException ioe) { |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
932 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
933 |
SSLLogger.warning( |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
934 |
"input stream close depletion failed", ioe); |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
935 |
} |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
936 |
} |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
937 |
} |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
938 |
} |
2 | 939 |
} |
940 |
||
50768 | 941 |
@Override |
942 |
public synchronized OutputStream getOutputStream() throws IOException { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
943 |
if (isClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
944 |
throw new SocketException("Socket is closed"); |
50768 | 945 |
} |
2 | 946 |
|
50768 | 947 |
if (!isConnected) { |
948 |
throw new SocketException("Socket is not connected"); |
|
949 |
} |
|
950 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
951 |
if (conContext.isOutboundDone() || isOutputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
952 |
throw new SocketException("Socket output is already shutdown"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
953 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
954 |
|
50768 | 955 |
return appOutput; |
956 |
} |
|
957 |
||
958 |
||
959 |
/** |
|
960 |
* OutputStream for application data as returned by |
|
961 |
* SSLSocket.getOutputStream(). |
|
12428 | 962 |
*/ |
50768 | 963 |
private class AppOutputStream extends OutputStream { |
964 |
// One element array used to implement the write(byte) method |
|
965 |
private final byte[] oneByte = new byte[1]; |
|
966 |
||
967 |
@Override |
|
968 |
public void write(int i) throws IOException { |
|
969 |
oneByte[0] = (byte)i; |
|
970 |
write(oneByte, 0, 1); |
|
971 |
} |
|
972 |
||
973 |
@Override |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
974 |
public void write(byte[] b, |
50768 | 975 |
int off, int len) throws IOException { |
976 |
if (b == null) { |
|
977 |
throw new NullPointerException("the source buffer is null"); |
|
978 |
} else if (off < 0 || len < 0 || len > b.length - off) { |
|
979 |
throw new IndexOutOfBoundsException( |
|
980 |
"buffer length: " + b.length + ", offset; " + off + |
|
981 |
", bytes to read:" + len); |
|
982 |
} else if (len == 0) { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
983 |
// |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
984 |
// 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
|
985 |
// far to drive the handshake machinery, for correctness; not |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
986 |
// writing empty records improves performance by cutting CPU |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
987 |
// time and network resource usage. However, some protocol |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
988 |
// 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
|
989 |
// records, so this also increases robustness. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
990 |
// |
50768 | 991 |
return; |
992 |
} |
|
993 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
994 |
// Start handshaking if the connection has not been negotiated. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
995 |
if (!conContext.isNegotiated && !conContext.isBroken && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
996 |
!conContext.isInboundClosed() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
997 |
!conContext.isOutboundClosed()) { |
50768 | 998 |
ensureNegotiated(); |
999 |
} |
|
1000 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1001 |
// Check if the Socket is invalid (error or closed). |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1002 |
if (!conContext.isNegotiated || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1003 |
conContext.isBroken || conContext.isOutboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1004 |
throw new SocketException("Connection or outbound has closed"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1005 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1006 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1007 |
// |
2 | 1008 |
|
50768 | 1009 |
// Delegate the writing to the underlying socket. |
1010 |
try { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1011 |
conContext.outputRecord.deliver(b, off, len); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1012 |
} catch (SSLHandshakeException she) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1013 |
// may be record sequence number overflow |
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
53018
diff
changeset
|
1014 |
throw conContext.fatal(Alert.HANDSHAKE_FAILURE, she); |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1015 |
} catch (IOException e) { |
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
53018
diff
changeset
|
1016 |
throw conContext.fatal(Alert.UNEXPECTED_MESSAGE, e); |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1017 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1018 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1019 |
// 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
|
1020 |
// limit been reached? |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1021 |
if (conContext.outputRecord.seqNumIsHuge() || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1022 |
conContext.outputRecord.writeCipher.atKeyLimit()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1023 |
tryKeyUpdate(); |
50768 | 1024 |
} |
1025 |
} |
|
2 | 1026 |
|
50768 | 1027 |
@Override |
1028 |
public void close() throws IOException { |
|
1029 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
1030 |
SSLLogger.finest("Closing output stream"); |
|
1031 |
} |
|
1032 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1033 |
try { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1034 |
shutdownOutput(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1035 |
} catch (IOException ioe) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1036 |
// ignore the exception |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1037 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1038 |
SSLLogger.warning("output stream close failed", ioe); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1039 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1040 |
} |
50768 | 1041 |
} |
1042 |
} |
|
1043 |
||
1044 |
@Override |
|
1045 |
public synchronized SSLParameters getSSLParameters() { |
|
1046 |
return conContext.sslConfig.getSSLParameters(); |
|
1047 |
} |
|
1048 |
||
1049 |
@Override |
|
1050 |
public synchronized void setSSLParameters(SSLParameters params) { |
|
1051 |
conContext.sslConfig.setSSLParameters(params); |
|
2 | 1052 |
|
50768 | 1053 |
if (conContext.sslConfig.maximumPacketSize != 0) { |
1054 |
conContext.outputRecord.changePacketSize( |
|
1055 |
conContext.sslConfig.maximumPacketSize); |
|
1056 |
} |
|
1057 |
} |
|
1058 |
||
1059 |
@Override |
|
1060 |
public synchronized String getApplicationProtocol() { |
|
1061 |
return conContext.applicationProtocol; |
|
1062 |
} |
|
1063 |
||
1064 |
@Override |
|
1065 |
public synchronized String getHandshakeApplicationProtocol() { |
|
1066 |
if (conContext.handshakeContext != null) { |
|
1067 |
return conContext.handshakeContext.applicationProtocol; |
|
1068 |
} |
|
2 | 1069 |
|
50768 | 1070 |
return null; |
1071 |
} |
|
1072 |
||
1073 |
@Override |
|
1074 |
public synchronized void setHandshakeApplicationProtocolSelector( |
|
1075 |
BiFunction<SSLSocket, List<String>, String> selector) { |
|
1076 |
conContext.sslConfig.socketAPSelector = selector; |
|
1077 |
} |
|
2 | 1078 |
|
50768 | 1079 |
@Override |
1080 |
public synchronized BiFunction<SSLSocket, List<String>, String> |
|
1081 |
getHandshakeApplicationProtocolSelector() { |
|
1082 |
return conContext.sslConfig.socketAPSelector; |
|
1083 |
} |
|
1084 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1085 |
/** |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1086 |
* Read the initial handshake records. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1087 |
*/ |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1088 |
private int readHandshakeRecord() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1089 |
while (!conContext.isInboundClosed()) { |
50768 | 1090 |
try { |
1091 |
Plaintext plainText = decode(null); |
|
1092 |
if ((plainText.contentType == ContentType.HANDSHAKE.id) && |
|
1093 |
conContext.isNegotiated) { |
|
1094 |
return 0; |
|
1095 |
} |
|
1096 |
} catch (SSLException ssle) { |
|
1097 |
throw ssle; |
|
1098 |
} catch (IOException ioe) { |
|
1099 |
if (!(ioe instanceof SSLException)) { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1100 |
throw new SSLException("readHandshakeRecord", ioe); |
50768 | 1101 |
} else { |
1102 |
throw ioe; |
|
1103 |
} |
|
1104 |
} |
|
2 | 1105 |
} |
30904 | 1106 |
|
50768 | 1107 |
return -1; |
30904 | 1108 |
} |
1109 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1110 |
/** |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1111 |
* 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
|
1112 |
* here so as to use the socket level synchronization. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1113 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1114 |
* 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
|
1115 |
* 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
|
1116 |
* them here. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1117 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1118 |
* 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
|
1119 |
* method other than in the AppInputStream class! |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1120 |
*/ |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1121 |
private ByteBuffer readApplicationRecord( |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1122 |
ByteBuffer buffer) throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1123 |
while (!conContext.isInboundClosed()) { |
50768 | 1124 |
/* |
1125 |
* clean the buffer and check if it is too small, e.g. because |
|
1126 |
* the AppInputStream did not have the chance to see the |
|
1127 |
* current packet length but rather something like that of the |
|
1128 |
* handshake before. In that case we return 0 at this point to |
|
1129 |
* give the caller the chance to adjust the buffer. |
|
1130 |
*/ |
|
1131 |
buffer.clear(); |
|
1132 |
int inLen = conContext.inputRecord.bytesInCompletePacket(); |
|
1133 |
if (inLen < 0) { // EOF |
|
1134 |
handleEOF(null); |
|
2 | 1135 |
|
50768 | 1136 |
// if no exception thrown |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1137 |
return null; |
50768 | 1138 |
} |
30904 | 1139 |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1140 |
// Is this packet bigger than SSL/TLS normally allows? |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1141 |
if (inLen > SSLRecord.maxLargeRecordSize) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1142 |
throw new SSLProtocolException( |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1143 |
"Illegal packet size: " + inLen); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1144 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1145 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1146 |
if (inLen > buffer.remaining()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1147 |
buffer = ByteBuffer.allocate(inLen); |
50768 | 1148 |
} |
7039 | 1149 |
|
50768 | 1150 |
try { |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1151 |
Plaintext plainText; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1152 |
synchronized (this) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1153 |
plainText = decode(buffer); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1154 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1155 |
if (plainText.contentType == ContentType.APPLICATION_DATA.id && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1156 |
buffer.position() > 0) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1157 |
return buffer; |
30904 | 1158 |
} |
50768 | 1159 |
} catch (SSLException ssle) { |
1160 |
throw ssle; |
|
1161 |
} catch (IOException ioe) { |
|
1162 |
if (!(ioe instanceof SSLException)) { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1163 |
throw new SSLException("readApplicationRecord", ioe); |
50768 | 1164 |
} else { |
1165 |
throw ioe; |
|
30904 | 1166 |
} |
50768 | 1167 |
} |
7039 | 1168 |
} |
1169 |
||
50768 | 1170 |
// |
1171 |
// couldn't read, due to some kind of error |
|
1172 |
// |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1173 |
return null; |
50768 | 1174 |
} |
1175 |
||
1176 |
private Plaintext decode(ByteBuffer destination) throws IOException { |
|
1177 |
Plaintext plainText; |
|
1178 |
try { |
|
1179 |
if (destination == null) { |
|
1180 |
plainText = SSLTransport.decode(conContext, |
|
1181 |
null, 0, 0, null, 0, 0); |
|
1182 |
} else { |
|
1183 |
plainText = SSLTransport.decode(conContext, |
|
1184 |
null, 0, 0, new ByteBuffer[]{destination}, 0, 1); |
|
7039 | 1185 |
} |
50768 | 1186 |
} catch (EOFException eofe) { |
1187 |
// EOFException is special as it is related to close_notify. |
|
1188 |
plainText = handleEOF(eofe); |
|
7039 | 1189 |
} |
30904 | 1190 |
|
50768 | 1191 |
// Is the sequence number is nearly overflow? |
1192 |
if (plainText != Plaintext.PLAINTEXT_NULL && |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1193 |
(conContext.inputRecord.seqNumIsHuge() || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1194 |
conContext.inputRecord.readCipher.atKeyLimit())) { |
50768 | 1195 |
tryKeyUpdate(); |
1196 |
} |
|
30904 | 1197 |
|
50768 | 1198 |
return plainText; |
2 | 1199 |
} |
1200 |
||
1201 |
/** |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1202 |
* Try key update for sequence number wrap or key usage limit. |
50768 | 1203 |
* |
1204 |
* 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
|
1205 |
* 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
|
1206 |
* reading/writing process. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1207 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1208 |
* As we request renegotiation or close the connection for wrapped sequence |
50768 | 1209 |
* number when there is enough sequence number space left to handle a few |
1210 |
* more records, so the sequence number of the last record cannot be |
|
1211 |
* wrapped. |
|
2 | 1212 |
*/ |
50768 | 1213 |
private void tryKeyUpdate() throws IOException { |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1214 |
// 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
|
1215 |
// connection is not duplex-open. |
50768 | 1216 |
if ((conContext.handshakeContext == null) && |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1217 |
!conContext.isOutboundClosed() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1218 |
!conContext.isInboundClosed() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1219 |
!conContext.isBroken) { |
50768 | 1220 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1221 |
SSLLogger.finest("trigger key update"); |
50768 | 1222 |
} |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1223 |
startHandshake(); |
2 | 1224 |
} |
1225 |
} |
|
1226 |
||
1227 |
/** |
|
50768 | 1228 |
* Initialize the handshaker and socket streams. |
2 | 1229 |
* |
50768 | 1230 |
* Called by connect, the layered constructor, and SSLServerSocket. |
2 | 1231 |
*/ |
50768 | 1232 |
synchronized void doneConnect() throws IOException { |
1233 |
// In server mode, it is not necessary to set host and serverNames. |
|
1234 |
// Otherwise, would require a reverse DNS lookup to get the hostname. |
|
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52427
diff
changeset
|
1235 |
if (peerHost == null || peerHost.isEmpty()) { |
50768 | 1236 |
boolean useNameService = |
1237 |
trustNameService && conContext.sslConfig.isClientMode; |
|
1238 |
useImplicitHost(useNameService); |
|
1239 |
} else { |
|
1240 |
conContext.sslConfig.serverNames = |
|
1241 |
Utilities.addToSNIServerNameList( |
|
1242 |
conContext.sslConfig.serverNames, peerHost); |
|
2 | 1243 |
} |
1244 |
||
50768 | 1245 |
InputStream sockInput = super.getInputStream(); |
1246 |
conContext.inputRecord.setReceiverStream(sockInput); |
|
2 | 1247 |
|
50768 | 1248 |
OutputStream sockOutput = super.getOutputStream(); |
1249 |
conContext.inputRecord.setDeliverStream(sockOutput); |
|
1250 |
conContext.outputRecord.setDeliverStream(sockOutput); |
|
2 | 1251 |
|
50768 | 1252 |
this.isConnected = true; |
2 | 1253 |
} |
1254 |
||
50768 | 1255 |
private void useImplicitHost(boolean useNameService) { |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1256 |
// Note: If the local name service is not trustworthy, reverse |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1257 |
// host name resolution should not be performed for endpoint |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1258 |
// identification. Use the application original specified |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1259 |
// hostname or IP address instead. |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1260 |
|
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
51986
diff
changeset
|
1261 |
// Get the original hostname via jdk.internal.access.SharedSecrets |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1262 |
InetAddress inetAddress = getInetAddress(); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1263 |
if (inetAddress == null) { // not connected |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1264 |
return; |
31687 | 1265 |
} |
1266 |
||
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1267 |
JavaNetInetAddressAccess jna = |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1268 |
SharedSecrets.getJavaNetInetAddressAccess(); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1269 |
String originalHostname = jna.getOriginalHostName(inetAddress); |
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52427
diff
changeset
|
1270 |
if (originalHostname != null && !originalHostname.isEmpty()) { |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1271 |
|
50768 | 1272 |
this.peerHost = originalHostname; |
1273 |
if (conContext.sslConfig.serverNames.isEmpty() && |
|
1274 |
!conContext.sslConfig.noSniExtension) { |
|
1275 |
conContext.sslConfig.serverNames = |
|
1276 |
Utilities.addToSNIServerNameList( |
|
1277 |
conContext.sslConfig.serverNames, peerHost); |
|
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1278 |
} |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1279 |
|
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1280 |
return; |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1281 |
} |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1282 |
|
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1283 |
// No explicitly specified hostname, no server name indication. |
50768 | 1284 |
if (!useNameService) { |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1285 |
// The local name service is not trustworthy, use IP address. |
50768 | 1286 |
this.peerHost = inetAddress.getHostAddress(); |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1287 |
} else { |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1288 |
// Use the underlying reverse host name resolution service. |
50768 | 1289 |
this.peerHost = getInetAddress().getHostName(); |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1290 |
} |
31687 | 1291 |
} |
1292 |
||
5162
0dbedf4fdb8c
6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents:
2068
diff
changeset
|
1293 |
// 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
|
1294 |
// |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1295 |
// 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
|
1296 |
// SSLSocket.setSSLParameters(). Otherwise, the {@code host} parameter |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1297 |
// may override SNIHostName in the customized server name indication. |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
1298 |
public synchronized void setHost(String host) { |
50768 | 1299 |
this.peerHost = host; |
1300 |
this.conContext.sslConfig.serverNames = |
|
1301 |
Utilities.addToSNIServerNameList( |
|
1302 |
conContext.sslConfig.serverNames, host); |
|
2 | 1303 |
} |
1304 |
||
1305 |
/** |
|
50768 | 1306 |
* Handle an exception. |
1307 |
* |
|
1308 |
* This method is called by top level exception handlers (in read(), |
|
1309 |
* write()) to make sure we always shutdown the connection correctly |
|
1310 |
* and do not pass runtime exception to the application. |
|
2 | 1311 |
* |
50768 | 1312 |
* This method never returns normally, it always throws an IOException. |
2 | 1313 |
*/ |
50768 | 1314 |
private void handleException(Exception cause) throws IOException { |
1315 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
1316 |
SSLLogger.warning("handling exception", cause); |
|
1317 |
} |
|
1318 |
||
1319 |
// Don't close the Socket in case of timeouts or interrupts. |
|
1320 |
if (cause instanceof InterruptedIOException) { |
|
1321 |
throw (IOException)cause; |
|
1322 |
} |
|
2 | 1323 |
|
50768 | 1324 |
// need to perform error shutdown |
1325 |
boolean isSSLException = (cause instanceof SSLException); |
|
1326 |
Alert alert; |
|
1327 |
if (isSSLException) { |
|
1328 |
if (cause instanceof SSLHandshakeException) { |
|
1329 |
alert = Alert.HANDSHAKE_FAILURE; |
|
1330 |
} else { |
|
1331 |
alert = Alert.UNEXPECTED_MESSAGE; |
|
1332 |
} |
|
1333 |
} else { |
|
1334 |
if (cause instanceof IOException) { |
|
1335 |
alert = Alert.UNEXPECTED_MESSAGE; |
|
1336 |
} else { |
|
1337 |
// RuntimeException |
|
1338 |
alert = Alert.INTERNAL_ERROR; |
|
1339 |
} |
|
2 | 1340 |
} |
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
53018
diff
changeset
|
1341 |
|
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
53018
diff
changeset
|
1342 |
throw conContext.fatal(alert, cause); |
2 | 1343 |
} |
1344 |
||
50768 | 1345 |
private Plaintext handleEOF(EOFException eofe) throws IOException { |
1346 |
if (requireCloseNotify || conContext.handshakeContext != null) { |
|
1347 |
SSLException ssle; |
|
1348 |
if (conContext.handshakeContext != null) { |
|
1349 |
ssle = new SSLHandshakeException( |
|
1350 |
"Remote host terminated the handshake"); |
|
1351 |
} else { |
|
1352 |
ssle = new SSLProtocolException( |
|
1353 |
"Remote host terminated the connection"); |
|
1354 |
} |
|
2 | 1355 |
|
50768 | 1356 |
if (eofe != null) { |
1357 |
ssle.initCause(eofe); |
|
1358 |
} |
|
1359 |
throw ssle; |
|
1360 |
} else { |
|
1361 |
// treat as if we had received a close_notify |
|
1362 |
conContext.isInputCloseNotified = true; |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1363 |
shutdownInput(); |
2 | 1364 |
|
50768 | 1365 |
return Plaintext.PLAINTEXT_NULL; |
2 | 1366 |
} |
1367 |
} |
|
1368 |
||
1369 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1370 |
@Override |
50768 | 1371 |
public String getPeerHost() { |
1372 |
return peerHost; |
|
2 | 1373 |
} |
1374 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1375 |
@Override |
50768 | 1376 |
public int getPeerPort() { |
1377 |
return getPort(); |
|
2 | 1378 |
} |
1379 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1380 |
@Override |
50768 | 1381 |
public boolean useDelegatedTask() { |
1382 |
return false; |
|
2 | 1383 |
} |
1384 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1385 |
@Override |
50768 | 1386 |
public void shutdown() throws IOException { |
1387 |
if (!isClosed()) { |
|
1388 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
1389 |
SSLLogger.fine("close the underlying socket"); |
|
1390 |
} |
|
2 | 1391 |
|
50768 | 1392 |
try { |
1393 |
if (conContext.isInputCloseNotified) { |
|
1394 |
// Close the connection, no wait for more peer response. |
|
1395 |
closeSocket(false); |
|
1396 |
} else { |
|
1397 |
// Close the connection, may wait for peer close_notify. |
|
1398 |
closeSocket(true); |
|
1399 |
} |
|
1400 |
} finally { |
|
1401 |
tlsIsClosed = true; |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1402 |
} |
7043 | 1403 |
} |
2 | 1404 |
} |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1405 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1406 |
private void closeSocket(boolean selfInitiated) throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1407 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1408 |
SSLLogger.fine("close the SSL connection " + |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1409 |
(selfInitiated ? "(initiative)" : "(passive)")); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1410 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1411 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1412 |
if (autoClose || !isLayered()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1413 |
super.close(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1414 |
} else if (selfInitiated) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1415 |
if (!conContext.isInboundClosed() && !isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1416 |
// wait for close_notify alert to clear input stream. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1417 |
waitForClose(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1418 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1419 |
} |
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 |
/** |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1423 |
* Wait for close_notify alert for a graceful closure. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1424 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1425 |
* [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
|
1426 |
* 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
|
1427 |
* 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
|
1428 |
* 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
|
1429 |
* 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
|
1430 |
* 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
|
1431 |
* 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
|
1432 |
* transport without waiting for the responding close_notify. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1433 |
*/ |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1434 |
private void waitForClose() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1435 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1436 |
SSLLogger.fine("wait for close_notify or alert"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1437 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1438 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1439 |
while (!conContext.isInboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1440 |
try { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1441 |
Plaintext plainText = decode(null); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1442 |
// discard and continue |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1443 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1444 |
SSLLogger.finest( |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1445 |
"discard plaintext while waiting for close", plainText); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1446 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1447 |
} catch (Exception e) { // including RuntimeException |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1448 |
handleException(e); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1449 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1450 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1451 |
} |
2 | 1452 |
} |