Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/javax/net/ssl/SSLSession/TestEnabledProtocols.java
66646 views
1
/*
2
* Copyright (c) 2001, 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
// SunJSSE does not support dynamic system properties, no way to re-use
25
// system properties in samevm/agentvm mode.
26
27
/*
28
* @test
29
* @bug 4416068 4478803 4479736
30
* @summary 4273544 JSSE request for function forceV3ClientHello()
31
* 4479736 setEnabledProtocols API does not work correctly
32
* 4478803 Need APIs to determine the protocol versions used in an SSL
33
* session
34
* 4701722 protocol mismatch exceptions should be consistent between
35
* SSLv3 and TLSv1
36
* @library /javax/net/ssl/templates
37
* @run main/othervm TestEnabledProtocols
38
* @author Ram Marti
39
*/
40
41
import java.io.IOException;
42
import java.io.InputStream;
43
import java.io.InterruptedIOException;
44
import java.io.OutputStream;
45
import java.net.InetAddress;
46
import java.net.SocketException;
47
import java.security.Security;
48
import java.util.Arrays;
49
50
import javax.net.ssl.SSLException;
51
import javax.net.ssl.SSLHandshakeException;
52
import javax.net.ssl.SSLServerSocket;
53
import javax.net.ssl.SSLSocket;
54
55
public class TestEnabledProtocols extends SSLSocketTemplate {
56
57
private final String[] serverProtocols;
58
private final String[] clientProtocols;
59
private final boolean exceptionExpected;
60
private final String selectedProtocol;
61
62
public TestEnabledProtocols(String[] serverProtocols,
63
String[] clientProtocols, boolean exceptionExpected,
64
String selectedProtocol) {
65
this.serverProtocols = serverProtocols;
66
this.clientProtocols = clientProtocols;
67
this.exceptionExpected = exceptionExpected;
68
this.selectedProtocol = selectedProtocol;
69
this.serverAddress = InetAddress.getLoopbackAddress();
70
}
71
72
@Override
73
protected void configureServerSocket(SSLServerSocket sslServerSocket) {
74
sslServerSocket.setEnabledProtocols(serverProtocols);
75
}
76
77
@Override
78
protected void runServerApplication(SSLSocket socket) throws Exception {
79
try {
80
socket.startHandshake();
81
82
InputStream in = socket.getInputStream();
83
OutputStream out = socket.getOutputStream();
84
out.write(280);
85
in.read();
86
} catch (SSLHandshakeException se) {
87
// ignore it; this is part of the testing
88
// log it for debugging
89
System.out.println("Server SSLHandshakeException:");
90
se.printStackTrace(System.out);
91
} catch (InterruptedIOException ioe) {
92
// must have been interrupted, no harm
93
} catch (SSLException | SocketException se) {
94
// The client side may have closed the socket.
95
System.out.println("Server SSLException:");
96
se.printStackTrace(System.out);
97
} catch (Exception e) {
98
System.out.println("Server exception:");
99
e.printStackTrace(System.out);
100
throw new RuntimeException(e);
101
}
102
}
103
104
@Override
105
protected void runClientApplication(SSLSocket sslSocket) throws Exception {
106
try {
107
System.out.println("=== Starting new test run ===");
108
showProtocols("server", serverProtocols);
109
showProtocols("client", clientProtocols);
110
111
sslSocket.setEnabledProtocols(clientProtocols);
112
sslSocket.startHandshake();
113
114
String protocolName = sslSocket.getSession().getProtocol();
115
System.out.println("Protocol name after getSession is " +
116
protocolName);
117
118
if (protocolName.equals(selectedProtocol)) {
119
System.out.println("** Success **");
120
} else {
121
System.out.println("** FAILURE ** ");
122
throw new RuntimeException
123
("expected protocol " + selectedProtocol +
124
" but using " + protocolName);
125
}
126
127
InputStream in = sslSocket.getInputStream();
128
OutputStream out = sslSocket.getOutputStream();
129
in.read();
130
out.write(280);
131
} catch (SSLHandshakeException e) {
132
if (!exceptionExpected) {
133
failTest(e, "Client got UNEXPECTED SSLHandshakeException:");
134
} else {
135
System.out.println(
136
"Client got expected SSLHandshakeException:");
137
e.printStackTrace(System.out);
138
System.out.println("** Success **");
139
}
140
} catch (SSLException | SocketException se) {
141
// The server side may have closed the socket.
142
if (isConnectionReset(se)) {
143
System.out.println("Client SocketException:");
144
se.printStackTrace(System.out);
145
} else {
146
failTest(se, "Client got UNEXPECTED Exception:");
147
}
148
149
} catch (Exception e) {
150
failTest(e, "Client got UNEXPECTED Exception:");
151
}
152
}
153
154
private boolean isConnectionReset(IOException ioe) {
155
Throwable cause = ioe instanceof SSLException se ? se.getCause() : ioe;
156
return cause instanceof SocketException
157
&& "Connection reset".equals(cause.getMessage());
158
}
159
160
private void failTest(Exception e, String message) {
161
System.out.println(message);
162
e.printStackTrace(System.out);
163
System.out.println("** FAILURE **");
164
throw new RuntimeException(e);
165
}
166
167
public static void main(String[] args) throws Exception {
168
Security.setProperty("jdk.tls.disabledAlgorithms", "");
169
170
runCase(new String[] { "TLSv1" },
171
new String[] { "TLSv1" },
172
false, "TLSv1");
173
runCase(new String[] { "TLSv1" },
174
new String[] { "TLSv1", "SSLv2Hello" },
175
true, null);
176
runCase(new String[] { "TLSv1" },
177
new String[] { "TLSv1", "SSLv3" },
178
false, "TLSv1");
179
runCase(new String[] { "TLSv1" },
180
new String[] { "SSLv3", "SSLv2Hello" },
181
true, null);
182
runCase(new String[] { "TLSv1" },
183
new String[] { "SSLv3" },
184
true, null);
185
runCase(new String[] { "TLSv1" },
186
new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },
187
true, null);
188
189
runCase(new String[] { "TLSv1", "SSLv2Hello" },
190
new String[] { "TLSv1" },
191
false, "TLSv1");
192
runCase(new String[] { "TLSv1", "SSLv2Hello" },
193
new String[] { "TLSv1", "SSLv2Hello" },
194
false, "TLSv1");
195
runCase(new String[] { "TLSv1", "SSLv2Hello" },
196
new String[] { "TLSv1", "SSLv3" },
197
false, "TLSv1");
198
runCase(new String[] { "TLSv1", "SSLv2Hello" },
199
new String[] { "SSLv3", "SSLv2Hello" },
200
true, null);
201
runCase(new String[] { "TLSv1", "SSLv2Hello" },
202
new String[] { "SSLv3" },
203
true, null);
204
runCase(new String[] { "TLSv1", "SSLv2Hello" },
205
new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },
206
false, "TLSv1");
207
208
runCase(new String[] { "TLSv1", "SSLv3" },
209
new String[] { "TLSv1" },
210
false, "TLSv1");
211
runCase(new String[] { "TLSv1", "SSLv3" },
212
new String[] { "TLSv1", "SSLv2Hello" },
213
true, null);
214
runCase(new String[] { "TLSv1", "SSLv3" },
215
new String[] { "TLSv1", "SSLv3" },
216
false, "TLSv1");
217
runCase(new String[] { "TLSv1", "SSLv3" },
218
new String[] { "SSLv3", "SSLv2Hello" },
219
true, null);
220
runCase(new String[] { "TLSv1", "SSLv3" },
221
new String[] { "SSLv3" },
222
false, "SSLv3");
223
runCase(new String[] { "TLSv1", "SSLv3" },
224
new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },
225
true, null);
226
227
runCase(new String[] { "SSLv3", "SSLv2Hello" },
228
new String[] { "TLSv1" },
229
true, null);
230
runCase(new String[] { "SSLv3", "SSLv2Hello" },
231
new String[] { "TLSv1", "SSLv2Hello" },
232
true, null);
233
runCase(new String[] { "SSLv3", "SSLv2Hello" },
234
new String[] { "TLSv1", "SSLv3" },
235
false, "SSLv3");
236
runCase(new String[] { "SSLv3", "SSLv2Hello" },
237
new String[] { "SSLv3", "SSLv2Hello" },
238
false, "SSLv3");
239
runCase(new String[] { "SSLv3", "SSLv2Hello" },
240
new String[] { "SSLv3" },
241
false, "SSLv3");
242
runCase(new String[] { "SSLv3", "SSLv2Hello" },
243
new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },
244
false, "SSLv3");
245
246
runCase(new String[] { "SSLv3" },
247
new String[] { "TLSv1" },
248
true, null);
249
runCase(new String[] { "SSLv3" },
250
new String[] { "TLSv1", "SSLv2Hello" },
251
true, null);
252
runCase(new String[] { "SSLv3" },
253
new String[] { "TLSv1", "SSLv3" },
254
false, "SSLv3");
255
runCase(new String[] { "SSLv3" },
256
new String[] { "SSLv3", "SSLv2Hello" },
257
true, null);
258
runCase(new String[] { "SSLv3" },
259
new String[] { "SSLv3" },
260
false, "SSLv3");
261
runCase(new String[] { "SSLv3" },
262
new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },
263
true, null);
264
265
runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },
266
new String[] { "TLSv1" },
267
false, "TLSv1");
268
runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },
269
new String[] { "TLSv1", "SSLv2Hello" },
270
false, "TLSv1");
271
runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },
272
new String[] { "TLSv1", "SSLv3" },
273
false, "TLSv1");
274
runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },
275
new String[] { "SSLv3", "SSLv2Hello" },
276
false, "SSLv3");
277
runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },
278
new String[] { "SSLv3" },
279
false, "SSLv3");
280
runCase(new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },
281
new String[] { "TLSv1", "SSLv3", "SSLv2Hello" },
282
false, "TLSv1");
283
}
284
285
private static void runCase(
286
String[] serverProtocols,
287
String[] clientProtocols,
288
boolean exceptionExpected,
289
String selectedProtocol) throws Exception {
290
new TestEnabledProtocols(
291
serverProtocols,
292
clientProtocols,
293
exceptionExpected,
294
selectedProtocol).run();
295
}
296
297
private static void showProtocols(String name, String[] protocols) {
298
System.out.printf("Enabled protocols on the %s are: %s%n",
299
name,
300
Arrays.asList(protocols));
301
}
302
}
303
304