Path: blob/master/test/jdk/javax/net/ssl/SSLEngine/LargeBufs.java
41152 views
/*1* Copyright (c) 2004, 2015, 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* @test25* @bug 449574226* @summary Add non-blocking SSL/TLS functionality, usable with any27* I/O abstraction28*29* This is to test larger buffer arrays, and make sure the maximum30* is being passed.31*32* @run main/othervm -Djsse.enableCBCProtection=false LargeBufs33*34* @author Brad R. Wetmore35* @key randomness36*/3738import javax.net.ssl.*;39import javax.net.ssl.SSLEngineResult.*;40import java.io.*;41import java.security.*;42import java.nio.*;43import java.util.Random;4445public class LargeBufs {4647private static boolean debug = true;4849private SSLContext sslc;50static private SSLEngine ssle1; // client51static private SSLEngine ssle2; // server5253private static String pathToStores = "../etc";54private static String keyStoreFile = "keystore";55private static String trustStoreFile = "truststore";56private static String passwd = "passphrase";5758private static String keyFilename =59System.getProperty("test.src", "./") + "/" + pathToStores +60"/" + keyStoreFile;61private static String trustFilename =62System.getProperty("test.src", "./") + "/" + pathToStores +63"/" + trustStoreFile;6465private ByteBuffer appOut1; // write side of ssle166private ByteBuffer appIn1; // read side of ssle167private ByteBuffer appOut2; // write side of ssle268private ByteBuffer appIn2; // read side of ssle26970private ByteBuffer oneToTwo; // "reliable" transport ssle1->ssle271private ByteBuffer twoToOne; // "reliable" transport ssle2->ssle17273private int appBufferMax;74private int netBufferMax;75private int OFFSET = 37;7677/*78* Majority of the test case is here, setup is done below.79*/80private void createSSLEngines() throws Exception {81ssle1 = sslc.createSSLEngine("client", 1);82ssle1.setUseClientMode(true);8384ssle2 = sslc.createSSLEngine();85ssle2.setUseClientMode(false);86ssle2.setNeedClientAuth(true);87}8889private void runTest(String cipher) throws Exception {90boolean dataDone = false;9192createSSLEngines();9394System.out.println("Using " + cipher);95ssle1.setEnabledCipherSuites(new String [] { cipher });96ssle2.setEnabledCipherSuites(new String [] { cipher });9798createBuffers();99100SSLEngineResult result1; // ssle1's results from last operation101SSLEngineResult result2; // ssle2's results from last operation102103while (!isEngineClosed(ssle1) || !isEngineClosed(ssle2)) {104105log("================");106107result1 = ssle1.wrap(appOut1, oneToTwo);108result2 = ssle2.wrap(appOut2, twoToOne);109110if ((result1.bytesConsumed() != 0) &&111(result1.bytesConsumed() != appBufferMax) &&112(result1.bytesConsumed() != OFFSET)) {113throw new Exception("result1: " + result1);114}115116if ((result2.bytesConsumed() != 0) &&117(result2.bytesConsumed() != appBufferMax) &&118(result2.bytesConsumed() != 2 * OFFSET)) {119throw new Exception("result2: " + result2);120}121122log("wrap1: " + result1);123log("oneToTwo = " + oneToTwo);124log("");125126log("wrap2: " + result2);127log("twoToOne = " + twoToOne);128129runDelegatedTasks(result1, ssle1);130runDelegatedTasks(result2, ssle2);131132oneToTwo.flip();133twoToOne.flip();134135log("----");136137result1 = ssle1.unwrap(twoToOne, appIn1);138result2 = ssle2.unwrap(oneToTwo, appIn2);139140if ((result1.bytesProduced() != 0) &&141(result1.bytesProduced() != appBufferMax) &&142(result1.bytesProduced() != 2 * OFFSET)) {143throw new Exception("result1: " + result1);144}145146if ((result2.bytesProduced() != 0) &&147(result2.bytesProduced() != appBufferMax) &&148(result2.bytesProduced() != OFFSET)) {149throw new Exception("result1: " + result1);150}151152log("unwrap1: " + result1);153log("twoToOne = " + twoToOne);154log("");155156log("unwrap2: " + result2);157log("oneToTwo = " + oneToTwo);158159runDelegatedTasks(result1, ssle1);160runDelegatedTasks(result2, ssle2);161162oneToTwo.compact();163twoToOne.compact();164165/*166* If we've transfered all the data between app1 and app2,167* we try to close and see what that gets us.168*/169if (!dataDone && (appOut1.limit() == appIn2.position()) &&170(appOut2.limit() == appIn1.position())) {171172checkTransfer(appOut1, appIn2);173checkTransfer(appOut2, appIn1);174175log("Closing ssle1's *OUTBOUND*...");176ssle1.closeOutbound();177dataDone = true;178}179}180}181182public static void main(String args[]) throws Exception {183// reset the security property to make sure that the algorithms184// and keys used in this test are not disabled.185Security.setProperty("jdk.tls.disabledAlgorithms", "");186187LargeBufs test;188189test = new LargeBufs();190test.runTest("SSL_RSA_WITH_RC4_128_MD5");191192test = new LargeBufs();193test.runTest("SSL_RSA_WITH_3DES_EDE_CBC_SHA");194195System.out.println("Test Passed.");196}197198/*199* **********************************************************200* Majority of the test case is above, below is just setup stuff201* **********************************************************202*/203204public LargeBufs() throws Exception {205sslc = getSSLContext(keyFilename, trustFilename);206}207208/*209* Create an initialized SSLContext to use for this test.210*/211private SSLContext getSSLContext(String keyFile, String trustFile)212throws Exception {213214KeyStore ks = KeyStore.getInstance("JKS");215KeyStore ts = KeyStore.getInstance("JKS");216217char[] passphrase = "passphrase".toCharArray();218219ks.load(new FileInputStream(keyFile), passphrase);220ts.load(new FileInputStream(trustFile), passphrase);221222KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");223kmf.init(ks, passphrase);224225TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");226tmf.init(ts);227228SSLContext sslCtx = SSLContext.getInstance("TLS");229230sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);231232return sslCtx;233}234235private void createBuffers() {236// Size the buffers as appropriate.237238SSLSession session = ssle1.getSession();239240// The maximum application buffer should calculate like241// appBufferMax = session.getApplicationBufferSize();242// however, the getApplicationBufferSize() doesn't guarantee243// that the ability to concume or produce applicaton data upto244// the size. 16384 is the default JSSE implementation maximum245// application size that could be consumed and produced.246appBufferMax = 16384;247netBufferMax = session.getPacketBufferSize();248249Random random = new Random();250byte [] one = new byte [appBufferMax * 5 + OFFSET];251byte [] two = new byte [appBufferMax * 5 + 2 * OFFSET];252253random.nextBytes(one);254random.nextBytes(two);255256appOut1 = ByteBuffer.wrap(one);257appOut2 = ByteBuffer.wrap(two);258259appIn1 = ByteBuffer.allocate(appBufferMax * 6);260appIn2 = ByteBuffer.allocate(appBufferMax * 6);261262oneToTwo = ByteBuffer.allocateDirect(netBufferMax);263twoToOne = ByteBuffer.allocateDirect(netBufferMax);264265System.out.println("Testing arrays of: " + one.length +266" and " + two.length);267268log("AppOut1 = " + appOut1);269log("AppOut2 = " + appOut2);270log("");271}272273private static void runDelegatedTasks(SSLEngineResult result,274SSLEngine engine) throws Exception {275276if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {277Runnable runnable;278while ((runnable = engine.getDelegatedTask()) != null) {279log("running delegated task...");280runnable.run();281}282}283}284285private static boolean isEngineClosed(SSLEngine engine) {286return (engine.isOutboundDone() && engine.isInboundDone());287}288289private static void checkTransfer(ByteBuffer a, ByteBuffer b)290throws Exception {291a.flip();292b.flip();293294if (!a.equals(b)) {295throw new Exception("Data didn't transfer cleanly");296} else {297log("Data transferred cleanly");298}299300a.position(a.limit());301b.position(b.limit());302a.limit(a.capacity());303b.limit(b.capacity());304}305306private static void log(String str) {307if (debug) {308System.out.println(str);309}310}311}312313314