Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLEngineImpl/EngineEnforceUseClientMode.java
38854 views
/*1* Copyright (c) 2004, 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// SunJSSE does not support dynamic system properties, no way to re-use25// system properties in samevm/agentvm mode.26//2728/*29* @test30* @bug 4980882 820725031* @summary SSLEngine should enforce setUseClientMode32* @run main/othervm EngineEnforceUseClientMode33* @author Brad R. Wetmore34*/3536import javax.net.ssl.*;37import javax.net.ssl.SSLEngineResult.*;38import java.io.*;39import java.security.*;40import java.nio.*;4142public class EngineEnforceUseClientMode {4344private static boolean debug = false;4546private SSLContext sslc;47private SSLEngine ssle1; // client48private SSLEngine ssle2; // server4950private SSLEngine ssle3; // server51private SSLEngine ssle4; // server52private SSLEngine ssle5; // server5354private static String pathToStores = "../../../../javax/net/ssl/etc";55private static String keyStoreFile = "keystore";56private static String trustStoreFile = "truststore";57private static String passwd = "passphrase";5859private static String keyFilename =60System.getProperty("test.src", "./") + "/" + pathToStores +61"/" + keyStoreFile;62private static String trustFilename =63System.getProperty("test.src", "./") + "/" + pathToStores +64"/" + trustStoreFile;6566private ByteBuffer appOut1; // write side of ssle167private ByteBuffer appIn1; // read side of ssle168private ByteBuffer appOut2; // write side of ssle269private ByteBuffer appIn2; // read side of ssle27071private ByteBuffer oneToTwo; // "reliable" transport ssle1->ssle272private ByteBuffer twoToOne; // "reliable" transport ssle2->ssle17374/*75* Majority of the test case is here, setup is done below.76*/77private void createSSLEngines() throws Exception {78ssle1 = sslc.createSSLEngine("client", 1);79ssle1.setUseClientMode(true);8081ssle2 = sslc.createSSLEngine();82ssle2.setUseClientMode(false);83ssle2.setNeedClientAuth(true);8485/*86* Note, these are not initialized to client/server87*/88ssle3 = sslc.createSSLEngine();89ssle4 = sslc.createSSLEngine();90ssle5 = sslc.createSSLEngine();91}9293private void runTest() throws Exception {9495createSSLEngines();96createBuffers();9798/*99* First try the engines with no client/server initialization100* All should fail.101*/102try {103System.out.println("Testing wrap()");104ssle3.wrap(appOut1, oneToTwo);105throw new RuntimeException(106"wrap(): Didn't catch the exception properly");107} catch (IllegalStateException e) {108System.out.println("Caught the correct exception.");109oneToTwo.flip();110if (oneToTwo.hasRemaining()) {111throw new Exception("wrap generated data");112}113oneToTwo.clear();114}115116try {117System.out.println("Testing unwrap()");118ssle4.unwrap(oneToTwo, appIn1);119throw new RuntimeException(120"unwrap(): Didn't catch the exception properly");121} catch (IllegalStateException e) {122System.out.println("Caught the correct exception.");123appIn1.flip();124if (appIn1.hasRemaining()) {125throw new Exception("unwrap generated data");126}127appIn1.clear();128}129130try {131System.out.println("Testing beginHandshake()");132ssle5.beginHandshake();133throw new RuntimeException(134"unwrap(): Didn't catch the exception properly");135} catch (IllegalStateException e) {136System.out.println("Caught the correct exception.");137}138139boolean dataDone = false;140141SSLEngineResult result1; // ssle1's results from last operation142SSLEngineResult result2; // ssle2's results from last operation143144while (!isEngineClosed(ssle1) || !isEngineClosed(ssle2)) {145146log("================");147148result1 = ssle1.wrap(appOut1, oneToTwo);149result2 = ssle2.wrap(appOut2, twoToOne);150151log("wrap1: " + result1);152log("oneToTwo = " + oneToTwo);153log("");154155log("wrap2: " + result2);156log("twoToOne = " + twoToOne);157158runDelegatedTasks(result1, ssle1);159runDelegatedTasks(result2, ssle2);160161oneToTwo.flip();162twoToOne.flip();163164log("----");165166result1 = ssle1.unwrap(twoToOne, appIn1);167result2 = ssle2.unwrap(oneToTwo, appIn2);168169log("unwrap1: " + result1);170log("twoToOne = " + twoToOne);171log("");172173log("unwrap2: " + result2);174log("oneToTwo = " + oneToTwo);175176runDelegatedTasks(result1, ssle1);177runDelegatedTasks(result2, ssle2);178179oneToTwo.compact();180twoToOne.compact();181182/*183* If we've transfered all the data between app1 and app2,184* we try to close and see what that gets us.185*/186if (!dataDone && (appOut1.limit() == appIn2.position()) &&187(appOut2.limit() == appIn1.position())) {188189checkTransfer(appOut1, appIn2);190checkTransfer(appOut2, appIn1);191192// Should not be able to set mode now, no matter if193// it is the same of different.194System.out.println("Try changing modes...");195for (boolean b : new Boolean[] {true, false}) {196try {197ssle2.setUseClientMode(b);198throw new RuntimeException(199"setUseClientMode(" + b + "): " +200"Didn't catch the exception properly");201} catch (IllegalArgumentException e) {202System.out.println("Caught the correct exception.");203}204}205206return;207}208}209}210211public static void main(String args[]) throws Exception {212213EngineEnforceUseClientMode test;214215test = new EngineEnforceUseClientMode();216217test.createSSLEngines();218219test.runTest();220221System.out.println("Test Passed.");222}223224/*225* **********************************************************226* Majority of the test case is above, below is just setup stuff227* **********************************************************228*/229230public EngineEnforceUseClientMode() throws Exception {231sslc = getSSLContext(keyFilename, trustFilename);232}233234/*235* Create an initialized SSLContext to use for this test.236*/237private SSLContext getSSLContext(String keyFile, String trustFile)238throws Exception {239240KeyStore ks = KeyStore.getInstance("JKS");241KeyStore ts = KeyStore.getInstance("JKS");242243char[] passphrase = "passphrase".toCharArray();244245ks.load(new FileInputStream(keyFile), passphrase);246ts.load(new FileInputStream(trustFile), passphrase);247248KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");249kmf.init(ks, passphrase);250251TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");252tmf.init(ts);253254SSLContext sslCtx = SSLContext.getInstance("TLS");255256sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);257258return sslCtx;259}260261private void createBuffers() {262// Size the buffers as appropriate.263264SSLSession session = ssle1.getSession();265int appBufferMax = session.getApplicationBufferSize();266int netBufferMax = session.getPacketBufferSize();267268appIn1 = ByteBuffer.allocateDirect(appBufferMax + 50);269appIn2 = ByteBuffer.allocateDirect(appBufferMax + 50);270271oneToTwo = ByteBuffer.allocateDirect(netBufferMax);272twoToOne = ByteBuffer.allocateDirect(netBufferMax);273274appOut1 = ByteBuffer.wrap("Hi Engine2, I'm SSLEngine1".getBytes());275appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes());276277log("AppOut1 = " + appOut1);278log("AppOut2 = " + appOut2);279log("");280}281282private static void runDelegatedTasks(SSLEngineResult result,283SSLEngine engine) throws Exception {284285if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {286Runnable runnable;287while ((runnable = engine.getDelegatedTask()) != null) {288log("running delegated task...");289runnable.run();290}291}292}293294private static boolean isEngineClosed(SSLEngine engine) {295return (engine.isOutboundDone() && engine.isInboundDone());296}297298private static void checkTransfer(ByteBuffer a, ByteBuffer b)299throws Exception {300a.flip();301b.flip();302303if (!a.equals(b)) {304throw new Exception("Data didn't transfer cleanly");305} else {306log("Data transferred cleanly");307}308309a.position(a.limit());310b.position(b.limit());311a.limit(a.capacity());312b.limit(b.capacity());313}314315private static void log(String str) {316if (debug) {317System.out.println(str);318}319}320}321322323