Path: blob/master/test/jdk/sun/security/ssl/SSLSessionImpl/ResumeChecksServer.java
41152 views
/*1* Copyright (c) 2018, 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 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 -Djdk.tls.server.enableSessionTicketExtension=false -Djdk.tls.client.enableSessionTicketExtension=false ResumeChecksServer BASIC29* @run main/othervm -Djdk.tls.client.protocols=TLSv1.2 -Djdk.tls.server.enableSessionTicketExtension=true -Djdk.tls.client.enableSessionTicketExtension=false ResumeChecksServer BASIC30* @run main/othervm -Djdk.tls.client.protocols=TLSv1.2 -Djdk.tls.server.enableSessionTicketExtension=true -Djdk.tls.client.enableSessionTicketExtension=true ResumeChecksServer BASIC31* @run main/othervm -Djdk.tls.client.protocols=TLSv1.2 -Djdk.tls.server.enableSessionTicketExtension=false -Djdk.tls.client.enableSessionTicketExtension=false ResumeChecksServer CLIENT_AUTH32* @run main/othervm -Djdk.tls.client.protocols=TLSv1.2 -Djdk.tls.server.enableSessionTicketExtension=true -Djdk.tls.client.enableSessionTicketExtension=false ResumeChecksServer CLIENT_AUTH33* @run main/othervm -Djdk.tls.client.protocols=TLSv1.2 -Djdk.tls.server.enableSessionTicketExtension=true -Djdk.tls.client.enableSessionTicketExtension=true ResumeChecksServer CLIENT_AUTH34* @run main/othervm -Djdk.tls.server.enableSessionTicketExtension=false -Djdk.tls.client.enableSessionTicketExtension=false ResumeChecksServer VERSION_2_TO_335* @run main/othervm -Djdk.tls.server.enableSessionTicketExtension=false -Djdk.tls.client.enableSessionTicketExtension=true ResumeChecksServer VERSION_2_TO_336* @run main/othervm -Djdk.tls.server.enableSessionTicketExtension=true -Djdk.tls.client.enableSessionTicketExtension=false ResumeChecksServer VERSION_2_TO_337* @run main/othervm -Djdk.tls.server.enableSessionTicketExtension=false -Djdk.tls.client.enableSessionTicketExtension=false ResumeChecksServer VERSION_3_TO_238* @run main/othervm -Djdk.tls.server.enableSessionTicketExtension=false -Djdk.tls.client.enableSessionTicketExtension=true ResumeChecksServer VERSION_3_TO_239* @run main/othervm -Djdk.tls.server.enableSessionTicketExtension=true -Djdk.tls.client.enableSessionTicketExtension=false ResumeChecksServer VERSION_3_TO_240*41*/4243import javax.net.*;44import javax.net.ssl.*;45import java.io.*;46import java.security.*;47import java.net.*;48import java.util.*;4950public class ResumeChecksServer {5152static String pathToStores = "../../../../javax/net/ssl/etc";53static String keyStoreFile = "keystore";54static String trustStoreFile = "truststore";55static String passwd = "passphrase";5657enum TestMode {58BASIC,59CLIENT_AUTH,60VERSION_2_TO_3,61VERSION_3_TO_2,62CIPHER_SUITE,63SIGNATURE_SCHEME64}6566public static void main(String[] args) throws Exception {6768TestMode mode = TestMode.valueOf(args[0]);6970String keyFilename =71System.getProperty("test.src", "./") + "/" + pathToStores +72"/" + keyStoreFile;73String trustFilename =74System.getProperty("test.src", "./") + "/" + pathToStores +75"/" + trustStoreFile;7677System.setProperty("javax.net.ssl.keyStore", keyFilename);78System.setProperty("javax.net.ssl.keyStorePassword", passwd);79System.setProperty("javax.net.ssl.trustStore", trustFilename);80System.setProperty("javax.net.ssl.trustStorePassword", passwd);8182SSLSession secondSession = null;8384SSLContext sslContext = SSLContext.getDefault();85ServerSocketFactory fac = sslContext.getServerSocketFactory();86SSLServerSocket ssock = (SSLServerSocket)87fac.createServerSocket(0);8889Client client = startClient(ssock.getLocalPort());9091try {92connect(client, ssock, mode, false);93} catch (Exception ex) {94throw new RuntimeException(ex);95}9697long secondStartTime = System.currentTimeMillis();98Thread.sleep(10);99try {100secondSession = connect(client, ssock, mode, true);101} catch (SSLHandshakeException ex) {102// this is expected103} catch (Exception ex) {104throw new RuntimeException(ex);105}106107client.go = false;108client.signal();109110switch (mode) {111case BASIC:112// fail if session is not resumed113if (secondSession.getCreationTime() > secondStartTime) {114throw new RuntimeException("Session was not reused");115}116break;117case CLIENT_AUTH:118// throws an exception if the client is not authenticated119secondSession.getPeerCertificates();120break;121case VERSION_2_TO_3:122case VERSION_3_TO_2:123case CIPHER_SUITE:124case SIGNATURE_SCHEME:125// fail if a new session is not created126if (secondSession.getCreationTime() <= secondStartTime) {127throw new RuntimeException("Existing session was used");128}129break;130default:131throw new RuntimeException("unknown mode: " + mode);132}133}134135private static class NoSig implements AlgorithmConstraints {136137private final String alg;138139NoSig(String alg) {140this.alg = alg;141}142143144private boolean test(String a) {145return !a.toLowerCase().contains(alg.toLowerCase());146}147148public boolean permits(Set<CryptoPrimitive> primitives, Key key) {149return true;150}151public boolean permits(Set<CryptoPrimitive> primitives,152String algorithm, AlgorithmParameters parameters) {153154return test(algorithm);155}156public boolean permits(Set<CryptoPrimitive> primitives,157String algorithm, Key key, AlgorithmParameters parameters) {158159return test(algorithm);160}161}162163private static SSLSession connect(Client client, SSLServerSocket ssock,164TestMode mode, boolean second) throws Exception {165166try {167client.signal();168System.out.println("Waiting for connection");169SSLSocket sock = (SSLSocket) ssock.accept();170SSLParameters params = sock.getSSLParameters();171172switch (mode) {173case BASIC:174// do nothing to ensure resumption works175break;176case CLIENT_AUTH:177if (second) {178params.setNeedClientAuth(true);179} else {180params.setNeedClientAuth(false);181}182break;183case VERSION_2_TO_3:184if (second) {185params.setProtocols(new String[] {"TLSv1.3"});186} else {187params.setProtocols(new String[] {"TLSv1.2"});188}189break;190case VERSION_3_TO_2:191if (second) {192params.setProtocols(new String[] {"TLSv1.2"});193} else {194params.setProtocols(new String[] {"TLSv1.3"});195}196break;197case CIPHER_SUITE:198if (second) {199params.setCipherSuites(200new String[] {"TLS_AES_128_GCM_SHA256"});201} else {202params.setCipherSuites(203new String[] {"TLS_AES_256_GCM_SHA384"});204}205break;206case SIGNATURE_SCHEME:207params.setNeedClientAuth(true);208AlgorithmConstraints constraints =209params.getAlgorithmConstraints();210if (second) {211params.setAlgorithmConstraints(new NoSig("ecdsa"));212} else {213params.setAlgorithmConstraints(new NoSig("rsa"));214}215break;216default:217throw new RuntimeException("unknown mode: " + mode);218}219sock.setSSLParameters(params);220BufferedReader reader = new BufferedReader(221new InputStreamReader(sock.getInputStream()));222String line = reader.readLine();223System.out.println("server read: " + line);224PrintWriter out = new PrintWriter(225new OutputStreamWriter(sock.getOutputStream()));226out.println(line);227out.flush();228out.close();229SSLSession result = sock.getSession();230sock.close();231return result;232} catch (SSLHandshakeException ex) {233if (!second) {234throw ex;235}236}237return null;238}239240private static Client startClient(int port) {241Client client = new Client(port);242new Thread(client).start();243return client;244}245246private static class Client implements Runnable {247248public volatile boolean go = true;249private boolean signal = false;250private final int port;251252Client(int port) {253this.port = port;254}255256private synchronized void waitForSignal() {257while (!signal) {258try {259wait();260} catch (InterruptedException ex) {261// do nothing262}263}264signal = false;265266try {267Thread.sleep(1000);268} catch (InterruptedException ex) {269// do nothing270}271}272public synchronized void signal() {273signal = true;274notify();275}276277public void run() {278try {279280SSLContext sc = SSLContext.getDefault();281282waitForSignal();283while (go) {284try {285SSLSocket sock = (SSLSocket)286sc.getSocketFactory().createSocket();287sock.connect(new InetSocketAddress("localhost", port));288PrintWriter out = new PrintWriter(289new OutputStreamWriter(sock.getOutputStream()));290out.println("message");291out.flush();292BufferedReader reader = new BufferedReader(293new InputStreamReader(sock.getInputStream()));294String inMsg = reader.readLine();295System.out.println("Client received: " + inMsg);296out.close();297sock.close();298waitForSignal();299} catch (Exception ex) {300ex.printStackTrace();301}302}303} catch (Exception ex) {304throw new RuntimeException(ex);305}306}307}308}309310311