Path: blob/master/test/jdk/javax/net/ssl/SSLSession/JSSERenegotiate.java
41152 views
/*1* Copyright (c) 2001, 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/*24* @test25* @bug 428033826* @summary "Unsupported SSL message version" SSLProtocolException27* w/SSL_RSA_WITH_NULL_MD528* @run main/othervm JSSERenegotiate29*30* SunJSSE does not support dynamic system properties, no way to re-use31* system properties in samevm/agentvm mode.32*33* @author Ram Marti34* @author Brad Wetmore35*/3637import java.io.*;38import java.net.*;39import java.security.Security;40import javax.net.ssl.*;4142public class JSSERenegotiate {4344static final String suite1 = "SSL_RSA_WITH_NULL_MD5";45static final String suite2 = "SSL_RSA_WITH_NULL_SHA";4647static final String dataString = "This is a test";484950/*51* =============================================================52* Set the various variables needed for the tests, then53* specify what tests to run on each side.54*/5556/*57* Should we run the client or server in a separate thread?58* Both sides can throw exceptions, but do you have a preference59* as to which side should be the main thread.60*/61static boolean separateServerThread = false;6263/*64* Where do we find the keystores?65*/66static String pathToStores = "../etc";67static String keyStoreFile = "keystore";68static String trustStoreFile = "truststore";69static String passwd = "passphrase";7071/*72* Is the server ready to serve?73*/74volatile static boolean serverReady = false;7576/*77* Turn on SSL debugging?78*/79static boolean debug = false;8081/*82* If the client or server is doing some kind of object creation83* that the other side depends on, and that thread prematurely84* exits, you may experience a hang. The test harness will85* terminate all hung threads after its timeout has expired,86* currently 3 minutes by default, but you might try to be87* smart about it....88*/8990/*91* Define the server side of the test.92*93* If the server prematurely exits, serverReady will be set to true94* to avoid infinite hangs.95*/96void doServerSide() throws Exception {97SSLServerSocketFactory sslssf =98(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();99SSLServerSocket sslServerSocket =100(SSLServerSocket) sslssf.createServerSocket(serverPort, 3);101102sslServerSocket.setNeedClientAuth(true);103sslServerSocket.setEnabledCipherSuites(new String[] {suite1, suite2 });104105serverPort = sslServerSocket.getLocalPort();106107/*108* Signal Client, we're ready for his connect.109*/110serverReady = true;111112SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();113114DataInputStream sslIS =115new DataInputStream(sslSocket.getInputStream());116DataOutputStream sslOS =117new DataOutputStream(sslSocket.getOutputStream());118while (true) {119try {120System.out.println("Received: " + sslIS.readUTF());121} catch (SSLException e) {122System.out.println ("Received wrong exception");123break;124} catch (IOException e) {125System.out.println ("Received right exception");126break;127}128}129sslSocket.close();130}131132/*133* Define the client side of the test.134*135* If the server prematurely exits, serverReady will be set to true136* to avoid infinite hangs.137*/138void doClientSide() throws Exception {139140/*141* Wait for server to get started.142*/143while (!serverReady) {144Thread.sleep(50);145}146147SSLSocketFactory sslsf =148(SSLSocketFactory) SSLSocketFactory.getDefault();149SSLSocket sslSocket = (SSLSocket)150sslsf.createSocket("localhost", serverPort);151152sslSocket.setEnabledCipherSuites(new String[] { suite1 });153System.out.println("Enabled " + suite1);154155DataInputStream sslIS =156new DataInputStream(sslSocket.getInputStream());157DataOutputStream sslOS =158new DataOutputStream(sslSocket.getOutputStream());159BufferedReader in = new BufferedReader(160new InputStreamReader(sslSocket.getInputStream()));161sslOS.writeUTF("With " + suite1);162163sslSocket.setEnabledCipherSuites(new String[] { suite2 });164sslSocket.startHandshake();165166System.out.println("Enabled " + suite2);167// write the message a few times - see bug 4462616 why we do this168sslOS.writeUTF("With " + suite2);169sslOS.writeUTF("With " + suite2);170sslOS.writeUTF("With " + suite2);171172sslSocket.setEnabledCipherSuites(new String[] { suite1 });173sslSocket.startHandshake();174System.out.println("Re-enabled " + suite1);175sslOS.writeUTF("With " + suite1);176sslOS.writeUTF("With " + suite1);177sslOS.writeUTF("With " + suite1);178sslSocket.close();179}180181/*182* =============================================================183* The remainder is just support stuff184*/185186// use any free port by default187volatile int serverPort = 0;188189volatile Exception serverException = null;190volatile Exception clientException = null;191192public static void main(String[] args) throws Exception {193// reset the security property to make sure that the cipher suites194// used in this test are not disabled195Security.setProperty("jdk.tls.disabledAlgorithms", "");196197String keyFilename =198System.getProperty("test.src", "./") + "/" + pathToStores +199"/" + keyStoreFile;200String trustFilename =201System.getProperty("test.src", "./") + "/" + pathToStores +202"/" + trustStoreFile;203204System.setProperty("javax.net.ssl.keyStore", keyFilename);205System.setProperty("javax.net.ssl.keyStorePassword", passwd);206System.setProperty("javax.net.ssl.trustStore", trustFilename);207System.setProperty("javax.net.ssl.trustStorePassword", passwd);208209if (debug)210System.setProperty("javax.net.debug", "all");211212/*213* Start the tests.214*/215new JSSERenegotiate();216}217218Thread clientThread = null;219Thread serverThread = null;220221/*222* Primary constructor, used to drive remainder of the test.223*224* Fork off the other side, then do your work.225*/226JSSERenegotiate() throws Exception {227try {228if (separateServerThread) {229startServer(true);230startClient(false);231} else {232startClient(true);233startServer(false);234}235} catch (Exception e) {236// swallow for now. Show later237}238239/*240* Wait for other side to close down.241*/242if (separateServerThread) {243serverThread.join();244} else {245clientThread.join();246}247248/*249* When we get here, the test is pretty much over.250* Which side threw the error?251*/252Exception local;253Exception remote;254String whichRemote;255256if (separateServerThread) {257remote = serverException;258local = clientException;259whichRemote = "server";260} else {261remote = clientException;262local = serverException;263whichRemote = "client";264}265266/*267* If both failed, return the curthread's exception, but also268* print the remote side Exception269*/270if ((local != null) && (remote != null)) {271System.out.println(whichRemote + " also threw:");272remote.printStackTrace();273System.out.println();274throw local;275}276277if (remote != null) {278throw remote;279}280281if (local != null) {282throw local;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 {306try {307doServerSide();308} catch (Exception e) {309serverException = e;310} finally {311serverReady = true;312}313}314}315316void startClient(boolean newThread) throws Exception {317if (newThread) {318clientThread = new Thread() {319public void run() {320try {321doClientSide();322} catch (Exception e) {323/*324* Our client thread just died.325*/326System.err.println("Client died...");327clientException = e;328}329}330};331clientThread.start();332} else {333try {334doClientSide();335} catch (Exception e) {336clientException = e;337}338}339}340}341342343