Path: blob/master/test/jdk/sun/security/ssl/ClientHandshaker/CipherSuiteOrder.java
41152 views
/*1* Copyright (c) 2001, 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.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* @test 1.3 01/03/0825* @bug 433053526* @summary Client should follow suite order in27* SSLSocket.setEnabledCipherSuites()28* @run main/othervm CipherSuiteOrder29*30* SunJSSE does not support dynamic system properties, no way to re-use31* system properties in samevm/agentvm mode.32* @author Jaya Hangal33*/3435import java.io.*;36import java.security.Security;37import javax.net.ssl.*;3839public class CipherSuiteOrder {4041/*42* =============================================================43* Set the various variables needed for the tests, then44* specify what tests to run on each side.45*/4647/*48* Should we run the client or server in a separate thread?49* Both sides can throw exceptions, but do you have a preference50* as to which side should be the main thread.51*/52static boolean separateServerThread = true;5354/*55* Where do we find the keystores?56*/57static String pathToStores = "../../../../javax/net/ssl/etc";58static String keyStoreFile = "keystore";59static String trustStoreFile = "truststore";60static String passwd = "passphrase";6162/*63* Is the server ready to serve?64*/65volatile static boolean serverReady = false;6667/*68* Turn on SSL debugging?69*/70static boolean debug = false;7172/*73* If the client or server is doing some kind of object creation74* that the other side depends on, and that thread prematurely75* exits, you may experience a hang. The test harness will76* terminate all hung threads after its timeout has expired,77* currently 3 minutes by default, but you might try to be78* smart about it....79*/8081/*82* Define the server side of the test.83*84* If the server prematurely exits, serverReady will be set to true85* to avoid infinite hangs.86*/87void doServerSide() throws Exception {88SSLServerSocketFactory sslssf =89(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();90SSLServerSocket sslServerSocket =91(SSLServerSocket) sslssf.createServerSocket(serverPort);9293serverPort = sslServerSocket.getLocalPort();9495/*96* Signal Client, we're ready for this connect.97*/98serverReady = true;99SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();100101/**102* The suite "SSL_RSA_WITH_RC4_128_MD5" is first in the103* client ordered cipher suite list. Place it last in this104* list to make sure that the server conforms to the client set105* ordering in choosing the cipher suite to use.106*/107String enabledSuites[] = {108"SSL_RSA_WITH_DES_CBC_SHA",109"SSL_DHE_DSS_WITH_DES_CBC_SHA",110"SSL_RSA_WITH_RC4_128_MD5"111};112sslSocket.setEnabledCipherSuites(enabledSuites);113System.out.println("");114System.out.println("server enabled suites: ");115System.out.println("=====================");116String suites[] = sslSocket.getEnabledCipherSuites();117for (int i = 0; i < suites.length; i++)118System.out.println(suites[i]);119System.out.println("");120121InputStream sslIS = sslSocket.getInputStream();122OutputStream sslOS = sslSocket.getOutputStream();123124int read = sslIS.read();125System.out.println("Server read: " + read);126sslOS.write(85);127sslOS.flush();128String cipherSuiteChosen = sslSocket.getSession().getCipherSuite();129System.out.println("Cipher suite in use: " +130cipherSuiteChosen);131sslSocket.close();132if (!cipherSuiteChosen.equals("SSL_RSA_WITH_RC4_128_MD5"))133throw new Exception("Test failed: Wrong cipher suite is chosen");134}135136/*137* Define the client side of the test.138*139* If the server prematurely exits, serverReady will be set to true140* to avoid infinite hangs.141*/142void doClientSide() throws Exception {143144/*145* Wait for server to get started.146*/147while (!serverReady) {148Thread.sleep(50);149}150151SSLSocketFactory sslsf =152(SSLSocketFactory) SSLSocketFactory.getDefault();153SSLSocket sslSocket = (SSLSocket)154sslsf.createSocket("localhost", serverPort);155156/*157* Pick a random order for the suites that is different from the158* default ordering.159*/160String enabledSuites[] = {161"SSL_RSA_WITH_RC4_128_MD5",162"SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA",163"SSL_RSA_WITH_RC4_128_SHA",164"SSL_DHE_DSS_WITH_DES_CBC_SHA"165};166sslSocket.setEnabledCipherSuites(enabledSuites);167System.out.println("");168System.out.println("client enabled suites: ");169System.out.println("======================");170String[] suites = sslSocket.getEnabledCipherSuites();171for (int i = 0; i < suites.length; i++)172System.out.println(suites[i]);173System.out.println("");174175InputStream sslIS = sslSocket.getInputStream();176OutputStream sslOS = sslSocket.getOutputStream();177178sslOS.write(80);179sslOS.flush();180int read = sslIS.read();181System.out.println("client read: " + read);182183sslSocket.close();184}185186/*187* =============================================================188* The remainder is just support stuff189*/190191// use any free port by default192volatile int serverPort = 0;193194volatile Exception serverException = null;195volatile Exception clientException = null;196197public static void main(String[] args) throws Exception {198// reset the security property to make sure that the algorithms199// and keys used in this test are not disabled.200Security.setProperty("jdk.tls.disabledAlgorithms", "");201202String keyFilename =203System.getProperty("test.src", "./") + "/" + pathToStores +204"/" + keyStoreFile;205String trustFilename =206System.getProperty("test.src", "./") + "/" + pathToStores +207"/" + trustStoreFile;208209System.setProperty("javax.net.ssl.keyStore", keyFilename);210System.setProperty("javax.net.ssl.keyStorePassword", passwd);211System.setProperty("javax.net.ssl.trustStore", trustFilename);212System.setProperty("javax.net.ssl.trustStorePassword", passwd);213214if (debug)215System.setProperty("javax.net.debug", "all");216217/*218* Start the tests.219*/220new CipherSuiteOrder();221}222223Thread clientThread = null;224Thread serverThread = null;225226/*227* Primary constructor, used to drive remainder of the test.228*229* Fork off the other side, then do your work.230*/231CipherSuiteOrder() throws Exception {232if (separateServerThread) {233startServer(true);234startClient(false);235} else {236startClient(true);237startServer(false);238}239240/*241* Wait for other side to close down.242*/243if (separateServerThread) {244serverThread.join();245} else {246clientThread.join();247}248249/*250* When we get here, the test is pretty much over.251*252* If the main thread excepted, that propagates back253* immediately. If the other thread threw an exception, we254* should report back.255*/256if (serverException != null)257throw serverException;258if (clientException != null)259throw clientException;260}261262void startServer(boolean newThread) throws Exception {263if (newThread) {264serverThread = new Thread() {265public void run() {266try {267doServerSide();268} catch (Exception e) {269/*270* Our server thread just died.271*272* Release the client, if not active already...273*/274System.err.println("Server died..." + e);275serverReady = true;276serverException = e;277}278}279};280serverThread.start();281} else {282doServerSide();283}284}285286void startClient(boolean newThread) throws Exception {287if (newThread) {288clientThread = new Thread() {289public void run() {290try {291doClientSide();292} catch (Exception e) {293/*294* Our client thread just died.295*/296System.err.println("Client died...");297clientException = e;298}299}300};301clientThread.start();302} else {303doClientSide();304}305}306}307308309