Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/TLS/JSSEServer.java
41152 views
1
/*
2
* Copyright (c) 2010, 2016, 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
import java.io.IOException;
25
import java.io.InputStream;
26
import java.io.OutputStream;
27
import javax.net.ssl.KeyManager;
28
import javax.net.ssl.SSLContext;
29
import javax.net.ssl.SSLServerSocket;
30
import javax.net.ssl.SSLServerSocketFactory;
31
import javax.net.ssl.SSLSocket;
32
import javax.net.ssl.TrustManager;
33
34
public class JSSEServer extends CipherTestUtils.Server {
35
36
private final SSLServerSocket serverSocket;
37
private static volatile boolean closeServer = false;
38
39
JSSEServer(CipherTestUtils cipherTest, int serverPort,
40
String protocol, String cipherSuite) throws Exception {
41
super(cipherTest);
42
SSLContext serverContext = SSLContext.getInstance("TLS");
43
serverContext.init(new KeyManager[]{cipherTest.getServerKeyManager()},
44
new TrustManager[]{cipherTest.getServerTrustManager()},
45
CipherTestUtils.secureRandom);
46
SSLServerSocketFactory factory =
47
(SSLServerSocketFactory)serverContext.getServerSocketFactory();
48
serverSocket =
49
(SSLServerSocket) factory.createServerSocket(serverPort);
50
serverSocket.setEnabledProtocols(protocol.split(","));
51
serverSocket.setEnabledCipherSuites(cipherSuite.split(","));
52
53
CipherTestUtils.printInfo(serverSocket);
54
}
55
56
@Override
57
public void run() {
58
System.out.println("JSSE Server listening on port " + getPort());
59
while (!closeServer) {
60
try (final SSLSocket socket = (SSLSocket) serverSocket.accept()) {
61
socket.setSoTimeout(CipherTestUtils.TIMEOUT);
62
63
try (InputStream in = socket.getInputStream();
64
OutputStream out = socket.getOutputStream()) {
65
handleRequest(in, out);
66
out.flush();
67
} catch (IOException e) {
68
CipherTestUtils.addFailure(e);
69
System.out.println("Got IOException:");
70
e.printStackTrace(System.out);
71
}
72
} catch (Exception e) {
73
CipherTestUtils.addFailure(e);
74
System.out.println("Exception:");
75
e.printStackTrace(System.out);
76
}
77
}
78
}
79
80
int getPort() {
81
return serverSocket.getLocalPort();
82
}
83
84
@Override
85
public void close() throws IOException {
86
closeServer = true;
87
if (serverSocket != null && !serverSocket.isClosed()) {
88
serverSocket.close();
89
}
90
}
91
}
92
93