Path: blob/master/test/jdk/javax/net/ssl/TLSv11/GenericStreamCipher.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* @test27* @bug 487318828* @summary Support TLS 1.129* @modules java.security.jgss30* java.security.jgss/sun.security.jgss.krb531* java.security.jgss/sun.security.krb5:+open32* java.security.jgss/sun.security.krb5.internal:+open33* java.security.jgss/sun.security.krb5.internal.ccache34* java.security.jgss/sun.security.krb5.internal.crypto35* java.security.jgss/sun.security.krb5.internal.ktab36* java.base/sun.security.util37* @run main/othervm GenericStreamCipher38*39* SunJSSE does not support dynamic system properties, no way to re-use40* system properties in samevm/agentvm mode.41*42* @author Xuelei Fan43*/4445import java.io.InputStream;46import java.io.OutputStream;47import java.security.Security;48import javax.net.ssl.SSLServerSocket;49import javax.net.ssl.SSLServerSocketFactory;50import javax.net.ssl.SSLSocket;51import javax.net.ssl.SSLSocketFactory;5253public class GenericStreamCipher {5455/*56* =============================================================57* Set the various variables needed for the tests, then58* specify what tests to run on each side.59*/6061/*62* Should we run the client or server in a separate thread?63* Both sides can throw exceptions, but do you have a preference64* as to which side should be the main thread.65*/66static boolean separateServerThread = false;6768/*69* Where do we find the keystores?70*/71static String pathToStores = "../etc";72static String keyStoreFile = "keystore";73static String trustStoreFile = "truststore";74static String passwd = "passphrase";7576/*77* Is the server ready to serve?78*/79volatile static boolean serverReady = false;8081/*82* Turn on SSL debugging?83*/84static boolean debug = false;8586/*87* If the client or server is doing some kind of object creation88* that the other side depends on, and that thread prematurely89* exits, you may experience a hang. The test harness will90* terminate all hung threads after its timeout has expired,91* currently 3 minutes by default, but you might try to be92* smart about it....93*/9495/*96* Define the server side of the test.97*98* If the server prematurely exits, serverReady will be set to true99* to avoid infinite hangs.100*/101void doServerSide() throws Exception {102SSLServerSocketFactory sslssf =103(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();104SSLServerSocket sslServerSocket =105(SSLServerSocket) sslssf.createServerSocket(serverPort);106107// enable a stream cipher108sslServerSocket.setEnabledCipherSuites(109new String[] {"SSL_RSA_WITH_RC4_128_MD5"});110111serverPort = sslServerSocket.getLocalPort();112113/*114* Signal Client, we're ready for his connect.115*/116serverReady = true;117118SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();119InputStream sslIS = sslSocket.getInputStream();120OutputStream sslOS = sslSocket.getOutputStream();121122sslIS.read();123sslOS.write('A');124sslOS.flush();125126sslSocket.close();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 =145(SSLSocketFactory) SSLSocketFactory.getDefault();146SSLSocket sslSocket = (SSLSocket)147sslsf.createSocket("localhost", serverPort);148149// enable TLSv1.1 only150sslSocket.setEnabledProtocols(new String[] {"TLSv1.1"});151152// enable a stream cipher153sslSocket.setEnabledCipherSuites(154new String[] {"SSL_RSA_WITH_RC4_128_MD5"});155156InputStream sslIS = sslSocket.getInputStream();157OutputStream sslOS = sslSocket.getOutputStream();158159sslOS.write('B');160sslOS.flush();161sslIS.read();162163sslSocket.close();164}165166/*167* =============================================================168* The remainder is just support stuff169*/170171// use any free port by default172volatile int serverPort = 0;173174volatile Exception serverException = null;175volatile Exception clientException = null;176177public static void main(String[] args) throws Exception {178// reset the security property to make sure that the algorithms179// and keys used in this test are not disabled.180Security.setProperty("jdk.tls.disabledAlgorithms", "");181182String keyFilename =183System.getProperty("test.src", ".") + "/" + pathToStores +184"/" + keyStoreFile;185String trustFilename =186System.getProperty("test.src", ".") + "/" + pathToStores +187"/" + trustStoreFile;188189System.setProperty("javax.net.ssl.keyStore", keyFilename);190System.setProperty("javax.net.ssl.keyStorePassword", passwd);191System.setProperty("javax.net.ssl.trustStore", trustFilename);192System.setProperty("javax.net.ssl.trustStorePassword", passwd);193194if (debug)195System.setProperty("javax.net.debug", "all");196197/*198* Start the tests.199*/200new GenericStreamCipher();201}202203Thread clientThread = null;204Thread serverThread = null;205206/*207* Primary constructor, used to drive remainder of the test.208*209* Fork off the other side, then do your work.210*/211GenericStreamCipher() throws Exception {212try {213if (separateServerThread) {214startServer(true);215startClient(false);216} else {217startClient(true);218startServer(false);219}220} catch (Exception e) {221// swallow for now. Show later222}223224/*225* Wait for other side to close down.226*/227if (separateServerThread) {228serverThread.join();229} else {230clientThread.join();231}232233/*234* When we get here, the test is pretty much over.235* Which side threw the error?236*/237Exception local;238Exception remote;239String whichRemote;240241if (separateServerThread) {242remote = serverException;243local = clientException;244whichRemote = "server";245} else {246remote = clientException;247local = serverException;248whichRemote = "client";249}250251/*252* If both failed, return the curthread's exception, but also253* print the remote side Exception254*/255if ((local != null) && (remote != null)) {256System.out.println(whichRemote + " also threw:");257remote.printStackTrace();258System.out.println();259throw local;260}261262if (remote != null) {263throw remote;264}265266if (local != null) {267throw local;268}269}270271void startServer(boolean newThread) throws Exception {272if (newThread) {273serverThread = new Thread() {274public void run() {275try {276doServerSide();277} catch (Exception e) {278/*279* Our server thread just died.280*281* Release the client, if not active already...282*/283System.err.println("Server died...");284serverReady = true;285serverException = e;286}287}288};289serverThread.start();290} else {291try {292doServerSide();293} catch (Exception e) {294serverException = e;295} finally {296serverReady = true;297}298}299}300301void startClient(boolean newThread) throws Exception {302if (newThread) {303clientThread = new Thread() {304public void run() {305try {306doClientSide();307} catch (Exception e) {308/*309* Our client thread just died.310*/311System.err.println("Client died...");312clientException = e;313}314}315};316clientThread.start();317} else {318try {319doClientSide();320} catch (Exception e) {321clientException = e;322}323}324}325}326327328