src/java.base/share/classes/sun/security/ssl/SSLSocketFactoryImpl.java
author xuelei
Mon, 25 Jun 2018 13:41:39 -0700
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
permissions -rw-r--r--
8196584: TLS 1.3 Implementation Reviewed-by: ascarpino, coffeys, dfuchs, jjiang, jnimeh, mullan, rhalade, ssahoo, valeriep, weijun, wetmore, xuelei Contributed-by: Adam Petcher <adam.petcher@oracle.com>, Amanda Jiang <amanda.jiang@oracle.com>, Anthony Scarpino <anthony.scarpino@oracle.com>, Bradford Wetmore <bradford.wetmore@oracle.com>, Jamil Nimeh <jamil.j.nimeh@oracle.com>, John Jiang <sha.jiang@oracle.com>, Rajan Halade <rajan.halade@oracle.com>, Sibabrata Sahoo <sibabrata.sahoo@oracle.com>, Valerie Peng <valerie.peng@oracle.com>, Weijun Wang <weijun.wang@oracle.com>, Xuelei Fan <xuelei.fan@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Implementation of an SSL socket factory.  This provides the public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * hooks to create SSL sockets, using a "high level" programming
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * interface which encapsulates system security policy defaults rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * offering application flexibility.  In particular, it uses a configurable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * authentication context (and the keys held there) rather than offering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * any flexibility about which keys to use; that context defaults to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * process-default context, but may be explicitly specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
    44
public final class SSLSocketFactoryImpl extends SSLSocketFactory {
9246
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 5506
diff changeset
    45
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    46
    private final SSLContextImpl context;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Constructor used to instantiate the default factory. This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * only called if the old "ssl.SocketFactory.provider" property in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * java.security file is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public SSLSocketFactoryImpl() throws Exception {
9246
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 5506
diff changeset
    54
        this.context = SSLContextImpl.DefaultSSLContext.getDefaultImpl();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Constructs an SSL socket factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    SSLSocketFactoryImpl(SSLContextImpl context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.context = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Creates an unconnected socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @return the unconnected socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @see java.net.Socket#connect(java.net.SocketAddress, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
    70
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public Socket createSocket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return new SSLSocketImpl(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Constructs an SSL connection to a named host at a specified port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * This acts as the SSL client, and may authenticate itself or rejoin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * existing SSL sessions allowed by the authentication context which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * has been configured.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param host name of the host with which to connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param port number of the server's port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
    84
    @Override
2
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
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   107
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public Socket createSocket(Socket s, String host, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            boolean autoClose) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return new SSLSocketImpl(context, s, host, port, autoClose);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   113
    @Override
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   114
    public Socket createSocket(Socket s, InputStream consumed,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   115
            boolean autoClose) throws IOException {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   116
        if (s == null) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   117
            throw new NullPointerException(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   118
                    "the existing socket cannot be null");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   119
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   120
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   121
        return new SSLSocketImpl(context, s, consumed, autoClose);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   122
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Constructs an SSL connection to a server at a specified address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * and TCP port.  This acts as the SSL client, and may authenticate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * itself or rejoin existing SSL sessions allowed by the authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * context which has been configured.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param address the server's host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param port its port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   133
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public Socket createSocket(InetAddress address, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return new SSLSocketImpl(context, address, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Constructs an SSL connection to a named host at a specified port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * This acts as the SSL client, and may authenticate itself or rejoin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * existing SSL sessions allowed by the authentication context which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * has been configured. The socket will also bind() to the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * address and port supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   148
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public Socket createSocket(String host, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        InetAddress clientAddress, int clientPort)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return new SSLSocketImpl(context, host, port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                clientAddress, clientPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Constructs an SSL connection to a server at a specified address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * and TCP port.  This acts as the SSL client, and may authenticate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * itself or rejoin existing SSL sessions allowed by the authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * context which has been configured. The socket will also bind() to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * the local address and port supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   164
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public Socket createSocket(InetAddress address, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        InetAddress clientAddress, int clientPort)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return new SSLSocketImpl(context, address, port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                clientAddress, clientPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Returns the subset of the supported cipher suites which are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * enabled by default.  These cipher suites all provide a minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * quality of service whereby the server authenticates itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * (preventing person-in-the-middle attacks) and where traffic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * is encrypted to provide confidentiality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   181
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public String[] getDefaultCipherSuites() {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   183
        return CipherSuite.namesOf(context.getDefaultCipherSuites(false));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Returns the names of the cipher suites which could be enabled for use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * on an SSL connection.  Normally, only a subset of these will actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * be enabled by default, since this list may include cipher suites which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * do not support the mutual authentication of servers and clients, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * which do not protect data confidentiality.  Servers may also need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * certain kinds of certificates to use certain cipher suites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   194
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public String[] getSupportedCipherSuites() {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   196
        return CipherSuite.namesOf(context.getSupportedCipherSuites());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}