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.conscrypt;
19
20import java.io.IOException;
21import java.net.InetAddress;
22import java.net.ServerSocket;
23import java.security.KeyManagementException;
24import javax.net.ssl.SSLServerSocketFactory;
25import org.conscrypt.util.EmptyArray;
26
27/**
28 * Implementation of SSLServerSocketFactory.
29 */
30public class SSLServerSocketFactoryImpl extends SSLServerSocketFactory {
31
32    private SSLParametersImpl sslParameters;
33    private IOException instantiationException;
34
35    /**
36     * Constructor.
37     */
38    public SSLServerSocketFactoryImpl() {
39        try {
40            this.sslParameters = SSLParametersImpl.getDefault();
41            this.sslParameters.setUseClientMode(false);
42        } catch (KeyManagementException e) {
43            instantiationException =
44                new IOException("Delayed instantiation exception:");
45            instantiationException.initCause(e);
46        }
47    }
48
49    /**
50     * Constructor.
51     */
52    protected SSLServerSocketFactoryImpl(SSLParametersImpl sslParameters) {
53        this.sslParameters = (SSLParametersImpl) sslParameters.clone();
54        this.sslParameters.setUseClientMode(false);
55    }
56
57    /**
58     * @see javax.net.ssl.SSLServerSocketFactory#getDefaultCipherSuites()
59     */
60    @Override
61    public String[] getDefaultCipherSuites() {
62        if (instantiationException != null) {
63            return EmptyArray.STRING;
64        }
65        return sslParameters.getEnabledCipherSuites();
66    }
67
68    /**
69     * @see javax.net.ssl.SSLServerSocketFactory#getSupportedCipherSuites()
70     */
71    @Override
72    public String[] getSupportedCipherSuites() {
73        if (instantiationException != null) {
74            return EmptyArray.STRING;
75        }
76        return CipherSuite.getSupportedCipherSuiteNames();
77    }
78
79    /**
80     * @see javax.net.ServerSocketFactory#createServerSocket()
81     */
82    @Override
83    public ServerSocket createServerSocket() throws IOException {
84        if (instantiationException != null) {
85            throw instantiationException;
86        }
87        return new SSLServerSocketImpl((SSLParametersImpl) sslParameters.clone());
88    }
89
90
91    /**
92     * @see javax.net.ServerSocketFactory#createServerSocket(int)
93     */
94    @Override
95    public ServerSocket createServerSocket(int port) throws IOException {
96        if (instantiationException != null) {
97            throw instantiationException;
98        }
99        return new SSLServerSocketImpl(port,
100                (SSLParametersImpl) sslParameters.clone());
101    }
102
103    /**
104     * @see javax.net.ServerSocketFactory#createServerSocket(int,int)
105     */
106    @Override
107    public ServerSocket createServerSocket(int port, int backlog)
108            throws IOException {
109        if (instantiationException != null) {
110            throw instantiationException;
111        }
112        return new SSLServerSocketImpl(port, backlog,
113                (SSLParametersImpl) sslParameters.clone());
114    }
115
116    /**
117     * @see javax.net.ServerSocketFactory#createServerSocket(int,int,InetAddress)
118     */
119    @Override
120    public ServerSocket createServerSocket(int port, int backlog,
121            InetAddress iAddress) throws IOException {
122        if (instantiationException != null) {
123            throw instantiationException;
124        }
125        return new SSLServerSocketImpl(port, backlog, iAddress,
126                (SSLParametersImpl) sslParameters.clone());
127    }
128}
129