test/jdk/sun/security/ssl/X509TrustManagerImpl/CheckNullEntity.java
author xuelei
Fri, 01 Mar 2019 08:35:14 -0800
changeset 53972 43c2ab1bdfd3
parent 47216 71c04702a3d5
permissions -rw-r--r--
8215430: Remove the internal package com.sun.net.ssl Reviewed-by: chegar, mullan, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53972
43c2ab1bdfd3 8215430: Remove the internal package com.sun.net.ssl
xuelei
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 5053815
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary unspecified exceptions in X509TrustManager.checkClient[Server]Truste
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
d
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Xuelei Fan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class CheckNullEntity {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * Set the various variables needed for the tests, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * specify what tests to run on each side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * Should we run the client or server in a separate thread?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Both sides can throw exceptions, but do you have a preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * as to which side should be the main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static boolean separateServerThread = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Where do we find the keystores?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 5506
diff changeset
    57
    static String pathToStores = "../../../../javax/net/ssl/etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private void initialize() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        char[] passphrase = "passphrase".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        ks.load(new FileInputStream(trustFilename), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        for (Enumeration e = ks.aliases() ; e.hasMoreElements() ;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            String alias = (String)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (ks.isCertificateEntry(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                certChain[0] = (X509Certificate)ks.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        TrustManagerFactory tmf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            TrustManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        tmf.init(ks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        trustManager = (X509TrustManager)(tmf.getTrustManagers())[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * The remainder is just support stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
         * Start the tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        new CheckNullEntity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    X509Certificate[] certChain = {null, null};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    X509TrustManager trustManager = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Primary constructor, used to drive remainder of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Fork off the other side, then do your work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    CheckNullEntity() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        String authType = "RSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int failed = 0x3F; // indicate six tests for normal TM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int extFailed = 0x3F; // indicate six tests for extended TM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                trustManager.checkClientTrusted(certChain, (String)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                // get the right exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                failed >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                trustManager.checkServerTrusted(certChain, (String)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                // get the right exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                failed >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                trustManager.checkClientTrusted(certChain, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                // get the right exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                failed >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                trustManager.checkServerTrusted(certChain, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                // get the right exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                failed >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                trustManager.checkClientTrusted(null, authType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                // get the right exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                failed >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                trustManager.checkServerTrusted(null, authType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                // get the right exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                failed >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (trustManager instanceof X509ExtendedTrustManager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    ((X509ExtendedTrustManager)trustManager).checkClientTrusted(
53972
43c2ab1bdfd3 8215430: Remove the internal package com.sun.net.ssl
xuelei
parents: 47216
diff changeset
   157
                        certChain, (String)null, (Socket)null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    // get the right exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    extFailed >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    ((X509ExtendedTrustManager)trustManager).checkServerTrusted(
53972
43c2ab1bdfd3 8215430: Remove the internal package com.sun.net.ssl
xuelei
parents: 47216
diff changeset
   165
                        certChain, (String)null, (Socket)null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    // get the right exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    extFailed >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    ((X509ExtendedTrustManager)trustManager).checkClientTrusted(
53972
43c2ab1bdfd3 8215430: Remove the internal package com.sun.net.ssl
xuelei
parents: 47216
diff changeset
   173
                        certChain, "", (Socket)null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    // get the right exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    extFailed >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    ((X509ExtendedTrustManager)trustManager).checkServerTrusted(
53972
43c2ab1bdfd3 8215430: Remove the internal package com.sun.net.ssl
xuelei
parents: 47216
diff changeset
   181
                        certChain, "", (Socket)null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    // get the right exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    extFailed >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    ((X509ExtendedTrustManager)trustManager).checkClientTrusted(
53972
43c2ab1bdfd3 8215430: Remove the internal package com.sun.net.ssl
xuelei
parents: 47216
diff changeset
   189
                        null, authType, (Socket)null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    // get the right exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    extFailed >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    ((X509ExtendedTrustManager)trustManager).checkServerTrusted(
53972
43c2ab1bdfd3 8215430: Remove the internal package com.sun.net.ssl
xuelei
parents: 47216
diff changeset
   197
                        null, authType, (Socket)null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    // get the right exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    extFailed >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                extFailed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // IllegalArgumentException should be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            failed = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            System.out.println("Got another exception e" + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (failed != 0 || extFailed != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw new Exception("Should throw IllegalArgumentException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
}