jdk/src/java.base/share/classes/sun/net/www/protocol/https/HttpsClient.java
author redestad
Thu, 21 Apr 2016 13:39:53 +0200
changeset 37593 824750ada3d6
parent 29615 b0057b63b4e7
child 37781 71ed5645f17c
permissions -rw-r--r--
8154231: Simplify access to System properties from JDK code Reviewed-by: rriggs, chegar, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29377
d3f457ce9c48 8073430: Deprecate security APIs that have been superseded
juh
parents: 28568
diff changeset
     2
 * Copyright (c) 2001, 2015, 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: 5162
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: 5162
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: 5162
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5162
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5162
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.net.www.protocol.https;
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.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.BufferedOutputStream;
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
    33
import java.net.InetAddress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.Socket;
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
    35
import java.net.SocketException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.InetSocketAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.net.www.http.HttpClient;
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
    49
import sun.net.www.protocol.http.HttpURLConnection;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.security.action.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.security.util.HostnameChecker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.security.ssl.SSLSocketImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
    55
import sun.util.logging.PlatformLogger;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
    56
import static sun.net.www.protocol.http.HttpURLConnection.TunnelState.*;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
    57
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * This class provides HTTPS client URL support, building on the standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * "sun.net.www" HTTP protocol handler.  HTTPS is the same protocol as HTTP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * but differs in the transport layer which it uses:  <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *      <LI>There's a <em>Secure Sockets Layer</em> between TCP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *      and the HTTP protocol code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *      <LI>It uses a different default TCP port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *      <LI>It doesn't use application level proxies, which can see and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *      manipulate HTTP user level data, compromising privacy.  It uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *      low level tunneling instead, which hides HTTP protocol and data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *      from all third parties.  (Traffic analysis is still possible).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *      <LI>It does basic server authentication, to protect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *      against "URL spoofing" attacks.  This involves deciding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *      whether the X.509 certificate chain identifying the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *      is trusted, and verifying that the name of the server is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *      found in the certificate.  (The application may enable an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *      anonymous SSL cipher suite, and such checks are not done
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *      for anonymous ciphers.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *      <LI>It exposes key SSL session attributes, specifically the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *      cipher suite in use and the server's X509 certificates, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *      application software which knows about this protocol handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *      </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <P> System properties used include:  <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *      <LI><em>https.proxyHost</em> ... the host supporting SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *      tunneling using the conventional CONNECT syntax
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *      <LI><em>https.proxyPort</em> ... port to use on proxyHost
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *      <LI><em>https.cipherSuites</em> ... comma separated list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *      SSL cipher suite names to enable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *      <LI><em>http.nonProxyHosts</em> ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *      </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * @author Bill Foote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
// final for export control reasons (access to APIs); remove with care
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
final class HttpsClient extends HttpClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    implements HandshakeCompletedListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // STATIC STATE and ACCESSORS THERETO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // HTTPS uses a different default port number than HTTP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private static final int    httpsPortNumber = 443;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   115
    // default HostnameVerifier class canonical name
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   116
    private static final String defaultHVCanonicalName =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   117
            "javax.net.ssl.HttpsURLConnection.DefaultHostnameVerifier";
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   118
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /** Returns the default HTTPS port (443) */
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   120
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    protected int getDefaultPort() { return httpsPortNumber; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private HostnameVerifier hv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private SSLSocketFactory sslSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    // HttpClient.proxyDisabled will always be false, because we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    // use an application-level HTTP proxy.  We might tunnel through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    // our http proxy, though.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    // INSTANCE DATA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    // last negotiated SSL session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private SSLSession  session;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private String [] getCipherSuites() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // If ciphers are assigned, sort them into an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        String ciphers [];
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 29615
diff changeset
   141
        String cipherString =
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 29615
diff changeset
   142
                GetPropertyAction.getProperty("https.cipherSuites");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (cipherString == null || "".equals(cipherString)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            ciphers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            StringTokenizer     tokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            Vector<String>      v = new Vector<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            tokenizer = new StringTokenizer(cipherString, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            while (tokenizer.hasMoreTokens())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                v.addElement(tokenizer.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            ciphers = new String [v.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            for (int i = 0; i < ciphers.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                ciphers [i] = v.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return ciphers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private String [] getProtocols() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // If protocols are assigned, sort them into an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        String protocols [];
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 29615
diff changeset
   165
        String protocolString =
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 29615
diff changeset
   166
                GetPropertyAction.getProperty("https.protocols");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (protocolString == null || "".equals(protocolString)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            protocols = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            StringTokenizer     tokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            Vector<String>      v = new Vector<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            tokenizer = new StringTokenizer(protocolString, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            while (tokenizer.hasMoreTokens())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                v.addElement(tokenizer.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            protocols = new String [v.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            for (int i = 0; i < protocols.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                protocols [i] = v.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return protocols;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private String getUserAgent() {
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 29615
diff changeset
   186
        String userAgent = GetPropertyAction.getProperty("https.agent");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (userAgent == null || userAgent.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            userAgent = "JSSE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return userAgent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    // CONSTRUCTOR, FACTORY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Create an HTTPS client URL.  Traffic will be tunneled through any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * intermediate nodes rather than proxied, so that confidentiality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * of data exchanged can be preserved.  However, note that all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * anonymous SSL flavors are subject to "person-in-the-middle"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * attacks against confidentiality.  If you enable use of those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * flavors, you may be giving up the protection you get through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * SSL tunneling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Use New to get new HttpsClient. This constructor is meant to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * used only by New method. New properly checks for URL spoofing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param URL https URL with which a connection must be established
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private HttpsClient(SSLSocketFactory sf, URL url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        // HttpClient-level proxying is always disabled,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        // because we override doConnect to do tunneling instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        this(sf, url, (String)null, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *  Create an HTTPS client URL.  Traffic will be tunneled through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * the specified proxy server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    HttpsClient(SSLSocketFactory sf, URL url, String proxyHost, int proxyPort)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        this(sf, url, proxyHost, proxyPort, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *  Create an HTTPS client URL.  Traffic will be tunneled through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * the specified proxy server, with a connect timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    HttpsClient(SSLSocketFactory sf, URL url, String proxyHost, int proxyPort,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                int connectTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        this(sf, url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
             (proxyHost == null? null:
28568
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   236
                HttpClient.newHttpProxy(proxyHost, proxyPort, "https")),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *  Same as previous constructor except using a Proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    HttpsClient(SSLSocketFactory sf, URL url, Proxy proxy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                int connectTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        throws IOException {
28568
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   246
        PlatformLogger logger = HttpURLConnection.getHttpLogger();
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   247
        if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   248
             logger.finest("Creating new HttpsClient with url:" + url + " and proxy:" + proxy +
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   249
             " with connect timeout:" + connectTimeout);
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   250
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        this.proxy = proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        setSSLSocketFactory(sf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        this.proxyDisabled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        this.host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        this.url = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (port == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            port = getDefaultPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        setConnectTimeout(connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        openServer();
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
    // This code largely ripped off from HttpClient.New, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    // it uses the same keepalive cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   269
    static HttpClient New(SSLSocketFactory sf, URL url, HostnameVerifier hv,
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   270
                          HttpURLConnection httpuc)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            throws IOException {
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   272
        return HttpsClient.New(sf, url, hv, true, httpuc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /** See HttpClient for the model for this method. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    static HttpClient New(SSLSocketFactory sf, URL url,
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   277
            HostnameVerifier hv, boolean useCache,
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   278
            HttpURLConnection httpuc) throws IOException {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   279
        return HttpsClient.New(sf, url, hv, (String)null, -1, useCache, httpuc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Get a HTTPS client to the URL.  Traffic will be tunneled through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * the specified proxy server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    static HttpClient New(SSLSocketFactory sf, URL url, HostnameVerifier hv,
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   287
                           String proxyHost, int proxyPort,
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   288
                           HttpURLConnection httpuc) throws IOException {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   289
        return HttpsClient.New(sf, url, hv, proxyHost, proxyPort, true, httpuc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    static HttpClient New(SSLSocketFactory sf, URL url, HostnameVerifier hv,
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   293
                           String proxyHost, int proxyPort, boolean useCache,
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   294
                           HttpURLConnection httpuc)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        throws IOException {
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   296
        return HttpsClient.New(sf, url, hv, proxyHost, proxyPort, useCache, -1,
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   297
                               httpuc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    static HttpClient New(SSLSocketFactory sf, URL url, HostnameVerifier hv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                          String proxyHost, int proxyPort, boolean useCache,
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   302
                          int connectTimeout, HttpURLConnection httpuc)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return HttpsClient.New(sf, url, hv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                               (proxyHost == null? null :
28568
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   307
                                HttpClient.newHttpProxy(proxyHost, proxyPort, "https")),
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   308
                               useCache, connectTimeout, httpuc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    static HttpClient New(SSLSocketFactory sf, URL url, HostnameVerifier hv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                          Proxy p, boolean useCache,
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   313
                          int connectTimeout, HttpURLConnection httpuc)
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   314
        throws IOException
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   315
    {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   316
        if (p == null) {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   317
            p = Proxy.NO_PROXY;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   318
        }
28568
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   319
        PlatformLogger logger = HttpURLConnection.getHttpLogger();
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   320
        if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   321
            logger.finest("Looking for HttpClient for URL " + url +
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   322
                " and proxy value of " + p);
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   323
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        HttpsClient ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (useCache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            /* see if one's already around */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            ret = (HttpsClient) kac.get(url, sf);
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   328
            if (ret != null && httpuc != null &&
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   329
                httpuc.streaming() &&
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   330
                httpuc.getRequestMethod() == "POST") {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   331
                if (!ret.available())
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   332
                    ret = null;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   333
            }
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   334
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (ret != null) {
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   336
                if ((ret.proxy != null && ret.proxy.equals(p)) ||
28568
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   337
                    (ret.proxy == null && p == Proxy.NO_PROXY)) {
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   338
                    synchronized (ret) {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   339
                        ret.cachedHttpClient = true;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   340
                        assert ret.inCache;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   341
                        ret.inCache = false;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   342
                        if (httpuc != null && ret.needsTunneling())
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   343
                            httpuc.setTunnelState(TUNNELING);
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16499
diff changeset
   344
                        if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   345
                            logger.finest("KeepAlive stream retrieved from the cache, " + ret);
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   346
                        }
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   347
                    }
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   348
                } else {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   349
                    // We cannot return this connection to the cache as it's
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   350
                    // KeepAliveTimeout will get reset. We simply close the connection.
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   351
                    // This should be fine as it is very rare that a connection
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   352
                    // to the same host will not use the same proxy.
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   353
                    synchronized(ret) {
28568
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   354
                        if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   355
                            logger.finest("Not returning this connection to cache: " + ret);
5325bdefc06b 8065994: HTTP Tunnel connection to NTLM proxy reauthenticates instead of using keep-alive
coffeys
parents: 25859
diff changeset
   356
                        }
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   357
                        ret.inCache = false;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   358
                        ret.closeServer();
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   359
                    }
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   360
                    ret = null;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   361
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (ret == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            ret = new HttpsClient(sf, url, p, connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            if (security != null) {
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   369
                if (ret.proxy == Proxy.NO_PROXY || ret.proxy == null) {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   370
                    security.checkConnect(InetAddress.getByName(url.getHost()).getHostAddress(), url.getPort());
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   371
                } else {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   372
                    security.checkConnect(url.getHost(), url.getPort());
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   373
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            ret.url = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        ret.setHostnameVerifier(hv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    // METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    void setHostnameVerifier(HostnameVerifier hv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        this.hv = hv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    void setSSLSocketFactory(SSLSocketFactory sf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        sslSocketFactory = sf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    SSLSocketFactory getSSLSocketFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return sslSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   395
    /**
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   396
     * The following method, createSocket, is defined in NetworkClient
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   397
     * and overridden here so that the socket facroty is used to create
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   398
     * new sockets.
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   399
     */
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   400
    @Override
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   401
    protected Socket createSocket() throws IOException {
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   402
        try {
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   403
            return sslSocketFactory.createSocket();
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   404
        } catch (SocketException se) {
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   405
            //
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   406
            // bug 6771432
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   407
            // javax.net.SocketFactory throws a SocketException with an
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   408
            // UnsupportedOperationException as its cause to indicate that
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   409
            // unconnected sockets have not been implemented.
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   410
            //
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   411
            Throwable t = se.getCause();
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   412
            if (t != null && t instanceof UnsupportedOperationException) {
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   413
                return super.createSocket();
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   414
            } else {
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   415
                throw se;
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   416
            }
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   417
        }
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   418
    }
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   419
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   420
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   421
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public boolean needsTunneling() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return (proxy != null && proxy.type() != Proxy.Type.DIRECT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                && proxy.type() != Proxy.Type.SOCKS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   427
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public void afterConnect() throws IOException, UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if (!isCachedConnection()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            SSLSocket s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            SSLSocketFactory factory = sslSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                if (!(serverSocket instanceof SSLSocket)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    s = (SSLSocket)factory.createSocket(serverSocket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                                                        host, port, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    s = (SSLSocket)serverSocket;
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   438
                    if (s instanceof SSLSocketImpl) {
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   439
                        ((SSLSocketImpl)s).setHost(host);
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   440
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                // If we fail to connect through the tunnel, try it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                // locally, as a last resort.  If this doesn't work,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                // throw the original exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    s = (SSLSocket)factory.createSocket(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                } catch (IOException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            // Force handshaking, so that we get any authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            // Register a handshake callback so our session state tracks any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            // later session renegotiations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            String [] protocols = getProtocols();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            String [] ciphers = getCipherSuites();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            if (protocols != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                s.setEnabledProtocols(protocols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            if (ciphers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                s.setEnabledCipherSuites(ciphers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            s.addHandshakeCompletedListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   468
            // We have two hostname verification approaches. One is in
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   469
            // SSL/TLS socket layer, where the algorithm is configured with
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   470
            // SSLParameters.setEndpointIdentificationAlgorithm(), and the
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   471
            // hostname verification is done by X509ExtendedTrustManager when
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   472
            // the algorithm is "HTTPS". The other one is in HTTPS layer,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   473
            // where the algorithm is customized by
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   474
            // HttpsURLConnection.setHostnameVerifier(), and the hostname
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   475
            // verification is done by HostnameVerifier when the default
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   476
            // rules for hostname verification fail.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   477
            //
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   478
            // The relationship between two hostname verification approaches
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   479
            // likes the following:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   480
            //
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   481
            //               |             EIA algorithm
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   482
            //               +----------------------------------------------
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   483
            //               |     null      |   HTTPS    |   LDAP/other   |
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   484
            // -------------------------------------------------------------
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   485
            //     |         |1              |2           |3               |
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   486
            // HNV | default | Set HTTPS EIA | use EIA    | HTTPS          |
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   487
            //     |--------------------------------------------------------
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   488
            //     | non -   |4              |5           |6               |
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   489
            //     | default | HTTPS/HNV     | use EIA    | HTTPS/HNV      |
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   490
            // -------------------------------------------------------------
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   491
            //
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   492
            // Abbreviation:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   493
            //     EIA: the endpoint identification algorithm in SSL/TLS
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   494
            //           socket layer
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   495
            //     HNV: the hostname verification object in HTTPS layer
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   496
            // Notes:
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   497
            //     case 1. default HNV and EIA is null
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   498
            //           Set EIA as HTTPS, hostname check done in SSL/TLS
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   499
            //           layer.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   500
            //     case 2. default HNV and EIA is HTTPS
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   501
            //           Use existing EIA, hostname check done in SSL/TLS
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   502
            //           layer.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   503
            //     case 3. default HNV and EIA is other than HTTPS
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   504
            //           Use existing EIA, EIA check done in SSL/TLS
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   505
            //           layer, then do HTTPS check in HTTPS layer.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   506
            //     case 4. non-default HNV and EIA is null
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   507
            //           No EIA, no EIA check done in SSL/TLS layer, then do
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   508
            //           HTTPS check in HTTPS layer using HNV as override.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   509
            //     case 5. non-default HNV and EIA is HTTPS
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   510
            //           Use existing EIA, hostname check done in SSL/TLS
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   511
            //           layer. No HNV override possible. We will review this
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   512
            //           decision and may update the architecture for JDK 7.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   513
            //     case 6. non-default HNV and EIA is other than HTTPS
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   514
            //           Use existing EIA, EIA check done in SSL/TLS layer,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   515
            //           then do HTTPS check in HTTPS layer as override.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   516
            boolean needToCheckSpoofing = true;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   517
            String identification =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   518
                s.getSSLParameters().getEndpointIdentificationAlgorithm();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   519
            if (identification != null && identification.length() != 0) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   520
                if (identification.equalsIgnoreCase("HTTPS")) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   521
                    // Do not check server identity again out of SSLSocket,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   522
                    // the endpoint will be identified during TLS handshaking
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   523
                    // in SSLSocket.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   524
                    needToCheckSpoofing = false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   525
                }   // else, we don't understand the identification algorithm,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   526
                    // need to check URL spoofing here.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   527
            } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   528
                boolean isDefaultHostnameVerifier = false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   529
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   530
                // We prefer to let the SSLSocket do the spoof checks, but if
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   531
                // the application has specified a HostnameVerifier (HNV),
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   532
                // we will always use that.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   533
                if (hv != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   534
                    String canonicalName = hv.getClass().getCanonicalName();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   535
                    if (canonicalName != null &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   536
                    canonicalName.equalsIgnoreCase(defaultHVCanonicalName)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   537
                        isDefaultHostnameVerifier = true;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   538
                    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   539
                } else {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   540
                    // Unlikely to happen! As the behavior is the same as the
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   541
                    // default hostname verifier, so we prefer to let the
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   542
                    // SSLSocket do the spoof checks.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   543
                    isDefaultHostnameVerifier = true;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   544
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   545
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   546
                if (isDefaultHostnameVerifier) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   547
                    // If the HNV is the default from HttpsURLConnection, we
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   548
                    // will do the spoof checks in SSLSocket.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   549
                    SSLParameters paramaters = s.getSSLParameters();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   550
                    paramaters.setEndpointIdentificationAlgorithm("HTTPS");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   551
                    s.setSSLParameters(paramaters);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   552
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   553
                    needToCheckSpoofing = false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   554
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            s.startHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            session = s.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            // change the serverSocket and serverOutput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            serverSocket = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                serverOutput = new PrintStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    new BufferedOutputStream(serverSocket.getOutputStream()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    false, encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            } catch (UnsupportedEncodingException e) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10419
diff changeset
   566
                throw new InternalError(encoding+" encoding not found");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            // check URL spoofing if it has not been checked under handshaking
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   570
            if (needToCheckSpoofing) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                checkURLSpoofing(hv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            // if we are reusing a cached https session,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            // we don't need to do handshaking etc. But we do need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            // set the ssl session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            session = ((SSLSocket)serverSocket).getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    // Server identity checking is done according to RFC 2818: HTTP over TLS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    // Section 3.1 Server Identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    private void checkURLSpoofing(HostnameVerifier hostnameVerifier)
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   584
            throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        // Get authenticated server name, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        String host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        // if IPv6 strip off the "[]"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        if (host != null && host.startsWith("[") && host.endsWith("]")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            host = host.substring(1, host.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        Certificate[] peerCerts = null;
3957
c8fdb8fad795 6885204: JSSE should not require Kerberos to be present
vinnie
parents: 98
diff changeset
   596
        String cipher = session.getCipherSuite();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            HostnameChecker checker = HostnameChecker.getInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                                                HostnameChecker.TYPE_TLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 3957
diff changeset
   601
            // Use ciphersuite to determine whether Kerberos is present.
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 3957
diff changeset
   602
            if (cipher.startsWith("TLS_KRB5")) {
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   603
                if (!HostnameChecker.match(host, getPeerPrincipal())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    throw new SSLPeerUnverifiedException("Hostname checker" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                                " failed for Kerberos");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                }
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 3957
diff changeset
   607
            } else { // X.509
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 3957
diff changeset
   608
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                // get the subject's certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                peerCerts = session.getPeerCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                X509Certificate peerCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                if (peerCerts[0] instanceof
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                        java.security.cert.X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                    peerCert = (java.security.cert.X509Certificate)peerCerts[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    throw new SSLPeerUnverifiedException("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                checker.match(host, peerCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            // if it doesn't throw an exception, we passed. Return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        } catch (SSLPeerUnverifiedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            // client explicitly changed default policy and enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            // anonymous ciphers; we can't check the standard policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        } catch (java.security.cert.CertificateException cpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        if ((cipher != null) && (cipher.indexOf("_anon_") != -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        } else if ((hostnameVerifier != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                   (hostnameVerifier.verify(host, session))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        serverSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        session.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        throw new IOException("HTTPS hostname wrong:  should be <"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                              + url.getHost() + ">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   650
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    protected void putInKeepAliveCache() {
16499
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   652
        if (inCache) {
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   653
            assert false : "Duplicate put to keep alive cache";
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   654
            return;
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   655
        }
88276d2da9c0 8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents: 14342
diff changeset
   656
        inCache = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        kac.put(url, sslSocketFactory, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
98
4087c83cfab8 6618387: SSL client sessions do not close cleanly. A TCP reset occurs instead of a close_notify alert.
xuelei
parents: 2
diff changeset
   660
    /*
4087c83cfab8 6618387: SSL client sessions do not close cleanly. A TCP reset occurs instead of a close_notify alert.
xuelei
parents: 2
diff changeset
   661
     * Close an idle connection to this URL (if it exists in the cache).
4087c83cfab8 6618387: SSL client sessions do not close cleanly. A TCP reset occurs instead of a close_notify alert.
xuelei
parents: 2
diff changeset
   662
     */
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   663
    @Override
98
4087c83cfab8 6618387: SSL client sessions do not close cleanly. A TCP reset occurs instead of a close_notify alert.
xuelei
parents: 2
diff changeset
   664
    public void closeIdleConnection() {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10419
diff changeset
   665
        HttpClient http = kac.get(url, sslSocketFactory);
98
4087c83cfab8 6618387: SSL client sessions do not close cleanly. A TCP reset occurs instead of a close_notify alert.
xuelei
parents: 2
diff changeset
   666
        if (http != null) {
4087c83cfab8 6618387: SSL client sessions do not close cleanly. A TCP reset occurs instead of a close_notify alert.
xuelei
parents: 2
diff changeset
   667
            http.closeServer();
4087c83cfab8 6618387: SSL client sessions do not close cleanly. A TCP reset occurs instead of a close_notify alert.
xuelei
parents: 2
diff changeset
   668
        }
4087c83cfab8 6618387: SSL client sessions do not close cleanly. A TCP reset occurs instead of a close_notify alert.
xuelei
parents: 2
diff changeset
   669
    }
4087c83cfab8 6618387: SSL client sessions do not close cleanly. A TCP reset occurs instead of a close_notify alert.
xuelei
parents: 2
diff changeset
   670
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * Returns the cipher suite in use on this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    String getCipherSuite() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        return session.getCipherSuite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * Returns the certificate chain the client sent to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * server, or null if the client did not authenticate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    public java.security.cert.Certificate [] getLocalCertificates() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        return session.getLocalCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * Returns the certificate chain with which the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * authenticated itself, or throw a SSLPeerUnverifiedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * if the server did not authenticate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    java.security.cert.Certificate [] getServerCertificates()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            throws SSLPeerUnverifiedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        return session.getPeerCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * Returns the principal with which the server authenticated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * itself, or throw a SSLPeerUnverifiedException if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * server did not authenticate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    Principal getPeerPrincipal()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            throws SSLPeerUnverifiedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        Principal principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            principal = session.getPeerPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        } catch (AbstractMethodError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            // if the provider does not support it, fallback to peer certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            // return the X500Principal of the end-entity cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            java.security.cert.Certificate[] certs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                        session.getPeerCertificates();
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10419
diff changeset
   713
            principal = ((X509Certificate)certs[0]).getSubjectX500Principal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        return principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * Returns the principal the client sent to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * server, or null if the client did not authenticate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    Principal getLocalPrincipal()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        Principal principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            principal = session.getLocalPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        } catch (AbstractMethodError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            principal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            // if the provider does not support it, fallback to local certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            // return the X500Principal of the end-entity cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            java.security.cert.Certificate[] certs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                        session.getLocalCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            if (certs != null) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10419
diff changeset
   734
                principal = ((X509Certificate)certs[0]).getSubjectX500Principal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        return principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * This method implements the SSL HandshakeCompleted callback,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * remembering the resulting session so that it may be queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * for the current cipher suite and peer certificates.  Servers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * sometimes re-initiate handshaking, so the session in use on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * a given connection may change.  When sessions change, so may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * peer identities and cipher suites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    public void handshakeCompleted(HandshakeCompletedEvent event)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        session = event.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * @return the proxy host being used for this client, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     *          if we're not going through a proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     */
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   757
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    public String getProxyHostUsed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        if (!needsTunneling()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        } else {
5160
c0e0c9a9d338 6632169: HttpClient and HttpsClient should not try to reverse lookup IP address of a proxy server
chegar
parents: 4236
diff changeset
   762
            return super.getProxyHostUsed();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * @return the proxy port being used for this client.  Meaningless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *          if getProxyHostUsed() gives null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     */
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 5160
diff changeset
   770
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    public int getProxyPortUsed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        return (proxy == null || proxy.type() == Proxy.Type.DIRECT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                proxy.type() == Proxy.Type.SOCKS)? -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            ((InetSocketAddress)proxy.address()).getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
}