1 /* 2 * Copyright (c) 2018, Oracle 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 // Please run in othervm mode. SunJSSE does not support dynamic system 26 // properties, no way to re-use system properties in samevm/agentvm mode. 27 // 28 29 /* 30 * @test 31 * @bug 8209333 32 * @summary Socket reset issue for TLS 1.3 socket close 33 * @library /javax/net/ssl/templates 34 * @run main/othervm SSLSocketBruceForceClose 35 */ 36 37 import javax.net.ssl.*; 38 import java.io.*; 39 import java.net.InetAddress; 40 41 public class SSLSocketBruceForceClose implements SSLContextTemplate { 42 43 public static void main(String[] args) throws Exception { 44 for (int i = 0; i<= 10; i++) { 45 System.err.println("==================================="); 46 System.err.println("loop " + i); 47 System.err.println("==================================="); 48 new SSLSocketBruceForceClose().test(); 49 } 50 } 51 52 private void test() throws Exception { 53 SSLServerSocket listenSocket = null; 54 SSLSocket serverSocket = null; 55 ClientSocket clientSocket = null; 56 try { 57 SSLServerSocketFactory serversocketfactory = 58 createServerSSLContext().getServerSocketFactory(); 59 listenSocket = 60 (SSLServerSocket)serversocketfactory.createServerSocket(0); 61 listenSocket.setNeedClientAuth(false); 62 listenSocket.setEnableSessionCreation(true); 63 listenSocket.setUseClientMode(false); 64 65 66 System.err.println("Starting client"); 67 clientSocket = new ClientSocket(listenSocket.getLocalPort()); 68 clientSocket.start(); 69 70 System.err.println("Accepting client requests"); 71 serverSocket = (SSLSocket) listenSocket.accept(); 72 73 System.err.println("Reading data from client"); 74 BufferedReader serverReader = new BufferedReader( 75 new InputStreamReader(serverSocket.getInputStream())); 76 String data = serverReader.readLine(); 77 System.err.println("Received data from client: " + data); 78 79 System.err.println("Reading more data from client"); 80 data = serverReader.readLine(); 81 System.err.println("Received data from client: " + data); 82 } finally { 83 if (listenSocket != null) { 84 listenSocket.close(); 85 } 86 87 if (serverSocket != null) { 88 serverSocket.close(); 89 } 90 } 91 92 if (clientSocket != null && clientSocket.clientException != null) { 93 throw clientSocket.clientException; 94 } 95 } 96 97 private class ClientSocket extends Thread{ 98 int serverPort = 0; 99 Exception clientException; 100 101 public ClientSocket(int serverPort) { 102 this.serverPort = serverPort; 103 } 104 105 @Override 106 public void run() { 107 SSLSocket clientSocket = null; 108 String clientData = "Hi, I am client"; 109 try { 110 System.err.println( 111 "Connecting to server at port " + serverPort); 112 SSLSocketFactory sslSocketFactory = 113 createClientSSLContext().getSocketFactory(); 114 clientSocket = (SSLSocket)sslSocketFactory.createSocket( 115 InetAddress.getLocalHost(), serverPort); 116 clientSocket.setSoLinger(true, 3); 117 clientSocket.setSoTimeout(1000); 118 119 120 System.err.println("Sending data to server ..."); 121 122 BufferedWriter os = new BufferedWriter( 123 new OutputStreamWriter(clientSocket.getOutputStream())); 124 os.write(clientData, 0, clientData.length()); 125 os.newLine(); 126 os.flush(); 127 128 System.err.println("Sending more data to server ..."); 129 os.write(clientData, 0, clientData.length()); 130 os.newLine(); 131 os.flush(); 132 } catch (Exception e) { 133 clientException = e; 134 } finally { 135 if (clientSocket != null) { 136 try{ 137 clientSocket.close(); 138 System.err.println("client socket closed"); 139 } catch (IOException ioe) { 140 clientException = ioe; 141 } 142 } 143 } 144 } 145 } 146 } 147