Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/ReverseNameLookup.java
41152 views
/*1* Copyright (c) 2002, 2021, 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/*24* @test25* @bug 474829226* @library /test/lib27* @summary Prevent/Disable reverse name lookups with JSSE SSL sockets28* @run main/othervm ReverseNameLookup -Djava.net.preferIPv4Stack29*30* SunJSSE does not support dynamic system properties, no way to re-use31* system properties in samevm/agentvm mode.32*/3334import jdk.test.lib.net.IPSupport;3536import java.io.*;37import java.net.*;38import javax.net.ssl.*;3940public class ReverseNameLookup {4142/*43* =============================================================44* Set the various variables needed for the tests, then45* specify what tests to run on each side.46*/4748/*49* Should we run the client or server in a separate thread?50* Both sides can throw exceptions, but do you have a preference51* as to which side should be the main thread.52*/53static boolean separateServerThread = true;5455/*56* Where do we find the keystores?57*/58static String pathToStores = "../../../../javax/net/ssl/etc";59static String keyStoreFile = "keystore";60static String trustStoreFile = "truststore";61static String passwd = "passphrase";6263/*64* Is the server ready to serve?65*/66volatile static boolean serverReady = false;6768/*69* Turn on SSL debugging?70*/71static boolean debug = false;7273/*74* If the client or server is doing some kind of object creation75* that the other side depends on, and that thread prematurely76* exits, you may experience a hang. The test harness will77* terminate all hung threads after its timeout has expired,78* currently 3 minutes by default, but you might try to be79* smart about it....80*/8182/*83* Define the server side of the test.84*85* If the server prematurely exits, serverReady will be set to true86* to avoid infinite hangs.87*/88void doServerSide() throws Exception {89SSLServerSocketFactory sslssf =90(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();91InetSocketAddress socketAddress =92new InetSocketAddress(InetAddress.getLoopbackAddress(), serverPort);93SSLServerSocket sslServerSocket =94(SSLServerSocket) sslssf.createServerSocket();95sslServerSocket.bind(socketAddress);9697serverPort = sslServerSocket.getLocalPort();9899/*100* Signal Client, we're ready for his connect.101*/102serverReady = true;103104SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();105InputStream sslIS = sslSocket.getInputStream();106OutputStream sslOS = sslSocket.getOutputStream();107108sslIS.read();109sslOS.write(85);110sslOS.flush();111112sslSocket.close();113}114115/*116* Define the client side of the test.117*118* If the server prematurely exits, serverReady will be set to true119* to avoid infinite hangs.120*/121void doClientSide() throws Exception {122123/*124* Wait for server to get started.125*/126while (!serverReady) {127Thread.sleep(50);128}129130SSLSocketFactory sslsf =131(SSLSocketFactory) SSLSocketFactory.getDefault();132SSLSocket sslSocket = (SSLSocket)133sslsf.createSocket("127.0.0.1", serverPort);134135InputStream sslIS = sslSocket.getInputStream();136OutputStream sslOS = sslSocket.getOutputStream();137138sslOS.write(280);139sslOS.flush();140sslIS.read();141SSLSession session = sslSocket.getSession();142if (!session.getPeerHost().equals("127.0.0.1")) {143throw new RuntimeException("we shouldn't do reverse name lookup");144}145sslSocket.close();146}147148/*149* =============================================================150* The remainder is just support stuff151*/152153// use any free port by default154volatile int serverPort = 0;155156volatile Exception serverException = null;157volatile Exception clientException = null;158159public static void main(String[] args) throws Exception {160IPSupport.throwSkippedExceptionIfNonOperational();161String keyFilename =162System.getProperty("test.src", "./") + "/" + pathToStores +163"/" + keyStoreFile;164String trustFilename =165System.getProperty("test.src", "./") + "/" + pathToStores +166"/" + trustStoreFile;167168System.setProperty("javax.net.ssl.keyStore", keyFilename);169System.setProperty("javax.net.ssl.keyStorePassword", passwd);170System.setProperty("javax.net.ssl.trustStore", trustFilename);171System.setProperty("javax.net.ssl.trustStorePassword", passwd);172173if (debug)174System.setProperty("javax.net.debug", "all");175176/*177* Start the tests.178*/179new ReverseNameLookup();180}181182Thread clientThread = null;183Thread serverThread = null;184185/*186* Primary constructor, used to drive remainder of the test.187*188* Fork off the other side, then do your work.189*/190ReverseNameLookup() throws Exception {191if (separateServerThread) {192startServer(true);193startClient(false);194} else {195startClient(true);196startServer(false);197}198199/*200* Wait for other side to close down.201*/202if (separateServerThread) {203serverThread.join();204} else {205clientThread.join();206}207208/*209* When we get here, the test is pretty much over.210*211* If the main thread excepted, that propagates back212* immediately. If the other thread threw an exception, we213* should report back.214*/215if (serverException != null)216throw serverException;217if (clientException != null)218throw clientException;219}220221void startServer(boolean newThread) throws Exception {222if (newThread) {223serverThread = new Thread() {224public void run() {225try {226doServerSide();227} catch (Exception e) {228/*229* Our server thread just died.230*231* Release the client, if not active already...232*/233System.err.println("Server died...");234serverReady = true;235serverException = e;236}237}238};239serverThread.start();240} else {241try {242doServerSide();243} finally {244serverReady = true;245}246}247}248249void startClient(boolean newThread) throws Exception {250if (newThread) {251clientThread = new Thread() {252public void run() {253try {254doClientSide();255} catch (Exception e) {256/*257* Our client thread just died.258*/259System.err.println("Client died...");260clientException = e;261}262}263};264clientThread.start();265} else {266doClientSide();267}268}269}270271272