jdk/src/java.base/share/classes/sun/security/x509/URIName.java
author juh
Tue, 07 Oct 2014 22:23:19 -0700
changeset 26967 c182469301ee
parent 25859 3317bb8137f4
child 28059 e576535359cc
permissions -rw-r--r--
8037550: Update RFC references in javadoc to RFC 5280 Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2014, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.x509;
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.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.URISyntaxException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class implements the URIName as required by the GeneralNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * ASN.1 object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
    38
 * [RFC5280] When the subjectAltName extension contains a URI, the name MUST be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * stored in the uniformResourceIdentifier (an IA5String). The name MUST
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * be a non-relative URL, and MUST follow the URL syntax and encoding
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
    41
 * rules specified in [RFC 3986].  The name must include both a scheme
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * (e.g., "http" or "ftp") and a scheme-specific-part.  The scheme-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * specific-part must include a fully qualified domain name or IP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * address as the host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
    46
 * As specified in [RFC 3986], the scheme name is not case-sensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * (e.g., "http" is equivalent to "HTTP").  The host part is also not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * case-sensitive, but other components of the scheme-specific-part may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * be case-sensitive. When comparing URIs, conforming implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * MUST compare the scheme and host without regard to case, but assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the remainder of the scheme-specific-part is case sensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * [RFC1738] In general, URLs are written as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <scheme>:<scheme-specific-part>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * A URL contains the name of the scheme being used (<scheme>) followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * by a colon and then a string (the <scheme-specific-part>) whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * interpretation depends on the scheme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * While the syntax for the rest of the URL may vary depending on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * particular scheme selected, URL schemes that involve the direct use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * of an IP-based protocol to a specified host on the Internet use a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * common syntax for the scheme-specific data:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * //<user>:<password>@<host>:<port>/<url-path>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * [RFC2732] specifies that an IPv6 address contained inside a URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * must be enclosed in square brackets (to allow distinguishing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * colons that separate IPv6 components from the colons that separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * scheme-specific data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @author Steve Hanna
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @see GeneralName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * @see GeneralNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * @see GeneralNameInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
public class URIName implements GeneralNameInterface {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // private attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private URI uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private DNSName hostDNS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private IPAddressName hostIP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Create the URIName object from the passed encoded Der value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param derValue the encoded DER URIName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @exception IOException on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public URIName(DerValue derValue) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this(derValue.getIA5String());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Create the URIName object with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param name the URIName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @throws IOException if name is not a proper URIName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public URIName(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            uri = new URI(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } catch (URISyntaxException use) {
15268
d815f9973d97 8005389: Backout fix for JDK-6500133
juh
parents: 14342
diff changeset
   109
            throw new IOException("invalid URI name:" + name, use);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (uri.getScheme() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new IOException("URI name must include scheme:" + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        host = uri.getHost();
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   116
        // RFC 5280 says that the host should be non-null, but we allow it to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // be null because some widely deployed certificates contain CDP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // extensions with URIs that have no hostname (see bugs 4802236 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // 5107944).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (host.charAt(0) == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                // Verify host is a valid IPv6 address name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                String ipV6Host = host.substring(1, host.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    hostIP = new IPAddressName(ipV6Host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    throw new IOException("invalid URI name (host " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        "portion is not a valid IPv6 address):" + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    hostDNS = new DNSName(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    // Not a valid DNS Name; see if it is a valid IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    // IPAddressName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        hostIP = new IPAddressName(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    } catch (Exception ioe2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                        throw new IOException("invalid URI name (host " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                            "portion is not a valid DNS name, IPv4 address," +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                            " or IPv6 address):" + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Create the URIName object with the specified name constraint. URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * name constraints syntax is different than SubjectAltNames, etc. See
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   151
     * 4.2.1.10 of RFC 5280.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param value the URI name constraint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @throws IOException if name is not a proper URI name constraint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public static URIName nameConstraint(DerValue value) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        URI uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        String name = value.getIA5String();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            uri = new URI(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        } catch (URISyntaxException use) {
13575
017f49f1f3cb 6500133: REGRESSION: CertificateParsingException for CRL Distribution Point with blank
mullan
parents: 5506
diff changeset
   162
            throw new IOException("invalid URI name constraint:" + name, use);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (uri.getScheme() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            String host = uri.getSchemeSpecificPart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                DNSName hostDNS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                if (host.charAt(0) == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    hostDNS = new DNSName(host.substring(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    hostDNS = new DNSName(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                return new URIName(uri, host, hostDNS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            } catch (IOException ioe) {
13575
017f49f1f3cb 6500133: REGRESSION: CertificateParsingException for CRL Distribution Point with blank
mullan
parents: 5506
diff changeset
   175
                throw new IOException("invalid URI name constraint:" + name, ioe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            throw new IOException("invalid URI name constraint (should not " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                "include scheme):" + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    URIName(URI uri, String host, DNSName hostDNS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        this.uri = uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        this.host = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        this.hostDNS = hostDNS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Return the type of the GeneralName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public int getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return GeneralNameInterface.NAME_URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Encode the URI name into the DerOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param out the DER stream to encode the URIName to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @exception IOException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public void encode(DerOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        out.putIA5String(uri.toASCIIString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Convert the name into user readable string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return "URIName: " + uri.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Compares this name with another, for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @return true iff the names are equivalent according to RFC2459.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (!(obj instanceof URIName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        URIName other = (URIName) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return uri.equals(other.getURI());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Returns the URIName as a java.net.URI object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public URI getURI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Returns this URI name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return uri.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Return the scheme name portion of a URIName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @returns scheme portion of full name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public String getScheme() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return uri.getScheme();
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
     * Return the host name or IP address portion of the URIName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @returns host name or IP address portion of full name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public String getHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Return the host object type; if host name is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * DNSName, then this host object does not include any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * initial "." on the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @returns host name as DNSName or IPAddressName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public Object getHostObject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (hostIP != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            return hostIP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            return hostDNS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return uri.hashCode();
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
     * Return type of constraint inputName places on this name:<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *   <li>NAME_DIFF_TYPE = -1: input name is different type from name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *       (i.e. does not constrain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *   <li>NAME_MATCH = 0: input name matches name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *   <li>NAME_NARROWS = 1: input name narrows name (is lower in the naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *       subtree)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *   <li>NAME_WIDENS = 2: input name widens name (is higher in the naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *       subtree)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *   <li>NAME_SAME_TYPE = 3: input name does not match or narrow name, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *       is same type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * </ul>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * These results are used in checking NameConstraints during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * certification path verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <p>
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   303
     * RFC5280: For URIs, the constraint applies to the host part of the name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * The constraint may specify a host or a domain.  Examples would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * "foo.bar.com";  and ".xyz.com".  When the the constraint begins with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * a period, it may be expanded with one or more subdomains.  That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * the constraint ".xyz.com" is satisfied by both abc.xyz.com and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * abc.def.xyz.com.  However, the constraint ".xyz.com" is not satisfied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * by "xyz.com".  When the constraint does not begin with a period, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * specifies a host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @param inputName to be checked for being constrained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @returns constraint type above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @throws UnsupportedOperationException if name is not exact match, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *  narrowing and widening are not supported for this name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public int constrains(GeneralNameInterface inputName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        throws UnsupportedOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        int constraintType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (inputName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            constraintType = NAME_DIFF_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        } else if (inputName.getType() != NAME_URI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            constraintType = NAME_DIFF_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            // Assuming from here on that one or both of these is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            // actually a URI name constraint (not a URI), so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            // only need to compare the host portion of the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            String otherHost = ((URIName)inputName).getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            // Quick check for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if (otherHost.equalsIgnoreCase(host)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                constraintType = NAME_MATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                Object otherHostObject = ((URIName)inputName).getHostObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                if ((hostDNS == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    !(otherHostObject instanceof DNSName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    // If one (or both) is an IP address, only same type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    // Both host portions are DNS names. Are they domains?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    boolean thisDomain = (host.charAt(0) == '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    boolean otherDomain = (otherHost.charAt(0) == '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    DNSName otherDNS = (DNSName) otherHostObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    // Run DNSName.constrains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    constraintType = hostDNS.constrains(otherDNS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    // If neither one is a domain, then they can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    // widen or narrow. That's just SAME_TYPE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    if ((!thisDomain && !otherDomain) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                        ((constraintType == NAME_WIDENS) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                         (constraintType == NAME_NARROWS))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    // If one is a domain and the other isn't,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    // then they can't match. The one that's a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    // domain doesn't include the one that's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    // not a domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    if ((thisDomain != otherDomain) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        (constraintType == NAME_MATCH)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        if (thisDomain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                            constraintType = NAME_WIDENS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                            constraintType = NAME_NARROWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return constraintType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * Return subtree depth of this name for purposes of determining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * NameConstraints minimum and maximum bounds and for calculating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * path lengths in name subtrees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @returns distance of name from root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @throws UnsupportedOperationException if not supported for this name type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public int subtreeDepth() throws UnsupportedOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        DNSName dnsName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            dnsName = new DNSName(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            throw new UnsupportedOperationException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return dnsName.subtreeDepth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
}