Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/InvalidateServerSessionRenegotiate.java
41152 views
/*1* Copyright (c) 2001, 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//24// SunJSSE does not support dynamic system properties, no way to re-use25// system properties in samevm/agentvm mode.26//2728/*29* @test30* @bug 4403428 819049231* @summary Invalidating JSSE session on server causes SSLProtocolException32* @run main/othervm InvalidateServerSessionRenegotiate SSLv333* @run main/othervm InvalidateServerSessionRenegotiate TLSv134* @run main/othervm InvalidateServerSessionRenegotiate TLSv1.135* @run main/othervm InvalidateServerSessionRenegotiate TLSv1.236* @author Brad Wetmore37*/3839import java.io.*;40import java.net.*;41import java.security.Security;42import javax.net.ssl.*;4344public class InvalidateServerSessionRenegotiate implements45HandshakeCompletedListener {4647static byte handshakesCompleted = 0;4849/*50* Define what happens when handshaking is completed51*/52public void handshakeCompleted(HandshakeCompletedEvent event) {53synchronized (this) {54handshakesCompleted++;55System.out.println("Session: " + event.getSession().toString());56System.out.println("Seen handshake completed #" +57handshakesCompleted);58}59}6061/*62* =============================================================63* Set the various variables needed for the tests, then64* specify what tests to run on each side.65*/6667/*68* Should we run the client or server in a separate thread?69* Both sides can throw exceptions, but do you have a preference70* as to which side should be the main thread.71*/72static boolean separateServerThread = false;7374/*75* Where do we find the keystores?76*/77static String pathToStores = "../../../../javax/net/ssl/etc";78static String keyStoreFile = "keystore";79static String trustStoreFile = "truststore";80static String passwd = "passphrase";8182/*83* Is the server ready to serve?84*/85volatile static boolean serverReady = false;8687/*88* Turn on SSL debugging?89*/90static boolean debug = false;9192/*93* If the client or server is doing some kind of object creation94* that the other side depends on, and that thread prematurely95* exits, you may experience a hang. The test harness will96* terminate all hung threads after its timeout has expired,97* currently 3 minutes by default, but you might try to be98* smart about it....99*/100101/*102* Define the server side of the test.103*104* If the server prematurely exits, serverReady will be set to true105* to avoid infinite hangs.106*/107void doServerSide() throws Exception {108SSLServerSocketFactory sslssf =109(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();110SSLServerSocket sslServerSocket =111(SSLServerSocket) sslssf.createServerSocket(serverPort);112113serverPort = sslServerSocket.getLocalPort();114115/*116* Signal Client, we're ready for his connect.117*/118serverReady = true;119120SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();121sslSocket.addHandshakeCompletedListener(this);122123// Enable all supported protocols on server side to test SSLv3124if ("SSLv3".equals(tlsProtocol)) {125sslSocket.setEnabledProtocols(sslSocket.getSupportedProtocols());126}127128InputStream sslIS = sslSocket.getInputStream();129OutputStream sslOS = sslSocket.getOutputStream();130131for (int i = 0; i < 10; i++) {132sslIS.read();133sslOS.write(85);134sslOS.flush();135}136137System.out.println("invalidating");138sslSocket.getSession().invalidate();139System.out.println("starting new handshake");140sslSocket.startHandshake();141142for (int i = 0; i < 10; i++) {143System.out.println("sending/receiving data, iteration: " + i);144sslIS.read();145sslOS.write(85);146sslOS.flush();147}148149sslSocket.close();150}151152/*153* Define the client side of the test.154*155* If the server prematurely exits, serverReady will be set to true156* to avoid infinite hangs.157*/158void doClientSide() throws Exception {159160/*161* Wait for server to get started.162*/163while (!serverReady) {164Thread.sleep(50);165}166167SSLSocketFactory sslsf =168(SSLSocketFactory) SSLSocketFactory.getDefault();169SSLSocket sslSocket = (SSLSocket)170sslsf.createSocket("localhost", serverPort);171sslSocket.setEnabledProtocols(new String[] { tlsProtocol });172173InputStream sslIS = sslSocket.getInputStream();174OutputStream sslOS = sslSocket.getOutputStream();175176for (int i = 0; i < 10; i++) {177sslOS.write(280);178sslOS.flush();179sslIS.read();180}181182for (int i = 0; i < 10; i++) {183sslOS.write(280);184sslOS.flush();185sslIS.read();186}187188sslSocket.close();189}190191/*192* =============================================================193* The remainder is just support stuff194*/195196// use any free port by default197volatile int serverPort = 0;198199volatile Exception serverException = null;200volatile Exception clientException = null;201202// the specified protocol203private static String tlsProtocol;204205public static void main(String[] args) throws Exception {206String keyFilename =207System.getProperty("test.src", "./") + "/" + pathToStores +208"/" + keyStoreFile;209String trustFilename =210System.getProperty("test.src", "./") + "/" + pathToStores +211"/" + trustStoreFile;212213System.setProperty("javax.net.ssl.keyStore", keyFilename);214System.setProperty("javax.net.ssl.keyStorePassword", passwd);215System.setProperty("javax.net.ssl.trustStore", trustFilename);216System.setProperty("javax.net.ssl.trustStorePassword", passwd);217218if (debug) {219System.setProperty("javax.net.debug", "all");220}221222Security.setProperty("jdk.tls.disabledAlgorithms", "");223224tlsProtocol = args[0];225226/*227* Start the tests.228*/229new InvalidateServerSessionRenegotiate();230}231232Thread clientThread = null;233Thread serverThread = null;234235/*236* Primary constructor, used to drive remainder of the test.237*238* Fork off the other side, then do your work.239*/240InvalidateServerSessionRenegotiate() throws Exception {241if (separateServerThread) {242startServer(true);243startClient(false);244} else {245startClient(true);246startServer(false);247}248249/*250* Wait for other side to close down.251*/252if (separateServerThread) {253serverThread.join();254} else {255clientThread.join();256}257258/*259* When we get here, the test is pretty much over.260*261* If the main thread excepted, that propagates back262* immediately. If the other thread threw an exception, we263* should report back.264*/265if (serverException != null) {266System.out.print("Server Exception:");267throw serverException;268}269if (clientException != null) {270System.out.print("Client Exception:");271throw clientException;272}273274/*275* Give the Handshaker Thread a chance to run276*/277Thread.sleep(1000);278279synchronized (this) {280if (handshakesCompleted != 2) {281throw new Exception("Didn't see 2 handshake completed events.");282}283}284}285286void startServer(boolean newThread) throws Exception {287if (newThread) {288serverThread = new Thread() {289public void run() {290try {291doServerSide();292} catch (Exception e) {293/*294* Our server thread just died.295*296* Release the client, if not active already...297*/298System.err.println("Server died...");299serverReady = true;300serverException = e;301}302}303};304serverThread.start();305} else {306doServerSide();307}308}309310void startClient(boolean newThread) throws Exception {311if (newThread) {312clientThread = new Thread() {313public void run() {314try {315doClientSide();316} catch (Exception e) {317/*318* Our client thread just died.319*/320System.err.println("Client died...");321clientException = e;322}323}324};325clientThread.start();326} else {327doClientSide();328}329}330}331332333