Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/net/ssl/SSLEngine/Arrays.java
38853 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 /lib/security28* @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.*;4344public class Arrays {4546private static boolean debug = false;47private static boolean acknowledgeCloseNotify =48"true".equals(System.getProperty("jdk.tls.acknowledgeCloseNotify"));4950private SSLContext sslc;51private SSLEngine ssle1; // client52private SSLEngine ssle2; // server5354private static String pathToStores = "../etc";55private static String keyStoreFile = "keystore";56private static String trustStoreFile = "truststore";57private static String passwd = "passphrase";5859private static String keyFilename =60System.getProperty("test.src", "./") + "/" + pathToStores +61"/" + keyStoreFile;62private static String trustFilename =63System.getProperty("test.src", "./") + "/" + pathToStores +64"/" + trustStoreFile;6566private ByteBuffer [] appOutArray1;67private ByteBuffer [] appInArray1;6869private ByteBuffer appOut2; // write side of ssle270private ByteBuffer appIn2; // read side of ssle27172private ByteBuffer oneToTwo; // "reliable" transport ssle1->ssle273private ByteBuffer twoToOne; // "reliable" transport ssle2->ssle17475/*76* Majority of the test case is here, setup is done below.77*/78private void createSSLEngines() throws Exception {79ssle1 = sslc.createSSLEngine("client", 1);80ssle1.setUseClientMode(true);8182ssle2 = sslc.createSSLEngine();83ssle2.setUseClientMode(false);84ssle2.setNeedClientAuth(true);85}8687private void runTest() throws Exception {88boolean dataDone = false;8990createSSLEngines();91createBuffers();9293SSLEngineResult result1; // ssle1's results from last operation94SSLEngineResult result2; // ssle2's results from last operation9596while (!isEngineClosed(ssle1) || !isEngineClosed(ssle2)) {9798log("================");99100result1 = ssle1.wrap(appOutArray1, oneToTwo);101result2 = ssle2.wrap(appOut2, twoToOne);102103log("wrap1: " + result1);104log("oneToTwo = " + oneToTwo);105log("");106107log("wrap2: " + result2);108log("twoToOne = " + twoToOne);109110runDelegatedTasks(result1, ssle1);111runDelegatedTasks(result2, ssle2);112113oneToTwo.flip();114twoToOne.flip();115116log("----");117118result1 = ssle1.unwrap(twoToOne, appInArray1);119result2 = ssle2.unwrap(oneToTwo, appIn2);120121log("unwrap1: " + result1);122log("twoToOne = " + twoToOne);123log("");124125log("unwrap2: " + result2);126log("oneToTwo = " + oneToTwo);127128runDelegatedTasks(result1, ssle1);129runDelegatedTasks(result2, ssle2);130131oneToTwo.compact();132twoToOne.compact();133134/*135* If we've transfered all the data between app1 and app2,136* we try to close and see what that gets us.137*/138if (!dataDone) {139boolean done = true;140141for (int i = 0; i < appOutArray1.length; i++) {142if (appOutArray1[i].remaining() != 0) {143log("1st out not done");144done = false;145}146}147148if (appOut2.remaining() != 0) {149log("2nd out not done");150done = false;151}152153if (done) {154log("Closing ssle1's *OUTBOUND*...");155for (int i = 0; i < appOutArray1.length; i++) {156appOutArray1[i].rewind();157}158ssle1.closeOutbound();159String protocol = ssle2.getSession().getProtocol();160if (!acknowledgeCloseNotify) {161switch (ssle2.getSession().getProtocol()) {162case "SSLv3":163case "TLSv1":164case "TLSv1.1":165case "TLSv1.2":166break;167default: // TLSv1.3168// TLS 1.3, half-close only.169ssle2.closeOutbound();170}171}172dataDone = true;173}174}175}176checkTransfer(appOutArray1, appIn2);177appInArray1[appInArray1.length - 1].limit(178appInArray1[appInArray1.length - 1].position());179checkTransfer(appInArray1, appOut2);180}181182private static String contextVersion;183public static void main(String args[]) throws Exception {184contextVersion = args[0];185// Re-enable context version if it is disabled.186// If context version is SSLv3, TLSv1 needs to be re-enabled.187if (contextVersion.equals("SSLv3")) {188SecurityUtils.removeFromDisabledTlsAlgs("TLSv1");189} else if (contextVersion.equals("TLSv1") ||190contextVersion.equals("TLSv1.1")) {191SecurityUtils.removeFromDisabledTlsAlgs(contextVersion);192}193194Arrays test;195196test = new Arrays();197198test.createSSLEngines();199200test.runTest();201202System.err.println("Test Passed.");203}204205/*206* **********************************************************207* Majority of the test case is above, below is just setup stuff208* **********************************************************209*/210211public Arrays() throws Exception {212sslc = getSSLContext(keyFilename, trustFilename);213}214215/*216* Create an initialized SSLContext to use for this test.217*/218private SSLContext getSSLContext(String keyFile, String trustFile)219throws Exception {220221KeyStore ks = KeyStore.getInstance("JKS");222KeyStore ts = KeyStore.getInstance("JKS");223224char[] passphrase = "passphrase".toCharArray();225226ks.load(new FileInputStream(keyFile), passphrase);227ts.load(new FileInputStream(trustFile), passphrase);228229KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");230kmf.init(ks, passphrase);231232TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");233tmf.init(ts);234235SSLContext sslCtx = SSLContext.getInstance(contextVersion);236237sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);238239return sslCtx;240}241242private void createBuffers() {243// Size the buffers as appropriate.244245SSLSession session = ssle1.getSession();246int appBufferMax = session.getApplicationBufferSize();247int netBufferMax = session.getPacketBufferSize();248249appIn2 = ByteBuffer.allocateDirect(appBufferMax + 50);250251oneToTwo = ByteBuffer.allocateDirect(netBufferMax);252twoToOne = ByteBuffer.allocateDirect(netBufferMax);253254ByteBuffer strBB = ByteBuffer.wrap(255"Hi Engine2, I'm SSLEngine1, So Be it" .getBytes());256257strBB.position(0);258strBB.limit(5);259ByteBuffer appOut1a = strBB.slice();260261strBB.position(5);262strBB.limit(15);263ByteBuffer appOut1b = strBB.slice();264265strBB.position(15);266strBB.limit(strBB.capacity());267ByteBuffer appOut1c = strBB.slice();268269strBB.rewind();270271appOutArray1 = new ByteBuffer [] { appOut1a, appOut1b, appOut1c };272273appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes());274275ByteBuffer appIn1a = ByteBuffer.allocateDirect(5);276ByteBuffer appIn1b = ByteBuffer.allocateDirect(10);277ByteBuffer appIn1c = ByteBuffer.allocateDirect(appBufferMax + 50);278appInArray1 = new ByteBuffer [] { appIn1a, appIn1b, appIn1c };279280log("AppOut1a = " + appOut1a);281log("AppOut1a = " + appOut1b);282log("AppOut1a = " + appOut1c);283log("AppOut2 = " + appOut2);284log("");285}286287private static void runDelegatedTasks(SSLEngineResult result,288SSLEngine engine) throws Exception {289290if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {291Runnable runnable;292while ((runnable = engine.getDelegatedTask()) != null) {293log("running delegated task...");294runnable.run();295}296}297}298299private static boolean isEngineClosed(SSLEngine engine) {300return (engine.isOutboundDone() && engine.isInboundDone());301}302303private static void checkTransfer(ByteBuffer [] a, ByteBuffer b)304throws Exception {305306b.flip();307308for (int i = 0; i < a.length; i++) {309a[i].rewind();310311b.limit(b.position() + a[i].remaining());312313if (!a[i].equals(b)) {314throw new Exception("Data didn't transfer cleanly");315}316317b.position(b.limit());318}319320log("Data transferred cleanly");321}322323private static void log(String str) {324if (debug) {325System.err.println(str);326}327}328}329330331