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