author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 57485 | af4b0fc25bc4 |
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; |
|
55411
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
41 |
import java.util.concurrent.TimeUnit; |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
42 |
import java.util.concurrent.locks.ReentrantLock; |
42706
796cf076d69b
8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents:
38865
diff
changeset
|
43 |
import java.util.function.BiFunction; |
50768 | 44 |
import javax.net.ssl.HandshakeCompletedListener; |
45 |
import javax.net.ssl.SSLException; |
|
46 |
import javax.net.ssl.SSLHandshakeException; |
|
47 |
import javax.net.ssl.SSLParameters; |
|
48 |
import javax.net.ssl.SSLProtocolException; |
|
49 |
import javax.net.ssl.SSLServerSocket; |
|
50 |
import javax.net.ssl.SSLSession; |
|
51 |
import javax.net.ssl.SSLSocket; |
|
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
51986
diff
changeset
|
52 |
import jdk.internal.access.JavaNetInetAddressAccess; |
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
51986
diff
changeset
|
53 |
import jdk.internal.access.SharedSecrets; |
31687 | 54 |
|
2 | 55 |
/** |
50768 | 56 |
* Implementation of an SSL socket. |
57 |
* <P> |
|
58 |
* This is a normal connection type socket, implementing SSL over some lower |
|
59 |
* level socket, such as TCP. Because it is layered over some lower level |
|
60 |
* socket, it MUST override all default socket methods. |
|
61 |
* <P> |
|
62 |
* This API offers a non-traditional option for establishing SSL |
|
2 | 63 |
* connections. You may first establish the connection directly, then pass |
64 |
* that connection to the SSL socket constructor with a flag saying which |
|
65 |
* role should be taken in the handshake protocol. (The two ends of the |
|
66 |
* connection must not choose the same role!) This allows setup of SSL |
|
67 |
* proxying or tunneling, and also allows the kind of "role reversal" |
|
68 |
* that is required for most FTP data transfers. |
|
69 |
* |
|
70 |
* @see javax.net.ssl.SSLSocket |
|
71 |
* @see SSLServerSocket |
|
72 |
* |
|
73 |
* @author David Brownell |
|
74 |
*/ |
|
50768 | 75 |
public final class SSLSocketImpl |
76 |
extends BaseSSLSocketImpl implements SSLTransport { |
|
2 | 77 |
|
50768 | 78 |
final SSLContextImpl sslContext; |
79 |
final TransportContext conContext; |
|
34380
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
80 |
|
50768 | 81 |
private final AppInputStream appInput = new AppInputStream(); |
82 |
private final AppOutputStream appOutput = new AppOutputStream(); |
|
2 | 83 |
|
50768 | 84 |
private String peerHost; |
85 |
private boolean autoClose; |
|
86 |
private boolean isConnected = false; |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
87 |
private volatile boolean tlsIsClosed = false; |
30904 | 88 |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
89 |
private final ReentrantLock socketLock = new ReentrantLock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
90 |
private final ReentrantLock handshakeLock = new ReentrantLock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
91 |
|
31687 | 92 |
/* |
93 |
* Is the local name service trustworthy? |
|
94 |
* |
|
95 |
* If the local name service is not trustworthy, reverse host name |
|
96 |
* resolution should not be performed for endpoint identification. |
|
97 |
*/ |
|
50768 | 98 |
private static final boolean trustNameService = |
99 |
Utilities.getBooleanProperty("jdk.tls.trustNameService", false); |
|
31687 | 100 |
|
50768 | 101 |
/** |
102 |
* Package-private constructor used to instantiate an unconnected |
|
103 |
* socket. |
|
104 |
* |
|
105 |
* This instance is meant to set handshake state to use "client mode". |
|
106 |
*/ |
|
107 |
SSLSocketImpl(SSLContextImpl sslContext) { |
|
108 |
super(); |
|
109 |
this.sslContext = sslContext; |
|
110 |
HandshakeHash handshakeHash = new HandshakeHash(); |
|
111 |
this.conContext = new TransportContext(sslContext, this, |
|
112 |
new SSLSocketInputRecord(handshakeHash), |
|
113 |
new SSLSocketOutputRecord(handshakeHash), true); |
|
114 |
} |
|
2 | 115 |
|
116 |
/** |
|
50768 | 117 |
* Package-private constructor used to instantiate a server socket. |
2 | 118 |
* |
50768 | 119 |
* This instance is meant to set handshake state to use "server mode". |
2 | 120 |
*/ |
50768 | 121 |
SSLSocketImpl(SSLContextImpl sslContext, SSLConfiguration sslConfig) { |
2 | 122 |
super(); |
50768 | 123 |
this.sslContext = sslContext; |
124 |
HandshakeHash handshakeHash = new HandshakeHash(); |
|
125 |
this.conContext = new TransportContext(sslContext, this, sslConfig, |
|
126 |
new SSLSocketInputRecord(handshakeHash), |
|
127 |
new SSLSocketOutputRecord(handshakeHash)); |
|
2 | 128 |
} |
129 |
||
130 |
/** |
|
50768 | 131 |
* Constructs an SSL connection to a named host at a specified |
132 |
* port, using the authentication context provided. |
|
2 | 133 |
* |
50768 | 134 |
* This endpoint acts as the client, and may rejoin an existing SSL session |
135 |
* if appropriate. |
|
2 | 136 |
*/ |
50768 | 137 |
SSLSocketImpl(SSLContextImpl sslContext, String peerHost, |
138 |
int peerPort) throws IOException, UnknownHostException { |
|
2 | 139 |
super(); |
50768 | 140 |
this.sslContext = sslContext; |
141 |
HandshakeHash handshakeHash = new HandshakeHash(); |
|
142 |
this.conContext = new TransportContext(sslContext, this, |
|
143 |
new SSLSocketInputRecord(handshakeHash), |
|
144 |
new SSLSocketOutputRecord(handshakeHash), true); |
|
145 |
this.peerHost = peerHost; |
|
146 |
SocketAddress socketAddress = |
|
147 |
peerHost != null ? new InetSocketAddress(peerHost, peerPort) : |
|
148 |
new InetSocketAddress(InetAddress.getByName(null), peerPort); |
|
2 | 149 |
connect(socketAddress, 0); |
150 |
} |
|
151 |
||
152 |
/** |
|
50768 | 153 |
* Constructs an SSL connection to a server at a specified |
154 |
* address, and TCP port, using the authentication context |
|
155 |
* provided. |
|
2 | 156 |
* |
50768 | 157 |
* This endpoint acts as the client, and may rejoin an existing SSL |
158 |
* session if appropriate. |
|
2 | 159 |
*/ |
50768 | 160 |
SSLSocketImpl(SSLContextImpl sslContext, |
161 |
InetAddress address, int peerPort) throws IOException { |
|
2 | 162 |
super(); |
50768 | 163 |
this.sslContext = sslContext; |
164 |
HandshakeHash handshakeHash = new HandshakeHash(); |
|
165 |
this.conContext = new TransportContext(sslContext, this, |
|
166 |
new SSLSocketInputRecord(handshakeHash), |
|
167 |
new SSLSocketOutputRecord(handshakeHash), true); |
|
2 | 168 |
|
50768 | 169 |
SocketAddress socketAddress = new InetSocketAddress(address, peerPort); |
2 | 170 |
connect(socketAddress, 0); |
171 |
} |
|
172 |
||
50768 | 173 |
/** |
174 |
* Constructs an SSL connection to a named host at a specified |
|
175 |
* port, using the authentication context provided. |
|
176 |
* |
|
177 |
* This endpoint acts as the client, and may rejoin an existing SSL |
|
178 |
* session if appropriate. |
|
2 | 179 |
*/ |
50768 | 180 |
SSLSocketImpl(SSLContextImpl sslContext, |
181 |
String peerHost, int peerPort, InetAddress localAddr, |
|
182 |
int localPort) throws IOException, UnknownHostException { |
|
2 | 183 |
super(); |
50768 | 184 |
this.sslContext = sslContext; |
185 |
HandshakeHash handshakeHash = new HandshakeHash(); |
|
186 |
this.conContext = new TransportContext(sslContext, this, |
|
187 |
new SSLSocketInputRecord(handshakeHash), |
|
188 |
new SSLSocketOutputRecord(handshakeHash), true); |
|
189 |
this.peerHost = peerHost; |
|
2 | 190 |
|
50768 | 191 |
bind(new InetSocketAddress(localAddr, localPort)); |
192 |
SocketAddress socketAddress = |
|
193 |
peerHost != null ? new InetSocketAddress(peerHost, peerPort) : |
|
194 |
new InetSocketAddress(InetAddress.getByName(null), peerPort); |
|
195 |
connect(socketAddress, 0); |
|
2 | 196 |
} |
197 |
||
198 |
/** |
|
50768 | 199 |
* Constructs an SSL connection to a server at a specified |
200 |
* address, and TCP port, using the authentication context |
|
201 |
* provided. |
|
2 | 202 |
* |
50768 | 203 |
* This endpoint acts as the client, and may rejoin an existing SSL |
204 |
* session if appropriate. |
|
2 | 205 |
*/ |
50768 | 206 |
SSLSocketImpl(SSLContextImpl sslContext, |
207 |
InetAddress peerAddr, int peerPort, |
|
208 |
InetAddress localAddr, int localPort) throws IOException { |
|
209 |
super(); |
|
210 |
this.sslContext = sslContext; |
|
211 |
HandshakeHash handshakeHash = new HandshakeHash(); |
|
212 |
this.conContext = new TransportContext(sslContext, this, |
|
213 |
new SSLSocketInputRecord(handshakeHash), |
|
214 |
new SSLSocketOutputRecord(handshakeHash), true); |
|
215 |
||
216 |
bind(new InetSocketAddress(localAddr, localPort)); |
|
217 |
SocketAddress socketAddress = new InetSocketAddress(peerAddr, peerPort); |
|
218 |
connect(socketAddress, 0); |
|
2 | 219 |
} |
220 |
||
221 |
/** |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
222 |
* 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
|
223 |
* 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
|
224 |
* 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
|
225 |
* underlying {@link InputStream}. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
226 |
*/ |
50768 | 227 |
SSLSocketImpl(SSLContextImpl sslContext, Socket sock, |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
228 |
InputStream consumed, boolean autoClose) throws IOException { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
229 |
super(sock, consumed); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
230 |
// We always layer over a connected socket |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
231 |
if (!sock.isConnected()) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
232 |
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
|
233 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
234 |
|
50768 | 235 |
this.sslContext = sslContext; |
236 |
HandshakeHash handshakeHash = new HandshakeHash(); |
|
237 |
this.conContext = new TransportContext(sslContext, this, |
|
238 |
new SSLSocketInputRecord(handshakeHash), |
|
239 |
new SSLSocketOutputRecord(handshakeHash), false); |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
240 |
this.autoClose = autoClose; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
241 |
doneConnect(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
242 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
243 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
244 |
/** |
50768 | 245 |
* Layer SSL traffic over an existing connection, rather than |
246 |
* creating a new connection. |
|
247 |
* |
|
248 |
* The existing connection may be used only for SSL traffic (using this |
|
249 |
* SSLSocket) until the SSLSocket.close() call returns. However, if a |
|
250 |
* protocol error is detected, that existing connection is automatically |
|
251 |
* closed. |
|
252 |
* <p> |
|
253 |
* This particular constructor always uses the socket in the |
|
254 |
* role of an SSL client. It may be useful in cases which start |
|
255 |
* using SSL after some initial data transfers, for example in some |
|
256 |
* SSL tunneling applications or as part of some kinds of application |
|
257 |
* protocols which negotiate use of a SSL based security. |
|
2 | 258 |
*/ |
50768 | 259 |
SSLSocketImpl(SSLContextImpl sslContext, Socket sock, |
260 |
String peerHost, int port, boolean autoClose) throws IOException { |
|
261 |
super(sock); |
|
262 |
// We always layer over a connected socket |
|
263 |
if (!sock.isConnected()) { |
|
264 |
throw new SocketException("Underlying socket is not connected"); |
|
265 |
} |
|
2 | 266 |
|
50768 | 267 |
this.sslContext = sslContext; |
268 |
HandshakeHash handshakeHash = new HandshakeHash(); |
|
269 |
this.conContext = new TransportContext(sslContext, this, |
|
270 |
new SSLSocketInputRecord(handshakeHash), |
|
271 |
new SSLSocketOutputRecord(handshakeHash), true); |
|
272 |
this.peerHost = peerHost; |
|
273 |
this.autoClose = autoClose; |
|
274 |
doneConnect(); |
|
2 | 275 |
} |
276 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
277 |
@Override |
50768 | 278 |
public void connect(SocketAddress endpoint, |
279 |
int timeout) throws IOException { |
|
2 | 280 |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
281 |
if (isLayered()) { |
2 | 282 |
throw new SocketException("Already connected"); |
283 |
} |
|
284 |
||
285 |
if (!(endpoint instanceof InetSocketAddress)) { |
|
286 |
throw new SocketException( |
|
50768 | 287 |
"Cannot handle non-Inet socket addresses."); |
2 | 288 |
} |
289 |
||
290 |
super.connect(endpoint, timeout); |
|
50768 | 291 |
doneConnect(); |
292 |
} |
|
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
293 |
|
50768 | 294 |
@Override |
295 |
public String[] getSupportedCipherSuites() { |
|
296 |
return CipherSuite.namesOf(sslContext.getSupportedCipherSuites()); |
|
297 |
} |
|
298 |
||
299 |
@Override |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
300 |
public String[] getEnabledCipherSuites() { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
301 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
302 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
303 |
return CipherSuite.namesOf( |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
304 |
conContext.sslConfig.enabledCipherSuites); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
305 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
306 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
307 |
} |
50768 | 308 |
} |
309 |
||
310 |
@Override |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
311 |
public void setEnabledCipherSuites(String[] suites) { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
312 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
313 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
314 |
conContext.sslConfig.enabledCipherSuites = |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
315 |
CipherSuite.validValuesOf(suites); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
316 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
317 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
318 |
} |
50768 | 319 |
} |
320 |
||
321 |
@Override |
|
322 |
public String[] getSupportedProtocols() { |
|
323 |
return ProtocolVersion.toStringArray( |
|
324 |
sslContext.getSupportedProtocolVersions()); |
|
325 |
} |
|
326 |
||
327 |
@Override |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
328 |
public String[] getEnabledProtocols() { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
329 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
330 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
331 |
return ProtocolVersion.toStringArray( |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
332 |
conContext.sslConfig.enabledProtocols); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
333 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
334 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
335 |
} |
50768 | 336 |
} |
337 |
||
338 |
@Override |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
339 |
public void setEnabledProtocols(String[] protocols) { |
50768 | 340 |
if (protocols == null) { |
341 |
throw new IllegalArgumentException("Protocols cannot be null"); |
|
342 |
} |
|
343 |
||
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
344 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
345 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
346 |
conContext.sslConfig.enabledProtocols = |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
347 |
ProtocolVersion.namesOf(protocols); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
348 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
349 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
350 |
} |
50768 | 351 |
} |
352 |
||
353 |
@Override |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
354 |
public SSLSession getSession() { |
50768 | 355 |
try { |
356 |
// start handshaking, if failed, the connection will be closed. |
|
357 |
ensureNegotiated(); |
|
358 |
} catch (IOException ioe) { |
|
359 |
if (SSLLogger.isOn && SSLLogger.isOn("handshake")) { |
|
360 |
SSLLogger.severe("handshake failed", ioe); |
|
361 |
} |
|
362 |
||
363 |
return SSLSessionImpl.nullSession; |
|
364 |
} |
|
365 |
||
366 |
return conContext.conSession; |
|
367 |
} |
|
368 |
||
369 |
@Override |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
370 |
public SSLSession getHandshakeSession() { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
371 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
372 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
373 |
return conContext.handshakeContext == null ? |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
374 |
null : conContext.handshakeContext.handshakeSession; |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
375 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
376 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
377 |
} |
50768 | 378 |
} |
379 |
||
380 |
@Override |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
381 |
public void addHandshakeCompletedListener( |
50768 | 382 |
HandshakeCompletedListener listener) { |
383 |
if (listener == null) { |
|
384 |
throw new IllegalArgumentException("listener is null"); |
|
385 |
} |
|
386 |
||
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
387 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
388 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
389 |
conContext.sslConfig.addHandshakeCompletedListener(listener); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
390 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
391 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
392 |
} |
50768 | 393 |
} |
394 |
||
395 |
@Override |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
396 |
public void removeHandshakeCompletedListener( |
50768 | 397 |
HandshakeCompletedListener listener) { |
398 |
if (listener == null) { |
|
399 |
throw new IllegalArgumentException("listener is null"); |
|
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
400 |
} |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
401 |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
402 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
403 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
404 |
conContext.sslConfig.removeHandshakeCompletedListener(listener); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
405 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
406 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
407 |
} |
50768 | 408 |
} |
409 |
||
410 |
@Override |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
411 |
public void startHandshake() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
412 |
if (!isConnected) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
413 |
throw new SocketException("Socket is not connected"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
414 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
415 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
416 |
if (conContext.isBroken || conContext.isInboundClosed() || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
417 |
conContext.isOutboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
418 |
throw new SocketException("Socket has been closed or broken"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
419 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
420 |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
421 |
handshakeLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
422 |
try { |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
423 |
// double check the context status |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
424 |
if (conContext.isBroken || conContext.isInboundClosed() || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
425 |
conContext.isOutboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
426 |
throw new SocketException("Socket has been closed or broken"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
427 |
} |
50768 | 428 |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
429 |
try { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
430 |
conContext.kickstart(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
431 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
432 |
// All initial handshaking goes through this operation until we |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
433 |
// have a valid SSL connection. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
434 |
// |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
435 |
// Handle handshake messages only, need no application data. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
436 |
if (!conContext.isNegotiated) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
437 |
readHandshakeRecord(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
438 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
439 |
} catch (IOException ioe) { |
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
53018
diff
changeset
|
440 |
throw conContext.fatal(Alert.HANDSHAKE_FAILURE, |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
441 |
"Couldn't kickstart handshaking", ioe); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
442 |
} catch (Exception oe) { // including RuntimeException |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
443 |
handleException(oe); |
50768 | 444 |
} |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
445 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
446 |
handshakeLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
447 |
} |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
448 |
} |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
449 |
|
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
450 |
@Override |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
451 |
public void setUseClientMode(boolean mode) { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
452 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
453 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
454 |
conContext.setUseClientMode(mode); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
455 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
456 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
457 |
} |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
458 |
} |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
459 |
|
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
460 |
@Override |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
461 |
public boolean getUseClientMode() { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
462 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
463 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
464 |
return conContext.sslConfig.isClientMode; |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
465 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
466 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
467 |
} |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
468 |
} |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
469 |
|
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
470 |
@Override |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
471 |
public void setNeedClientAuth(boolean need) { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
472 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
473 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
474 |
conContext.sslConfig.clientAuthType = |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
475 |
(need ? ClientAuthType.CLIENT_AUTH_REQUIRED : |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
476 |
ClientAuthType.CLIENT_AUTH_NONE); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
477 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
478 |
socketLock.unlock(); |
50768 | 479 |
} |
480 |
} |
|
481 |
||
482 |
@Override |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
483 |
public boolean getNeedClientAuth() { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
484 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
485 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
486 |
return (conContext.sslConfig.clientAuthType == |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
487 |
ClientAuthType.CLIENT_AUTH_REQUIRED); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
488 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
489 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
490 |
} |
50768 | 491 |
} |
492 |
||
493 |
@Override |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
494 |
public void setWantClientAuth(boolean want) { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
495 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
496 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
497 |
conContext.sslConfig.clientAuthType = |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
498 |
(want ? ClientAuthType.CLIENT_AUTH_REQUESTED : |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
499 |
ClientAuthType.CLIENT_AUTH_NONE); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
500 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
501 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
502 |
} |
50768 | 503 |
} |
504 |
||
505 |
@Override |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
506 |
public boolean getWantClientAuth() { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
507 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
508 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
509 |
return (conContext.sslConfig.clientAuthType == |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
510 |
ClientAuthType.CLIENT_AUTH_REQUESTED); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
511 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
512 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
513 |
} |
50768 | 514 |
} |
515 |
||
516 |
@Override |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
517 |
public void setEnableSessionCreation(boolean flag) { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
518 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
519 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
520 |
conContext.sslConfig.enableSessionCreation = flag; |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
521 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
522 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
523 |
} |
50768 | 524 |
} |
525 |
||
526 |
@Override |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
527 |
public boolean getEnableSessionCreation() { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
528 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
529 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
530 |
return conContext.sslConfig.enableSessionCreation; |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
531 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
532 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
533 |
} |
50768 | 534 |
} |
535 |
||
536 |
@Override |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
537 |
public boolean isClosed() { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
538 |
return tlsIsClosed; |
50768 | 539 |
} |
540 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
541 |
// 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
|
542 |
// locks may be deadlocked. |
50768 | 543 |
@Override |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
544 |
public void close() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
545 |
if (tlsIsClosed) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
546 |
return; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
547 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
548 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
549 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
550 |
SSLLogger.fine("duplex close of SSLSocket"); |
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 |
|
50768 | 553 |
try { |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
554 |
// shutdown output bound, which may have been closed previously. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
555 |
if (!isOutputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
556 |
duplexCloseOutput(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
557 |
} |
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 |
// shutdown input bound, which may have been closed previously. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
560 |
if (!isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
561 |
duplexCloseInput(); |
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 |
if (!isClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
565 |
// close the connection directly |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
566 |
closeSocket(false); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
567 |
} |
50768 | 568 |
} catch (IOException ioe) { |
569 |
// ignore the exception |
|
570 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
571 |
SSLLogger.warning("SSLSocket duplex close failed", ioe); |
50768 | 572 |
} |
573 |
} finally { |
|
574 |
tlsIsClosed = true; |
|
575 |
} |
|
576 |
} |
|
577 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
578 |
/** |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
579 |
* Duplex close, start from closing outbound. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
580 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
581 |
* 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
|
582 |
* 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
|
583 |
* 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
|
584 |
* 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
|
585 |
* the connection immediately, discarding any pending writes. It is |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
586 |
* 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
|
587 |
* 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
|
588 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
589 |
* 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
|
590 |
* 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
|
591 |
* 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
|
592 |
* 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
|
593 |
* 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
|
594 |
* so would introduce the possibility of truncation. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
595 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
596 |
* 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
|
597 |
* 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
|
598 |
*/ |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
599 |
private void duplexCloseOutput() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
600 |
boolean useUserCanceled = false; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
601 |
boolean hasCloseReceipt = false; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
602 |
if (conContext.isNegotiated) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
603 |
if (!conContext.protocolVersion.useTLS13PlusSpec()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
604 |
hasCloseReceipt = true; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
605 |
} else { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
606 |
// 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
|
607 |
useUserCanceled = true; |
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 if (conContext.handshakeContext != null) { // initial handshake |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
610 |
// Use user_canceled alert regardless the protocol versions. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
611 |
useUserCanceled = true; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
612 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
613 |
// The protocol version may have been negotiated. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
614 |
ProtocolVersion pv = conContext.handshakeContext.negotiatedProtocol; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
615 |
if (pv == null || (!pv.useTLS13PlusSpec())) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
616 |
hasCloseReceipt = true; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
617 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
618 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
619 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
620 |
// 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
|
621 |
// close_notify alert can be delivered together. |
55411
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
622 |
int linger = getSoLinger(); |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
623 |
if (linger >= 0) { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
624 |
// don't wait more than SO_LINGER for obtaining the |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
625 |
// the lock. |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
626 |
// |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
627 |
// keep and clear the current thread interruption status. |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
628 |
boolean interrupted = Thread.interrupted(); |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
629 |
try { |
55411
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
630 |
if (conContext.outputRecord.recordLock.tryLock() || |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
631 |
conContext.outputRecord.recordLock.tryLock( |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
632 |
linger, TimeUnit.SECONDS)) { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
633 |
try { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
634 |
handleClosedNotifyAlert(useUserCanceled); |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
635 |
} finally { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
636 |
conContext.outputRecord.recordLock.unlock(); |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
637 |
} |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
638 |
} else { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
639 |
// For layered, non-autoclose sockets, we are not |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
640 |
// able to bring them into a usable state, so we |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
641 |
// treat it as fatal error. |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
642 |
if (!super.isOutputShutdown()) { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
643 |
if (isLayered() && !autoClose) { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
644 |
throw new SSLException( |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
645 |
"SO_LINGER timeout, " + |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
646 |
"close_notify message cannot be sent."); |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
647 |
} else { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
648 |
super.shutdownOutput(); |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
649 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
650 |
SSLLogger.warning( |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
651 |
"SSLSocket output duplex close failed: " + |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
652 |
"SO_LINGER timeout, " + |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
653 |
"close_notify message cannot be sent."); |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
654 |
} |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
655 |
} |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
656 |
} |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
657 |
|
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
658 |
// RFC2246 requires that the session becomes |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
659 |
// unresumable if any connection is terminated |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
660 |
// without proper close_notify messages with |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
661 |
// level equal to warning. |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
662 |
// |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
663 |
// RFC4346 no longer requires that a session not be |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
664 |
// resumed if failure to properly close a connection. |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
665 |
// |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
666 |
// We choose to make the session unresumable if |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
667 |
// failed to send the close_notify message. |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
668 |
// |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
669 |
conContext.conSession.invalidate(); |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
670 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
671 |
SSLLogger.warning( |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
672 |
"Invalidate the session: SO_LINGER timeout, " + |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
673 |
"close_notify message cannot be sent."); |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
674 |
} |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
675 |
} |
55411
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
676 |
} catch (InterruptedException ex) { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
677 |
// keep interrupted status |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
678 |
interrupted = true; |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
679 |
} |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
680 |
|
55411
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
681 |
// restore the interrupted status |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
682 |
if (interrupted) { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
683 |
Thread.currentThread().interrupt(); |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
684 |
} |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
685 |
} else { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
686 |
conContext.outputRecord.recordLock.lock(); |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
687 |
try { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
688 |
handleClosedNotifyAlert(useUserCanceled); |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
689 |
} finally { |
55411
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
690 |
conContext.outputRecord.recordLock.unlock(); |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
691 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
692 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
693 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
694 |
if (!isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
695 |
bruteForceCloseInput(hasCloseReceipt); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
696 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
697 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
698 |
|
55411
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
699 |
private void handleClosedNotifyAlert( |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
700 |
boolean useUserCanceled) throws IOException { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
701 |
try { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
702 |
// send a user_canceled alert if needed. |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
703 |
if (useUserCanceled) { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
704 |
conContext.warning(Alert.USER_CANCELED); |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
705 |
} |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
706 |
|
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
707 |
// send a close_notify alert |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
708 |
conContext.warning(Alert.CLOSE_NOTIFY); |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
709 |
} finally { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
710 |
if (!conContext.isOutboundClosed()) { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
711 |
conContext.outputRecord.close(); |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
712 |
} |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
713 |
|
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
714 |
if (!super.isOutputShutdown() && |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
715 |
(autoClose || !isLayered())) { |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
716 |
super.shutdownOutput(); |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
717 |
} |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
718 |
} |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
719 |
} |
328d4a455e4b
8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents:
54718
diff
changeset
|
720 |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
721 |
/** |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
722 |
* Duplex close, start from closing inbound. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
723 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
724 |
* 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
|
725 |
* but the inbound is still open. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
726 |
*/ |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
727 |
private void duplexCloseInput() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
728 |
boolean hasCloseReceipt = false; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
729 |
if (conContext.isNegotiated && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
730 |
!conContext.protocolVersion.useTLS13PlusSpec()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
731 |
hasCloseReceipt = true; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
732 |
} // No close receipt if handshake has no completed. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
733 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
734 |
bruteForceCloseInput(hasCloseReceipt); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
735 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
736 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
737 |
/** |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
738 |
* Brute force close the input bound. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
739 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
740 |
* 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
|
741 |
* but the inbound is still open. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
742 |
*/ |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
743 |
private void bruteForceCloseInput( |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
744 |
boolean hasCloseReceipt) throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
745 |
if (hasCloseReceipt) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
746 |
// 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
|
747 |
// 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
|
748 |
// of the connection. However, if the application protocol using |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
749 |
// 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
|
750 |
// transport after the TLS connection is closed, the TLS |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
751 |
// implementation MUST receive a "close_notify" alert before |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
752 |
// indicating end-of-data to the application-layer. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
753 |
try { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
754 |
this.shutdown(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
755 |
} finally { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
756 |
if (!isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
757 |
shutdownInput(false); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
758 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
759 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
760 |
} else { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
761 |
if (!conContext.isInboundClosed()) { |
53067
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
762 |
try (conContext.inputRecord) { |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
763 |
// 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
|
764 |
// socket gracefully, without impact the performance too |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
765 |
// much. |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
766 |
appInput.deplete(); |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
767 |
} |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
768 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
769 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
770 |
if ((autoClose || !isLayered()) && !super.isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
771 |
super.shutdownInput(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
772 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
773 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
774 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
775 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
776 |
// 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
|
777 |
// locks may be deadlocked. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
778 |
@Override |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
779 |
public void shutdownInput() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
780 |
shutdownInput(true); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
781 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
782 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
783 |
// 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
|
784 |
// application call shutdownInput() explicitly. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
785 |
private void shutdownInput( |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
786 |
boolean checkCloseNotify) throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
787 |
if (isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
788 |
return; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
789 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
790 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
791 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
792 |
SSLLogger.fine("close inbound of SSLSocket"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
793 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
794 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
795 |
// Is it ready to close inbound? |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
796 |
// |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
797 |
// 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
|
798 |
if (checkCloseNotify && !conContext.isInputCloseNotified && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
799 |
(conContext.isNegotiated || conContext.handshakeContext != null)) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
800 |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
53018
diff
changeset
|
801 |
throw conContext.fatal(Alert.INTERNAL_ERROR, |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
802 |
"closing inbound before receiving peer's close_notify"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
803 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
804 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
805 |
conContext.closeInbound(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
806 |
if ((autoClose || !isLayered()) && !super.isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
807 |
super.shutdownInput(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
808 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
809 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
810 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
811 |
@Override |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
812 |
public boolean isInputShutdown() { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
813 |
return conContext.isInboundClosed() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
814 |
((autoClose || !isLayered()) ? super.isInputShutdown(): true); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
815 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
816 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
817 |
// 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
|
818 |
// locks may be deadlocked. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
819 |
@Override |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
820 |
public void shutdownOutput() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
821 |
if (isOutputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
822 |
return; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
823 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
824 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
825 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
826 |
SSLLogger.fine("close outbound of SSLSocket"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
827 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
828 |
conContext.closeOutbound(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
829 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
830 |
if ((autoClose || !isLayered()) && !super.isOutputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
831 |
super.shutdownOutput(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
832 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
833 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
834 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
835 |
@Override |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
836 |
public boolean isOutputShutdown() { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
837 |
return conContext.isOutboundClosed() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
838 |
((autoClose || !isLayered()) ? super.isOutputShutdown(): true); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
839 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
840 |
|
50768 | 841 |
@Override |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
842 |
public InputStream getInputStream() throws IOException { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
843 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
844 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
845 |
if (isClosed()) { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
846 |
throw new SocketException("Socket is closed"); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
847 |
} |
50768 | 848 |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
849 |
if (!isConnected) { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
850 |
throw new SocketException("Socket is not connected"); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
851 |
} |
50768 | 852 |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
853 |
if (conContext.isInboundClosed() || isInputShutdown()) { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
854 |
throw new SocketException("Socket input is already shutdown"); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
855 |
} |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
856 |
|
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
857 |
return appInput; |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
858 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
859 |
socketLock.unlock(); |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
860 |
} |
50768 | 861 |
} |
862 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
863 |
private void ensureNegotiated() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
864 |
if (conContext.isNegotiated || conContext.isBroken || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
865 |
conContext.isInboundClosed() || conContext.isOutboundClosed()) { |
50768 | 866 |
return; |
867 |
} |
|
868 |
||
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
869 |
handshakeLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
870 |
try { |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
871 |
// double check the context status |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
872 |
if (conContext.isNegotiated || conContext.isBroken || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
873 |
conContext.isInboundClosed() || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
874 |
conContext.isOutboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
875 |
return; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
876 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
877 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
878 |
startHandshake(); |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
879 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
880 |
handshakeLock.unlock(); |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
881 |
} |
2 | 882 |
} |
883 |
||
884 |
/** |
|
50768 | 885 |
* InputStream for application data as returned by |
886 |
* SSLSocket.getInputStream(). |
|
2 | 887 |
*/ |
50768 | 888 |
private class AppInputStream extends InputStream { |
889 |
// One element array used to implement the single byte read() method |
|
890 |
private final byte[] oneByte = new byte[1]; |
|
891 |
||
892 |
// the temporary buffer used to read network |
|
893 |
private ByteBuffer buffer; |
|
894 |
||
895 |
// Is application data available in the stream? |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
896 |
private volatile boolean appDataIsAvailable; |
50768 | 897 |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
898 |
// reading lock |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
899 |
private final ReentrantLock readLock = new ReentrantLock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
900 |
|
54718
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
901 |
// closing status |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
902 |
private volatile boolean isClosing; |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
903 |
private volatile boolean hasDepleted; |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
904 |
|
50768 | 905 |
AppInputStream() { |
906 |
this.appDataIsAvailable = false; |
|
907 |
this.buffer = ByteBuffer.allocate(4096); |
|
908 |
} |
|
909 |
||
910 |
/** |
|
911 |
* Return the minimum number of bytes that can be read |
|
912 |
* without blocking. |
|
913 |
*/ |
|
914 |
@Override |
|
915 |
public int available() throws IOException { |
|
916 |
// Currently not synchronized. |
|
917 |
if ((!appDataIsAvailable) || checkEOF()) { |
|
918 |
return 0; |
|
919 |
} |
|
920 |
||
921 |
return buffer.remaining(); |
|
922 |
} |
|
923 |
||
924 |
/** |
|
925 |
* Read a single byte, returning -1 on non-fault EOF status. |
|
2 | 926 |
*/ |
50768 | 927 |
@Override |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
928 |
public int read() throws IOException { |
50768 | 929 |
int n = read(oneByte, 0, 1); |
930 |
if (n <= 0) { // EOF |
|
931 |
return -1; |
|
932 |
} |
|
933 |
||
934 |
return oneByte[0] & 0xFF; |
|
935 |
} |
|
2 | 936 |
|
50768 | 937 |
/** |
938 |
* Reads up to {@code len} bytes of data from the input stream |
|
939 |
* into an array of bytes. |
|
940 |
* |
|
941 |
* An attempt is made to read as many as {@code len} bytes, but a |
|
942 |
* smaller number may be read. The number of bytes actually read |
|
943 |
* is returned as an integer. |
|
944 |
* |
|
945 |
* If the layer above needs more data, it asks for more, so we |
|
946 |
* are responsible only for blocking to fill at most one buffer, |
|
947 |
* and returning "-1" on non-fault EOF status. |
|
948 |
*/ |
|
949 |
@Override |
|
54718
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
950 |
public int read(byte[] b, int off, int len) throws IOException { |
50768 | 951 |
if (b == null) { |
952 |
throw new NullPointerException("the target buffer is null"); |
|
953 |
} else if (off < 0 || len < 0 || len > b.length - off) { |
|
954 |
throw new IndexOutOfBoundsException( |
|
955 |
"buffer length: " + b.length + ", offset; " + off + |
|
956 |
", bytes to read:" + len); |
|
957 |
} else if (len == 0) { |
|
958 |
return 0; |
|
959 |
} |
|
960 |
||
961 |
if (checkEOF()) { |
|
962 |
return -1; |
|
963 |
} |
|
964 |
||
965 |
// 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
|
966 |
if (!conContext.isNegotiated && !conContext.isBroken && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
967 |
!conContext.isInboundClosed() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
968 |
!conContext.isOutboundClosed()) { |
50768 | 969 |
ensureNegotiated(); |
970 |
} |
|
971 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
972 |
// Check if the Socket is invalid (error or closed). |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
973 |
if (!conContext.isNegotiated || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
974 |
conContext.isBroken || conContext.isInboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
975 |
throw new SocketException("Connection or inbound has closed"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
976 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
977 |
|
54718
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
978 |
// Check if the input stream has been depleted. |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
979 |
// |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
980 |
// Note that the "hasDepleted" rather than the isClosing |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
981 |
// filed is checked here, in case the closing process is |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
982 |
// still in progress. |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
983 |
if (hasDepleted) { |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
984 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
985 |
SSLLogger.fine("The input stream has been depleted"); |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
986 |
} |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
987 |
|
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
988 |
return -1; |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
989 |
} |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
990 |
|
50768 | 991 |
// Read the available bytes at first. |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
992 |
// |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
993 |
// 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
|
994 |
// are also synchronized with the read lock. |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
995 |
readLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
996 |
try { |
54718
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
997 |
// Double check if the Socket is invalid (error or closed). |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
998 |
if (conContext.isBroken || conContext.isInboundClosed()) { |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
999 |
throw new SocketException( |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1000 |
"Connection or inbound has closed"); |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1001 |
} |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1002 |
|
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1003 |
// Double check if the input stream has been depleted. |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1004 |
if (hasDepleted) { |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1005 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1006 |
SSLLogger.fine("The input stream is closing"); |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1007 |
} |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1008 |
|
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1009 |
return -1; |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1010 |
} |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1011 |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1012 |
int remains = available(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1013 |
if (remains > 0) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1014 |
int howmany = Math.min(remains, len); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1015 |
buffer.get(b, off, howmany); |
30904 | 1016 |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1017 |
return howmany; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1018 |
} |
50768 | 1019 |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1020 |
appDataIsAvailable = false; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1021 |
try { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1022 |
ByteBuffer bb = readApplicationRecord(buffer); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1023 |
if (bb == null) { // EOF |
50768 | 1024 |
return -1; |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1025 |
} else { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1026 |
// The buffer may be reallocated for bigger capacity. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1027 |
buffer = bb; |
50768 | 1028 |
} |
1029 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1030 |
bb.flip(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1031 |
int volume = Math.min(len, bb.remaining()); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1032 |
buffer.get(b, off, volume); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1033 |
appDataIsAvailable = true; |
50768 | 1034 |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1035 |
return volume; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1036 |
} catch (Exception e) { // including RuntimeException |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1037 |
// shutdown and rethrow (wrapped) exception as appropriate |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1038 |
handleException(e); |
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 |
// dummy for compiler |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1041 |
return -1; |
50768 | 1042 |
} |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1043 |
} finally { |
54718
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1044 |
// Check if the input stream is closing. |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1045 |
// |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1046 |
// If the deplete() did not hold the lock, clean up the |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1047 |
// input stream here. |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1048 |
try { |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1049 |
if (isClosing) { |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1050 |
readLockedDeplete(); |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1051 |
} |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1052 |
} finally { |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1053 |
readLock.unlock(); |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1054 |
} |
50768 | 1055 |
} |
1056 |
} |
|
2 | 1057 |
|
50768 | 1058 |
/** |
1059 |
* Skip n bytes. |
|
1060 |
* |
|
1061 |
* This implementation is somewhat less efficient than possible, but |
|
1062 |
* 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
|
1063 |
* things simpler. |
50768 | 1064 |
*/ |
1065 |
@Override |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1066 |
public long skip(long n) throws IOException { |
50768 | 1067 |
// dummy array used to implement skip() |
1068 |
byte[] skipArray = new byte[256]; |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1069 |
long skipped = 0; |
2 | 1070 |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1071 |
readLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1072 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1073 |
while (n > 0) { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1074 |
int len = (int)Math.min(n, skipArray.length); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1075 |
int r = read(skipArray, 0, len); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1076 |
if (r <= 0) { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1077 |
break; |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1078 |
} |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1079 |
n -= r; |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1080 |
skipped += r; |
50768 | 1081 |
} |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1082 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1083 |
readLock.unlock(); |
50768 | 1084 |
} |
1085 |
||
1086 |
return skipped; |
|
1087 |
} |
|
1088 |
||
1089 |
@Override |
|
1090 |
public void close() throws IOException { |
|
1091 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
1092 |
SSLLogger.finest("Closing input stream"); |
|
1093 |
} |
|
1094 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1095 |
try { |
54551
6f8a7671afef
8216326: SSLSocket stream close() does not close the associated socket
xuelei
parents:
54443
diff
changeset
|
1096 |
SSLSocketImpl.this.close(); |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1097 |
} catch (IOException ioe) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1098 |
// ignore the exception |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1099 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1100 |
SSLLogger.warning("input stream close failed", ioe); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1101 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1102 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1103 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1104 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1105 |
/** |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1106 |
* Return whether we have reached end-of-file. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1107 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1108 |
* 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
|
1109 |
* or has been closed, throw an Exception. |
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 |
private boolean checkEOF() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1112 |
if (conContext.isInboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1113 |
return true; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1114 |
} else if (conContext.isInputCloseNotified || conContext.isBroken) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1115 |
if (conContext.closeReason == null) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1116 |
return true; |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1117 |
} else { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1118 |
throw new SSLException( |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1119 |
"Connection has closed: " + conContext.closeReason, |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1120 |
conContext.closeReason); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1121 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1122 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1123 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1124 |
return false; |
50768 | 1125 |
} |
53067
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
1126 |
|
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
1127 |
/** |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
1128 |
* 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
|
1129 |
* socket gracefully, without impact the performance too much. |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
1130 |
*/ |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1131 |
private void deplete() { |
54718
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1132 |
if (conContext.isInboundClosed() || isClosing) { |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1133 |
return; |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1134 |
} |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1135 |
|
54718
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1136 |
isClosing = true; |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1137 |
if (readLock.tryLock()) { |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1138 |
try { |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1139 |
readLockedDeplete(); |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1140 |
} finally { |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1141 |
readLock.unlock(); |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1142 |
} |
54718
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1143 |
} |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1144 |
} |
53067
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
1145 |
|
54718
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1146 |
/** |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1147 |
* Try to use up the input records. |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1148 |
* |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1149 |
* Please don't call this method unless the readLock is held by |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1150 |
* the current thread. |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1151 |
*/ |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1152 |
private void readLockedDeplete() { |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1153 |
// double check |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1154 |
if (hasDepleted || conContext.isInboundClosed()) { |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1155 |
return; |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1156 |
} |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1157 |
|
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1158 |
if (!(conContext.inputRecord instanceof SSLSocketInputRecord)) { |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1159 |
return; |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1160 |
} |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1161 |
|
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1162 |
SSLSocketInputRecord socketInputRecord = |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1163 |
(SSLSocketInputRecord)conContext.inputRecord; |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1164 |
try { |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1165 |
socketInputRecord.deplete( |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1166 |
conContext.isNegotiated && (getSoTimeout() > 0)); |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1167 |
} catch (Exception ex) { |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1168 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1169 |
SSLLogger.warning( |
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1170 |
"input stream close depletion failed", ex); |
53067
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
1171 |
} |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1172 |
} finally { |
54718
ca251ef47e0b
8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
54551
diff
changeset
|
1173 |
hasDepleted = true; |
53067
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
1174 |
} |
8a61a04c456c
8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents:
53064
diff
changeset
|
1175 |
} |
2 | 1176 |
} |
1177 |
||
50768 | 1178 |
@Override |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1179 |
public OutputStream getOutputStream() throws IOException { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1180 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1181 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1182 |
if (isClosed()) { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1183 |
throw new SocketException("Socket is closed"); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1184 |
} |
2 | 1185 |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1186 |
if (!isConnected) { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1187 |
throw new SocketException("Socket is not connected"); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1188 |
} |
50768 | 1189 |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1190 |
if (conContext.isOutboundDone() || isOutputShutdown()) { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1191 |
throw new SocketException("Socket output is already shutdown"); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1192 |
} |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1193 |
|
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1194 |
return appOutput; |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1195 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1196 |
socketLock.unlock(); |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1197 |
} |
50768 | 1198 |
} |
1199 |
||
1200 |
||
1201 |
/** |
|
1202 |
* OutputStream for application data as returned by |
|
1203 |
* SSLSocket.getOutputStream(). |
|
12428 | 1204 |
*/ |
50768 | 1205 |
private class AppOutputStream extends OutputStream { |
1206 |
// One element array used to implement the write(byte) method |
|
1207 |
private final byte[] oneByte = new byte[1]; |
|
1208 |
||
1209 |
@Override |
|
1210 |
public void write(int i) throws IOException { |
|
1211 |
oneByte[0] = (byte)i; |
|
1212 |
write(oneByte, 0, 1); |
|
1213 |
} |
|
1214 |
||
1215 |
@Override |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1216 |
public void write(byte[] b, |
50768 | 1217 |
int off, int len) throws IOException { |
1218 |
if (b == null) { |
|
1219 |
throw new NullPointerException("the source buffer is null"); |
|
1220 |
} else if (off < 0 || len < 0 || len > b.length - off) { |
|
1221 |
throw new IndexOutOfBoundsException( |
|
1222 |
"buffer length: " + b.length + ", offset; " + off + |
|
1223 |
", bytes to read:" + len); |
|
1224 |
} else if (len == 0) { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1225 |
// |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1226 |
// 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
|
1227 |
// far to drive the handshake machinery, for correctness; not |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1228 |
// writing empty records improves performance by cutting CPU |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1229 |
// time and network resource usage. However, some protocol |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1230 |
// 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
|
1231 |
// records, so this also increases robustness. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1232 |
// |
50768 | 1233 |
return; |
1234 |
} |
|
1235 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1236 |
// Start handshaking if the connection has not been negotiated. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1237 |
if (!conContext.isNegotiated && !conContext.isBroken && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1238 |
!conContext.isInboundClosed() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1239 |
!conContext.isOutboundClosed()) { |
50768 | 1240 |
ensureNegotiated(); |
1241 |
} |
|
1242 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1243 |
// Check if the Socket is invalid (error or closed). |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1244 |
if (!conContext.isNegotiated || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1245 |
conContext.isBroken || conContext.isOutboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1246 |
throw new SocketException("Connection or outbound has closed"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1247 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1248 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1249 |
// |
2 | 1250 |
|
50768 | 1251 |
// Delegate the writing to the underlying socket. |
1252 |
try { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1253 |
conContext.outputRecord.deliver(b, off, len); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1254 |
} catch (SSLHandshakeException she) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1255 |
// may be record sequence number overflow |
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
53018
diff
changeset
|
1256 |
throw conContext.fatal(Alert.HANDSHAKE_FAILURE, she); |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1257 |
} catch (IOException e) { |
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
53018
diff
changeset
|
1258 |
throw conContext.fatal(Alert.UNEXPECTED_MESSAGE, e); |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1259 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1260 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1261 |
// 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
|
1262 |
// limit been reached? |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1263 |
if (conContext.outputRecord.seqNumIsHuge() || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1264 |
conContext.outputRecord.writeCipher.atKeyLimit()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1265 |
tryKeyUpdate(); |
50768 | 1266 |
} |
57485 | 1267 |
// Check if NewSessionTicket PostHandshake message needs to be sent |
1268 |
if (conContext.conSession.updateNST) { |
|
1269 |
conContext.conSession.updateNST = false; |
|
1270 |
tryNewSessionTicket(); |
|
1271 |
} |
|
50768 | 1272 |
} |
2 | 1273 |
|
50768 | 1274 |
@Override |
1275 |
public void close() throws IOException { |
|
1276 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
1277 |
SSLLogger.finest("Closing output stream"); |
|
1278 |
} |
|
1279 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1280 |
try { |
54551
6f8a7671afef
8216326: SSLSocket stream close() does not close the associated socket
xuelei
parents:
54443
diff
changeset
|
1281 |
SSLSocketImpl.this.close(); |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1282 |
} catch (IOException ioe) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1283 |
// ignore the exception |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1284 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1285 |
SSLLogger.warning("output stream close failed", ioe); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1286 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1287 |
} |
50768 | 1288 |
} |
1289 |
} |
|
1290 |
||
1291 |
@Override |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1292 |
public SSLParameters getSSLParameters() { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1293 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1294 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1295 |
return conContext.sslConfig.getSSLParameters(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1296 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1297 |
socketLock.unlock(); |
50768 | 1298 |
} |
1299 |
} |
|
1300 |
||
1301 |
@Override |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1302 |
public void setSSLParameters(SSLParameters params) { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1303 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1304 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1305 |
conContext.sslConfig.setSSLParameters(params); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1306 |
|
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1307 |
if (conContext.sslConfig.maximumPacketSize != 0) { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1308 |
conContext.outputRecord.changePacketSize( |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1309 |
conContext.sslConfig.maximumPacketSize); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1310 |
} |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1311 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1312 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1313 |
} |
50768 | 1314 |
} |
1315 |
||
1316 |
@Override |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1317 |
public String getApplicationProtocol() { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1318 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1319 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1320 |
return conContext.applicationProtocol; |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1321 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1322 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1323 |
} |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1324 |
} |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1325 |
|
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1326 |
@Override |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1327 |
public String getHandshakeApplicationProtocol() { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1328 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1329 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1330 |
if (conContext.handshakeContext != null) { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1331 |
return conContext.handshakeContext.applicationProtocol; |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1332 |
} |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1333 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1334 |
socketLock.unlock(); |
50768 | 1335 |
} |
2 | 1336 |
|
50768 | 1337 |
return null; |
1338 |
} |
|
1339 |
||
1340 |
@Override |
|
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1341 |
public void setHandshakeApplicationProtocolSelector( |
50768 | 1342 |
BiFunction<SSLSocket, List<String>, String> selector) { |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1343 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1344 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1345 |
conContext.sslConfig.socketAPSelector = selector; |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1346 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1347 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1348 |
} |
50768 | 1349 |
} |
2 | 1350 |
|
50768 | 1351 |
@Override |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1352 |
public BiFunction<SSLSocket, List<String>, String> |
50768 | 1353 |
getHandshakeApplicationProtocolSelector() { |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1354 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1355 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1356 |
return conContext.sslConfig.socketAPSelector; |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1357 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1358 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1359 |
} |
50768 | 1360 |
} |
1361 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1362 |
/** |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1363 |
* Read the initial handshake records. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1364 |
*/ |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1365 |
private int readHandshakeRecord() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1366 |
while (!conContext.isInboundClosed()) { |
50768 | 1367 |
try { |
1368 |
Plaintext plainText = decode(null); |
|
1369 |
if ((plainText.contentType == ContentType.HANDSHAKE.id) && |
|
1370 |
conContext.isNegotiated) { |
|
1371 |
return 0; |
|
1372 |
} |
|
1373 |
} catch (SSLException ssle) { |
|
1374 |
throw ssle; |
|
1375 |
} catch (IOException ioe) { |
|
1376 |
if (!(ioe instanceof SSLException)) { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1377 |
throw new SSLException("readHandshakeRecord", ioe); |
50768 | 1378 |
} else { |
1379 |
throw ioe; |
|
1380 |
} |
|
1381 |
} |
|
2 | 1382 |
} |
30904 | 1383 |
|
50768 | 1384 |
return -1; |
30904 | 1385 |
} |
1386 |
||
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1387 |
/** |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1388 |
* 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
|
1389 |
* here so as to use the socket level synchronization. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1390 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1391 |
* 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
|
1392 |
* 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
|
1393 |
* them here. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1394 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1395 |
* 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
|
1396 |
* method other than in the AppInputStream class! |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1397 |
*/ |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1398 |
private ByteBuffer readApplicationRecord( |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1399 |
ByteBuffer buffer) throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1400 |
while (!conContext.isInboundClosed()) { |
50768 | 1401 |
/* |
1402 |
* clean the buffer and check if it is too small, e.g. because |
|
1403 |
* the AppInputStream did not have the chance to see the |
|
1404 |
* current packet length but rather something like that of the |
|
1405 |
* handshake before. In that case we return 0 at this point to |
|
1406 |
* give the caller the chance to adjust the buffer. |
|
1407 |
*/ |
|
1408 |
buffer.clear(); |
|
1409 |
int inLen = conContext.inputRecord.bytesInCompletePacket(); |
|
1410 |
if (inLen < 0) { // EOF |
|
1411 |
handleEOF(null); |
|
2 | 1412 |
|
50768 | 1413 |
// if no exception thrown |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1414 |
return null; |
50768 | 1415 |
} |
30904 | 1416 |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1417 |
// Is this packet bigger than SSL/TLS normally allows? |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1418 |
if (inLen > SSLRecord.maxLargeRecordSize) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1419 |
throw new SSLProtocolException( |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1420 |
"Illegal packet size: " + inLen); |
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 |
if (inLen > buffer.remaining()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1424 |
buffer = ByteBuffer.allocate(inLen); |
50768 | 1425 |
} |
7039 | 1426 |
|
50768 | 1427 |
try { |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1428 |
Plaintext plainText; |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1429 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1430 |
try { |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1431 |
plainText = decode(buffer); |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1432 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1433 |
socketLock.unlock(); |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1434 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1435 |
if (plainText.contentType == ContentType.APPLICATION_DATA.id && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1436 |
buffer.position() > 0) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1437 |
return buffer; |
30904 | 1438 |
} |
50768 | 1439 |
} catch (SSLException ssle) { |
1440 |
throw ssle; |
|
1441 |
} catch (IOException ioe) { |
|
1442 |
if (!(ioe instanceof SSLException)) { |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1443 |
throw new SSLException("readApplicationRecord", ioe); |
50768 | 1444 |
} else { |
1445 |
throw ioe; |
|
30904 | 1446 |
} |
50768 | 1447 |
} |
7039 | 1448 |
} |
1449 |
||
50768 | 1450 |
// |
1451 |
// couldn't read, due to some kind of error |
|
1452 |
// |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1453 |
return null; |
50768 | 1454 |
} |
1455 |
||
1456 |
private Plaintext decode(ByteBuffer destination) throws IOException { |
|
1457 |
Plaintext plainText; |
|
1458 |
try { |
|
1459 |
if (destination == null) { |
|
1460 |
plainText = SSLTransport.decode(conContext, |
|
1461 |
null, 0, 0, null, 0, 0); |
|
1462 |
} else { |
|
1463 |
plainText = SSLTransport.decode(conContext, |
|
1464 |
null, 0, 0, new ByteBuffer[]{destination}, 0, 1); |
|
7039 | 1465 |
} |
50768 | 1466 |
} catch (EOFException eofe) { |
1467 |
// EOFException is special as it is related to close_notify. |
|
1468 |
plainText = handleEOF(eofe); |
|
7039 | 1469 |
} |
30904 | 1470 |
|
50768 | 1471 |
// Is the sequence number is nearly overflow? |
1472 |
if (plainText != Plaintext.PLAINTEXT_NULL && |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1473 |
(conContext.inputRecord.seqNumIsHuge() || |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1474 |
conContext.inputRecord.readCipher.atKeyLimit())) { |
50768 | 1475 |
tryKeyUpdate(); |
1476 |
} |
|
30904 | 1477 |
|
50768 | 1478 |
return plainText; |
2 | 1479 |
} |
1480 |
||
1481 |
/** |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1482 |
* Try key update for sequence number wrap or key usage limit. |
50768 | 1483 |
* |
1484 |
* 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
|
1485 |
* 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
|
1486 |
* reading/writing process. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1487 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1488 |
* As we request renegotiation or close the connection for wrapped sequence |
50768 | 1489 |
* number when there is enough sequence number space left to handle a few |
1490 |
* more records, so the sequence number of the last record cannot be |
|
1491 |
* wrapped. |
|
2 | 1492 |
*/ |
50768 | 1493 |
private void tryKeyUpdate() throws IOException { |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1494 |
// 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
|
1495 |
// connection is not duplex-open. |
50768 | 1496 |
if ((conContext.handshakeContext == null) && |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1497 |
!conContext.isOutboundClosed() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1498 |
!conContext.isInboundClosed() && |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1499 |
!conContext.isBroken) { |
50768 | 1500 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1501 |
SSLLogger.finest("trigger key update"); |
50768 | 1502 |
} |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1503 |
startHandshake(); |
2 | 1504 |
} |
1505 |
} |
|
1506 |
||
57485 | 1507 |
// Try to generate a PostHandshake NewSessionTicket message. This is |
1508 |
// TLS 1.3 only. |
|
1509 |
private void tryNewSessionTicket() throws IOException { |
|
1510 |
// Don't bother to kickstart if handshaking is in progress, or if the |
|
1511 |
// connection is not duplex-open. |
|
1512 |
if (!conContext.sslConfig.isClientMode && |
|
1513 |
conContext.protocolVersion.useTLS13PlusSpec() && |
|
1514 |
conContext.handshakeContext == null && |
|
1515 |
!conContext.isOutboundClosed() && |
|
1516 |
!conContext.isInboundClosed() && |
|
1517 |
!conContext.isBroken) { |
|
1518 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
1519 |
SSLLogger.finest("trigger new session ticket"); |
|
1520 |
} |
|
1521 |
NewSessionTicket.kickstartProducer.produce( |
|
1522 |
new PostHandshakeContext(conContext)); |
|
1523 |
} |
|
1524 |
} |
|
1525 |
||
2 | 1526 |
/** |
50768 | 1527 |
* Initialize the handshaker and socket streams. |
2 | 1528 |
* |
50768 | 1529 |
* Called by connect, the layered constructor, and SSLServerSocket. |
2 | 1530 |
*/ |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1531 |
void doneConnect() throws IOException { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1532 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1533 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1534 |
// In server mode, it is not necessary to set host and serverNames. |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1535 |
// Otherwise, would require a reverse DNS lookup to get |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1536 |
// the hostname. |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1537 |
if (peerHost == null || peerHost.isEmpty()) { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1538 |
boolean useNameService = |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1539 |
trustNameService && conContext.sslConfig.isClientMode; |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1540 |
useImplicitHost(useNameService); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1541 |
} else { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1542 |
conContext.sslConfig.serverNames = |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1543 |
Utilities.addToSNIServerNameList( |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1544 |
conContext.sslConfig.serverNames, peerHost); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1545 |
} |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1546 |
|
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1547 |
InputStream sockInput = super.getInputStream(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1548 |
conContext.inputRecord.setReceiverStream(sockInput); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1549 |
|
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1550 |
OutputStream sockOutput = super.getOutputStream(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1551 |
conContext.inputRecord.setDeliverStream(sockOutput); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1552 |
conContext.outputRecord.setDeliverStream(sockOutput); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1553 |
|
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1554 |
this.isConnected = true; |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1555 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1556 |
socketLock.unlock(); |
2 | 1557 |
} |
1558 |
} |
|
1559 |
||
50768 | 1560 |
private void useImplicitHost(boolean useNameService) { |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1561 |
// Note: If the local name service is not trustworthy, reverse |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1562 |
// host name resolution should not be performed for endpoint |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1563 |
// identification. Use the application original specified |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1564 |
// hostname or IP address instead. |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1565 |
|
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
51986
diff
changeset
|
1566 |
// Get the original hostname via jdk.internal.access.SharedSecrets |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1567 |
InetAddress inetAddress = getInetAddress(); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1568 |
if (inetAddress == null) { // not connected |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1569 |
return; |
31687 | 1570 |
} |
1571 |
||
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1572 |
JavaNetInetAddressAccess jna = |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1573 |
SharedSecrets.getJavaNetInetAddressAccess(); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1574 |
String originalHostname = jna.getOriginalHostName(inetAddress); |
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52427
diff
changeset
|
1575 |
if (originalHostname != null && !originalHostname.isEmpty()) { |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1576 |
|
50768 | 1577 |
this.peerHost = originalHostname; |
1578 |
if (conContext.sslConfig.serverNames.isEmpty() && |
|
1579 |
!conContext.sslConfig.noSniExtension) { |
|
1580 |
conContext.sslConfig.serverNames = |
|
1581 |
Utilities.addToSNIServerNameList( |
|
1582 |
conContext.sslConfig.serverNames, peerHost); |
|
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1583 |
} |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1584 |
|
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1585 |
return; |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1586 |
} |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1587 |
|
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1588 |
// No explicitly specified hostname, no server name indication. |
50768 | 1589 |
if (!useNameService) { |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1590 |
// The local name service is not trustworthy, use IP address. |
50768 | 1591 |
this.peerHost = inetAddress.getHostAddress(); |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1592 |
} else { |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1593 |
// Use the underlying reverse host name resolution service. |
50768 | 1594 |
this.peerHost = getInetAddress().getHostName(); |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
1595 |
} |
31687 | 1596 |
} |
1597 |
||
5162
0dbedf4fdb8c
6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents:
2068
diff
changeset
|
1598 |
// 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
|
1599 |
// |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1600 |
// 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
|
1601 |
// SSLSocket.setSSLParameters(). Otherwise, the {@code host} parameter |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1602 |
// may override SNIHostName in the customized server name indication. |
54443
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1603 |
public void setHost(String host) { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1604 |
socketLock.lock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1605 |
try { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1606 |
this.peerHost = host; |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1607 |
this.conContext.sslConfig.serverNames = |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1608 |
Utilities.addToSNIServerNameList( |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1609 |
conContext.sslConfig.serverNames, host); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1610 |
} finally { |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1611 |
socketLock.unlock(); |
dfba4e321ab3
8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents:
54221
diff
changeset
|
1612 |
} |
2 | 1613 |
} |
1614 |
||
1615 |
/** |
|
50768 | 1616 |
* Handle an exception. |
1617 |
* |
|
1618 |
* This method is called by top level exception handlers (in read(), |
|
1619 |
* write()) to make sure we always shutdown the connection correctly |
|
1620 |
* and do not pass runtime exception to the application. |
|
2 | 1621 |
* |
50768 | 1622 |
* This method never returns normally, it always throws an IOException. |
2 | 1623 |
*/ |
50768 | 1624 |
private void handleException(Exception cause) throws IOException { |
1625 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
1626 |
SSLLogger.warning("handling exception", cause); |
|
1627 |
} |
|
1628 |
||
1629 |
// Don't close the Socket in case of timeouts or interrupts. |
|
1630 |
if (cause instanceof InterruptedIOException) { |
|
1631 |
throw (IOException)cause; |
|
1632 |
} |
|
2 | 1633 |
|
50768 | 1634 |
// need to perform error shutdown |
1635 |
boolean isSSLException = (cause instanceof SSLException); |
|
1636 |
Alert alert; |
|
1637 |
if (isSSLException) { |
|
1638 |
if (cause instanceof SSLHandshakeException) { |
|
1639 |
alert = Alert.HANDSHAKE_FAILURE; |
|
1640 |
} else { |
|
1641 |
alert = Alert.UNEXPECTED_MESSAGE; |
|
1642 |
} |
|
1643 |
} else { |
|
1644 |
if (cause instanceof IOException) { |
|
1645 |
alert = Alert.UNEXPECTED_MESSAGE; |
|
1646 |
} else { |
|
1647 |
// RuntimeException |
|
1648 |
alert = Alert.INTERNAL_ERROR; |
|
1649 |
} |
|
2 | 1650 |
} |
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
53018
diff
changeset
|
1651 |
|
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
53018
diff
changeset
|
1652 |
throw conContext.fatal(alert, cause); |
2 | 1653 |
} |
1654 |
||
50768 | 1655 |
private Plaintext handleEOF(EOFException eofe) throws IOException { |
1656 |
if (requireCloseNotify || conContext.handshakeContext != null) { |
|
1657 |
SSLException ssle; |
|
1658 |
if (conContext.handshakeContext != null) { |
|
1659 |
ssle = new SSLHandshakeException( |
|
1660 |
"Remote host terminated the handshake"); |
|
1661 |
} else { |
|
1662 |
ssle = new SSLProtocolException( |
|
1663 |
"Remote host terminated the connection"); |
|
1664 |
} |
|
2 | 1665 |
|
50768 | 1666 |
if (eofe != null) { |
1667 |
ssle.initCause(eofe); |
|
1668 |
} |
|
1669 |
throw ssle; |
|
1670 |
} else { |
|
1671 |
// treat as if we had received a close_notify |
|
1672 |
conContext.isInputCloseNotified = true; |
|
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1673 |
shutdownInput(); |
2 | 1674 |
|
50768 | 1675 |
return Plaintext.PLAINTEXT_NULL; |
2 | 1676 |
} |
1677 |
} |
|
1678 |
||
1679 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1680 |
@Override |
50768 | 1681 |
public String getPeerHost() { |
1682 |
return peerHost; |
|
2 | 1683 |
} |
1684 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1685 |
@Override |
50768 | 1686 |
public int getPeerPort() { |
1687 |
return getPort(); |
|
2 | 1688 |
} |
1689 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1690 |
@Override |
50768 | 1691 |
public boolean useDelegatedTask() { |
1692 |
return false; |
|
2 | 1693 |
} |
1694 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1695 |
@Override |
50768 | 1696 |
public void shutdown() throws IOException { |
1697 |
if (!isClosed()) { |
|
1698 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
1699 |
SSLLogger.fine("close the underlying socket"); |
|
1700 |
} |
|
2 | 1701 |
|
50768 | 1702 |
try { |
1703 |
if (conContext.isInputCloseNotified) { |
|
1704 |
// Close the connection, no wait for more peer response. |
|
1705 |
closeSocket(false); |
|
1706 |
} else { |
|
1707 |
// Close the connection, may wait for peer close_notify. |
|
1708 |
closeSocket(true); |
|
1709 |
} |
|
1710 |
} finally { |
|
1711 |
tlsIsClosed = true; |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1712 |
} |
7043 | 1713 |
} |
2 | 1714 |
} |
51407
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1715 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1716 |
private void closeSocket(boolean selfInitiated) throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1717 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1718 |
SSLLogger.fine("close the SSL connection " + |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1719 |
(selfInitiated ? "(initiative)" : "(passive)")); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1720 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1721 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1722 |
if (autoClose || !isLayered()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1723 |
super.close(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1724 |
} else if (selfInitiated) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1725 |
if (!conContext.isInboundClosed() && !isInputShutdown()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1726 |
// wait for close_notify alert to clear input stream. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1727 |
waitForClose(); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1728 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1729 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1730 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1731 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1732 |
/** |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1733 |
* Wait for close_notify alert for a graceful closure. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1734 |
* |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1735 |
* [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
|
1736 |
* 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
|
1737 |
* 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
|
1738 |
* 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
|
1739 |
* 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
|
1740 |
* 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
|
1741 |
* 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
|
1742 |
* transport without waiting for the responding close_notify. |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1743 |
*/ |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1744 |
private void waitForClose() throws IOException { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1745 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1746 |
SSLLogger.fine("wait for close_notify or alert"); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1747 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1748 |
|
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1749 |
while (!conContext.isInboundClosed()) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1750 |
try { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1751 |
Plaintext plainText = decode(null); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1752 |
// discard and continue |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1753 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1754 |
SSLLogger.finest( |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1755 |
"discard plaintext while waiting for close", plainText); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1756 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1757 |
} catch (Exception e) { // including RuntimeException |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1758 |
handleException(e); |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1759 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1760 |
} |
910f7b56592f
8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents:
50768
diff
changeset
|
1761 |
} |
2 | 1762 |
} |