|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @library /test/lib |
| 27 | + * @library /javax/net/ssl/templates |
| 28 | + * @bug 8242008 |
| 29 | + * @summary Verifies multiple PSKs are used by JSSE |
| 30 | + * @run main/othervm MultiNSTClient -Djdk.tls.client.protocols=TLSv1.3 -Djdk.tls.server.newSessionTicketCount=1 |
| 31 | + * @run main/othervm MultiNSTClient -Djdk.tls.client.protocols=TLSv1.3 -Djdk.tls.server.newSessionTicketCount=3 |
| 32 | + * @run main/othervm MultiNSTClient -Djdk.tls.client.protocols=TLSv1.3 -Djdk.tls.server.newSessionTicketCount=10 |
| 33 | + * @run main/othervm MultiNSTClient -Djdk.tls.client.protocols=TLSv1.3 -Djdk.tls.server.enableSessionTicketExtension=true -Djdk.tls.client.enableSessionTicketExtension=true |
| 34 | + * @run main/othervm MultiNSTClient -Djdk.tls.client.protocols=TLSv1.3 -Djdk.tls.server.enableSessionTicketExtension=false -Djdk.tls.client.enableSessionTicketExtension=true |
| 35 | + * @run main/othervm MultiNSTClient -Djdk.tls.client.protocols=TLSv1.3 -Djdk.tls.server.enableSessionTicketExtension=true -Djdk.tls.client.enableSessionTicketExtension=false |
| 36 | + * @run main/othervm MultiNSTClient -Djdk.tls.client.protocols=TLSv1.3 -Djdk.tls.server.enableSessionTicketExtension=false -Djdk.tls.client.enableSessionTicketExtension=false |
| 37 | + * @run main/othervm MultiNSTClient -Djdk.tls.client.protocols=TLSv1.2 -Djdk.tls.server.enableSessionTicketExtension=true -Djdk.tls.client.enableSessionTicketExtension=true |
| 38 | + */ |
| 39 | + |
| 40 | +import jdk.test.lib.Utils; |
| 41 | +import jdk.test.lib.process.OutputAnalyzer; |
| 42 | +import jdk.test.lib.process.ProcessTools; |
| 43 | + |
| 44 | +import javax.net.ssl.SSLSession; |
| 45 | +import java.util.Arrays; |
| 46 | +import java.util.HexFormat; |
| 47 | +import java.util.List; |
| 48 | + |
| 49 | +/** |
| 50 | + * This test verifies that multiple NSTs and PSKs are sent by a JSSE server. |
| 51 | + * Then JSSE client is able to store them all and resume the connection. It |
| 52 | + * requires specific text in the TLS debugging to verify the success. |
| 53 | + */ |
| 54 | + |
| 55 | +public class MultiNSTClient { |
| 56 | + |
| 57 | + static HexFormat hex = HexFormat.of(); |
| 58 | + |
| 59 | + public static void main(String[] args) throws Exception { |
| 60 | + |
| 61 | + if (!args[0].equalsIgnoreCase("p")) { |
| 62 | + StringBuilder sb = new StringBuilder(); |
| 63 | + Arrays.stream(args).forEach(a -> { |
| 64 | + sb.append(a); |
| 65 | + sb.append(" "); |
| 66 | + }); |
| 67 | + String params = sb.toString(); |
| 68 | + System.setProperty("test.java.opts", |
| 69 | + "-Dtest.src=" + System.getProperty("test.src") + |
| 70 | + " -Dtest.jdk=" + System.getProperty("test.jdk") + |
| 71 | + " -Dtest.root=" + System.getProperty("test.root") + |
| 72 | + " -Djavax.net.debug=ssl,handshake " + params |
| 73 | + ); |
| 74 | + |
| 75 | + boolean TLS13 = args[0].contains("1.3"); |
| 76 | + |
| 77 | + System.out.println("test.java.opts: " + |
| 78 | + System.getProperty("test.java.opts")); |
| 79 | + |
| 80 | + ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder( |
| 81 | + Utils.addTestJavaOpts("MultiNSTClient", "p")); |
| 82 | + |
| 83 | + OutputAnalyzer output = ProcessTools.executeProcess(pb); |
| 84 | + System.out.println("I'm here"); |
| 85 | + boolean pass = true; |
| 86 | + try { |
| 87 | + List<String> list = output.stderrShouldContain("MultiNST PSK"). |
| 88 | + asLines().stream().filter(s -> |
| 89 | + s.contains("MultiNST PSK")).toList(); |
| 90 | + List<String> serverPSK = list.stream().filter(s -> |
| 91 | + s.contains("MultiNST PSK (Server)")).toList(); |
| 92 | + List<String> clientPSK = list.stream().filter(s -> |
| 93 | + s.contains("MultiNST PSK (Client)")).toList(); |
| 94 | + System.out.println("found list: " + list.size()); |
| 95 | + System.out.println("found server: " + serverPSK.size()); |
| 96 | + serverPSK.stream().forEach(s -> System.out.println("\t" + s)); |
| 97 | + System.out.println("found client: " + clientPSK.size()); |
| 98 | + clientPSK.stream().forEach(s -> System.out.println("\t" + s)); |
| 99 | + for (int i = 0; i < 2; i++) { |
| 100 | + String svr = serverPSK.getFirst(); |
| 101 | + String cli = clientPSK.getFirst(); |
| 102 | + if (svr.regionMatches(svr.length() - 16, cli, cli.length() - 16, 16)) { |
| 103 | + System.out.println("entry " + (i + 1) + " match."); |
| 104 | + } else { |
| 105 | + System.out.println("entry " + (i + 1) + " server and client PSK didn't match:"); |
| 106 | + System.out.println(" server: " + svr); |
| 107 | + System.out.println(" client: " + cli); |
| 108 | + pass = false; |
| 109 | + } |
| 110 | + } |
| 111 | + } catch (RuntimeException e) { |
| 112 | + System.out.println("No MultiNST PSK found."); |
| 113 | + pass = false; |
| 114 | + } |
| 115 | + |
| 116 | + if (TLS13) { |
| 117 | + if (!pass) { |
| 118 | + throw new Exception("Test failed: " + params); |
| 119 | + } |
| 120 | + } else { |
| 121 | + if (pass) { |
| 122 | + throw new Exception("Test failed: " + params); |
| 123 | + } |
| 124 | + } |
| 125 | + System.out.println("Test Passed"); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + TLSBase.Server server = new TLSBase.Server(); |
| 130 | + |
| 131 | + System.out.println("------ Start connection"); |
| 132 | + TLSBase.Client initial = new TLSBase.Client(); |
| 133 | + SSLSession initialSession = initial.connect().getSession(); |
| 134 | + System.out.println("id = " + hex.formatHex(initialSession.getId())); |
| 135 | + System.out.println("session = " + initialSession); |
| 136 | + |
| 137 | + System.out.println("------ getNewSession from original client"); |
| 138 | + TLSBase.Client resumClient = new TLSBase.Client(initial); |
| 139 | + SSLSession resumption = resumClient.connect().getSession(); |
| 140 | + System.out.println("id = " + hex.formatHex(resumption.getId())); |
| 141 | + System.out.println("session = " + resumption); |
| 142 | + if (!initialSession.toString().equalsIgnoreCase(resumption.toString())) { |
| 143 | + throw new Exception("Resumed session did not match"); |
| 144 | + } |
| 145 | + |
| 146 | + System.out.println("------ Second getNewSession from original client"); |
| 147 | + TLSBase.Client resumClient2 = new TLSBase.Client(initial); |
| 148 | + resumption = resumClient2.connect().getSession(); |
| 149 | + System.out.println("id = " + hex.formatHex(resumption.getId())); |
| 150 | + System.out.println("session = " + resumption); |
| 151 | + if (!initialSession.toString().equalsIgnoreCase(resumption.toString())) { |
| 152 | + throw new Exception("Resumed session did not match"); |
| 153 | + } |
| 154 | + |
| 155 | + System.out.println("------ New client connection"); |
| 156 | + TLSBase.Client newConnection = new TLSBase.Client(); |
| 157 | + SSLSession newSession = newConnection.connect().getSession(); |
| 158 | + System.out.println("id = " + hex.formatHex(newSession.getId())); |
| 159 | + System.out.println("session = " + newSession); |
| 160 | + if (initialSession.toString().equalsIgnoreCase(newSession.toString())) { |
| 161 | + throw new Exception("new session is the same as the initial."); |
| 162 | + } |
| 163 | + |
| 164 | + System.out.println("------ Closing connections"); |
| 165 | + initial.close(); |
| 166 | + resumClient.close(); |
| 167 | + resumClient2.close(); |
| 168 | + newConnection.close(); |
| 169 | + server.close(); |
| 170 | + System.out.println("------ End"); |
| 171 | + System.exit(0); |
| 172 | + } |
| 173 | +} |
0 commit comments