Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLEngineImpl/EmptyExtensionData.java
38853 views
/*1* Copyright (c) 2008, 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// SunJSSE does not support dynamic system properties, no way to re-use25// system properties in samevm/agentvm mode.26//2728/*29* @test30* @bug 672812631* @summary Parsing Extensions in Client Hello message is done in a wrong way32* @library /lib/security33* @run main/othervm EmptyExtensionData34*/3536import javax.net.ssl.*;37import javax.net.ssl.SSLEngineResult.*;38import java.io.*;39import java.security.*;40import java.nio.*;4142public class EmptyExtensionData {4344private static boolean debug = false;4546private static String pathToStores = "../../../../javax/net/ssl/etc";47private static String keyStoreFile = "keystore";48private static String trustStoreFile = "truststore";49private static String passwd = "passphrase";5051private static String keyFilename =52System.getProperty("test.src", "./") + "/" + pathToStores +53"/" + keyStoreFile;54private static String trustFilename =55System.getProperty("test.src", "./") + "/" + pathToStores +56"/" + trustStoreFile;5758private static void checkDone(SSLEngine ssle) throws Exception {59if (!ssle.isInboundDone()) {60throw new Exception("isInboundDone isn't done");61}62if (!ssle.isOutboundDone()) {63throw new Exception("isOutboundDone isn't done");64}65}6667private static void runTest(SSLEngine ssle) throws Exception {68// a client hello message with an empty extension data69byte[] msg_clihello = {70(byte)0x16, (byte)0x03, (byte)0x01, (byte)0x00,71(byte)0x6f, (byte)0x01, (byte)0x00, (byte)0x00,72(byte)0x6b, (byte)0x03, (byte)0x01, (byte)0x48,73(byte)0x90, (byte)0x71, (byte)0xfc, (byte)0xf9,74(byte)0xa2, (byte)0x3a, (byte)0xd7, (byte)0xa8,75(byte)0x0b, (byte)0x25, (byte)0xf1, (byte)0x2b,76(byte)0x88, (byte)0x80, (byte)0x66, (byte)0xca,77(byte)0x07, (byte)0x78, (byte)0x2a, (byte)0x08,78(byte)0x9d, (byte)0x62, (byte)0x1d, (byte)0x89,79(byte)0xc9, (byte)0x1e, (byte)0x1f, (byte)0xe5,80(byte)0x92, (byte)0xfe, (byte)0x8d, (byte)0x00,81(byte)0x00, (byte)0x24, (byte)0x00, (byte)0x88,82(byte)0x00, (byte)0x87, (byte)0x00, (byte)0x39,83(byte)0x00, (byte)0x38, (byte)0x00, (byte)0x84,84(byte)0x00, (byte)0x35, (byte)0x00, (byte)0x45,85(byte)0x00, (byte)0x44, (byte)0x00, (byte)0x33,86(byte)0x00, (byte)0x32, (byte)0x00, (byte)0x41,87(byte)0x00, (byte)0x04, (byte)0x00, (byte)0x05,88(byte)0x00, (byte)0x2f, (byte)0x00, (byte)0x16,89(byte)0x00, (byte)0x13, (byte)0xfe, (byte)0xff,90(byte)0x00, (byte)0x0a, (byte)0x01, (byte)0x00,91(byte)0x00, (byte)0x1e, (byte)0x00, (byte)0x00,92(byte)0x00, (byte)0x16, (byte)0x00, (byte)0x14,93(byte)0x00, (byte)0x00, (byte)0x11, (byte)0x6a,94(byte)0x75, (byte)0x73, (byte)0x74, (byte)0x69,95(byte)0x6e, (byte)0x2e, (byte)0x75, (byte)0x6b,96(byte)0x2e, (byte)0x73, (byte)0x75, (byte)0x6e,97(byte)0x2e, (byte)0x63, (byte)0x6f, (byte)0x6d,98(byte)0x00, (byte)0x23, (byte)0x00, (byte)0x0099};100ByteBuffer bf_clihello = ByteBuffer.wrap(msg_clihello);101102SSLSession session = ssle.getSession();103int appBufferMax = session.getApplicationBufferSize();104int netBufferMax = session.getPacketBufferSize();105106ByteBuffer serverIn = ByteBuffer.allocate(appBufferMax + 50);107ByteBuffer serverOut = ByteBuffer.wrap("I'm Server".getBytes());108ByteBuffer sTOc = ByteBuffer.allocate(netBufferMax);109110ssle.beginHandshake();111112// unwrap the clientHello message.113SSLEngineResult result = ssle.unwrap(bf_clihello, serverIn);114System.out.println("server unwrap " + result);115runDelegatedTasks(result, ssle);116117// one more step, ensure the clientHello message is parsed.118SSLEngineResult.HandshakeStatus status = ssle.getHandshakeStatus();119if ( status == HandshakeStatus.NEED_UNWRAP) {120result = ssle.unwrap(bf_clihello, serverIn);121System.out.println("server unwrap " + result);122runDelegatedTasks(result, ssle);123} else if ( status == HandshakeStatus.NEED_WRAP) {124result = ssle.wrap(serverOut, sTOc);125System.out.println("server wrap " + result);126runDelegatedTasks(result, ssle);127} else {128throw new Exception("unexpected handshake status " + status);129}130131// enough, stop132}133134/*135* If the result indicates that we have outstanding tasks to do,136* go ahead and run them in this thread.137*/138private static void runDelegatedTasks(SSLEngineResult result,139SSLEngine engine) throws Exception {140141if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {142Runnable runnable;143while ((runnable = engine.getDelegatedTask()) != null) {144log("\trunning delegated task...");145runnable.run();146}147HandshakeStatus hsStatus = engine.getHandshakeStatus();148if (hsStatus == HandshakeStatus.NEED_TASK) {149throw new Exception(150"handshake shouldn't need additional tasks");151}152log("\tnew HandshakeStatus: " + hsStatus);153}154}155156public static void main(String args[]) throws Exception {157// Re-enable TLSv1 since test depends on it.158SecurityUtils.removeFromDisabledTlsAlgs("TLSv1");159160SSLEngine ssle = createSSLEngine(keyFilename, trustFilename);161runTest(ssle);162163System.out.println("Test Passed.");164}165166/*167* Create an initialized SSLContext to use for this test.168*/169static private SSLEngine createSSLEngine(String keyFile, String trustFile)170throws Exception {171172SSLEngine ssle;173174KeyStore ks = KeyStore.getInstance("JKS");175KeyStore ts = KeyStore.getInstance("JKS");176177char[] passphrase = "passphrase".toCharArray();178179ks.load(new FileInputStream(keyFile), passphrase);180ts.load(new FileInputStream(trustFile), passphrase);181182KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");183kmf.init(ks, passphrase);184185TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");186tmf.init(ts);187188SSLContext sslCtx = SSLContext.getInstance("TLS");189190sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);191192ssle = sslCtx.createSSLEngine();193ssle.setUseClientMode(false);194195return ssle;196}197198199private static void log(String str) {200if (debug) {201System.out.println(str);202}203}204}205206207