src/java.base/share/classes/sun/security/util/HostnameChecker.java
author xuelei
Mon, 25 Jun 2018 13:41:39 -0700
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
child 50788 6274aee1f692
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
/*
45981
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
     2
 * Copyright (c) 2002, 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 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.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.cert.*;
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
    33
import java.util.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.security.auth.x500.X500Principal;
45981
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
    35
import javax.net.ssl.SNIHostName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
    37
import sun.net.util.IPAddressUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.x509.X500Name;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    39
import sun.security.ssl.SSLLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Class to check hostnames against the names specified in a certificate as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * required for TLS and LDAP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class HostnameChecker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // Constant for a HostnameChecker for TLS
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
    49
    public static final byte TYPE_TLS = 1;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
    50
    private static final HostnameChecker INSTANCE_TLS =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                        new HostnameChecker(TYPE_TLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // Constant for a HostnameChecker for LDAP
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
    54
    public static final byte TYPE_LDAP = 2;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
    55
    private static final HostnameChecker INSTANCE_LDAP =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                                        new HostnameChecker(TYPE_LDAP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // 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
    59
    private static final int ALTNAME_DNS = 2;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30905
diff changeset
    60
    private static final int ALTNAME_IP  = 7;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // the algorithm to follow to perform the check. Currently unused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private final byte checkType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private HostnameChecker(byte checkType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.checkType = checkType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Get a HostnameChecker instance. checkType should be one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * TYPE_* constants defined in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static HostnameChecker getInstance(byte checkType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (checkType == TYPE_TLS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            return INSTANCE_TLS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        } else if (checkType == TYPE_LDAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            return INSTANCE_LDAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        throw new IllegalArgumentException("Unknown check type: " + checkType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Perform the check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
    85
     * @param expectedName the expected host name or ip address
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
    86
     * @param cert the certificate to check against
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
    87
     * @param chainsToPublicCA true if the certificate chains to a public
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
    88
     *     root CA (as pre-installed in the cacerts file)
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
    89
     * @throws CertificateException if the name does not match any of
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
    90
     *     the names specified in the certificate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
    92
    public void match(String expectedName, X509Certificate cert,
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
    93
                      boolean chainsToPublicCA) throws CertificateException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (isIpAddress(expectedName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
           matchIP(expectedName, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } else {
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
    97
           matchDNS(expectedName, cert, chainsToPublicCA);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   101
    public void match(String expectedName, X509Certificate cert)
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   102
            throws CertificateException {
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   103
        match(expectedName, cert, false);
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   104
    }
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   105
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Test whether the given hostname looks like a literal IPv4 or IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * address. The hostname does not need to be a fully qualified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * This is not a strict check that performs full input validation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * That means if the method returns true, name need not be a correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * IP address, rather that it does not represent a valid DNS hostname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Likewise for IP addresses when it returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private static boolean isIpAddress(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (IPAddressUtil.isIPv4LiteralAddress(name) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            IPAddressUtil.isIPv6LiteralAddress(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Check if the certificate allows use of the given IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * From RFC2818:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * In some cases, the URI is specified as an IP address rather than a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * hostname. In this case, the iPAddress subjectAltName must be present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * in the certificate and must exactly match the IP in the URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private static void matchIP(String expectedIP, X509Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Collection<List<?>> subjAltNames = cert.getSubjectAlternativeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (subjAltNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                ("No subject alternative names present");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        for (List<?> next : subjAltNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // For IP address, it needs to be exact match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (((Integer)next.get(0)).intValue() == ALTNAME_IP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                String ipAddress = (String)next.get(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                if (expectedIP.equalsIgnoreCase(ipAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    return;
28865
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   145
                } else {
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   146
                    // compare InetAddress objects in order to ensure
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   147
                    // equality between a long IPv6 address and its
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   148
                    // abbreviated form.
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   149
                    try {
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   150
                        if (InetAddress.getByName(expectedIP).equals(
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   151
                                InetAddress.getByName(ipAddress))) {
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   152
                            return;
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   153
                        }
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   154
                    } catch (UnknownHostException e) {
4729ff15079b 8065553: Failed Java web start via IPv6 (Java7u71 or later)
robm
parents: 25859
diff changeset
   155
                    } catch (SecurityException e) {}
2
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
        throw new CertificateException("No subject alternative " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        "names matching " + "IP address " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        expectedIP + " found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Check if the certificate allows use of the given DNS name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * From RFC2818:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * If a subjectAltName extension of type dNSName is present, that MUST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * be used as the identity. Otherwise, the (most specific) Common Name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * field in the Subject field of the certificate MUST be used. Although
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * the use of the Common Name is existing practice, it is deprecated and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Certification Authorities are encouraged to use the dNSName instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Matching is performed using the matching rules specified by
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   175
     * [RFC5280].  If more than one identity of a given type is present in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * the certificate (e.g., more than one dNSName name, a match in any one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * of the set is considered acceptable.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   179
    private void matchDNS(String expectedName, X509Certificate cert,
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   180
                          boolean chainsToPublicCA)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throws CertificateException {
45981
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   182
        // Check that the expected name is a valid domain name.
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   183
        try {
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   184
            // Using the checking implemented in SNIHostName
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   185
            SNIHostName sni = new SNIHostName(expectedName);
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   186
        } catch (IllegalArgumentException iae) {
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   187
            throw new CertificateException(
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   188
                "Illegal given domain name: " + expectedName, iae);
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   189
        }
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   190
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        Collection<List<?>> subjAltNames = cert.getSubjectAlternativeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (subjAltNames != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            boolean foundDNS = false;
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   194
            for (List<?> next : subjAltNames) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                if (((Integer)next.get(0)).intValue() == ALTNAME_DNS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    foundDNS = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    String dnsName = (String)next.get(1);
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   198
                    if (isMatched(expectedName, dnsName, chainsToPublicCA)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (foundDNS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                // if certificate contains any subject alt names of type DNS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                // but none match, reject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                throw new CertificateException("No subject alternative DNS "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        + "name matching " + expectedName + " found.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        X500Name subjectName = getSubjectX500Name(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        DerValue derValue = subjectName.findMostSpecificAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                                    (X500Name.commonName_oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (derValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            try {
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   215
                if (isMatched(expectedName, derValue.getAsString(),
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   216
                              chainsToPublicCA)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        String msg = "No name matching " + expectedName + " found";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        throw new CertificateException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Return the subject of a certificate as X500Name, by reparsing if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * 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
   231
     * is required, in other cases X500Principal is to be preferred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * This method is currently used from within JSSE, do not remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public static X500Name getSubjectX500Name(X509Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            throws CertificateParsingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            Principal subjectDN = cert.getSubjectDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (subjectDN instanceof X500Name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                return (X500Name)subjectDN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                X500Principal subjectX500 = cert.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                return new X500Name(subjectX500.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            throw(CertificateParsingException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                new CertificateParsingException().initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
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
     * Returns true if name matches against template.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * The matching is performed as per RFC 2818 rules for TLS and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * RFC 2830 rules for LDAP.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * The <code>name</code> parameter should represent a DNS name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * The <code>template</code> parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * may contain the wildcard character *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   262
    private boolean isMatched(String name, String template,
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   263
                              boolean chainsToPublicCA) {
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   264
        if (hasIllegalWildcard(name, template, chainsToPublicCA)) {
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   265
            return false;
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   266
        }
45981
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   267
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   268
        // check the validity of the domain name template.
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   269
        try {
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   270
            // Replacing wildcard character '*' with 'x' so as to check
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   271
            // the domain name template validity.
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   272
            //
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   273
            // Using the checking implemented in SNIHostName
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   274
            SNIHostName sni = new SNIHostName(template.replace('*', 'x'));
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   275
        } catch (IllegalArgumentException iae) {
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   276
            // It would be nice to add debug log if not matching.
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   277
            return false;
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   278
        }
2ef418883ade 8174873: Improved certificate procesing
xuelei
parents: 44760
diff changeset
   279
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (checkType == TYPE_TLS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return matchAllWildcards(name, template);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } else if (checkType == TYPE_LDAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return matchLeftmostWildcard(name, template);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   289
    /**
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   290
     * Returns true if the template contains an illegal wildcard character.
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   291
     */
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   292
    private static boolean hasIllegalWildcard(String domain, String template,
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   293
                                              boolean chainsToPublicCA) {
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   294
        // not ok if it is a single wildcard character or "*."
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   295
        if (template.equals("*") || template.equals("*.")) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   296
            if (SSLLogger.isOn) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   297
                SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   298
                    "Certificate domain name has illegal single " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   299
                      "wildcard character: " + template);
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   300
            }
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   301
            return true;
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   302
        }
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   303
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   304
        int lastWildcardIndex = template.lastIndexOf("*");
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   305
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   306
        // ok if it has no wildcard character
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   307
        if (lastWildcardIndex == -1) {
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   308
            return false;
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   309
        }
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   310
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   311
        String afterWildcard = template.substring(lastWildcardIndex);
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   312
        int firstDotIndex = afterWildcard.indexOf(".");
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   313
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   314
        // not ok if there is no dot after wildcard (ex: "*com")
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   315
        if (firstDotIndex == -1) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   316
            if (SSLLogger.isOn) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   317
                SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   318
                    "Certificate domain name has illegal wildcard, " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   319
                    "no dot after wildcard character: " + template);
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   320
            }
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   321
            return true;
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   322
        }
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   323
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   324
        // If the wildcarded domain is a top-level domain under which names
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   325
        // can be registered, then a wildcard is not allowed.
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   326
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   327
        if (!chainsToPublicCA) {
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   328
            return false; // skip check for non-public certificates
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   329
        }
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   330
        Optional<RegisteredDomain> rd = RegisteredDomain.from(domain)
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   331
                .filter(d -> d.type() == RegisteredDomain.Type.ICANN);
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   332
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   333
        if (rd.isPresent()) {
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   334
            String wDomain = afterWildcard.substring(firstDotIndex + 1);
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   335
            if (rd.get().publicSuffix().equalsIgnoreCase(wDomain)) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   336
                if (SSLLogger.isOn) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   337
                    SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   338
                        "Certificate domain name has illegal " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   339
                        "wildcard for public suffix: " + template);
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   340
                }
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   341
                return true;
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   342
            }
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   343
        }
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   344
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   345
        return false;
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   346
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * Returns true if name matches against template.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * According to RFC 2818, section 3.1 -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Names may contain the wildcard character * which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * considered to match any single domain name component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * or component fragment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * E.g., *.a.com matches foo.a.com but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * bar.foo.a.com. f*.com matches foo.com but not bar.com.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    private static boolean matchAllWildcards(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         String template) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
   360
        name = name.toLowerCase(Locale.ENGLISH);
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
   361
        template = template.toLowerCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        StringTokenizer nameSt = new StringTokenizer(name, ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        StringTokenizer templateSt = new StringTokenizer(template, ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (nameSt.countTokens() != templateSt.countTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        while (nameSt.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            if (!matchWildCards(nameSt.nextToken(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                        templateSt.nextToken())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Returns true if name matches against template.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * As per RFC 2830, section 3.6 -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * The "*" wildcard character is allowed.  If present, it applies only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * to the left-most name component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * E.g. *.bar.com would match a.bar.com, b.bar.com, etc. but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * bar.com.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    private static boolean matchLeftmostWildcard(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                         String template) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
   390
        name = name.toLowerCase(Locale.ENGLISH);
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
   391
        template = template.toLowerCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   393
        // Retrieve leftmost component
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   394
        int templateIdx = template.indexOf(".");
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   395
        int nameIdx = name.indexOf(".");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (templateIdx == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            templateIdx = template.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (nameIdx == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            nameIdx = name.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (matchWildCards(name.substring(0, nameIdx),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            template.substring(0, templateIdx))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            // match rest of the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            return template.substring(templateIdx).equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                        name.substring(nameIdx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * Returns true if the name matches against the template that may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * contain wildcard char * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    private static boolean matchWildCards(String name, String template) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   420
        int wildcardIdx = template.indexOf("*");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if (wildcardIdx == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            return name.equals(template);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        boolean isBeginning = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        String beforeWildcard = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        String afterWildcard = template;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        while (wildcardIdx != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            // match in sequence the non-wildcard chars in the template.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            beforeWildcard = afterWildcard.substring(0, wildcardIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            afterWildcard = afterWildcard.substring(wildcardIdx + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            int beforeStartIdx = name.indexOf(beforeWildcard);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            if ((beforeStartIdx == -1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        (isBeginning && beforeStartIdx != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            isBeginning = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            // update the match scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            name = name.substring(beforeStartIdx + beforeWildcard.length());
44760
61b03b960583 8038893: Recertify certificate matching
mullan
parents: 32649
diff changeset
   443
            wildcardIdx = afterWildcard.indexOf("*");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        return name.endsWith(afterWildcard);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
}