src/java.base/share/classes/com/sun/net/ssl/internal/www/protocol/https/DelegateHttpsURLConnection.java
author xuelei
Mon, 25 Jun 2018 13:41:39 -0700
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
permissions -rw-r--r--
8196584: TLS 1.3 Implementation Reviewed-by: ascarpino, coffeys, dfuchs, jjiang, jnimeh, mullan, rhalade, ssahoo, valeriep, weijun, wetmore, xuelei Contributed-by: Adam Petcher <adam.petcher@oracle.com>, Amanda Jiang <amanda.jiang@oracle.com>, Anthony Scarpino <anthony.scarpino@oracle.com>, Bradford Wetmore <bradford.wetmore@oracle.com>, Jamil Nimeh <jamil.j.nimeh@oracle.com>, John Jiang <sha.jiang@oracle.com>, Rajan Halade <rajan.halade@oracle.com>, Sibabrata Sahoo <sibabrata.sahoo@oracle.com>, Valerie Peng <valerie.peng@oracle.com>, Weijun Wang <weijun.wang@oracle.com>, Xuelei Fan <xuelei.fan@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43541
9aeb1de77a63 8173827: Remove forRemoval=true from several deprecated security APIs
mullan
parents: 39562
diff changeset
     2
 * Copyright (c) 2001, 2017, 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: 4236
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: 4236
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: 4236
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.net.ssl.internal.www.protocol.https;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.util.HostnameChecker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.security.x509.X500Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This class was introduced to provide an additional level of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * abstraction between javax.net.ssl.HttpURLConnection and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * com.sun.net.ssl.HttpURLConnection objects. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * javax.net.ssl.HttpURLConnection is used in the new sun.net version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * of protocol implementation (this one)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * com.sun.net.ssl.HttpURLConnection is used in the com.sun version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
43541
9aeb1de77a63 8173827: Remove forRemoval=true from several deprecated security APIs
mullan
parents: 39562
diff changeset
    56
@Deprecated(since="9")
27804
4659e70271c4 8066617: Suppress deprecation warnings in java.base module
darcy
parents: 25859
diff changeset
    57
@SuppressWarnings("deprecation") // HttpsURLConnection is deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class DelegateHttpsURLConnection extends AbstractDelegateHttpsURLConnection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // we need a reference to the HttpsURLConnection to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // the properties set there
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // we also need it to be public so that it can be referenced
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // from sun.net.www.protocol.http.HttpURLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // this is for ResponseCache.put(URI, URLConnection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // second parameter needs to be cast to javax.net.ssl.HttpsURLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // instead of AbstractDelegateHttpsURLConnection
27804
4659e70271c4 8066617: Suppress deprecation warnings in java.base module
darcy
parents: 25859
diff changeset
    67
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public com.sun.net.ssl.HttpsURLConnection httpsURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    DelegateHttpsURLConnection(URL url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            sun.net.www.protocol.http.Handler handler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            com.sun.net.ssl.HttpsURLConnection httpsURLConnection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this(url, null, handler, httpsURLConnection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    DelegateHttpsURLConnection(URL url, Proxy p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            sun.net.www.protocol.http.Handler handler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            com.sun.net.ssl.HttpsURLConnection httpsURLConnection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        super(url, p, handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this.httpsURLConnection = httpsURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    protected javax.net.ssl.SSLSocketFactory getSSLSocketFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return httpsURLConnection.getSSLSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    protected javax.net.ssl.HostnameVerifier getHostnameVerifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // note: getHostnameVerifier() never returns null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return new VerifierWrapper(httpsURLConnection.getHostnameVerifier());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Called by layered delegator's finalize() method to handle closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * the underlying object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    protected void dispose() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        super.finalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
class VerifierWrapper implements javax.net.ssl.HostnameVerifier {
27804
4659e70271c4 8066617: Suppress deprecation warnings in java.base module
darcy
parents: 25859
diff changeset
   104
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private com.sun.net.ssl.HostnameVerifier verifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
27804
4659e70271c4 8066617: Suppress deprecation warnings in java.base module
darcy
parents: 25859
diff changeset
   107
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    VerifierWrapper(com.sun.net.ssl.HostnameVerifier verifier) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this.verifier = verifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * In com.sun.net.ssl.HostnameVerifier the method is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * as verify(String urlHostname, String certHostname).
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 3957
diff changeset
   115
     * This means we need to extract the hostname from the X.509 certificate
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   116
     * in this wrapper.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public boolean verify(String hostname, javax.net.ssl.SSLSession session) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        try {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   120
            Certificate[] serverChain = session.getPeerCertificates();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   121
            if ((serverChain == null) || (serverChain.length == 0)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   122
                return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   124
            if (serverChain[0] instanceof X509Certificate == false) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   125
                return false;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   126
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   127
            X509Certificate serverCert = (X509Certificate)serverChain[0];
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   128
            String serverName = getServername(serverCert);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if (serverName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return verifier.verify(hostname, serverName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } catch (javax.net.ssl.SSLPeerUnverifiedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Extract the name of the SSL server from the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Note this code is essentially a subset of the hostname extraction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * code in HostnameChecker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private static String getServername(X509Certificate peerCert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            // compare to subjectAltNames if dnsName is present
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   147
            Collection<List<?>> subjAltNames = peerCert.getSubjectAlternativeNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (subjAltNames != null) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   149
                for (Iterator<List<?>> itr = subjAltNames.iterator(); itr.hasNext(); ) {
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   150
                    List<?> next = itr.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    if (((Integer)next.get(0)).intValue() == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        // compare dNSName with host in url
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        String dnsName = ((String)next.get(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        return dnsName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            // else check against common name in the subject field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            X500Name subject = HostnameChecker.getSubjectX500Name(peerCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            DerValue derValue = subject.findMostSpecificAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                                (X500Name.commonName_oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (derValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    String name = derValue.getAsString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } catch (java.security.cert.CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
}