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