1
/* SSLServerSocket.java -- SSL server socket.
2
Copyright (C) 2006 Free Software Foundation, Inc.
4
This file is a part of GNU Classpath.
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or (at
9
your option) any later version.
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library. Thus, the terms and
23
conditions of the GNU General Public License cover the whole
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module. An independent module is a module which is not derived from
33
or based on this library. If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so. If you do not wish to do so, delete this
36
exception statement from your version. */
39
package gnu.javax.net.ssl.provider;
41
import java.io.IOException;
43
import java.net.InetAddress;
44
import java.net.Socket;
46
import java.security.SecureRandom;
48
import java.util.ArrayList;
49
import java.util.Iterator;
50
import java.util.List;
51
import java.util.SortedSet;
52
import java.util.TreeSet;
54
import javax.net.ssl.X509KeyManager;
55
import javax.net.ssl.X509TrustManager;
57
import gnu.javax.net.ssl.SRPTrustManager;
59
class SSLServerSocket extends javax.net.ssl.SSLServerSocket
63
// -------------------------------------------------------------------------
65
private SessionContext sessions;
66
private SortedSet enabledProtocols = new TreeSet(SSLSocket.supportedProtocols);
67
private List enabledSuites = new ArrayList(SSLSocket.supportedSuites);
68
private boolean clientMode = false;
69
private boolean needClientAuth = false;
70
private boolean wantClientAuth = false;
71
private boolean createSessions = true;
72
private SRPTrustManager srpTrustManager;
73
private X509TrustManager trustManager;
74
private X509KeyManager keyManager;
75
private SecureRandom random;
78
// -------------------------------------------------------------------------
80
SSLServerSocket() throws IOException
85
SSLServerSocket(int port) throws IOException
90
SSLServerSocket(int port, int backlog) throws IOException
95
SSLServerSocket(int port, int backlog, InetAddress address)
98
super(port, backlog, address);
102
// -------------------------------------------------------------------------
104
public String[] getSupportedCipherSuites()
106
return (String[]) CipherSuite.availableSuiteNames().toArray(new String[0]);
109
public String[] getEnabledCipherSuites()
111
synchronized (enabledSuites)
113
String[] s = new String[enabledSuites.size()];
115
for (Iterator it = enabledSuites.iterator(); it.hasNext(); )
116
s[i++] = it.next().toString();
121
public void setEnabledCipherSuites(String[] suites)
123
if (suites == null || suites.length == 0)
124
throw new IllegalArgumentException();
125
for (int i = 0; i < suites.length; i++)
126
if (CipherSuite.forName(suites[i]) == null)
127
throw new IllegalArgumentException("unsupported suite: " +
129
synchronized (enabledSuites)
131
enabledSuites.clear();
132
for (int i = 0; i < suites.length; i++)
134
CipherSuite suite = CipherSuite.forName(suites[i]);
135
if (!enabledSuites.contains(suite))
136
enabledSuites.add(suite);
141
public String[] getSupportedProtocols()
143
return new String[] { "SSLv3", "TLSv1", "TLSv1.1" };
146
public String[] getEnabledProtocols()
148
synchronized (enabledProtocols)
150
String[] s = new String[enabledProtocols.size()];
152
for (Iterator it = enabledProtocols.iterator(); it.hasNext(); )
153
s[i++] = it.next().toString();
158
public void setEnabledProtocols(String[] protocols)
160
if (protocols == null || protocols.length == 0)
161
throw new IllegalArgumentException();
162
for (int i = 0; i < protocols.length; i++)
164
if (!(protocols[i].equalsIgnoreCase("SSLv3") ||
165
protocols[i].equalsIgnoreCase("TLSv1") ||
166
protocols[i].equalsIgnoreCase("TLSv1.1")))
169
IllegalArgumentException("unsupported protocol: " +
173
synchronized (enabledProtocols)
175
enabledProtocols.clear();
176
for (int i = 0; i < protocols.length; i++)
178
if (protocols[i].equalsIgnoreCase("SSLv3"))
179
enabledProtocols.add(ProtocolVersion.SSL_3);
180
else if (protocols[i].equalsIgnoreCase("TLSv1"))
181
enabledProtocols.add(ProtocolVersion.TLS_1);
183
enabledProtocols.add(ProtocolVersion.TLS_1_1);
188
public void setUseClientMode(boolean clientMode)
190
this.clientMode = clientMode;
193
public boolean getUseClientMode()
198
public void setNeedClientAuth(boolean needClientAuth)
200
this.needClientAuth = needClientAuth;
203
public boolean getNeedClientAuth()
205
return needClientAuth;
208
public void setWantClientAuth(boolean wantClientAuth)
210
this.wantClientAuth = wantClientAuth;
213
public boolean getWantClientAuth()
215
return wantClientAuth;
218
// I misspelled this method in javax.net.SSLServerSocket, and that version
219
// made it into kaffe 1.1.4.
220
public void setEnabledSessionCreation(boolean createSessions)
222
setEnableSessionCreation(createSessions);
225
public void setEnableSessionCreation(boolean createSessions)
227
this.createSessions = createSessions;
230
public boolean getEnableSessionCreation()
232
return createSessions;
236
// -------------------------------------------------------------------------
238
public Socket accept() throws IOException
240
SSLSocket socket = new SSLSocket();
242
socket.setUseClientMode(clientMode);
243
socket.setNeedClientAuth(needClientAuth);
244
socket.setWantClientAuth(wantClientAuth);
245
socket.setEnableSessionCreation(createSessions);
246
socket.setSessionContext(sessions);
247
socket.setEnabledCipherSuites(new ArrayList(enabledSuites));
248
socket.setEnabledProtocols(new TreeSet(enabledProtocols));
249
socket.setSRPTrustManager(srpTrustManager);
250
socket.setTrustManager(trustManager);
251
socket.setKeyManager(keyManager);
252
socket.setRandom(random);
257
// -------------------------------------------------------------------------
259
void setSessionContext(SessionContext sessions)
261
this.sessions = sessions;
264
void setKeyManager(X509KeyManager keyManager)
266
this.keyManager = keyManager;
269
void setTrustManager(X509TrustManager trustManager)
271
this.trustManager = trustManager;
274
void setSRPTrustManager(SRPTrustManager srpTrustManager)
276
this.srpTrustManager = srpTrustManager;
279
void setRandom(SecureRandom random)
281
this.random = random;