1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 // SunJSSE does not support dynamic system properties, no way to re-use
  25 // system properties in samevm/agentvm mode.
  26 
  27 /*
  28  * @test
  29  * @bug 7188658
  30  * @summary Add possibility to disable client initiated renegotiation
  31  * @run main/othervm
  32  *      -Djsse.rejectClientInitializedRenego=true NoImpactServerRenego
  33  */
  34 
  35 import java.io.*;
  36 import java.net.*;
  37 import javax.net.ssl.*;
  38 
  39 public class NoImpactServerRenego implements
  40         HandshakeCompletedListener {
  41 
  42     static byte handshakesCompleted = 0;
  43 
  44     /*
  45      * Define what happens when handshaking is completed
  46      */
  47     public void handshakeCompleted(HandshakeCompletedEvent event) {
  48         synchronized (this) {
  49             handshakesCompleted++;
  50             System.out.println("Session: " + event.getSession().toString());
  51             System.out.println("Seen handshake completed #" +
  52                 handshakesCompleted);
  53         }
  54     }
  55 
  56     /*
  57      * =============================================================
  58      * Set the various variables needed for the tests, then
  59      * specify what tests to run on each side.
  60      */
  61 
  62     /*
  63      * Should we run the client or server in a separate thread?
  64      * Both sides can throw exceptions, but do you have a preference
  65      * as to which side should be the main thread.
  66      */
  67     static boolean separateServerThread = false;
  68 
  69     /*
  70      * Where do we find the keystores?
  71      */
  72     static String pathToStores = "../../../../../../../etc";
  73     static String keyStoreFile = "keystore";
  74     static String trustStoreFile = "truststore";
  75     static String passwd = "passphrase";
  76 
  77     /*
  78      * Is the server ready to serve?
  79      */
  80     volatile static boolean serverReady = false;
  81 
  82     /*
  83      * Turn on SSL debugging?
  84      */
  85     static boolean debug = false;
  86 
  87     /*
  88      * If the client or server is doing some kind of object creation
  89      * that the other side depends on, and that thread prematurely
  90      * exits, you may experience a hang.  The test harness will
  91      * terminate all hung threads after its timeout has expired,
  92      * currently 3 minutes by default, but you might try to be
  93      * smart about it....
  94      */
  95 
  96     /*
  97      * Define the server side of the test.
  98      *
  99      * If the server prematurely exits, serverReady will be set to true
 100      * to avoid infinite hangs.
 101      */
 102     void doServerSide() throws Exception {
 103         SSLServerSocketFactory sslssf =
 104             (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
 105         SSLServerSocket sslServerSocket =
 106             (SSLServerSocket) sslssf.createServerSocket(serverPort);
 107 
 108         serverPort = sslServerSocket.getLocalPort();
 109 
 110         /*
 111          * Signal Client, we're ready for his connect.
 112          */
 113         serverReady = true;
 114 
 115         SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
 116         sslSocket.addHandshakeCompletedListener(this);
 117         InputStream sslIS = sslSocket.getInputStream();
 118         OutputStream sslOS = sslSocket.getOutputStream();
 119 
 120         for (int i = 0; i < 10; i++) {
 121             sslIS.read();
 122             sslOS.write(85);
 123             sslOS.flush();
 124         }
 125 
 126         System.out.println("invalidating");
 127         sslSocket.getSession().invalidate();
 128         System.out.println("starting new handshake");
 129         sslSocket.startHandshake();
 130 
 131         for (int i = 0; i < 10; i++) {
 132             System.out.println("sending/receiving data, iteration: " + i);
 133             sslIS.read();
 134             sslOS.write(85);
 135             sslOS.flush();
 136         }
 137 
 138         sslSocket.close();
 139     }
 140 
 141     /*
 142      * Define the client side of the test.
 143      *
 144      * If the server prematurely exits, serverReady will be set to true
 145      * to avoid infinite hangs.
 146      */
 147     void doClientSide() throws Exception {
 148 
 149         /*
 150          * Wait for server to get started.
 151          */
 152         while (!serverReady) {
 153             Thread.sleep(50);
 154         }
 155 
 156         SSLSocketFactory sslsf =
 157             (SSLSocketFactory) SSLSocketFactory.getDefault();
 158         SSLSocket sslSocket = (SSLSocket)
 159             sslsf.createSocket("localhost", serverPort);
 160 
 161         InputStream sslIS = sslSocket.getInputStream();
 162         OutputStream sslOS = sslSocket.getOutputStream();
 163 
 164         for (int i = 0; i < 10; i++) {
 165             sslOS.write(280);
 166             sslOS.flush();
 167             sslIS.read();
 168         }
 169 
 170         for (int i = 0; i < 10; i++) {
 171             sslOS.write(280);
 172             sslOS.flush();
 173             sslIS.read();
 174         }
 175 
 176         sslSocket.close();
 177     }
 178 
 179     /*
 180      * =============================================================
 181      * The remainder is just support stuff
 182      */
 183 
 184     // use any free port by default
 185     volatile int serverPort = 0;
 186 
 187     volatile Exception serverException = null;
 188     volatile Exception clientException = null;
 189 
 190     public static void main(String[] args) throws Exception {
 191         String keyFilename =
 192             System.getProperty("test.src", "./") + "/" + pathToStores +
 193                 "/" + keyStoreFile;
 194         String trustFilename =
 195             System.getProperty("test.src", "./") + "/" + pathToStores +
 196                 "/" + trustStoreFile;
 197 
 198         System.setProperty("javax.net.ssl.keyStore", keyFilename);
 199         System.setProperty("javax.net.ssl.keyStorePassword", passwd);
 200         System.setProperty("javax.net.ssl.trustStore", trustFilename);
 201         System.setProperty("javax.net.ssl.trustStorePassword", passwd);
 202 
 203         if (debug)
 204             System.setProperty("javax.net.debug", "all");
 205 
 206         /*
 207          * Start the tests.
 208          */
 209         new NoImpactServerRenego();
 210     }
 211 
 212     Thread clientThread = null;
 213     Thread serverThread = null;
 214 
 215     /*
 216      * Primary constructor, used to drive remainder of the test.
 217      *
 218      * Fork off the other side, then do your work.
 219      */
 220     NoImpactServerRenego() throws Exception {
 221         if (separateServerThread) {
 222             startServer(true);
 223             startClient(false);
 224         } else {
 225             startClient(true);
 226             startServer(false);
 227         }
 228 
 229         /*
 230          * Wait for other side to close down.
 231          */
 232         if (separateServerThread) {
 233             serverThread.join();
 234         } else {
 235             clientThread.join();
 236         }
 237 
 238         /*
 239          * When we get here, the test is pretty much over.
 240          *
 241          * If the main thread excepted, that propagates back
 242          * immediately.  If the other thread threw an exception, we
 243          * should report back.
 244          */
 245         if (serverException != null) {
 246             System.out.print("Server Exception:");
 247             throw serverException;
 248         }
 249         if (clientException != null) {
 250             System.out.print("Client Exception:");
 251             throw clientException;
 252         }
 253 
 254         /*
 255          * Give the Handshaker Thread a chance to run
 256          */
 257         Thread.sleep(1000);
 258 
 259         synchronized (this) {
 260             if (handshakesCompleted != 2) {
 261                 throw new Exception("Didn't see 2 handshake completed events.");
 262             }
 263         }
 264     }
 265 
 266     void startServer(boolean newThread) throws Exception {
 267         if (newThread) {
 268             serverThread = new Thread() {
 269                 public void run() {
 270                     try {
 271                         doServerSide();
 272                     } catch (Exception e) {
 273                         /*
 274                          * Our server thread just died.
 275                          *
 276                          * Release the client, if not active already...
 277                          */
 278                         System.err.println("Server died...");
 279                         serverReady = true;
 280                         serverException = e;
 281                     }
 282                 }
 283             };
 284             serverThread.start();
 285         } else {
 286             doServerSide();
 287         }
 288     }
 289 
 290     void startClient(boolean newThread) throws Exception {
 291         if (newThread) {
 292             clientThread = new Thread() {
 293                 public void run() {
 294                     try {
 295                         doClientSide();
 296                     } catch (Exception e) {
 297                         /*
 298                          * Our client thread just died.
 299                          */
 300                         System.err.println("Client died...");
 301                         clientException = e;
 302                     }
 303                 }
 304             };
 305             clientThread.start();
 306         } else {
 307             doClientSide();
 308         }
 309     }
 310 }