Path: blob/master/test/jdk/javax/net/ssl/TLSv11/GenericBlockCipher.java
41152 views
/*1* Copyright (c) 2010, 2020, 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* @library /test/lib30* @modules java.security.jgss31* java.security.jgss/sun.security.jgss.krb532* java.security.jgss/sun.security.krb5:+open33* java.security.jgss/sun.security.krb5.internal:+open34* java.security.jgss/sun.security.krb5.internal.ccache35* java.security.jgss/sun.security.krb5.internal.crypto36* java.security.jgss/sun.security.krb5.internal.ktab37* java.base/sun.security.util38* @run main/othervm GenericBlockCipher39*40* SunJSSE does not support dynamic system properties, no way to re-use41* system properties in samevm/agentvm mode.42*43* @author Xuelei Fan44*/4546import java.io.InputStream;47import java.io.OutputStream;48import javax.net.ssl.SSLServerSocket;49import javax.net.ssl.SSLServerSocketFactory;50import javax.net.ssl.SSLSocket;51import javax.net.ssl.SSLSocketFactory;5253import jdk.test.lib.security.SecurityUtils;5455public class GenericBlockCipher {5657/*58* =============================================================59* Set the various variables needed for the tests, then60* specify what tests to run on each side.61*/6263/*64* Should we run the client or server in a separate thread?65* Both sides can throw exceptions, but do you have a preference66* as to which side should be the main thread.67*/68static boolean separateServerThread = false;6970/*71* Where do we find the keystores?72*/73static String pathToStores = "../etc";74static String keyStoreFile = "keystore";75static String trustStoreFile = "truststore";76static String passwd = "passphrase";7778/*79* Is the server ready to serve?80*/81volatile static boolean serverReady = false;8283/*84* Turn on SSL debugging?85*/86static boolean debug = false;8788/*89* If the client or server is doing some kind of object creation90* that the other side depends on, and that thread prematurely91* exits, you may experience a hang. The test harness will92* terminate all hung threads after its timeout has expired,93* currently 3 minutes by default, but you might try to be94* smart about it....95*/9697/*98* Define the server side of the test.99*100* If the server prematurely exits, serverReady will be set to true101* to avoid infinite hangs.102*/103void doServerSide() throws Exception {104SSLServerSocketFactory sslssf =105(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();106SSLServerSocket sslServerSocket =107(SSLServerSocket) sslssf.createServerSocket(serverPort);108109serverPort = sslServerSocket.getLocalPort();110111/*112* Signal Client, we're ready for his connect.113*/114serverReady = true;115116SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();117InputStream sslIS = sslSocket.getInputStream();118OutputStream sslOS = sslSocket.getOutputStream();119120sslIS.read();121sslOS.write('A');122sslOS.flush();123124sslSocket.close();125}126127/*128* Define the client side of the test.129*130* If the server prematurely exits, serverReady will be set to true131* to avoid infinite hangs.132*/133void doClientSide() throws Exception {134135/*136* Wait for server to get started.137*/138while (!serverReady) {139Thread.sleep(50);140}141142SSLSocketFactory sslsf =143(SSLSocketFactory) SSLSocketFactory.getDefault();144SSLSocket sslSocket = (SSLSocket)145sslsf.createSocket("localhost", serverPort);146147// enable TLSv1.1 only148sslSocket.setEnabledProtocols(new String[] {"TLSv1.1"});149150// enable a block cipher151sslSocket.setEnabledCipherSuites(152new String[] {"TLS_RSA_WITH_AES_128_CBC_SHA"});153154InputStream sslIS = sslSocket.getInputStream();155OutputStream sslOS = sslSocket.getOutputStream();156157sslOS.write('B');158sslOS.flush();159sslIS.read();160161sslSocket.close();162}163164/*165* =============================================================166* The remainder is just support stuff167*/168169// use any free port by default170volatile int serverPort = 0;171172volatile Exception serverException = null;173volatile Exception clientException = null;174175public static void main(String[] args) throws Exception {176// Re-enable TLSv1.1 since test depends on it.177SecurityUtils.removeFromDisabledTlsAlgs("TLSv1.1");178179String keyFilename =180System.getProperty("test.src", ".") + "/" + pathToStores +181"/" + keyStoreFile;182String trustFilename =183System.getProperty("test.src", ".") + "/" + pathToStores +184"/" + trustStoreFile;185186System.setProperty("javax.net.ssl.keyStore", keyFilename);187System.setProperty("javax.net.ssl.keyStorePassword", passwd);188System.setProperty("javax.net.ssl.trustStore", trustFilename);189System.setProperty("javax.net.ssl.trustStorePassword", passwd);190191if (debug)192System.setProperty("javax.net.debug", "all");193194/*195* Start the tests.196*/197new GenericBlockCipher();198}199200Thread clientThread = null;201Thread serverThread = null;202203/*204* Primary constructor, used to drive remainder of the test.205*206* Fork off the other side, then do your work.207*/208GenericBlockCipher() throws Exception {209try {210if (separateServerThread) {211startServer(true);212startClient(false);213} else {214startClient(true);215startServer(false);216}217} catch (Exception e) {218// swallow for now. Show later219}220221/*222* Wait for other side to close down.223*/224if (separateServerThread) {225serverThread.join();226} else {227clientThread.join();228}229230/*231* When we get here, the test is pretty much over.232* Which side threw the error?233*/234Exception local;235Exception remote;236String whichRemote;237238if (separateServerThread) {239remote = serverException;240local = clientException;241whichRemote = "server";242} else {243remote = clientException;244local = serverException;245whichRemote = "client";246}247248/*249* If both failed, return the curthread's exception, but also250* print the remote side Exception251*/252if ((local != null) && (remote != null)) {253System.out.println(whichRemote + " also threw:");254remote.printStackTrace();255System.out.println();256throw local;257}258259if (remote != null) {260throw remote;261}262263if (local != null) {264throw local;265}266}267268void startServer(boolean newThread) throws Exception {269if (newThread) {270serverThread = new Thread() {271public void run() {272try {273doServerSide();274} catch (Exception e) {275/*276* Our server thread just died.277*278* Release the client, if not active already...279*/280System.err.println("Server died...");281serverReady = true;282serverException = e;283}284}285};286serverThread.start();287} else {288try {289doServerSide();290} catch (Exception e) {291serverException = e;292} finally {293serverReady = true;294}295}296}297298void startClient(boolean newThread) throws Exception {299if (newThread) {300clientThread = new Thread() {301public void run() {302try {303doClientSide();304} catch (Exception e) {305/*306* Our client thread just died.307*/308System.err.println("Client died...");309clientException = e;310}311}312};313clientThread.start();314} else {315try {316doClientSide();317} catch (Exception e) {318clientException = e;319}320}321}322}323324325