Path: blob/master/test/jdk/sun/security/ssl/SSLEngineImpl/TLS13BeginHandshake.java
41152 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.nio.ByteBuffer;39import java.security.KeyStore;40import java.security.SecureRandom;4142public class TLS13BeginHandshake {43static String pathToStores =44System.getProperty("test.src") + "/../../../../javax/net/ssl/etc/";45static String keyStoreFile = "keystore";46static String passwd = "passphrase";4748private SSLEngine serverEngine, clientEngine;49SSLEngineResult clientResult, serverResult;50private ByteBuffer clientOut, clientIn;51private ByteBuffer serverOut, serverIn;52private ByteBuffer cTOs,sTOc;5354public static void main(String args[]) throws Exception{55new TLS13BeginHandshake().runDemo();56}5758private void runDemo() throws Exception {59int done = 0;6061createSSLEngines();62createBuffers();6364while (!isEngineClosed(clientEngine) || !isEngineClosed(serverEngine)) {6566System.out.println("================");67clientResult = clientEngine.wrap(clientOut, cTOs);68System.out.println("client wrap: " + clientResult);69runDelegatedTasks(clientResult, clientEngine);70serverResult = serverEngine.wrap(serverOut, sTOc);71System.out.println("server wrap: " + serverResult);72runDelegatedTasks(serverResult, serverEngine);7374cTOs.flip();75sTOc.flip();7677System.out.println("----");78clientResult = clientEngine.unwrap(sTOc, clientIn);79System.out.println("client unwrap: " + clientResult);80if (clientResult.getStatus() == SSLEngineResult.Status.CLOSED) {81break;82} runDelegatedTasks(clientResult, clientEngine);83serverResult = serverEngine.unwrap(cTOs, serverIn);84System.out.println("server unwrap: " + serverResult);85runDelegatedTasks(serverResult, serverEngine);8687cTOs.compact();88sTOc.compact();8990//System.err.println("so limit="+serverOut.limit()+" so pos="+serverOut.position());91//System.out.println("bf ctos limit="+cTOs.limit()+" pos="+cTOs.position()+" cap="+cTOs.capacity());92//System.out.println("bf stoc limit="+sTOc.limit()+" pos="+sTOc.position()+" cap="+sTOc.capacity());93if (done < 2 && (clientOut.limit() == serverIn.position()) &&94(serverOut.limit() == clientIn.position())) {9596if (done == 0) {97checkTransfer(serverOut, clientIn);98checkTransfer(clientOut, serverIn);99clientEngine.beginHandshake();100done++;101continue;102}103104checkTransfer(serverOut, clientIn);105checkTransfer(clientOut, serverIn);106System.out.println("\tClosing...");107clientEngine.closeOutbound();108serverEngine.closeOutbound();109done++;110continue;111}112}113}114115private static boolean isEngineClosed(SSLEngine engine) {116if (engine.isInboundDone())117System.out.println("inbound closed");118if (engine.isOutboundDone())119System.out.println("outbound closed");120return (engine.isOutboundDone() && engine.isInboundDone());121}122123private static void checkTransfer(ByteBuffer a, ByteBuffer b)124throws Exception {125a.flip();126b.flip();127128if (!a.equals(b)) {129throw new Exception("Data didn't transfer cleanly");130} else {131System.out.println("\tData transferred cleanly");132}133134a.compact();135b.compact();136137}138private void createBuffers() {139SSLSession session = clientEngine.getSession();140int appBufferMax = session.getApplicationBufferSize();141int netBufferMax = session.getPacketBufferSize();142143clientIn = ByteBuffer.allocate(appBufferMax + 50);144serverIn = ByteBuffer.allocate(appBufferMax + 50);145146cTOs = ByteBuffer.allocateDirect(netBufferMax);147sTOc = ByteBuffer.allocateDirect(netBufferMax);148149clientOut = ByteBuffer.wrap("client".getBytes());150serverOut = ByteBuffer.wrap("server".getBytes());151}152153private void createSSLEngines() throws Exception {154serverEngine = initContext().createSSLEngine();155serverEngine.setUseClientMode(false);156serverEngine.setNeedClientAuth(true);157158clientEngine = initContext().createSSLEngine("client", 80);159clientEngine.setUseClientMode(true);160}161162private SSLContext initContext() throws Exception {163SSLContext sc = SSLContext.getInstance("TLSv1.3");164KeyStore ks = KeyStore.getInstance(new File(pathToStores + keyStoreFile),165passwd.toCharArray());166KeyManagerFactory kmf =167KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());168kmf.init(ks, passwd.toCharArray());169TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());170tmf.init(ks);171sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());172return sc;173}174175private static void runDelegatedTasks(SSLEngineResult result,176SSLEngine engine) throws Exception {177178if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {179Runnable runnable;180while ((runnable = engine.getDelegatedTask()) != null) {181runnable.run();182}183HandshakeStatus hsStatus = engine.getHandshakeStatus();184if (hsStatus == HandshakeStatus.NEED_TASK) {185throw new Exception(186"handshake shouldn't need additional tasks");187}188}189}190}191192193