jdk/src/share/classes/com/sun/net/ssl/internal/www/protocol/https/DelegateHttpsURLConnection.java
author chegar
Fri, 16 Sep 2011 12:09:04 -0700
changeset 10596 39b3a979e600
parent 5506 202f599c92aa
child 14342 8435a30053c1
permissions -rw-r--r--
7090158: Networking Libraries don't build with javac -Werror Summary: Minor changes to networking java files to remove warnings Reviewed-by: chegar, weijun, hawtin Contributed-by: kurchi.subhra.hazra@oracle.com, sasha_bu@hotmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
     2
 * Copyright (c) 2001, 2009, 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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class DelegateHttpsURLConnection extends AbstractDelegateHttpsURLConnection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // we need a reference to the HttpsURLConnection to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // the properties set there
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // we also need it to be public so that it can be referenced
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // from sun.net.www.protocol.http.HttpURLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // this is for ResponseCache.put(URI, URLConnection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // second parameter needs to be cast to javax.net.ssl.HttpsURLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // instead of AbstractDelegateHttpsURLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public com.sun.net.ssl.HttpsURLConnection httpsURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    DelegateHttpsURLConnection(URL url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            sun.net.www.protocol.http.Handler handler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            com.sun.net.ssl.HttpsURLConnection httpsURLConnection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this(url, null, handler, httpsURLConnection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    DelegateHttpsURLConnection(URL url, Proxy p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            sun.net.www.protocol.http.Handler handler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            com.sun.net.ssl.HttpsURLConnection httpsURLConnection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        super(url, p, handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.httpsURLConnection = httpsURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected javax.net.ssl.SSLSocketFactory getSSLSocketFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return httpsURLConnection.getSSLSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected javax.net.ssl.HostnameVerifier getHostnameVerifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // note: getHostnameVerifier() never returns null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return new VerifierWrapper(httpsURLConnection.getHostnameVerifier());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Called by layered delegator's finalize() method to handle closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * the underlying object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    protected void dispose() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        super.finalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
class VerifierWrapper implements javax.net.ssl.HostnameVerifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private com.sun.net.ssl.HostnameVerifier verifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    VerifierWrapper(com.sun.net.ssl.HostnameVerifier verifier) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.verifier = verifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * In com.sun.net.ssl.HostnameVerifier the method is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * as verify(String urlHostname, String certHostname).
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 3957
diff changeset
   111
     * This means we need to extract the hostname from the X.509 certificate
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 3957
diff changeset
   112
     * or from the Kerberos principal name, in this wrapper.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public boolean verify(String hostname, javax.net.ssl.SSLSession session) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            String serverName;
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 3957
diff changeset
   117
            // Use ciphersuite to determine whether Kerberos is active.
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 3957
diff changeset
   118
            if (session.getCipherSuite().startsWith("TLS_KRB5")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                serverName =
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 3957
diff changeset
   120
                    HostnameChecker.getServerName(getPeerPrincipal(session));
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 3957
diff changeset
   121
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 3957
diff changeset
   122
            } else { // X.509
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                Certificate[] serverChain = session.getPeerCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                if ((serverChain == null) || (serverChain.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (serverChain[0] instanceof X509Certificate == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                X509Certificate serverCert = (X509Certificate)serverChain[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                serverName = getServername(serverCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (serverName == null) {
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
            return verifier.verify(hostname, serverName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } catch (javax.net.ssl.SSLPeerUnverifiedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Get the peer principal from the session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private Principal getPeerPrincipal(javax.net.ssl.SSLSession session)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        throws javax.net.ssl.SSLPeerUnverifiedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        Principal principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            principal = session.getPeerPrincipal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } catch (AbstractMethodError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            // if the provider does not support it, return null, since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // we need it only for Kerberos.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            principal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Extract the name of the SSL server from the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Note this code is essentially a subset of the hostname extraction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * code in HostnameChecker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private static String getServername(X509Certificate peerCert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            // compare to subjectAltNames if dnsName is present
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   168
            Collection<List<?>> subjAltNames = peerCert.getSubjectAlternativeNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if (subjAltNames != null) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   170
                for (Iterator<List<?>> itr = subjAltNames.iterator(); itr.hasNext(); ) {
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 5506
diff changeset
   171
                    List<?> next = itr.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    if (((Integer)next.get(0)).intValue() == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        // compare dNSName with host in url
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        String dnsName = ((String)next.get(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        return dnsName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            // else check against common name in the subject field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            X500Name subject = HostnameChecker.getSubjectX500Name(peerCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            DerValue derValue = subject.findMostSpecificAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                                (X500Name.commonName_oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (derValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    String name = derValue.getAsString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } catch (java.security.cert.CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
}