jdk/src/java.base/share/classes/sun/security/util/HostnameChecker.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 24685 jdk/src/share/classes/sun/security/util/HostnameChecker.java@215fa91e1b4c
child 28865 4729ff15079b
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
    36
import sun.security.ssl.Krb5Helper;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.x509.X500Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.net.util.IPAddressUtil;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public final static byte TYPE_TLS = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private final static HostnameChecker INSTANCE_TLS =
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public final static byte TYPE_LDAP = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private final static HostnameChecker INSTANCE_LDAP =
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private final static int ALTNAME_DNS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private final static int ALTNAME_IP  = 7;
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @exception CertificateException if the name does not match any of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * the names specified in the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public void match(String expectedName, X509Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (isIpAddress(expectedName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
           matchIP(expectedName, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
           matchDNS(expectedName, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Perform the check for Kerberos.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
   100
    public static boolean match(String expectedName, Principal principal) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        String hostName = getServerName(principal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return (expectedName.equalsIgnoreCase(hostName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Return the Server name from Kerberos principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
   108
    public static String getServerName(Principal principal) {
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
   109
        return Krb5Helper.getPrincipalHostName(principal);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Test whether the given hostname looks like a literal IPv4 or IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * address. The hostname does not need to be a fully qualified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * This is not a strict check that performs full input validation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * That means if the method returns true, name need not be a correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * IP address, rather that it does not represent a valid DNS hostname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Likewise for IP addresses when it returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private static boolean isIpAddress(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (IPAddressUtil.isIPv4LiteralAddress(name) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            IPAddressUtil.isIPv6LiteralAddress(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Check if the certificate allows use of the given IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * From RFC2818:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * In some cases, the URI is specified as an IP address rather than a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * hostname. In this case, the iPAddress subjectAltName must be present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * in the certificate and must exactly match the IP in the URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private static void matchIP(String expectedIP, X509Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        Collection<List<?>> subjAltNames = cert.getSubjectAlternativeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (subjAltNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throw new CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                ("No subject alternative names present");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        for (List<?> next : subjAltNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            // For IP address, it needs to be exact match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (((Integer)next.get(0)).intValue() == ALTNAME_IP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                String ipAddress = (String)next.get(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                if (expectedIP.equalsIgnoreCase(ipAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        throw new CertificateException("No subject alternative " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        "names matching " + "IP address " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        expectedIP + " found");
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
     * Check if the certificate allows use of the given DNS name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * From RFC2818:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * If a subjectAltName extension of type dNSName is present, that MUST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * be used as the identity. Otherwise, the (most specific) Common Name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * field in the Subject field of the certificate MUST be used. Although
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * the use of the Common Name is existing practice, it is deprecated and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Certification Authorities are encouraged to use the dNSName instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Matching is performed using the matching rules specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * [RFC2459].  If more than one identity of a given type is present in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * the certificate (e.g., more than one dNSName name, a match in any one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * of the set is considered acceptable.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private void matchDNS(String expectedName, X509Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        Collection<List<?>> subjAltNames = cert.getSubjectAlternativeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (subjAltNames != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            boolean foundDNS = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            for ( List<?> next : subjAltNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                if (((Integer)next.get(0)).intValue() == ALTNAME_DNS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    foundDNS = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    String dnsName = (String)next.get(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    if (isMatched(expectedName, dnsName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (foundDNS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                // if certificate contains any subject alt names of type DNS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                // but none match, reject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                throw new CertificateException("No subject alternative DNS "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        + "name matching " + expectedName + " found.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        X500Name subjectName = getSubjectX500Name(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        DerValue derValue = subjectName.findMostSpecificAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                                    (X500Name.commonName_oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (derValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                if (isMatched(expectedName, derValue.getAsString())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        String msg = "No name matching " + expectedName + " found";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        throw new CertificateException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Return the subject of a certificate as X500Name, by reparsing if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * 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
   215
     * is required, in other cases X500Principal is to be preferred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * This method is currently used from within JSSE, do not remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public static X500Name getSubjectX500Name(X509Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            throws CertificateParsingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            Principal subjectDN = cert.getSubjectDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if (subjectDN instanceof X500Name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                return (X500Name)subjectDN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                X500Principal subjectX500 = cert.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                return new X500Name(subjectX500.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throw(CertificateParsingException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                new CertificateParsingException().initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Returns true if name matches against template.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * The matching is performed as per RFC 2818 rules for TLS and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * RFC 2830 rules for LDAP.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * The <code>name</code> parameter should represent a DNS name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * The <code>template</code> parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * may contain the wildcard character *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private boolean isMatched(String name, String template) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (checkType == TYPE_TLS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            return matchAllWildcards(name, template);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } else if (checkType == TYPE_LDAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return matchLeftmostWildcard(name, template);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Returns true if name matches against template.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * According to RFC 2818, section 3.1 -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Names may contain the wildcard character * which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * considered to match any single domain name component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * or component fragment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * E.g., *.a.com matches foo.a.com but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * bar.foo.a.com. f*.com matches foo.com but not bar.com.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    private static boolean matchAllWildcards(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
         String template) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
   269
        name = name.toLowerCase(Locale.ENGLISH);
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
   270
        template = template.toLowerCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        StringTokenizer nameSt = new StringTokenizer(name, ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        StringTokenizer templateSt = new StringTokenizer(template, ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (nameSt.countTokens() != templateSt.countTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        while (nameSt.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (!matchWildCards(nameSt.nextToken(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        templateSt.nextToken())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Returns true if name matches against template.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * As per RFC 2830, section 3.6 -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * The "*" wildcard character is allowed.  If present, it applies only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * to the left-most name component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * E.g. *.bar.com would match a.bar.com, b.bar.com, etc. but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * bar.com.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    private static boolean matchLeftmostWildcard(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                         String template) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
   299
        name = name.toLowerCase(Locale.ENGLISH);
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
   300
        template = template.toLowerCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // Retreive leftmost component
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23010
diff changeset
   303
        int templateIdx = template.indexOf('.');
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23010
diff changeset
   304
        int nameIdx = name.indexOf('.');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (templateIdx == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            templateIdx = template.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (nameIdx == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            nameIdx = name.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (matchWildCards(name.substring(0, nameIdx),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            template.substring(0, templateIdx))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            // match rest of the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return template.substring(templateIdx).equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                        name.substring(nameIdx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Returns true if the name matches against the template that may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * contain wildcard char * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private static boolean matchWildCards(String name, String template) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23010
diff changeset
   329
        int wildcardIdx = template.indexOf('*');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if (wildcardIdx == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            return name.equals(template);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        boolean isBeginning = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        String beforeWildcard = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        String afterWildcard = template;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        while (wildcardIdx != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            // match in sequence the non-wildcard chars in the template.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            beforeWildcard = afterWildcard.substring(0, wildcardIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            afterWildcard = afterWildcard.substring(wildcardIdx + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            int beforeStartIdx = name.indexOf(beforeWildcard);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            if ((beforeStartIdx == -1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        (isBeginning && beforeStartIdx != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            isBeginning = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            // update the match scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            name = name.substring(beforeStartIdx + beforeWildcard.length());
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23010
diff changeset
   352
            wildcardIdx = afterWildcard.indexOf('*');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return name.endsWith(afterWildcard);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
}