Path: blob/master/test/jdk/sun/security/ssl/SSLContextImpl/BadTSProvider.java
41152 views
/*1* Copyright (c) 2003, 2019, 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// SunJSSE does not support dynamic system properties, no way to re-use25// system properties in samevm/agentvm mode.26//2728/*29* @test30* @bug 4919147 816806931* @summary Support for token-based KeyStores32* @run main/othervm BadTSProvider33*/3435import java.io.*;36import java.net.*;37import java.security.*;38import javax.net.ssl.*;3940public class BadTSProvider {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 = false;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();91SSLServerSocket sslServerSocket =92(SSLServerSocket) sslssf.createServerSocket(serverPort);9394serverPort = sslServerSocket.getLocalPort();9596/*97* Signal Client, we're ready for his connect.98*/99serverReady = true;100101SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();102InputStream sslIS = sslSocket.getInputStream();103OutputStream sslOS = sslSocket.getOutputStream();104105sslIS.read();106sslOS.write(85);107sslOS.flush();108109sslSocket.close();110}111112/*113* Define the client side of the test.114*115* If the server prematurely exits, serverReady will be set to true116* to avoid infinite hangs.117*/118void doClientSide() throws Exception {119120/*121* Wait for server to get started.122*/123while (!serverReady) {124Thread.sleep(50);125}126127SSLSocketFactory sslsf =128(SSLSocketFactory) SSLSocketFactory.getDefault();129SSLSocket sslSocket = (SSLSocket)130sslsf.createSocket("localhost", serverPort);131132InputStream sslIS = sslSocket.getInputStream();133OutputStream sslOS = sslSocket.getOutputStream();134135sslOS.write(280);136sslOS.flush();137sslIS.read();138139sslSocket.close();140}141142/*143* =============================================================144* The remainder is just support stuff145*/146147// use any free port by default148volatile int serverPort = 0;149150volatile Exception serverException = null;151volatile Exception clientException = null;152153public static void main(String[] args) throws Exception {154String keyFilename =155System.getProperty("test.src", "./") + "/" + pathToStores +156"/" + keyStoreFile;157String trustFilename =158System.getProperty("test.src", "./") + "/" + pathToStores +159"/" + trustStoreFile;160161// first test a good provider name162163System.setProperty("javax.net.ssl.keyStore", keyFilename);164System.setProperty("javax.net.ssl.keyStoreProvider", "SUN");165System.setProperty("javax.net.ssl.keyStorePassword", passwd);166System.setProperty("javax.net.ssl.trustStore", trustFilename);167System.setProperty("javax.net.ssl.trustStoreProvider", "BAD-PROVIDER");168System.setProperty("javax.net.ssl.trustStorePassword", passwd);169170if (debug)171System.setProperty("javax.net.debug", "ssl,defaultctx");172173try {174new BadTSProvider();175throw new SecurityException("expected no-such-provider exception");176} catch (SocketException se) {177// catching the exception is ok,178// but let's try to confirm it is the right exception.179//180// Note: this test must be updated if the exception message changes181Throwable cause = se.getCause();182if (!(cause instanceof NoSuchAlgorithmException)) {183se.printStackTrace();184throw new Exception("Unexpected exception" + se);185}186187cause = cause.getCause();188if (!(cause instanceof KeyManagementException)) {189se.printStackTrace();190throw new Exception("Unexpected exception" + se);191}192193System.out.println("OK");194}195}196197Thread clientThread = null;198Thread serverThread = null;199200/*201* Primary constructor, used to drive remainder of the test.202*203* Fork off the other side, then do your work.204*/205BadTSProvider() throws Exception {206try {207if (separateServerThread) {208startServer(true);209startClient(false);210} else {211startClient(true);212startServer(false);213}214} catch (Exception e) {215//swallow for now. Show later216}217218/*219* Wait for other side to close down.220*/221if (separateServerThread) {222serverThread.join();223} else {224clientThread.join();225}226227/*228* When we get here, the test is pretty much over.229* Which side threw the error?230*/231Exception local;232Exception remote;233String whichRemote;234235if (separateServerThread) {236remote = serverException;237local = clientException;238whichRemote = "server";239} else {240remote = clientException;241local = serverException;242whichRemote = "client";243}244245/*246* If both failed, return the curthread's exception, but also247* print the remote side Exception248*/249if ((local != null) && (remote != null)) {250System.out.println(whichRemote + " also threw:");251//remote.printStackTrace();252System.out.println();253throw local;254}255256if (remote != null) {257throw remote;258}259260if (local != null) {261throw local;262}263}264265void startServer(boolean newThread) throws Exception {266if (newThread) {267serverThread = new Thread() {268public void run() {269try {270doServerSide();271} catch (Exception e) {272/*273* Our server thread just died.274*275* Release the client, if not active already...276*/277System.err.println("Server died...");278serverReady = true;279serverException = e;280}281}282};283serverThread.start();284} else {285try {286doServerSide();287} catch (Exception e) {288serverException = e;289} finally {290serverReady = true;291}292}293}294295void startClient(boolean newThread) throws Exception {296if (newThread) {297clientThread = new Thread() {298public void run() {299try {300doClientSide();301} catch (Exception e) {302/*303* Our client thread just died.304*/305System.err.println("Client died...");306clientException = e;307}308}309};310clientThread.start();311} else {312try {313doClientSide();314} catch (Exception e) {315clientException = e;316}317}318}319}320321322