jdk/src/share/classes/sun/security/ssl/SSLSocketFactoryImpl.java
author xuelei
Fri, 08 Apr 2011 02:00:09 -0700
changeset 9246 c459f79af46b
parent 5506 202f599c92aa
child 13815 2de30ecf335e
permissions -rw-r--r--
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled Summary: Reorg the SSLContext implementation Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9246
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.net.ssl.SSLSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.net.ssl.SSLSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Implementation of an SSL socket factory.  This provides the public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * hooks to create SSL sockets, using a "high level" programming
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * interface which encapsulates system security policy defaults rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * offering application flexibility.  In particular, it uses a configurable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * authentication context (and the keys held there) rather than offering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * any flexibility about which keys to use; that context defaults to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * process-default context, but may be explicitly specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
9246
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 5506
diff changeset
    45
final public class SSLSocketFactoryImpl extends SSLSocketFactory {
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 5506
diff changeset
    46
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static SSLContextImpl defaultContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private SSLContextImpl context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Constructor used to instantiate the default factory. This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * only called if the old "ssl.SocketFactory.provider" property in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * java.security file is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public SSLSocketFactoryImpl() throws Exception {
9246
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 5506
diff changeset
    56
        this.context = SSLContextImpl.DefaultSSLContext.getDefaultImpl();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Constructs an SSL socket factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    SSLSocketFactoryImpl(SSLContextImpl context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.context = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Creates an unconnected socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @return the unconnected socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @see java.net.Socket#connect(java.net.SocketAddress, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public Socket createSocket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return new SSLSocketImpl(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Constructs an SSL connection to a named host at a specified port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * This acts as the SSL client, and may authenticate itself or rejoin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * existing SSL sessions allowed by the authentication context which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * has been configured.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param host name of the host with which to connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param port number of the server's port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public Socket createSocket(String host, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    throws IOException, UnknownHostException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return new SSLSocketImpl(context, host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Returns a socket layered over an existing socket to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * ServerSocket on the named host, at the given port.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * constructor can be used when tunneling SSL through a proxy. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * host and port refer to the logical destination server.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * socket is configured using the socket options established for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * this factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param s the existing socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param host the server host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param port the server port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param autoClose close the underlying socket when this socket is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @exception IOException if the connection can't be established
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @exception UnknownHostException if the host is not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public Socket createSocket(Socket s, String host, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            boolean autoClose) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return new SSLSocketImpl(context, s, host, port, autoClose);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Constructs an SSL connection to a server at a specified address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * and TCP port.  This acts as the SSL client, and may authenticate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * itself or rejoin existing SSL sessions allowed by the authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * context which has been configured.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param address the server's host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param port its port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public Socket createSocket(InetAddress address, int port)
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
        return new SSLSocketImpl(context, address, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
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
     * Constructs an SSL connection to a named host at a specified port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * This acts as the SSL client, and may authenticate itself or rejoin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * existing SSL sessions allowed by the authentication context which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * has been configured. The socket will also bind() to the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * address and port supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public Socket createSocket(String host, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        InetAddress clientAddress, int clientPort)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return new SSLSocketImpl(context, host, port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                clientAddress, clientPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Constructs an SSL connection to a server at a specified address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * and TCP port.  This acts as the SSL client, and may authenticate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * itself or rejoin existing SSL sessions allowed by the authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * context which has been configured. The socket will also bind() to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * the local address and port supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public Socket createSocket(InetAddress address, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        InetAddress clientAddress, int clientPort)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return new SSLSocketImpl(context, address, port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                clientAddress, clientPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Returns the subset of the supported cipher suites which are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * enabled by default.  These cipher suites all provide a minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * quality of service whereby the server authenticates itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * (preventing person-in-the-middle attacks) and where traffic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * is encrypted to provide confidentiality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public String[] getDefaultCipherSuites() {
9246
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 5506
diff changeset
   168
        return context.getDefaultCipherSuiteList(false).toStringArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Returns the names of the cipher suites which could be enabled for use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * on an SSL connection.  Normally, only a subset of these will actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * be enabled by default, since this list may include cipher suites which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * do not support the mutual authentication of servers and clients, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * which do not protect data confidentiality.  Servers may also need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * certain kinds of certificates to use certain cipher suites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public String[] getSupportedCipherSuites() {
9246
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 5506
diff changeset
   180
        return context.getSuportedCipherSuiteList().toStringArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
}