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.ServerSocket;
22import java.security.KeyManagementException;
23
24/**
25 * An implementation of {@link javax.net.ssl.SSLServerSocketFactory} using BoringSSL.
26 *
27 * @hide
28 */
29@Internal
30public class OpenSSLServerSocketFactoryImpl extends javax.net.ssl.SSLServerSocketFactory {
31    private static boolean useEngineSocketByDefault = SSLUtils.USE_ENGINE_SOCKET_BY_DEFAULT;
32
33    private SSLParametersImpl sslParameters;
34    private IOException instantiationException;
35    private boolean useEngineSocket = useEngineSocketByDefault;
36
37    public OpenSSLServerSocketFactoryImpl() {
38        try {
39            this.sslParameters = SSLParametersImpl.getDefault();
40            this.sslParameters.setUseClientMode(false);
41        } catch (KeyManagementException e) {
42            instantiationException =
43                new IOException("Delayed instantiation exception:");
44            instantiationException.initCause(e);
45        }
46    }
47
48    public OpenSSLServerSocketFactoryImpl(SSLParametersImpl sslParameters) {
49        this.sslParameters = (SSLParametersImpl) sslParameters.clone();
50        this.sslParameters.setUseClientMode(false);
51    }
52
53    /**
54     * Configures the default socket to be created for all instances.
55     */
56    public static void setUseEngineSocketByDefault(boolean useEngineSocket) {
57        useEngineSocketByDefault = useEngineSocket;
58    }
59
60    /**
61     * Configures the socket to be created for this instance. If not called,
62     * {@link #useEngineSocketByDefault} will be used.
63     */
64    public void setUseEngineSocket(boolean useEngineSocket) {
65        this.useEngineSocket = useEngineSocket;
66    }
67
68    @Override
69    public String[] getDefaultCipherSuites() {
70        return sslParameters.getEnabledCipherSuites();
71    }
72
73    @Override
74    public String[] getSupportedCipherSuites() {
75        return NativeCrypto.getSupportedCipherSuites();
76    }
77
78    @Override
79    public ServerSocket createServerSocket() throws IOException {
80        return new OpenSSLServerSocketImpl((SSLParametersImpl) sslParameters.clone())
81                .setUseEngineSocket(useEngineSocket);
82    }
83
84    @Override
85    public ServerSocket createServerSocket(int port) throws IOException {
86        return new OpenSSLServerSocketImpl(port, (SSLParametersImpl) sslParameters.clone())
87                .setUseEngineSocket(useEngineSocket);
88    }
89
90    @Override
91    public ServerSocket createServerSocket(int port, int backlog) throws IOException {
92        return new OpenSSLServerSocketImpl(port, backlog, (SSLParametersImpl) sslParameters.clone())
93                .setUseEngineSocket(useEngineSocket);
94    }
95
96    @Override
97    public ServerSocket createServerSocket(int port, int backlog, InetAddress iAddress)
98            throws IOException {
99        return new OpenSSLServerSocketImpl(
100                port, backlog, iAddress, (SSLParametersImpl) sslParameters.clone())
101                .setUseEngineSocket(useEngineSocket);
102    }
103}
104