Path: blob/master/test/jdk/javax/net/ssl/SSLEngine/ExtendedKeySocket.java
41152 views
/*1* Copyright (c) 2004, 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 498169726* @summary Rework the X509KeyManager to avoid incompatibility issues27* @run main/othervm ExtendedKeySocket28*29* SunJSSE does not support dynamic system properties, no way to re-use30* system properties in samevm/agentvm mode.31* @author Brad R. Wetmore32*/3334import java.io.*;35import java.net.*;36import javax.net.ssl.*;37import java.security.*;3839public class ExtendedKeySocket {4041/*42* =============================================================43* Set the various variables needed for the tests, then44* specify what tests to run on each side.45*/4647/*48* Should we run the client or server in a separate thread?49* Both sides can throw exceptions, but do you have a preference50* as to which side should be the main thread.51*/52static boolean separateServerThread = false;5354/*55* Where do we find the keystores?56*/57static String pathToStores = "../etc";58static String keyStoreFile = "keystore";59static String trustStoreFile = "truststore";60static char [] passwd = "passphrase".toCharArray();6162/*63* Is the server ready to serve?64*/65volatile static boolean serverReady = false;6667/*68* Turn on SSL debugging?69*/70static boolean debug = false;7172/*73* If the client or server is doing some kind of object creation74* that the other side depends on, and that thread prematurely75* exits, you may experience a hang. The test harness will76* terminate all hung threads after its timeout has expired,77* currently 3 minutes by default, but you might try to be78* smart about it....79*/8081private String keyFilename =82System.getProperty("test.src", "./") + "/" + pathToStores +83"/" + keyStoreFile;84private String trustFilename =85System.getProperty("test.src", "./") + "/" + pathToStores +86"/" + trustStoreFile;8788SSLContext getSSLContext(boolean abs) throws Exception {89SSLContext ctx = SSLContext.getInstance("TLS");9091KeyStore keyKS = KeyStore.getInstance("JKS");92keyKS.load(new FileInputStream(keyFilename), passwd);9394KeyStore trustKS = KeyStore.getInstance("JKS");95trustKS.load(new FileInputStream(trustFilename), passwd);9697KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");98kmf.init(keyKS, passwd);99100KeyManager [] kms = kmf.getKeyManagers();101if (!(kms[0] instanceof X509ExtendedKeyManager)) {102throw new Exception("kms[0] not X509ExtendedKeyManager");103}104105if (abs) {106kms = new KeyManager [] {107new MyX509ExtendedKeyManager((X509ExtendedKeyManager)kms[0])108};109} else {110kms = new KeyManager [] {111new MyX509KeyManager((X509KeyManager)kms[0])112};113}114115TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");116tmf.init(trustKS);117TrustManager [] tms = tmf.getTrustManagers();118119ctx.init(kms, tms, null);120121return ctx;122}123124/*125* Define the server side of the test.126*127* If the server prematurely exits, serverReady will be set to true128* to avoid infinite hangs.129*/130void doServerSide() throws Exception {131System.out.println("Starting Server1");132doServerTest(getSSLContext(true));133134System.out.println("Starting Server2");135doServerTest(getSSLContext(false));136137System.out.println("Finishing Server");138}139140void doServerTest(SSLContext ctx) throws Exception {141serverPort = 0;142SSLServerSocketFactory sslssf = ctx.getServerSocketFactory();143SSLServerSocket sslServerSocket =144(SSLServerSocket) sslssf.createServerSocket(serverPort);145sslServerSocket.setNeedClientAuth(true);146147serverPort = sslServerSocket.getLocalPort();148149/*150* Signal Client, we're ready for his connect.151*/152serverReady = true;153154SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();155InputStream sslIS = sslSocket.getInputStream();156OutputStream sslOS = sslSocket.getOutputStream();157158sslIS.read();159sslOS.write(85);160sslOS.flush();161162sslSocket.close();163}164165/*166* Define the client side of the test.167*168* If the server prematurely exits, serverReady will be set to true169* to avoid infinite hangs.170*/171void doClientSide() throws Exception {172System.out.println("Starting Client1");173doClientTest(getSSLContext(true));174System.out.println("Starting Client2");175doClientTest(getSSLContext(false));176System.out.println("Finishing Client");177}178179void doClientTest(SSLContext ctx) throws Exception {180/*181* Wait for server to get started.182*/183while (!serverReady) {184Thread.sleep(50);185}186187/*188* Reset for next time through.189*/190serverReady = false;191192SSLSocketFactory sslsf = ctx.getSocketFactory();193SSLSocket sslSocket = (SSLSocket)194sslsf.createSocket("localhost", serverPort);195196InputStream sslIS = sslSocket.getInputStream();197OutputStream sslOS = sslSocket.getOutputStream();198199sslOS.write(280);200sslOS.flush();201sslIS.read();202203sslSocket.close();204}205206/*207* =============================================================208* The remainder is just support stuff209*/210211// use any free port by default212volatile int serverPort = 0;213214volatile Exception serverException = null;215volatile Exception clientException = null;216217public static void main(String[] args) throws Exception {218if (debug)219System.setProperty("javax.net.debug", "all");220221/*222* Start the tests.223*/224new ExtendedKeySocket();225}226227Thread clientThread = null;228Thread serverThread = null;229230/*231* Primary constructor, used to drive remainder of the test.232*233* Fork off the other side, then do your work.234*/235ExtendedKeySocket() throws Exception {236try {237if (separateServerThread) {238startServer(true);239startClient(false);240} else {241startClient(true);242startServer(false);243}244} catch (Exception e) {245// swallow for now. Show later246}247248/*249* Wait for other side to close down.250*/251if (separateServerThread) {252serverThread.join();253} else {254clientThread.join();255}256257/*258* When we get here, the test is pretty much over.259* Which side threw the error?260*/261Exception local;262Exception remote;263String whichRemote;264265if (separateServerThread) {266remote = serverException;267local = clientException;268whichRemote = "server";269} else {270remote = clientException;271local = serverException;272whichRemote = "client";273}274275/*276* If both failed, return the curthread's exception, but also277* print the remote side Exception278*/279if ((local != null) && (remote != null)) {280System.out.println(whichRemote + " also threw:");281remote.printStackTrace();282System.out.println();283throw local;284}285286if (remote != null) {287throw remote;288}289290if (local != null) {291throw local;292}293}294295void startServer(boolean newThread) throws Exception {296if (newThread) {297serverThread = new Thread() {298public void run() {299try {300doServerSide();301} catch (Exception e) {302/*303* Our server thread just died.304*305* Release the client, if not active already...306*/307System.err.println("Server died...");308serverReady = true;309serverException = e;310}311}312};313serverThread.start();314} else {315try {316doServerSide();317} catch (Exception e) {318serverException = e;319} finally {320serverReady = true;321}322}323}324325void startClient(boolean newThread) throws Exception {326if (newThread) {327clientThread = new Thread() {328public void run() {329try {330doClientSide();331} catch (Exception e) {332/*333* Our client thread just died.334*/335System.err.println("Client died...");336clientException = e;337}338}339};340clientThread.start();341} else {342try {343doClientSide();344} catch (Exception e) {345clientException = e;346}347}348}349}350351352