Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/net/ssl/SSLSession/CheckMyTrustedKeystore.java
38853 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 432911426* @summary Need better way of reflecting the reason when a chain is27* rejected as untrusted.28* @run main/othervm CheckMyTrustedKeystore29*30* SunJSSE does not support dynamic system properties, no way to re-use31* system properties in samevm/agentvm mode.32* @ignore JSSE supports algorithm constraints with CR 6916074,33* need to update this test case in JDK 7 soon34* This is a serious hack job!35* @author Brad Wetmore36*/3738import java.io.*;39import java.net.*;40import java.security.*;41import javax.net.ssl.*;42import java.security.cert.*;4344public class CheckMyTrustedKeystore {4546/*47* =============================================================48* Set the various variables needed for the tests, then49* specify what tests to run on each side.50*/5152/*53* Should we run the client or server in a separate thread?54* Both sides can throw exceptions, but do you have a preference55* as to which side should be the main thread.56*/57static boolean separateServerThread = true;5859/*60* Where do we find the keystores?61*/62final static String pathToStores = "../etc";63final static String keyStoreFile = "keystore";64final static String trustStoreFile = "truststore";65final static String unknownStoreFile = "unknown_keystore";66final static String passwd = "passphrase";67final static char[] cpasswd = "passphrase".toCharArray();6869/*70* Is the server ready to serve?71*/72volatile static boolean serverReady = false;7374/*75* Turn on SSL debugging?76*/77final static boolean debug = false;7879/*80* If the client or server is doing some kind of object creation81* that the other side depends on, and that thread prematurely82* exits, you may experience a hang. The test harness will83* terminate all hung threads after its timeout has expired,84* currently 3 minutes by default, but you might try to be85* smart about it....86*/8788/*89* Define the server side of the test.90*91* If the server prematurely exits, serverReady will be set to true92* to avoid infinite hangs.93*/94void doServerSide() throws Exception {95KeyStore ks = KeyStore.getInstance("JKS");96com.sun.net.ssl.SSLContext ctx =97com.sun.net.ssl.SSLContext.getInstance("TLS");98com.sun.net.ssl.KeyManagerFactory kmf =99com.sun.net.ssl.KeyManagerFactory.getInstance("SunX509");100101ks.load(new FileInputStream(keyFilename), cpasswd);102kmf.init(ks, cpasswd);103104com.sun.net.ssl.TrustManager [] tms =105new com.sun.net.ssl.TrustManager []106{ new MyComX509TrustManager() };107108ctx.init(kmf.getKeyManagers(), tms, null);109110SSLServerSocketFactory sslssf =111(SSLServerSocketFactory) ctx.getServerSocketFactory();112113SSLServerSocket sslServerSocket =114(SSLServerSocket) sslssf.createServerSocket(serverPort);115serverPort = sslServerSocket.getLocalPort();116117sslServerSocket.setNeedClientAuth(true);118119/*120* Create using the other type.121*/122SSLContext ctx1 =123SSLContext.getInstance("TLS");124KeyManagerFactory kmf1 =125KeyManagerFactory.getInstance("SunX509");126127TrustManager [] tms1 =128new TrustManager []129{ new MyJavaxX509TrustManager() };130131kmf1.init(ks, cpasswd);132133ctx1.init(kmf1.getKeyManagers(), tms1, null);134135sslssf = (SSLServerSocketFactory) ctx1.getServerSocketFactory();136137SSLServerSocket sslServerSocket1 =138(SSLServerSocket) sslssf.createServerSocket(serverPort1);139serverPort1 = sslServerSocket1.getLocalPort();140sslServerSocket1.setNeedClientAuth(true);141142/*143* Signal Client, we're ready for his connect.144*/145serverReady = true;146147SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();148sslServerSocket.close();149serverReady = false;150151InputStream sslIS = sslSocket.getInputStream();152OutputStream sslOS = sslSocket.getOutputStream();153154sslIS.read();155sslOS.write(85);156sslOS.flush();157sslSocket.close();158159sslSocket = (SSLSocket) sslServerSocket1.accept();160sslIS = sslSocket.getInputStream();161sslOS = sslSocket.getOutputStream();162163sslIS.read();164sslOS.write(85);165sslOS.flush();166sslSocket.close();167168System.out.println("Server exiting!");169System.out.flush();170}171172void doTest(SSLSocket sslSocket) throws Exception {173InputStream sslIS = sslSocket.getInputStream();174OutputStream sslOS = sslSocket.getOutputStream();175176System.out.println(" Writing");177sslOS.write(280);178sslOS.flush();179System.out.println(" Reading");180sslIS.read();181182sslSocket.close();183}184185/*186* Define the client side of the test.187*188* If the server prematurely exits, serverReady will be set to true189* to avoid infinite hangs.190*/191void doClientSide() throws Exception {192193/*194* Wait for server to get started.195*/196while (!serverReady) {197Thread.sleep(50);198}199200/*201* See if an unknown keystore actually gets checked ok.202*/203System.out.println("==============");204System.out.println("Starting test0");205KeyStore uks = KeyStore.getInstance("JKS");206SSLContext ctx =207SSLContext.getInstance("TLS");208KeyManagerFactory kmf =209KeyManagerFactory.getInstance("SunX509");210211uks.load(new FileInputStream(unknownFilename), cpasswd);212kmf.init(uks, cpasswd);213214TrustManager [] tms = new TrustManager []215{ new MyJavaxX509TrustManager() };216217ctx.init(kmf.getKeyManagers(), tms, null);218219SSLSocketFactory sslsf =220(SSLSocketFactory) ctx.getSocketFactory();221222System.out.println("Trying first socket " + serverPort);223SSLSocket sslSocket = (SSLSocket)224sslsf.createSocket("localhost", serverPort);225226doTest(sslSocket);227228/*229* Now try the other way.230*/231com.sun.net.ssl.SSLContext ctx1 =232com.sun.net.ssl.SSLContext.getInstance("TLS");233com.sun.net.ssl.KeyManagerFactory kmf1 =234com.sun.net.ssl.KeyManagerFactory.getInstance("SunX509");235kmf1.init(uks, cpasswd);236237com.sun.net.ssl.TrustManager [] tms1 =238new com.sun.net.ssl.TrustManager []239{ new MyComX509TrustManager() };240241ctx1.init(kmf1.getKeyManagers(), tms1, null);242243sslsf = (SSLSocketFactory) ctx1.getSocketFactory();244245System.out.println("Trying second socket " + serverPort1);246sslSocket = (SSLSocket) sslsf.createSocket("localhost",247serverPort1);248249doTest(sslSocket);250System.out.println("Completed test1");251}252253/*254* =============================================================255* The remainder is just support stuff256*/257258int serverPort = 0;259int serverPort1 = 0;260261volatile Exception serverException = null;262volatile Exception clientException = null;263264final static String keyFilename =265System.getProperty("test.src", "./") + "/" + pathToStores +266"/" + keyStoreFile;267final static String unknownFilename =268System.getProperty("test.src", "./") + "/" + pathToStores +269"/" + unknownStoreFile;270271public static void main(String[] args) throws Exception {272273if (debug)274System.setProperty("javax.net.debug", "all");275276/*277* Start the tests.278*/279new CheckMyTrustedKeystore();280}281282Thread clientThread = null;283Thread serverThread = null;284285/*286* Primary constructor, used to drive remainder of the test.287*288* Fork off the other side, then do your work.289*/290CheckMyTrustedKeystore() throws Exception {291if (separateServerThread) {292startServer(true);293startClient(false);294} else {295startClient(true);296startServer(false);297}298299/*300* Wait for other side to close down.301*/302if (separateServerThread) {303serverThread.join();304} else {305clientThread.join();306}307308/*309* When we get here, the test is pretty much over.310*311* If the main thread excepted, that propagates back312* immediately. If the other thread threw an exception, we313* should report back.314*/315if (serverException != null) {316System.out.print("Server Exception:");317throw serverException;318}319if (clientException != null) {320System.out.print("Client Exception:");321throw clientException;322}323}324325void startServer(boolean newThread) throws Exception {326if (newThread) {327serverThread = new Thread() {328public void run() {329try {330doServerSide();331} catch (Exception e) {332/*333* Our server thread just died.334*335* Release the client, if not active already...336*/337System.err.println("Server died...");338serverReady = true;339serverException = e;340}341}342};343serverThread.start();344} else {345doServerSide();346}347}348349void startClient(boolean newThread) throws Exception {350if (newThread) {351clientThread = new Thread() {352public void run() {353try {354doClientSide();355} catch (Exception e) {356/*357* Our client thread just died.358*/359System.err.println("Client died...");360clientException = e;361}362}363};364clientThread.start();365} else {366doClientSide();367}368}369}370371class MyComX509TrustManager implements com.sun.net.ssl.X509TrustManager {372373public X509Certificate[] getAcceptedIssuers() {374return (new X509Certificate[0]);375}376377public boolean isClientTrusted(X509Certificate[] chain) {378System.out.println(" IsClientTrusted?");379return true;380}381382public boolean isServerTrusted(X509Certificate[] chain) {383System.out.println(" IsServerTrusted?");384return true;385}386}387388class MyJavaxX509TrustManager implements X509TrustManager {389390public X509Certificate[] getAcceptedIssuers() {391return (new X509Certificate[0]);392}393394public void checkClientTrusted(X509Certificate[] chain, String authType)395throws CertificateException {396System.out.println(" CheckClientTrusted(" + authType + ")?");397}398399public void checkServerTrusted(X509Certificate[] chain, String authType)400throws CertificateException {401System.out.println(" CheckServerTrusted(" + authType + ")?");402}403}404405406