Path: blob/master/src/java.base/share/classes/javax/net/ssl/SSLServerSocketFactory.java
41159 views
/*1* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package javax.net.ssl;2728import java.io.IOException;29import java.net.InetAddress;30import java.net.ServerSocket;31import java.net.SocketException;32import javax.net.ServerSocketFactory;33import java.security.*;3435/**36* <code>SSLServerSocketFactory</code>s create37* <code>SSLServerSocket</code>s.38*39* @since 1.440* @see SSLSocket41* @see SSLServerSocket42* @author David Brownell43*/44public abstract class SSLServerSocketFactory extends ServerSocketFactory {4546/**47* Constructor is used only by subclasses.48*/49protected SSLServerSocketFactory() { /* NOTHING */ }5051/**52* Returns the default SSL server socket factory.53*54* <p>The first time this method is called, the security property55* "ssl.ServerSocketFactory.provider" is examined. If it is non-null, a56* class by that name is loaded and instantiated. If that is successful and57* the object is an instance of SSLServerSocketFactory, it is made the58* default SSL server socket factory.59*60* <p>Otherwise, this method returns61* <code>SSLContext.getDefault().getServerSocketFactory()</code>. If that62* call fails, an inoperative factory is returned.63*64* @return the default <code>ServerSocketFactory</code>65* @see SSLContext#getDefault66*/67public static ServerSocketFactory getDefault() {68if (DefaultFactoryHolder.defaultFactory != null) {69return DefaultFactoryHolder.defaultFactory;70}7172try {73return SSLContext.getDefault().getServerSocketFactory();74} catch (NoSuchAlgorithmException | UnsupportedOperationException e) {75return new DefaultSSLServerSocketFactory(e);76}77}7879/**80* Returns the list of cipher suites which are enabled by default.81* Unless a different list is enabled, handshaking on an SSL connection82* will use one of these cipher suites. The minimum quality of service83* for these defaults requires confidentiality protection and server84* authentication (that is, no anonymous cipher suites).85* <P>86* The returned array includes cipher suites from the list of standard87* cipher suite names in the <a href=88* "{@docRoot}/../specs/security/standard-names.html#jsse-cipher-suite-names">89* JSSE Cipher Suite Names</a> section of the Java Cryptography90* Architecture Standard Algorithm Name Documentation, and may also91* include other cipher suites that the provider supports.92*93* @see #getSupportedCipherSuites()94* @return array of the cipher suites enabled by default95*/96public abstract String [] getDefaultCipherSuites();979899/**100* Returns the names of the cipher suites which could be enabled for use101* on an SSL connection created by this factory.102* Normally, only a subset of these will actually103* be enabled by default, since this list may include cipher suites which104* do not meet quality of service requirements for those defaults. Such105* cipher suites are useful in specialized applications.106* <P>107* The returned array includes cipher suites from the list of standard108* cipher suite names in the <a href=109* "{@docRoot}/../specs/security/standard-names.html#jsse-cipher-suite-names">110* JSSE Cipher Suite Names</a> section of the Java Cryptography111* Architecture Standard Algorithm Name Documentation, and may also112* include other cipher suites that the provider supports.113*114* @return an array of cipher suite names115* @see #getDefaultCipherSuites()116*/117public abstract String [] getSupportedCipherSuites();118119// lazy initialization holder class idiom for static default factory120//121// See Effective Java Second Edition: Item 71.122private static final class DefaultFactoryHolder {123private static final SSLServerSocketFactory defaultFactory;124125static {126SSLServerSocketFactory mediator = null;127String clsName = SSLSocketFactory.getSecurityProperty(128"ssl.ServerSocketFactory.provider");129if (clsName != null) {130log("setting up default SSLServerSocketFactory");131try {132Class<?> cls = null;133try {134cls = Class.forName(clsName);135} catch (ClassNotFoundException e) {136ClassLoader cl = ClassLoader.getSystemClassLoader();137if (cl != null) {138cls = cl.loadClass(clsName);139}140}141log("class " + clsName + " is loaded");142143mediator = (SSLServerSocketFactory)cls144.getDeclaredConstructor().newInstance();145log("instantiated an instance of class " + clsName);146} catch (Exception e) {147log("SSLServerSocketFactory instantiation failed: " + e);148mediator = new DefaultSSLServerSocketFactory(e);149}150}151152defaultFactory = mediator;153}154155private static void log(String msg) {156if (SSLSocketFactory.DEBUG) {157System.out.println(msg);158}159}160}161}162163//164// The default factory does NOTHING.165//166class DefaultSSLServerSocketFactory extends SSLServerSocketFactory {167168private final Exception reason;169170DefaultSSLServerSocketFactory(Exception reason) {171this.reason = reason;172}173174private ServerSocket throwException() throws SocketException {175throw (SocketException)176new SocketException(reason.toString()).initCause(reason);177}178179@Override180public ServerSocket createServerSocket() throws IOException {181return throwException();182}183184185@Override186public ServerSocket createServerSocket(int port)187throws IOException188{189return throwException();190}191192@Override193public ServerSocket createServerSocket(int port, int backlog)194throws IOException195{196return throwException();197}198199@Override200public ServerSocket201createServerSocket(int port, int backlog, InetAddress ifAddress)202throws IOException203{204return throwException();205}206207@Override208public String [] getDefaultCipherSuites() {209return new String[0];210}211212@Override213public String [] getSupportedCipherSuites() {214return new String[0];215}216}217218219