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