1 /*
   2  * Copyright (c) 2005, 2011, 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 6223624
  30  * @ignore this test does not grant to work.  The handshake may have completed
  31  *        when getSession() return.  Please update or remove this test case.
  32  * @summary SSLSocket.setUseClientMode() fails to throw expected
  33  *        IllegalArgumentException
  34  * @run main/othervm SetClientMode
  35  */
  36 
  37 /*
  38  * Attempts to replicate a TCK test failure which creates SSLServerSockets
  39  * and then runs client threads which connect and start handshaking. Once
  40  * handshaking is begun the server side attempts to invoke
  41  * SSLSocket.setUseClientMode() on one or the other of the ends of the
  42  * connection, expecting an IllegalArgumentException.
  43  *
  44  * If the server side of the connection tries setUseClientMode() we
  45  * see the expected exception. If the setting is tried on the
  46  * client side SSLSocket, we do *not* see the exception, except
  47  * occasionally on the very first iteration.
  48  */
  49 
  50 import java.io.*;
  51 import java.lang.*;
  52 import java.net.*;
  53 import javax.net.ssl.*;
  54 import java.security.*;
  55 import java.security.cert.*;
  56 
  57 public class SetClientMode {
  58     private static String[] algorithms = {"TLS", "SSL", "SSLv3", "TLS"};
  59     volatile int serverPort = 0;
  60 
  61     /*
  62      * Where do we find the keystores?
  63      */
  64     static String pathToStores = "../../../../javax/net/ssl/etc";
  65     static String keyStoreFile = "keystore";
  66     static String trustStoreFile = "truststore";
  67     static String passwd = "passphrase";
  68 
  69 
  70     public SetClientMode() {
  71         // trivial constructor
  72     }
  73 
  74     public static void main(String[] args) throws Exception {
  75         String keyFilename =
  76             System.getProperty("test.src", "./") + "/" + pathToStores +
  77                 "/" + keyStoreFile;
  78         String trustFilename =
  79             System.getProperty("test.src", "./") + "/" + pathToStores +
  80                 "/" + trustStoreFile;
  81 
  82         System.setProperty("javax.net.ssl.keyStore", keyFilename);
  83         System.setProperty("javax.net.ssl.keyStorePassword", passwd);
  84         System.setProperty("javax.net.ssl.trustStore", trustFilename);
  85         System.setProperty("javax.net.ssl.trustStorePassword", passwd);
  86 
  87         new SetClientMode().run();
  88     }
  89 
  90     public void run() throws Exception {
  91         for (int i = 0; i < algorithms.length; i++) {
  92             testCombo( algorithms[i] );
  93         }
  94     }
  95 
  96     public void testCombo(String algorithm) throws Exception {
  97         Exception modeException = null ;
  98 
  99         // Create a server socket
 100         SSLServerSocketFactory ssf =
 101             (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
 102         SSLServerSocket serverSocket =
 103             (SSLServerSocket)ssf.createServerSocket(serverPort);
 104         serverPort = serverSocket.getLocalPort();
 105 
 106         // Create a client socket
 107         SSLSocketFactory sf = (SSLSocketFactory)SSLSocketFactory.getDefault();
 108         SSLSocket clientSocket = (SSLSocket)sf.createSocket(
 109                                 InetAddress.getLocalHost(),
 110                                 serverPort );
 111 
 112         // Create a client which will use the SSLSocket to talk to the server
 113         SocketClient client = new SocketClient(clientSocket);
 114 
 115         // Start the client and then accept any connection
 116         client.start();
 117 
 118         SSLSocket connectedSocket = (SSLSocket)serverSocket.accept();
 119 
 120         // force handshaking to complete
 121         connectedSocket.getSession();
 122 
 123         try {
 124             // Now try invoking setClientMode() on one
 125             // or the other of our two sockets. We expect
 126             // to see an IllegalArgumentException because
 127             // handshaking has begun.
 128             clientSocket.setUseClientMode(false);
 129 
 130             modeException = new Exception("no IllegalArgumentException");
 131         } catch (IllegalArgumentException iae) {
 132             System.out.println("succeeded, we can't set the client mode");
 133         } catch (Exception e) {
 134             modeException = e;
 135         } finally {
 136             // Shut down.
 137             connectedSocket.close();
 138             serverSocket.close();
 139 
 140             if (modeException != null) {
 141                 throw modeException;
 142             }
 143         }
 144 
 145         return;
 146     }
 147 
 148     // A thread-based client which does nothing except
 149     // start handshaking on the socket it's given.
 150     class SocketClient extends Thread {
 151         SSLSocket clientsideSocket;
 152         Exception clientException = null;
 153         boolean done = false;
 154 
 155         public SocketClient( SSLSocket s ) {
 156             clientsideSocket = s;
 157         }
 158 
 159         public void run() {
 160             try {
 161                 clientsideSocket.startHandshake();
 162 
 163                 // If we were to invoke setUseClientMode()
 164                 // here, the expected exception will happen.
 165                 //clientsideSocket.getSession();
 166                 //clientsideSocket.setUseClientMode( false );
 167             } catch ( Exception e ) {
 168                 e.printStackTrace();
 169                 clientException = e;
 170             } finally {
 171                 done = true;
 172                 try {
 173                     clientsideSocket.close();
 174                 } catch ( IOException e ) {
 175                     // eat it
 176                 }
 177             }
 178             return;
 179         }
 180 
 181         boolean isDone() {
 182             return done;
 183         }
 184 
 185         Exception getException() {
 186             return clientException;
 187         }
 188     }
 189 }