1 /*
   2  * Copyright (c) 2002, 2019, 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 8190492
  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         // Enable all supported protocols on server side to test SSLv3
  92         sslServerSocket.setEnabledProtocols(sslServerSocket.getSupportedProtocols());
  93 
  94         serverPort = sslServerSocket.getLocalPort();
  95 
  96         /*
  97          * Signal Client, we're ready for his connect.
  98          */
  99         serverReady = true;
 100 
 101         SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
 102 
 103         DataOutputStream out =
 104                         new DataOutputStream(sslSocket.getOutputStream());
 105 
 106         BufferedReader in =
 107             new BufferedReader(new InputStreamReader(
 108             sslSocket.getInputStream()));
 109 
 110         // read the request
 111         readRequest(in);
 112 
 113         byte [] bytecodes = "Hello world".getBytes();
 114 
 115         out.writeBytes("HTTP/1.0 200 OK\r\n");
 116         out.writeBytes("Content-Length: " + bytecodes.length +
 117                                            "\r\n");
 118 
 119         out.writeBytes("Content-Type: text/html\r\n\r\n");
 120         out.write(bytecodes);
 121         out.flush();
 122 
 123         sslSocket.close();
 124     }
 125 
 126     /**
 127      * read the response, don't care for the syntax of the request-line
 128      */
 129     private static void readRequest(BufferedReader in)
 130         throws IOException {
 131         String line = null;
 132         do {
 133             line = in.readLine();
 134             System.out.println("Server received: " + line);
 135         } while ((line.length() != 0) &&
 136                 (line.charAt(0) != '\r') && (line.charAt(0) != '\n'));
 137     }
 138 
 139     /*
 140      * Define the client side of the test.
 141      *
 142      * If the server prematurely exits, serverReady will be set to true
 143      * to avoid infinite hangs.
 144      */
 145     void doClientSide() throws Exception {
 146 
 147         /*
 148          * Wait for server to get started.
 149          */
 150         while (!serverReady) {
 151             Thread.sleep(50);
 152         }
 153 
 154         HostnameVerifier reservedHV =
 155             HttpsURLConnection.getDefaultHostnameVerifier();
 156         try {
 157             HttpsURLConnection.setDefaultHostnameVerifier(this);
 158 
 159             URL url = new URL("https://localhost:" + serverPort + "/");
 160             HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
 161 
 162             System.out.println("response is " + urlc.getResponseCode());
 163         } finally {
 164             HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
 165         }
 166     }
 167 
 168     public boolean verify(String hostname, SSLSession session) {
 169         return true;
 170     }
 171 
 172     /*
 173      * =============================================================
 174      * The remainder is just support stuff
 175      */
 176 
 177     // use any free port by default
 178     volatile int serverPort = 0;
 179 
 180     volatile Exception serverException = null;
 181     volatile Exception clientException = null;
 182 
 183     public static void main(String[] args) throws Exception {
 184         // reset the security property to make sure that the algorithms
 185         // and keys used in this test are not disabled.
 186         Security.setProperty("jdk.tls.disabledAlgorithms", "");
 187 
 188         String keyFilename =
 189             System.getProperty("test.src", "./") + "/" + pathToStores +
 190                 "/" + keyStoreFile;
 191         String trustFilename =
 192             System.getProperty("test.src", "./") + "/" + pathToStores +
 193                 "/" + trustStoreFile;
 194 
 195         System.setProperty("javax.net.ssl.keyStore", keyFilename);
 196         System.setProperty("javax.net.ssl.keyStorePassword", passwd);
 197         System.setProperty("javax.net.ssl.trustStore", trustFilename);
 198         System.setProperty("javax.net.ssl.trustStorePassword", passwd);
 199 
 200         String prop = System.getProperty("https.protocols");
 201         System.out.println("protocols = " + prop);
 202 
 203         if ((prop == null) || (!prop.equals("SSLv3"))) {
 204             throw new Exception("https.protocols not set properly");
 205         }
 206 
 207         if (debug)
 208             System.setProperty("javax.net.debug", "all");
 209 
 210         /*
 211          * Start the tests.
 212          */
 213         new HttpsProtocols();
 214     }
 215 
 216     Thread clientThread = null;
 217     Thread serverThread = null;
 218 
 219     /*
 220      * Primary constructor, used to drive remainder of the test.
 221      *
 222      * Fork off the other side, then do your work.
 223      */
 224     HttpsProtocols() throws Exception {
 225         if (separateServerThread) {
 226             startServer(true);
 227             startClient(false);
 228         } else {
 229             startClient(true);
 230             startServer(false);
 231         }
 232 
 233         /*
 234          * Wait for other side to close down.
 235          */
 236         if (separateServerThread) {
 237             serverThread.join();
 238         } else {
 239             clientThread.join();
 240         }
 241 
 242         /*
 243          * When we get here, the test is pretty much over.
 244          *
 245          * If the main thread excepted, that propagates back
 246          * immediately.  If the other thread threw an exception, we
 247          * should report back.
 248          */
 249         if (serverException != null) {
 250             System.out.print("Server Exception:");
 251             throw serverException;
 252         }
 253         if (clientException != null) {
 254             System.out.print("Client Exception:");
 255             throw clientException;
 256         }
 257     }
 258 
 259     void startServer(boolean newThread) throws Exception {
 260         if (newThread) {
 261             serverThread = new Thread() {
 262                 public void run() {
 263                     try {
 264                         doServerSide();
 265                     } catch (Exception e) {
 266                         /*
 267                          * Our server thread just died.
 268                          *
 269                          * Release the client, if not active already...
 270                          */
 271                         System.err.println("Server died...");
 272                         serverReady = true;
 273                         serverException = e;
 274                     }
 275                 }
 276             };
 277             serverThread.start();
 278         } else {
 279             doServerSide();
 280         }
 281     }
 282 
 283     void startClient(boolean newThread) throws Exception {
 284         if (newThread) {
 285             clientThread = new Thread() {
 286                 public void run() {
 287                     try {
 288                         doClientSide();
 289                     } catch (Exception e) {
 290                         /*
 291                          * Our client thread just died.
 292                          */
 293                         System.err.println("Client died...");
 294                         clientException = e;
 295                     }
 296                 }
 297             };
 298             clientThread.start();
 299         } else {
 300             doClientSide();
 301         }
 302     }
 303 }