Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLSocketImpl/NoImpactServerRenego.java
38853 views
/*1* Copyright (c) 2013, 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 718865829* @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);123InputStream sslIS = sslSocket.getInputStream();124OutputStream sslOS = sslSocket.getOutputStream();125126for (int i = 0; i < 10; i++) {127sslIS.read();128sslOS.write(85);129sslOS.flush();130}131132System.out.println("invalidating");133sslSocket.getSession().invalidate();134System.out.println("starting new handshake");135sslSocket.startHandshake();136137for (int i = 0; i < 10; i++) {138System.out.println("sending/receiving data, iteration: " + i);139sslIS.read();140sslOS.write(85);141sslOS.flush();142}143144sslSocket.close();145}146147/*148* Define the client side of the test.149*150* If the server prematurely exits, serverReady will be set to true151* to avoid infinite hangs.152*/153void doClientSide() throws Exception {154155/*156* Wait for server to get started.157*/158while (!serverReady) {159Thread.sleep(50);160}161162SSLSocketFactory sslsf =163(SSLSocketFactory) SSLSocketFactory.getDefault();164SSLSocket sslSocket = (SSLSocket)165sslsf.createSocket("localhost", serverPort);166sslSocket.setEnabledProtocols(new String[] { tlsProtocol });167168InputStream sslIS = sslSocket.getInputStream();169OutputStream sslOS = sslSocket.getOutputStream();170171for (int i = 0; i < 10; i++) {172sslOS.write(280);173sslOS.flush();174sslIS.read();175}176177for (int i = 0; i < 10; i++) {178sslOS.write(280);179sslOS.flush();180sslIS.read();181}182183sslSocket.close();184}185186/*187* =============================================================188* The remainder is just support stuff189*/190191// use any free port by default192volatile int serverPort = 0;193194volatile Exception serverException = null;195volatile Exception clientException = null;196197// the specified protocol198private static String tlsProtocol;199200public static void main(String[] args) throws Exception {201String keyFilename =202System.getProperty("test.src", "./") + "/" + pathToStores +203"/" + keyStoreFile;204String trustFilename =205System.getProperty("test.src", "./") + "/" + pathToStores +206"/" + trustStoreFile;207208System.setProperty("javax.net.ssl.keyStore", keyFilename);209System.setProperty("javax.net.ssl.keyStorePassword", passwd);210System.setProperty("javax.net.ssl.trustStore", trustFilename);211System.setProperty("javax.net.ssl.trustStorePassword", passwd);212213if (debug) {214System.setProperty("javax.net.debug", "all");215}216217Security.setProperty("jdk.tls.disabledAlgorithms", "");218219tlsProtocol = args[0];220221/*222* Start the tests.223*/224new NoImpactServerRenego();225}226227Thread clientThread = null;228Thread serverThread = null;229230/*231* Primary constructor, used to drive remainder of the test.232*233* Fork off the other side, then do your work.234*/235NoImpactServerRenego() throws Exception {236if (separateServerThread) {237startServer(true);238startClient(false);239} else {240startClient(true);241startServer(false);242}243244/*245* Wait for other side to close down.246*/247if (separateServerThread) {248serverThread.join();249} else {250clientThread.join();251}252253/*254* When we get here, the test is pretty much over.255*256* If the main thread excepted, that propagates back257* immediately. If the other thread threw an exception, we258* should report back.259*/260if (serverException != null) {261System.out.print("Server Exception:");262throw serverException;263}264if (clientException != null) {265System.out.print("Client Exception:");266throw clientException;267}268269/*270* Give the Handshaker Thread a chance to run271*/272Thread.sleep(1000);273274synchronized (this) {275if (handshakesCompleted != 2) {276throw new Exception("Didn't see 2 handshake completed events.");277}278}279}280281void startServer(boolean newThread) throws Exception {282if (newThread) {283serverThread = new Thread() {284public void run() {285try {286doServerSide();287} catch (Exception e) {288/*289* Our server thread just died.290*291* Release the client, if not active already...292*/293System.err.println("Server died...");294serverReady = true;295serverException = e;296}297}298};299serverThread.start();300} else {301doServerSide();302}303}304305void startClient(boolean newThread) throws Exception {306if (newThread) {307clientThread = new Thread() {308public void run() {309try {310doClientSide();311} catch (Exception e) {312/*313* Our client thread just died.314*/315System.err.println("Client died...");316clientException = e;317}318}319};320clientThread.start();321} else {322doClientSide();323}324}325}326327328