Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/net/ssl/SSLServerSocket.java
38918 views
/*1* Copyright (c) 1997, 2012, 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.*;29import java.net.*;303132/**33* This class extends <code>ServerSocket</code>s and34* provides secure server sockets using protocols such as the Secure35* Sockets Layer (SSL) or Transport Layer Security (TLS) protocols.36* <P>37* Instances of this class are generally created using a38* <code>SSLServerSocketFactory</code>. The primary function39* of <code>SSLServerSocket</code>s40* is to create <code>SSLSocket</code>s by <code>accept</code>ing41* connections.42* <P>43* <code>SSLServerSocket</code>s contain several pieces of state data44* which are inherited by the <code>SSLSocket</code> at45* socket creation. These include the enabled cipher46* suites and protocols, whether client47* authentication is necessary, and whether created sockets should48* begin handshaking in client or server mode. The state49* inherited by the created <code>SSLSocket</code> can be50* overriden by calling the appropriate methods.51*52* @see java.net.ServerSocket53* @see SSLSocket54*55* @since 1.456* @author David Brownell57*/58public abstract class SSLServerSocket extends ServerSocket {5960/**61* Used only by subclasses.62* <P>63* Create an unbound TCP server socket using the default authentication64* context.65*66* @throws IOException if an I/O error occurs when creating the socket67*/68protected SSLServerSocket()69throws IOException70{ super(); }717273/**74* Used only by subclasses.75* <P>76* Create a TCP server socket on a port, using the default77* authentication context. The connection backlog defaults to78* fifty connections queued up before the system starts to79* reject new connection requests.80* <P>81* A port number of <code>0</code> creates a socket on any free port.82* <P>83* If there is a security manager, its <code>checkListen</code>84* method is called with the <code>port</code> argument as its85* argument to ensure the operation is allowed. This could result86* in a SecurityException.87*88* @param port the port on which to listen89* @throws IOException if an I/O error occurs when creating the socket90* @throws SecurityException if a security manager exists and its91* <code>checkListen</code> method doesn't allow the operation.92* @throws IllegalArgumentException if the port parameter is outside the93* specified range of valid port values, which is between 0 and94* 65535, inclusive.95* @see SecurityManager#checkListen96*/97protected SSLServerSocket(int port)98throws IOException99{ super(port); }100101102/**103* Used only by subclasses.104* <P>105* Create a TCP server socket on a port, using the default106* authentication context and a specified backlog of connections.107* <P>108* A port number of <code>0</code> creates a socket on any free port.109* <P>110* The <code>backlog</code> argument is the requested maximum number of111* pending connections on the socket. Its exact semantics are implementation112* specific. In particular, an implementation may impose a maximum length113* or may choose to ignore the parameter altogther. The value provided114* should be greater than <code>0</code>. If it is less than or equal to115* <code>0</code>, then an implementation specific default will be used.116* <P>117* If there is a security manager, its <code>checkListen</code>118* method is called with the <code>port</code> argument as its119* argument to ensure the operation is allowed. This could result120* in a SecurityException.121*122* @param port the port on which to listen123* @param backlog requested maximum length of the queue of incoming124* connections.125* @throws IOException if an I/O error occurs when creating the socket126* @throws SecurityException if a security manager exists and its127* <code>checkListen</code> method doesn't allow the operation.128* @throws IllegalArgumentException if the port parameter is outside the129* specified range of valid port values, which is between 0 and130* 65535, inclusive.131* @see SecurityManager#checkListen132*/133protected SSLServerSocket(int port, int backlog)134throws IOException135{ super(port, backlog); }136137138/**139* Used only by subclasses.140* <P>141* Create a TCP server socket on a port, using the default142* authentication context and a specified backlog of connections143* as well as a particular specified network interface. This144* constructor is used on multihomed hosts, such as those used145* for firewalls or as routers, to control through which interface146* a network service is provided.147* <P>148* If there is a security manager, its <code>checkListen</code>149* method is called with the <code>port</code> argument as its150* argument to ensure the operation is allowed. This could result151* in a SecurityException.152* <P>153* A port number of <code>0</code> creates a socket on any free port.154* <P>155* The <code>backlog</code> argument is the requested maximum number of156* pending connections on the socket. Its exact semantics are implementation157* specific. In particular, an implementation may impose a maximum length158* or may choose to ignore the parameter altogther. The value provided159* should be greater than <code>0</code>. If it is less than or equal to160* <code>0</code>, then an implementation specific default will be used.161* <P>162* If <i>address</i> is null, it will default accepting connections163* on any/all local addresses.164*165* @param port the port on which to listen166* @param backlog requested maximum length of the queue of incoming167* connections.168* @param address the address of the network interface through169* which connections will be accepted170* @throws IOException if an I/O error occurs when creating the socket171* @throws SecurityException if a security manager exists and its172* <code>checkListen</code> method doesn't allow the operation.173* @throws IllegalArgumentException if the port parameter is outside the174* specified range of valid port values, which is between 0 and175* 65535, inclusive.176* @see SecurityManager#checkListen177*/178protected SSLServerSocket(int port, int backlog, InetAddress address)179throws IOException180{ super(port, backlog, address); }181182183184/**185* Returns the list of cipher suites which are currently enabled186* for use by newly accepted connections.187* <P>188* If this list has not been explicitly modified, a system-provided189* default guarantees a minimum quality of service in all enabled190* cipher suites.191* <P>192* There are several reasons why an enabled cipher suite might193* not actually be used. For example: the server socket might194* not have appropriate private keys available to it or the cipher195* suite might be anonymous, precluding the use of client authentication,196* while the server socket has been told to require that sort of197* authentication.198*199* @return an array of cipher suites enabled200* @see #getSupportedCipherSuites()201* @see #setEnabledCipherSuites(String [])202*/203public abstract String [] getEnabledCipherSuites();204205206/**207* Sets the cipher suites enabled for use by accepted connections.208* <P>209* The cipher suites must have been listed by getSupportedCipherSuites()210* as being supported. Following a successful call to this method,211* only suites listed in the <code>suites</code> parameter are enabled212* for use.213* <P>214* Suites that require authentication information which is not available215* in this ServerSocket's authentication context will not be used216* in any case, even if they are enabled.217* <P>218* <code>SSLSocket</code>s returned from <code>accept()</code>219* inherit this setting.220*221* @param suites Names of all the cipher suites to enable222* @exception IllegalArgumentException when one or more of ciphers223* named by the parameter is not supported, or when224* the parameter is null.225* @see #getSupportedCipherSuites()226* @see #getEnabledCipherSuites()227*/228public abstract void setEnabledCipherSuites(String suites []);229230231/**232* Returns the names of the cipher suites which could be enabled for use233* on an SSL connection.234* <P>235* Normally, only a subset of these will actually236* be enabled by default, since this list may include cipher suites which237* do not meet quality of service requirements for those defaults. Such238* cipher suites are useful in specialized applications.239*240* @return an array of cipher suite names241* @see #getEnabledCipherSuites()242* @see #setEnabledCipherSuites(String [])243*/244public abstract String [] getSupportedCipherSuites();245246247/**248* Returns the names of the protocols which could be enabled for use.249*250* @return an array of protocol names supported251* @see #getEnabledProtocols()252* @see #setEnabledProtocols(String [])253*/254public abstract String [] getSupportedProtocols();255256257/**258* Returns the names of the protocols which are currently259* enabled for use by the newly accepted connections.260*261* @return an array of protocol names262* @see #getSupportedProtocols()263* @see #setEnabledProtocols(String [])264*/265public abstract String [] getEnabledProtocols();266267268/**269* Controls which particular protocols are enabled for use by270* accepted connections.271* <P>272* The protocols must have been listed by273* getSupportedProtocols() as being supported.274* Following a successful call to this method, only protocols listed275* in the <code>protocols</code> parameter are enabled for use.276* <P>277* <code>SSLSocket</code>s returned from <code>accept()</code>278* inherit this setting.279*280* @param protocols Names of all the protocols to enable.281* @exception IllegalArgumentException when one or more of282* the protocols named by the parameter is not supported or283* when the protocols parameter is null.284* @see #getEnabledProtocols()285* @see #getSupportedProtocols()286*/287public abstract void setEnabledProtocols(String protocols[]);288289290/**291* Controls whether <code>accept</code>ed server-mode292* <code>SSLSockets</code> will be initially configured to293* <i>require</i> client authentication.294* <P>295* A socket's client authentication setting is one of the following:296* <ul>297* <li> client authentication required298* <li> client authentication requested299* <li> no client authentication desired300* </ul>301* <P>302* Unlike {@link #setWantClientAuth(boolean)}, if the accepted303* socket's option is set and the client chooses not to provide304* authentication information about itself, <i>the negotiations305* will stop and the connection will be dropped</i>.306* <P>307* Calling this method overrides any previous setting made by308* this method or {@link #setWantClientAuth(boolean)}.309* <P>310* The initial inherited setting may be overridden by calling311* {@link SSLSocket#setNeedClientAuth(boolean)} or312* {@link SSLSocket#setWantClientAuth(boolean)}.313*314* @param need set to true if client authentication is required,315* or false if no client authentication is desired.316* @see #getNeedClientAuth()317* @see #setWantClientAuth(boolean)318* @see #getWantClientAuth()319* @see #setUseClientMode(boolean)320*/321public abstract void setNeedClientAuth(boolean need);322323324/**325* Returns true if client authentication will be <i>required</i> on326* newly <code>accept</code>ed server-mode <code>SSLSocket</code>s.327* <P>328* The initial inherited setting may be overridden by calling329* {@link SSLSocket#setNeedClientAuth(boolean)} or330* {@link SSLSocket#setWantClientAuth(boolean)}.331*332* @return true if client authentication is required,333* or false if no client authentication is desired.334* @see #setNeedClientAuth(boolean)335* @see #setWantClientAuth(boolean)336* @see #getWantClientAuth()337* @see #setUseClientMode(boolean)338*/339public abstract boolean getNeedClientAuth();340341342/**343* Controls whether <code>accept</code>ed server-mode344* <code>SSLSockets</code> will be initially configured to345* <i>request</i> client authentication.346* <P>347* A socket's client authentication setting is one of the following:348* <ul>349* <li> client authentication required350* <li> client authentication requested351* <li> no client authentication desired352* </ul>353* <P>354* Unlike {@link #setNeedClientAuth(boolean)}, if the accepted355* socket's option is set and the client chooses not to provide356* authentication information about itself, <i>the negotiations357* will continue</i>.358* <P>359* Calling this method overrides any previous setting made by360* this method or {@link #setNeedClientAuth(boolean)}.361* <P>362* The initial inherited setting may be overridden by calling363* {@link SSLSocket#setNeedClientAuth(boolean)} or364* {@link SSLSocket#setWantClientAuth(boolean)}.365*366* @param want set to true if client authentication is requested,367* or false if no client authentication is desired.368* @see #getWantClientAuth()369* @see #setNeedClientAuth(boolean)370* @see #getNeedClientAuth()371* @see #setUseClientMode(boolean)372*/373public abstract void setWantClientAuth(boolean want);374375376/**377* Returns true if client authentication will be <i>requested</i> on378* newly accepted server-mode connections.379* <P>380* The initial inherited setting may be overridden by calling381* {@link SSLSocket#setNeedClientAuth(boolean)} or382* {@link SSLSocket#setWantClientAuth(boolean)}.383*384* @return true if client authentication is requested,385* or false if no client authentication is desired.386* @see #setWantClientAuth(boolean)387* @see #setNeedClientAuth(boolean)388* @see #getNeedClientAuth()389* @see #setUseClientMode(boolean)390*/391public abstract boolean getWantClientAuth();392393394/**395* Controls whether accepted connections are in the (default) SSL396* server mode, or the SSL client mode.397* <P>398* Servers normally authenticate themselves, and clients are not399* required to do so.400* <P>401* In rare cases, TCP servers402* need to act in the SSL client mode on newly accepted403* connections. For example, FTP clients acquire server sockets404* and listen there for reverse connections from the server. An405* FTP client would use an SSLServerSocket in "client" mode to406* accept the reverse connection while the FTP server uses an407* SSLSocket with "client" mode disabled to initiate the408* connection. During the resulting handshake, existing SSL409* sessions may be reused.410* <P>411* <code>SSLSocket</code>s returned from <code>accept()</code>412* inherit this setting.413*414* @param mode true if newly accepted connections should use SSL415* client mode.416* @see #getUseClientMode()417*/418public abstract void setUseClientMode(boolean mode);419420421/**422* Returns true if accepted connections will be in SSL client mode.423*424* @see #setUseClientMode(boolean)425* @return true if the connection should use SSL client mode.426*/427public abstract boolean getUseClientMode();428429430/**431* Controls whether new SSL sessions may be established by the432* sockets which are created from this server socket.433* <P>434* <code>SSLSocket</code>s returned from <code>accept()</code>435* inherit this setting.436*437* @param flag true indicates that sessions may be created; this438* is the default. false indicates that an existing session439* must be resumed.440* @see #getEnableSessionCreation()441*/442public abstract void setEnableSessionCreation(boolean flag);443444445/**446* Returns true if new SSL sessions may be established by the447* sockets which are created from this server socket.448*449* @return true indicates that sessions may be created; this450* is the default. false indicates that an existing451* session must be resumed452* @see #setEnableSessionCreation(boolean)453*/454public abstract boolean getEnableSessionCreation();455456/**457* Returns the SSLParameters in effect for newly accepted connections.458* The ciphersuites and protocols of the returned SSLParameters459* are always non-null.460*461* @return the SSLParameters in effect for newly accepted connections462*463* @see #setSSLParameters(SSLParameters)464*465* @since 1.7466*/467public SSLParameters getSSLParameters() {468SSLParameters parameters = new SSLParameters();469470parameters.setCipherSuites(getEnabledCipherSuites());471parameters.setProtocols(getEnabledProtocols());472if (getNeedClientAuth()) {473parameters.setNeedClientAuth(true);474} else if (getWantClientAuth()) {475parameters.setWantClientAuth(true);476}477478return parameters;479}480481/**482* Applies SSLParameters to newly accepted connections.483*484* <p>This means:485* <ul>486* <li>If {@code params.getCipherSuites()} is non-null,487* {@code setEnabledCipherSuites()} is called with that value.</li>488* <li>If {@code params.getProtocols()} is non-null,489* {@code setEnabledProtocols()} is called with that value.</li>490* <li>If {@code params.getNeedClientAuth()} or491* {@code params.getWantClientAuth()} return {@code true},492* {@code setNeedClientAuth(true)} and493* {@code setWantClientAuth(true)} are called, respectively;494* otherwise {@code setWantClientAuth(false)} is called.</li>495* <li>If {@code params.getServerNames()} is non-null, the socket will496* configure its server names with that value.</li>497* <li>If {@code params.getSNIMatchers()} is non-null, the socket will498* configure its SNI matchers with that value.</li>499* </ul>500*501* @param params the parameters502* @throws IllegalArgumentException if the setEnabledCipherSuites() or503* the setEnabledProtocols() call fails504*505* @see #getSSLParameters()506*507* @since 1.7508*/509public void setSSLParameters(SSLParameters params) {510String[] s;511s = params.getCipherSuites();512if (s != null) {513setEnabledCipherSuites(s);514}515516s = params.getProtocols();517if (s != null) {518setEnabledProtocols(s);519}520521if (params.getNeedClientAuth()) {522setNeedClientAuth(true);523} else if (params.getWantClientAuth()) {524setWantClientAuth(true);525} else {526setWantClientAuth(false);527}528}529530}531532533