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