Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/ServerName/SSLEngineExplorerUnmatchedSNI.java
41152 views
1
/*
2
* Copyright (c) 2012, 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
// SunJSSE does not support dynamic system properties, no way to re-use
26
// system properties in samevm/agentvm mode.
27
//
28
29
/*
30
* @test
31
* @bug 7068321
32
* @summary Support TLS Server Name Indication (SNI) Extension in JSSE Server
33
* @library ../SSLEngine ../templates
34
* @build SSLEngineService SSLCapabilities SSLExplorer
35
* @run main/othervm SSLEngineExplorerUnmatchedSNI www.example.com
36
* www\.example\.org
37
*/
38
39
import javax.net.ssl.*;
40
import java.io.*;
41
import java.nio.*;
42
import java.net.*;
43
import java.util.*;
44
import java.nio.channels.*;
45
46
public class SSLEngineExplorerUnmatchedSNI extends SSLEngineService {
47
48
/*
49
* =============================================================
50
* Set the various variables needed for the tests, then
51
* specify what tests to run on each side.
52
*/
53
54
/*
55
* Should we run the client or server in a separate thread?
56
* Both sides can throw exceptions, but do you have a preference
57
* as to which side should be the main thread.
58
*/
59
static boolean separateServerThread = false;
60
61
// Is the server ready to serve?
62
volatile static boolean serverReady = false;
63
64
/*
65
* Turn on SSL debugging?
66
*/
67
static boolean debug = false;
68
69
/*
70
* Define the server side of the test.
71
*
72
* If the server prematurely exits, serverReady will be set to true
73
* to avoid infinite hangs.
74
*/
75
void doServerSide() throws Exception {
76
77
// create SSLEngine.
78
SSLEngine ssle = createSSLEngine(false);
79
80
// Create a server socket channel.
81
InetSocketAddress isa =
82
new InetSocketAddress(InetAddress.getLocalHost(), serverPort);
83
ServerSocketChannel ssc = ServerSocketChannel.open();
84
ssc.socket().bind(isa);
85
serverPort = ssc.socket().getLocalPort();
86
87
// Signal Client, we're ready for his connect.
88
serverReady = true;
89
90
// Accept a socket channel.
91
SocketChannel sc = ssc.accept();
92
93
// Complete connection.
94
while (!sc.finishConnect()) {
95
Thread.sleep(50);
96
// waiting for the connection completed.
97
}
98
99
ByteBuffer buffer = ByteBuffer.allocate(0xFF);
100
int position = 0;
101
SSLCapabilities capabilities = null;
102
103
// Read the header of TLS record
104
buffer.limit(SSLExplorer.RECORD_HEADER_SIZE);
105
while (position < SSLExplorer.RECORD_HEADER_SIZE) {
106
int n = sc.read(buffer);
107
if (n < 0) {
108
throw new Exception("unexpected end of stream!");
109
}
110
position += n;
111
}
112
buffer.flip();
113
114
int recordLength = SSLExplorer.getRequiredSize(buffer);
115
if (buffer.capacity() < recordLength) {
116
ByteBuffer oldBuffer = buffer;
117
buffer = ByteBuffer.allocate(recordLength);
118
buffer.put(oldBuffer);
119
}
120
121
buffer.position(SSLExplorer.RECORD_HEADER_SIZE);
122
buffer.limit(buffer.capacity());
123
while (position < recordLength) {
124
int n = sc.read(buffer);
125
if (n < 0) {
126
throw new Exception("unexpected end of stream!");
127
}
128
position += n;
129
}
130
buffer.flip();
131
132
capabilities = SSLExplorer.explore(buffer);
133
if (capabilities != null) {
134
System.out.println("Record version: " +
135
capabilities.getRecordVersion());
136
System.out.println("Hello version: " +
137
capabilities.getHelloVersion());
138
}
139
140
// enable server name indication checking
141
SNIMatcher matcher = SNIHostName.createSNIMatcher(
142
serverAcceptableHostname);
143
Collection<SNIMatcher> matchers = new ArrayList<>(1);
144
matchers.add(matcher);
145
SSLParameters params = ssle.getSSLParameters();
146
params.setSNIMatchers(matchers);
147
ssle.setSSLParameters(params);
148
149
try {
150
// handshaking
151
handshaking(ssle, sc, buffer);
152
153
// receive application data
154
receive(ssle, sc);
155
156
// send out application data
157
deliver(ssle, sc);
158
159
// check server name indication
160
ExtendedSSLSession session = (ExtendedSSLSession)ssle.getSession();
161
checkCapabilities(capabilities, session);
162
163
throw new Exception(
164
"Mismatched server name indication was accepted");
165
} catch (SSLHandshakeException sslhe) {
166
// the expected unrecognized server name indication exception
167
} catch (IOException ioe) {
168
// the peer may have closed the socket because of the unmatched
169
// server name indication.
170
} finally {
171
// close the socket channel.
172
sc.close();
173
ssc.close();
174
}
175
}
176
177
/*
178
* Define the client side of the test.
179
*
180
* If the server prematurely exits, serverReady will be set to true
181
* to avoid infinite hangs.
182
*/
183
void doClientSide() throws Exception {
184
// create SSLEngine.
185
SSLEngine ssle = createSSLEngine(true);
186
187
/*
188
* Wait for server to get started.
189
*/
190
while (!serverReady) {
191
Thread.sleep(50);
192
}
193
194
// Create a non-blocking socket channel.
195
SocketChannel sc = SocketChannel.open();
196
sc.configureBlocking(false);
197
InetSocketAddress isa =
198
new InetSocketAddress(InetAddress.getLocalHost(), serverPort);
199
sc.connect(isa);
200
201
// Complete connection.
202
while (!sc.finishConnect() ) {
203
Thread.sleep(50);
204
// waiting for the connection completed.
205
}
206
207
SNIHostName serverName = new SNIHostName(clientRequestedHostname);
208
List<SNIServerName> serverNames = new ArrayList<>(1);
209
serverNames.add(serverName);
210
SSLParameters params = ssle.getSSLParameters();
211
params.setServerNames(serverNames);
212
ssle.setSSLParameters(params);
213
214
try {
215
// handshaking
216
handshaking(ssle, sc, null);
217
218
// send out application data
219
deliver(ssle, sc);
220
221
// receive application data
222
receive(ssle, sc);
223
224
// check server name indication
225
ExtendedSSLSession session = (ExtendedSSLSession)ssle.getSession();
226
checkSNIInSession(session);
227
228
throw new Exception(
229
"Mismatched server name indication was accepted");
230
} catch (SSLHandshakeException sslhe) {
231
// the expected unrecognized server name indication exception
232
} catch (IOException ioe) {
233
// the peer may have closed the socket because of the unmatched
234
// server name indication.
235
} finally {
236
// close the socket channel.
237
sc.close();
238
}
239
}
240
241
void checkCapabilities(SSLCapabilities capabilities,
242
ExtendedSSLSession session) throws Exception {
243
List<SNIServerName> sessionSNI = session.getRequestedServerNames();
244
if (!sessionSNI.equals(capabilities.getServerNames())) {
245
for (SNIServerName sni : sessionSNI) {
246
System.out.println("SNI in session is " + sni);
247
}
248
249
List<SNIServerName> capaSNI = capabilities.getServerNames();
250
for (SNIServerName sni : capaSNI) {
251
System.out.println("SNI in session is " + sni);
252
}
253
254
throw new Exception(
255
"server name indication does not match capabilities");
256
}
257
258
checkSNIInSession(session);
259
}
260
261
void checkSNIInSession(ExtendedSSLSession session) throws Exception {
262
List<SNIServerName> sessionSNI = session.getRequestedServerNames();
263
if (sessionSNI.isEmpty()) {
264
throw new Exception(
265
"unexpected empty request server name indication");
266
}
267
268
if (sessionSNI.size() != 1) {
269
throw new Exception(
270
"unexpected request server name indication");
271
}
272
273
SNIServerName serverName = sessionSNI.get(0);
274
if (!(serverName instanceof SNIHostName)) {
275
throw new Exception(
276
"unexpected instance of request server name indication");
277
}
278
279
String hostname = ((SNIHostName)serverName).getAsciiName();
280
if (!clientRequestedHostname.equalsIgnoreCase(hostname)) {
281
throw new Exception(
282
"unexpected request server name indication value");
283
}
284
}
285
286
private static String clientRequestedHostname;
287
private static String serverAcceptableHostname;
288
289
private static void parseArguments(String[] args) {
290
clientRequestedHostname = args[0];
291
serverAcceptableHostname = args[1];
292
}
293
294
/*
295
* =============================================================
296
* The remainder is just support stuff
297
*/
298
volatile Exception serverException = null;
299
volatile Exception clientException = null;
300
301
// use any free port by default
302
volatile int serverPort = 0;
303
304
public static void main(String args[]) throws Exception {
305
if (debug)
306
System.setProperty("javax.net.debug", "all");
307
308
/*
309
* Get the customized arguments.
310
*/
311
parseArguments(args);
312
313
new SSLEngineExplorerUnmatchedSNI();
314
}
315
316
Thread clientThread = null;
317
Thread serverThread = null;
318
319
/*
320
* Primary constructor, used to drive remainder of the test.
321
*
322
* Fork off the other side, then do your work.
323
*/
324
SSLEngineExplorerUnmatchedSNI() throws Exception {
325
super("../etc");
326
327
if (separateServerThread) {
328
startServer(true);
329
startClient(false);
330
} else {
331
startClient(true);
332
startServer(false);
333
}
334
335
/*
336
* Wait for other side to close down.
337
*/
338
if (separateServerThread) {
339
serverThread.join();
340
} else {
341
clientThread.join();
342
}
343
344
/*
345
* When we get here, the test is pretty much over.
346
*
347
* If the main thread excepted, that propagates back
348
* immediately. If the other thread threw an exception, we
349
* should report back.
350
*/
351
if (serverException != null) {
352
System.out.print("Server Exception:");
353
throw serverException;
354
}
355
if (clientException != null) {
356
System.out.print("Client Exception:");
357
throw clientException;
358
}
359
}
360
361
void startServer(boolean newThread) throws Exception {
362
if (newThread) {
363
serverThread = new Thread() {
364
public void run() {
365
try {
366
doServerSide();
367
} catch (Exception e) {
368
/*
369
* Our server thread just died.
370
*
371
* Release the client, if not active already...
372
*/
373
System.err.println("Server died...");
374
System.err.println(e);
375
serverReady = true;
376
serverException = e;
377
}
378
}
379
};
380
serverThread.start();
381
} else {
382
doServerSide();
383
}
384
}
385
386
void startClient(boolean newThread) throws Exception {
387
if (newThread) {
388
clientThread = new Thread() {
389
public void run() {
390
try {
391
doClientSide();
392
} catch (Exception e) {
393
/*
394
* Our client thread just died.
395
*/
396
System.err.println("Client died...");
397
clientException = e;
398
}
399
}
400
};
401
clientThread.start();
402
} else {
403
doClientSide();
404
}
405
}
406
}
407
408