2
|
1 |
/*
|
50768
|
2 |
* Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
50768
|
24 |
import java.io.IOException;
|
|
25 |
import java.io.InputStream;
|
|
26 |
import java.io.OutputStream;
|
|
27 |
import java.util.concurrent.Executor;
|
|
28 |
import java.util.concurrent.Executors;
|
2
|
29 |
|
50768
|
30 |
import javax.net.ssl.KeyManager;
|
|
31 |
import javax.net.ssl.SSLContext;
|
|
32 |
import javax.net.ssl.SSLServerSocket;
|
|
33 |
import javax.net.ssl.SSLServerSocketFactory;
|
|
34 |
import javax.net.ssl.SSLSocket;
|
|
35 |
import javax.net.ssl.TrustManager;
|
2
|
36 |
|
|
37 |
class JSSEServer extends CipherTest.Server {
|
|
38 |
|
|
39 |
SSLServerSocket serverSocket;
|
|
40 |
|
|
41 |
JSSEServer(CipherTest cipherTest) throws Exception {
|
|
42 |
super(cipherTest);
|
|
43 |
SSLContext serverContext = SSLContext.getInstance("TLS");
|
50768
|
44 |
serverContext.init(
|
|
45 |
new KeyManager[] { CipherTest.keyManager },
|
|
46 |
new TrustManager[] { CipherTest.trustManager },
|
|
47 |
CipherTest.secureRandom);
|
2
|
48 |
|
50768
|
49 |
SSLServerSocketFactory factory
|
|
50 |
= (SSLServerSocketFactory) serverContext.getServerSocketFactory();
|
|
51 |
serverSocket
|
|
52 |
= (SSLServerSocket) factory.createServerSocket(CipherTest.serverPort);
|
|
53 |
CipherTest.serverPort = serverSocket.getLocalPort();
|
2
|
54 |
serverSocket.setEnabledCipherSuites(factory.getSupportedCipherSuites());
|
|
55 |
serverSocket.setWantClientAuth(true);
|
|
56 |
}
|
|
57 |
|
|
58 |
public void run() {
|
50768
|
59 |
System.out.println("JSSE Server listening on port " + CipherTest.serverPort);
|
2
|
60 |
Executor exec = Executors.newFixedThreadPool
|
|
61 |
(cipherTest.THREADS, DaemonThreadFactory.INSTANCE);
|
|
62 |
try {
|
|
63 |
while (true) {
|
|
64 |
final SSLSocket socket = (SSLSocket)serverSocket.accept();
|
50768
|
65 |
socket.setSoTimeout(CipherTest.TIMEOUT);
|
2
|
66 |
Runnable r = new Runnable() {
|
|
67 |
public void run() {
|
|
68 |
try {
|
|
69 |
InputStream in = socket.getInputStream();
|
|
70 |
OutputStream out = socket.getOutputStream();
|
|
71 |
handleRequest(in, out);
|
|
72 |
out.flush();
|
|
73 |
socket.close();
|
|
74 |
socket.getSession().invalidate();
|
|
75 |
} catch (IOException e) {
|
|
76 |
cipherTest.setFailed();
|
|
77 |
e.printStackTrace();
|
|
78 |
} finally {
|
|
79 |
if (socket != null) {
|
|
80 |
try {
|
|
81 |
socket.close();
|
|
82 |
} catch (IOException e) {
|
|
83 |
cipherTest.setFailed();
|
|
84 |
System.out.println("Exception closing socket on server side:");
|
|
85 |
e.printStackTrace();
|
|
86 |
}
|
|
87 |
}
|
|
88 |
}
|
|
89 |
}
|
|
90 |
};
|
|
91 |
exec.execute(r);
|
|
92 |
}
|
|
93 |
} catch (IOException e) {
|
|
94 |
cipherTest.setFailed();
|
|
95 |
e.printStackTrace();
|
|
96 |
//
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
|
100 |
}
|