jdk/test/javax/net/ssl/SSLSession/CheckMyTrustedKeystore.java
author prr
Thu, 18 Dec 2014 10:45:45 -0800
changeset 29908 83e2c403fefd
parent 23052 241885315119
child 30820 0d4717a011d3
permissions -rw-r--r--
8067050: Better font consistency checking Reviewed-by: bae, srl, mschoene
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10328
diff changeset
     2
 * Copyright (c) 2001, 2011, 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
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
    24
// SunJSSE does not support dynamic system properties, no way to re-use
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
    25
// system properties in samevm/agentvm mode.
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
    26
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @bug 4329114
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @summary Need better way of reflecting the reason when a chain is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *      rejected as untrusted.
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
    32
 * @ignore JSSE supports algorithm constraints with CR 6916074,
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
    33
 *      need to update this test case in JDK 7 soon
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 7043
diff changeset
    34
 * @run main/othervm CheckMyTrustedKeystore
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 7043
diff changeset
    35
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author Brad Wetmore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
    39
// This is a serious hack job!
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class CheckMyTrustedKeystore {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Set the various variables needed for the tests, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * specify what tests to run on each side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Should we run the client or server in a separate thread?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Both sides can throw exceptions, but do you have a preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * as to which side should be the main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static boolean separateServerThread = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Where do we find the keystores?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
    65
    final static String pathToStores = "../etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    final static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    final static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    final static String unknownStoreFile = "unknown_keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    final static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    final static char[] cpasswd = "passphrase".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Is the server ready to serve?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    volatile static boolean serverReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Turn on SSL debugging?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    final static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * If the client or server is doing some kind of object creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * that the other side depends on, and that thread prematurely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * exits, you may experience a hang.  The test harness will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * terminate all hung threads after its timeout has expired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * currently 3 minutes by default, but you might try to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * smart about it....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Define the server side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    void doServerSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        com.sun.net.ssl.SSLContext ctx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            com.sun.net.ssl.SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        com.sun.net.ssl.KeyManagerFactory kmf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            com.sun.net.ssl.KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        ks.load(new FileInputStream(keyFilename), cpasswd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        kmf.init(ks, cpasswd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        com.sun.net.ssl.TrustManager [] tms =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            new com.sun.net.ssl.TrustManager []
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            { new MyComX509TrustManager() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        ctx.init(kmf.getKeyManagers(), tms, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        SSLServerSocketFactory sslssf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            (SSLServerSocketFactory) ctx.getServerSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        SSLServerSocket sslServerSocket =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            (SSLServerSocket) sslssf.createServerSocket(serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        serverPort = sslServerSocket.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        sslServerSocket.setNeedClientAuth(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
         * Create using the other type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        SSLContext ctx1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        KeyManagerFactory kmf1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        TrustManager [] tms1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            new TrustManager []
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            { new MyJavaxX509TrustManager() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        kmf1.init(ks, cpasswd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        ctx1.init(kmf1.getKeyManagers(), tms1, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        sslssf = (SSLServerSocketFactory) ctx1.getServerSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        SSLServerSocket sslServerSocket1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            (SSLServerSocket) sslssf.createServerSocket(serverPort1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        serverPort1 = sslServerSocket1.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        sslServerSocket1.setNeedClientAuth(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         * Signal Client, we're ready for his connect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        sslServerSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        serverReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        InputStream sslIS = sslSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        OutputStream sslOS = sslSocket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        sslIS.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        sslOS.write(85);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        sslOS.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        sslSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        sslSocket = (SSLSocket) sslServerSocket1.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        sslIS = sslSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        sslOS = sslSocket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        sslIS.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        sslOS.write(85);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        sslOS.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        sslSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        System.out.println("Server exiting!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        System.out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    void doTest(SSLSocket sslSocket) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        InputStream sslIS = sslSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        OutputStream sslOS = sslSocket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        System.out.println("  Writing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        sslOS.write(280);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        sslOS.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        System.out.println("  Reading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        sslIS.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        sslSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Define the client side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    void doClientSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
         * Wait for server to get started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        while (!serverReady) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            Thread.sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
         * See if an unknown keystore actually gets checked ok.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        System.out.println("==============");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        System.out.println("Starting test0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        KeyStore uks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        SSLContext ctx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        KeyManagerFactory kmf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        uks.load(new FileInputStream(unknownFilename), cpasswd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        kmf.init(uks, cpasswd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        TrustManager [] tms = new TrustManager []
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            { new MyJavaxX509TrustManager() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        ctx.init(kmf.getKeyManagers(), tms, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        SSLSocketFactory sslsf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            (SSLSocketFactory) ctx.getSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        System.out.println("Trying first socket " + serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        SSLSocket sslSocket = (SSLSocket)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            sslsf.createSocket("localhost", serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        doTest(sslSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         * Now try the other way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        com.sun.net.ssl.SSLContext ctx1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            com.sun.net.ssl.SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        com.sun.net.ssl.KeyManagerFactory kmf1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            com.sun.net.ssl.KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        kmf1.init(uks, cpasswd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        com.sun.net.ssl.TrustManager [] tms1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            new com.sun.net.ssl.TrustManager []
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            { new MyComX509TrustManager() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        ctx1.init(kmf1.getKeyManagers(), tms1, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        sslsf = (SSLSocketFactory) ctx1.getSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        System.out.println("Trying second socket " + serverPort1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        sslSocket = (SSLSocket) sslsf.createSocket("localhost",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            serverPort1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        doTest(sslSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        System.out.println("Completed test1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * The remainder is just support stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    int serverPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    int serverPort1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    volatile Exception serverException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    volatile Exception clientException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    final static String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    final static String unknownFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        "/" + unknownStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            System.setProperty("javax.net.debug", "all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
         * Start the tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        new CheckMyTrustedKeystore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    Thread clientThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    Thread serverThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Primary constructor, used to drive remainder of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Fork off the other side, then do your work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    CheckMyTrustedKeystore() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            startServer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            startClient(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            startClient(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            startServer(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
         * Wait for other side to close down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            serverThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            clientThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         * When we get here, the test is pretty much over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         * If the main thread excepted, that propagates back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         * immediately.  If the other thread threw an exception, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         * should report back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (serverException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            System.out.print("Server Exception:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            throw serverException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (clientException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            System.out.print("Client Exception:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throw clientException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    void startServer(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            serverThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                         * Our server thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                         * Release the client, if not active already...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        System.err.println("Server died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        serverException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            serverThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    void startClient(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            clientThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                         * Our client thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        System.err.println("Client died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        clientException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            clientThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
class MyComX509TrustManager implements com.sun.net.ssl.X509TrustManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public X509Certificate[] getAcceptedIssuers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return (new X509Certificate[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public boolean isClientTrusted(X509Certificate[] chain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        System.out.println("    IsClientTrusted?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    public boolean isServerTrusted(X509Certificate[] chain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        System.out.println("    IsServerTrusted?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
class MyJavaxX509TrustManager implements X509TrustManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public X509Certificate[] getAcceptedIssuers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return (new X509Certificate[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public void checkClientTrusted(X509Certificate[] chain, String authType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        System.out.println("    CheckClientTrusted(" + authType + ")?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    public void checkServerTrusted(X509Certificate[] chain, String authType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        System.out.println("    CheckServerTrusted(" + authType + ")?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
}