Path: blob/master/src/java.base/share/classes/javax/net/ssl/SSLServerSocket.java
41159 views
/*1* Copyright (c) 1997, 2018, 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> 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 an38* <code>SSLServerSocketFactory</code>. The primary function39* of an <code>SSLServerSocket</code>40* is to create <code>SSLSocket</code>s by <code>accept</code>ing41* connections.42* <P>43* An <code>SSLServerSocket</code> contains 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* overridden 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* Note that even if a suite is enabled, it may never be used. This193* can occur if the peer does not support it, or its use is restricted,194* or the requisite certificates (and private keys) for the suite are195* not available, or an anonymous suite is enabled but authentication196* is required.197* <P>198* The returned array includes cipher suites from the list of standard199* cipher suite names in the <a href=200* "{@docRoot}/../specs/security/standard-names.html#jsse-cipher-suite-names">201* JSSE Cipher Suite Names</a> section of the Java Cryptography202* Architecture Standard Algorithm Name Documentation, and may also203* include other cipher suites that the provider supports.204*205* @return an array of cipher suites enabled206* @see #getSupportedCipherSuites()207* @see #setEnabledCipherSuites(String [])208*/209public abstract String [] getEnabledCipherSuites();210211212/**213* Sets the cipher suites enabled for use by accepted connections.214* <P>215* The cipher suites must have been listed by getSupportedCipherSuites()216* as being supported. Following a successful call to this method,217* only suites listed in the <code>suites</code> parameter are enabled218* for use.219* <P>220* Suites that require authentication information which is not available221* in this ServerSocket's authentication context will not be used222* in any case, even if they are enabled.223* <P>224* Note that the standard list of cipher suite names may be found in the225* <a href=226* "{@docRoot}/../specs/security/standard-names.html#jsse-cipher-suite-names">227* JSSE Cipher Suite Names</a> section of the Java Cryptography228* Architecture Standard Algorithm Name Documentation. Providers229* may support cipher suite names not found in this list or might not230* use the recommended name for a certain cipher suite.231* <P>232* <code>SSLSocket</code>s returned from <code>accept()</code>233* inherit this setting.234*235* @param suites Names of all the cipher suites to enable236* @exception IllegalArgumentException when one or more of ciphers237* named by the parameter is not supported, or when238* the parameter is null.239* @see #getSupportedCipherSuites()240* @see #getEnabledCipherSuites()241*/242public abstract void setEnabledCipherSuites(String suites []);243244245/**246* Returns the names of the cipher suites which could be enabled for use247* on an SSL connection.248* <P>249* Normally, only a subset of these will actually250* be enabled by default, since this list may include cipher suites which251* do not meet quality of service requirements for those defaults. Such252* cipher suites are useful in specialized applications.253* <P>254* The returned array includes cipher suites from the list of standard255* cipher suite names in the <a href=256* "{@docRoot}/../specs/security/standard-names.html#jsse-cipher-suite-names">257* JSSE Cipher Suite Names</a> section of the Java Cryptography258* Architecture Standard Algorithm Name Documentation, and may also259* include other cipher suites that the provider supports.260*261* @return an array of cipher suite names262* @see #getEnabledCipherSuites()263* @see #setEnabledCipherSuites(String [])264*/265public abstract String [] getSupportedCipherSuites();266267268/**269* Returns the names of the protocols which could be enabled for use.270*271* @return an array of protocol names supported272* @see #getEnabledProtocols()273* @see #setEnabledProtocols(String [])274*/275public abstract String [] getSupportedProtocols();276277278/**279* Returns the names of the protocols which are currently280* enabled for use by the newly accepted connections.281* <P>282* Note that even if a protocol is enabled, it may never be used.283* This can occur if the peer does not support the protocol, or its284* use is restricted, or there are no enabled cipher suites supported285* by the protocol.286*287* @return an array of protocol names288* @see #getSupportedProtocols()289* @see #setEnabledProtocols(String [])290*/291public abstract String [] getEnabledProtocols();292293294/**295* Controls which particular protocols are enabled for use by296* accepted connections.297* <P>298* The protocols must have been listed by299* getSupportedProtocols() as being supported.300* Following a successful call to this method, only protocols listed301* in the <code>protocols</code> parameter are enabled for use.302* <P>303* <code>SSLSocket</code>s returned from <code>accept()</code>304* inherit this setting.305*306* @param protocols Names of all the protocols to enable.307* @exception IllegalArgumentException when one or more of308* the protocols named by the parameter is not supported or309* when the protocols parameter is null.310* @see #getEnabledProtocols()311* @see #getSupportedProtocols()312*/313public abstract void setEnabledProtocols(String protocols[]);314315316/**317* Controls whether <code>accept</code>ed server-mode318* <code>SSLSockets</code> will be initially configured to319* <i>require</i> client authentication.320* <P>321* A socket's client authentication setting is one of the following:322* <ul>323* <li> client authentication required324* <li> client authentication requested325* <li> no client authentication desired326* </ul>327* <P>328* Unlike {@link #setWantClientAuth(boolean)}, if the accepted329* socket's option is set and the client chooses not to provide330* authentication information about itself, <i>the negotiations331* will stop and the connection will be dropped</i>.332* <P>333* Calling this method overrides any previous setting made by334* this method or {@link #setWantClientAuth(boolean)}.335* <P>336* The initial inherited setting may be overridden by calling337* {@link SSLSocket#setNeedClientAuth(boolean)} or338* {@link SSLSocket#setWantClientAuth(boolean)}.339*340* @param need set to true if client authentication is required,341* or false if no client authentication is desired.342* @see #getNeedClientAuth()343* @see #setWantClientAuth(boolean)344* @see #getWantClientAuth()345* @see #setUseClientMode(boolean)346*/347public abstract void setNeedClientAuth(boolean need);348349350/**351* Returns true if client authentication will be <i>required</i> on352* newly <code>accept</code>ed server-mode <code>SSLSocket</code>s.353* <P>354* The initial inherited setting may be overridden by calling355* {@link SSLSocket#setNeedClientAuth(boolean)} or356* {@link SSLSocket#setWantClientAuth(boolean)}.357*358* @return true if client authentication is required,359* or false if no client authentication is desired.360* @see #setNeedClientAuth(boolean)361* @see #setWantClientAuth(boolean)362* @see #getWantClientAuth()363* @see #setUseClientMode(boolean)364*/365public abstract boolean getNeedClientAuth();366367368/**369* Controls whether <code>accept</code>ed server-mode370* <code>SSLSockets</code> will be initially configured to371* <i>request</i> client authentication.372* <P>373* A socket's client authentication setting is one of the following:374* <ul>375* <li> client authentication required376* <li> client authentication requested377* <li> no client authentication desired378* </ul>379* <P>380* Unlike {@link #setNeedClientAuth(boolean)}, if the accepted381* socket's option is set and the client chooses not to provide382* authentication information about itself, <i>the negotiations383* will continue</i>.384* <P>385* Calling this method overrides any previous setting made by386* this method or {@link #setNeedClientAuth(boolean)}.387* <P>388* The initial inherited setting may be overridden by calling389* {@link SSLSocket#setNeedClientAuth(boolean)} or390* {@link SSLSocket#setWantClientAuth(boolean)}.391*392* @param want set to true if client authentication is requested,393* or false if no client authentication is desired.394* @see #getWantClientAuth()395* @see #setNeedClientAuth(boolean)396* @see #getNeedClientAuth()397* @see #setUseClientMode(boolean)398*/399public abstract void setWantClientAuth(boolean want);400401402/**403* Returns true if client authentication will be <i>requested</i> on404* newly accepted server-mode connections.405* <P>406* The initial inherited setting may be overridden by calling407* {@link SSLSocket#setNeedClientAuth(boolean)} or408* {@link SSLSocket#setWantClientAuth(boolean)}.409*410* @return true if client authentication is requested,411* or false if no client authentication is desired.412* @see #setWantClientAuth(boolean)413* @see #setNeedClientAuth(boolean)414* @see #getNeedClientAuth()415* @see #setUseClientMode(boolean)416*/417public abstract boolean getWantClientAuth();418419420/**421* Controls whether accepted connections are in the (default) SSL422* server mode, or the SSL client mode.423* <P>424* Servers normally authenticate themselves, and clients are not425* required to do so.426* <P>427* In rare cases, TCP servers428* need to act in the SSL client mode on newly accepted429* connections. For example, FTP clients acquire server sockets430* and listen there for reverse connections from the server. An431* FTP client would use an SSLServerSocket in "client" mode to432* accept the reverse connection while the FTP server uses an433* SSLSocket with "client" mode disabled to initiate the434* connection. During the resulting handshake, existing SSL435* sessions may be reused.436* <P>437* <code>SSLSocket</code>s returned from <code>accept()</code>438* inherit this setting.439*440* @param mode true if newly accepted connections should use SSL441* client mode.442* @see #getUseClientMode()443*/444public abstract void setUseClientMode(boolean mode);445446447/**448* Returns true if accepted connections will be in SSL client mode.449*450* @see #setUseClientMode(boolean)451* @return true if the connection should use SSL client mode.452*/453public abstract boolean getUseClientMode();454455456/**457* Controls whether new SSL sessions may be established by the458* sockets which are created from this server socket.459* <P>460* <code>SSLSocket</code>s returned from <code>accept()</code>461* inherit this setting.462*463* @param flag true indicates that sessions may be created; this464* is the default. false indicates that an existing session465* must be resumed.466* @see #getEnableSessionCreation()467*/468public abstract void setEnableSessionCreation(boolean flag);469470471/**472* Returns true if new SSL sessions may be established by the473* sockets which are created from this server socket.474*475* @return true indicates that sessions may be created; this476* is the default. false indicates that an existing477* session must be resumed478* @see #setEnableSessionCreation(boolean)479*/480public abstract boolean getEnableSessionCreation();481482/**483* Returns the SSLParameters in effect for newly accepted connections.484* The ciphersuites and protocols of the returned SSLParameters485* are always non-null.486*487* @return the SSLParameters in effect for newly accepted connections488*489* @see #setSSLParameters(SSLParameters)490*491* @since 1.7492*/493public SSLParameters getSSLParameters() {494SSLParameters parameters = new SSLParameters();495496parameters.setCipherSuites(getEnabledCipherSuites());497parameters.setProtocols(getEnabledProtocols());498if (getNeedClientAuth()) {499parameters.setNeedClientAuth(true);500} else if (getWantClientAuth()) {501parameters.setWantClientAuth(true);502}503504return parameters;505}506507/**508* Applies SSLParameters to newly accepted connections.509*510* <p>This means:511* <ul>512* <li>If {@code params.getCipherSuites()} is non-null,513* {@code setEnabledCipherSuites()} is called with that value.</li>514* <li>If {@code params.getProtocols()} is non-null,515* {@code setEnabledProtocols()} is called with that value.</li>516* <li>If {@code params.getNeedClientAuth()} or517* {@code params.getWantClientAuth()} return {@code true},518* {@code setNeedClientAuth(true)} and519* {@code setWantClientAuth(true)} are called, respectively;520* otherwise {@code setWantClientAuth(false)} is called.</li>521* <li>If {@code params.getServerNames()} is non-null, the socket will522* configure its server names with that value.</li>523* <li>If {@code params.getSNIMatchers()} is non-null, the socket will524* configure its SNI matchers with that value.</li>525* </ul>526*527* @param params the parameters528* @throws IllegalArgumentException if the setEnabledCipherSuites() or529* the setEnabledProtocols() call fails530*531* @see #getSSLParameters()532*533* @since 1.7534*/535public void setSSLParameters(SSLParameters params) {536String[] s;537s = params.getCipherSuites();538if (s != null) {539setEnabledCipherSuites(s);540}541542s = params.getProtocols();543if (s != null) {544setEnabledProtocols(s);545}546547if (params.getNeedClientAuth()) {548setNeedClientAuth(true);549} else if (params.getWantClientAuth()) {550setWantClientAuth(true);551} else {552setWantClientAuth(false);553}554}555556}557558559