Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/ServerRenegoWithTwoVersions.java
41152 views
/*1* Copyright (c) 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// SunJSSE does not support dynamic system properties, no way to re-use25// system properties in samevm/agentvm mode.26//2728/*29* @test30* @bug 820864231* @summary Server initiated TLSv1.2 renegotiation fails if Java32* client allows TLSv1.333* @run main/othervm ServerRenegoWithTwoVersions TLSv1 SSLv334* @run main/othervm ServerRenegoWithTwoVersions TLSv1 TLSv1.135* @run main/othervm ServerRenegoWithTwoVersions TLSv1.2 TLSv1.136* @run main/othervm ServerRenegoWithTwoVersions TLSv1.3 TLSv1.237*/3839import java.io.*;40import java.net.*;41import java.security.Security;42import javax.net.ssl.*;4344public class ServerRenegoWithTwoVersions 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.setEnabledProtocols(new String[] { serverProtocol });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(167new String[] { serverProtocol, clientProtocol });168169InputStream sslIS = sslSocket.getInputStream();170OutputStream sslOS = sslSocket.getOutputStream();171172for (int i = 0; i < 10; i++) {173sslOS.write(280);174sslOS.flush();175sslIS.read();176}177178for (int i = 0; i < 10; i++) {179sslOS.write(280);180sslOS.flush();181sslIS.read();182}183184sslSocket.close();185}186187/*188* =============================================================189* The remainder is just support stuff190*/191192// use any free port by default193volatile int serverPort = 0;194195volatile Exception serverException = null;196volatile Exception clientException = null;197198// the specified protocol199private static String clientProtocol;200private static String serverProtocol;201202public static void main(String[] args) throws Exception {203String keyFilename =204System.getProperty("test.src", "./") + "/" + pathToStores +205"/" + keyStoreFile;206String trustFilename =207System.getProperty("test.src", "./") + "/" + pathToStores +208"/" + trustStoreFile;209210System.setProperty("javax.net.ssl.keyStore", keyFilename);211System.setProperty("javax.net.ssl.keyStorePassword", passwd);212System.setProperty("javax.net.ssl.trustStore", trustFilename);213System.setProperty("javax.net.ssl.trustStorePassword", passwd);214215if (debug) {216System.setProperty("javax.net.debug", "all");217}218219Security.setProperty("jdk.tls.disabledAlgorithms", "");220221clientProtocol = args[0];222serverProtocol = args[1];223224/*225* Start the tests.226*/227new ServerRenegoWithTwoVersions();228}229230Thread clientThread = null;231Thread serverThread = null;232233/*234* Primary constructor, used to drive remainder of the test.235*236* Fork off the other side, then do your work.237*/238ServerRenegoWithTwoVersions() throws Exception {239if (separateServerThread) {240startServer(true);241startClient(false);242} else {243startClient(true);244startServer(false);245}246247/*248* Wait for other side to close down.249*/250if (separateServerThread) {251serverThread.join();252} else {253clientThread.join();254}255256/*257* When we get here, the test is pretty much over.258*259* If the main thread excepted, that propagates back260* immediately. If the other thread threw an exception, we261* should report back.262*/263if (serverException != null) {264System.out.print("Server Exception:");265throw serverException;266}267if (clientException != null) {268System.out.print("Client Exception:");269throw clientException;270}271272/*273* Give the Handshaker Thread a chance to run274*/275Thread.sleep(1000);276277synchronized (this) {278if (handshakesCompleted != 2) {279throw new Exception("Didn't see 2 handshake completed events.");280}281}282}283284void startServer(boolean newThread) throws Exception {285if (newThread) {286serverThread = new Thread() {287public void run() {288try {289doServerSide();290} catch (Exception e) {291/*292* Our server thread just died.293*294* Release the client, if not active already...295*/296System.err.println("Server died...");297serverReady = true;298serverException = e;299}300}301};302serverThread.start();303} else {304doServerSide();305}306}307308void startClient(boolean newThread) throws Exception {309if (newThread) {310clientThread = new Thread() {311public void run() {312try {313doClientSide();314} catch (Exception e) {315/*316* Our client thread just died.317*/318System.err.println("Client died...");319clientException = e;320}321}322};323clientThread.start();324} else {325doClientSide();326}327}328}329330331