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