Path: blob/master/test/jdk/javax/net/ssl/SSLEngine/TestAllSuites.java
41152 views
/*1* Copyright (c) 2003, 2010, 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* @ignore JSSE supported cipher suites are changed with CR 6916074,27* need to update this test case in JDK 7 soon28* @run main/timeout=180 TestAllSuites29* @summary Add non-blocking SSL/TLS functionality, usable with any30* I/O abstraction31*32* Iterate through all the suites using both TLS and SSLv3, and turn33* SSLv2Hello off and on. Exchange some bytes and shutdown.34*35* @author Brad Wetmore36*/3738import javax.net.ssl.*;39import javax.net.ssl.SSLEngineResult.*;40import java.io.*;41import java.security.*;42import java.nio.*;43import java.util.*;4445public class TestAllSuites {4647private static boolean debug = false;4849private SSLContext sslc;50private SSLEngine ssle1; // client51private 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->ssle17273String [][] protocols = new String [][] {74{ "SSLv3" },75{ "TLSv1" },76{ "SSLv3", "SSLv2Hello"},77{ "TLSv1", "SSLv2Hello"}78};7980/*81* Majority of the test case is here, setup is done below.82*/8384private void createSSLEngines() throws Exception {85ssle1 = sslc.createSSLEngine("client", 1);86ssle1.setUseClientMode(true);8788ssle2 = sslc.createSSLEngine("server", 2);89ssle2.setUseClientMode(false);90}9192private void test() throws Exception {9394createSSLEngines();95String [] suites = ssle1.getSupportedCipherSuites();9697for (int i = 0; i < suites.length; i++) {98for (int j = 0; j < protocols.length; j++) {99createSSLEngines();100runTest(suites[i], protocols[j]);101}102}103}104105private void runTest(String suite, String [] protocols) throws Exception {106107boolean dataDone = false;108109System.out.println("======================================");110System.out.println("Testing: " + suite);111for (int i = 0; i < protocols.length; i++) {112System.out.print(protocols[i] + " ");113}114115/*116* Don't run the Kerberized suites for now.117*/118if (suite.startsWith("TLS_KRB5")) {119System.out.println("Ignoring Kerberized suite");120return;121}122123/*124* Don't run the SCSV suite125*/126if (suite.equals("TLS_EMPTY_RENEGOTIATION_INFO_SCSV")) {127System.out.println("Ignoring SCSV suite");128return;129}130131132if (!suite.contains("DH_anon")) {133ssle2.setNeedClientAuth(true);134}135136String [] suites = new String [] { suite };137138ssle1.setEnabledCipherSuites(suites);139ssle2.setEnabledCipherSuites(suites);140141ssle1.setEnabledProtocols(protocols);142ssle2.setEnabledProtocols(protocols);143144createBuffers();145146SSLEngineResult result1; // ssle1's results from last operation147SSLEngineResult result2; // ssle2's results from last operation148149Date start = new Date();150while (!isEngineClosed(ssle1) || !isEngineClosed(ssle2)) {151152log("----------------");153154result1 = ssle1.wrap(appOut1, oneToTwo);155result2 = ssle2.wrap(appOut2, twoToOne);156157log("wrap1: " + result1);158log("oneToTwo = " + oneToTwo);159log("");160161log("wrap2: " + result2);162log("twoToOne = " + twoToOne);163164runDelegatedTasks(result1, ssle1);165runDelegatedTasks(result2, ssle2);166167oneToTwo.flip();168twoToOne.flip();169170log("----");171172result1 = ssle1.unwrap(twoToOne, appIn1);173result2 = ssle2.unwrap(oneToTwo, appIn2);174175log("unwrap1: " + result1);176log("twoToOne = " + twoToOne);177log("");178179log("unwrap2: " + result2);180log("oneToTwo = " + oneToTwo);181182runDelegatedTasks(result1, ssle1);183runDelegatedTasks(result2, ssle2);184185oneToTwo.compact();186twoToOne.compact();187188/*189* If we've transfered all the data between app1 and app2,190* we try to close and see what that gets us.191*/192if (!dataDone && (appOut1.limit() == appIn2.position()) &&193(appOut2.limit() == appIn1.position())) {194195checkTransfer(appOut1, appIn2);196checkTransfer(appOut2, appIn1);197198log("Closing ssle1's *OUTBOUND*...");199ssle1.closeOutbound();200dataDone = true;201}202}203204/*205* Just for grins, try closing again, make sure nothing206* strange is happening after we're closed.207*/208ssle1.closeInbound();209ssle1.closeOutbound();210211ssle2.closeInbound();212ssle2.closeOutbound();213214appOut1.rewind();215appIn1.clear();216oneToTwo.clear();217218result1 = ssle1.wrap(appOut1, oneToTwo);219checkResult(result1);220221result1 = ssle1.unwrap(oneToTwo, appIn1);222checkResult(result1);223224System.out.println("Test Passed.");225System.out.println("\n======================================");226227Date end = new Date();228elapsed += end.getTime() - start.getTime();229230}231232static long elapsed = 0;233234private static void checkResult(SSLEngineResult result) throws Exception {235if ((result.getStatus() != Status.CLOSED) ||236(result.getHandshakeStatus() !=237HandshakeStatus.NOT_HANDSHAKING) ||238(result.bytesConsumed() != 0) ||239(result.bytesProduced() != 0)) {240throw new Exception("Unexpected close status");241}242}243244public static void main(String args[]) throws Exception {245246TestAllSuites tas;247248tas = new TestAllSuites();249250tas.createSSLEngines();251252tas.test();253254System.out.println("All Tests Passed.");255System.out.println("Elapsed time: " + elapsed / 1000.0);256}257258/*259* **********************************************************260* Majority of the test case is above, below is just setup stuff261* **********************************************************262*/263264public TestAllSuites() throws Exception {265sslc = getSSLContext(keyFilename, trustFilename);266}267268/*269* Create an initialized SSLContext to use for this test.270*/271private SSLContext getSSLContext(String keyFile, String trustFile)272throws Exception {273274KeyStore ks = KeyStore.getInstance("JKS");275KeyStore ts = KeyStore.getInstance("JKS");276277char[] passphrase = "passphrase".toCharArray();278279ks.load(new FileInputStream(keyFile), passphrase);280ts.load(new FileInputStream(trustFile), passphrase);281282KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");283kmf.init(ks, passphrase);284285TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");286tmf.init(ts);287288SSLContext sslCtx = SSLContext.getInstance("TLS");289290sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);291292return sslCtx;293}294295private void createBuffers() {296// Size the buffers as appropriate.297298SSLSession session = ssle1.getSession();299int appBufferMax = session.getApplicationBufferSize();300int netBufferMax = session.getPacketBufferSize();301302appIn1 = ByteBuffer.allocateDirect(appBufferMax + 50);303appIn2 = ByteBuffer.allocateDirect(appBufferMax + 50);304305oneToTwo = ByteBuffer.allocateDirect(netBufferMax);306twoToOne = ByteBuffer.allocateDirect(netBufferMax);307308appOut1 = ByteBuffer.wrap("Hi Engine2, I'm SSLEngine1".getBytes());309appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes());310311log("AppOut1 = " + appOut1);312log("AppOut2 = " + appOut2);313log("");314}315316private static void runDelegatedTasks(SSLEngineResult result,317SSLEngine engine) throws Exception {318319if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {320Runnable runnable;321while ((runnable = engine.getDelegatedTask()) != null) {322log("running delegated task...");323runnable.run();324}325}326}327328private static boolean isEngineClosed(SSLEngine engine) {329return (engine.isOutboundDone() && engine.isInboundDone());330}331332private static void checkTransfer(ByteBuffer a, ByteBuffer b)333throws Exception {334a.flip();335b.flip();336337if (!a.equals(b)) {338throw new Exception("Data didn't transfer cleanly");339} else {340log("Data transferred cleanly");341}342343a.position(a.limit());344b.position(b.limit());345a.limit(a.capacity());346b.limit(b.capacity());347}348349private static void log(String str) {350if (debug) {351System.out.println(str);352}353}354}355356357