Path: blob/master/test/jdk/sun/security/ssl/SSLEngineImpl/EmptyExtensionData.java
41152 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 /test/lib33* @run main/othervm EmptyExtensionData34*/3536import javax.net.ssl.*;37import javax.net.ssl.SSLEngineResult.*;38import java.io.*;39import java.security.*;40import java.nio.*;4142import jdk.test.lib.security.SecurityUtils;4344public class EmptyExtensionData {4546private static boolean debug = false;4748private static String pathToStores = "../../../../javax/net/ssl/etc";49private static String keyStoreFile = "keystore";50private static String trustStoreFile = "truststore";51private static String passwd = "passphrase";5253private static String keyFilename =54System.getProperty("test.src", "./") + "/" + pathToStores +55"/" + keyStoreFile;56private static String trustFilename =57System.getProperty("test.src", "./") + "/" + pathToStores +58"/" + trustStoreFile;5960private static void checkDone(SSLEngine ssle) throws Exception {61if (!ssle.isInboundDone()) {62throw new Exception("isInboundDone isn't done");63}64if (!ssle.isOutboundDone()) {65throw new Exception("isOutboundDone isn't done");66}67}6869private static void runTest(SSLEngine ssle) throws Exception {70// a client hello message with an empty extension data71byte[] msg_clihello = {72(byte)0x16, (byte)0x03, (byte)0x01, (byte)0x00,73(byte)0x6f, (byte)0x01, (byte)0x00, (byte)0x00,74(byte)0x6b, (byte)0x03, (byte)0x01, (byte)0x48,75(byte)0x90, (byte)0x71, (byte)0xfc, (byte)0xf9,76(byte)0xa2, (byte)0x3a, (byte)0xd7, (byte)0xa8,77(byte)0x0b, (byte)0x25, (byte)0xf1, (byte)0x2b,78(byte)0x88, (byte)0x80, (byte)0x66, (byte)0xca,79(byte)0x07, (byte)0x78, (byte)0x2a, (byte)0x08,80(byte)0x9d, (byte)0x62, (byte)0x1d, (byte)0x89,81(byte)0xc9, (byte)0x1e, (byte)0x1f, (byte)0xe5,82(byte)0x92, (byte)0xfe, (byte)0x8d, (byte)0x00,83(byte)0x00, (byte)0x24, (byte)0x00, (byte)0x88,84(byte)0x00, (byte)0x87, (byte)0x00, (byte)0x39,85(byte)0x00, (byte)0x38, (byte)0x00, (byte)0x84,86(byte)0x00, (byte)0x35, (byte)0x00, (byte)0x45,87(byte)0x00, (byte)0x44, (byte)0x00, (byte)0x33,88(byte)0x00, (byte)0x32, (byte)0x00, (byte)0x41,89(byte)0x00, (byte)0x04, (byte)0x00, (byte)0x05,90(byte)0x00, (byte)0x2f, (byte)0x00, (byte)0x16,91(byte)0x00, (byte)0x13, (byte)0xfe, (byte)0xff,92(byte)0x00, (byte)0x0a, (byte)0x01, (byte)0x00,93(byte)0x00, (byte)0x1e, (byte)0x00, (byte)0x00,94(byte)0x00, (byte)0x16, (byte)0x00, (byte)0x14,95(byte)0x00, (byte)0x00, (byte)0x11, (byte)0x6a,96(byte)0x75, (byte)0x73, (byte)0x74, (byte)0x69,97(byte)0x6e, (byte)0x2e, (byte)0x75, (byte)0x6b,98(byte)0x2e, (byte)0x73, (byte)0x75, (byte)0x6e,99(byte)0x2e, (byte)0x63, (byte)0x6f, (byte)0x6d,100(byte)0x00, (byte)0x23, (byte)0x00, (byte)0x00101};102ByteBuffer bf_clihello = ByteBuffer.wrap(msg_clihello);103104SSLSession session = ssle.getSession();105int appBufferMax = session.getApplicationBufferSize();106int netBufferMax = session.getPacketBufferSize();107108ByteBuffer serverIn = ByteBuffer.allocate(appBufferMax + 50);109ByteBuffer serverOut = ByteBuffer.wrap("I'm Server".getBytes());110ByteBuffer sTOc = ByteBuffer.allocate(netBufferMax);111112ssle.beginHandshake();113114// unwrap the clientHello message.115SSLEngineResult result = ssle.unwrap(bf_clihello, serverIn);116System.out.println("server unwrap " + result);117runDelegatedTasks(result, ssle);118119// one more step, ensure the clientHello message is parsed.120SSLEngineResult.HandshakeStatus status = ssle.getHandshakeStatus();121if ( status == HandshakeStatus.NEED_UNWRAP) {122result = ssle.unwrap(bf_clihello, serverIn);123System.out.println("server unwrap " + result);124runDelegatedTasks(result, ssle);125} else if ( status == HandshakeStatus.NEED_WRAP) {126result = ssle.wrap(serverOut, sTOc);127System.out.println("server wrap " + result);128runDelegatedTasks(result, ssle);129} else {130throw new Exception("unexpected handshake status " + status);131}132133// enough, stop134}135136/*137* If the result indicates that we have outstanding tasks to do,138* go ahead and run them in this thread.139*/140private static void runDelegatedTasks(SSLEngineResult result,141SSLEngine engine) throws Exception {142143if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {144Runnable runnable;145while ((runnable = engine.getDelegatedTask()) != null) {146log("\trunning delegated task...");147runnable.run();148}149HandshakeStatus hsStatus = engine.getHandshakeStatus();150if (hsStatus == HandshakeStatus.NEED_TASK) {151throw new Exception(152"handshake shouldn't need additional tasks");153}154log("\tnew HandshakeStatus: " + hsStatus);155}156}157158public static void main(String args[]) throws Exception {159// Re-enable TLSv1 since test depends on it.160SecurityUtils.removeFromDisabledTlsAlgs("TLSv1");161162SSLEngine ssle = createSSLEngine(keyFilename, trustFilename);163runTest(ssle);164165System.out.println("Test Passed.");166}167168/*169* Create an initialized SSLContext to use for this test.170*/171static private SSLEngine createSSLEngine(String keyFile, String trustFile)172throws Exception {173174SSLEngine ssle;175176KeyStore ks = KeyStore.getInstance("JKS");177KeyStore ts = KeyStore.getInstance("JKS");178179char[] passphrase = "passphrase".toCharArray();180181ks.load(new FileInputStream(keyFile), passphrase);182ts.load(new FileInputStream(trustFile), passphrase);183184KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");185kmf.init(ks, passphrase);186187TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");188tmf.init(ts);189190SSLContext sslCtx = SSLContext.getInstance("TLS");191192sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);193194ssle = sslCtx.createSSLEngine();195ssle.setUseClientMode(false);196197return ssle;198}199200201private static void log(String str) {202if (debug) {203System.out.println(str);204}205}206}207208209