Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/ssl/SSLContextImpl/CustomizedDTLSServerDefaultProtocols.java
41152 views
1
/*
2
* Copyright (c) 2018, 2020, 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 8237474
30
* @summary Test jdk.tls.server.protocols with DTLS
31
* @run main/othervm -Djdk.tls.server.protocols="DTLSv1.0"
32
* CustomizedDTLSServerDefaultProtocols
33
*/
34
35
import java.lang.UnsupportedOperationException;
36
import java.security.NoSuchAlgorithmException;
37
import java.security.Security;
38
import java.util.Arrays;
39
import java.util.HashSet;
40
import java.util.Set;
41
42
import javax.net.SocketFactory;
43
import javax.net.ssl.SSLContext;
44
import javax.net.ssl.SSLEngine;
45
import javax.net.ssl.SSLParameters;
46
import javax.net.ssl.SSLServerSocket;
47
import javax.net.ssl.SSLServerSocketFactory;
48
import javax.net.ssl.SSLSocket;
49
50
public class CustomizedDTLSServerDefaultProtocols {
51
52
final static String[] supportedProtocols = new String[]{
53
"DTLSv1.0", "DTLSv1.2"};
54
55
enum ContextVersion {
56
TLS_CV_01("DTLS",
57
new String[]{"DTLSv1.0"},
58
supportedProtocols),
59
TLS_CV_02("DTLSv1.0",
60
supportedProtocols,
61
new String[]{"DTLSv1.0"}),
62
TLS_CV_03("DTLS1.2",
63
supportedProtocols,
64
supportedProtocols);
65
66
final String contextVersion;
67
final String[] serverEnabledProtocols;
68
final String[] clientEnabledProtocols;
69
70
ContextVersion(String contextVersion, String[] serverEnabledProtocols,
71
String[] clientEnabledProtocols) {
72
this.contextVersion = contextVersion;
73
this.serverEnabledProtocols = serverEnabledProtocols;
74
this.clientEnabledProtocols = clientEnabledProtocols;
75
}
76
}
77
78
private static boolean checkProtocols(String[] target, String[] expected) {
79
boolean success = true;
80
if (target.length == 0) {
81
System.out.println("\tError: No protocols");
82
success = false;
83
}
84
85
if (!protocolEquals(target, expected)) {
86
System.out.println("\tError: Expected to get protocols " +
87
Arrays.toString(expected));
88
success = false;
89
}
90
System.out.println("\t Protocols found " + Arrays.toString(target));
91
return success;
92
}
93
94
private static boolean protocolEquals(
95
String[] actualProtocols,
96
String[] expectedProtocols) {
97
if (actualProtocols.length != expectedProtocols.length) {
98
return false;
99
}
100
101
Set<String> set = new HashSet<>(Arrays.asList(expectedProtocols));
102
for (String actual : actualProtocols) {
103
if (set.add(actual)) {
104
return false;
105
}
106
}
107
108
return true;
109
}
110
111
private static boolean checkCipherSuites(String[] target) {
112
boolean success = true;
113
if (target.length == 0) {
114
System.out.println("\tError: No cipher suites");
115
success = false;
116
}
117
118
return success;
119
}
120
121
public static void main(String[] args) throws Exception {
122
// reset the security property to make sure that the algorithms
123
// and keys used in this test are not disabled.
124
Security.setProperty("jdk.tls.disabledAlgorithms", "");
125
System.out.println("jdk.tls.client.protocols = " +
126
System.getProperty("jdk.tls.client.protocols"));
127
System.out.println("jdk.tls.server.protocols = "+
128
System.getProperty("jdk.tls.server.protocols"));
129
Test();
130
}
131
132
static void Test() throws Exception {
133
boolean failed = false;
134
135
SSLContext context;
136
for (ContextVersion cv : ContextVersion.values()) {
137
System.out.println("Checking SSLContext of " + cv.contextVersion);
138
try {
139
context = SSLContext.getInstance(cv.contextVersion);
140
} catch (NoSuchAlgorithmException e) {
141
if (cv.contextVersion.compareToIgnoreCase("DTLS1.2") == 0) {
142
System.out.println("Exception expected: " + e.getMessage());
143
continue;
144
}
145
throw e;
146
}
147
// Default SSLContext is initialized automatically.
148
if (!cv.contextVersion.equals("Default")) {
149
// Use default TK, KM and random.
150
context.init(null, null, null);
151
}
152
153
//
154
// Check SSLContext
155
//
156
// Check default SSLParameters of SSLContext
157
System.out.println("\tChecking default SSLParameters");
158
SSLParameters parameters = context.getDefaultSSLParameters();
159
160
String[] protocols = parameters.getProtocols();
161
failed |= !checkProtocols(protocols, cv.clientEnabledProtocols);
162
163
String[] ciphers = parameters.getCipherSuites();
164
failed |= !checkCipherSuites(ciphers);
165
166
// Check supported SSLParameters of SSLContext
167
System.out.println("\tChecking supported SSLParameters");
168
parameters = context.getSupportedSSLParameters();
169
170
protocols = parameters.getProtocols();
171
failed |= !checkProtocols(protocols, supportedProtocols);
172
173
ciphers = parameters.getCipherSuites();
174
failed |= !checkCipherSuites(ciphers);
175
176
//
177
// Check SSLEngine
178
//
179
// Check SSLParameters of SSLEngine
180
System.out.println();
181
System.out.println("\tChecking SSLEngine of this SSLContext");
182
System.out.println("\tChecking SSLEngine.getSSLParameters()");
183
SSLEngine engine = context.createSSLEngine();
184
engine.setUseClientMode(true);
185
parameters = engine.getSSLParameters();
186
187
protocols = parameters.getProtocols();
188
failed |= !checkProtocols(protocols, cv.clientEnabledProtocols);
189
190
ciphers = parameters.getCipherSuites();
191
failed |= !checkCipherSuites(ciphers);
192
193
System.out.println("\tChecking SSLEngine.getEnabledProtocols()");
194
protocols = engine.getEnabledProtocols();
195
failed |= !checkProtocols(protocols, cv.clientEnabledProtocols);
196
197
System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");
198
ciphers = engine.getEnabledCipherSuites();
199
failed |= !checkCipherSuites(ciphers);
200
201
System.out.println("\tChecking SSLEngine.getSupportedProtocols()");
202
protocols = engine.getSupportedProtocols();
203
failed |= !checkProtocols(protocols, supportedProtocols);
204
205
System.out.println(
206
"\tChecking SSLEngine.getSupportedCipherSuites()");
207
ciphers = engine.getSupportedCipherSuites();
208
failed |= !checkCipherSuites(ciphers);
209
210
//
211
// Check SSLSocket
212
//
213
// Check SSLParameters of SSLSocket
214
System.out.println();
215
System.out.println("\tChecking SSLSocket of this SSLContext");
216
try {
217
context.getSocketFactory();
218
failed = true;
219
System.out.println("SSLSocket returned a socket for DTLS");
220
} catch (UnsupportedOperationException e) {
221
System.out.println("\t " + e.getMessage());
222
}
223
224
//
225
// Check SSLServerSocket
226
//
227
// Check SSLParameters of SSLServerSocket
228
System.out.println();
229
System.out.println("\tChecking SSLServerSocket of this SSLContext");
230
try {
231
context.getServerSocketFactory();
232
failed = true;
233
System.out.println("SSLServerSocket returned a socket for DTLS");
234
} catch (UnsupportedOperationException e) {
235
System.out.println("\t " + e.getMessage());
236
}
237
238
if (failed) {
239
throw new Exception("Run into problems, see log for more details");
240
} else {
241
System.out.println("\t... Success");
242
}
243
}
244
}
245
}
246
247