Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/ServerName/SSLSocketExplorerMatchedSNI.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 ../templates
34
* @build SSLCapabilities SSLExplorer
35
* @run main/othervm SSLSocketExplorerMatchedSNI www.example.com
36
* www\.example\.com
37
* @run main/othervm SSLSocketExplorerMatchedSNI www.example.com
38
* www\.example\.(com|org)
39
* @run main/othervm SSLSocketExplorerMatchedSNI example.com
40
* (.*\.)*example\.(com|org)
41
* @run main/othervm SSLSocketExplorerMatchedSNI www.example.com
42
* (.*\.)*example\.(com|org)
43
* @run main/othervm SSLSocketExplorerMatchedSNI www.us.example.com
44
* (.*\.)*example\.(com|org)
45
*/
46
47
import java.io.*;
48
import java.nio.*;
49
import java.nio.channels.*;
50
import java.util.*;
51
import java.net.*;
52
import javax.net.ssl.*;
53
54
public class SSLSocketExplorerMatchedSNI {
55
56
/*
57
* =============================================================
58
* Set the various variables needed for the tests, then
59
* specify what tests to run on each side.
60
*/
61
62
/*
63
* Should we run the client or server in a separate thread?
64
* Both sides can throw exceptions, but do you have a preference
65
* as to which side should be the main thread.
66
*/
67
static boolean separateServerThread = false;
68
69
/*
70
* Where do we find the keystores?
71
*/
72
static String pathToStores = "../etc";
73
static String keyStoreFile = "keystore";
74
static String trustStoreFile = "truststore";
75
static String passwd = "passphrase";
76
77
/*
78
* Is the server ready to serve?
79
*/
80
volatile static boolean serverReady = false;
81
82
/*
83
* Turn on SSL debugging?
84
*/
85
static boolean debug = false;
86
87
/*
88
* If the client or server is doing some kind of object creation
89
* that the other side depends on, and that thread prematurely
90
* exits, you may experience a hang. The test harness will
91
* terminate all hung threads after its timeout has expired,
92
* currently 3 minutes by default, but you might try to be
93
* smart about it....
94
*/
95
96
/*
97
* Define the server side of the test.
98
*
99
* If the server prematurely exits, serverReady will be set to true
100
* to avoid infinite hangs.
101
*/
102
void doServerSide() throws Exception {
103
104
ServerSocket serverSocket = new ServerSocket(serverPort);
105
106
// Signal Client, we're ready for his connect.
107
serverPort = serverSocket.getLocalPort();
108
serverReady = true;
109
110
Socket socket = serverSocket.accept();
111
InputStream ins = socket.getInputStream();
112
113
byte[] buffer = new byte[0xFF];
114
int position = 0;
115
SSLCapabilities capabilities = null;
116
117
// Read the header of TLS record
118
while (position < SSLExplorer.RECORD_HEADER_SIZE) {
119
int count = SSLExplorer.RECORD_HEADER_SIZE - position;
120
int n = ins.read(buffer, position, count);
121
if (n < 0) {
122
throw new Exception("unexpected end of stream!");
123
}
124
position += n;
125
}
126
127
int recordLength = SSLExplorer.getRequiredSize(buffer, 0, position);
128
if (buffer.length < recordLength) {
129
buffer = Arrays.copyOf(buffer, recordLength);
130
}
131
132
while (position < recordLength) {
133
int count = recordLength - position;
134
int n = ins.read(buffer, position, count);
135
if (n < 0) {
136
throw new Exception("unexpected end of stream!");
137
}
138
position += n;
139
}
140
141
capabilities = SSLExplorer.explore(buffer, 0, recordLength);;
142
if (capabilities != null) {
143
System.out.println("Record version: " +
144
capabilities.getRecordVersion());
145
System.out.println("Hello version: " +
146
capabilities.getHelloVersion());
147
}
148
149
SSLSocketFactory sslsf =
150
(SSLSocketFactory) SSLSocketFactory.getDefault();
151
ByteArrayInputStream bais =
152
new ByteArrayInputStream(buffer, 0, position);
153
SSLSocket sslSocket = (SSLSocket)sslsf.createSocket(socket, bais, true);
154
155
SNIMatcher matcher = SNIHostName.createSNIMatcher(
156
serverAcceptableHostname);
157
Collection<SNIMatcher> matchers = new ArrayList<>(1);
158
matchers.add(matcher);
159
SSLParameters params = sslSocket.getSSLParameters();
160
params.setSNIMatchers(matchers);
161
sslSocket.setSSLParameters(params);
162
163
InputStream sslIS = sslSocket.getInputStream();
164
OutputStream sslOS = sslSocket.getOutputStream();
165
166
sslIS.read();
167
sslOS.write(85);
168
sslOS.flush();
169
170
ExtendedSSLSession session = (ExtendedSSLSession)sslSocket.getSession();
171
checkCapabilities(capabilities, session);
172
173
sslSocket.close();
174
serverSocket.close();
175
}
176
177
178
/*
179
* Define the client side of the test.
180
*
181
* If the server prematurely exits, serverReady will be set to true
182
* to avoid infinite hangs.
183
*/
184
void doClientSide() throws Exception {
185
186
/*
187
* Wait for server to get started.
188
*/
189
while (!serverReady) {
190
Thread.sleep(50);
191
}
192
193
SSLSocketFactory sslsf =
194
(SSLSocketFactory) SSLSocketFactory.getDefault();
195
SSLSocket sslSocket = (SSLSocket)
196
sslsf.createSocket("localhost", serverPort);
197
198
SNIHostName serverName = new SNIHostName(clientRequestedHostname);
199
List<SNIServerName> serverNames = new ArrayList<>(1);
200
serverNames.add(serverName);
201
SSLParameters params = sslSocket.getSSLParameters();
202
params.setServerNames(serverNames);
203
sslSocket.setSSLParameters(params);
204
205
InputStream sslIS = sslSocket.getInputStream();
206
OutputStream sslOS = sslSocket.getOutputStream();
207
208
sslOS.write(280);
209
sslOS.flush();
210
sslIS.read();
211
212
ExtendedSSLSession session = (ExtendedSSLSession)sslSocket.getSession();
213
checkSNIInSession(session);
214
215
sslSocket.close();
216
}
217
218
219
void checkCapabilities(SSLCapabilities capabilities,
220
ExtendedSSLSession session) throws Exception {
221
222
List<SNIServerName> sessionSNI = session.getRequestedServerNames();
223
if (!sessionSNI.equals(capabilities.getServerNames())) {
224
for (SNIServerName sni : sessionSNI) {
225
System.out.println("SNI in session is " + sni);
226
}
227
228
List<SNIServerName> capaSNI = capabilities.getServerNames();
229
for (SNIServerName sni : capaSNI) {
230
System.out.println("SNI in session is " + sni);
231
}
232
233
throw new Exception(
234
"server name indication does not match capabilities");
235
}
236
237
checkSNIInSession(session);
238
}
239
240
void checkSNIInSession(ExtendedSSLSession session) throws Exception {
241
List<SNIServerName> sessionSNI = session.getRequestedServerNames();
242
if (sessionSNI.isEmpty()) {
243
throw new Exception(
244
"unexpected empty request server name indication");
245
}
246
247
if (sessionSNI.size() != 1) {
248
throw new Exception(
249
"unexpected request server name indication");
250
}
251
252
SNIServerName serverName = sessionSNI.get(0);
253
if (!(serverName instanceof SNIHostName)) {
254
throw new Exception(
255
"unexpected instance of request server name indication");
256
}
257
258
String hostname = ((SNIHostName)serverName).getAsciiName();
259
if (!clientRequestedHostname.equalsIgnoreCase(hostname)) {
260
throw new Exception(
261
"unexpected request server name indication value");
262
}
263
}
264
265
private static String clientRequestedHostname;
266
private static String serverAcceptableHostname;
267
268
private static void parseArguments(String[] args) {
269
clientRequestedHostname = args[0];
270
serverAcceptableHostname = args[1];
271
}
272
273
274
/*
275
* =============================================================
276
* The remainder is just support stuff
277
*/
278
279
// use any free port by default
280
volatile int serverPort = 0;
281
282
volatile Exception serverException = null;
283
volatile Exception clientException = null;
284
285
286
public static void main(String[] args) throws Exception {
287
String keyFilename =
288
System.getProperty("test.src", ".") + "/" + pathToStores +
289
"/" + keyStoreFile;
290
String trustFilename =
291
System.getProperty("test.src", ".") + "/" + pathToStores +
292
"/" + trustStoreFile;
293
294
System.setProperty("javax.net.ssl.keyStore", keyFilename);
295
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
296
System.setProperty("javax.net.ssl.trustStore", trustFilename);
297
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
298
299
if (debug)
300
System.setProperty("javax.net.debug", "all");
301
302
/*
303
* Get the customized arguments.
304
*/
305
parseArguments(args);
306
307
/*
308
* Start the tests.
309
*/
310
new SSLSocketExplorerMatchedSNI();
311
}
312
313
Thread clientThread = null;
314
Thread serverThread = null;
315
316
/*
317
* Primary constructor, used to drive remainder of the test.
318
*
319
* Fork off the other side, then do your work.
320
*/
321
SSLSocketExplorerMatchedSNI() throws Exception {
322
try {
323
if (separateServerThread) {
324
startServer(true);
325
startClient(false);
326
} else {
327
startClient(true);
328
startServer(false);
329
}
330
} catch (Exception e) {
331
// swallow for now. Show later
332
}
333
334
/*
335
* Wait for other side to close down.
336
*/
337
if (separateServerThread) {
338
serverThread.join();
339
} else {
340
clientThread.join();
341
}
342
343
/*
344
* When we get here, the test is pretty much over.
345
* Which side threw the error?
346
*/
347
Exception local;
348
Exception remote;
349
String whichRemote;
350
351
if (separateServerThread) {
352
remote = serverException;
353
local = clientException;
354
whichRemote = "server";
355
} else {
356
remote = clientException;
357
local = serverException;
358
whichRemote = "client";
359
}
360
361
/*
362
* If both failed, return the curthread's exception, but also
363
* print the remote side Exception
364
*/
365
if ((local != null) && (remote != null)) {
366
System.out.println(whichRemote + " also threw:");
367
remote.printStackTrace();
368
System.out.println();
369
throw local;
370
}
371
372
if (remote != null) {
373
throw remote;
374
}
375
376
if (local != null) {
377
throw local;
378
}
379
}
380
381
void startServer(boolean newThread) throws Exception {
382
if (newThread) {
383
serverThread = new Thread() {
384
public void run() {
385
try {
386
doServerSide();
387
} catch (Exception e) {
388
/*
389
* Our server thread just died.
390
*
391
* Release the client, if not active already...
392
*/
393
System.err.println("Server died...");
394
serverReady = true;
395
serverException = e;
396
}
397
}
398
};
399
serverThread.start();
400
} else {
401
try {
402
doServerSide();
403
} catch (Exception e) {
404
serverException = e;
405
} finally {
406
serverReady = true;
407
}
408
}
409
}
410
411
void startClient(boolean newThread) throws Exception {
412
if (newThread) {
413
clientThread = new Thread() {
414
public void run() {
415
try {
416
doClientSide();
417
} catch (Exception e) {
418
/*
419
* Our client thread just died.
420
*/
421
System.err.println("Client died...");
422
clientException = e;
423
}
424
}
425
};
426
clientThread.start();
427
} else {
428
try {
429
doClientSide();
430
} catch (Exception e) {
431
clientException = e;
432
}
433
}
434
}
435
}
436
437