Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/NoImpactServerRenego.java
41152 views
/*1* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223// SunJSSE does not support dynamic system properties, no way to re-use24// system properties in samevm/agentvm mode.2526/*27* @test28* @bug 7188658 819049229* @summary Add possibility to disable client initiated renegotiation30* @run main/othervm -Djdk.tls.rejectClientInitiatedRenegotiation=true31* NoImpactServerRenego SSLv332* @run main/othervm -Djdk.tls.rejectClientInitiatedRenegotiation=true33* NoImpactServerRenego TLSv134* @run main/othervm -Djdk.tls.rejectClientInitiatedRenegotiation=true35* NoImpactServerRenego TLSv1.136* @run main/othervm -Djdk.tls.rejectClientInitiatedRenegotiation=true37* NoImpactServerRenego TLSv1.238*/3940import java.io.*;41import java.net.*;42import java.security.Security;43import javax.net.ssl.*;4445public class NoImpactServerRenego implements46HandshakeCompletedListener {4748static byte handshakesCompleted = 0;4950/*51* Define what happens when handshaking is completed52*/53public void handshakeCompleted(HandshakeCompletedEvent event) {54synchronized (this) {55handshakesCompleted++;56System.out.println("Session: " + event.getSession().toString());57System.out.println("Seen handshake completed #" +58handshakesCompleted);59}60}6162/*63* =============================================================64* Set the various variables needed for the tests, then65* specify what tests to run on each side.66*/6768/*69* Should we run the client or server in a separate thread?70* Both sides can throw exceptions, but do you have a preference71* as to which side should be the main thread.72*/73static boolean separateServerThread = false;7475/*76* Where do we find the keystores?77*/78static String pathToStores = "../../../../javax/net/ssl/etc";79static String keyStoreFile = "keystore";80static String trustStoreFile = "truststore";81static String passwd = "passphrase";8283/*84* Is the server ready to serve?85*/86volatile static boolean serverReady = false;8788/*89* Turn on SSL debugging?90*/91static boolean debug = false;9293/*94* If the client or server is doing some kind of object creation95* that the other side depends on, and that thread prematurely96* exits, you may experience a hang. The test harness will97* terminate all hung threads after its timeout has expired,98* currently 3 minutes by default, but you might try to be99* smart about it....100*/101102/*103* Define the server side of the test.104*105* If the server prematurely exits, serverReady will be set to true106* to avoid infinite hangs.107*/108void doServerSide() throws Exception {109SSLServerSocketFactory sslssf =110(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();111SSLServerSocket sslServerSocket =112(SSLServerSocket) sslssf.createServerSocket(serverPort);113114serverPort = sslServerSocket.getLocalPort();115116/*117* Signal Client, we're ready for his connect.118*/119serverReady = true;120121SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();122sslSocket.addHandshakeCompletedListener(this);123124// Enable all supported protocols on server side to test SSLv3125if ("SSLv3".equals(tlsProtocol)) {126sslSocket.setEnabledProtocols(sslSocket.getSupportedProtocols());127}128129InputStream sslIS = sslSocket.getInputStream();130OutputStream sslOS = sslSocket.getOutputStream();131132for (int i = 0; i < 10; i++) {133sslIS.read();134sslOS.write(85);135sslOS.flush();136}137138System.out.println("invalidating");139sslSocket.getSession().invalidate();140System.out.println("starting new handshake");141sslSocket.startHandshake();142143for (int i = 0; i < 10; i++) {144System.out.println("sending/receiving data, iteration: " + i);145sslIS.read();146sslOS.write(85);147sslOS.flush();148}149150sslSocket.close();151}152153/*154* Define the client side of the test.155*156* If the server prematurely exits, serverReady will be set to true157* to avoid infinite hangs.158*/159void doClientSide() throws Exception {160161/*162* Wait for server to get started.163*/164while (!serverReady) {165Thread.sleep(50);166}167168SSLSocketFactory sslsf =169(SSLSocketFactory) SSLSocketFactory.getDefault();170SSLSocket sslSocket = (SSLSocket)171sslsf.createSocket("localhost", serverPort);172sslSocket.setEnabledProtocols(new String[] { tlsProtocol });173174InputStream sslIS = sslSocket.getInputStream();175OutputStream sslOS = sslSocket.getOutputStream();176177for (int i = 0; i < 10; i++) {178sslOS.write(280);179sslOS.flush();180sslIS.read();181}182183for (int i = 0; i < 10; i++) {184sslOS.write(280);185sslOS.flush();186sslIS.read();187}188189sslSocket.close();190}191192/*193* =============================================================194* The remainder is just support stuff195*/196197// use any free port by default198volatile int serverPort = 0;199200volatile Exception serverException = null;201volatile Exception clientException = null;202203// the specified protocol204private static String tlsProtocol;205206public static void main(String[] args) throws Exception {207String keyFilename =208System.getProperty("test.src", "./") + "/" + pathToStores +209"/" + keyStoreFile;210String trustFilename =211System.getProperty("test.src", "./") + "/" + pathToStores +212"/" + trustStoreFile;213214System.setProperty("javax.net.ssl.keyStore", keyFilename);215System.setProperty("javax.net.ssl.keyStorePassword", passwd);216System.setProperty("javax.net.ssl.trustStore", trustFilename);217System.setProperty("javax.net.ssl.trustStorePassword", passwd);218219if (debug) {220System.setProperty("javax.net.debug", "all");221}222223Security.setProperty("jdk.tls.disabledAlgorithms", "");224225tlsProtocol = args[0];226227/*228* Start the tests.229*/230new NoImpactServerRenego();231}232233Thread clientThread = null;234Thread serverThread = null;235236/*237* Primary constructor, used to drive remainder of the test.238*239* Fork off the other side, then do your work.240*/241NoImpactServerRenego() throws Exception {242if (separateServerThread) {243startServer(true);244startClient(false);245} else {246startClient(true);247startServer(false);248}249250/*251* Wait for other side to close down.252*/253if (separateServerThread) {254serverThread.join();255} else {256clientThread.join();257}258259/*260* When we get here, the test is pretty much over.261*262* If the main thread excepted, that propagates back263* immediately. If the other thread threw an exception, we264* should report back.265*/266if (serverException != null) {267System.out.print("Server Exception:");268throw serverException;269}270if (clientException != null) {271System.out.print("Client Exception:");272throw clientException;273}274275/*276* Give the Handshaker Thread a chance to run277*/278Thread.sleep(1000);279280synchronized (this) {281if (handshakesCompleted != 2) {282throw new Exception("Didn't see 2 handshake completed events.");283}284}285}286287void startServer(boolean newThread) throws Exception {288if (newThread) {289serverThread = new Thread() {290public void run() {291try {292doServerSide();293} catch (Exception e) {294/*295* Our server thread just died.296*297* Release the client, if not active already...298*/299System.err.println("Server died...");300serverReady = true;301serverException = e;302}303}304};305serverThread.start();306} else {307doServerSide();308}309}310311void startClient(boolean newThread) throws Exception {312if (newThread) {313clientThread = new Thread() {314public void run() {315try {316doClientSide();317} catch (Exception e) {318/*319* Our client thread just died.320*/321System.err.println("Client died...");322clientException = e;323}324}325};326clientThread.start();327} else {328doClientSide();329}330}331}332333334