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