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