Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLEngineImpl/TLS13BeginHandshake.java
38853 views
/*1* Copyright (c) 2018, 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* @summary Test SSLEngine.begineHandshake() triggers a KeyUpdate handshake26* in TLSv1.327* @run main/othervm TLS13BeginHandshake28*/2930import javax.net.ssl.KeyManagerFactory;31import javax.net.ssl.SSLContext;32import javax.net.ssl.SSLEngine;33import javax.net.ssl.SSLEngineResult;34import javax.net.ssl.SSLEngineResult.HandshakeStatus;35import javax.net.ssl.SSLSession;36import javax.net.ssl.TrustManagerFactory;37import java.io.File;38import java.io.FileInputStream;39import java.nio.ByteBuffer;40import java.security.KeyStore;41import java.security.SecureRandom;4243public class TLS13BeginHandshake {44static String pathToStores =45System.getProperty("test.src") + "/../../../../javax/net/ssl/etc/";46static String keyStoreFile = "keystore";47static String passwd = "passphrase";4849private SSLEngine serverEngine, clientEngine;50SSLEngineResult clientResult, serverResult;51private ByteBuffer clientOut, clientIn;52private ByteBuffer serverOut, serverIn;53private ByteBuffer cTOs,sTOc;5455public static void main(String args[]) throws Exception{56new TLS13BeginHandshake().runDemo();57}5859private void runDemo() throws Exception {60int done = 0;6162createSSLEngines();63createBuffers();6465while (!isEngineClosed(clientEngine) || !isEngineClosed(serverEngine)) {6667System.out.println("================");68clientResult = clientEngine.wrap(clientOut, cTOs);69System.out.println("client wrap: " + clientResult);70runDelegatedTasks(clientResult, clientEngine);71serverResult = serverEngine.wrap(serverOut, sTOc);72System.out.println("server wrap: " + serverResult);73runDelegatedTasks(serverResult, serverEngine);7475cTOs.flip();76sTOc.flip();7778System.out.println("----");79clientResult = clientEngine.unwrap(sTOc, clientIn);80System.out.println("client unwrap: " + clientResult);81if (clientResult.getStatus() == SSLEngineResult.Status.CLOSED) {82break;83} runDelegatedTasks(clientResult, clientEngine);84serverResult = serverEngine.unwrap(cTOs, serverIn);85System.out.println("server unwrap: " + serverResult);86runDelegatedTasks(serverResult, serverEngine);8788cTOs.compact();89sTOc.compact();9091//System.err.println("so limit="+serverOut.limit()+" so pos="+serverOut.position());92//System.out.println("bf ctos limit="+cTOs.limit()+" pos="+cTOs.position()+" cap="+cTOs.capacity());93//System.out.println("bf stoc limit="+sTOc.limit()+" pos="+sTOc.position()+" cap="+sTOc.capacity());94if (done < 2 && (clientOut.limit() == serverIn.position()) &&95(serverOut.limit() == clientIn.position())) {9697if (done == 0) {98checkTransfer(serverOut, clientIn);99checkTransfer(clientOut, serverIn);100clientEngine.beginHandshake();101done++;102continue;103}104105checkTransfer(serverOut, clientIn);106checkTransfer(clientOut, serverIn);107System.out.println("\tClosing...");108clientEngine.closeOutbound();109serverEngine.closeOutbound();110done++;111continue;112}113}114}115116private static boolean isEngineClosed(SSLEngine engine) {117if (engine.isInboundDone())118System.out.println("inbound closed");119if (engine.isOutboundDone())120System.out.println("outbound closed");121return (engine.isOutboundDone() && engine.isInboundDone());122}123124private static void checkTransfer(ByteBuffer a, ByteBuffer b)125throws Exception {126a.flip();127b.flip();128129if (!a.equals(b)) {130throw new Exception("Data didn't transfer cleanly");131} else {132System.out.println("\tData transferred cleanly");133}134135a.compact();136b.compact();137138}139private void createBuffers() {140SSLSession session = clientEngine.getSession();141int appBufferMax = session.getApplicationBufferSize();142int netBufferMax = session.getPacketBufferSize();143144clientIn = ByteBuffer.allocate(appBufferMax + 50);145serverIn = ByteBuffer.allocate(appBufferMax + 50);146147cTOs = ByteBuffer.allocateDirect(netBufferMax);148sTOc = ByteBuffer.allocateDirect(netBufferMax);149150clientOut = ByteBuffer.wrap("client".getBytes());151serverOut = ByteBuffer.wrap("server".getBytes());152}153154private void createSSLEngines() throws Exception {155serverEngine = initContext().createSSLEngine();156serverEngine.setUseClientMode(false);157serverEngine.setNeedClientAuth(true);158159clientEngine = initContext().createSSLEngine("client", 80);160clientEngine.setUseClientMode(true);161}162163private SSLContext initContext() throws Exception {164SSLContext sc = SSLContext.getInstance("TLSv1.3");165KeyStore ks = KeyStore.getInstance("JKS");166ks.load(new FileInputStream(new File(pathToStores + keyStoreFile)),167passwd.toCharArray());168KeyManagerFactory kmf =169KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());170kmf.init(ks, passwd.toCharArray());171TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());172tmf.init(ks);173sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());174return sc;175}176177private static void runDelegatedTasks(SSLEngineResult result,178SSLEngine engine) throws Exception {179180if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {181Runnable runnable;182while ((runnable = engine.getDelegatedTask()) != null) {183runnable.run();184}185HandshakeStatus hsStatus = engine.getHandshakeStatus();186if (hsStatus == HandshakeStatus.NEED_TASK) {187throw new Exception(188"handshake shouldn't need additional tasks");189}190}191}192}193194195