Path: blob/master/test/jdk/javax/net/ssl/SSLEngine/ExtendedKeyEngine.java
41152 views
/*1* Copyright (c) 2004, 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 498169726* @summary Rework the X509KeyManager to avoid incompatibility issues27* @author Brad R. Wetmore28*29* @run main/othervm -Djdk.tls.acknowledgeCloseNotify=true ExtendedKeyEngine30*/3132import javax.net.ssl.*;33import javax.net.ssl.SSLEngineResult.*;34import java.io.*;35import java.security.*;36import java.nio.*;3738public class ExtendedKeyEngine {3940private static boolean debug = false;4142private SSLContext sslc;43private SSLEngine ssle1; // client44private SSLEngine ssle2; // server4546private static String pathToStores = "../etc";47private static String keyStoreFile = "keystore";48private static String trustStoreFile = "truststore";49private static String passwd = "passphrase";5051private static String keyFilename =52System.getProperty("test.src", "./") + "/" + pathToStores +53"/" + keyStoreFile;54private static String trustFilename =55System.getProperty("test.src", "./") + "/" + pathToStores +56"/" + trustStoreFile;5758private ByteBuffer appOut1; // write side of ssle159private ByteBuffer appIn1; // read side of ssle160private ByteBuffer appOut2; // write side of ssle261private ByteBuffer appIn2; // read side of ssle26263private ByteBuffer oneToTwo; // "reliable" transport ssle1->ssle264private ByteBuffer twoToOne; // "reliable" transport ssle2->ssle16566/*67* Majority of the test case is here, setup is done below.68*/69private void createSSLEngines() throws Exception {70ssle1 = sslc.createSSLEngine("client", 1);71ssle1.setUseClientMode(true);7273ssle2 = sslc.createSSLEngine();74ssle2.setUseClientMode(false);75ssle2.setNeedClientAuth(true);76}7778private void runTest() throws Exception {79boolean dataDone = false;8081createSSLEngines();82createBuffers();8384SSLEngineResult result1; // ssle1's results from last operation85SSLEngineResult result2; // ssle2's results from last operation8687while (!isEngineClosed(ssle1) || !isEngineClosed(ssle2)) {8889log("================");9091result1 = ssle1.wrap(appOut1, oneToTwo);92result2 = ssle2.wrap(appOut2, twoToOne);9394log("wrap1: " + result1);95log("oneToTwo = " + oneToTwo);96log("");9798log("wrap2: " + result2);99log("twoToOne = " + twoToOne);100101runDelegatedTasks(result1, ssle1);102runDelegatedTasks(result2, ssle2);103104oneToTwo.flip();105twoToOne.flip();106107log("----");108109result1 = ssle1.unwrap(twoToOne, appIn1);110result2 = ssle2.unwrap(oneToTwo, appIn2);111112log("unwrap1: " + result1);113log("twoToOne = " + twoToOne);114log("");115116log("unwrap2: " + result2);117log("oneToTwo = " + oneToTwo);118119runDelegatedTasks(result1, ssle1);120runDelegatedTasks(result2, ssle2);121122oneToTwo.compact();123twoToOne.compact();124125/*126* If we've transfered all the data between app1 and app2,127* we try to close and see what that gets us.128*/129if (!dataDone && (appOut1.limit() == appIn2.position()) &&130(appOut2.limit() == appIn1.position())) {131132checkTransfer(appOut1, appIn2);133checkTransfer(appOut2, appIn1);134135log("Closing ssle1's *OUTBOUND*...");136ssle1.closeOutbound();137dataDone = true;138}139}140}141142public static void main(String args[]) throws Exception {143144ExtendedKeyEngine test;145146System.out.println("This test should run to completion");147test = new ExtendedKeyEngine(true);148test.createSSLEngines();149test.runTest();150System.out.println("Done!");151152System.out.println("This test should fail with a Handshake Error");153test = new ExtendedKeyEngine(false);154test.createSSLEngines();155156try {157test.runTest();158} catch (SSLHandshakeException e) {159System.out.println(160"Caught proper exception, should be 'no suites in common'");161e.printStackTrace();162}163164System.out.println("Test Passed.");165}166167/*168* **********************************************************169* Majority of the test case is above, below is just setup stuff170* **********************************************************171*/172173public ExtendedKeyEngine(boolean abs) throws Exception {174sslc = getSSLContext(keyFilename, trustFilename, abs);175}176177/*178* Create an initialized SSLContext to use for this test.179*/180private SSLContext getSSLContext(String keyFile, String trustFile,181boolean abs) throws Exception {182183KeyStore ks = KeyStore.getInstance("JKS");184KeyStore ts = KeyStore.getInstance("JKS");185186char[] passphrase = "passphrase".toCharArray();187188ks.load(new FileInputStream(keyFile), passphrase);189ts.load(new FileInputStream(trustFile), passphrase);190191KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");192kmf.init(ks, passphrase);193194KeyManager [] kms = kmf.getKeyManagers();195if (abs) {196kms = new KeyManager [] {197new MyX509ExtendedKeyManager((X509ExtendedKeyManager)kms[0])198};199} else {200kms = new KeyManager [] {201new MyX509KeyManager((X509KeyManager)kms[0])202};203}204205TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");206tmf.init(ts);207TrustManager [] tms = tmf.getTrustManagers();208209SSLContext sslCtx = SSLContext.getInstance("TLS");210211sslCtx.init(kms, tms, null);212213return sslCtx;214}215216private void createBuffers() {217// Size the buffers as appropriate.218219SSLSession session = ssle1.getSession();220int appBufferMax = session.getApplicationBufferSize();221int netBufferMax = session.getPacketBufferSize();222223appIn1 = ByteBuffer.allocateDirect(appBufferMax + 50);224appIn2 = ByteBuffer.allocateDirect(appBufferMax + 50);225226oneToTwo = ByteBuffer.allocateDirect(netBufferMax);227twoToOne = ByteBuffer.allocateDirect(netBufferMax);228229appOut1 = ByteBuffer.wrap("Hi Engine2, I'm SSLEngine1".getBytes());230appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes());231232log("AppOut1 = " + appOut1);233log("AppOut2 = " + appOut2);234log("");235}236237private static void runDelegatedTasks(SSLEngineResult result,238SSLEngine engine) throws Exception {239240if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {241Runnable runnable;242while ((runnable = engine.getDelegatedTask()) != null) {243log("running delegated task...");244runnable.run();245}246}247}248249private static boolean isEngineClosed(SSLEngine engine) {250return (engine.isOutboundDone() && engine.isInboundDone());251}252253private static void checkTransfer(ByteBuffer a, ByteBuffer b)254throws Exception {255a.flip();256b.flip();257258if (!a.equals(b)) {259throw new Exception("Data didn't transfer cleanly");260} else {261log("Data transferred cleanly");262}263264a.position(a.limit());265b.position(b.limit());266a.limit(a.capacity());267b.limit(b.capacity());268}269270private static void log(String str) {271if (debug) {272System.out.println(str);273}274}275}276277278