Path: blob/master/test/jdk/sun/security/ssl/HandshakeOutStream/NullCerts.java
41152 views
/*1* Copyright (c) 2001, 2011, 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 445305326* @summary If a server shuts down correctly during handshaking, the client27* doesn't see it.28* @run main/othervm NullCerts29*30* SunJSSE does not support dynamic system properties, no way to re-use31* system properties in samevm/agentvm mode.32* @author Brad Wetmore33*/3435import java.io.*;36import java.net.*;37import java.security.*;38import javax.net.ssl.*;3940public class NullCerts {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*/53private static boolean separateServerThread = true;5455/*56* Where do we find the keystores?57*/58private final static String pathToStores = "../../../../javax/net/ssl/etc";59private final static String keyStoreFile = "keystore";60private final static String trustStoreFile = "truststore";61private final static String passwd = "passphrase";62private final static char[] cpasswd = "passphrase".toCharArray();6364/*65* Is the server ready to serve?66*/67volatile static boolean serverReady = false;6869/*70* Turn on SSL debugging?71*/72private final static boolean DEBUG = false;7374/*75* If the client or server is doing some kind of object creation76* that the other side depends on, and that thread prematurely77* exits, you may experience a hang. The test harness will78* terminate all hung threads after its timeout has expired,79* currently 3 minutes by default, but you might try to be80* smart about it....81*/8283/*84* Define the server side of the test.85*86* If the server prematurely exits, serverReady will be set to true87* to avoid infinite hangs.88*/89private void doServerSide() throws Exception {90SSLServerSocketFactory sslssf =91(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();92SSLServerSocket sslServerSocket =93(SSLServerSocket) sslssf.createServerSocket(serverPort, 3);94sslServerSocket.setNeedClientAuth(true);9596serverPort = sslServerSocket.getLocalPort();9798/*99* Signal Client, we're ready for his connect.100*/101serverReady = true;102103SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();104InputStream sslIS = sslSocket.getInputStream();105OutputStream sslOS = sslSocket.getOutputStream();106107try {108sslIS.read();109sslOS.write(85);110sslOS.flush();111} catch (SSLHandshakeException e) {112System.out.println(113"Should see a null cert chain exception for server: "114+ e.toString());115}116117sslSocket.close();118System.out.println("Server done and exiting!");119}120121/*122* Define the client side of the test.123*124* If the server prematurely exits, serverReady will be set to true125* to avoid infinite hangs.126*/127private void doClientSide() throws Exception {128129/*130* Wait for server to get started.131*/132while (!serverReady) {133Thread.sleep(50);134}135136System.out.println("Starting test");137138KeyStore ks = KeyStore.getInstance("JKS");139KeyStore uks = KeyStore.getInstance("JKS");140SSLContext ctx = SSLContext.getInstance("TLS");141KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");142TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");143144uks.load(new FileInputStream(unknownFilename), cpasswd);145kmf.init(uks, cpasswd);146147ks.load(new FileInputStream(trustFilename), cpasswd);148tmf.init(ks);149150ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);151152SSLSocketFactory sslsf =153(SSLSocketFactory) ctx.getSocketFactory();154SSLSocket sslSocket = (SSLSocket)155sslsf.createSocket("localhost", serverPort);156157InputStream sslIS = sslSocket.getInputStream();158OutputStream sslOS = sslSocket.getOutputStream();159160try {161sslOS.write(280);162sslOS.flush();163sslIS.read();164165sslSocket.close();166} catch (IOException e) {167String str =168"\nYou will either see a bad_certificate SSLException\n" +169"or an IOException if the server shutdown while the\n" +170"client was still sending the remainder of its \n" +171"handshake data.";172System.out.println(str + e.toString());173}174}175176/*177* =============================================================178* The remainder is just support stuff179*/180181// use any free port by default182volatile int serverPort = 0;183184private volatile Exception serverException = null;185private volatile Exception clientException = null;186187private final static String keyFilename =188System.getProperty("test.src", ".") + "/" + pathToStores +189"/" + keyStoreFile;190private final static String trustFilename =191System.getProperty("test.src", ".") + "/" + pathToStores +192"/" + trustStoreFile;193private final static String unknownFilename =194System.getProperty("test.src", ".") + "/" + pathToStores +195"/" + "unknown_keystore";196197// Used for running test standalone198public static void main(String[] args) throws Exception {199200String testRoot = System.getProperty("test.src", ".");201System.setProperty("javax.net.ssl.keyStore", keyFilename);202System.setProperty("javax.net.ssl.keyStorePassword", passwd);203System.setProperty("javax.net.ssl.trustStore", trustFilename);204System.setProperty("javax.net.ssl.trustStorePassword", passwd);205206if (DEBUG)207System.setProperty("javax.net.debug", "all");208209/*210* Start the tests.211*/212new NullCerts();213}214215private Thread clientThread = null;216private Thread serverThread = null;217218/*219* Primary constructor, used to drive remainder of the test.220*221* Fork off the other side, then do your work.222*/223NullCerts() throws Exception {224225if (separateServerThread) {226startServer(true);227startClient(false);228} else {229startClient(true);230startServer(false);231}232233/*234* Wait for other side to close down.235*/236if (separateServerThread) {237serverThread.join();238} else {239clientThread.join();240}241242/*243* When we get here, the test is pretty much over.244*245* If the main thread excepted, that propagates back246* immediately. If the other thread threw an exception, we247* should report back.248*/249if (serverException != null) {250System.err.print("Server Exception:");251throw serverException;252}253if (clientException != null) {254System.err.print("Client Exception:");255throw clientException;256}257}258259private void startServer(boolean newThread) throws Exception {260if (newThread) {261serverThread = new Thread() {262public void run() {263try {264doServerSide();265} catch (Exception e) {266/*267* Our server thread just died.268*269* Release the client, if not active already...270*/271System.err.println("Server died...");272serverReady = true;273serverException = e;274}275}276};277serverThread.start();278} else {279doServerSide();280}281}282283private void startClient(boolean newThread) throws Exception {284if (newThread) {285clientThread = new Thread() {286public void run() {287try {288doClientSide();289} catch (Exception e) {290/*291* Our client thread just died.292*/293System.err.println("Client died...");294clientException = e;295}296}297};298clientThread.start();299} else {300doClientSide();301}302}303}304305306