jdk/src/share/classes/sun/security/ssl/SSLServerSocketImpl.java
author xuelei
Thu, 13 Nov 2008 23:25:10 -0800
changeset 1580 9af5946d4060
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6745052: SLServerSocket file descriptor leak Summary: SSLServerSocketImpl.checkEnabledSuites() does not release the temporary socket properly Reviewed-by: wetmore, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1580
9af5946d4060 6745052: SLServerSocket file descriptor leak
xuelei
parents: 2
diff changeset
     2
 * Copyright 1996-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.Socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.net.ServerSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.net.ssl.SSLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.net.ssl.SSLServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class provides a simple way for servers to support conventional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * use of the Secure Sockets Layer (SSL).  Application code uses an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * SSLServerSocketImpl exactly like it uses a regular TCP ServerSocket; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * difference is that the connections established are secured using SSL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <P> Also, the constructors take an explicit authentication context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * parameter, giving flexibility with respect to how the server socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * authenticates itself.  That policy flexibility is not exposed through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * the standard SSLServerSocketFactory API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <P> System security defaults prevent server sockets from accepting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * connections if they the authentication context has not been given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * a certificate chain and its matching private key.  If the clients
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * of your application support "anonymous" cipher suites, you may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * able to configure a server socket to accept those suites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see SSLSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see SSLServerSocketFactoryImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
class SSLServerSocketImpl extends SSLServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private SSLContextImpl      sslContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /* Do newly accepted connections require clients to authenticate? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private byte                doClientAuth = SSLEngineImpl.clauth_none;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /* Do new connections created here use the "server" mode of SSL? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private boolean             useServerMode = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /* Can new connections created establish new sessions? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private boolean             enableSessionCreation = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /* what cipher suites to use by default */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private CipherSuiteList     enabledCipherSuites = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /* which protocol to use by default */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private ProtocolList        enabledProtocols = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /* could enabledCipherSuites ever complete handshaking? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private boolean             checkedEnabled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Create an SSL server socket on a port, using a non-default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * authentication context and a specified connection backlog.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param port the port on which to listen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param backlog how many connections may be pending before
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *          the system should start rejecting new requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param context authentication context for this server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    SSLServerSocketImpl(int port, int backlog, SSLContextImpl context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    throws IOException, SSLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        super(port, backlog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        initServer(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Create an SSL server socket on a port, using a specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * authentication context and a specified backlog of connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * as well as a particular specified network interface.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * constructor is used on multihomed hosts, such as those used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * for firewalls or as routers, to control through which interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * a network service is provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param port the port on which to listen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param backlog how many connections may be pending before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *          the system should start rejecting new requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param address the address of the network interface through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *          which connections will be accepted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param context authentication context for this server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    SSLServerSocketImpl(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        int             port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        int             backlog,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        InetAddress     address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        SSLContextImpl  context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        super(port, backlog, address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        initServer(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Creates an unbound server socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    SSLServerSocketImpl(SSLContextImpl context) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        initServer(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Initializes the server socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private void initServer(SSLContextImpl context) throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (context == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            throw new SSLException("No Authentication context given");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        sslContext = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        enabledCipherSuites = CipherSuiteList.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        enabledProtocols = ProtocolList.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Returns the names of the cipher suites which could be enabled for use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * on an SSL connection.  Normally, only a subset of these will actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * be enabled by default, since this list may include cipher suites which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * do not support the mutual authentication of servers and clients, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * which do not protect data confidentiality.  Servers may also need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * certain kinds of certificates to use certain cipher suites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @return an array of cipher suite names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public String[] getSupportedCipherSuites() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        CipherSuiteList.clearAvailableCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return CipherSuiteList.getSupported().toStringArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Returns the list of cipher suites which are currently enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * for use by newly accepted connections.  A null return indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * that the system defaults are in effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    synchronized public String[] getEnabledCipherSuites() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return enabledCipherSuites.toStringArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Controls which particular SSL cipher suites are enabled for use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * by accepted connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param suites Names of all the cipher suites to enable; null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *  means to accept system defaults.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    synchronized public void setEnabledCipherSuites(String[] suites) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        enabledCipherSuites = new CipherSuiteList(suites);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        checkedEnabled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public String[] getSupportedProtocols() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return ProtocolList.getSupported().toStringArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Controls which protocols are enabled for use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * The protocols must have been listed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * getSupportedProtocols() as being supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param protocols protocols to enable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @exception IllegalArgumentException when one of the protocols
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *  named by the parameter is not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    synchronized public void setEnabledProtocols(String[] protocols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        enabledProtocols = new ProtocolList(protocols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    synchronized public String[] getEnabledProtocols() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return enabledProtocols.toStringArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Controls whether the connections which are accepted must include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * client authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public void setNeedClientAuth(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        doClientAuth = (flag ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            SSLEngineImpl.clauth_required : SSLEngineImpl.clauth_none);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public boolean getNeedClientAuth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return (doClientAuth == SSLEngineImpl.clauth_required);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Controls whether the connections which are accepted should request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * client authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public void setWantClientAuth(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        doClientAuth = (flag ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            SSLEngineImpl.clauth_requested : SSLEngineImpl.clauth_none);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public boolean getWantClientAuth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return (doClientAuth == SSLEngineImpl.clauth_requested);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Makes the returned sockets act in SSL "client" mode, not the usual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * server mode.  The canonical example of why this is needed is for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * FTP clients, which accept connections from servers and should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * rejoining the already-negotiated SSL connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public void setUseClientMode(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        useServerMode = !flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public boolean getUseClientMode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return !useServerMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Controls whether new connections may cause creation of new SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * sessions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public void setEnableSessionCreation(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        enableSessionCreation = flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Returns true if new connections may cause creation of new SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * sessions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public boolean getEnableSessionCreation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return enableSessionCreation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Accept a new SSL connection.  This server identifies itself with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * information provided in the authentication context which was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * presented during construction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public Socket accept() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        checkEnabledSuites();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        SSLSocketImpl s = new SSLSocketImpl(sslContext, useServerMode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            enabledCipherSuites, doClientAuth, enableSessionCreation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            enabledProtocols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        implAccept(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        s.doneConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * This is a sometimes helpful diagnostic check that is performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * once for each ServerSocket to verify that the initial set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * enabled suites are capable of supporting a successful handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    private void checkEnabledSuites() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        // We want to report an error if no cipher suites were actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        // enabled, since this is an error users are known to make.  Then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        // they get vastly confused by having clients report an error!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (checkedEnabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (useServerMode == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            SSLSocketImpl tmp = new SSLSocketImpl(sslContext, useServerMode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                         enabledCipherSuites, doClientAuth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                         enableSessionCreation, enabledProtocols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
1580
9af5946d4060 6745052: SLServerSocket file descriptor leak
xuelei
parents: 2
diff changeset
   307
            try {
9af5946d4060 6745052: SLServerSocket file descriptor leak
xuelei
parents: 2
diff changeset
   308
                ServerHandshaker handshaker = tmp.getServerHandshaker();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
1580
9af5946d4060 6745052: SLServerSocket file descriptor leak
xuelei
parents: 2
diff changeset
   310
                for (Iterator t = enabledCipherSuites.iterator(); t.hasNext(); ) {
9af5946d4060 6745052: SLServerSocket file descriptor leak
xuelei
parents: 2
diff changeset
   311
                    CipherSuite suite = (CipherSuite)t.next();
9af5946d4060 6745052: SLServerSocket file descriptor leak
xuelei
parents: 2
diff changeset
   312
                    if (handshaker.trySetCipherSuite(suite)) {
9af5946d4060 6745052: SLServerSocket file descriptor leak
xuelei
parents: 2
diff changeset
   313
                        checkedEnabled = true;
9af5946d4060 6745052: SLServerSocket file descriptor leak
xuelei
parents: 2
diff changeset
   314
                        return;
9af5946d4060 6745052: SLServerSocket file descriptor leak
xuelei
parents: 2
diff changeset
   315
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                }
1580
9af5946d4060 6745052: SLServerSocket file descriptor leak
xuelei
parents: 2
diff changeset
   317
            } finally {
9af5946d4060 6745052: SLServerSocket file descriptor leak
xuelei
parents: 2
diff changeset
   318
                tmp.closeSocket();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            // diagnostic text here is currently appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            // since it's only certificate unavailability that can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            // cause such problems ... but that might change someday.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            throw new SSLException("No available certificate or key corresponds"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                + " to the SSL cipher suites which are enabled.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Provides a brief description of this SSL socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return "[SSL: "+ super.toString() + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
}