ServerIdentityTest.java revision 15706:12e616e0a018
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 * @test 26 * @bug 4328195 27 * @summary Need to include the alternate subject DN for certs, 28 * https should check for this 29 * @library /javax/net/ssl/templates 30 * @run main/othervm ServerIdentityTest dnsstore 31 * @run main/othervm ServerIdentityTest ipstore 32 * 33 * SunJSSE does not support dynamic system properties, no way to re-use 34 * system properties in samevm/agentvm mode. 35 * 36 * @author Yingxian Wang 37 */ 38 39import java.io.BufferedWriter; 40import java.io.OutputStreamWriter; 41import java.net.HttpURLConnection; 42import java.net.URL; 43import java.security.KeyStore; 44import javax.net.ssl.HttpsURLConnection; 45import javax.net.ssl.KeyManager; 46import javax.net.ssl.SSLContext; 47 48public class ServerIdentityTest { 49 50 private static final String PASSWORD = "changeit"; 51 52 public static void main(String[] args) throws Exception { 53 final String keystore = args[0]; 54 String keystoreFilename = SSLTest.TEST_SRC + "/" + keystore; 55 56 SSLTest.setup(keystoreFilename, keystoreFilename, PASSWORD); 57 58 SSLContext context = SSLContext.getInstance("SSL"); 59 60 KeyManager[] kms = new KeyManager[1]; 61 KeyStore ks = SSLTest.loadJksKeyStore(keystoreFilename, PASSWORD); 62 KeyManager km = new MyKeyManager(ks, PASSWORD.toCharArray()); 63 kms[0] = km; 64 context.init(kms, null, null); 65 HttpsURLConnection.setDefaultSSLSocketFactory( 66 context.getSocketFactory()); 67 68 /* 69 * Start the test. 70 */ 71 System.out.println("Testing " + keystore); 72 73 new SSLTest() 74 .setSSLContext(context) 75 .setServerApplication((socket, test) -> { 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 Thread.sleep(2000); 81 socket.getSession().invalidate(); 82 SSLTest.print("Server application is done"); 83 }) 84 .setClientPeer((test) -> { 85 boolean serverIsReady = test.waitForServerSignal(); 86 if (!serverIsReady) { 87 SSLTest.print( 88 "The server is not ready, ignore on client side."); 89 return; 90 } 91 92 // Signal the server, the client is ready to communicate. 93 test.signalClientReady(); 94 95 String host = keystore.equals("ipstore") 96 ? "127.0.0.1" : "localhost"; 97 URL url = new URL("https://" + host + ":" + test.getServerPort() 98 + "/index.html"); 99 100 ((HttpURLConnection) url.openConnection()) 101 .getInputStream().close(); 102 103 SSLTest.print("Client is done"); 104 }).runTest(); 105 } 106} 107