Skip to content

Commit 01b2804

Browse files
cliveverghesesimonis
authored andcommittedJan 11, 2021
8237578: JDK-8214339 (SSLSocketImpl wraps SocketException) appears to not be fully fixed
Reviewed-by: xuelei, simonis
1 parent 1bd015f commit 01b2804

File tree

5 files changed

+182
-7
lines changed

5 files changed

+182
-7
lines changed
 

‎src/java.base/share/classes/sun/security/ssl/SSLSocketImpl.java

+18
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,8 @@ private void startHandshake(boolean resumable) throws IOException {
447447
throw conContext.fatal(Alert.HANDSHAKE_FAILURE,
448448
"Couldn't kickstart handshaking", iioe);
449449
}
450+
} catch (SocketException se) {
451+
handleException(se);
450452
} catch (IOException ioe) {
451453
throw conContext.fatal(Alert.HANDSHAKE_FAILURE,
452454
"Couldn't kickstart handshaking", ioe);
@@ -1411,6 +1413,9 @@ private int readHandshakeRecord() throws IOException {
14111413
} catch (InterruptedIOException iioe) {
14121414
// don't change exception in case of timeouts or interrupts
14131415
throw iioe;
1416+
} catch (SocketException se) {
1417+
// don't change exception in case of SocketException
1418+
throw se;
14141419
} catch (IOException ioe) {
14151420
throw new SSLException("readHandshakeRecord", ioe);
14161421
}
@@ -1476,6 +1481,9 @@ private ByteBuffer readApplicationRecord(
14761481
} catch (InterruptedIOException iioe) {
14771482
// don't change exception in case of timeouts or interrupts
14781483
throw iioe;
1484+
} catch (SocketException se) {
1485+
// don't change exception in case of SocketException
1486+
throw se;
14791487
} catch (IOException ioe) {
14801488
if (!(ioe instanceof SSLException)) {
14811489
throw new SSLException("readApplicationRecord", ioe);
@@ -1687,6 +1695,16 @@ private void handleException(Exception cause) throws IOException {
16871695
}
16881696
}
16891697

1698+
if (cause instanceof SocketException) {
1699+
try {
1700+
conContext.fatal(alert, cause);
1701+
} catch (Exception e) {
1702+
// Just delivering the fatal alert, re-throw the socket exception instead.
1703+
}
1704+
1705+
throw (SocketException)cause;
1706+
}
1707+
16901708
throw conContext.fatal(alert, cause);
16911709
}
16921710

‎src/java.base/share/classes/sun/security/ssl/SSLTransport.java

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.EOFException;
2929
import java.io.IOException;
3030
import java.io.InterruptedIOException;
31+
import java.net.SocketException;
3132
import java.nio.ByteBuffer;
3233
import javax.crypto.AEADBadTagException;
3334
import javax.crypto.BadPaddingException;
@@ -140,6 +141,9 @@ static Plaintext decode(TransportContext context,
140141
} catch (InterruptedIOException iioe) {
141142
// don't close the Socket in case of timeouts or interrupts.
142143
throw iioe;
144+
} catch (SocketException se) {
145+
// don't change exception in case of SocketException
146+
throw se;
143147
} catch (IOException ioe) {
144148
throw context.fatal(Alert.UNEXPECTED_MESSAGE, ioe);
145149
}

‎test/jdk/sun/security/ssl/SSLContextImpl/TrustTrustedCert.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ protected void runServerApplication(SSLSocket socket) throws Exception {
131131
sslIS.read();
132132
sslOS.write('A');
133133
sslOS.flush();
134-
} catch (SSLException ssle) {
134+
} catch (SSLException | SocketException se) {
135135
if (!expectFail) {
136-
throw ssle;
136+
throw se;
137137
} // Otherwise, ignore.
138138
}
139139
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright (c) 2017, 2020, Amazon and/or its affiliates. All rights reserved.
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
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8214339
27+
* @summary When a SocketException is thrown by the underlying layer, It
28+
* should be thrown as is and not be transformed to an SSLException.
29+
* @library /javax/net/ssl/templates
30+
* @run main/othervm SSLSocketShouldThrowSocketException
31+
*/
32+
33+
import java.io.*;
34+
import java.net.*;
35+
import java.util.*;
36+
import java.security.*;
37+
import javax.net.ssl.*;
38+
39+
import java.util.concurrent.CountDownLatch;
40+
import java.util.concurrent.TimeUnit;
41+
42+
public class SSLSocketShouldThrowSocketException extends SSLSocketTemplate {
43+
44+
boolean handshake;
45+
46+
private final CountDownLatch clientTerminatedCondition = new CountDownLatch(1);
47+
48+
SSLSocketShouldThrowSocketException(boolean handshake) {
49+
this.handshake = handshake;
50+
}
51+
52+
@Override
53+
protected boolean isCustomizedClientConnection() {
54+
return true;
55+
}
56+
57+
@Override
58+
protected void runServerApplication(SSLSocket socket) throws Exception {
59+
clientTerminatedCondition.await(30L, TimeUnit.SECONDS);
60+
}
61+
62+
@Override
63+
protected void runClientApplication(int serverPort) throws Exception {
64+
Socket baseSocket = new Socket("localhost", this.serverPort);
65+
66+
SSLSocketFactory sslsf =
67+
(SSLSocketFactory) SSLSocketFactory.getDefault();
68+
SSLSocket sslSocket = (SSLSocket)
69+
sslsf.createSocket(baseSocket, "localhost", serverPort, false);
70+
71+
if (this.handshake) {
72+
testHandshakeClose(baseSocket, sslSocket);
73+
} else {
74+
testDataClose(baseSocket, sslSocket);
75+
}
76+
77+
clientTerminatedCondition.countDown();
78+
79+
}
80+
81+
private void testHandshakeClose(Socket baseSocket, SSLSocket sslSocket) throws Exception {
82+
Thread aborter = new Thread() {
83+
@Override
84+
public void run() {
85+
86+
try {
87+
Thread.sleep(10);
88+
System.err.println("Closing the client socket : " + System.nanoTime());
89+
baseSocket.close();
90+
} catch (Exception ieo) {
91+
ieo.printStackTrace();
92+
}
93+
}
94+
};
95+
96+
aborter.start();
97+
98+
try {
99+
// handshaking
100+
System.err.println("Client starting handshake: " + System.nanoTime());
101+
sslSocket.startHandshake();
102+
throw new Exception("Start handshake did not throw an exception");
103+
} catch (SocketException se) {
104+
System.err.println("Caught Expected SocketException");
105+
}
106+
107+
aborter.join();
108+
}
109+
110+
private void testDataClose(Socket baseSocket, SSLSocket sslSocket) throws Exception{
111+
112+
CountDownLatch handshakeCondition = new CountDownLatch(1);
113+
114+
Thread aborter = new Thread() {
115+
@Override
116+
public void run() {
117+
118+
try {
119+
handshakeCondition.await(10L, TimeUnit.SECONDS);
120+
System.err.println("Closing the client socket : " + System.nanoTime());
121+
baseSocket.close();
122+
} catch (Exception ieo) {
123+
ieo.printStackTrace();
124+
}
125+
}
126+
};
127+
128+
aborter.start();
129+
130+
try {
131+
// handshaking
132+
System.err.println("Client starting handshake: " + System.nanoTime());
133+
sslSocket.startHandshake();
134+
handshakeCondition.countDown();
135+
System.err.println("Reading data from server");
136+
BufferedReader is = new BufferedReader(
137+
new InputStreamReader(sslSocket.getInputStream()));
138+
String data = is.readLine();
139+
throw new Exception("Start handshake did not throw an exception");
140+
} catch (SocketException se) {
141+
System.err.println("Caught Expected SocketException");
142+
}
143+
144+
aborter.join();
145+
}
146+
147+
public static void main(String[] args) throws Exception {
148+
// SocketException should be throws during a handshake phase.
149+
(new SSLSocketShouldThrowSocketException(true)).run();
150+
// SocketException should be throw during the application data phase.
151+
(new SSLSocketShouldThrowSocketException(false)).run();
152+
}
153+
}

‎test/jdk/sun/security/ssl/SSLSocketImpl/SSLExceptionForIOIssue.java ‎test/jdk/sun/security/ssl/SSLSocketImpl/SocketExceptionForSocketIssues.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@
3131
* @bug 8214339
3232
* @summary SSLSocketImpl erroneously wraps SocketException
3333
* @library /javax/net/ssl/templates
34-
* @run main/othervm SSLExceptionForIOIssue
34+
* @run main/othervm SocketExceptionForSocketIssues
3535
*/
3636

3737
import javax.net.ssl.*;
3838
import java.io.*;
3939
import java.net.*;
4040

41-
public class SSLExceptionForIOIssue implements SSLContextTemplate {
41+
public class SocketExceptionForSocketIssues implements SSLContextTemplate {
4242

4343
public static void main(String[] args) throws Exception {
4444
System.err.println("===================================");
45-
new SSLExceptionForIOIssue().test();
45+
new SocketExceptionForSocketIssues().test();
4646
}
4747

4848
private void test() throws Exception {
@@ -79,9 +79,9 @@ private void test() throws Exception {
7979
os.flush();
8080
} catch (SSLProtocolException | SSLHandshakeException sslhe) {
8181
throw sslhe;
82-
} catch (SSLException ssle) {
82+
} catch (SocketException se) {
8383
// the expected exception, ignore it
84-
System.err.println("server exception: " + ssle);
84+
System.err.println("server exception: " + se);
8585
} finally {
8686
if (listenSocket != null) {
8787
listenSocket.close();

0 commit comments

Comments
 (0)
Please sign in to comment.