Path: blob/master/test/jdk/sun/security/ssl/X509TrustManagerImpl/X509ExtendedTMEnabled.java
41152 views
/*1* Copyright (c) 2010, 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 691607426* @summary Add support for TLS 1.227* @run main/othervm X509ExtendedTMEnabled28*29* SunJSSE does not support dynamic system properties, no way to re-use30* system properties in samevm/agentvm mode.31*32* Ensure that the SunJSSE provider enables the X509ExtendedTrustManager.33*/3435import java.io.*;36import java.net.*;37import javax.net.ssl.*;38import java.security.cert.*;39import java.security.*;4041public class X509ExtendedTMEnabled {4243/*44* =============================================================45* Set the various variables needed for the tests, then46* specify what tests to run on each side.47*/4849/*50* Should we run the client or server in a separate thread?51* Both sides can throw exceptions, but do you have a preference52* as to which side should be the main thread.53*/54static boolean separateServerThread = true;5556/*57* Where do we find the keystores?58*/59static String pathToStores = "../../../../javax/net/ssl/etc";60static String keyStoreFile = "keystore";61static String trustStoreFile = "truststore";62static String passwd = "passphrase";63private final static char[] cpasswd = "passphrase".toCharArray();6465/*66* Is the server ready to serve?67*/68volatile static boolean serverReady = false;6970/*71* Turn on SSL debugging?72*/73static boolean debug = false;7475/*76* If the client or server is doing some kind of object creation77* that the other side depends on, and that thread prematurely78* exits, you may experience a hang. The test harness will79* terminate all hung threads after its timeout has expired,80* currently 3 minutes by default, but you might try to be81* smart about it....82*/8384/*85* Define the server side of the test.86*87* If the server prematurely exits, serverReady will be set to true88* to avoid infinite hangs.89*/90void doServerSide() throws Exception {91SSLServerSocketFactory sslssf =92getContext(true).getServerSocketFactory();93SSLServerSocket sslServerSocket =94(SSLServerSocket) sslssf.createServerSocket(serverPort);95serverPort = sslServerSocket.getLocalPort();9697// enable endpoint identification98// ignore, we may test the feature when known how to parse client99// hostname100//SSLParameters params = sslServerSocket.getSSLParameters();101//params.setEndpointIdentificationAlgorithm("HTTPS");102//sslServerSocket.setSSLParameters(params);103104/*105* Signal Client, we're ready for his connect.106*/107serverReady = true;108109SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();110sslSocket.setNeedClientAuth(true);111InputStream sslIS = sslSocket.getInputStream();112OutputStream sslOS = sslSocket.getOutputStream();113114sslIS.read();115sslOS.write(85);116sslOS.flush();117118sslSocket.close();119120if (!serverTM.wasServerChecked() && serverTM.wasClientChecked()) {121System.out.println("SERVER TEST PASSED!");122} else {123throw new Exception("SERVER TEST FAILED! " +124!serverTM.wasServerChecked() + " " +125serverTM.wasClientChecked());126}127}128129/*130* Define the client side of the test.131*132* If the server prematurely exits, serverReady will be set to true133* to avoid infinite hangs.134*/135void doClientSide() throws Exception {136137/*138* Wait for server to get started.139*/140while (!serverReady) {141Thread.sleep(50);142}143144SSLSocketFactory sslsf = getContext(false).getSocketFactory();145SSLSocket sslSocket = (SSLSocket)146sslsf.createSocket("localhost", serverPort);147148// enable endpoint identification149SSLParameters params = sslSocket.getSSLParameters();150params.setEndpointIdentificationAlgorithm("HTTPS");151sslSocket.setSSLParameters(params);152153InputStream sslIS = sslSocket.getInputStream();154OutputStream sslOS = sslSocket.getOutputStream();155156sslOS.write(280);157sslOS.flush();158sslIS.read();159160sslSocket.close();161162if (clientTM.wasServerChecked() && !clientTM.wasClientChecked()) {163System.out.println("CLIENT TEST PASSED!");164} else {165throw new Exception("CLIENT TEST FAILED! " +166clientTM.wasServerChecked() + " " +167!clientTM.wasClientChecked());168}169}170171MyExtendedX509TM serverTM;172MyExtendedX509TM clientTM;173174private SSLContext getContext(boolean server) throws Exception {175String keyFilename =176System.getProperty("test.src", "./") + "/" + pathToStores +177"/" + keyStoreFile;178String trustFilename =179System.getProperty("test.src", "./") + "/" + pathToStores +180"/" + trustStoreFile;181182KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");183KeyStore ks = KeyStore.getInstance("JKS");184ks.load(new FileInputStream(keyFilename), cpasswd);185kmf.init(ks, cpasswd);186187TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");188KeyStore ts = KeyStore.getInstance("JKS");189ts.load(new FileInputStream(trustFilename), cpasswd);190tmf.init(ts);191192TrustManager tms[] = tmf.getTrustManagers();193if (tms == null || tms.length == 0) {194throw new Exception("unexpected trust manager implementation");195} else {196if (!(tms[0] instanceof X509TrustManager)) {197throw new Exception("unexpected trust manager implementation: "198+ tms[0].getClass().getCanonicalName());199}200}201202if (server) {203serverTM = new MyExtendedX509TM((X509TrustManager)tms[0]);204205tms = new TrustManager[] {serverTM};206} else {207clientTM = new MyExtendedX509TM((X509TrustManager)tms[0]);208209tms = new TrustManager[] {clientTM};210}211212SSLContext ctx = SSLContext.getInstance("TLS");213ctx.init(kmf.getKeyManagers(), tms, null);214215return ctx;216}217218static class MyExtendedX509TM extends X509ExtendedTrustManager219implements X509TrustManager {220221X509TrustManager tm;222223boolean clientChecked;224boolean serverChecked;225226MyExtendedX509TM(X509TrustManager tm) {227clientChecked = false;228serverChecked = false;229230this.tm = tm;231}232233public boolean wasClientChecked() {234return clientChecked;235}236237public boolean wasServerChecked() {238return serverChecked;239}240241242public void checkClientTrusted(X509Certificate chain[], String authType)243throws CertificateException {244tm.checkClientTrusted(chain, authType);245}246247public void checkServerTrusted(X509Certificate chain[], String authType)248throws CertificateException {249tm.checkServerTrusted(chain, authType);250}251252public X509Certificate[] getAcceptedIssuers() {253return tm.getAcceptedIssuers();254}255256public void checkClientTrusted(X509Certificate[] chain, String authType,257Socket socket) throws CertificateException {258clientChecked = true;259tm.checkClientTrusted(chain, authType);260}261262public void checkServerTrusted(X509Certificate[] chain, String authType,263Socket socket) throws CertificateException {264serverChecked = true;265tm.checkServerTrusted(chain, authType);266}267268public void checkClientTrusted(X509Certificate[] chain, String authType,269SSLEngine engine) throws CertificateException {270clientChecked = true;271tm.checkClientTrusted(chain, authType);272}273274public void checkServerTrusted(X509Certificate[] chain, String authType,275SSLEngine engine) throws CertificateException {276serverChecked = true;277tm.checkServerTrusted(chain, authType);278}279}280281/*282* =============================================================283* The remainder is just support stuff284*/285286// use any free port by default287volatile int serverPort = 0;288289volatile Exception serverException = null;290volatile Exception clientException = null;291292public static void main(String[] args) throws Exception {293294if (debug)295System.setProperty("javax.net.debug", "all");296297/*298* Start the tests.299*/300new X509ExtendedTMEnabled();301}302303Thread clientThread = null;304Thread serverThread = null;305306/*307* Primary constructor, used to drive remainder of the test.308*309* Fork off the other side, then do your work.310*/311X509ExtendedTMEnabled() throws Exception {312if (separateServerThread) {313startServer(true);314startClient(false);315} else {316startClient(true);317startServer(false);318}319320/*321* Wait for other side to close down.322*/323if (separateServerThread) {324serverThread.join();325} else {326clientThread.join();327}328329/*330* When we get here, the test is pretty much over.331*332* If the main thread excepted, that propagates back333* immediately. If the other thread threw an exception, we334* should report back.335*/336if (serverException != null)337throw serverException;338if (clientException != null)339throw clientException;340}341342void startServer(boolean newThread) throws Exception {343if (newThread) {344serverThread = new Thread() {345public void run() {346try {347doServerSide();348} catch (Exception e) {349/*350* Our server thread just died.351*352* Release the client, if not active already...353*/354System.err.println("Server died...");355serverReady = true;356serverException = e;357}358}359};360serverThread.start();361} else {362doServerSide();363}364}365366void startClient(boolean newThread) throws Exception {367if (newThread) {368clientThread = new Thread() {369public void run() {370try {371doClientSide();372} catch (Exception e) {373/*374* Our client thread just died.375*/376System.err.println("Client died...");377clientException = e;378}379}380};381clientThread.start();382} else {383doClientSide();384}385}386}387388389390