Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLSessionImpl/ResumeChecksServer.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* @bug 820692926* @summary ensure that server only resumes a session if certain properties27* of the session are compatible with the new connection28* @run main/othervm -Djdk.tls.client.protocols=TLSv1.2 ResumeChecksServer BASIC29* @run main/othervm -Djdk.tls.client.protocols=TLSv1.3 ResumeChecksServer BASIC30* @run main/othervm ResumeChecksServer BASIC31* @run main/othervm -Djdk.tls.client.protocols=TLSv1.2 ResumeChecksServer CLIENT_AUTH32* @run main/othervm -Djdk.tls.client.protocols=TLSv1.3 ResumeChecksServer CLIENT_AUTH33* @run main/othervm ResumeChecksServer CLIENT_AUTH34* @run main/othervm -Djdk.tls.client.protocols=TLSv1.2,TLSv1.3 ResumeChecksServer VERSION_2_TO_335* @run main/othervm -Djdk.tls.client.protocols=TLSv1.2,TLSv1.3 ResumeChecksServer VERSION_3_TO_236* @run main/othervm -Djdk.tls.client.protocols=TLSv1.3 ResumeChecksServer CIPHER_SUITE37* @run main/othervm -Djdk.tls.client.protocols=TLSv1.3 ResumeChecksServer SIGNATURE_SCHEME38*39*/4041import javax.net.*;42import javax.net.ssl.*;43import java.io.*;44import java.security.*;45import java.net.*;46import java.util.*;4748public class ResumeChecksServer {4950static String pathToStores = "../../../../javax/net/ssl/etc";51static String keyStoreFile = "keystore";52static String trustStoreFile = "truststore";53static String passwd = "passphrase";5455enum TestMode {56BASIC,57CLIENT_AUTH,58VERSION_2_TO_3,59VERSION_3_TO_2,60CIPHER_SUITE,61SIGNATURE_SCHEME62}6364public static void main(String[] args) throws Exception {6566TestMode mode = TestMode.valueOf(args[0]);6768String keyFilename =69System.getProperty("test.src", "./") + "/" + pathToStores +70"/" + keyStoreFile;71String trustFilename =72System.getProperty("test.src", "./") + "/" + pathToStores +73"/" + trustStoreFile;7475System.setProperty("javax.net.ssl.keyStore", keyFilename);76System.setProperty("javax.net.ssl.keyStorePassword", passwd);77System.setProperty("javax.net.ssl.trustStore", trustFilename);78System.setProperty("javax.net.ssl.trustStorePassword", passwd);7980SSLSession secondSession = null;8182SSLContext sslContext = SSLContext.getDefault();83ServerSocketFactory fac = sslContext.getServerSocketFactory();84SSLServerSocket ssock = (SSLServerSocket)85fac.createServerSocket(0);8687Client client = startClient(ssock.getLocalPort());8889try {90connect(client, ssock, mode, false);91} catch (Exception ex) {92throw new RuntimeException(ex);93}9495long secondStartTime = System.currentTimeMillis();96Thread.sleep(10);97try {98secondSession = connect(client, ssock, mode, true);99} catch (SSLHandshakeException ex) {100// this is expected101} catch (Exception ex) {102throw new RuntimeException(ex);103}104105client.go = false;106client.signal();107108switch (mode) {109case BASIC:110// fail if session is not resumed111if (secondSession.getCreationTime() > secondStartTime) {112throw new RuntimeException("Session was not reused");113}114break;115case CLIENT_AUTH:116// throws an exception if the client is not authenticated117secondSession.getPeerCertificates();118break;119case VERSION_2_TO_3:120case VERSION_3_TO_2:121case CIPHER_SUITE:122case SIGNATURE_SCHEME:123// fail if a new session is not created124if (secondSession.getCreationTime() <= secondStartTime) {125throw new RuntimeException("Existing session was used");126}127break;128default:129throw new RuntimeException("unknown mode: " + mode);130}131}132133private static class NoSig implements AlgorithmConstraints {134135private final String alg;136137NoSig(String alg) {138this.alg = alg;139}140141142private boolean test(String a) {143return !a.toLowerCase().contains(alg.toLowerCase());144}145146public boolean permits(Set<CryptoPrimitive> primitives, Key key) {147return true;148}149public boolean permits(Set<CryptoPrimitive> primitives,150String algorithm, AlgorithmParameters parameters) {151152return test(algorithm);153}154public boolean permits(Set<CryptoPrimitive> primitives,155String algorithm, Key key, AlgorithmParameters parameters) {156157return test(algorithm);158}159}160161private static SSLSession connect(Client client, SSLServerSocket ssock,162TestMode mode, boolean second) throws Exception {163164try {165client.signal();166System.out.println("Waiting for connection");167SSLSocket sock = (SSLSocket) ssock.accept();168SSLParameters params = sock.getSSLParameters();169170switch (mode) {171case BASIC:172// do nothing to ensure resumption works173break;174case CLIENT_AUTH:175if (second) {176params.setNeedClientAuth(true);177} else {178params.setNeedClientAuth(false);179}180break;181case VERSION_2_TO_3:182if (second) {183params.setProtocols(new String[] {"TLSv1.3"});184} else {185params.setProtocols(new String[] {"TLSv1.2"});186}187break;188case VERSION_3_TO_2:189if (second) {190params.setProtocols(new String[] {"TLSv1.2"});191} else {192params.setProtocols(new String[] {"TLSv1.3"});193}194break;195case CIPHER_SUITE:196if (second) {197params.setCipherSuites(198new String[] {"TLS_AES_128_GCM_SHA256"});199} else {200params.setCipherSuites(201new String[] {"TLS_AES_256_GCM_SHA384"});202}203break;204case SIGNATURE_SCHEME:205params.setNeedClientAuth(true);206AlgorithmConstraints constraints =207params.getAlgorithmConstraints();208if (second) {209params.setAlgorithmConstraints(new NoSig("ecdsa"));210} else {211params.setAlgorithmConstraints(new NoSig("rsa"));212}213break;214default:215throw new RuntimeException("unknown mode: " + mode);216}217sock.setSSLParameters(params);218BufferedReader reader = new BufferedReader(219new InputStreamReader(sock.getInputStream()));220String line = reader.readLine();221System.out.println("server read: " + line);222PrintWriter out = new PrintWriter(223new OutputStreamWriter(sock.getOutputStream()));224out.println(line);225out.flush();226out.close();227SSLSession result = sock.getSession();228sock.close();229return result;230} catch (SSLHandshakeException ex) {231if (!second) {232throw ex;233}234}235return null;236}237238private static Client startClient(int port) {239Client client = new Client(port);240new Thread(client).start();241return client;242}243244private static class Client implements Runnable {245246public volatile boolean go = true;247private boolean signal = false;248private final int port;249250Client(int port) {251this.port = port;252}253254private synchronized void waitForSignal() {255while (!signal) {256try {257wait();258} catch (InterruptedException ex) {259// do nothing260}261}262signal = false;263264try {265Thread.sleep(1000);266} catch (InterruptedException ex) {267// do nothing268}269}270public synchronized void signal() {271signal = true;272notify();273}274275public void run() {276try {277278SSLContext sc = SSLContext.getDefault();279280waitForSignal();281while (go) {282try {283SSLSocket sock = (SSLSocket)284sc.getSocketFactory().createSocket();285sock.connect(new InetSocketAddress("localhost", port));286PrintWriter out = new PrintWriter(287new OutputStreamWriter(sock.getOutputStream()));288out.println("message");289out.flush();290BufferedReader reader = new BufferedReader(291new InputStreamReader(sock.getInputStream()));292String inMsg = reader.readLine();293System.out.println("Client received: " + inMsg);294out.close();295sock.close();296waitForSignal();297} catch (Exception ex) {298ex.printStackTrace();299}300}301} catch (Exception ex) {302throw new RuntimeException(ex);303}304}305}306}307308309