Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/SSLSocketKeyLimit.java
41152 views
/*1* Copyright (c) 2018, 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.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* @bug 816487926* @library ../../27* @library /test/lib28* @modules java.base/sun.security.util29* @summary Verify AES/GCM's limits set in the jdk.tls.keyLimits property30* @run main SSLSocketKeyLimit 0 server AES/GCM/NoPadding keyupdate 100000031* @run main SSLSocketKeyLimit 0 client AES/GCM/NoPadding keyupdate 100000032* @run main SSLSocketKeyLimit 1 client AES/GCM/NoPadding keyupdate 2^2233*/3435/**36* Verify AES/GCM's limits set in the jdk.tls.keyLimits property37* start a new handshake sequence to renegotiate the symmetric key with an38* SSLSocket connection. This test verifies the handshake method was called39* via debugging info. It does not verify the renegotiation was successful40* as that is very hard.41*/4243import javax.net.ssl.KeyManagerFactory;44import javax.net.ssl.SSLContext;45import javax.net.ssl.SSLServerSocket;46import javax.net.ssl.SSLServerSocketFactory;47import javax.net.ssl.SSLSocket;48import javax.net.ssl.SSLSocketFactory;49import javax.net.ssl.TrustManagerFactory;50import java.io.File;51import java.io.InputStream;52import java.io.OutputStream;53import java.io.PrintWriter;54import java.security.KeyStore;55import java.security.SecureRandom;56import java.util.Arrays;5758import jdk.test.lib.process.OutputAnalyzer;59import jdk.test.lib.process.ProcessTools;60import jdk.test.lib.Utils;61import jdk.test.lib.hexdump.HexPrinter;6263public class SSLSocketKeyLimit {64SSLSocket socket;65private InputStream in;66private OutputStream out;6768static boolean serverReady = false;69static int serverPort = 0;7071static String pathToStores = "../../../../javax/net/ssl/etc/";72static String keyStoreFile = "keystore";73static String passwd = "passphrase";74static int dataLen = 10240;75static byte[] data = new byte[dataLen];76static boolean serverwrite = true;77int totalDataLen = 0;78static boolean done = false;7980SSLSocketKeyLimit() {81}8283SSLContext initContext() throws Exception {84SSLContext sc = SSLContext.getInstance("TLSv1.3");85KeyStore ks = KeyStore.getInstance(86new File(System.getProperty("javax.net.ssl.keyStore")),87passwd.toCharArray());88KeyManagerFactory kmf =89KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());90kmf.init(ks, passwd.toCharArray());91TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());92tmf.init(ks);93sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());94return sc;95}9697/**98* args should have two values: server|client, <limit size>99* Prepending 'p' is for internal use only.100*/101public static void main(String args[]) throws Exception {102if (args[0].compareTo("p") != 0) {103104boolean expectedFail = (Integer.parseInt(args[0]) == 1);105if (expectedFail) {106System.out.println("Test expected to not find updated msg");107}108//Write security property file to overwrite default109File f = new File("keyusage."+ System.nanoTime());110PrintWriter p = new PrintWriter(f);111p.write("jdk.tls.keyLimits=");112for (int i = 2; i < args.length; i++) {113p.write(" "+ args[i]);114}115p.close();116System.out.println("Keyusage path = " + f.getAbsolutePath());117System.setProperty("test.java.opts",118"-Dtest.src=" + System.getProperty("test.src") +119" -Dtest.jdk=" + System.getProperty("test.jdk") +120" -Djavax.net.debug=ssl,handshake" +121" -Djava.security.properties=" + f.getName());122123System.out.println("test.java.opts: " +124System.getProperty("test.java.opts"));125126ProcessBuilder pb = ProcessTools.createTestJvm(127Utils.addTestJavaOpts("SSLSocketKeyLimit", "p", args[1]));128129OutputAnalyzer output = ProcessTools.executeProcess(pb);130try {131if (expectedFail) {132output.shouldNotContain("KeyUpdate: write key updated");133output.shouldNotContain("KeyUpdate: read key updated");134} else {135output.shouldContain("trigger key update");136output.shouldContain("KeyUpdate: write key updated");137output.shouldContain("KeyUpdate: read key updated");138}139} catch (Exception e) {140throw e;141} finally {142System.out.println("-- BEGIN Stdout:");143System.out.println(output.getStdout());144System.out.println("-- END Stdout");145System.out.println("-- BEGIN Stderr:");146System.out.println(output.getStderr());147System.out.println("-- END Stderr");148}149return;150}151152if (args.length > 0 && args[0].compareToIgnoreCase("client") == 0) {153serverwrite = false;154}155156String keyFilename =157System.getProperty("test.src", "./") + "/" + pathToStores +158"/" + keyStoreFile;159160System.setProperty("javax.net.ssl.keyStore", keyFilename);161System.setProperty("javax.net.ssl.keyStorePassword", passwd);162163Arrays.fill(data, (byte)0x0A);164Thread ts = new Thread(new Server());165166ts.start();167while (!serverReady) {168Thread.sleep(100);169}170new Client().run();171ts.join(10000); // 10sec172System.exit(0);173}174175void write(SSLSocket s) throws Exception {176int i = 0;177in = s.getInputStream();178out = s.getOutputStream();179while (i++ < 150) {180out.write(data, 0, dataLen);181System.out.print("W");182in.readNBytes(1);183System.out.print("R");184}185out.write(0x0D);186out.flush();187188// Let read side all the data189while (!done) {190Thread.sleep(100);191}192out.close();193in.close();194}195196197void read(SSLSocket s) throws Exception {198byte[] buf = new byte[dataLen];199int len;200byte i = 0;201try {202System.out.println("Server: connected " + s.getSession().getCipherSuite());203in = s.getInputStream();204out = s.getOutputStream();205while (true) {206len = in.read(buf, 0, dataLen);207System.out.print("r");208out.write(i++);209System.out.print("w");210for (byte b: buf) {211if (b == 0x0A || b == 0x0D) {212continue;213}214System.out.println("\nData invalid: " + HexPrinter.minimal().toString(buf));215break;216}217218if (len > 0 && buf[len-1] == 0x0D) {219System.out.println("got end byte");220break;221}222totalDataLen += len;223}224} catch (Exception e) {225System.out.println("\n" + e.getMessage());226e.printStackTrace();227} finally {228// Tell write side that we are done reading229out.close();230in.close();231done = true;232}233System.out.println("\nTotalDataLen = " + totalDataLen);234}235236static class Server extends SSLSocketKeyLimit implements Runnable {237private SSLServerSocketFactory ssf;238private SSLServerSocket ss;239Server() {240super();241try {242ssf = initContext().getServerSocketFactory();243ss = (SSLServerSocket) ssf.createServerSocket(serverPort);244serverPort = ss.getLocalPort();245} catch (Exception e) {246System.out.println("server: " + e.getMessage());247e.printStackTrace();248}249}250251public void run() {252try {253serverReady = true;254System.out.println("Server waiting... port: " + serverPort);255socket = (SSLSocket) ss.accept();256if (serverwrite) {257write(socket);258} else {259read(socket);260}261262socket.close();263} catch (Exception e) {264System.out.println("server: " + e.getMessage());265e.printStackTrace();266}267System.out.println("Server closed");268}269}270271272static class Client extends SSLSocketKeyLimit implements Runnable {273private SSLSocketFactory sf;274275Client() {276super();277}278279public void run() {280try {281sf = initContext().getSocketFactory();282System.out.println("Client: connecting... port: " + serverPort);283socket = (SSLSocket)sf.createSocket("localhost", serverPort);284System.out.println("Client: connected." + socket.getSession().getCipherSuite());285286// Opposite of what the server does287if (!serverwrite) {288write(socket);289} else {290read(socket);291}292293} catch (Exception e) {294System.err.println("client: " + e.getMessage());295e.printStackTrace();296}297}298}299}300301302