Path: blob/master/test/jdk/javax/net/ssl/SSLEngine/IllegalHandshakeMessage.java
41152 views
/*1* Copyright (c) 2017, 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// This test case relies on updated static security property, no way to re-use25// security property in samevm/agentvm mode.26//2728/*29* @test30* @bug 818064331* @summary Illegal handshake message32* @ignore the dependent implementation details are changed33* @run main/othervm IllegalHandshakeMessage34*/3536import javax.net.ssl.*;37import javax.net.ssl.SSLEngineResult.*;38import java.io.*;39import java.security.*;40import java.nio.*;4142public class IllegalHandshakeMessage {4344public static void main(String args[]) throws Exception {45SSLContext context = SSLContext.getDefault();4647SSLEngine cliEngine = context.createSSLEngine();48cliEngine.setUseClientMode(true);49SSLEngine srvEngine = context.createSSLEngine();50srvEngine.setUseClientMode(false);5152SSLSession session = cliEngine.getSession();53int netBufferMax = session.getPacketBufferSize();54int appBufferMax = session.getApplicationBufferSize();5556ByteBuffer cliToSrv = ByteBuffer.allocateDirect(netBufferMax);57ByteBuffer srvToCli = ByteBuffer.allocateDirect(netBufferMax);58ByteBuffer srvIBuff = ByteBuffer.allocateDirect(appBufferMax + 50);59ByteBuffer cliOBuff = ByteBuffer.wrap("I'm client".getBytes());60ByteBuffer srvOBuff = ByteBuffer.wrap("I'm server".getBytes());616263System.out.println("client hello (handshake type(0xAB))");64SSLEngineResult cliRes = cliEngine.wrap(cliOBuff, cliToSrv);65System.out.println("Client wrap result: " + cliRes);66cliToSrv.flip();67if (cliToSrv.limit() > 7) {68cliToSrv.put(5, (byte)0xAB); // use illegal handshake type69cliToSrv.put(7, (byte)0x80); // use illegal message length70} else {71// unlikely72throw new Exception("No handshake message is generated.");73}7475try {76SSLEngineResult srvRes = srvEngine.unwrap(cliToSrv, srvIBuff);77System.out.println("Server unwrap result: " + srvRes);78runDelegatedTasks(srvRes, srvEngine);7980srvRes = srvEngine.wrap(srvOBuff, srvToCli);81System.out.println("Server wrap result: " + srvRes);8283throw new Exception(84"Unsupported handshake message is not handled properly.");85} catch (SSLException e) {86// get the expected exception87System.out.println("Expected exception: " + e);88}89}9091private static void runDelegatedTasks(SSLEngineResult result,92SSLEngine engine) throws Exception {9394if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {95Runnable runnable;96while ((runnable = engine.getDelegatedTask()) != null) {97System.out.println("\trunning delegated task...");98runnable.run();99}100HandshakeStatus hsStatus = engine.getHandshakeStatus();101if (hsStatus == HandshakeStatus.NEED_TASK) {102throw new Exception(103"handshake shouldn't need additional tasks");104}105System.out.println("\tnew HandshakeStatus: " + hsStatus);106}107}108}109110111112