SSLServerSocketImpl.java revision 7365de1056414750d0a7d1fdd26025fd247f0d04
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.xnet.provider.jsse;
19
20import java.io.IOException;
21import java.net.InetAddress;
22import java.net.Socket;
23import javax.net.ssl.SSLServerSocket;
24
25/**
26 * SSLServerSocket implementation
27 * @see javax.net.ssl.SSLServerSocket class documentation for more information.
28 */
29public class SSLServerSocketImpl extends SSLServerSocket {
30
31    // the sslParameters object encapsulates all the info
32    // about supported and enabled cipher suites and protocols,
33    // as well as the information about client/server mode of
34    // ssl socket, whether it require/want client authentication or not,
35    // and controls whether new SSL sessions may be established by this
36    // socket or not.
37    private final SSLParameters sslParameters;
38
39    // logger
40    private Logger.Stream logger = Logger.getStream("ssocket");
41
42    /**
43     * Ctor
44     * @param   sslParameters:  SSLParameters
45     * @throws  IOException
46     */
47    protected SSLServerSocketImpl(SSLParameters sslParameters)
48        throws IOException {
49        super();
50        this.sslParameters = sslParameters;
51    }
52
53    /**
54     * Ctor
55     * @param   port:   int
56     * @param   sslParameters:  SSLParameters
57     * @throws  IOException
58     */
59    protected SSLServerSocketImpl(int port, SSLParameters sslParameters)
60        throws IOException {
61        super(port);
62        this.sslParameters = sslParameters;
63    }
64
65    /**
66     * Ctor
67     * @param   port:   int
68     * @param   backlog:    int
69     * @param   sslParameters:  SSLParameters
70     * @throws  IOException
71     */
72    protected SSLServerSocketImpl(int port, int backlog,
73            SSLParameters sslParameters) throws IOException {
74        super(port, backlog);
75        this.sslParameters = sslParameters;
76    }
77
78    /**
79     * Ctor
80     * @param   port:   int
81     * @param   backlog:    int
82     * @param   iAddress:   InetAddress
83     * @param   sslParameters:  SSLParameters
84     * @throws  IOException
85     */
86    protected SSLServerSocketImpl(int port, int backlog,
87                                InetAddress iAddress,
88                                SSLParameters sslParameters)
89        throws IOException {
90        super(port, backlog, iAddress);
91        this.sslParameters = sslParameters;
92    }
93
94    // --------------- SSLParameters based methods ---------------------
95
96    /**
97     * This method works according to the specification of implemented class.
98     * @see javax.net.ssl.SSLServerSocket#getSupportedCipherSuites()
99     * method documentation for more information
100     */
101    @Override
102    public String[] getSupportedCipherSuites() {
103        return CipherSuite.getSupportedCipherSuiteNames();
104    }
105
106    /**
107     * This method works according to the specification of implemented class.
108     * @see javax.net.ssl.SSLServerSocket#getEnabledCipherSuites()
109     * method documentation for more information
110     */
111    @Override
112    public String[] getEnabledCipherSuites() {
113        return sslParameters.getEnabledCipherSuites();
114    }
115
116    /**
117     * This method works according to the specification of implemented class.
118     * @see javax.net.ssl.SSLServerSocket#setEnabledCipherSuites(String[])
119     * method documentation for more information
120     */
121    @Override
122    public void setEnabledCipherSuites(String[] suites) {
123        sslParameters.setEnabledCipherSuites(suites);
124    }
125
126    /**
127     * This method works according to the specification of implemented class.
128     * @see javax.net.ssl.SSLServerSocket#getSupportedProtocols()
129     * method documentation for more information
130     */
131    @Override
132    public String[] getSupportedProtocols() {
133        return ProtocolVersion.supportedProtocols.clone();
134    }
135
136    /**
137     * This method works according to the specification of implemented class.
138     * @see javax.net.ssl.SSLServerSocket#getEnabledProtocols()
139     * method documentation for more information
140     */
141    @Override
142    public String[] getEnabledProtocols() {
143        return sslParameters.getEnabledProtocols();
144    }
145
146    /**
147     * This method works according to the specification of implemented class.
148     * @see javax.net.ssl.SSLServerSocket#setEnabledProtocols(String[])
149     * method documentation for more information
150     */
151    @Override
152    public void setEnabledProtocols(String[] protocols) {
153        sslParameters.setEnabledProtocols(protocols);
154    }
155
156    /**
157     * This method works according to the specification of implemented class.
158     * @see javax.net.ssl.SSLServerSocket#setUseClientMode(boolean)
159     * method documentation for more information
160     */
161    @Override
162    public void setUseClientMode(boolean mode) {
163        sslParameters.setUseClientMode(mode);
164    }
165
166    /**
167     * This method works according to the specification of implemented class.
168     * @see javax.net.ssl.SSLServerSocket#getUseClientMode()
169     * method documentation for more information
170     */
171    @Override
172    public boolean getUseClientMode() {
173        return sslParameters.getUseClientMode();
174    }
175
176    /**
177     * This method works according to the specification of implemented class.
178     * @see javax.net.ssl.SSLServerSocket#setNeedClientAuth(boolean)
179     * method documentation for more information
180     */
181    @Override
182    public void setNeedClientAuth(boolean need) {
183        sslParameters.setNeedClientAuth(need);
184    }
185
186    /**
187     * This method works according to the specification of implemented class.
188     * @see javax.net.ssl.SSLServerSocket#getNeedClientAuth()
189     * method documentation for more information
190     */
191    @Override
192    public boolean getNeedClientAuth() {
193        return sslParameters.getNeedClientAuth();
194    }
195
196    /**
197     * This method works according to the specification of implemented class.
198     * @see javax.net.ssl.SSLServerSocket#setWantClientAuth(boolean)
199     * method documentation for more information
200     */
201    @Override
202    public void setWantClientAuth(boolean want) {
203        sslParameters.setWantClientAuth(want);
204    }
205
206    /**
207     * This method works according to the specification of implemented class.
208     * @see javax.net.ssl.SSLServerSocket#getWantClientAuth()
209     * method documentation for more information
210     */
211    @Override
212    public boolean getWantClientAuth() {
213        return sslParameters.getWantClientAuth();
214    }
215
216    /**
217     * This method works according to the specification of implemented class.
218     * @see javax.net.ssl.SSLServerSocket#setEnableSessionCreation(boolean)
219     * method documentation for more information
220     */
221    @Override
222    public void setEnableSessionCreation(boolean flag) {
223        sslParameters.setEnableSessionCreation(flag);
224    }
225
226    /**
227     * This method works according to the specification of implemented class.
228     * @see javax.net.ssl.SSLServerSocket#getEnableSessionCreation()
229     * method documentation for more information
230     */
231    @Override
232    public boolean getEnableSessionCreation() {
233        return sslParameters.getEnableSessionCreation();
234    }
235
236
237    // ------------- ServerSocket's methods overridings ----------------
238
239    /**
240     * This method works according to the specification of implemented class.
241     * @see java.net.ServerSocket#accept()
242     * method documentation for more information
243     */
244    @Override
245    public Socket accept() throws IOException {
246        if (logger != null) {
247            logger.println("SSLServerSocketImpl.accept ..");
248        }
249        SSLSocketImpl s = new SSLSocketImpl(
250                (SSLParameters) sslParameters.clone());
251        implAccept(s);
252        SecurityManager sm = System.getSecurityManager();
253        if (sm != null) {
254            try {
255                sm.checkAccept(s.getInetAddress().getHostAddress(),
256                        s.getPort());
257            } catch(SecurityException e) {
258                s.close();
259                throw e;
260            }
261        }
262        s.init();
263        if (logger != null) {
264            logger.println("SSLServerSocketImpl: accepted, initialized");
265        }
266        return s;
267    }
268
269    /**
270     * Returns the string representation of the object.
271     */
272    @Override
273    public String toString() {
274        return "[SSLServerSocketImpl]";
275    }
276
277    // -----------------------------------------------------------------
278}
279
280