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  -Djdk.tls.rejectClientInitiatedRenegotiation=true
  32  *      NoImpactServerRenego SSLv3
  33  * @run main/othervm  -Djdk.tls.rejectClientInitiatedRenegotiation=true
  34  *      NoImpactServerRenego TLSv1
  35  * @run main/othervm  -Djdk.tls.rejectClientInitiatedRenegotiation=true
  36  *      NoImpactServerRenego TLSv1.1
  37  * @run main/othervm  -Djdk.tls.rejectClientInitiatedRenegotiation=true
  38  *      NoImpactServerRenego TLSv1.2
  39  */
  40 
  41 import java.io.*;
  42 import java.net.*;
  43 import java.security.Security;
  44 import javax.net.ssl.*;
  45 
  46 public class NoImpactServerRenego implements
  47         HandshakeCompletedListener {
  48 
  49     static byte handshakesCompleted = 0;
  50 
  51     /*
  52      * Define what happens when handshaking is completed
  53      */
  54     public void handshakeCompleted(HandshakeCompletedEvent event) {
  55         synchronized (this) {
  56             handshakesCompleted++;
  57             System.out.println("Session: " + event.getSession().toString());
  58             System.out.println("Seen handshake completed #" +
  59                 handshakesCompleted);
  60         }
  61     }
  62 
  63     /*
  64      * =============================================================
  65      * Set the various variables needed for the tests, then
  66      * specify what tests to run on each side.
  67      */
  68 
  69     /*
  70      * Should we run the client or server in a separate thread?
  71      * Both sides can throw exceptions, but do you have a preference
  72      * as to which side should be the main thread.
  73      */
  74     static boolean separateServerThread = false;
  75 
  76     /*
  77      * Where do we find the keystores?
  78      */
  79     static String pathToStores = "../../../../javax/net/ssl/etc";
  80     static String keyStoreFile = "keystore";
  81     static String trustStoreFile = "truststore";
  82     static String passwd = "passphrase";
  83 
  84     /*
  85      * Is the server ready to serve?
  86      */
  87     volatile static boolean serverReady = false;
  88 
  89     /*
  90      * Turn on SSL debugging?
  91      */
  92     static boolean debug = false;
  93 
  94     /*
  95      * If the client or server is doing some kind of object creation
  96      * that the other side depends on, and that thread prematurely
  97      * exits, you may experience a hang.  The test harness will
  98      * terminate all hung threads after its timeout has expired,
  99      * currently 3 minutes by default, but you might try to be
 100      * smart about it....
 101      */
 102 
 103     /*
 104      * Define the server side of the test.
 105      *
 106      * If the server prematurely exits, serverReady will be set to true
 107      * to avoid infinite hangs.
 108      */
 109     void doServerSide() throws Exception {
 110         SSLServerSocketFactory sslssf =
 111             (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
 112         SSLServerSocket sslServerSocket =
 113             (SSLServerSocket) sslssf.createServerSocket(serverPort);
 114 
 115         serverPort = sslServerSocket.getLocalPort();
 116 
 117         /*
 118          * Signal Client, we're ready for his connect.
 119          */
 120         serverReady = true;
 121 
 122         SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
 123         sslSocket.addHandshakeCompletedListener(this);
 124         InputStream sslIS = sslSocket.getInputStream();
 125         OutputStream sslOS = sslSocket.getOutputStream();
 126 
 127         for (int i = 0; i < 10; i++) {
 128             sslIS.read();
 129             sslOS.write(85);
 130             sslOS.flush();
 131         }
 132 
 133         System.out.println("invalidating");
 134         sslSocket.getSession().invalidate();
 135         System.out.println("starting new handshake");
 136         sslSocket.startHandshake();
 137 
 138         for (int i = 0; i < 10; i++) {
 139             System.out.println("sending/receiving data, iteration: " + i);
 140             sslIS.read();
 141             sslOS.write(85);
 142             sslOS.flush();
 143         }
 144 
 145         sslSocket.close();
 146     }
 147 
 148     /*
 149      * Define the client side of the test.
 150      *
 151      * If the server prematurely exits, serverReady will be set to true
 152      * to avoid infinite hangs.
 153      */
 154     void doClientSide() throws Exception {
 155 
 156         /*
 157          * Wait for server to get started.
 158          */
 159         while (!serverReady) {
 160             Thread.sleep(50);
 161         }
 162 
 163         SSLSocketFactory sslsf =
 164             (SSLSocketFactory) SSLSocketFactory.getDefault();
 165         SSLSocket sslSocket = (SSLSocket)
 166             sslsf.createSocket("localhost", serverPort);
 167         sslSocket.setEnabledProtocols(new String[] { tlsProtocol });
 168 
 169         InputStream sslIS = sslSocket.getInputStream();
 170         OutputStream sslOS = sslSocket.getOutputStream();
 171 
 172         for (int i = 0; i < 10; i++) {
 173             sslOS.write(280);
 174             sslOS.flush();
 175             sslIS.read();
 176         }
 177 
 178         for (int i = 0; i < 10; i++) {
 179             sslOS.write(280);
 180             sslOS.flush();
 181             sslIS.read();
 182         }
 183 
 184         sslSocket.close();
 185     }
 186 
 187     /*
 188      * =============================================================
 189      * The remainder is just support stuff
 190      */
 191 
 192     // use any free port by default
 193     volatile int serverPort = 0;
 194 
 195     volatile Exception serverException = null;
 196     volatile Exception clientException = null;
 197 
 198     // the specified protocol
 199     private static String tlsProtocol;
 200 
 201     public static void main(String[] args) throws Exception {
 202         String keyFilename =
 203             System.getProperty("test.src", "./") + "/" + pathToStores +
 204                 "/" + keyStoreFile;
 205         String trustFilename =
 206             System.getProperty("test.src", "./") + "/" + pathToStores +
 207                 "/" + trustStoreFile;
 208 
 209         System.setProperty("javax.net.ssl.keyStore", keyFilename);
 210         System.setProperty("javax.net.ssl.keyStorePassword", passwd);
 211         System.setProperty("javax.net.ssl.trustStore", trustFilename);
 212         System.setProperty("javax.net.ssl.trustStorePassword", passwd);
 213 
 214         if (debug) {
 215             System.setProperty("javax.net.debug", "all");
 216         }
 217 
 218         Security.setProperty("jdk.tls.disabledAlgorithms", "");
 219 
 220         tlsProtocol = args[0];
 221 
 222         /*
 223          * Start the tests.
 224          */
 225         new NoImpactServerRenego();
 226     }
 227 
 228     Thread clientThread = null;
 229     Thread serverThread = null;
 230 
 231     /*
 232      * Primary constructor, used to drive remainder of the test.
 233      *
 234      * Fork off the other side, then do your work.
 235      */
 236     NoImpactServerRenego() throws Exception {
 237         if (separateServerThread) {
 238             startServer(true);
 239             startClient(false);
 240         } else {
 241             startClient(true);
 242             startServer(false);
 243         }
 244 
 245         /*
 246          * Wait for other side to close down.
 247          */
 248         if (separateServerThread) {
 249             serverThread.join();
 250         } else {
 251             clientThread.join();
 252         }
 253 
 254         /*
 255          * When we get here, the test is pretty much over.
 256          *
 257          * If the main thread excepted, that propagates back
 258          * immediately.  If the other thread threw an exception, we
 259          * should report back.
 260          */
 261         if (serverException != null) {
 262             System.out.print("Server Exception:");
 263             throw serverException;
 264         }
 265         if (clientException != null) {
 266             System.out.print("Client Exception:");
 267             throw clientException;
 268         }
 269 
 270         /*
 271          * Give the Handshaker Thread a chance to run
 272          */
 273         Thread.sleep(1000);
 274 
 275         synchronized (this) {
 276             if (handshakesCompleted != 2) {
 277                 throw new Exception("Didn't see 2 handshake completed events.");
 278             }
 279         }
 280     }
 281 
 282     void startServer(boolean newThread) throws Exception {
 283         if (newThread) {
 284             serverThread = new Thread() {
 285                 public void run() {
 286                     try {
 287                         doServerSide();
 288                     } catch (Exception e) {
 289                         /*
 290                          * Our server thread just died.
 291                          *
 292                          * Release the client, if not active already...
 293                          */
 294                         System.err.println("Server died...");
 295                         serverReady = true;
 296                         serverException = e;
 297                     }
 298                 }
 299             };
 300             serverThread.start();
 301         } else {
 302             doServerSide();
 303         }
 304     }
 305 
 306     void startClient(boolean newThread) throws Exception {
 307         if (newThread) {
 308             clientThread = new Thread() {
 309                 public void run() {
 310                     try {
 311                         doClientSide();
 312                     } catch (Exception e) {
 313                         /*
 314                          * Our client thread just died.
 315                          */
 316                         System.err.println("Client died...");
 317                         clientException = e;
 318                     }
 319                 }
 320             };
 321             clientThread.start();
 322         } else {
 323             doClientSide();
 324         }
 325     }
 326 }