jdk/src/share/classes/sun/security/x509/URIName.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2004 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * [RFC3280] When the subjectAltName extension contains a URI, the name MUST be
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * rules specified in [RFC 1738].  The name must include both a scheme
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * As specified in [RFC 1738], the scheme name is not case-sensitive
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw (IOException) new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                ("invalid URI name:" + name).initCause(use);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (uri.getScheme() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new IOException("URI name must include scheme:" + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        host = uri.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // RFC 3280 says that the host should be non-null, but we allow it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // be null because some widely deployed certificates contain CDP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // extensions with URIs that have no hostname (see bugs 4802236 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // 5107944).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (host.charAt(0) == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                // Verify host is a valid IPv6 address name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                String ipV6Host = host.substring(1, host.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    hostIP = new IPAddressName(ipV6Host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    throw new IOException("invalid URI name (host " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        "portion is not a valid IPv6 address):" + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    hostDNS = new DNSName(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    // Not a valid DNS Name; see if it is a valid IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    // IPAddressName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        hostIP = new IPAddressName(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    } catch (Exception ioe2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        throw new IOException("invalid URI name (host " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                            "portion is not a valid DNS name, IPv4 address," +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                            " or IPv6 address):" + name);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Create the URIName object with the specified name constraint. URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * name constraints syntax is different than SubjectAltNames, etc. See
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * 4.2.1.11 of RFC 3280.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param value the URI name constraint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @throws IOException if name is not a proper URI name constraint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public static URIName nameConstraint(DerValue value) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        URI uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        String name = value.getIA5String();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            uri = new URI(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } catch (URISyntaxException use) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw (IOException) new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                ("invalid URI name constraint:" + name).initCause(use);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (uri.getScheme() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            String host = uri.getSchemeSpecificPart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                DNSName hostDNS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                if (host.charAt(0) == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    hostDNS = new DNSName(host.substring(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    hostDNS = new DNSName(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                return new URIName(uri, host, hostDNS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                throw (IOException) new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    ("invalid URI name constraint:" + name).initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new IOException("invalid URI name constraint (should not " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                "include scheme):" + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    URIName(URI uri, String host, DNSName hostDNS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        this.uri = uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        this.host = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        this.hostDNS = hostDNS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Return the type of the GeneralName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public int getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return GeneralNameInterface.NAME_URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Encode the URI name into the DerOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param out the DER stream to encode the URIName to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @exception IOException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public void encode(DerOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        out.putIA5String(uri.toASCIIString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Convert the name into user readable string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return "URIName: " + uri.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Compares this name with another, for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @return true iff the names are equivalent according to RFC2459.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (!(obj instanceof URIName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        URIName other = (URIName) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return uri.equals(other.getURI());
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
     * Returns the URIName as a java.net.URI object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public URI getURI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Returns this URI name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return uri.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Return the scheme name portion of a URIName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @returns scheme portion of full name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public String getScheme() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return uri.getScheme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Return the host name or IP address portion of the URIName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @returns host name or IP address portion of full name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public String getHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Return the host object type; if host name is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * DNSName, then this host object does not include any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * initial "." on the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @returns host name as DNSName or IPAddressName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public Object getHostObject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (hostIP != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return hostIP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            return hostDNS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return uri.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Return type of constraint inputName places on this name:<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *   <li>NAME_DIFF_TYPE = -1: input name is different type from name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *       (i.e. does not constrain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *   <li>NAME_MATCH = 0: input name matches name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *   <li>NAME_NARROWS = 1: input name narrows name (is lower in the naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *       subtree)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *   <li>NAME_WIDENS = 2: input name widens name (is higher in the naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *       subtree)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *   <li>NAME_SAME_TYPE = 3: input name does not match or narrow name, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *       is same type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * </ul>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * These results are used in checking NameConstraints during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * certification path verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * RFC3280: For URIs, the constraint applies to the host part of the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * The constraint may specify a host or a domain.  Examples would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * "foo.bar.com";  and ".xyz.com".  When the the constraint begins with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * a period, it may be expanded with one or more subdomains.  That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * the constraint ".xyz.com" is satisfied by both abc.xyz.com and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * abc.def.xyz.com.  However, the constraint ".xyz.com" is not satisfied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * by "xyz.com".  When the constraint does not begin with a period, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * specifies a host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @param inputName to be checked for being constrained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @returns constraint type above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @throws UnsupportedOperationException if name is not exact match, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *  narrowing and widening are not supported for this name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public int constrains(GeneralNameInterface inputName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        throws UnsupportedOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        int constraintType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (inputName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            constraintType = NAME_DIFF_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        } else if (inputName.getType() != NAME_URI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            constraintType = NAME_DIFF_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            // Assuming from here on that one or both of these is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            // actually a URI name constraint (not a URI), so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            // only need to compare the host portion of the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            String otherHost = ((URIName)inputName).getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            // Quick check for equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (otherHost.equalsIgnoreCase(host)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                constraintType = NAME_MATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                Object otherHostObject = ((URIName)inputName).getHostObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                if ((hostDNS == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    !(otherHostObject instanceof DNSName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    // If one (or both) is an IP address, only same type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    // Both host portions are DNS names. Are they domains?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    boolean thisDomain = (host.charAt(0) == '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    boolean otherDomain = (otherHost.charAt(0) == '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    DNSName otherDNS = (DNSName) otherHostObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    // Run DNSName.constrains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    constraintType = hostDNS.constrains(otherDNS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    // If neither one is a domain, then they can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    // widen or narrow. That's just SAME_TYPE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    if ((!thisDomain && !otherDomain) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                        ((constraintType == NAME_WIDENS) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                         (constraintType == NAME_NARROWS))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    // If one is a domain and the other isn't,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    // then they can't match. The one that's a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    // domain doesn't include the one that's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    // not a domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    if ((thisDomain != otherDomain) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        (constraintType == NAME_MATCH)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        if (thisDomain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                            constraintType = NAME_WIDENS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                            constraintType = NAME_NARROWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return constraintType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Return subtree depth of this name for purposes of determining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * NameConstraints minimum and maximum bounds and for calculating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * path lengths in name subtrees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @returns distance of name from root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @throws UnsupportedOperationException if not supported for this name type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public int subtreeDepth() throws UnsupportedOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        DNSName dnsName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            dnsName = new DNSName(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            throw new UnsupportedOperationException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return dnsName.subtreeDepth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
}