Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/net/ssl/TLSv11/GenericStreamCipher.java
38853 views
/*1* Copyright (c) 2010, 2015, 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* @run main/othervm GenericStreamCipher30*31* SunJSSE does not support dynamic system properties, no way to re-use32* system properties in samevm/agentvm mode.33*34* @author Xuelei Fan35*/3637import java.io.*;38import java.security.Security;39import javax.net.ssl.*;4041public class GenericStreamCipher {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 = false;5556/*57* Where do we find the keystores?58*/59static String pathToStores = "../etc";60static String keyStoreFile = "keystore";61static String trustStoreFile = "truststore";62static String passwd = "passphrase";6364/*65* Is the server ready to serve?66*/67volatile static boolean serverReady = false;6869/*70* Turn on SSL debugging?71*/72static boolean debug = false;7374/*75* If the client or server is doing some kind of object creation76* that the other side depends on, and that thread prematurely77* exits, you may experience a hang. The test harness will78* terminate all hung threads after its timeout has expired,79* currently 3 minutes by default, but you might try to be80* smart about it....81*/8283/*84* Define the server side of the test.85*86* If the server prematurely exits, serverReady will be set to true87* to avoid infinite hangs.88*/89void doServerSide() throws Exception {90SSLServerSocketFactory sslssf =91(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();92SSLServerSocket sslServerSocket =93(SSLServerSocket) sslssf.createServerSocket(serverPort);9495// enable a stream cipher96sslServerSocket.setEnabledCipherSuites(97new String[] {"SSL_RSA_WITH_RC4_128_MD5"});9899serverPort = sslServerSocket.getLocalPort();100101/*102* Signal Client, we're ready for his connect.103*/104serverReady = true;105106SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();107InputStream sslIS = sslSocket.getInputStream();108OutputStream sslOS = sslSocket.getOutputStream();109110sslIS.read();111sslOS.write('A');112sslOS.flush();113114sslSocket.close();115}116117/*118* Define the client side of the test.119*120* If the server prematurely exits, serverReady will be set to true121* to avoid infinite hangs.122*/123void doClientSide() throws Exception {124125/*126* Wait for server to get started.127*/128while (!serverReady) {129Thread.sleep(50);130}131132SSLSocketFactory sslsf =133(SSLSocketFactory) SSLSocketFactory.getDefault();134SSLSocket sslSocket = (SSLSocket)135sslsf.createSocket("localhost", serverPort);136137// enable TLSv1.1 only138sslSocket.setEnabledProtocols(new String[] {"TLSv1.1"});139140// enable a stream cipher141sslSocket.setEnabledCipherSuites(142new String[] {"SSL_RSA_WITH_RC4_128_MD5"});143144InputStream sslIS = sslSocket.getInputStream();145OutputStream sslOS = sslSocket.getOutputStream();146147sslOS.write('B');148sslOS.flush();149sslIS.read();150151sslSocket.close();152}153154/*155* =============================================================156* The remainder is just support stuff157*/158159// use any free port by default160volatile int serverPort = 0;161162volatile Exception serverException = null;163volatile Exception clientException = null;164165public static void main(String[] args) throws Exception {166// reset the security property to make sure that the algorithms167// and keys used in this test are not disabled.168Security.setProperty("jdk.tls.disabledAlgorithms", "");169170String keyFilename =171System.getProperty("test.src", ".") + "/" + pathToStores +172"/" + keyStoreFile;173String trustFilename =174System.getProperty("test.src", ".") + "/" + pathToStores +175"/" + trustStoreFile;176177System.setProperty("javax.net.ssl.keyStore", keyFilename);178System.setProperty("javax.net.ssl.keyStorePassword", passwd);179System.setProperty("javax.net.ssl.trustStore", trustFilename);180System.setProperty("javax.net.ssl.trustStorePassword", passwd);181182if (debug)183System.setProperty("javax.net.debug", "all");184185/*186* Start the tests.187*/188new GenericStreamCipher();189}190191Thread clientThread = null;192Thread serverThread = null;193194/*195* Primary constructor, used to drive remainder of the test.196*197* Fork off the other side, then do your work.198*/199GenericStreamCipher() throws Exception {200try {201if (separateServerThread) {202startServer(true);203startClient(false);204} else {205startClient(true);206startServer(false);207}208} catch (Exception e) {209// swallow for now. Show later210}211212/*213* Wait for other side to close down.214*/215if (separateServerThread) {216serverThread.join();217} else {218clientThread.join();219}220221/*222* When we get here, the test is pretty much over.223* Which side threw the error?224*/225Exception local;226Exception remote;227String whichRemote;228229if (separateServerThread) {230remote = serverException;231local = clientException;232whichRemote = "server";233} else {234remote = clientException;235local = serverException;236whichRemote = "client";237}238239/*240* If both failed, return the curthread's exception, but also241* print the remote side Exception242*/243if ((local != null) && (remote != null)) {244System.out.println(whichRemote + " also threw:");245remote.printStackTrace();246System.out.println();247throw local;248}249250if (remote != null) {251throw remote;252}253254if (local != null) {255throw local;256}257}258259void startServer(boolean newThread) throws Exception {260if (newThread) {261serverThread = new Thread() {262public void run() {263try {264doServerSide();265} catch (Exception e) {266/*267* Our server thread just died.268*269* Release the client, if not active already...270*/271System.err.println("Server died...");272serverReady = true;273serverException = e;274}275}276};277serverThread.start();278} else {279try {280doServerSide();281} catch (Exception e) {282serverException = e;283} finally {284serverReady = true;285}286}287}288289void startClient(boolean newThread) throws Exception {290if (newThread) {291clientThread = new Thread() {292public void run() {293try {294doClientSide();295} catch (Exception e) {296/*297* Our client thread just died.298*/299System.err.println("Client died...");300clientException = e;301}302}303};304clientThread.start();305} else {306try {307doClientSide();308} catch (Exception e) {309clientException = e;310}311}312}313}314315316