1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.conscrypt;
18
19import java.io.IOException;
20import java.net.InetAddress;
21import java.net.Socket;
22
23/**
24 * OpenSSL-based implementation of server sockets.
25 */
26public class OpenSSLServerSocketImpl extends javax.net.ssl.SSLServerSocket {
27    private final SSLParametersImpl sslParameters;
28    private boolean channelIdEnabled;
29
30    protected OpenSSLServerSocketImpl(SSLParametersImpl sslParameters) throws IOException {
31        this.sslParameters = sslParameters;
32    }
33
34    protected OpenSSLServerSocketImpl(int port, SSLParametersImpl sslParameters)
35        throws IOException {
36        super(port);
37        this.sslParameters = sslParameters;
38    }
39
40    protected OpenSSLServerSocketImpl(int port, int backlog, SSLParametersImpl sslParameters)
41        throws IOException {
42        super(port, backlog);
43        this.sslParameters = sslParameters;
44    }
45
46    protected OpenSSLServerSocketImpl(int port,
47                                      int backlog,
48                                      InetAddress iAddress,
49                                      SSLParametersImpl sslParameters)
50        throws IOException {
51        super(port, backlog, iAddress);
52        this.sslParameters = sslParameters;
53    }
54
55    @Override
56    public boolean getEnableSessionCreation() {
57        return sslParameters.getEnableSessionCreation();
58    }
59
60    @Override
61    public void setEnableSessionCreation(boolean flag) {
62        sslParameters.setEnableSessionCreation(flag);
63    }
64
65    /**
66     * The names of the protocols' versions that may be used on this SSL
67     * connection.
68     * @return an array of protocols names
69     */
70    @Override
71    public String[] getSupportedProtocols() {
72        return NativeCrypto.getSupportedProtocols();
73    }
74
75    /**
76     * The names of the protocols' versions that in use on this SSL connection.
77     *
78     * @return an array of protocols names
79     */
80    @Override
81    public String[] getEnabledProtocols() {
82        return sslParameters.getEnabledProtocols();
83    }
84
85    /**
86     * This method enables the protocols' versions listed by
87     * getSupportedProtocols().
88     *
89     * @param protocols names of all the protocols to enable.
90     *
91     * @throws IllegalArgumentException when one or more of the names in the
92     *             array are not supported, or when the array is null.
93     */
94    @Override
95    public void setEnabledProtocols(String[] protocols) {
96        sslParameters.setEnabledProtocols(protocols);
97    }
98
99    @Override
100    public String[] getSupportedCipherSuites() {
101        return NativeCrypto.getSupportedCipherSuites();
102    }
103
104    @Override
105    public String[] getEnabledCipherSuites() {
106        return sslParameters.getEnabledCipherSuites();
107    }
108
109    /**
110     * Enables/disables the TLS Channel ID extension for this server socket.
111     */
112    public void setChannelIdEnabled(boolean enabled) {
113      channelIdEnabled = enabled;
114    }
115
116    /**
117     * Checks whether the TLS Channel ID extension is enabled for this server socket.
118     */
119    public boolean isChannelIdEnabled() {
120      return channelIdEnabled;
121    }
122
123    /**
124     * This method enables the cipher suites listed by
125     * getSupportedCipherSuites().
126     *
127     * @param suites the names of all the cipher suites to enable
128     * @throws IllegalArgumentException when one or more of the ciphers in array
129     *         suites are not supported, or when the array is null.
130     */
131    @Override
132    public void setEnabledCipherSuites(String[] suites) {
133        sslParameters.setEnabledCipherSuites(suites);
134    }
135
136    @Override
137    public boolean getWantClientAuth() {
138        return sslParameters.getWantClientAuth();
139    }
140
141    @Override
142    public void setWantClientAuth(boolean want) {
143        sslParameters.setWantClientAuth(want);
144    }
145
146    @Override
147    public boolean getNeedClientAuth() {
148        return sslParameters.getNeedClientAuth();
149    }
150
151    @Override
152    public void setNeedClientAuth(boolean need) {
153        sslParameters.setNeedClientAuth(need);
154    }
155
156    @Override
157    public void setUseClientMode(boolean mode) {
158        sslParameters.setUseClientMode(mode);
159    }
160
161    @Override
162    public boolean getUseClientMode() {
163        return sslParameters.getUseClientMode();
164    }
165
166    @Override
167    public Socket accept() throws IOException {
168        OpenSSLSocketImpl socket = new OpenSSLSocketImpl(sslParameters);
169        socket.setChannelIdEnabled(channelIdEnabled);
170        implAccept(socket);
171        return socket;
172    }
173}
174