1/*
2 * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24//
25// SunJSSE does not support dynamic system properties, no way to re-use
26// system properties in samevm/agentvm mode.
27//
28
29/*
30 * @test
31 * @bug 4328195
32 * @summary Need to include the alternate subject DN for certs,
33 *          https should check for this
34 * @library /javax/net/ssl/templates
35 * @run main/othervm ServerIdentityTest dnsstore localhost
36 * @run main/othervm ServerIdentityTest ipstore 127.0.0.1
37 *
38 * @author Yingxian Wang
39 */
40
41import java.io.InputStream;
42import java.io.BufferedWriter;
43import java.io.OutputStreamWriter;
44import java.net.HttpURLConnection;
45import java.net.URL;
46
47import javax.net.ssl.HttpsURLConnection;
48import javax.net.ssl.SSLContext;
49import javax.net.ssl.SSLSocket;
50
51public final class ServerIdentityTest extends SSLSocketTemplate {
52
53    private static String keystore;
54    private static String hostname;
55    private static SSLContext context;
56
57    /*
58     * Run the test case.
59     */
60    public static void main(String[] args) throws Exception {
61        // Get the customized arguments.
62        initialize(args);
63
64        (new ServerIdentityTest()).run();
65    }
66
67    @Override
68    protected boolean isCustomizedClientConnection() {
69        return true;
70    }
71
72    @Override
73    protected void runServerApplication(SSLSocket socket) throws Exception {
74        InputStream sslIS = socket.getInputStream();
75        sslIS.read();
76        BufferedWriter bw = new BufferedWriter(
77                new OutputStreamWriter(socket.getOutputStream()));
78        bw.write("HTTP/1.1 200 OK\r\n\r\n\r\n");
79        bw.flush();
80        socket.getSession().invalidate();
81    }
82
83    @Override
84    protected void runClientApplication(int serverPort) throws Exception {
85        URL url = new URL(
86                "https://" + hostname + ":" + serverPort + "/index.html");
87
88        HttpURLConnection urlc = null;
89        InputStream is = null;
90        try {
91            urlc = (HttpURLConnection)url.openConnection();
92            is = urlc.getInputStream();
93        } finally {
94            if (is != null) {
95                is.close();
96            }
97            if (urlc != null) {
98                urlc.disconnect();
99            }
100        }
101    }
102
103    @Override
104    protected SSLContext createServerSSLContext() throws Exception {
105        return context;
106    }
107
108    @Override
109    protected SSLContext createClientSSLContext() throws Exception {
110        return context;
111    }
112
113    private static void initialize(String[] args) throws Exception {
114        keystore = args[0];
115        hostname = args[1];
116
117        String password = "changeit";
118        String keyFilename =
119                System.getProperty("test.src", ".") + "/" + keystore;
120        String trustFilename =
121                System.getProperty("test.src", ".") + "/" + keystore;
122
123        System.setProperty("javax.net.ssl.keyStore", keyFilename);
124        System.setProperty("javax.net.ssl.keyStorePassword", password);
125        System.setProperty("javax.net.ssl.trustStore", trustFilename);
126        System.setProperty("javax.net.ssl.trustStorePassword", password);
127
128        context = SSLContext.getDefault();
129        HttpsURLConnection.setDefaultSSLSocketFactory(
130                context.getSocketFactory());
131    }
132}
133