Path: blob/master/test/jdk/javax/net/ssl/SSLSession/RenegotiateTLS13.java
41152 views
/*1* Copyright (c) 2018, 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* @test25* @run main/othervm -Djavax.net.debug=ssl RenegotiateTLS1326*/2728import javax.net.ssl.KeyManagerFactory;29import javax.net.ssl.SSLContext;30import javax.net.ssl.SSLServerSocket;31import javax.net.ssl.SSLServerSocketFactory;32import javax.net.ssl.SSLSocket;33import javax.net.ssl.SSLSocketFactory;34import javax.net.ssl.TrustManagerFactory;35import java.io.DataInputStream;36import java.io.DataOutputStream;37import java.io.File;38import java.io.IOException;39import java.security.KeyStore;40import java.security.SecureRandom;4142public class RenegotiateTLS13 {4344static final String dataString = "This is a test";4546// Run the server as a thread instead of the client47static boolean separateServerThread = false;4849static String pathToStores = "../etc";50static String keyStoreFile = "keystore";51static String trustStoreFile = "truststore";52static String passwd = "passphrase";5354// Server ready flag55volatile static boolean serverReady = false;56// Turn on SSL debugging57static boolean debug = false;58// Server done flag59static boolean done = false;6061// Main server code6263void doServerSide() throws Exception {64SSLServerSocketFactory sslssf;65sslssf = initContext().getServerSocketFactory();66SSLServerSocket sslServerSocket =67(SSLServerSocket) sslssf.createServerSocket(serverPort);68serverPort = sslServerSocket.getLocalPort();6970serverReady = true;7172SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();7374DataInputStream sslIS =75new DataInputStream(sslSocket.getInputStream());76String s = "";77while (s.compareTo("done") != 0) {78try {79s = sslIS.readUTF();80System.out.println("Received: " + s);81} catch (IOException e) {82throw e;83}84}85done = true;86sslSocket.close();87}8889// Main client code90void doClientSide() throws Exception {9192while (!serverReady) {93Thread.sleep(5);94}9596SSLSocketFactory sslsf;97sslsf = initContext().getSocketFactory();9899SSLSocket sslSocket = (SSLSocket)100sslsf.createSocket("localhost", serverPort);101102DataOutputStream sslOS =103new DataOutputStream(sslSocket.getOutputStream());104105sslOS.writeUTF("With " + dataString);106sslOS.writeUTF("With " + dataString);107sslOS.writeUTF("With " + dataString);108109sslSocket.startHandshake();110111sslOS.writeUTF("With " + dataString);112sslOS.writeUTF("With " + dataString);113sslOS.writeUTF("With " + dataString);114115sslSocket.startHandshake();116117sslOS.writeUTF("With " + dataString);118sslOS.writeUTF("With " + dataString);119sslOS.writeUTF("With " + dataString);120sslOS.writeUTF("done");121122while (!done) {123Thread.sleep(5);124}125sslSocket.close();126}127128volatile int serverPort = 0;129130volatile Exception serverException = null;131volatile Exception clientException = null;132133public static void main(String[] args) throws Exception {134String keyFilename =135System.getProperty("test.src", "./") + "/" + pathToStores +136"/" + keyStoreFile;137String trustFilename =138System.getProperty("test.src", "./") + "/" + pathToStores +139"/" + trustStoreFile;140141System.setProperty("javax.net.ssl.keyStore", keyFilename);142System.setProperty("javax.net.ssl.keyStorePassword", passwd);143System.setProperty("javax.net.ssl.trustStore", trustFilename);144System.setProperty("javax.net.ssl.trustStorePassword", passwd);145146if (debug)147System.setProperty("javax.net.debug", "ssl");148149new RenegotiateTLS13();150}151152Thread clientThread = null;153Thread serverThread = null;154155/*156* Primary constructor, used to drive remainder of the test.157*158* Fork off the other side, then do your work.159*/160RenegotiateTLS13() throws Exception {161try {162if (separateServerThread) {163startServer(true);164startClient(false);165} else {166startClient(true);167startServer(false);168}169} catch (Exception e) {170// swallow for now. Show later171}172173/*174* Wait for other side to close down.175*/176if (separateServerThread) {177serverThread.join();178} else {179clientThread.join();180}181182/*183* When we get here, the test is pretty much over.184* Which side threw the error?185*/186Exception local;187Exception remote;188String whichRemote;189190if (separateServerThread) {191remote = serverException;192local = clientException;193whichRemote = "server";194} else {195remote = clientException;196local = serverException;197whichRemote = "client";198}199200/*201* If both failed, return the curthread's exception, but also202* print the remote side Exception203*/204if ((local != null) && (remote != null)) {205System.out.println(whichRemote + " also threw:");206remote.printStackTrace();207System.out.println();208throw local;209}210211if (remote != null) {212throw remote;213}214215if (local != null) {216throw local;217}218}219220void startServer(boolean newThread) throws Exception {221if (newThread) {222serverThread = new Thread() {223public void run() {224try {225doServerSide();226} catch (Exception e) {227/*228* Our server thread just died.229*230* Release the client, if not active already...231*/232System.err.println("Server died...");233serverReady = true;234serverException = e;235}236}237};238serverThread.start();239} else {240try {241doServerSide();242} catch (Exception e) {243serverException = e;244} finally {245serverReady = true;246}247}248}249250void startClient(boolean newThread) throws Exception {251if (newThread) {252clientThread = new Thread() {253public void run() {254try {255doClientSide();256} catch (Exception e) {257/*258* Our client thread just died.259*/260System.err.println("Client died...");261clientException = e;262}263}264};265clientThread.start();266} else {267try {268doClientSide();269} catch (Exception e) {270clientException = e;271}272}273}274275// Initialize context for TLS 1.3276SSLContext initContext() throws Exception {277System.out.println("Using TLS13");278SSLContext sc = SSLContext.getInstance("TLSv1.3");279KeyStore ks = KeyStore.getInstance(280new File(System.getProperty("javax.net.ssl.keyStore")),281passwd.toCharArray());282KeyManagerFactory kmf = KeyManagerFactory.getInstance(283KeyManagerFactory.getDefaultAlgorithm());284kmf.init(ks, passwd.toCharArray());285TrustManagerFactory tmf = TrustManagerFactory.getInstance(286TrustManagerFactory.getDefaultAlgorithm());287tmf.init(ks);288sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());289return sc;290}291}292293294