Path: blob/master/test/jdk/sun/security/ssl/SSLEngineImpl/CloseEngineException.java
41152 views
/*1* Copyright (c) 2003, 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 496979931* @summary javax.net.ssl.SSLSocket.SSLSocket(InetAddress,int) shouldn't32* throw exception33* @run main/othervm CloseEngineException34*/3536//37// This is making sure that starting a new handshake throws the right38// exception. There is a similar test for SSLSocket.39//4041import javax.net.ssl.*;42import javax.net.ssl.SSLEngineResult.*;43import java.io.*;44import java.security.*;45import java.nio.*;4647// Note that this test case depends on JSSE provider implementation details.48public class CloseEngineException {4950private static boolean debug = true;5152private SSLContext sslc;53private SSLEngine ssle1; // client54private SSLEngine ssle2; // server5556private static String pathToStores = "../../../../javax/net/ssl/etc";57private static String keyStoreFile = "keystore";58private static String trustStoreFile = "truststore";59private static String passwd = "passphrase";6061private static String keyFilename =62System.getProperty("test.src", "./") + "/" + pathToStores +63"/" + keyStoreFile;64private static String trustFilename =65System.getProperty("test.src", "./") + "/" + pathToStores +66"/" + trustStoreFile;6768private ByteBuffer appOut1; // write side of ssle169private ByteBuffer appIn1; // read side of ssle170private ByteBuffer appOut2; // write side of ssle271private ByteBuffer appIn2; // read side of ssle27273private ByteBuffer oneToTwo; // "reliable" transport ssle1->ssle274private ByteBuffer twoToOne; // "reliable" transport ssle2->ssle17576/*77* Majority of the test case is here, setup is done below.78*/79private void createSSLEngines() throws Exception {80ssle1 = sslc.createSSLEngine("client", 1);81ssle1.setUseClientMode(true);8283ssle2 = sslc.createSSLEngine();84ssle2.setUseClientMode(false);85ssle2.setNeedClientAuth(true);86}8788private void runTest() throws Exception {89boolean dataDone = false;9091createSSLEngines();92createBuffers();9394SSLEngineResult result1; // ssle1's results from last operation95SSLEngineResult result2; // ssle2's results from last operation9697while (!isEngineClosed(ssle1) && !isEngineClosed(ssle2)) {9899log("================");100101if (!isEngineClosed(ssle1)) {102result1 = ssle1.wrap(appOut1, oneToTwo);103runDelegatedTasks(result1, ssle1);104105log("wrap1: " + result1);106log("oneToTwo = " + oneToTwo);107log("");108109oneToTwo.flip();110}111if (!isEngineClosed(ssle2)) {112result2 = ssle2.wrap(appOut2, twoToOne);113runDelegatedTasks(result2, ssle2);114115log("wrap2: " + result2);116log("twoToOne = " + twoToOne);117118twoToOne.flip();119}120121log("----");122123if (!isEngineClosed(ssle1) && !dataDone) {124log("--");125result1 = ssle1.unwrap(twoToOne, appIn1);126runDelegatedTasks(result1, ssle1);127128log("unwrap1: " + result1);129log("twoToOne = " + twoToOne);130log("");131132twoToOne.compact();133}134if (!isEngineClosed(ssle2)) {135log("---");136result2 = ssle2.unwrap(oneToTwo, appIn2);137runDelegatedTasks(result2, ssle2);138139log("unwrap2: " + result2);140log("oneToTwo = " + oneToTwo);141142oneToTwo.compact();143}144145/*146* If we've transfered all the data between app1 and app2,147* we try to close and see what that gets us.148*/149if (!dataDone && (appOut1.limit() == appIn2.position()) &&150(appOut2.limit() == appIn1.position())) {151152checkTransfer(appOut1, appIn2);153checkTransfer(appOut2, appIn1);154155log("Closing ssle1's *OUTBOUND*...");156ssle1.closeOutbound();157dataDone = true;158159try {160/*161* Check that closed Outbound generates.162*/163ssle1.beginHandshake();164throw new Exception(165"TEST FAILED: didn't throw Exception");166} catch (SSLException e) {167System.err.println("PARTIAL PASS");168}169}170}171172try {173/*174* Check that closed Inbound generates.175*/176ssle2.beginHandshake();177throw new Exception(178"TEST FAILED: didn't throw Exception");179} catch (SSLException e) {180System.err.println("TEST PASSED");181}182}183184public static void main(String args[]) throws Exception {185186CloseEngineException test;187188test = new CloseEngineException();189190test.createSSLEngines();191192test.runTest();193194System.err.println("Test Passed.");195}196197/*198* **********************************************************199* Majority of the test case is above, below is just setup stuff200* **********************************************************201*/202203public CloseEngineException() throws Exception {204sslc = getSSLContext(keyFilename, trustFilename);205}206207/*208* Create an initialized SSLContext to use for this test.209*/210private SSLContext getSSLContext(String keyFile, String trustFile)211throws Exception {212213KeyStore ks = KeyStore.getInstance("JKS");214KeyStore ts = KeyStore.getInstance("JKS");215216char[] passphrase = "passphrase".toCharArray();217218ks.load(new FileInputStream(keyFile), passphrase);219ts.load(new FileInputStream(trustFile), passphrase);220221KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");222kmf.init(ks, passphrase);223224TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");225tmf.init(ts);226227SSLContext sslCtx = SSLContext.getInstance("TLS");228229sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);230231return sslCtx;232}233234private void createBuffers() {235// Size the buffers as appropriate.236237SSLSession session = ssle1.getSession();238int appBufferMax = session.getApplicationBufferSize();239int netBufferMax = session.getPacketBufferSize();240241appIn1 = ByteBuffer.allocateDirect(appBufferMax + 50);242appIn2 = ByteBuffer.allocateDirect(appBufferMax + 50);243244oneToTwo = ByteBuffer.allocateDirect(netBufferMax);245twoToOne = ByteBuffer.allocateDirect(netBufferMax);246247appOut1 = ByteBuffer.wrap("Hi Engine2, I'm SSLEngine1".getBytes());248appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes());249250log("AppOut1 = " + appOut1);251log("AppOut2 = " + appOut2);252log("");253}254255private static void runDelegatedTasks(SSLEngineResult result,256SSLEngine engine) throws Exception {257258if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {259Runnable runnable;260while ((runnable = engine.getDelegatedTask()) != null) {261log("running delegated task...");262runnable.run();263}264}265}266267private static boolean isEngineClosed(SSLEngine engine) {268return (engine.isOutboundDone() && engine.isInboundDone());269}270271private static void checkTransfer(ByteBuffer a, ByteBuffer b)272throws Exception {273a.flip();274b.flip();275276if (!a.equals(b)) {277throw new Exception("Data didn't transfer cleanly");278} else {279log("Data transferred cleanly");280}281282a.position(a.limit());283b.position(b.limit());284a.limit(a.capacity());285b.limit(b.capacity());286}287288private static void log(String str) {289if (debug) {290System.err.println(str);291}292}293}294295296