test/jdk/sun/security/ssl/X509TrustManagerImpl/ClientServer.java
branchniosocketimpl-branch
changeset 57250 b38f280d2114
parent 57249 3e39753ed05b
parent 54043 0324b3756aa2
child 57252 d70fc9bc1430
equal deleted inserted replaced
57249:3e39753ed05b 57250:b38f280d2114
     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 4717766
       
    27  * @author Brad Wetmore
       
    28  * @summary 1.0.3 JsseX509TrustManager erroneously calls isClientTrusted()
       
    29  * @modules java.base/com.sun.net.ssl
       
    30  * @run main/manual ClientServer
       
    31  */
       
    32 
       
    33 /*
       
    34  * SunJSSE does not support dynamic system properties, no way to re-use
       
    35  * system properties in samevm/agentvm mode.
       
    36  *
       
    37  * JSSE supports algorithm constraints with CR 6916074, need to update
       
    38  * this test case in JDK 7 soon.
       
    39  *
       
    40  * This problem didn't exist in JSSE 1.4, only JSSE 1.0.3.  However,
       
    41  * this is a useful test, so I decided to include it in 1.4.2.
       
    42  */
       
    43 
       
    44 import java.io.*;
       
    45 import java.net.*;
       
    46 import javax.net.ssl.*;
       
    47 import java.security.cert.*;
       
    48 import java.security.*;
       
    49 import com.sun.net.ssl.*;
       
    50 
       
    51 public class ClientServer {
       
    52 
       
    53     /*
       
    54      * =============================================================
       
    55      * Set the various variables needed for the tests, then
       
    56      * specify what tests to run on each side.
       
    57      */
       
    58 
       
    59     /*
       
    60      * Should we run the client or server in a separate thread?
       
    61      * Both sides can throw exceptions, but do you have a preference
       
    62      * as to which side should be the main thread.
       
    63      */
       
    64     static boolean separateServerThread = true;
       
    65 
       
    66     /*
       
    67      * Where do we find the keystores?
       
    68      */
       
    69     static String pathToStores = "../../../../javax/net/ssl/etc";
       
    70     static String keyStoreFile = "keystore";
       
    71     static String trustStoreFile = "truststore";
       
    72     static String passwd = "passphrase";
       
    73 
       
    74     /*
       
    75      * Is the server ready to serve?
       
    76      */
       
    77     volatile static boolean serverReady = false;
       
    78 
       
    79     /*
       
    80      * Turn on SSL debugging?
       
    81      */
       
    82     static boolean debug = false;
       
    83 
       
    84     /*
       
    85      * If the client or server is doing some kind of object creation
       
    86      * that the other side depends on, and that thread prematurely
       
    87      * exits, you may experience a hang.  The test harness will
       
    88      * terminate all hung threads after its timeout has expired,
       
    89      * currently 3 minutes by default, but you might try to be
       
    90      * smart about it....
       
    91      */
       
    92 
       
    93     /*
       
    94      * Define the server side of the test.
       
    95      *
       
    96      * If the server prematurely exits, serverReady will be set to true
       
    97      * to avoid infinite hangs.
       
    98      */
       
    99     void doServerSide() throws Exception {
       
   100         SSLServerSocketFactory sslssf = getDefaultServer();
       
   101         SSLServerSocket sslServerSocket =
       
   102             (SSLServerSocket) sslssf.createServerSocket(serverPort);
       
   103         serverPort = sslServerSocket.getLocalPort();
       
   104 
       
   105         /*
       
   106          * Signal Client, we're ready for his connect.
       
   107          */
       
   108         serverReady = true;
       
   109 
       
   110         SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
       
   111         sslSocket.setNeedClientAuth(true);
       
   112         InputStream sslIS = sslSocket.getInputStream();
       
   113         OutputStream sslOS = sslSocket.getOutputStream();
       
   114 
       
   115         sslIS.read();
       
   116         sslOS.write(85);
       
   117         sslOS.flush();
       
   118 
       
   119         sslSocket.close();
       
   120 
       
   121         if (!serverTM.wasServerChecked() && serverTM.wasClientChecked()) {
       
   122             System.out.println("SERVER TEST PASSED!");
       
   123         } else {
       
   124             throw new Exception("SERVER TEST FAILED!  " +
       
   125                 !serverTM.wasServerChecked() + " " +
       
   126                 serverTM.wasClientChecked());
       
   127         }
       
   128     }
       
   129 
       
   130     /*
       
   131      * Define the client side of the test.
       
   132      *
       
   133      * If the server prematurely exits, serverReady will be set to true
       
   134      * to avoid infinite hangs.
       
   135      */
       
   136     void doClientSide() throws Exception {
       
   137 
       
   138         /*
       
   139          * Wait for server to get started.
       
   140          */
       
   141         while (!serverReady) {
       
   142             Thread.sleep(50);
       
   143         }
       
   144 
       
   145         SSLSocketFactory sslsf = getDefaultClient();
       
   146         SSLSocket sslSocket = (SSLSocket)
       
   147             sslsf.createSocket("localhost", serverPort);
       
   148 
       
   149         InputStream sslIS = sslSocket.getInputStream();
       
   150         OutputStream sslOS = sslSocket.getOutputStream();
       
   151 
       
   152         sslOS.write(280);
       
   153         sslOS.flush();
       
   154         sslIS.read();
       
   155 
       
   156         sslSocket.close();
       
   157 
       
   158         if (clientTM.wasServerChecked() && !clientTM.wasClientChecked()) {
       
   159             System.out.println("CLIENT TEST PASSED!");
       
   160         } else {
       
   161             throw new Exception("CLIENT TEST FAILED!  " +
       
   162                 clientTM.wasServerChecked() + " " +
       
   163                 !clientTM.wasClientChecked());
       
   164         }
       
   165     }
       
   166 
       
   167     private com.sun.net.ssl.SSLContext getDefault(MyX509TM tm)
       
   168             throws Exception {
       
   169 
       
   170         String keyFilename =
       
   171             System.getProperty("test.src", "./") + "/" + pathToStores +
       
   172                 "/" + keyStoreFile;
       
   173         String trustFilename =
       
   174             System.getProperty("test.src", "./") + "/" + pathToStores +
       
   175                 "/" + trustStoreFile;
       
   176 
       
   177         char[] passphrase = "passphrase".toCharArray();
       
   178         KeyStore ks = KeyStore.getInstance("JKS");
       
   179         ks.load(new FileInputStream(keyFilename), passphrase);
       
   180 
       
   181         com.sun.net.ssl.KeyManagerFactory kmf =
       
   182             com.sun.net.ssl.KeyManagerFactory.getInstance("SunX509");
       
   183         kmf.init(ks, passphrase);
       
   184 
       
   185         ks = KeyStore.getInstance("JKS");
       
   186         ks.load(new FileInputStream(trustFilename), passphrase);
       
   187 
       
   188         com.sun.net.ssl.TrustManagerFactory tmf =
       
   189             com.sun.net.ssl.TrustManagerFactory.getInstance("SunX509");
       
   190         tmf.init(ks);
       
   191 
       
   192         com.sun.net.ssl.TrustManager [] tms = tmf.getTrustManagers();
       
   193 
       
   194         int i;
       
   195         for (i = 0; i < tms.length; i++) {
       
   196             if (tms[i] instanceof com.sun.net.ssl.X509TrustManager) {
       
   197                 break;
       
   198             }
       
   199         }
       
   200 
       
   201         if (i >= tms.length) {
       
   202             throw new Exception("Couldn't find X509TM");
       
   203         }
       
   204 
       
   205         tm.init((com.sun.net.ssl.X509TrustManager)tms[i]);
       
   206         tms = new MyX509TM [] { tm };
       
   207 
       
   208         com.sun.net.ssl.SSLContext ctx =
       
   209             com.sun.net.ssl.SSLContext.getInstance("TLS");
       
   210         ctx.init(kmf.getKeyManagers(), tms, null);
       
   211         return ctx;
       
   212     }
       
   213 
       
   214     MyX509TM serverTM;
       
   215     MyX509TM clientTM;
       
   216 
       
   217     private SSLServerSocketFactory getDefaultServer() throws Exception {
       
   218         serverTM = new MyX509TM();
       
   219         return getDefault(serverTM).getServerSocketFactory();
       
   220     }
       
   221 
       
   222     private SSLSocketFactory getDefaultClient() throws Exception {
       
   223         clientTM = new MyX509TM();
       
   224         return getDefault(clientTM).getSocketFactory();
       
   225     }
       
   226 
       
   227     static class MyX509TM implements com.sun.net.ssl.X509TrustManager {
       
   228 
       
   229         com.sun.net.ssl.X509TrustManager tm;
       
   230         boolean clientChecked;
       
   231         boolean serverChecked;
       
   232 
       
   233         void init(com.sun.net.ssl.X509TrustManager x509TM) {
       
   234             tm = x509TM;
       
   235         }
       
   236 
       
   237         public boolean wasClientChecked() {
       
   238             return clientChecked;
       
   239         }
       
   240 
       
   241         public boolean wasServerChecked() {
       
   242             return serverChecked;
       
   243         }
       
   244 
       
   245         public boolean isClientTrusted(X509Certificate[] chain) {
       
   246             clientChecked = true;
       
   247             return true;
       
   248         }
       
   249 
       
   250         public boolean isServerTrusted(X509Certificate[] chain) {
       
   251             serverChecked = true;
       
   252             return true;
       
   253         }
       
   254 
       
   255         public X509Certificate[] getAcceptedIssuers() {
       
   256             return tm.getAcceptedIssuers();
       
   257         }
       
   258     }
       
   259 
       
   260     /*
       
   261      * =============================================================
       
   262      * The remainder is just support stuff
       
   263      */
       
   264 
       
   265     // use any free port by default
       
   266     volatile int serverPort = 0;
       
   267 
       
   268     volatile Exception serverException = null;
       
   269     volatile Exception clientException = null;
       
   270 
       
   271     public static void main(String[] args) throws Exception {
       
   272 
       
   273         if (debug)
       
   274             System.setProperty("javax.net.debug", "all");
       
   275 
       
   276         /*
       
   277          * Start the tests.
       
   278          */
       
   279         new ClientServer();
       
   280     }
       
   281 
       
   282     Thread clientThread = null;
       
   283     Thread serverThread = null;
       
   284 
       
   285     /*
       
   286      * Primary constructor, used to drive remainder of the test.
       
   287      *
       
   288      * Fork off the other side, then do your work.
       
   289      */
       
   290     ClientServer() throws Exception {
       
   291         if (separateServerThread) {
       
   292             startServer(true);
       
   293             startClient(false);
       
   294         } else {
       
   295             startClient(true);
       
   296             startServer(false);
       
   297         }
       
   298 
       
   299         /*
       
   300          * Wait for other side to close down.
       
   301          */
       
   302         if (separateServerThread) {
       
   303             serverThread.join();
       
   304         } else {
       
   305             clientThread.join();
       
   306         }
       
   307 
       
   308         /*
       
   309          * When we get here, the test is pretty much over.
       
   310          *
       
   311          * If the main thread excepted, that propagates back
       
   312          * immediately.  If the other thread threw an exception, we
       
   313          * should report back.
       
   314          */
       
   315         if (serverException != null)
       
   316             throw serverException;
       
   317         if (clientException != null)
       
   318             throw clientException;
       
   319     }
       
   320 
       
   321     void startServer(boolean newThread) throws Exception {
       
   322         if (newThread) {
       
   323             serverThread = new Thread() {
       
   324                 public void run() {
       
   325                     try {
       
   326                         doServerSide();
       
   327                     } catch (Exception e) {
       
   328                         /*
       
   329                          * Our server thread just died.
       
   330                          *
       
   331                          * Release the client, if not active already...
       
   332                          */
       
   333                         System.err.println("Server died...");
       
   334                         serverReady = true;
       
   335                         serverException = e;
       
   336                     }
       
   337                 }
       
   338             };
       
   339             serverThread.start();
       
   340         } else {
       
   341             doServerSide();
       
   342         }
       
   343     }
       
   344 
       
   345     void startClient(boolean newThread) throws Exception {
       
   346         if (newThread) {
       
   347             clientThread = new Thread() {
       
   348                 public void run() {
       
   349                     try {
       
   350                         doClientSide();
       
   351                     } catch (Exception e) {
       
   352                         /*
       
   353                          * Our client thread just died.
       
   354                          */
       
   355                         System.err.println("Client died...");
       
   356                         clientException = e;
       
   357                     }
       
   358                 }
       
   359             };
       
   360             clientThread.start();
       
   361         } else {
       
   362             doClientSide();
       
   363         }
       
   364     }
       
   365 }