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