0N/A/*
2619N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.x509;
0N/A
0N/Aimport java.io.IOException;
2619N/Aimport java.util.Locale;
0N/A
0N/Aimport sun.security.util.*;
0N/A
0N/A/**
0N/A * This class implements the DNSName as required by the GeneralNames
0N/A * ASN.1 object.
0N/A * <p>
0N/A * [RFC2459] When the subjectAltName extension contains a domain name service
0N/A * label, the domain name MUST be stored in the dNSName (an IA5String).
0N/A * The name MUST be in the "preferred name syntax," as specified by RFC
0N/A * 1034 [RFC 1034]. Note that while upper and lower case letters are
0N/A * allowed in domain names, no signifigance is attached to the case. In
0N/A * addition, while the string " " is a legal domain name, subjectAltName
0N/A * extensions with a dNSName " " are not permitted. Finally, the use of
0N/A * the DNS representation for Internet mail addresses (wpolk.nist.gov
0N/A * instead of wpolk@nist.gov) is not permitted; such identities are to
0N/A * be encoded as rfc822Name.
0N/A * <p>
0N/A * @author Amit Kapoor
0N/A * @author Hemma Prafullchandra
0N/A */
0N/Apublic class DNSName implements GeneralNameInterface {
0N/A private String name;
0N/A
0N/A private static final String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
0N/A private static final String digitsAndHyphen = "0123456789-";
0N/A private static final String alphaDigitsAndHyphen = alpha + digitsAndHyphen;
0N/A
0N/A /**
0N/A * Create the DNSName object from the passed encoded Der value.
0N/A *
0N/A * @param derValue the encoded DER DNSName.
0N/A * @exception IOException on error.
0N/A */
0N/A public DNSName(DerValue derValue) throws IOException {
0N/A name = derValue.getIA5String();
0N/A }
0N/A
0N/A /**
0N/A * Create the DNSName object with the specified name.
0N/A *
0N/A * @param name the DNSName.
0N/A * @throws IOException if the name is not a valid DNSName subjectAltName
0N/A */
0N/A public DNSName(String name) throws IOException {
0N/A if (name == null || name.length() == 0)
0N/A throw new IOException("DNS name must not be null");
0N/A if (name.indexOf(' ') != -1)
0N/A throw new IOException("DNS names or NameConstraints with blank components are not permitted");
0N/A if (name.charAt(0) == '.' || name.charAt(name.length() -1) == '.')
0N/A throw new IOException("DNS names or NameConstraints may not begin or end with a .");
0N/A //Name will consist of label components separated by "."
0N/A //startIndex is the index of the first character of a component
0N/A //endIndex is the index of the last character of a component plus 1
0N/A for (int endIndex,startIndex=0; startIndex < name.length(); startIndex = endIndex+1) {
0N/A endIndex = name.indexOf('.', startIndex);
0N/A if (endIndex < 0) {
0N/A endIndex = name.length();
0N/A }
0N/A if ((endIndex-startIndex) < 1)
0N/A throw new IOException("DNSName SubjectAltNames with empty components are not permitted");
0N/A
0N/A //DNSName components must begin with a letter A-Z or a-z
0N/A if (alpha.indexOf(name.charAt(startIndex)) < 0)
0N/A throw new IOException("DNSName components must begin with a letter");
0N/A //nonStartIndex: index for characters in the component beyond the first one
0N/A for (int nonStartIndex=startIndex+1; nonStartIndex < endIndex; nonStartIndex++) {
0N/A char x = name.charAt(nonStartIndex);
0N/A if ((alphaDigitsAndHyphen).indexOf(x) < 0)
0N/A throw new IOException("DNSName components must consist of letters, digits, and hyphens");
0N/A }
0N/A }
0N/A this.name = name;
0N/A }
0N/A
0N/A /**
0N/A * Return the type of the GeneralName.
0N/A */
0N/A public int getType() {
0N/A return (GeneralNameInterface.NAME_DNS);
0N/A }
0N/A
0N/A /**
0N/A * Return the actual name value of the GeneralName.
0N/A */
0N/A public String getName() {
0N/A return name;
0N/A }
0N/A
0N/A /**
0N/A * Encode the DNS name into the DerOutputStream.
0N/A *
0N/A * @param out the DER stream to encode the DNSName to.
0N/A * @exception IOException on encoding errors.
0N/A */
0N/A public void encode(DerOutputStream out) throws IOException {
0N/A out.putIA5String(name);
0N/A }
0N/A
0N/A /**
0N/A * Convert the name into user readable string.
0N/A */
0N/A public String toString() {
0N/A return ("DNSName: " + name);
0N/A }
0N/A
0N/A /**
0N/A * Compares this name with another, for equality.
0N/A *
0N/A * @return true iff the names are equivalent
0N/A * according to RFC2459.
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (this == obj)
0N/A return true;
0N/A
0N/A if (!(obj instanceof DNSName))
0N/A return false;
0N/A
0N/A DNSName other = (DNSName)obj;
0N/A
0N/A // RFC2459 mandates that these names are
0N/A // not case-sensitive
0N/A return name.equalsIgnoreCase(other.name);
0N/A }
0N/A
0N/A /**
0N/A * Returns the hash code value for this object.
0N/A *
0N/A * @return a hash code value for this object.
0N/A */
0N/A public int hashCode() {
0N/A return name.toUpperCase().hashCode();
0N/A }
0N/A
0N/A /**
0N/A * Return type of constraint inputName places on this name:<ul>
0N/A * <li>NAME_DIFF_TYPE = -1: input name is different type from name (i.e. does not constrain).
0N/A * <li>NAME_MATCH = 0: input name matches name.
0N/A * <li>NAME_NARROWS = 1: input name narrows name (is lower in the naming subtree)
0N/A * <li>NAME_WIDENS = 2: input name widens name (is higher in the naming subtree)
0N/A * <li>NAME_SAME_TYPE = 3: input name does not match or narrow name, but is same type.
0N/A * </ul>. These results are used in checking NameConstraints during
0N/A * certification path verification.
0N/A * <p>
0N/A * RFC2459: DNS name restrictions are expressed as foo.bar.com. Any subdomain
0N/A * satisfies the name constraint. For example, www.foo.bar.com would
0N/A * satisfy the constraint but bigfoo.bar.com would not.
0N/A * <p>
0N/A * draft-ietf-pkix-new-part1-00.txt: DNS name restrictions are expressed as foo.bar.com.
0N/A * Any DNS name that
0N/A * can be constructed by simply adding to the left hand side of the name
0N/A * satisfies the name constraint. For example, www.foo.bar.com would
0N/A * satisfy the constraint but foo1.bar.com would not.
0N/A * <p>
0N/A * RFC1034: By convention, domain names can be stored with arbitrary case, but
0N/A * domain name comparisons for all present domain functions are done in a
0N/A * case-insensitive manner, assuming an ASCII character set, and a high
0N/A * order zero bit.
0N/A * <p>
0N/A * @param inputName to be checked for being constrained
0N/A * @returns constraint type above
0N/A * @throws UnsupportedOperationException if name is not exact match, but narrowing and widening are
0N/A * not supported for this name type.
0N/A */
0N/A public int constrains(GeneralNameInterface inputName) throws UnsupportedOperationException {
0N/A int constraintType;
0N/A if (inputName == null)
0N/A constraintType = NAME_DIFF_TYPE;
0N/A else if (inputName.getType() != NAME_DNS)
0N/A constraintType = NAME_DIFF_TYPE;
0N/A else {
2619N/A String inName =
2619N/A (((DNSName)inputName).getName()).toLowerCase(Locale.ENGLISH);
2619N/A String thisName = name.toLowerCase(Locale.ENGLISH);
0N/A if (inName.equals(thisName))
0N/A constraintType = NAME_MATCH;
0N/A else if (thisName.endsWith(inName)) {
0N/A int inNdx = thisName.lastIndexOf(inName);
0N/A if (thisName.charAt(inNdx-1) == '.' )
0N/A constraintType = NAME_WIDENS;
0N/A else
0N/A constraintType = NAME_SAME_TYPE;
0N/A } else if (inName.endsWith(thisName)) {
0N/A int ndx = inName.lastIndexOf(thisName);
0N/A if (inName.charAt(ndx-1) == '.' )
0N/A constraintType = NAME_NARROWS;
0N/A else
0N/A constraintType = NAME_SAME_TYPE;
0N/A } else {
0N/A constraintType = NAME_SAME_TYPE;
0N/A }
0N/A }
0N/A return constraintType;
0N/A }
0N/A
0N/A /**
0N/A * Return subtree depth of this name for purposes of determining
0N/A * NameConstraints minimum and maximum bounds and for calculating
0N/A * path lengths in name subtrees.
0N/A *
0N/A * @returns distance of name from root
0N/A * @throws UnsupportedOperationException if not supported for this name type
0N/A */
0N/A public int subtreeDepth() throws UnsupportedOperationException {
0N/A String subtree=name;
0N/A int i=1;
0N/A
0N/A /* count dots */
0N/A for (; subtree.lastIndexOf('.') >= 0; i++) {
0N/A subtree=subtree.substring(0,subtree.lastIndexOf('.'));
0N/A }
0N/A
0N/A return i;
0N/A }
0N/A
0N/A}