jdk/src/java.base/share/classes/sun/security/util/HostnameChecker.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 30905 bba6fefdd660
child 44760 61b03b960583
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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 sun.security.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
28865
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
    29
import java.net.InetAddress;
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
    30
import java.net.UnknownHostException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 28865
diff changeset
    38
import sun.security.ssl.ClientKeyExchangeService;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.x509.X500Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.net.util.IPAddressUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Class to check hostnames against the names specified in a certificate as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * required for TLS and LDAP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class HostnameChecker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // Constant for a HostnameChecker for TLS
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
    51
    public static final byte TYPE_TLS = 1;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
    52
    private static final HostnameChecker INSTANCE_TLS =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                                        new HostnameChecker(TYPE_TLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // Constant for a HostnameChecker for LDAP
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
    56
    public static final byte TYPE_LDAP = 2;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
    57
    private static final HostnameChecker INSTANCE_LDAP =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                                        new HostnameChecker(TYPE_LDAP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // constants for subject alt names of type DNS and IP
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
    61
    private static final int ALTNAME_DNS = 2;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
    62
    private static final int ALTNAME_IP  = 7;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // the algorithm to follow to perform the check. Currently unused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private final byte checkType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private HostnameChecker(byte checkType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.checkType = checkType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Get a HostnameChecker instance. checkType should be one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * TYPE_* constants defined in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static HostnameChecker getInstance(byte checkType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (checkType == TYPE_TLS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            return INSTANCE_TLS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        } else if (checkType == TYPE_LDAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            return INSTANCE_LDAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        throw new IllegalArgumentException("Unknown check type: " + checkType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Perform the check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @exception CertificateException if the name does not match any of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * the names specified in the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public void match(String expectedName, X509Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (isIpAddress(expectedName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
           matchIP(expectedName, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
           matchDNS(expectedName, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
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
     * Perform the check for Kerberos.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
   102
    public static boolean match(String expectedName, Principal principal) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        String hostName = getServerName(principal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return (expectedName.equalsIgnoreCase(hostName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Return the Server name from Kerberos principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
   110
    public static String getServerName(Principal principal) {
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 28865
diff changeset
   111
        ClientKeyExchangeService p =
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 28865
diff changeset
   112
                ClientKeyExchangeService.find("KRB5");
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 28865
diff changeset
   113
        if (p == null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 28865
diff changeset
   114
            throw new AssertionError("Kerberos should have been available");
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 28865
diff changeset
   115
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents: 28865
diff changeset
   116
        return p.getServiceHostName(principal);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Test whether the given hostname looks like a literal IPv4 or IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * address. The hostname does not need to be a fully qualified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * This is not a strict check that performs full input validation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * That means if the method returns true, name need not be a correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * IP address, rather that it does not represent a valid DNS hostname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Likewise for IP addresses when it returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private static boolean isIpAddress(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (IPAddressUtil.isIPv4LiteralAddress(name) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            IPAddressUtil.isIPv6LiteralAddress(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
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
     * Check if the certificate allows use of the given IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * From RFC2818:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * In some cases, the URI is specified as an IP address rather than a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * hostname. In this case, the iPAddress subjectAltName must be present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * in the certificate and must exactly match the IP in the URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private static void matchIP(String expectedIP, X509Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        Collection<List<?>> subjAltNames = cert.getSubjectAlternativeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (subjAltNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                ("No subject alternative names present");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        for (List<?> next : subjAltNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // For IP address, it needs to be exact match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (((Integer)next.get(0)).intValue() == ALTNAME_IP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                String ipAddress = (String)next.get(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                if (expectedIP.equalsIgnoreCase(ipAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    return;
28865
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   158
                } else {
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   159
                    // compare InetAddress objects in order to ensure
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   160
                    // equality between a long IPv6 address and its
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   161
                    // abbreviated form.
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   162
                    try {
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   163
                        if (InetAddress.getByName(expectedIP).equals(
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   164
                                InetAddress.getByName(ipAddress))) {
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   165
                            return;
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   166
                        }
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   167
                    } catch (UnknownHostException e) {
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   168
                    } catch (SecurityException e) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        throw new CertificateException("No subject alternative " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        "names matching " + "IP address " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        expectedIP + " found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Check if the certificate allows use of the given DNS name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * From RFC2818:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * If a subjectAltName extension of type dNSName is present, that MUST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * be used as the identity. Otherwise, the (most specific) Common Name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * field in the Subject field of the certificate MUST be used. Although
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * the use of the Common Name is existing practice, it is deprecated and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Certification Authorities are encouraged to use the dNSName instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Matching is performed using the matching rules specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * [RFC2459].  If more than one identity of a given type is present in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * the certificate (e.g., more than one dNSName name, a match in any one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * of the set is considered acceptable.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private void matchDNS(String expectedName, X509Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        Collection<List<?>> subjAltNames = cert.getSubjectAlternativeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (subjAltNames != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            boolean foundDNS = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            for ( List<?> next : subjAltNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                if (((Integer)next.get(0)).intValue() == ALTNAME_DNS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    foundDNS = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    String dnsName = (String)next.get(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    if (isMatched(expectedName, dnsName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (foundDNS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                // if certificate contains any subject alt names of type DNS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                // but none match, reject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                throw new CertificateException("No subject alternative DNS "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        + "name matching " + expectedName + " found.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        X500Name subjectName = getSubjectX500Name(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        DerValue derValue = subjectName.findMostSpecificAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                                    (X500Name.commonName_oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (derValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                if (isMatched(expectedName, derValue.getAsString())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        String msg = "No name matching " + expectedName + " found";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        throw new CertificateException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Return the subject of a certificate as X500Name, by reparsing if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * necessary. X500Name should only be used if access to name components
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14342
diff changeset
   233
     * is required, in other cases X500Principal is to be preferred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * This method is currently used from within JSSE, do not remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public static X500Name getSubjectX500Name(X509Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throws CertificateParsingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            Principal subjectDN = cert.getSubjectDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (subjectDN instanceof X500Name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                return (X500Name)subjectDN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                X500Principal subjectX500 = cert.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                return new X500Name(subjectX500.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            throw(CertificateParsingException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                new CertificateParsingException().initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
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
     * Returns true if name matches against template.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * The matching is performed as per RFC 2818 rules for TLS and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * RFC 2830 rules for LDAP.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * The <code>name</code> parameter should represent a DNS name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * The <code>template</code> parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * may contain the wildcard character *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    private boolean isMatched(String name, String template) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (checkType == TYPE_TLS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            return matchAllWildcards(name, template);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        } else if (checkType == TYPE_LDAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return matchLeftmostWildcard(name, template);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Returns true if name matches against template.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * According to RFC 2818, section 3.1 -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Names may contain the wildcard character * which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * considered to match any single domain name component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * or component fragment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * E.g., *.a.com matches foo.a.com but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * bar.foo.a.com. f*.com matches foo.com but not bar.com.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    private static boolean matchAllWildcards(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         String template) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
   287
        name = name.toLowerCase(Locale.ENGLISH);
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
   288
        template = template.toLowerCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        StringTokenizer nameSt = new StringTokenizer(name, ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        StringTokenizer templateSt = new StringTokenizer(template, ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (nameSt.countTokens() != templateSt.countTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        while (nameSt.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (!matchWildCards(nameSt.nextToken(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        templateSt.nextToken())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Returns true if name matches against template.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * As per RFC 2830, section 3.6 -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * The "*" wildcard character is allowed.  If present, it applies only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * to the left-most name component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * E.g. *.bar.com would match a.bar.com, b.bar.com, etc. but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * bar.com.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    private static boolean matchLeftmostWildcard(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                         String template) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
   317
        name = name.toLowerCase(Locale.ENGLISH);
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
   318
        template = template.toLowerCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        // Retreive leftmost component
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23010
diff changeset
   321
        int templateIdx = template.indexOf('.');
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23010
diff changeset
   322
        int nameIdx = name.indexOf('.');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (templateIdx == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            templateIdx = template.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (nameIdx == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            nameIdx = name.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (matchWildCards(name.substring(0, nameIdx),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            template.substring(0, templateIdx))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            // match rest of the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            return template.substring(templateIdx).equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                        name.substring(nameIdx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Returns true if the name matches against the template that may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * contain wildcard char * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    private static boolean matchWildCards(String name, String template) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23010
diff changeset
   347
        int wildcardIdx = template.indexOf('*');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (wildcardIdx == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            return name.equals(template);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        boolean isBeginning = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        String beforeWildcard = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        String afterWildcard = template;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        while (wildcardIdx != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            // match in sequence the non-wildcard chars in the template.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            beforeWildcard = afterWildcard.substring(0, wildcardIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            afterWildcard = afterWildcard.substring(wildcardIdx + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            int beforeStartIdx = name.indexOf(beforeWildcard);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            if ((beforeStartIdx == -1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        (isBeginning && beforeStartIdx != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            isBeginning = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            // update the match scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            name = name.substring(beforeStartIdx + beforeWildcard.length());
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23010
diff changeset
   370
            wildcardIdx = afterWildcard.indexOf('*');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return name.endsWith(afterWildcard);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
}