Path: blob/master/test/jdk/javax/net/ssl/SSLSession/SSLCtxAccessToSessCtx.java
41152 views
/*1* Copyright (c) 2001, 2019, 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 447321026* @summary SSLSessionContext should be accessible from SSLContext27* @run main/othervm -Djdk.tls.server.enableSessionTicketExtension=false28* SSLCtxAccessToSessCtx29*30*31* SunJSSE does not support dynamic system properties, no way to re-use32* system properties in samevm/agentvm mode.33*/3435import java.io.*;36import java.net.*;37import javax.net.ssl.*;38import java.util.*;39import java.util.concurrent.atomic.AtomicInteger;40import java.security.KeyStore;4142public class SSLCtxAccessToSessCtx {4344/*45* =============================================================46* Set the various variables needed for the tests, then47* specify what tests to run on each side.48*/4950/*51* Should we run the client or server in a separate thread?52* Both sides can throw exceptions, but do you have a preference53* as to which side should be the main thread.54*/55static boolean separateServerThread = true;5657/*58* Where do we find the keystores?59*/60static String pathToStores = "../etc";61static String keyStoreFile = "keystore";62static String trustStoreFile = "truststore";63static String passwd = "passphrase";6465/*66* Is the server ready to serve?67*/68AtomicInteger serverReady = new AtomicInteger(1); // only one port now6970/*71* Turn on SSL debugging?72*/73static boolean debug = false;7475/*76* If the client or server is doing some kind of object creation77* that the other side depends on, and that thread prematurely78* exits, you may experience a hang. The test harness will79* terminate all hung threads after its timeout has expired,80* currently 3 minutes by default, but you might try to be81* smart about it....82*/8384/*85* Define the server side of the test.86*87* If the server prematurely exits, serverReady will be set to true88* to avoid infinite hangs.89*/90void doServerSide(int serverPort) throws Exception {9192SSLServerSocket sslServerSocket =93(SSLServerSocket) sslssf.createServerSocket(serverPort);94int slot = createdPorts.getAndIncrement();95serverPorts[slot] = sslServerSocket.getLocalPort();9697/*98* Signal Client, we're ready for his connect.99*/100serverReady.getAndDecrement();101int read = 0;102SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();103InputStream sslIS = sslSocket.getInputStream();104OutputStream sslOS = sslSocket.getOutputStream();105read = sslIS.read();106SSLSessionContext sslctxCache = sslctx.getServerSessionContext();107SSLSessionContext sessCache = sslSocket.getSession().108getSessionContext();109if (sessCache != sslctxCache)110throw new Exception("Test failed, session_cache != sslctx_cache");111sslOS.write(85);112sslOS.flush();113sslSocket.close();114}115116/*117* Define the client side of the test.118*119* If the server prematurely exits, serverReady will be set to true120* to avoid infinite hangs.121*/122void doClientSide() throws Exception {123124/*125* Wait for server to get started.126*/127while (serverReady.get() > 0) {128Thread.sleep(50);129}130/*131* first connection to serverPorts[0] -- a new session, session11132* gets created, and is cached.133*/134SSLSocket sslSocket;135sslSocket = (SSLSocket) sslsf.136createSocket("localhost", serverPorts[0]);137InputStream sslIS = sslSocket.getInputStream();138OutputStream sslOS = sslSocket.getOutputStream();139sslOS.write(237);140sslOS.flush();141142SSLSession sess = sslSocket.getSession();143SSLSessionContext sessCache = sess.getSessionContext();144SSLSessionContext sslctxCache = sslctx.getClientSessionContext();145if (sessCache != sslctxCache)146throw new Exception("Test failed, session_cache != sslctx_cache");147148int read = sslIS.read();149sslSocket.close();150}151152/*153* =============================================================154* The remainder is just support stuff155*/156157int serverPorts[] = new int[]{0}; // only one port at present158AtomicInteger createdPorts = new AtomicInteger(0);159static SSLServerSocketFactory sslssf;160static SSLSocketFactory sslsf;161static SSLContext sslctx;162163volatile Exception serverException = null;164volatile Exception clientException = null;165166public static void main(String[] args) throws Exception {167String keyFilename =168System.getProperty("test.src", "./") + "/" + pathToStores +169"/" + keyStoreFile;170String trustFilename =171System.getProperty("test.src", "./") + "/" + pathToStores +172"/" + trustStoreFile;173174System.setProperty("javax.net.ssl.keyStore", keyFilename);175System.setProperty("javax.net.ssl.keyStorePassword", passwd);176System.setProperty("javax.net.ssl.trustStore", trustFilename);177System.setProperty("javax.net.ssl.trustStorePassword", passwd);178179sslctx = SSLContext.getInstance("TLS");180KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");181KeyStore ks = KeyStore.getInstance("JKS");182ks.load(new FileInputStream(keyFilename), passwd.toCharArray());183kmf.init(ks, passwd.toCharArray());184sslctx.init(kmf.getKeyManagers(), null, null);185186sslssf = (SSLServerSocketFactory) sslctx.getServerSocketFactory();187sslsf = (SSLSocketFactory) sslctx.getSocketFactory();188189if (debug)190System.setProperty("javax.net.debug", "all");191192/*193* Start the tests.194*/195new SSLCtxAccessToSessCtx();196}197198Thread clientThread = null;199Thread serverThread = null;200201/*202* Primary constructor, used to drive remainder of the test.203*204* Fork off the other side, then do your work.205*/206SSLCtxAccessToSessCtx() throws Exception {207208/*209* create the SSLServerSocket and SSLSocket factories210*/211if (separateServerThread) {212for (int i = 0; i < serverPorts.length; i++) {213startServer(serverPorts[i], true);214}215startClient(false);216} else {217startClient(true);218for (int i = 0; i < serverPorts.length; i++) {219startServer(serverPorts[i], false);220}221}222223/*224* Wait for other side to close down.225*/226if (separateServerThread) {227serverThread.join();228} else {229clientThread.join();230}231232/*233* When we get here, the test is pretty much over.234*235* If the main thread excepted, that propagates back236* immediately. If the other thread threw an exception, we237* should report back.238*/239if (serverException != null)240throw serverException;241if (clientException != null)242throw clientException;243System.out.println("The Session context tests passed");244}245246void startServer(final int port,247boolean newThread) throws Exception {248if (newThread) {249serverThread = new Thread() {250public void run() {251try {252doServerSide(port);253} catch (Exception e) {254/*255* Our server thread just died.256*257* Release the client, if not active already...258*/259System.err.println("Server died...");260e.printStackTrace();261serverReady.set(0);262serverException = e;263}264}265};266serverThread.start();267} else {268try {269doServerSide(port);270} catch (Exception e) {271serverException = e;272} finally {273serverReady.set(0);274}275}276}277278void startClient(boolean newThread)279throws Exception {280if (newThread) {281clientThread = new Thread() {282public void run() {283try {284doClientSide();285} catch (Exception e) {286/*287* Our client thread just died.288*/289System.err.println("Client died...");290clientException = e;291}292}293};294clientThread.start();295} else {296try {297doClientSide();298} catch (Exception e) {299clientException = e;300}301}302}303}304305306