Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/RejectClientRenego.java
41152 views
/*1* Copyright (c) 2013, 2018, 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 RejectClientRenego true SSLv331* @run main/othervm RejectClientRenego false SSLv332* @run main/othervm RejectClientRenego true TLSv133* @run main/othervm RejectClientRenego false TLSv134* @run main/othervm RejectClientRenego true TLSv1.135* @run main/othervm RejectClientRenego false TLSv1.136* @run main/othervm RejectClientRenego true TLSv1.237* @run main/othervm RejectClientRenego false TLSv1.238*/3940import java.io.*;41import java.net.*;42import java.security.Security;43import javax.net.ssl.*;4445public class RejectClientRenego 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.setEnabledProtocols(new String[] { tlsProtocol });123sslSocket.addHandshakeCompletedListener(this);124InputStream sslIS = sslSocket.getInputStream();125OutputStream sslOS = sslSocket.getOutputStream();126127for (int i = 0; i < 10; i++) {128sslIS.read();129sslOS.write(85);130sslOS.flush();131}132133try {134for (int i = 0; i < 10; i++) {135System.out.println("sending/receiving data, iteration: " + i);136sslIS.read();137sslOS.write(85);138sslOS.flush();139}140throw new Exception("Not reject client initialized renegotiation");141} catch (IOException ioe) {142System.out.println("Got the expected exception");143} finally {144sslSocket.close();145}146}147148/*149* Define the client side of the test.150*151* If the server prematurely exits, serverReady will be set to true152* to avoid infinite hangs.153*/154void doClientSide() throws Exception {155156/*157* Wait for server to get started.158*/159while (!serverReady) {160Thread.sleep(50);161}162163SSLSocketFactory sslsf =164(SSLSocketFactory) SSLSocketFactory.getDefault();165SSLSocket sslSocket = (SSLSocket)166sslsf.createSocket("localhost", serverPort);167sslSocket.setEnabledProtocols(new String[] { tlsProtocol });168169InputStream sslIS = sslSocket.getInputStream();170OutputStream sslOS = sslSocket.getOutputStream();171172for (int i = 0; i < 10; i++) {173sslOS.write(280);174sslOS.flush();175sslIS.read();176}177178if (!isAbbreviated) {179System.out.println("invalidating");180sslSocket.getSession().invalidate();181}182System.out.println("starting new handshake");183sslSocket.startHandshake();184185try {186for (int i = 0; i < 10; i++) {187sslOS.write(280);188sslOS.flush();189sslIS.read();190}191throw new Exception("Not reject client initialized renegotiation");192} catch (IOException ioe) {193System.out.println("Got the expected exception");194} finally {195sslSocket.close();196}197}198199/*200* =============================================================201* The remainder is just support stuff202*/203204// use any free port by default205volatile int serverPort = 0;206207volatile Exception serverException = null;208volatile Exception clientException = null;209210// Is it abbreviated handshake?211private static boolean isAbbreviated = false;212213// the specified protocol214private static String tlsProtocol;215216public static void main(String[] args) throws Exception {217String keyFilename =218System.getProperty("test.src", "./") + "/" + pathToStores +219"/" + keyStoreFile;220String trustFilename =221System.getProperty("test.src", "./") + "/" + pathToStores +222"/" + trustStoreFile;223224System.setProperty("javax.net.ssl.keyStore", keyFilename);225System.setProperty("javax.net.ssl.keyStorePassword", passwd);226System.setProperty("javax.net.ssl.trustStore", trustFilename);227System.setProperty("javax.net.ssl.trustStorePassword", passwd);228229// reject client initialized SSL renegotiation.230System.setProperty(231"jdk.tls.rejectClientInitiatedRenegotiation", "true");232233if (debug) {234System.setProperty("javax.net.debug", "all");235}236237Security.setProperty("jdk.tls.disabledAlgorithms", "");238239// Is it abbreviated handshake?240if ("true".equals(args[0])) {241isAbbreviated = true;242}243244tlsProtocol = args[1];245246/*247* Start the tests.248*/249new RejectClientRenego();250}251252Thread clientThread = null;253Thread serverThread = null;254255/*256* Primary constructor, used to drive remainder of the test.257*258* Fork off the other side, then do your work.259*/260RejectClientRenego() throws Exception {261if (separateServerThread) {262startServer(true);263startClient(false);264} else {265startClient(true);266startServer(false);267}268269/*270* Wait for other side to close down.271*/272if (separateServerThread) {273serverThread.join();274} else {275clientThread.join();276}277278/*279* When we get here, the test is pretty much over.280*281* If the main thread excepted, that propagates back282* immediately. If the other thread threw an exception, we283* should report back.284*/285if (serverException != null) {286System.out.print("Server Exception:");287throw serverException;288}289if (clientException != null) {290System.out.print("Client Exception:");291throw clientException;292}293}294295void startServer(boolean newThread) throws Exception {296if (newThread) {297serverThread = new Thread() {298public void run() {299try {300doServerSide();301} catch (Exception e) {302/*303* Our server thread just died.304*305* Release the client, if not active already...306*/307System.err.println("Server died...");308serverReady = true;309serverException = e;310}311}312};313serverThread.start();314} else {315doServerSide();316}317}318319void startClient(boolean newThread) throws Exception {320if (newThread) {321clientThread = new Thread() {322public void run() {323try {324doClientSide();325} catch (Exception e) {326/*327* Our client thread just died.328*/329System.err.println("Client died...");330clientException = e;331}332}333};334clientThread.start();335} else {336doClientSide();337}338}339}340341342