jdk/test/sun/security/ssl/InputRecord/ClientHelloRead.java
author mchung
Thu, 28 May 2015 10:54:48 -0700
changeset 30820 0d4717a011d3
parent 23052 241885315119
permissions -rw-r--r--
8081347: Add @modules to jdk_core tests Reviewed-by: alanb, joehw, lancea Contributed-by: alexander.kulyakhtin@oracle.com, alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
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
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 4432868
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary A client-hello message may not always be read correctly
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 23052
diff changeset
    28
 * @modules java.base/sun.net.www
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    29
 * @run main/othervm ClientHelloRead
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    30
 *
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    31
 *     SunJSSE does not support dynamic system properties, no way to re-use
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    32
 *     system properties in samevm/agentvm mode.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * ClientHelloRead.java -- includes a simple server that can serve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Http get request in both clear and secure channel, and a client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * that makes https requests behind the firewall through a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * proxy. The proxy delays the bytes sent from client to the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The server should read all delayed bytes to construct a correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * V2 ClientHello Message, if it doesn't, it will throw a SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * handshake exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class ClientHelloRead {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Where do we find the keystores?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 10328
diff changeset
    56
    static String pathToStores = "../../../../javax/net/ssl/etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    volatile private static int serverPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * The TestServer implements a OriginServer that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * processes HTTP requests and responses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static class TestServer extends OriginServer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        public TestServer(ServerSocket ss) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            super(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
         * Returns an array of bytes containing the bytes for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
         * the data sent in the response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
         * @return bytes for the data in the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        public byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            return "SSL V2 Client Hello read was successful..".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                        getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Main method to create the server and client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static void main(String args[]) throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        System.setProperty("javax.net.ssl.keyStore", keyFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        System.setProperty("javax.net.ssl.keyStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        System.setProperty("javax.net.ssl.trustStore", trustFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        System.setProperty("javax.net.ssl.trustStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        boolean useSSL = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
         * setup the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            ServerSocketFactory ssf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                ClientHelloRead.getServerSocketFactory(useSSL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            ServerSocket ss = ssf.createServerSocket(serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            serverPort = ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            new TestServer(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            System.out.println("Server side failed:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // trigger the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            System.out.println("Client side failed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw e;
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
    private static ServerSocketFactory getServerSocketFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                   (boolean useSSL) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (useSSL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            SSLServerSocketFactory ssf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            // set up key manager to do server authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            SSLContext ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            KeyManagerFactory kmf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            char[] passphrase = passwd.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            ctx = SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            kmf = KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            ks.load(new FileInputStream(System.getProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                        "javax.net.ssl.keyStore")), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            kmf.init(ks, passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            ctx.init(kmf.getKeyManagers(), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            ssf = ctx.getServerSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return ssf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return ServerSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    static void doClientSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
         * setup up a proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        setupProxy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * we want to avoid URLspoofCheck failures in cases where the cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         * DN name does not match the hostname in the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         */
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   162
        HostnameVerifier reservedHV =
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   163
            HttpsURLConnection.getDefaultHostnameVerifier();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        try {
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   165
            HttpsURLConnection.setDefaultHostnameVerifier(
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   166
                                          new NameVerifier());
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   167
            URL url = new URL("https://" + "localhost:" + serverPort
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   168
                                    + "/index.html");
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   169
            BufferedReader in = null;
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   170
            try {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   171
                in = new BufferedReader(new InputStreamReader(
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   172
                                   url.openStream()));
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   173
                String inputLine;
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   174
                System.out.print("Client recieved from the server: ");
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   175
                while ((inputLine = in.readLine()) != null)
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   176
                    System.out.println(inputLine);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                in.close();
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   178
            } catch (SSLException e) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   179
                if (in != null)
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   180
                    in.close();
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   181
                throw e;
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   182
            }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   183
        } finally {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   184
            HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    static class NameVerifier implements HostnameVerifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        public boolean verify(String hostname, SSLSession session) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    static void setupProxy() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        ProxyTunnelServer pserver = new ProxyTunnelServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        pserver.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        System.setProperty("https.proxyHost", "localhost");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        System.setProperty("https.proxyPort", String.valueOf(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                        pserver.getPort()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
}