Path: blob/master/test/jdk/javax/net/ssl/SSLEngine/CheckStatus.java
41152 views
/*1* Copyright (c) 2003, 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 494807926* @summary SSLEngineResult needs updating [none yet]27* @ignore the dependent implementation details are changed28* @run main/othervm -Djsse.enableCBCProtection=false CheckStatus29*30* @author Brad Wetmore31*/3233/*34* This is a simple hack to test a bunch of conditions and check35* their return codes.36*/37import javax.net.ssl.*;38import javax.net.ssl.SSLEngineResult.*;39import java.io.*;40import java.security.*;41import java.nio.*;4243public class CheckStatus {4445private static boolean debug = true;4647private SSLContext sslc;48private SSLEngine ssle1; // client49private SSLEngine ssle2; // server5051private static String pathToStores = "../etc";52private static String keyStoreFile = "keystore";53private static String trustStoreFile = "truststore";54private static String passwd = "passphrase";5556private static String keyFilename =57System.getProperty("test.src", "./") + "/" + pathToStores +58"/" + keyStoreFile;59private static String trustFilename =60System.getProperty("test.src", "./") + "/" + pathToStores +61"/" + trustStoreFile;6263private ByteBuffer appOut1; // write side of ssle164private ByteBuffer appIn1; // read side of ssle165private ByteBuffer appOut2; // write side of ssle266private ByteBuffer appIn2; // read side of ssle26768private ByteBuffer oneToTwo; // "reliable" transport ssle1->ssle269private ByteBuffer twoToOne; // "reliable" transport ssle2->ssle17071/*72* Majority of the test case is here, setup is done below.73*/7475private void createSSLEngines() throws Exception {76ssle1 = sslc.createSSLEngine("client", 1);77ssle1.setUseClientMode(true);7879ssle2 = sslc.createSSLEngine("server", 2);80ssle2.setUseClientMode(false);81}8283private boolean isHandshaking(SSLEngine e) {84return (e.getHandshakeStatus() != HandshakeStatus.NOT_HANDSHAKING);85}8687private void checkResult(ByteBuffer bbIn, ByteBuffer bbOut,88SSLEngineResult result,89Status status, HandshakeStatus hsStatus,90int consumed, int produced)91throws Exception {9293if ((status != null) && (result.getStatus() != status)) {94throw new Exception("Unexpected Status: need = " + status +95" got = " + result.getStatus());96}9798if ((hsStatus != null) && (result.getHandshakeStatus() != hsStatus)) {99throw new Exception("Unexpected hsStatus: need = " + hsStatus +100" got = " + result.getHandshakeStatus());101}102103if ((consumed != -1) && (consumed != result.bytesConsumed())) {104throw new Exception("Unexpected consumed: need = " + consumed +105" got = " + result.bytesConsumed());106}107108if ((produced != -1) && (produced != result.bytesProduced())) {109throw new Exception("Unexpected produced: need = " + produced +110" got = " + result.bytesProduced());111}112113if ((consumed != -1) && (bbIn.position() != result.bytesConsumed())) {114throw new Exception("Consumed " + bbIn.position() +115" != " + consumed);116}117118if ((produced != -1) && (bbOut.position() != result.bytesProduced())) {119throw new Exception("produced " + bbOut.position() +120" != " + produced);121}122}123124private void test() throws Exception {125createSSLEngines();126createBuffers();127128SSLEngineResult result1; // ssle1's results from last operation129SSLEngineResult result2; // ssle2's results from last operation130131String [] suite1 = new String [] {132"SSL_RSA_WITH_RC4_128_MD5" };133String [] suite2 = new String [] {134"SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA" };135136ssle1.setEnabledCipherSuites(suite1);137ssle2.setEnabledCipherSuites(suite1);138139log("================");140141log("unexpected empty unwrap");142twoToOne.limit(0);143result1 = ssle1.unwrap(twoToOne, appIn1);144checkResult(twoToOne, appIn1, result1,145Status.OK, HandshakeStatus.NEED_WRAP, 0, 0);146twoToOne.limit(twoToOne.capacity());147148log("======================================");149log("client hello");150result1 = ssle1.wrap(appOut1, oneToTwo);151checkResult(appOut1, oneToTwo, result1,152Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1);153154oneToTwo.flip();155result2 = ssle2.unwrap(oneToTwo, appIn2);156157checkResult(oneToTwo, appIn2, result2,158Status.OK, HandshakeStatus.NEED_TASK, result1.bytesProduced(), 0);159runDelegatedTasks(ssle2);160161oneToTwo.compact();162163log("Check for unwrap when wrap needed");164result2 = ssle2.unwrap(oneToTwo, appIn2);165checkResult(oneToTwo, appIn2, result2,166Status.OK, HandshakeStatus.NEED_WRAP, 0, 0);167168log("======================================");169log("ServerHello");170171result2 = ssle2.wrap(appOut2, twoToOne);172checkResult(appOut2, twoToOne, result2,173Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1);174twoToOne.flip();175176result1 = ssle1.unwrap(twoToOne, appIn1);177checkResult(twoToOne, appIn1, result1,178Status.OK, HandshakeStatus.NEED_TASK, result2.bytesProduced(), 0);179twoToOne.compact();180181runDelegatedTasks(ssle1);182183log("======================================");184log("Key Exchange");185result1 = ssle1.wrap(appOut1, oneToTwo);186checkResult(appOut1, oneToTwo, result1,187Status.OK, HandshakeStatus.NEED_WRAP, 0, -1);188189oneToTwo.flip();190result2 = ssle2.unwrap(oneToTwo, appIn2);191192checkResult(oneToTwo, appIn2, result2,193Status.OK, HandshakeStatus.NEED_TASK, result1.bytesProduced(), 0);194runDelegatedTasks(ssle2);195196oneToTwo.compact();197198log("======================================");199log("CCS");200result1 = ssle1.wrap(appOut1, oneToTwo);201checkResult(appOut1, oneToTwo, result1,202Status.OK, HandshakeStatus.NEED_WRAP, 0, -1);203204oneToTwo.flip();205result2 = ssle2.unwrap(oneToTwo, appIn2);206207checkResult(oneToTwo, appIn2, result2,208Status.OK, HandshakeStatus.NEED_UNWRAP,209result1.bytesProduced(), 0);210211oneToTwo.compact();212213log("======================================");214log("Finished");215result1 = ssle1.wrap(appOut1, oneToTwo);216checkResult(appOut1, oneToTwo, result1,217Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1);218219oneToTwo.flip();220result2 = ssle2.unwrap(oneToTwo, appIn2);221222checkResult(oneToTwo, appIn2, result2,223Status.OK, HandshakeStatus.NEED_WRAP, result1.bytesProduced(), 0);224225oneToTwo.compact();226227log("======================================");228log("CCS");229230result2 = ssle2.wrap(appOut2, twoToOne);231checkResult(appOut2, twoToOne, result2,232Status.OK, HandshakeStatus.NEED_WRAP, 0, -1);233twoToOne.flip();234235result1 = ssle1.unwrap(twoToOne, appIn1);236checkResult(twoToOne, appIn1, result1,237Status.OK, HandshakeStatus.NEED_UNWRAP, result2.bytesProduced(), 0);238twoToOne.compact();239240log("======================================");241log("FINISHED");242243result2 = ssle2.wrap(appOut2, twoToOne);244checkResult(appOut2, twoToOne, result2,245Status.OK, HandshakeStatus.FINISHED, 0, -1);246twoToOne.flip();247248result1 = ssle1.unwrap(twoToOne, appIn1);249checkResult(twoToOne, appIn1, result1,250Status.OK, HandshakeStatus.FINISHED, result2.bytesProduced(), 0);251twoToOne.compact();252253log("======================================");254log("Check Session/Ciphers");255256String suite = ssle1.getSession().getCipherSuite();257if (!suite.equals(suite1[0])) {258throw new Exception("suites not equal: " + suite + "/" +259suite1[0]);260}261262suite = ssle2.getSession().getCipherSuite();263if (!suite.equals(suite1[0])) {264throw new Exception("suites not equal: " + suite + "/" +265suite1[0]);266}267268log("======================================");269log("DATA");270271result1 = ssle1.wrap(appOut1, oneToTwo);272checkResult(appOut1, oneToTwo, result1,273Status.OK, HandshakeStatus.NOT_HANDSHAKING,274appOut1.capacity(), -1);275oneToTwo.flip();276277result2 = ssle2.wrap(appOut2, twoToOne);278checkResult(appOut2, twoToOne, result2,279Status.OK, HandshakeStatus.NOT_HANDSHAKING,280appOut2.capacity(), -1);281twoToOne.flip();282283SSLEngineResult result3 = ssle1.unwrap(twoToOne, appIn1);284checkResult(twoToOne, appIn1, result3,285Status.OK, HandshakeStatus.NOT_HANDSHAKING,286result2.bytesProduced(), result2.bytesConsumed());287twoToOne.compact();288289SSLEngineResult result4 = ssle2.unwrap(oneToTwo, appIn2);290checkResult(oneToTwo, appIn2, result4,291Status.OK, HandshakeStatus.NOT_HANDSHAKING,292result1.bytesProduced(), result1.bytesConsumed());293oneToTwo.compact();294295appIn1.clear();296appIn2.clear();297appOut1.rewind();298appOut2.rewind();299300log("======================================");301log("RENEGOTIATE");302303ssle2.getSession().invalidate();304ssle2.setNeedClientAuth(true);305306ssle1.setEnabledCipherSuites(suite2);307ssle2.setEnabledCipherSuites(suite2);308309ssle2.beginHandshake();310311log("======================================");312log("HelloRequest");313314result2 = ssle2.wrap(appOut2, twoToOne);315checkResult(appOut2, twoToOne, result2,316Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1);317twoToOne.flip();318319result1 = ssle1.unwrap(twoToOne, appIn1);320checkResult(twoToOne, appIn1, result1,321Status.OK, HandshakeStatus.NEED_TASK, result2.bytesProduced(), 0);322twoToOne.compact();323324runDelegatedTasks(ssle1);325326log("======================================");327log("ClientHello");328329result1 = ssle1.wrap(appOut1, oneToTwo);330checkResult(appOut1, oneToTwo, result1,331Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1);332333oneToTwo.flip();334result2 = ssle2.unwrap(oneToTwo, appIn2);335336checkResult(oneToTwo, appIn2, result2,337Status.OK, HandshakeStatus.NEED_TASK, result1.bytesProduced(), 0);338runDelegatedTasks(ssle2);339340oneToTwo.compact();341342log("======================================");343log("CLIENT->SERVER DATA IN MIDDLE OF HANDSHAKE");344345result1 = ssle1.wrap(appOut1, oneToTwo);346checkResult(appOut1, oneToTwo, result1,347Status.OK, HandshakeStatus.NEED_UNWRAP,348appOut1.capacity(), -1);349oneToTwo.flip();350351result4 = ssle2.unwrap(oneToTwo, appIn2);352checkResult(oneToTwo, appIn2, result4,353Status.OK, HandshakeStatus.NEED_WRAP,354result1.bytesProduced(), result1.bytesConsumed());355oneToTwo.compact();356357appIn2.clear();358appOut1.rewind();359360log("======================================");361log("ServerHello");362363result2 = ssle2.wrap(appOut2, twoToOne);364checkResult(appOut2, twoToOne, result2,365Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1);366twoToOne.flip();367368result1 = ssle1.unwrap(twoToOne, appIn1);369checkResult(twoToOne, appIn1, result1,370Status.OK, HandshakeStatus.NEED_TASK, result2.bytesProduced(), 0);371twoToOne.compact();372373runDelegatedTasks(ssle1);374375log("======================================");376log("SERVER->CLIENT DATA IN MIDDLE OF HANDSHAKE");377378result2 = ssle2.wrap(appOut2, twoToOne);379checkResult(appOut2, twoToOne, result2,380Status.OK, HandshakeStatus.NEED_UNWRAP,381appOut2.capacity(), -1);382twoToOne.flip();383384result3 = ssle1.unwrap(twoToOne, appIn1);385checkResult(twoToOne, appIn1, result3,386Status.OK, HandshakeStatus.NEED_WRAP,387result2.bytesProduced(), result2.bytesConsumed());388twoToOne.compact();389390appIn1.clear();391appOut2.rewind();392393log("======================================");394log("Client Cert and Key Exchange");395result1 = ssle1.wrap(appOut1, oneToTwo);396checkResult(appOut1, oneToTwo, result1,397Status.OK, HandshakeStatus.NEED_WRAP, 0, -1);398399oneToTwo.flip();400result2 = ssle2.unwrap(oneToTwo, appIn2);401402checkResult(oneToTwo, appIn2, result2,403Status.OK, HandshakeStatus.NEED_TASK, result1.bytesProduced(), 0);404runDelegatedTasks(ssle2);405406oneToTwo.compact();407408log("======================================");409log("CCS");410result1 = ssle1.wrap(appOut1, oneToTwo);411checkResult(appOut1, oneToTwo, result1,412Status.OK, HandshakeStatus.NEED_WRAP, 0, -1);413414oneToTwo.flip();415result2 = ssle2.unwrap(oneToTwo, appIn2);416417checkResult(oneToTwo, appIn2, result2,418Status.OK, HandshakeStatus.NEED_UNWRAP,419result1.bytesProduced(), 0);420421oneToTwo.compact();422423log("======================================");424log("Finished");425result1 = ssle1.wrap(appOut1, oneToTwo);426checkResult(appOut1, oneToTwo, result1,427Status.OK, HandshakeStatus.NEED_UNWRAP, 0, -1);428429oneToTwo.flip();430result2 = ssle2.unwrap(oneToTwo, appIn2);431432checkResult(oneToTwo, appIn2, result2,433Status.OK, HandshakeStatus.NEED_WRAP, result1.bytesProduced(), 0);434435oneToTwo.compact();436437log("======================================");438log("CCS");439440result2 = ssle2.wrap(appOut2, twoToOne);441checkResult(appOut2, twoToOne, result2,442Status.OK, HandshakeStatus.NEED_WRAP, 0, -1);443twoToOne.flip();444445result1 = ssle1.unwrap(twoToOne, appIn1);446checkResult(twoToOne, appIn1, result1,447Status.OK, HandshakeStatus.NEED_UNWRAP, result2.bytesProduced(), 0);448twoToOne.compact();449450log("======================================");451log("FINISHED");452453result2 = ssle2.wrap(appOut2, twoToOne);454checkResult(appOut2, twoToOne, result2,455Status.OK, HandshakeStatus.FINISHED, 0, -1);456twoToOne.flip();457458result1 = ssle1.unwrap(twoToOne, appIn1);459checkResult(twoToOne, appIn1, result1,460Status.OK, HandshakeStatus.FINISHED, result2.bytesProduced(), 0);461twoToOne.compact();462463log("======================================");464log("Check Session/Ciphers");465466suite = ssle1.getSession().getCipherSuite();467if (!suite.equals(suite2[0])) {468throw new Exception("suites not equal: " + suite + "/" +469suite2[0]);470}471472suite = ssle2.getSession().getCipherSuite();473if (!suite.equals(suite2[0])) {474throw new Exception("suites not equal: " + suite + "/" +475suite2[0]);476}477478log("======================================");479log("DATA USING NEW SESSION");480481result1 = ssle1.wrap(appOut1, oneToTwo);482checkResult(appOut1, oneToTwo, result1,483Status.OK, HandshakeStatus.NOT_HANDSHAKING,484appOut1.capacity(), -1);485oneToTwo.flip();486487result2 = ssle2.wrap(appOut2, twoToOne);488checkResult(appOut2, twoToOne, result2,489Status.OK, HandshakeStatus.NOT_HANDSHAKING,490appOut2.capacity(), -1);491twoToOne.flip();492493result3 = ssle1.unwrap(twoToOne, appIn1);494checkResult(twoToOne, appIn1, result3,495Status.OK, HandshakeStatus.NOT_HANDSHAKING,496result2.bytesProduced(), result2.bytesConsumed());497twoToOne.compact();498499result4 = ssle2.unwrap(oneToTwo, appIn2);500checkResult(oneToTwo, appIn2, result4,501Status.OK, HandshakeStatus.NOT_HANDSHAKING,502result1.bytesProduced(), result1.bytesConsumed());503oneToTwo.compact();504505appIn1.clear();506appIn2.clear();507appOut1.rewind();508appOut2.rewind();509510log("======================================");511log("CN");512513if (isHandshaking(ssle1)) {514throw new Exception("ssle1 IS handshaking");515}516517if (isHandshaking(ssle2)) {518throw new Exception("ssle2 IS handshaking");519}520521ssle2.closeOutbound();522523if (!isHandshaking(ssle2)) {524throw new Exception("ssle1 IS NOT handshaking");525}526527appOut1.rewind();528appOut2.rewind();529530result2 = ssle2.wrap(appOut2, twoToOne);531checkResult(appOut2, twoToOne, result2,532Status.CLOSED, HandshakeStatus.NEED_UNWRAP, 0, -1);533twoToOne.flip();534535if (ssle1.isInboundDone()) {536throw new Exception("ssle1 inboundDone");537}538539result1 = ssle1.unwrap(twoToOne, appIn1);540checkResult(twoToOne, appIn1, result1,541Status.CLOSED, HandshakeStatus.NEED_WRAP,542result2.bytesProduced(), 0);543twoToOne.compact();544545if (!ssle1.isInboundDone()) {546throw new Exception("ssle1 inboundDone");547}548549if (!isHandshaking(ssle1)) {550throw new Exception("ssle1 IS NOT handshaking");551}552553result2 = ssle2.wrap(appOut2, twoToOne);554checkResult(appOut2, twoToOne, result2,555Status.CLOSED, HandshakeStatus.NEED_UNWRAP, 0, 0);556twoToOne.flip();557558log("======================================");559log("CN response");560561if (ssle1.isOutboundDone()) {562throw new Exception("ssle1 outboundDone");563}564565result1 = ssle1.wrap(appOut1, oneToTwo);566checkResult(appOut1, oneToTwo, result1,567Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING, 0, -1);568569if (!ssle1.isOutboundDone()) {570throw new Exception("ssle1 outboundDone is NOT done");571}572573if (isHandshaking(ssle1)) {574throw new Exception("ssle1 IS handshaking");575}576577oneToTwo.flip();578579if (!ssle2.isOutboundDone()) {580throw new Exception("ssle1 outboundDone");581}582583if (ssle2.isInboundDone()) {584throw new Exception("ssle1 inboundDone");585}586587result2 = ssle2.unwrap(oneToTwo, appIn2);588589checkResult(oneToTwo, appIn2, result2,590Status.CLOSED, HandshakeStatus.NOT_HANDSHAKING,591result1.bytesProduced(), 0);592593if (!ssle2.isOutboundDone()) {594throw new Exception("ssle1 outboundDone is NOT done");595}596597if (!ssle2.isInboundDone()) {598throw new Exception("ssle1 inboundDone is NOT done");599}600601if (isHandshaking(ssle2)) {602throw new Exception("ssle1 IS handshaking");603}604605oneToTwo.compact();606}607608public static void main(String args[]) throws Exception {609// reset the security property to make sure that the algorithms610// and keys used in this test are not disabled.611Security.setProperty("jdk.tls.disabledAlgorithms", "");612613CheckStatus cs;614615cs = new CheckStatus();616617cs.createSSLEngines();618619cs.test();620621System.out.println("Test Passed.");622}623624/*625* **********************************************************626* Majority of the test case is above, below is just setup stuff627* **********************************************************628*/629630public CheckStatus() throws Exception {631sslc = getSSLContext(keyFilename, trustFilename);632}633634/*635* Create an initialized SSLContext to use for this test.636*/637private SSLContext getSSLContext(String keyFile, String trustFile)638throws Exception {639640KeyStore ks = KeyStore.getInstance("JKS");641KeyStore ts = KeyStore.getInstance("JKS");642643char[] passphrase = "passphrase".toCharArray();644645ks.load(new FileInputStream(keyFile), passphrase);646ts.load(new FileInputStream(trustFile), passphrase);647648KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");649kmf.init(ks, passphrase);650651TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");652tmf.init(ts);653654SSLContext sslCtx = SSLContext.getInstance("TLS");655656sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);657658return sslCtx;659}660661private void createBuffers() {662// Size the buffers as appropriate.663664SSLSession session = ssle1.getSession();665int appBufferMax = session.getApplicationBufferSize();666int netBufferMax = session.getPacketBufferSize();667668appIn1 = ByteBuffer.allocateDirect(appBufferMax + 50);669appIn2 = ByteBuffer.allocateDirect(appBufferMax + 50);670671oneToTwo = ByteBuffer.allocateDirect(netBufferMax);672twoToOne = ByteBuffer.allocateDirect(netBufferMax);673674appOut1 = ByteBuffer.wrap("Hi Engine2, I'm SSLEngine1".getBytes());675appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes());676677log("AppOut1 = " + appOut1);678log("AppOut2 = " + appOut2);679log("");680}681682private static void runDelegatedTasks(SSLEngine engine) throws Exception {683684Runnable runnable;685while ((runnable = engine.getDelegatedTask()) != null) {686log("running delegated task...");687runnable.run();688}689}690691private static void checkTransfer(ByteBuffer a, ByteBuffer b)692throws Exception {693a.flip();694b.flip();695696if (!a.equals(b)) {697throw new Exception("Data didn't transfer cleanly");698} else {699log("Data transferred cleanly");700}701702a.position(a.limit());703b.position(b.limit());704a.limit(a.capacity());705b.limit(b.capacity());706}707708private static void log(String str) {709if (debug) {710System.out.println(str);711}712}713}714715716