1 /*
   2  * Copyright (c) 2002, 2014, 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 /*
  25  * @test
  26  * @bug 4671289
  27  * @summary passing https.protocols from command line doesn't work.
  28  * @run main/othervm -Dhttps.protocols=SSLv3 HttpsProtocols
  29  * @author Brad Wetmore
  30  */
  31 
  32 import java.io.*;
  33 import java.net.*;
  34 import javax.net.ssl.*;
  35 import java.security.Security;
  36 
  37 public class HttpsProtocols implements HostnameVerifier {
  38 
  39     /*
  40      * =============================================================
  41      * Set the various variables needed for the tests, then
  42      * specify what tests to run on each side.
  43      */
  44 
  45     /*
  46      * Should we run the client or server in a separate thread?
  47      * Both sides can throw exceptions, but do you have a preference
  48      * as to which side should be the main thread.
  49      */
  50     static boolean separateServerThread = true;
  51 
  52     /*
  53      * Where do we find the keystores?
  54      */
  55     static String pathToStores = "../../../../javax/net/ssl/etc";
  56     static String keyStoreFile = "keystore";
  57     static String trustStoreFile = "truststore";
  58     static String passwd = "passphrase";
  59 
  60     /*
  61      * Is the server ready to serve?
  62      */
  63     volatile static boolean serverReady = false;
  64 
  65     /*
  66      * Turn on SSL debugging?
  67      */
  68     static boolean debug = false;
  69 
  70     /*
  71      * If the client or server is doing some kind of object creation
  72      * that the other side depends on, and that thread prematurely
  73      * exits, you may experience a hang.  The test harness will
  74      * terminate all hung threads after its timeout has expired,
  75      * currently 3 minutes by default, but you might try to be
  76      * smart about it....
  77      */
  78 
  79     /*
  80      * Define the server side of the test.
  81      *
  82      * If the server prematurely exits, serverReady will be set to true
  83      * to avoid infinite hangs.
  84      */
  85     void doServerSide() throws Exception {
  86         SSLServerSocketFactory sslssf =
  87             (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
  88         SSLServerSocket sslServerSocket =
  89             (SSLServerSocket) sslssf.createServerSocket(serverPort);
  90 
  91         serverPort = sslServerSocket.getLocalPort();
  92 
  93         /*
  94          * Signal Client, we're ready for his connect.
  95          */
  96         serverReady = true;
  97 
  98         SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
  99 
 100         DataOutputStream out =
 101                         new DataOutputStream(sslSocket.getOutputStream());
 102 
 103         BufferedReader in =
 104             new BufferedReader(new InputStreamReader(
 105             sslSocket.getInputStream()));
 106 
 107         // read the request
 108         readRequest(in);
 109 
 110         byte [] bytecodes = "Hello world".getBytes();
 111 
 112         out.writeBytes("HTTP/1.0 200 OK\r\n");
 113         out.writeBytes("Content-Length: " + bytecodes.length +
 114                                            "\r\n");
 115 
 116         out.writeBytes("Content-Type: text/html\r\n\r\n");
 117         out.write(bytecodes);
 118         out.flush();
 119 
 120         sslSocket.close();
 121     }
 122 
 123     /**
 124      * read the response, don't care for the syntax of the request-line
 125      */
 126     private static void readRequest(BufferedReader in)
 127         throws IOException {
 128         String line = null;
 129         do {
 130             line = in.readLine();
 131             System.out.println("Server received: " + line);
 132         } while ((line.length() != 0) &&
 133                 (line.charAt(0) != '\r') && (line.charAt(0) != '\n'));
 134     }
 135 
 136     /*
 137      * Define the client side of the test.
 138      *
 139      * If the server prematurely exits, serverReady will be set to true
 140      * to avoid infinite hangs.
 141      */
 142     void doClientSide() throws Exception {
 143 
 144         /*
 145          * Wait for server to get started.
 146          */
 147         while (!serverReady) {
 148             Thread.sleep(50);
 149         }
 150 
 151         HostnameVerifier reservedHV =
 152             HttpsURLConnection.getDefaultHostnameVerifier();
 153         try {
 154             HttpsURLConnection.setDefaultHostnameVerifier(this);
 155 
 156             URL url = new URL("https://localhost:" + serverPort + "/");
 157             HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
 158 
 159             System.out.println("response is " + urlc.getResponseCode());
 160         } finally {
 161             HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
 162         }
 163     }
 164 
 165     public boolean verify(String hostname, SSLSession session) {
 166         return true;
 167     }
 168 
 169     /*
 170      * =============================================================
 171      * The remainder is just support stuff
 172      */
 173 
 174     // use any free port by default
 175     volatile int serverPort = 0;
 176 
 177     volatile Exception serverException = null;
 178     volatile Exception clientException = null;
 179 
 180     public static void main(String[] args) throws Exception {
 181         // reset the security property to make sure that the algorithms
 182         // and keys used in this test are not disabled.
 183         Security.setProperty("jdk.tls.disabledAlgorithms", "");
 184 
 185         String keyFilename =
 186             System.getProperty("test.src", "./") + "/" + pathToStores +
 187                 "/" + keyStoreFile;
 188         String trustFilename =
 189             System.getProperty("test.src", "./") + "/" + pathToStores +
 190                 "/" + trustStoreFile;
 191 
 192         System.setProperty("javax.net.ssl.keyStore", keyFilename);
 193         System.setProperty("javax.net.ssl.keyStorePassword", passwd);
 194         System.setProperty("javax.net.ssl.trustStore", trustFilename);
 195         System.setProperty("javax.net.ssl.trustStorePassword", passwd);
 196 
 197         String prop = System.getProperty("https.protocols");
 198         System.out.println("protocols = " + prop);
 199 
 200         if ((prop == null) || (!prop.equals("SSLv3"))) {
 201             throw new Exception("https.protocols not set properly");
 202         }
 203 
 204         if (debug)
 205             System.setProperty("javax.net.debug", "all");
 206 
 207         /*
 208          * Start the tests.
 209          */
 210         new HttpsProtocols();
 211     }
 212 
 213     Thread clientThread = null;
 214     Thread serverThread = null;
 215 
 216     /*
 217      * Primary constructor, used to drive remainder of the test.
 218      *
 219      * Fork off the other side, then do your work.
 220      */
 221     HttpsProtocols() throws Exception {
 222         if (separateServerThread) {
 223             startServer(true);
 224             startClient(false);
 225         } else {
 226             startClient(true);
 227             startServer(false);
 228         }
 229 
 230         /*
 231          * Wait for other side to close down.
 232          */
 233         if (separateServerThread) {
 234             serverThread.join();
 235         } else {
 236             clientThread.join();
 237         }
 238 
 239         /*
 240          * When we get here, the test is pretty much over.
 241          *
 242          * If the main thread excepted, that propagates back
 243          * immediately.  If the other thread threw an exception, we
 244          * should report back.
 245          */
 246         if (serverException != null) {
 247             System.out.print("Server Exception:");
 248             throw serverException;
 249         }
 250         if (clientException != null) {
 251             System.out.print("Client Exception:");
 252             throw clientException;
 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             doServerSide();
 277         }
 278     }
 279 
 280     void startClient(boolean newThread) throws Exception {
 281         if (newThread) {
 282             clientThread = new Thread() {
 283                 public void run() {
 284                     try {
 285                         doClientSide();
 286                     } catch (Exception e) {
 287                         /*
 288                          * Our client thread just died.
 289                          */
 290                         System.err.println("Client died...");
 291                         clientException = e;
 292                     }
 293                 }
 294             };
 295             clientThread.start();
 296         } else {
 297             doClientSide();
 298         }
 299     }
 300 }