Path: blob/master/test/jdk/sun/security/ssl/SSLEngineImpl/EngineEnforceUseClientMode.java
41152 views
/*1* Copyright (c) 2004, 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 4980882 8207250 823747431* @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//Check default SSLEngine role.92if (ssle5.getUseClientMode()) {93throw new RuntimeException("Expected default role to be server");94}9596}9798private void runTest() throws Exception {99100createSSLEngines();101createBuffers();102103/*104* First try the engines with no client/server initialization105* All should fail.106*/107try {108System.out.println("Testing wrap()");109ssle3.wrap(appOut1, oneToTwo);110throw new RuntimeException(111"wrap(): Didn't catch the exception properly");112} catch (IllegalStateException e) {113System.out.println("Caught the correct exception.");114oneToTwo.flip();115if (oneToTwo.hasRemaining()) {116throw new Exception("wrap generated data");117}118oneToTwo.clear();119}120121try {122System.out.println("Testing unwrap()");123ssle4.unwrap(oneToTwo, appIn1);124throw new RuntimeException(125"unwrap(): Didn't catch the exception properly");126} catch (IllegalStateException e) {127System.out.println("Caught the correct exception.");128appIn1.flip();129if (appIn1.hasRemaining()) {130throw new Exception("unwrap generated data");131}132appIn1.clear();133}134135try {136System.out.println("Testing beginHandshake()");137ssle5.beginHandshake();138throw new RuntimeException(139"unwrap(): Didn't catch the exception properly");140} catch (IllegalStateException e) {141System.out.println("Caught the correct exception.");142}143144boolean dataDone = false;145146SSLEngineResult result1; // ssle1's results from last operation147SSLEngineResult result2; // ssle2's results from last operation148149while (!isEngineClosed(ssle1) || !isEngineClosed(ssle2)) {150151log("================");152153result1 = ssle1.wrap(appOut1, oneToTwo);154result2 = ssle2.wrap(appOut2, twoToOne);155156log("wrap1: " + result1);157log("oneToTwo = " + oneToTwo);158log("");159160log("wrap2: " + result2);161log("twoToOne = " + twoToOne);162163runDelegatedTasks(result1, ssle1);164runDelegatedTasks(result2, ssle2);165166oneToTwo.flip();167twoToOne.flip();168169log("----");170171result1 = ssle1.unwrap(twoToOne, appIn1);172result2 = ssle2.unwrap(oneToTwo, appIn2);173174log("unwrap1: " + result1);175log("twoToOne = " + twoToOne);176log("");177178log("unwrap2: " + result2);179log("oneToTwo = " + oneToTwo);180181runDelegatedTasks(result1, ssle1);182runDelegatedTasks(result2, ssle2);183184oneToTwo.compact();185twoToOne.compact();186187/*188* If we've transfered all the data between app1 and app2,189* we try to close and see what that gets us.190*/191if (!dataDone && (appOut1.limit() == appIn2.position()) &&192(appOut2.limit() == appIn1.position())) {193194checkTransfer(appOut1, appIn2);195checkTransfer(appOut2, appIn1);196197// Should not be able to set mode now, no matter if198// it is the same of different.199System.out.println("Try changing modes...");200for (boolean b : new Boolean[] {true, false}) {201try {202ssle2.setUseClientMode(b);203throw new RuntimeException(204"setUseClientMode(" + b + "): " +205"Didn't catch the exception properly");206} catch (IllegalArgumentException e) {207System.out.println("Caught the correct exception.");208}209}210211return;212}213}214}215216public static void main(String args[]) throws Exception {217218EngineEnforceUseClientMode test;219220test = new EngineEnforceUseClientMode();221222test.createSSLEngines();223224test.runTest();225226System.out.println("Test Passed.");227}228229/*230* **********************************************************231* Majority of the test case is above, below is just setup stuff232* **********************************************************233*/234235public EngineEnforceUseClientMode() throws Exception {236sslc = getSSLContext(keyFilename, trustFilename);237}238239/*240* Create an initialized SSLContext to use for this test.241*/242private SSLContext getSSLContext(String keyFile, String trustFile)243throws Exception {244245KeyStore ks = KeyStore.getInstance("JKS");246KeyStore ts = KeyStore.getInstance("JKS");247248char[] passphrase = "passphrase".toCharArray();249250ks.load(new FileInputStream(keyFile), passphrase);251ts.load(new FileInputStream(trustFile), passphrase);252253KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");254kmf.init(ks, passphrase);255256TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");257tmf.init(ts);258259SSLContext sslCtx = SSLContext.getInstance("TLS");260261sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);262263return sslCtx;264}265266private void createBuffers() {267// Size the buffers as appropriate.268269SSLSession session = ssle1.getSession();270int appBufferMax = session.getApplicationBufferSize();271int netBufferMax = session.getPacketBufferSize();272273appIn1 = ByteBuffer.allocateDirect(appBufferMax + 50);274appIn2 = ByteBuffer.allocateDirect(appBufferMax + 50);275276oneToTwo = ByteBuffer.allocateDirect(netBufferMax);277twoToOne = ByteBuffer.allocateDirect(netBufferMax);278279appOut1 = ByteBuffer.wrap("Hi Engine2, I'm SSLEngine1".getBytes());280appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes());281282log("AppOut1 = " + appOut1);283log("AppOut2 = " + appOut2);284log("");285}286287private static void runDelegatedTasks(SSLEngineResult result,288SSLEngine engine) throws Exception {289290if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {291Runnable runnable;292while ((runnable = engine.getDelegatedTask()) != null) {293log("running delegated task...");294runnable.run();295}296}297}298299private static boolean isEngineClosed(SSLEngine engine) {300return (engine.isOutboundDone() && engine.isInboundDone());301}302303private static void checkTransfer(ByteBuffer a, ByteBuffer b)304throws Exception {305a.flip();306b.flip();307308if (!a.equals(b)) {309throw new Exception("Data didn't transfer cleanly");310} else {311log("Data transferred cleanly");312}313314a.position(a.limit());315b.position(b.limit());316a.limit(a.capacity());317b.limit(b.capacity());318}319320private static void log(String str) {321if (debug) {322System.out.println(str);323}324}325}326327328