Path: blob/master/src/java.base/share/classes/javax/net/ssl/SSLSocketFactory.java
41159 views
/*1* Copyright (c) 1997, 2021, 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.net.*;29import javax.net.SocketFactory;30import java.io.IOException;31import java.io.InputStream;32import java.security.*;33import java.util.Locale;3435import sun.security.action.GetPropertyAction;3637/**38* <code>SSLSocketFactory</code>s create <code>SSLSocket</code>s.39*40* @since 1.441* @see SSLSocket42* @author David Brownell43*/44public abstract class SSLSocketFactory extends SocketFactory {45static final boolean DEBUG;4647static {48String s = GetPropertyAction.privilegedGetProperty(49"javax.net.debug", "").toLowerCase(Locale.ENGLISH);50DEBUG = s.contains("all") || s.contains("ssl");51}5253/**54* Constructor is used only by subclasses.55*/56public SSLSocketFactory() {57// blank58}5960/**61* Returns the default SSL socket factory.62*63* <p>The first time this method is called, the security property64* "ssl.SocketFactory.provider" is examined. If it is non-null, a class by65* that name is loaded and instantiated. If that is successful and the66* object is an instance of SSLSocketFactory, it is made the default SSL67* socket factory.68*69* <p>Otherwise, this method returns70* <code>SSLContext.getDefault().getSocketFactory()</code>. If that71* call fails, an inoperative factory is returned.72*73* @return the default <code>SocketFactory</code>74* @see SSLContext#getDefault75*/76public static SocketFactory getDefault() {77if (DefaultFactoryHolder.defaultFactory != null) {78return DefaultFactoryHolder.defaultFactory;79}8081try {82return SSLContext.getDefault().getSocketFactory();83} catch (NoSuchAlgorithmException | UnsupportedOperationException e) {84return new DefaultSSLSocketFactory(e);85}86}8788@SuppressWarnings("removal")89static String getSecurityProperty(final String name) {90return AccessController.doPrivileged(new PrivilegedAction<>() {91@Override92public String run() {93String s = java.security.Security.getProperty(name);94if (s != null) {95s = s.trim();96if (s.isEmpty()) {97s = null;98}99}100return s;101}102});103}104105/**106* Returns the list of cipher suites which are enabled by default.107* Unless a different list is enabled, handshaking on an SSL connection108* will use one of these cipher suites. The minimum quality of service109* for these defaults requires confidentiality protection and server110* authentication (that is, no anonymous cipher suites).111* <P>112* The returned array includes cipher suites from the list of standard113* cipher suite names in the <a href=114* "{@docRoot}/../specs/security/standard-names.html#jsse-cipher-suite-names">115* JSSE Cipher Suite Names</a> section of the Java Cryptography116* Architecture Standard Algorithm Name Documentation, and may also117* include other cipher suites that the provider supports.118*119* @see #getSupportedCipherSuites()120* @return array of the cipher suites enabled by default121*/122public abstract String [] getDefaultCipherSuites();123124/**125* Returns the names of the cipher suites which could be enabled for use126* on an SSL connection. Normally, only a subset of these will actually127* be enabled by default, since this list may include cipher suites which128* do not meet quality of service requirements for those defaults. Such129* cipher suites are useful in specialized applications.130* <P>131* The returned array includes cipher suites from the list of standard132* cipher suite names in the <a href=133* "{@docRoot}/../specs/security/standard-names.html#jsse-cipher-suite-names">134* JSSE Cipher Suite Names</a> section of the Java Cryptography135* Architecture Standard Algorithm Name Documentation, and may also136* include other cipher suites that the provider supports.137*138* @see #getDefaultCipherSuites()139* @return an array of cipher suite names140*/141public abstract String [] getSupportedCipherSuites();142143/**144* Returns a socket layered over an existing socket connected to the named145* host, at the given port. This constructor can be used when tunneling SSL146* through a proxy or when negotiating the use of SSL over an existing147* socket. The host and port refer to the logical peer destination.148* This socket is configured using the socket options established for149* this factory.150*151* @param s the existing socket152* @param host the server host153* @param port the server port154* @param autoClose close the underlying socket when this socket is closed155* @return a socket connected to the specified host and port156* @throws IOException if an I/O error occurs when creating the socket157* @throws NullPointerException if the parameter s is null158*/159public abstract Socket createSocket(Socket s, String host,160int port, boolean autoClose) throws IOException;161162/**163* Creates a server mode {@link Socket} layered over an164* existing connected socket, and is able to read data which has165* already been consumed/removed from the {@link Socket}'s166* underlying {@link InputStream}.167* <p>168* This method can be used by a server application that needs to169* observe the inbound data but still create valid SSL/TLS170* connections: for example, inspection of Server Name Indication171* (SNI) extensions (See section 3 of <A172* HREF="http://www.ietf.org/rfc/rfc6066.txt">TLS Extensions173* (RFC6066)</A>). Data that has been already removed from the174* underlying {@link InputStream} should be loaded into the175* {@code consumed} stream before this method is called, perhaps176* using a {@link java.io.ByteArrayInputStream}. When this177* {@link Socket} begins handshaking, it will read all of the data in178* {@code consumed} until it reaches {@code EOF}, then all further179* data is read from the underlying {@link InputStream} as180* usual.181* <p>182* The returned socket is configured using the socket options183* established for this factory, and is set to use server mode when184* handshaking (see {@link SSLSocket#setUseClientMode(boolean)}).185*186* @param s187* the existing socket188* @param consumed189* the consumed inbound network data that has already been190* removed from the existing {@link Socket}191* {@link InputStream}. This parameter may be192* {@code null} if no data has been removed.193* @param autoClose close the underlying socket when this socket is closed.194*195* @return the {@link Socket} compliant with the socket options196* established for this factory197*198* @throws IOException if an I/O error occurs when creating the socket199* @throws UnsupportedOperationException if the underlying provider200* does not implement the operation201* @throws NullPointerException if {@code s} is {@code null}202*203* @since 1.8204*/205public Socket createSocket(Socket s, InputStream consumed,206boolean autoClose) throws IOException {207throw new UnsupportedOperationException();208}209210// lazy initialization holder class idiom for static default factory211//212// See Effective Java Second Edition: Item 71.213private static final class DefaultFactoryHolder {214private static final SSLSocketFactory defaultFactory;215216static {217SSLSocketFactory mediator = null;218String clsName = getSecurityProperty("ssl.SocketFactory.provider");219if (clsName != null) {220log("setting up default SSLSocketFactory");221try {222Class<?> cls = null;223try {224cls = Class.forName(clsName);225} catch (ClassNotFoundException e) {226ClassLoader cl = ClassLoader.getSystemClassLoader();227if (cl != null) {228cls = cl.loadClass(clsName);229}230}231log("class " + clsName + " is loaded");232233mediator = (SSLSocketFactory)cls234.getDeclaredConstructor().newInstance();235236log("instantiated an instance of class " + clsName);237} catch (Exception e) {238log("SSLSocketFactory instantiation failed: " + e);239mediator = new DefaultSSLSocketFactory(e);240}241}242243defaultFactory = mediator;244}245246private static void log(String msg) {247if (DEBUG) {248System.out.println(msg);249}250}251}252}253254255// file private256class DefaultSSLSocketFactory extends SSLSocketFactory257{258private Exception reason;259260DefaultSSLSocketFactory(Exception reason) {261this.reason = reason;262}263264private Socket throwException() throws SocketException {265throw (SocketException)266new SocketException(reason.toString()).initCause(reason);267}268269@Override270public Socket createSocket()271throws IOException272{273return throwException();274}275276@Override277public Socket createSocket(String host, int port)278throws IOException279{280return throwException();281}282283@Override284public Socket createSocket(Socket s, String host,285int port, boolean autoClose)286throws IOException287{288return throwException();289}290291@Override292public Socket createSocket(InetAddress address, int port)293throws IOException294{295return throwException();296}297298@Override299public Socket createSocket(String host, int port,300InetAddress clientAddress, int clientPort)301throws IOException302{303return throwException();304}305306@Override307public Socket createSocket(InetAddress address, int port,308InetAddress clientAddress, int clientPort)309throws IOException310{311return throwException();312}313314@Override315public String [] getDefaultCipherSuites() {316return new String[0];317}318319@Override320public String [] getSupportedCipherSuites() {321return new String[0];322}323}324325326