1 /*
   2  * Copyright (c) 2005, 2007, 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 5053815
  27  * @summary unspecified exceptions in X509TrustManager.checkClient[Server]Truste
  28 d
  29  * @author Xuelei Fan
  30  * @modules java.base/com.sun.net.ssl.internal.ssl
  31  */
  32 
  33 import java.io.*;
  34 import java.net.*;
  35 import javax.net.ssl.*;
  36 import java.security.cert.X509Certificate;
  37 import java.security.*;
  38 import java.util.Enumeration;
  39 
  40 import com.sun.net.ssl.internal.ssl.X509ExtendedTrustManager;
  41 
  42 public class CheckNullEntity {
  43 
  44     /*
  45      * =============================================================
  46      * Set the various variables needed for the tests, then
  47      * specify what tests to run on each side.
  48      */
  49 
  50     /*
  51      * Should we run the client or server in a separate thread?
  52      * Both sides can throw exceptions, but do you have a preference
  53      * as to which side should be the main thread.
  54      */
  55     static boolean separateServerThread = true;
  56 
  57     /*
  58      * Where do we find the keystores?
  59      */
  60     static String pathToStores = "../../../../javax/net/ssl/etc";
  61     static String keyStoreFile = "keystore";
  62     static String trustStoreFile = "truststore";
  63     static String passwd = "passphrase";
  64 
  65     private void initialize() throws Exception {
  66         String trustFilename =
  67             System.getProperty("test.src", "./") + "/" + pathToStores +
  68                 "/" + trustStoreFile;
  69         char[] passphrase = "passphrase".toCharArray();
  70 
  71         KeyStore ks = KeyStore.getInstance("JKS");
  72         ks.load(new FileInputStream(trustFilename), passphrase);
  73 
  74         for (Enumeration e = ks.aliases() ; e.hasMoreElements() ;) {
  75             String alias = (String)e.nextElement();
  76             if (ks.isCertificateEntry(alias)) {
  77                 certChain[0] = (X509Certificate)ks.getCertificate(alias);
  78                 break;
  79             }
  80         }
  81 
  82         TrustManagerFactory tmf =
  83             TrustManagerFactory.getInstance("SunX509");
  84         tmf.init(ks);
  85 
  86         trustManager = (X509TrustManager)(tmf.getTrustManagers())[0];
  87     }
  88 
  89     /*
  90      * =============================================================
  91      * The remainder is just support stuff
  92      */
  93     public static void main(String[] args) throws Exception {
  94         /*
  95          * Start the tests.
  96          */
  97         new CheckNullEntity();
  98     }
  99 
 100     X509Certificate[] certChain = {null, null};
 101     X509TrustManager trustManager = null;
 102 
 103     /*
 104      * Primary constructor, used to drive remainder of the test.
 105      *
 106      * Fork off the other side, then do your work.
 107      */
 108     CheckNullEntity() throws Exception {
 109         String authType = "RSA";
 110         int failed = 0x3F; // indicate six tests for normal TM
 111         int extFailed = 0x3F; // indicate six tests for extended TM
 112 
 113         initialize();
 114         try {
 115             try {
 116                 trustManager.checkClientTrusted(certChain, (String)null);
 117             } catch (IllegalArgumentException iae) {
 118                 // get the right exception
 119                 failed >>= 1;
 120             }
 121 
 122             try {
 123                 trustManager.checkServerTrusted(certChain, (String)null);
 124             } catch (IllegalArgumentException iae) {
 125                 // get the right exception
 126                 failed >>= 1;
 127             }
 128 
 129             try {
 130                 trustManager.checkClientTrusted(certChain, "");
 131             } catch (IllegalArgumentException iae) {
 132                 // get the right exception
 133                 failed >>= 1;
 134             }
 135 
 136             try {
 137                 trustManager.checkServerTrusted(certChain, "");
 138             } catch (IllegalArgumentException iae) {
 139                 // get the right exception
 140                 failed >>= 1;
 141             }
 142 
 143             try {
 144                 trustManager.checkClientTrusted(null, authType);
 145             } catch (IllegalArgumentException iae) {
 146                 // get the right exception
 147                 failed >>= 1;
 148             }
 149 
 150             try {
 151                 trustManager.checkServerTrusted(null, authType);
 152             } catch (IllegalArgumentException iae) {
 153                 // get the right exception
 154                 failed >>= 1;
 155             }
 156 
 157             if (trustManager instanceof X509ExtendedTrustManager) {
 158                 try {
 159                     ((X509ExtendedTrustManager)trustManager).checkClientTrusted(
 160                         certChain, (String)null, "localhost", null);
 161                 } catch (IllegalArgumentException iae) {
 162                     // get the right exception
 163                     extFailed >>= 1;
 164                 }
 165 
 166                 try {
 167                     ((X509ExtendedTrustManager)trustManager).checkServerTrusted(
 168                         certChain, (String)null, "localhost", null);
 169                 } catch (IllegalArgumentException iae) {
 170                     // get the right exception
 171                     extFailed >>= 1;
 172                 }
 173 
 174                 try {
 175                     ((X509ExtendedTrustManager)trustManager).checkClientTrusted(
 176                         certChain, "", "localhost", null);
 177                 } catch (IllegalArgumentException iae) {
 178                     // get the right exception
 179                     extFailed >>= 1;
 180                 }
 181 
 182                 try {
 183                     ((X509ExtendedTrustManager)trustManager).checkServerTrusted(
 184                         certChain, "", "localhost", null);
 185                 } catch (IllegalArgumentException iae) {
 186                     // get the right exception
 187                     extFailed >>= 1;
 188                 }
 189 
 190                 try {
 191                     ((X509ExtendedTrustManager)trustManager).checkClientTrusted(
 192                         null, authType, "localhost", null);
 193                 } catch (IllegalArgumentException iae) {
 194                     // get the right exception
 195                     extFailed >>= 1;
 196                 }
 197 
 198                 try {
 199                     ((X509ExtendedTrustManager)trustManager).checkServerTrusted(
 200                         null, authType, "localhost", null);
 201                 } catch (IllegalArgumentException iae) {
 202                     // get the right exception
 203                     extFailed >>= 1;
 204                 }
 205             } else {
 206                 extFailed = 0;
 207             }
 208         } catch (NullPointerException npe) {
 209             // IllegalArgumentException should be thrown
 210             failed = 1;
 211         } catch (Exception e) {
 212             // ignore
 213             System.out.println("Got another exception e" + e);
 214         }
 215 
 216         if (failed != 0 || extFailed != 0) {
 217             throw new Exception("Should throw IllegalArgumentException");
 218         }
 219     }
 220 }