Path: blob/master/test/jdk/javax/net/ssl/TLSv11/EmptyCertificateAuthorities.java
41152 views
/*1* Copyright (c) 2010, 2016, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425//26// SunJSSE does not support dynamic system properties, no way to re-use27// system properties in samevm/agentvm mode.28//2930/*31* @test32* @bug 487318833* @summary Support TLS 1.134* @run main/othervm EmptyCertificateAuthorities35* @modules java.security.jgss36* java.security.jgss/sun.security.jgss.krb537* java.security.jgss/sun.security.krb5:+open38* java.security.jgss/sun.security.krb5.internal:+open39* java.security.jgss/sun.security.krb5.internal.ccache40* java.security.jgss/sun.security.krb5.internal.crypto41* java.security.jgss/sun.security.krb5.internal.ktab42* java.base/sun.security.util43* @author Xuelei Fan44*/4546import java.io.FileInputStream;47import java.io.InputStream;48import java.io.OutputStream;49import java.security.KeyStore;50import java.security.Security;51import java.security.cert.CertificateException;52import java.security.cert.X509Certificate;53import javax.net.ssl.KeyManager;54import javax.net.ssl.KeyManagerFactory;55import javax.net.ssl.SSLContext;56import javax.net.ssl.SSLServerSocket;57import javax.net.ssl.SSLServerSocketFactory;58import javax.net.ssl.SSLSocket;59import javax.net.ssl.SSLSocketFactory;60import javax.net.ssl.TrustManager;61import javax.net.ssl.TrustManagerFactory;62import javax.net.ssl.X509TrustManager;6364public class EmptyCertificateAuthorities {6566/*67* =============================================================68* Set the various variables needed for the tests, then69* specify what tests to run on each side.70*/7172/*73* Should we run the client or server in a separate thread?74* Both sides can throw exceptions, but do you have a preference75* as to which side should be the main thread.76*/77static boolean separateServerThread = false;7879/*80* Where do we find the keystores?81*/82static String pathToStores = "../etc";83static String keyStoreFile = "keystore";84static String trustStoreFile = "truststore";85static String passwd = "passphrase";8687/*88* Is the server ready to serve?89*/90volatile static boolean serverReady = false;9192/*93* Turn on SSL debugging?94*/95static boolean debug = false;9697/*98* If the client or server is doing some kind of object creation99* that the other side depends on, and that thread prematurely100* exits, you may experience a hang. The test harness will101* terminate all hung threads after its timeout has expired,102* currently 3 minutes by default, but you might try to be103* smart about it....104*/105106/*107* Define the server side of the test.108*109* If the server prematurely exits, serverReady will be set to true110* to avoid infinite hangs.111*/112void doServerSide() throws Exception {113SSLServerSocketFactory sslssf = getSSLServerSF();114SSLServerSocket sslServerSocket =115(SSLServerSocket) sslssf.createServerSocket(serverPort);116117// require client authentication.118sslServerSocket.setNeedClientAuth(true);119120serverPort = sslServerSocket.getLocalPort();121122/*123* Signal Client, we're ready for his connect.124*/125serverReady = true;126127SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();128InputStream sslIS = sslSocket.getInputStream();129OutputStream sslOS = sslSocket.getOutputStream();130131sslIS.read();132sslOS.write('A');133sslOS.flush();134135sslSocket.close();136}137138/*139* Define the client side of the test.140*141* If the server prematurely exits, serverReady will be set to true142* to avoid infinite hangs.143*/144void doClientSide() throws Exception {145146/*147* Wait for server to get started.148*/149while (!serverReady) {150Thread.sleep(50);151}152153SSLSocketFactory sslsf =154(SSLSocketFactory) SSLSocketFactory.getDefault();155SSLSocket sslSocket = (SSLSocket)156sslsf.createSocket("localhost", serverPort);157158// enable TLSv1.1 only159sslSocket.setEnabledProtocols(new String[] {"TLSv1.1"});160161InputStream sslIS = sslSocket.getInputStream();162OutputStream sslOS = sslSocket.getOutputStream();163164sslOS.write('B');165sslOS.flush();166sslIS.read();167168sslSocket.close();169}170171private SSLServerSocketFactory getSSLServerSF() throws Exception {172173char [] password =174System.getProperty("javax.net.ssl.keyStorePassword").toCharArray();175String keyFilename = System.getProperty("javax.net.ssl.keyStore");176177KeyStore ks = KeyStore.getInstance("JKS");178ks.load(new FileInputStream(keyFilename), password);179180KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509");181kmf.init(ks, password);182183KeyManager[] kms = kmf.getKeyManagers();184TrustManager[] tms = new MyX509TM[] {new MyX509TM()};185186SSLContext ctx = SSLContext.getInstance("TLS");187ctx.init(kms, tms, null);188189return ctx.getServerSocketFactory();190}191192193static class MyX509TM implements X509TrustManager {194X509TrustManager tm;195196public void checkClientTrusted(X509Certificate[] chain,197String authType) throws CertificateException {198if (tm == null) {199initialize();200}201tm.checkClientTrusted(chain, authType);202}203204public void checkServerTrusted(X509Certificate[] chain,205String authType) throws CertificateException {206if (tm == null) {207initialize();208}209tm.checkServerTrusted(chain, authType);210}211212public X509Certificate[] getAcceptedIssuers() {213// always return empty array214return new X509Certificate[0];215}216217private void initialize() throws CertificateException {218String passwd =219System.getProperty("javax.net.ssl.trustStorePassword");220char [] password = passwd.toCharArray();221String trustFilename =222System.getProperty("javax.net.ssl.trustStore");223224try {225KeyStore ks = KeyStore.getInstance("JKS");226ks.load(new FileInputStream(trustFilename), password);227228TrustManagerFactory tmf =229TrustManagerFactory.getInstance("PKIX");230tmf.init(ks);231tm = (X509TrustManager)tmf.getTrustManagers()[0];232} catch (Exception e) {233throw new CertificateException("Unable to initialize TM");234}235236}237}238239/*240* =============================================================241* The remainder is just support stuff242*/243244// use any free port by default245volatile int serverPort = 0;246247volatile Exception serverException = null;248volatile Exception clientException = null;249250public static void main(String[] args) throws Exception {251// MD5 is used in this test case, don't disable MD5 algorithm.252Security.setProperty("jdk.certpath.disabledAlgorithms",253"MD2, RSA keySize < 1024");254Security.setProperty("jdk.tls.disabledAlgorithms",255"SSLv3, RC4, DH keySize < 768");256257String keyFilename =258System.getProperty("test.src", ".") + "/" + pathToStores +259"/" + keyStoreFile;260String trustFilename =261System.getProperty("test.src", ".") + "/" + pathToStores +262"/" + trustStoreFile;263264System.setProperty("javax.net.ssl.keyStore", keyFilename);265System.setProperty("javax.net.ssl.keyStorePassword", passwd);266System.setProperty("javax.net.ssl.trustStore", trustFilename);267System.setProperty("javax.net.ssl.trustStorePassword", passwd);268269if (debug)270System.setProperty("javax.net.debug", "all");271272/*273* Start the tests.274*/275new EmptyCertificateAuthorities();276}277278Thread clientThread = null;279Thread serverThread = null;280281/*282* Primary constructor, used to drive remainder of the test.283*284* Fork off the other side, then do your work.285*/286EmptyCertificateAuthorities() throws Exception {287try {288if (separateServerThread) {289startServer(true);290startClient(false);291} else {292startClient(true);293startServer(false);294}295} catch (Exception e) {296// swallow for now. Show later297}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* Which side threw the error?311*/312Exception local;313Exception remote;314String whichRemote;315316if (separateServerThread) {317remote = serverException;318local = clientException;319whichRemote = "server";320} else {321remote = clientException;322local = serverException;323whichRemote = "client";324}325326/*327* If both failed, return the curthread's exception, but also328* print the remote side Exception329*/330if ((local != null) && (remote != null)) {331System.out.println(whichRemote + " also threw:");332remote.printStackTrace();333System.out.println();334throw local;335}336337if (remote != null) {338throw remote;339}340341if (local != null) {342throw local;343}344}345346void startServer(boolean newThread) throws Exception {347if (newThread) {348serverThread = new Thread() {349public void run() {350try {351doServerSide();352} catch (Exception e) {353/*354* Our server thread just died.355*356* Release the client, if not active already...357*/358System.err.println("Server died...");359serverReady = true;360serverException = e;361}362}363};364serverThread.start();365} else {366try {367doServerSide();368} catch (Exception e) {369serverException = e;370} finally {371serverReady = true;372}373}374}375376void startClient(boolean newThread) throws Exception {377if (newThread) {378clientThread = new Thread() {379public void run() {380try {381doClientSide();382} catch (Exception e) {383/*384* Our client thread just died.385*/386System.err.println("Client died...");387clientException = e;388}389}390};391clientThread.start();392} else {393try {394doClientSide();395} catch (Exception e) {396clientException = e;397}398}399}400}401402403