Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/net/ssl/SSLSession/RenegotiateTLS13.java
38853 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.FileInputStream;39import java.io.IOException;40import java.security.KeyStore;41import java.security.SecureRandom;4243public class RenegotiateTLS13 {4445static final String dataString = "This is a test";4647// Run the server as a thread instead of the client48static boolean separateServerThread = false;4950static String pathToStores = "../etc";51static String keyStoreFile = "keystore";52static String trustStoreFile = "truststore";53static String passwd = "passphrase";5455// Server ready flag56volatile static boolean serverReady = false;57// Turn on SSL debugging58static boolean debug = false;59// Server done flag60static boolean done = false;6162// Main server code6364void doServerSide() throws Exception {65SSLServerSocketFactory sslssf;66sslssf = initContext().getServerSocketFactory();67SSLServerSocket sslServerSocket =68(SSLServerSocket) sslssf.createServerSocket(serverPort);69serverPort = sslServerSocket.getLocalPort();7071serverReady = true;7273SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();7475DataInputStream sslIS =76new DataInputStream(sslSocket.getInputStream());77String s = "";78while (s.compareTo("done") != 0) {79try {80s = sslIS.readUTF();81System.out.println("Received: " + s);82} catch (IOException e) {83throw e;84}85}86done = true;87sslSocket.close();88}8990// Main client code91void doClientSide() throws Exception {9293while (!serverReady) {94Thread.sleep(5);95}9697SSLSocketFactory sslsf;98sslsf = initContext().getSocketFactory();99100SSLSocket sslSocket = (SSLSocket)101sslsf.createSocket("localhost", serverPort);102103DataOutputStream sslOS =104new DataOutputStream(sslSocket.getOutputStream());105106sslOS.writeUTF("With " + dataString);107sslOS.writeUTF("With " + dataString);108sslOS.writeUTF("With " + dataString);109110sslSocket.startHandshake();111112sslOS.writeUTF("With " + dataString);113sslOS.writeUTF("With " + dataString);114sslOS.writeUTF("With " + dataString);115116sslSocket.startHandshake();117118sslOS.writeUTF("With " + dataString);119sslOS.writeUTF("With " + dataString);120sslOS.writeUTF("With " + dataString);121sslOS.writeUTF("done");122123while (!done) {124Thread.sleep(5);125}126sslSocket.close();127}128129volatile int serverPort = 0;130131volatile Exception serverException = null;132volatile Exception clientException = null;133134public static void main(String[] args) throws Exception {135String keyFilename =136System.getProperty("test.src", "./") + "/" + pathToStores +137"/" + keyStoreFile;138String trustFilename =139System.getProperty("test.src", "./") + "/" + pathToStores +140"/" + trustStoreFile;141142System.setProperty("javax.net.ssl.keyStore", keyFilename);143System.setProperty("javax.net.ssl.keyStorePassword", passwd);144System.setProperty("javax.net.ssl.trustStore", trustFilename);145System.setProperty("javax.net.ssl.trustStorePassword", passwd);146147if (debug)148System.setProperty("javax.net.debug", "ssl");149150new RenegotiateTLS13();151}152153Thread clientThread = null;154Thread serverThread = null;155156/*157* Primary constructor, used to drive remainder of the test.158*159* Fork off the other side, then do your work.160*/161RenegotiateTLS13() throws Exception {162try {163if (separateServerThread) {164startServer(true);165startClient(false);166} else {167startClient(true);168startServer(false);169}170} catch (Exception e) {171// swallow for now. Show later172}173174/*175* Wait for other side to close down.176*/177if (separateServerThread) {178serverThread.join();179} else {180clientThread.join();181}182183/*184* When we get here, the test is pretty much over.185* Which side threw the error?186*/187Exception local;188Exception remote;189String whichRemote;190191if (separateServerThread) {192remote = serverException;193local = clientException;194whichRemote = "server";195} else {196remote = clientException;197local = serverException;198whichRemote = "client";199}200201/*202* If both failed, return the curthread's exception, but also203* print the remote side Exception204*/205if ((local != null) && (remote != null)) {206System.out.println(whichRemote + " also threw:");207remote.printStackTrace();208System.out.println();209throw local;210}211212if (remote != null) {213throw remote;214}215216if (local != null) {217throw local;218}219}220221void startServer(boolean newThread) throws Exception {222if (newThread) {223serverThread = new Thread() {224public void run() {225try {226doServerSide();227} catch (Exception e) {228/*229* Our server thread just died.230*231* Release the client, if not active already...232*/233System.err.println("Server died...");234serverReady = true;235serverException = e;236}237}238};239serverThread.start();240} else {241try {242doServerSide();243} catch (Exception e) {244serverException = e;245} finally {246serverReady = true;247}248}249}250251void startClient(boolean newThread) throws Exception {252if (newThread) {253clientThread = new Thread() {254public void run() {255try {256doClientSide();257} catch (Exception e) {258/*259* Our client thread just died.260*/261System.err.println("Client died...");262clientException = e;263}264}265};266clientThread.start();267} else {268try {269doClientSide();270} catch (Exception e) {271clientException = e;272}273}274}275276// Initialize context for TLS 1.3277SSLContext initContext() throws Exception {278System.out.println("Using TLS13");279SSLContext sc = SSLContext.getInstance("TLSv1.3");280KeyStore ks = KeyStore.getInstance("jks");281ks.load(new FileInputStream(new File(System.getProperty("javax.net.ssl.keyStore"))), passwd.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