Path: blob/master/test/jdk/javax/net/ssl/TLSv11/ExportableStreamCipher.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* @modules java.security.jgss35* java.security.jgss/sun.security.jgss.krb536* java.security.jgss/sun.security.krb5:+open37* java.security.jgss/sun.security.krb5.internal:+open38* java.security.jgss/sun.security.krb5.internal.ccache39* java.security.jgss/sun.security.krb5.internal.crypto40* java.security.jgss/sun.security.krb5.internal.ktab41* java.base/sun.security.util42* @run main/othervm ExportableStreamCipher43* @author Xuelei Fan44*/4546import java.io.IOException;47import java.io.InputStream;48import java.io.OutputStream;49import javax.net.ssl.SSLException;50import javax.net.ssl.SSLHandshakeException;51import javax.net.ssl.SSLServerSocket;52import javax.net.ssl.SSLServerSocketFactory;53import javax.net.ssl.SSLSocket;54import javax.net.ssl.SSLSocketFactory;5556public class ExportableStreamCipher {5758/*59* =============================================================60* Set the various variables needed for the tests, then61* specify what tests to run on each side.62*/6364/*65* Should we run the client or server in a separate thread?66* Both sides can throw exceptions, but do you have a preference67* as to which side should be the main thread.68*/69static boolean separateServerThread = false;7071/*72* Where do we find the keystores?73*/74static String pathToStores = "../etc";75static String keyStoreFile = "keystore";76static String trustStoreFile = "truststore";77static String passwd = "passphrase";7879/*80* Is the server ready to serve?81*/82volatile static boolean serverReady = false;8384/*85* Turn on SSL debugging?86*/87static boolean debug = false;8889/*90* If the client or server is doing some kind of object creation91* that the other side depends on, and that thread prematurely92* exits, you may experience a hang. The test harness will93* terminate all hung threads after its timeout has expired,94* currently 3 minutes by default, but you might try to be95* smart about it....96*/9798/*99* Define the server side of the test.100*101* If the server prematurely exits, serverReady will be set to true102* to avoid infinite hangs.103*/104void doServerSide() throws Exception {105SSLServerSocketFactory sslssf =106(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();107SSLServerSocket sslServerSocket =108(SSLServerSocket) sslssf.createServerSocket(serverPort);109110serverPort = sslServerSocket.getLocalPort();111112/*113* Signal Client, we're ready for his connect.114*/115serverReady = true;116117SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();118InputStream sslIS = sslSocket.getInputStream();119OutputStream sslOS = sslSocket.getOutputStream();120121boolean interrupted = false;122try {123sslIS.read();124sslOS.write('A');125sslOS.flush();126} catch (IOException ioe) {127// get the expected exception128interrupted = true;129} finally {130sslSocket.close();131}132133if (!interrupted) {134throw new SSLHandshakeException(135"A weak cipher suite is negotiated, " +136"TLSv1.1 must not negotiate the exportable cipher suites.");137}138}139140/*141* Define the client side of the test.142*143* If the server prematurely exits, serverReady will be set to true144* to avoid infinite hangs.145*/146void doClientSide() throws Exception {147148/*149* Wait for server to get started.150*/151while (!serverReady) {152Thread.sleep(50);153}154155SSLSocketFactory sslsf =156(SSLSocketFactory) SSLSocketFactory.getDefault();157SSLSocket sslSocket = (SSLSocket)158sslsf.createSocket("localhost", serverPort);159160// enable TLSv1.1 only161sslSocket.setEnabledProtocols(new String[] {"TLSv1.1"});162163// enable a exportable stream cipher164sslSocket.setEnabledCipherSuites(165new String[] {"SSL_RSA_EXPORT_WITH_RC4_40_MD5"});166167InputStream sslIS = sslSocket.getInputStream();168OutputStream sslOS = sslSocket.getOutputStream();169170boolean interrupted = false;171try {172sslOS.write('B');173sslOS.flush();174sslIS.read();175} catch (SSLException ssle) {176// get the expected exception177interrupted = true;178} finally {179sslSocket.close();180}181182if (!interrupted) {183throw new SSLHandshakeException(184"A weak cipher suite is negotiated, " +185"TLSv1.1 must not negotiate the exportable cipher suites.");186}187}188189/*190* =============================================================191* The remainder is just support stuff192*/193194// use any free port by default195volatile int serverPort = 0;196197volatile Exception serverException = null;198volatile Exception clientException = null;199200public static void main(String[] args) throws Exception {201String keyFilename =202System.getProperty("test.src", ".") + "/" + pathToStores +203"/" + keyStoreFile;204String trustFilename =205System.getProperty("test.src", ".") + "/" + pathToStores +206"/" + trustStoreFile;207208System.setProperty("javax.net.ssl.keyStore", keyFilename);209System.setProperty("javax.net.ssl.keyStorePassword", passwd);210System.setProperty("javax.net.ssl.trustStore", trustFilename);211System.setProperty("javax.net.ssl.trustStorePassword", passwd);212213if (debug)214System.setProperty("javax.net.debug", "all");215216/*217* Start the tests.218*/219new ExportableStreamCipher();220}221222Thread clientThread = null;223Thread serverThread = null;224225/*226* Primary constructor, used to drive remainder of the test.227*228* Fork off the other side, then do your work.229*/230ExportableStreamCipher() throws Exception {231try {232if (separateServerThread) {233startServer(true);234startClient(false);235} else {236startClient(true);237startServer(false);238}239} catch (Exception e) {240// swallow for now. Show later241}242243/*244* Wait for other side to close down.245*/246if (separateServerThread) {247serverThread.join();248} else {249clientThread.join();250}251252/*253* When we get here, the test is pretty much over.254* Which side threw the error?255*/256Exception local;257Exception remote;258String whichRemote;259260if (separateServerThread) {261remote = serverException;262local = clientException;263whichRemote = "server";264} else {265remote = clientException;266local = serverException;267whichRemote = "client";268}269270/*271* If both failed, return the curthread's exception, but also272* print the remote side Exception273*/274if ((local != null) && (remote != null)) {275System.out.println(whichRemote + " also threw:");276remote.printStackTrace();277System.out.println();278throw local;279}280281if (remote != null) {282throw remote;283}284285if (local != null) {286throw local;287}288}289290void startServer(boolean newThread) throws Exception {291if (newThread) {292serverThread = new Thread() {293public void run() {294try {295doServerSide();296} catch (Exception e) {297/*298* Our server thread just died.299*300* Release the client, if not active already...301*/302System.err.println("Server died...");303serverReady = true;304serverException = e;305}306}307};308serverThread.start();309} else {310try {311doServerSide();312} catch (Exception e) {313serverException = e;314} finally {315serverReady = true;316}317}318}319320void startClient(boolean newThread) throws Exception {321if (newThread) {322clientThread = new Thread() {323public void run() {324try {325doClientSide();326} catch (Exception e) {327/*328* Our client thread just died.329*/330System.err.println("Client died...");331clientException = e;332}333}334};335clientThread.start();336} else {337try {338doClientSide();339} catch (Exception e) {340clientException = e;341}342}343}344}345346347