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