Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLSocketImpl/SSLSocketKeyLimit.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* @bug 816487926* @library /lib/testlibrary ../../27* @summary Verify AES/GCM's limits set in the jdk.tls.keyLimits property28* @run main SSLSocketKeyLimit 0 server AES/GCM/NoPadding keyupdate 100000029* @run main SSLSocketKeyLimit 0 client AES/GCM/NoPadding keyupdate 100000030* @run main SSLSocketKeyLimit 1 client AES/GCM/NoPadding keyupdate 2^2231*/3233/**34* Verify AES/GCM's limits set in the jdk.tls.keyLimits property35* start a new handshake sequence to renegotiate the symmetric key with an36* SSLSocket connection. This test verifies the handshake method was called37* via debugging info. It does not verify the renegotiation was successful38* as that is very hard.39*/4041import javax.net.ssl.KeyManagerFactory;42import javax.net.ssl.SSLContext;43import javax.net.ssl.SSLServerSocket;44import javax.net.ssl.SSLServerSocketFactory;45import javax.net.ssl.SSLSocket;46import javax.net.ssl.SSLSocketFactory;47import javax.net.ssl.TrustManagerFactory;48import java.io.ByteArrayInputStream;49import java.io.ByteArrayOutputStream;50import java.io.File;51import java.io.FileInputStream;52import java.io.InputStream;53import java.io.OutputStream;54import java.io.PrintWriter;55import java.security.KeyStore;56import java.security.SecureRandom;57import java.util.Arrays;5859import jdk.testlibrary.ProcessTools;60import jdk.testlibrary.Utils;61import jdk.testlibrary.OutputAnalyzer;62import sun.misc.HexDumpEncoder;63import sun.misc.IOUtils;6465public class SSLSocketKeyLimit {66SSLSocket socket;67private InputStream in;68private OutputStream out;6970static boolean serverReady = false;71static int serverPort = 0;7273static String pathToStores = "../../../../javax/net/ssl/etc/";74static String keyStoreFile = "keystore";75static String passwd = "passphrase";76static int dataLen = 10240;77static byte[] data = new byte[dataLen];78static boolean serverwrite = true;79int totalDataLen = 0;80static boolean done = false;8182SSLSocketKeyLimit() {83}8485SSLContext initContext() throws Exception {86SSLContext sc = SSLContext.getInstance("TLSv1.3");87KeyStore ks = KeyStore.getInstance("JKS");88ks.load(new FileInputStream(new File(System.getProperty("javax.net.ssl.keyStore"))),89passwd.toCharArray());90KeyManagerFactory kmf =91KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());92kmf.init(ks, passwd.toCharArray());93TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());94tmf.init(ks);95sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());96return sc;97}9899/**100* args should have two values: server|client, <limit size>101* Prepending 'p' is for internal use only.102*/103public static void main(String args[]) throws Throwable {104if (args[0].compareTo("p") != 0) {105106boolean expectedFail = (Integer.parseInt(args[0]) == 1);107if (expectedFail) {108System.out.println("Test expected to not find updated msg");109}110//Write security property file to overwrite default111File f = new File("keyusage."+ System.nanoTime());112PrintWriter p = new PrintWriter(f);113p.write("jdk.tls.keyLimits=");114for (int i = 2; i < args.length; i++) {115p.write(" "+ args[i]);116}117p.close();118System.out.println("Keyusage path = " + f.getAbsolutePath());119System.setProperty("test.java.opts",120"-Dtest.src=" + System.getProperty("test.src") +121" -Dtest.jdk=" + System.getProperty("test.jdk") +122" -Djavax.net.debug=ssl,handshake" +123" -Djava.security.properties=" + f.getName());124125System.out.println("test.java.opts: " +126System.getProperty("test.java.opts"));127128ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,129Utils.addTestJavaOpts("SSLSocketKeyLimit", "p", args[1]));130131OutputAnalyzer output = ProcessTools.executeProcess(pb);132try {133if (expectedFail) {134output.shouldNotContain("KeyUpdate: write key updated");135output.shouldNotContain("KeyUpdate: read key updated");136} else {137output.shouldContain("trigger key update");138output.shouldContain("KeyUpdate: write key updated");139output.shouldContain("KeyUpdate: read key updated");140}141} catch (Exception e) {142throw e;143} finally {144System.out.println("-- BEGIN Stdout:");145System.out.println(output.getStdout());146System.out.println("-- END Stdout");147System.out.println("-- BEGIN Stderr:");148System.out.println(output.getStderr());149System.out.println("-- END Stderr");150}151return;152}153154if (args.length > 0 && args[0].compareToIgnoreCase("client") == 0) {155serverwrite = false;156}157158String keyFilename =159System.getProperty("test.src", "./") + "/" + pathToStores +160"/" + keyStoreFile;161162System.setProperty("javax.net.ssl.keyStore", keyFilename);163System.setProperty("javax.net.ssl.keyStorePassword", passwd);164165Arrays.fill(data, (byte)0x0A);166Thread ts = new Thread(new Server());167168ts.start();169while (!serverReady) {170Thread.sleep(100);171}172new Client().run();173ts.join(10000); // 10sec174System.exit(0);175}176177void write(SSLSocket s) throws Exception {178int i = 0;179in = s.getInputStream();180out = s.getOutputStream();181while (i++ < 150) {182out.write(data, 0, dataLen);183System.out.print("W");184IOUtils.readNBytes(in,1);185System.out.print("R");186}187out.write(0x0D);188out.flush();189190// Let read side all the data191while (!done) {192Thread.sleep(100);193}194out.close();195in.close();196}197198199void read(SSLSocket s) throws Exception {200byte[] buf = new byte[dataLen];201int len;202byte i = 0;203try {204System.out.println("Server: connected " + s.getSession().getCipherSuite());205in = s.getInputStream();206out = s.getOutputStream();207while (true) {208len = in.read(buf, 0, dataLen);209System.out.print("r");210out.write(i++);211System.out.print("w");212for (byte b: buf) {213if (b == 0x0A || b == 0x0D) {214continue;215}216System.out.println("\nData invalid: " + new HexDumpEncoder().encode(buf));217break;218}219220if (len > 0 && buf[len-1] == 0x0D) {221System.out.println("got end byte");222break;223}224totalDataLen += len;225}226} catch (Exception e) {227System.out.println("\n" + e.getMessage());228e.printStackTrace();229} finally {230// Tell write side that we are done reading231out.close();232in.close();233done = true;234}235System.out.println("\nTotalDataLen = " + totalDataLen);236}237238static class Server extends SSLSocketKeyLimit implements Runnable {239private SSLServerSocketFactory ssf;240private SSLServerSocket ss;241Server() {242super();243try {244ssf = initContext().getServerSocketFactory();245ss = (SSLServerSocket) ssf.createServerSocket(serverPort);246serverPort = ss.getLocalPort();247} catch (Exception e) {248System.out.println("server: " + e.getMessage());249e.printStackTrace();250}251}252253public void run() {254try {255serverReady = true;256System.out.println("Server waiting... port: " + serverPort);257socket = (SSLSocket) ss.accept();258if (serverwrite) {259write(socket);260} else {261read(socket);262}263264socket.close();265} catch (Exception e) {266System.out.println("server: " + e.getMessage());267e.printStackTrace();268}269System.out.println("Server closed");270}271}272273274static class Client extends SSLSocketKeyLimit implements Runnable {275private SSLSocketFactory sf;276277Client() {278super();279}280281public void run() {282try {283sf = initContext().getSocketFactory();284System.out.println("Client: connecting... port: " + serverPort);285socket = (SSLSocket)sf.createSocket("localhost", serverPort);286System.out.println("Client: connected." + socket.getSession().getCipherSuite());287288// Opposite of what the server does289if (!serverwrite) {290write(socket);291} else {292read(socket);293}294295} catch (Exception e) {296System.err.println("client: " + e.getMessage());297e.printStackTrace();298}299}300}301}302303304