Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/NewImpl/JavaxHostnameVerifier.java
38889 views
/*1* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223// SunJSSE does not support dynamic system properties, no way to re-use24// system properties in samevm/agentvm mode.2526/*27* @test28* @bug 4474255 448424629* @summary When an application enables anonymous SSL cipher suite,30* Hostname verification is not required31* @run main/othervm JavaxHostnameVerifier32*/3334import java.io.*;35import java.net.*;36import java.security.Security;37import java.security.cert.*;38import javax.net.ssl.*;3940/**41* Use javax.net.ssl.HostnameVerifier42*/43public class JavaxHostnameVerifier {4445/*46* =============================================================47* Set the various variables needed for the tests, then48* specify what tests to run on each side.49*/5051/*52* Should we run the client or server in a separate thread?53* Both sides can throw exceptions, but do you have a preference54* as to which side should be the main thread.55*/56static boolean separateServerThread = true;5758/*59* Is the server ready to serve?60*/61volatile static boolean serverReady = false;6263/*64* Turn on SSL debugging?65*/66static boolean debug = false;6768/*69* If the client or server is doing some kind of object creation70* that the other side depends on, and that thread prematurely71* exits, you may experience a hang. The test harness will72* terminate all hung threads after its timeout has expired,73* currently 3 minutes by default, but you might try to be74* smart about it....75*/7677/**78* Returns the path to the file obtained from79* parsing the HTML header.80*/81private static String getPath(DataInputStream in)82throws IOException83{84String line = in.readLine();85String path = "";86// extract class from GET line87if (line == null)88return null;8990if (line.startsWith("GET /")) {91line = line.substring(5, line.length()-1).trim();92int index = line.indexOf(' ');93if (index != -1) {94path = line.substring(0, index);95}96}9798// eat the rest of header99do {100line = in.readLine();101} while ((line.length() != 0) &&102(line.charAt(0) != '\r') && (line.charAt(0) != '\n'));103104if (path.length() != 0) {105return path;106} else {107throw new IOException("Malformed Header");108}109}110111/**112* Returns an array of bytes containing the bytes for113* the file represented by the argument <b>path</b>.114*115* In our case, we just pretend to send something back.116*117* @return the bytes for the file118* @exception FileNotFoundException if the file corresponding119* to <b>path</b> could not be loaded.120*/121private byte[] getBytes(String path)122throws IOException123{124return "Hello world, I am here".getBytes();125}126127/*128* Define the server side of the test.129*130* If the server prematurely exits, serverReady will be set to true131* to avoid infinite hangs.132*/133void doServerSide() throws Exception {134135SSLServerSocketFactory sslssf =136(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();137SSLServerSocket sslServerSocket =138(SSLServerSocket) sslssf.createServerSocket(serverPort);139serverPort = sslServerSocket.getLocalPort();140141String ciphers[]= { "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA" };142sslServerSocket.setEnabledCipherSuites(ciphers);143144/*145* Signal Client, we're ready for his connect.146*/147serverReady = true;148149SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();150DataOutputStream out =151new DataOutputStream(sslSocket.getOutputStream());152153try {154// get path to class file from header155DataInputStream in =156new DataInputStream(sslSocket.getInputStream());157String path = getPath(in);158// retrieve bytecodes159byte[] bytecodes = getBytes(path);160// send bytecodes in response (assumes HTTP/1.0 or later)161try {162out.writeBytes("HTTP/1.0 200 OK\r\n");163out.writeBytes("Content-Length: " + bytecodes.length + "\r\n");164out.writeBytes("Content-Type: text/html\r\n\r\n");165out.write(bytecodes);166out.flush();167} catch (IOException ie) {168ie.printStackTrace();169return;170}171172} catch (Exception e) {173e.printStackTrace();174// write out error response175out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n");176out.writeBytes("Content-Type: text/html\r\n\r\n");177out.flush();178} finally {179// close the socket180System.out.println("Server closing socket");181sslSocket.close();182serverReady = false;183}184}185186/*187* Define the client side of the test.188*189* If the server prematurely exits, serverReady will be set to true190* to avoid infinite hangs.191*/192void doClientSide() throws Exception {193/*194* Wait for server to get started.195*/196while (!serverReady) {197Thread.sleep(50);198}199200System.setProperty("https.cipherSuites",201"SSL_DH_anon_WITH_3DES_EDE_CBC_SHA");202203// use the default hostname verifier204205URL url = new URL("https://" + "localhost:" + serverPort +206"/etc/hosts");207URLConnection urlc = url.openConnection();208209if (!(urlc instanceof javax.net.ssl.HttpsURLConnection)) {210throw new Exception(211"URLConnection ! instanceof javax.net.ssl.HttpsURLConnection");212}213214BufferedReader in = null;215try {216in = new BufferedReader(new InputStreamReader(217urlc.getInputStream()));218String inputLine;219System.out.print("Client reading... ");220while ((inputLine = in.readLine()) != null)221System.out.println(inputLine);222System.out.println("Cipher Suite: " +223((HttpsURLConnection)urlc).getCipherSuite());224in.close();225} catch (SSLException e) {226if (in != null)227in.close();228throw e;229}230System.out.println("Client reports: SUCCESS");231}232233/*234* =============================================================235* The remainder is just support stuff236*/237238// use any free port by default239volatile int serverPort = 0;240241volatile Exception serverException = null;242volatile Exception clientException = null;243244public static void main(String[] args) throws Exception {245// re-enable 3DES246Security.setProperty("jdk.tls.disabledAlgorithms", "");247248if (debug)249System.setProperty("javax.net.debug", "all");250251/*252* Start the tests.253*/254new JavaxHostnameVerifier();255}256257Thread clientThread = null;258Thread serverThread = null;259260/*261* Primary constructor, used to drive remainder of the test.262*263* Fork off the other side, then do your work.264*/265JavaxHostnameVerifier() throws Exception {266if (separateServerThread) {267startServer(true);268startClient(false);269} else {270startClient(true);271startServer(false);272}273274/*275* Wait for other side to close down.276*/277if (separateServerThread) {278serverThread.join();279} else {280clientThread.join();281}282283/*284* When we get here, the test is pretty much over.285*286* If the main thread excepted, that propagates back287* immediately. If the other thread threw an exception, we288* should report back.289*/290if (serverException != null) {291System.out.print("Server Exception:");292throw serverException;293}294if (clientException != null) {295System.out.print("Client Exception:");296throw clientException;297}298}299300void startServer(boolean newThread) throws Exception {301if (newThread) {302serverThread = new Thread() {303public void run() {304try {305doServerSide();306} catch (Exception e) {307/*308* Our server thread just died.309*310* Release the client, if not active already...311*/312System.err.println("Server died...");313serverReady = true;314serverException = e;315}316}317};318serverThread.start();319} else {320doServerSide();321}322}323324void startClient(boolean newThread) throws Exception {325if (newThread) {326clientThread = new Thread() {327public void run() {328try {329doClientSide();330} catch (Exception e) {331/*332* Our client thread just died.333*/334System.err.println("Client died...");335clientException = e;336}337}338};339clientThread.start();340} else {341doClientSide();342}343}344}345346347