OSDN Git Service

* config/i386/i386.md (*sinxf2): Rename to *sinxf2_i387.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / javax / net / ssl / provider / SSLServerSocket.java
1 /* SSLServerSocket.java -- SSL server socket.
2    Copyright (C) 2006  Free Software Foundation, Inc.
3
4 This file is a part of GNU Classpath.
5
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.
10
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.
15
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
19 USA
20
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
24 combination.
25
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.  */
37
38
39 package gnu.javax.net.ssl.provider;
40
41 import java.io.IOException;
42
43 import java.net.InetAddress;
44 import java.net.Socket;
45
46 import java.security.SecureRandom;
47
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;
53
54 import javax.net.ssl.X509KeyManager;
55 import javax.net.ssl.X509TrustManager;
56
57 import gnu.javax.net.ssl.SRPTrustManager;
58
59 class SSLServerSocket extends javax.net.ssl.SSLServerSocket
60 {
61
62   // Fields.
63   // -------------------------------------------------------------------------
64
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;
76
77   // Constructors.
78   // -------------------------------------------------------------------------
79
80   SSLServerSocket() throws IOException
81   {
82     super();
83   }
84
85   SSLServerSocket(int port) throws IOException
86   {
87     super(port);
88   }
89
90   SSLServerSocket(int port, int backlog) throws IOException
91   {
92     super(port, backlog);
93   }
94
95   SSLServerSocket(int port, int backlog, InetAddress address)
96     throws IOException
97   {
98     super(port, backlog, address);
99   }
100
101   // SSL methods.
102   // -------------------------------------------------------------------------
103
104   public String[] getSupportedCipherSuites()
105   {
106     return (String[]) CipherSuite.availableSuiteNames().toArray(new String[0]);
107   }
108
109   public String[] getEnabledCipherSuites()
110   {
111     synchronized (enabledSuites)
112       {
113         String[] s = new String[enabledSuites.size()];
114         int i = 0;
115         for (Iterator it = enabledSuites.iterator(); it.hasNext(); )
116           s[i++] = it.next().toString();
117         return s;
118       }
119   }
120
121   public void setEnabledCipherSuites(String[] suites)
122   {
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: " +
128                                            suites[i]);
129     synchronized (enabledSuites)
130       {
131         enabledSuites.clear();
132         for (int i = 0; i < suites.length; i++)
133           {
134             CipherSuite suite = CipherSuite.forName(suites[i]);
135             if (!enabledSuites.contains(suite))
136               enabledSuites.add(suite);
137           }
138       }
139   }
140
141   public String[] getSupportedProtocols()
142   {
143     return new String[] { "SSLv3", "TLSv1", "TLSv1.1" };
144   }
145
146   public String[] getEnabledProtocols()
147   {
148     synchronized (enabledProtocols)
149       {
150         String[] s = new String[enabledProtocols.size()];
151         int i = 0;
152         for (Iterator it = enabledProtocols.iterator(); it.hasNext(); )
153           s[i++] = it.next().toString();
154         return s;
155       }
156   }
157
158   public void setEnabledProtocols(String[] protocols)
159   {
160     if (protocols == null || protocols.length == 0)
161       throw new IllegalArgumentException();
162     for (int i = 0; i < protocols.length; i++)
163       {
164         if (!(protocols[i].equalsIgnoreCase("SSLv3") ||
165               protocols[i].equalsIgnoreCase("TLSv1") ||
166               protocols[i].equalsIgnoreCase("TLSv1.1")))
167           {
168             throw new
169               IllegalArgumentException("unsupported protocol: " +
170                                        protocols[i]);
171           }
172       }
173     synchronized (enabledProtocols)
174       {
175         enabledProtocols.clear();
176         for (int i = 0; i < protocols.length; i++)
177           {
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);
182             else
183               enabledProtocols.add(ProtocolVersion.TLS_1_1);
184           }
185       }
186   }
187
188   public void setUseClientMode(boolean clientMode)
189   {
190     this.clientMode = clientMode;
191   }
192
193   public boolean getUseClientMode()
194   {
195     return clientMode;
196   }
197
198   public void setNeedClientAuth(boolean needClientAuth)
199   {
200     this.needClientAuth = needClientAuth;
201   }
202
203   public boolean getNeedClientAuth()
204   {
205     return needClientAuth;
206   }
207
208   public void setWantClientAuth(boolean wantClientAuth)
209   {
210     this.wantClientAuth = wantClientAuth;
211   }
212
213   public boolean getWantClientAuth()
214   {
215     return wantClientAuth;
216   }
217
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)
221   {
222     setEnableSessionCreation(createSessions);
223   }
224
225   public void setEnableSessionCreation(boolean createSessions)
226   {
227     this.createSessions = createSessions;
228   }
229
230   public boolean getEnableSessionCreation()
231   {
232     return createSessions;
233   }
234
235   // Socket methods.
236   // -------------------------------------------------------------------------
237
238   public Socket accept() throws IOException
239   {
240     SSLSocket socket = new SSLSocket();
241     implAccept(socket);
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);
253     return socket;
254   }
255
256   // Package methods.
257   // -------------------------------------------------------------------------
258
259   void setSessionContext(SessionContext sessions)
260   {
261     this.sessions = sessions;
262   }
263
264   void setKeyManager(X509KeyManager keyManager)
265   {
266     this.keyManager = keyManager;
267   }
268
269   void setTrustManager(X509TrustManager trustManager)
270   {
271     this.trustManager = trustManager;
272   }
273
274   void setSRPTrustManager(SRPTrustManager srpTrustManager)
275   {
276     this.srpTrustManager = srpTrustManager;
277   }
278
279   void setRandom(SecureRandom random)
280   {
281     this.random = random;
282   }
283 }