test/jdk/sun/security/pkcs11/KeyStore/ClientAuth.java
author xuelei
Fri, 22 Jun 2018 15:37:46 -0700
branchJDK-8145252-TLS13-branch
changeset 56804 63ab0dfe3dbb
parent 47216 71c04702a3d5
permissions -rw-r--r--
set signature parameters after key initialization, and more
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 11521
diff changeset
     2
 * Copyright (c) 2003, 2015, 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
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
public class ClientAuth extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
     * Set the various variables needed for the tests, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
     * specify what tests to run on each side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static final String NSS_PWD = "test12";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static final String JKS_PWD = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static final String SERVER_KS = "server.keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final String TS = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static String p11config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static String DIR = System.getProperty("DIR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Should we run the client or server in a separate thread?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Both sides can throw exceptions, but do you have a preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * as to which side should be the main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static boolean separateServerThread = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Is the server ready to serve?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    volatile static boolean serverReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Turn on SSL debugging?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * If the client or server is doing some kind of object creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * that the other side depends on, and that thread prematurely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * exits, you may experience a hang.  The test harness will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * terminate all hung threads after its timeout has expired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * currently 3 minutes by default, but you might try to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * smart about it....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Define the server side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    void doServerSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        SSLContext ctx = SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        char[] passphrase = JKS_PWD.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // server gets KeyStore from JKS keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        ks.load(new FileInputStream(new File(DIR, SERVER_KS)), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        kmf.init(ks, passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // server gets TrustStore from PKCS#11 token
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        passphrase = NSS_PWD.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        KeyStore ts = KeyStore.getInstance("PKCS11", "SunPKCS11-nss");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        ts.load(null, passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        tmf.init(ts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        //ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        ctx.init(kmf.getKeyManagers(), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        ServerSocketFactory ssf = ctx.getServerSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        SSLServerSocket sslServerSocket = (SSLServerSocket)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                ssf.createServerSocket(serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        sslServerSocket.setNeedClientAuth(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        serverPort = sslServerSocket.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        System.out.println("serverPort = " + serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         * Signal Client, we're ready for his connect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        InputStream sslIS = sslSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        OutputStream sslOS = sslSocket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        sslIS.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        sslOS.write(85);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        sslOS.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        sslSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Define the client side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    void doClientSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         * Wait for server to get started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        while (!serverReady) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            Thread.sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        SSLContext ctx = SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // client gets KeyStore from PKCS#11 token,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // and gets TrustStore from JKS KeyStore (using system properties)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        char[] passphrase = NSS_PWD.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        KeyStore ks = KeyStore.getInstance("PKCS11", "SunPKCS11-nss");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        ks.load(null, passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        kmf = KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        kmf.init(ks, passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        ctx.init(kmf.getKeyManagers(), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        SSLSocketFactory sslsf = ctx.getSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        SSLSocket sslSocket = (SSLSocket)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            sslsf.createSocket("localhost", serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   157
        if (clientProtocol != null) {
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   158
            sslSocket.setEnabledProtocols(new String[] {clientProtocol});
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   159
        }
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   160
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   161
        if (clientCiperSuite != null) {
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   162
            sslSocket.setEnabledCipherSuites(new String[] {clientCiperSuite});
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   163
        }
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   164
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        InputStream sslIS = sslSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        OutputStream sslOS = sslSocket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        sslOS.write(280);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        sslOS.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        sslIS.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        sslSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * The remainder is just support stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    // use any free port by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    volatile int serverPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    volatile Exception serverException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    volatile Exception clientException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   186
    private static String clientProtocol = null;
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   187
    private static String clientCiperSuite = null;
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   188
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   189
    private static void parseArguments(String[] args) {
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   190
        if (args.length > 0) {
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   191
            clientProtocol = args[0];
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   192
        }
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   193
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   194
        if (args.length > 1) {
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   195
            clientCiperSuite = args[1];
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   196
        }
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   197
    }
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   198
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public static void main(String[] args) throws Exception {
56804
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 47216
diff changeset
   200
        Security.setProperty("jdk.tls.disabledAlgorithms", "");
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 47216
diff changeset
   201
        Security.setProperty("jdk.certpath.disabledAlgorithms", "");
63ab0dfe3dbb set signature parameters after key initialization, and more
xuelei
parents: 47216
diff changeset
   202
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   203
        // Get the customized arguments.
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 5506
diff changeset
   204
        parseArguments(args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        main(new ClientAuth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public void main(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        // SSL RSA client auth currently needs an RSA cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // (cf. NONEwithRSA hack), which is currently not available in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // open builds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            javax.crypto.Cipher.getInstance("RSA/ECB/PKCS1Padding", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            System.out.println("Not supported by provider, skipping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        this.provider = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        System.setProperty("javax.net.ssl.trustStore",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                        new File(DIR, TS).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        System.setProperty("javax.net.ssl.trustStoreType", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        System.setProperty("javax.net.ssl.trustStoreProvider", "SUN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        System.setProperty("javax.net.ssl.trustStorePassword", JKS_PWD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // perform Security.addProvider of P11 provider
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 11521
diff changeset
   228
        Security.addProvider(getSunPKCS11(System.getProperty("CUSTOM_P11_CONFIG")));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            System.setProperty("javax.net.debug", "all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
         * Start the tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        go();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    Thread clientThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    Thread serverThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Fork off the other side, then do your work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                startServer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                startClient(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                startClient(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                startServer(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            //swallow for now.  Show later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
         * Wait for other side to close down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            serverThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            clientThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         * When we get here, the test is pretty much over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         * Which side threw the error?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        Exception local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        Exception remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        String whichRemote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            remote = serverException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            local = clientException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            whichRemote = "server";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            remote = clientException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            local = serverException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            whichRemote = "client";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         * If both failed, return the curthread's exception, but also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         * print the remote side Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if ((local != null) && (remote != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            System.out.println(whichRemote + " also threw:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            remote.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            throw local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (remote != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            throw remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (local != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            throw local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    void startServer(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            serverThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                        doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                         * Our server thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                         * Release the client, if not active already...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                        System.err.println("Server died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                        serverException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            serverThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                serverException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    void startClient(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            clientThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                         * Our client thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                        System.err.println("Client died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        clientException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            clientThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                clientException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
}