Path: blob/master/test/jdk/javax/net/ssl/SSLEngine/Arrays.java
41152 views
/*1* Copyright (c) 2004, 2020, 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 501909626* @summary Add scatter/gather APIs for SSLEngine27* @library /test/lib28* @run main/othervm Arrays SSL29* @run main/othervm Arrays TLS30* @run main/othervm Arrays SSLv331* @run main/othervm Arrays TLSv132* @run main/othervm Arrays TLSv1.133* @run main/othervm Arrays TLSv1.234* @run main/othervm Arrays TLSv1.335* @run main/othervm -Djdk.tls.acknowledgeCloseNotify=true Arrays TLSv1.336*/3738import javax.net.ssl.*;39import javax.net.ssl.SSLEngineResult.*;40import java.io.*;41import java.security.*;42import java.nio.*;4344import jdk.test.lib.security.SecurityUtils;4546public class Arrays {4748private static boolean debug = false;49private static boolean acknowledgeCloseNotify =50"true".equals(System.getProperty("jdk.tls.acknowledgeCloseNotify"));5152private SSLContext sslc;53private SSLEngine ssle1; // client54private SSLEngine ssle2; // server5556private static String pathToStores = "../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 [] appOutArray1;69private ByteBuffer [] appInArray1;7071private ByteBuffer appOut2; // write side of ssle272private ByteBuffer appIn2; // read side of ssle27374private ByteBuffer oneToTwo; // "reliable" transport ssle1->ssle275private ByteBuffer twoToOne; // "reliable" transport ssle2->ssle17677/*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() throws Exception {90boolean dataDone = false;9192createSSLEngines();93createBuffers();9495SSLEngineResult result1; // ssle1's results from last operation96SSLEngineResult result2; // ssle2's results from last operation9798while (!isEngineClosed(ssle1) || !isEngineClosed(ssle2)) {99100log("================");101102result1 = ssle1.wrap(appOutArray1, oneToTwo);103result2 = ssle2.wrap(appOut2, twoToOne);104105log("wrap1: " + result1);106log("oneToTwo = " + oneToTwo);107log("");108109log("wrap2: " + result2);110log("twoToOne = " + twoToOne);111112runDelegatedTasks(result1, ssle1);113runDelegatedTasks(result2, ssle2);114115oneToTwo.flip();116twoToOne.flip();117118log("----");119120result1 = ssle1.unwrap(twoToOne, appInArray1);121result2 = ssle2.unwrap(oneToTwo, appIn2);122123log("unwrap1: " + result1);124log("twoToOne = " + twoToOne);125log("");126127log("unwrap2: " + result2);128log("oneToTwo = " + oneToTwo);129130runDelegatedTasks(result1, ssle1);131runDelegatedTasks(result2, ssle2);132133oneToTwo.compact();134twoToOne.compact();135136/*137* If we've transfered all the data between app1 and app2,138* we try to close and see what that gets us.139*/140if (!dataDone) {141boolean done = true;142143for (int i = 0; i < appOutArray1.length; i++) {144if (appOutArray1[i].remaining() != 0) {145log("1st out not done");146done = false;147}148}149150if (appOut2.remaining() != 0) {151log("2nd out not done");152done = false;153}154155if (done) {156log("Closing ssle1's *OUTBOUND*...");157for (int i = 0; i < appOutArray1.length; i++) {158appOutArray1[i].rewind();159}160ssle1.closeOutbound();161String protocol = ssle2.getSession().getProtocol();162if (!acknowledgeCloseNotify) {163switch (ssle2.getSession().getProtocol()) {164case "SSLv3":165case "TLSv1":166case "TLSv1.1":167case "TLSv1.2":168break;169default: // TLSv1.3170// TLS 1.3, half-close only.171ssle2.closeOutbound();172}173}174dataDone = true;175}176}177}178checkTransfer(appOutArray1, appIn2);179appInArray1[appInArray1.length - 1].limit(180appInArray1[appInArray1.length - 1].position());181checkTransfer(appInArray1, appOut2);182}183184private static String contextVersion;185public static void main(String args[]) throws Exception {186contextVersion = args[0];187// Re-enable context version if it is disabled.188// If context version is SSLv3, TLSv1 needs to be re-enabled.189if (contextVersion.equals("SSLv3")) {190SecurityUtils.removeFromDisabledTlsAlgs("TLSv1");191} else if (contextVersion.equals("TLSv1") ||192contextVersion.equals("TLSv1.1")) {193SecurityUtils.removeFromDisabledTlsAlgs(contextVersion);194}195196Arrays test;197198test = new Arrays();199200test.createSSLEngines();201202test.runTest();203204System.err.println("Test Passed.");205}206207/*208* **********************************************************209* Majority of the test case is above, below is just setup stuff210* **********************************************************211*/212213public Arrays() throws Exception {214sslc = getSSLContext(keyFilename, trustFilename);215}216217/*218* Create an initialized SSLContext to use for this test.219*/220private SSLContext getSSLContext(String keyFile, String trustFile)221throws Exception {222223KeyStore ks = KeyStore.getInstance("JKS");224KeyStore ts = KeyStore.getInstance("JKS");225226char[] passphrase = "passphrase".toCharArray();227228ks.load(new FileInputStream(keyFile), passphrase);229ts.load(new FileInputStream(trustFile), passphrase);230231KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");232kmf.init(ks, passphrase);233234TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");235tmf.init(ts);236237SSLContext sslCtx = SSLContext.getInstance(contextVersion);238239sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);240241return sslCtx;242}243244private void createBuffers() {245// Size the buffers as appropriate.246247SSLSession session = ssle1.getSession();248int appBufferMax = session.getApplicationBufferSize();249int netBufferMax = session.getPacketBufferSize();250251appIn2 = ByteBuffer.allocateDirect(appBufferMax + 50);252253oneToTwo = ByteBuffer.allocateDirect(netBufferMax);254twoToOne = ByteBuffer.allocateDirect(netBufferMax);255256ByteBuffer strBB = ByteBuffer.wrap(257"Hi Engine2, I'm SSLEngine1, So Be it" .getBytes());258259strBB.position(0);260strBB.limit(5);261ByteBuffer appOut1a = strBB.slice();262263strBB.position(5);264strBB.limit(15);265ByteBuffer appOut1b = strBB.slice();266267strBB.position(15);268strBB.limit(strBB.capacity());269ByteBuffer appOut1c = strBB.slice();270271strBB.rewind();272273appOutArray1 = new ByteBuffer [] { appOut1a, appOut1b, appOut1c };274275appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes());276277ByteBuffer appIn1a = ByteBuffer.allocateDirect(5);278ByteBuffer appIn1b = ByteBuffer.allocateDirect(10);279ByteBuffer appIn1c = ByteBuffer.allocateDirect(appBufferMax + 50);280appInArray1 = new ByteBuffer [] { appIn1a, appIn1b, appIn1c };281282log("AppOut1a = " + appOut1a);283log("AppOut1a = " + appOut1b);284log("AppOut1a = " + appOut1c);285log("AppOut2 = " + appOut2);286log("");287}288289private static void runDelegatedTasks(SSLEngineResult result,290SSLEngine engine) throws Exception {291292if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {293Runnable runnable;294while ((runnable = engine.getDelegatedTask()) != null) {295log("running delegated task...");296runnable.run();297}298}299}300301private static boolean isEngineClosed(SSLEngine engine) {302return (engine.isOutboundDone() && engine.isInboundDone());303}304305private static void checkTransfer(ByteBuffer [] a, ByteBuffer b)306throws Exception {307308b.flip();309310for (int i = 0; i < a.length; i++) {311a[i].rewind();312313b.limit(b.position() + a[i].remaining());314315if (!a[i].equals(b)) {316throw new Exception("Data didn't transfer cleanly");317}318319b.position(b.limit());320}321322log("Data transferred cleanly");323}324325private static void log(String str) {326if (debug) {327System.err.println(str);328}329}330}331332333