Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/sun/security/ssl/SSLSessionImpl/InvalidateSession.java
66645 views
1
/*
2
* Copyright (c) 2021, 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
* @bug 8270344
27
* @library /test/lib /javax/net/ssl/templates
28
* @summary Session resumption errors
29
* @run main/othervm InvalidateSession
30
*/
31
32
import javax.net.*;
33
import javax.net.ssl.*;
34
import java.io.*;
35
import java.net.*;
36
import java.util.*;
37
38
import jdk.test.lib.security.SecurityUtils;
39
40
public class InvalidateSession implements SSLContextTemplate {
41
42
static ServerSocketFactory serverSsf = null;
43
static SSLSocketFactory clientSsf = null;
44
45
static Server server;
46
static SSLSession cacheSession;
47
static final String[] CLIENT_VERSIONS = {"TLSv1", "TLSv1.1", "TLSv1.2"};
48
49
public static void main(String args[]) throws Exception {
50
// drop the supported_versions extension to force test to use the legacy
51
// TLS protocol version field during handshakes
52
System.setProperty("jdk.tls.client.disableExtensions", "supported_versions");
53
SecurityUtils.removeFromDisabledTlsAlgs("TLSv1", "TLSv1.1");
54
55
InvalidateSession test = new InvalidateSession();
56
test.sessionTest();
57
server.go = false;
58
}
59
60
/**
61
* 3 test iterations
62
* 1) Server configured with TLSv1, client with TLSv1, v1.1, v1.2
63
* - Handshake should succeed
64
* - Session "A" established
65
* 2) 2nd iteration, server configured with TLSv1.2 only
66
* - Connection should succeed but with a new session due to TLS protocol version change
67
* - Session "A" should be invalidated
68
* - Session "B" is created
69
* 3) 3rd iteration, same server/client config
70
* - Session "B" should continue to be in use
71
*/
72
private void sessionTest() throws Exception {
73
serverSsf = createServerSSLContext().getServerSocketFactory();
74
clientSsf = createClientSSLContext().getSocketFactory();
75
server = startServer();
76
while (!server.started) {
77
Thread.yield();
78
}
79
80
for (int i = 1; i <= 3; i++) {
81
clientConnect(i);
82
Thread.sleep(1000);
83
}
84
}
85
86
public void clientConnect(int testIterationCount) throws Exception {
87
System.out.printf("Connecting to: localhost: %s, iteration count %d%n",
88
"localhost:" + server.port, testIterationCount);
89
SSLSocket sslSocket = (SSLSocket) clientSsf.createSocket("localhost", server.port);
90
sslSocket.setEnabledProtocols(CLIENT_VERSIONS);
91
sslSocket.startHandshake();
92
93
System.out.println("Got session: " + sslSocket.getSession());
94
95
if (testIterationCount == 2 && Objects.equals(cacheSession, sslSocket.getSession())) {
96
throw new RuntimeException("Same session should not have resumed");
97
}
98
if (testIterationCount == 3 && !Objects.equals(cacheSession, sslSocket.getSession())) {
99
throw new RuntimeException("Same session should have resumed");
100
}
101
102
cacheSession = sslSocket.getSession();
103
104
try (
105
ObjectOutputStream oos = new ObjectOutputStream(sslSocket.getOutputStream());
106
ObjectInputStream ois = new ObjectInputStream(sslSocket.getInputStream())) {
107
oos.writeObject("Hello");
108
String serverMsg = (String) ois.readObject();
109
System.out.println("Server message : " + serverMsg);
110
} catch (Exception ex) {
111
throw new RuntimeException(ex);
112
} finally {
113
sslSocket.close();
114
}
115
}
116
117
private static Server startServer() {
118
Server server = new Server();
119
new Thread(server).start();
120
return server;
121
}
122
123
private static class Server implements Runnable {
124
public volatile boolean go = true;
125
public volatile int port = 0;
126
public volatile boolean started = false;
127
128
@Override
129
public void run() {
130
try {
131
SSLServerSocket ssock = (SSLServerSocket)
132
serverSsf.createServerSocket(0);
133
this.port = ssock.getLocalPort();
134
ssock.setEnabledProtocols(new String[]{"TLSv1"});
135
started = true;
136
while (go) {
137
try {
138
System.out.println("Waiting for connection");
139
Socket sock = ssock.accept();
140
// now flip server to TLSv1.2 mode for successive connections
141
ssock.setEnabledProtocols(new String[]{"TLSv1.2"});
142
try (
143
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
144
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream())) {
145
String recv = (String) ois.readObject();
146
oos.writeObject("Received: " + recv);
147
} catch (SSLHandshakeException she) {
148
System.out.println("Server caught :" + she);
149
} finally {
150
sock.close();
151
}
152
} catch (Exception ex) {
153
throw new RuntimeException(ex);
154
}
155
}
156
} catch (Exception ex) {
157
throw new RuntimeException(ex);
158
}
159
}
160
}
161
}
162
163
164