1/*
2 * Copyright (c) 1997, 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
24import java.io.*;
25import javax.net.ssl.*;
26
27class ServerThread extends TestThread
28    implements HandshakeCompletedListener
29{
30    // Basic server identity info
31    private int                 port;
32    private SSLServerSocketFactory factory;
33
34    private int                 backlog = 50;
35    private SSLServerSocket     ss;
36
37    // Crypto options ... start with basic ciphers, renegotiation
38    // can use a different set.
39    private String              renegotiateSuites [];
40
41    // Flags controlling testing -- passed to handler
42    private boolean             needClientAuth;
43
44    // Flags controlling testing -- main server thread only
45    private boolean             useMT;
46
47    ServerThread (int port, int backlog, SSLContext ctx)
48    {
49        super ("Server");
50        setDaemon (true);
51
52        this.port = port;
53        this.backlog = backlog;
54        factory = ctx.getServerSocketFactory();
55    }
56
57
58    // NEGOTIATION OPTIONS
59
60    public void setRenegotiateSuites (String suites [])
61        { renegotiateSuites = suites; }
62
63
64    // DEFINE WHAT IS BEING TESTED
65
66    public void setNeedClientAuth (boolean flag)
67        { needClientAuth = flag; }
68    public boolean getNeedClientAuth ()
69        { return needClientAuth; }
70
71    public void setUseMT (boolean flag)
72        { useMT = flag; }
73    public boolean getUseMT ()
74        { return useMT; }
75
76    public int getServerPort() {
77        return port;
78    }
79
80
81    public void waitTillReady ()
82    {
83        synchronized (this) {
84            while (ss == null)
85                try { wait (); }
86                catch (InterruptedException e) { }
87        }
88    }
89
90
91    public void run ()
92    {
93        // NOT TESTING:
94        //      - connection backlog
95        //      - binding to IP address
96        //      - base class methods
97
98        try {
99            synchronized (this) {
100                ss = (SSLServerSocket)
101                    factory.createServerSocket(port, backlog);
102                port = ss.getLocalPort();
103                notify ();
104            }
105
106            if (needClientAuth)
107                ss.setNeedClientAuth (true);
108            if (basicCipherSuites != null)
109                ss.setEnabledCipherSuites (basicCipherSuites);
110
111            out.println ("%% Starting " + getName ());
112
113        } catch (Throwable t) {
114            synchronized (out) {
115                out.println ("%% Error in " + getName ());
116                t.printStackTrace (out);
117            }
118
119            return;
120        }
121
122            // XXX for N connections ...
123
124        while (true) {
125            try {
126                SSLSocket       s = (SSLSocket) ss.accept ();
127
128                if (listenHandshake)
129                    s.addHandshakeCompletedListener (this);
130
131                if (verbosity >= 1)
132                    out.println ("%% " + getName () + " accepted from "
133                        + s.getInetAddress ().getHostName ()
134                        + ":" + s.getPort ());
135
136                ServerHandler   handler = new ServerHandler (s);
137
138                handler.setVerbosity (verbosity);
139                handler.setOutput (out);
140
141                handler.setDoRenegotiate (doRenegotiate);
142                handler.setInitiateHandshake (initiateHandshake);
143                handler.setListenHandshake (listenHandshake);
144                handler.setReverseRole (reverseRole);
145                handler.setNeedClientAuth (needClientAuth);
146
147                if (prng != null)
148                    handler.setPRNG (prng);
149
150                if (useMT)
151                    handler.start ();
152                else
153                    handler.run ();
154
155            } catch (Throwable t) {
156                synchronized (out) {
157                    out.println ("%% Error in " + getName ());
158                    t.printStackTrace (out);
159                }
160
161
162                return;
163            }
164        }
165    }
166
167
168    public void handshakeCompleted (HandshakeCompletedEvent event)
169    {
170        if (verbosity >= 2) {
171            out.println ("%% Handshook: " + event.getSource ());
172
173            // if more verbosity, give cert chain
174        }
175    }
176}
177