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