1 /*
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.security.x509;
27
28 import java.io.IOException;
29 import java.util.Locale;
30
31 import sun.security.util.*;
32
33 /**
34 * This class implements the DNSName as required by the GeneralNames
35 * ASN.1 object.
36 * <p>
37 * [RFC2459] When the subjectAltName extension contains a domain name service
38 * label, the domain name MUST be stored in the dNSName (an IA5String).
39 * The name MUST be in the "preferred name syntax," as specified by RFC
40 * 1034 [RFC 1034]. Note that while upper and lower case letters are
41 * allowed in domain names, no signifigance is attached to the case. In
42 * addition, while the string " " is a legal domain name, subjectAltName
43 * extensions with a dNSName " " are not permitted. Finally, the use of
44 * the DNS representation for Internet mail addresses (wpolk.nist.gov
45 * instead of wpolk@nist.gov) is not permitted; such identities are to
46 * be encoded as rfc822Name.
47 * <p>
48 * @author Amit Kapoor
49 * @author Hemma Prafullchandra
50 */
51 public class DNSName implements GeneralNameInterface {
52 private String name;
53
54 private static final String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
55 private static final String digitsAndHyphen = "0123456789-";
56 private static final String alphaDigitsAndHyphen = alpha + digitsAndHyphen;
57
58 /**
59 * Create the DNSName object from the passed encoded Der value.
60 *
61 * @param derValue the encoded DER DNSName.
62 * @exception IOException on error.
63 */
64 public DNSName(DerValue derValue) throws IOException {
65 name = derValue.getIA5String();
66 }
67
68 /**
69 * Create the DNSName object with the specified name.
70 *
71 * @param name the DNSName.
72 * @throws IOException if the name is not a valid DNSName subjectAltName
73 */
74 public DNSName(String name) throws IOException {
75 if (name == null || name.length() == 0)
76 throw new IOException("DNS name must not be null");
77 if (name.indexOf(' ') != -1)
78 throw new IOException("DNS names or NameConstraints with blank components are not permitted");
79 if (name.charAt(0) == '.' || name.charAt(name.length() -1) == '.')
80 throw new IOException("DNS names or NameConstraints may not begin or end with a .");
81 //Name will consist of label components separated by "."
82 //startIndex is the index of the first character of a component
83 //endIndex is the index of the last character of a component plus 1
84 for (int endIndex,startIndex=0; startIndex < name.length(); startIndex = endIndex+1) {
85 endIndex = name.indexOf('.', startIndex);
86 if (endIndex < 0) {
87 endIndex = name.length();
88 }
89 if ((endIndex-startIndex) < 1)
90 throw new IOException("DNSName SubjectAltNames with empty components are not permitted");
91
92 //DNSName components must begin with a letter A-Z or a-z
93 if (alpha.indexOf(name.charAt(startIndex)) < 0)
94 throw new IOException("DNSName components must begin with a letter");
95 //nonStartIndex: index for characters in the component beyond the first one
96 for (int nonStartIndex=startIndex+1; nonStartIndex < endIndex; nonStartIndex++) {
97 char x = name.charAt(nonStartIndex);
98 if ((alphaDigitsAndHyphen).indexOf(x) < 0)
99 throw new IOException("DNSName components must consist of letters, digits, and hyphens");
100 }
101 }
102 this.name = name;
103 }
104
105 /**
106 * Return the type of the GeneralName.
107 */
108 public int getType() {
109 return (GeneralNameInterface.NAME_DNS);
110 }
111
112 /**
113 * Return the actual name value of the GeneralName.
114 */
115 public String getName() {
116 return name;
117 }
118
119 /**
120 * Encode the DNS name into the DerOutputStream.
121 *
122 * @param out the DER stream to encode the DNSName to.
123 * @exception IOException on encoding errors.
124 */
125 public void encode(DerOutputStream out) throws IOException {
126 out.putIA5String(name);
127 }
128
129 /**
130 * Convert the name into user readable string.
131 */
132 public String toString() {
133 return ("DNSName: " + name);
134 }
135
136 /**
137 * Compares this name with another, for equality.
138 *
139 * @return true iff the names are equivalent
140 * according to RFC2459.
141 */
142 public boolean equals(Object obj) {
143 if (this == obj)
144 return true;
145
146 if (!(obj instanceof DNSName))
147 return false;
148
149 DNSName other = (DNSName)obj;
150
151 // RFC2459 mandates that these names are
152 // not case-sensitive
153 return name.equalsIgnoreCase(other.name);
154 }
155
156 /**
157 * Returns the hash code value for this object.
158 *
159 * @return a hash code value for this object.
160 */
161 public int hashCode() {
162 return name.toUpperCase(Locale.ENGLISH).hashCode();
163 }
164
165 /**
166 * Return type of constraint inputName places on this name:<ul>
167 * <li>NAME_DIFF_TYPE = -1: input name is different type from name (i.e. does not constrain).
168 * <li>NAME_MATCH = 0: input name matches name.
169 * <li>NAME_NARROWS = 1: input name narrows name (is lower in the naming subtree)
170 * <li>NAME_WIDENS = 2: input name widens name (is higher in the naming subtree)
171 * <li>NAME_SAME_TYPE = 3: input name does not match or narrow name, but is same type.
172 * </ul>. These results are used in checking NameConstraints during
173 * certification path verification.
174 * <p>
175 * RFC2459: DNS name restrictions are expressed as foo.bar.com. Any subdomain
176 * satisfies the name constraint. For example, www.foo.bar.com would
177 * satisfy the constraint but bigfoo.bar.com would not.
178 * <p>
179 * draft-ietf-pkix-new-part1-00.txt: DNS name restrictions are expressed as foo.bar.com.
180 * Any DNS name that
181 * can be constructed by simply adding to the left hand side of the name
182 * satisfies the name constraint. For example, www.foo.bar.com would
183 * satisfy the constraint but foo1.bar.com would not.
184 * <p>
185 * RFC1034: By convention, domain names can be stored with arbitrary case, but
186 * domain name comparisons for all present domain functions are done in a
187 * case-insensitive manner, assuming an ASCII character set, and a high
188 * order zero bit.
189 * <p>
190 * @param inputName to be checked for being constrained
191 * @returns constraint type above
192 * @throws UnsupportedOperationException if name is not exact match, but narrowing and widening are
193 * not supported for this name type.
194 */
195 public int constrains(GeneralNameInterface inputName) throws UnsupportedOperationException {
196 int constraintType;
197 if (inputName == null)
198 constraintType = NAME_DIFF_TYPE;
199 else if (inputName.getType() != NAME_DNS)
200 constraintType = NAME_DIFF_TYPE;
|
1 /*
2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.security.x509;
27
28 import java.io.IOException;
29 import java.util.Locale;
30
31 import sun.security.util.*;
32
33 /**
34 * This class implements the DNSName as required by the GeneralNames
35 * ASN.1 object.
36 * <p>
37 * [RFC5280] When the subjectAltName extension contains a domain name system
38 * label, the domain name MUST be stored in the dNSName (an IA5String).
39 * The name MUST be in the "preferred name syntax", as specified by
40 * Section 3.5 of [RFC1034] and as modified by Section 2.1 of
41 * [RFC1123]. Note that while uppercase and lowercase letters are
42 * allowed in domain names, no significance is attached to the case. In
43 * addition, while the string " " is a legal domain name, subjectAltName
44 * extensions with a dNSName of " " MUST NOT be used. Finally, the use
45 * of the DNS representation for Internet mail addresses
46 * (subscriber.example.com instead of subscriber@example.com) MUST NOT
47 * be used; such identities are to be encoded as rfc822Name.
48 * <p>
49 * @author Amit Kapoor
50 * @author Hemma Prafullchandra
51 */
52 public class DNSName implements GeneralNameInterface {
53 private String name;
54
55 private static final String alphaDigits =
56 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
57
58 /**
59 * Create the DNSName object from the passed encoded Der value.
60 *
61 * @param derValue the encoded DER DNSName.
62 * @exception IOException on error.
63 */
64 public DNSName(DerValue derValue) throws IOException {
65 name = derValue.getIA5String();
66 }
67
68 /**
69 * Create the DNSName object with the specified name.
70 *
71 * @param name the DNSName.
72 * @throws IOException if the name is not a valid DNSName subjectAltName
73 */
74 public DNSName(String name) throws IOException {
75 if (name == null || name.length() == 0)
76 throw new IOException("DNSName must not be null or empty");
77 if (name.contains(" "))
78 throw new IOException("DNSName with blank components is not permitted");
79 if (name.startsWith(".") || name.endsWith("."))
80 throw new IOException("DNSName may not begin or end with a .");
81 /*
82 * Name will consist of label components separated by "."
83 * startIndex is the index of the first character of a component
84 * endIndex is the index of the last character of a component plus 1
85 */
86 for (int endIndex,startIndex = 0; startIndex < name.length(); startIndex = endIndex+1) {
87 endIndex = name.indexOf('.', startIndex);
88 if (endIndex < 0) {
89 endIndex = name.length();
90 }
91 if (endIndex - startIndex < 1)
92 throw new IOException("DNSName with empty components are not permitted");
93
94 // RFC 1123: DNSName components must begin with a letter or digit
95 if (alphaDigits.indexOf(name.charAt(startIndex)) < 0)
96 throw new IOException("DNSName components must begin with a letter or digit");
97 //nonStartIndex: index for characters in the component beyond the first one
98 for (int nonStartIndex=startIndex+1; nonStartIndex < endIndex; nonStartIndex++) {
99 char x = name.charAt(nonStartIndex);
100 if ((alphaDigits).indexOf(x) < 0 && x != '-')
101 throw new IOException("DNSName components must consist of letters, digits, and hyphens");
102 }
103 }
104 this.name = name;
105 }
106
107
108 /**
109 * Return the type of the GeneralName.
110 */
111 public int getType() {
112 return (GeneralNameInterface.NAME_DNS);
113 }
114
115 /**
116 * Return the actual name value of the GeneralName.
117 */
118 public String getName() {
119 return name;
120 }
121
122 /**
123 * Encode the DNSName into the DerOutputStream.
124 *
125 * @param out the DER stream to encode the DNSName to.
126 * @exception IOException on encoding errors.
127 */
128 public void encode(DerOutputStream out) throws IOException {
129 out.putIA5String(name);
130 }
131
132 /**
133 * Convert the name into user readable string.
134 */
135 public String toString() {
136 return ("DNSName: " + name);
137 }
138
139 /**
140 * Compares this name with another, for equality.
141 *
142 * @return true iff the names are equivalent
143 * according to RFC5280.
144 */
145 public boolean equals(Object obj) {
146 if (this == obj)
147 return true;
148
149 if (!(obj instanceof DNSName))
150 return false;
151
152 DNSName other = (DNSName)obj;
153
154 // RFC5280 mandates that these names are
155 // not case-sensitive
156 return name.equalsIgnoreCase(other.name);
157 }
158
159 /**
160 * Returns the hash code value for this object.
161 *
162 * @return a hash code value for this object.
163 */
164 public int hashCode() {
165 return name.toUpperCase(Locale.ENGLISH).hashCode();
166 }
167
168 /**
169 * Return type of constraint inputName places on this name:<ul>
170 * <li>NAME_DIFF_TYPE = -1: input name is different type from name (i.e. does not constrain).
171 * <li>NAME_MATCH = 0: input name matches name.
172 * <li>NAME_NARROWS = 1: input name narrows name (is lower in the naming subtree)
173 * <li>NAME_WIDENS = 2: input name widens name (is higher in the naming subtree)
174 * <li>NAME_SAME_TYPE = 3: input name does not match or narrow name, but is same type.
175 * </ul>. These results are used in checking NameConstraints during
176 * certification path verification.
177 * <p>
178 * RFC5280: DNS name restrictions are expressed as host.example.com.
179 * Any DNS name that can be constructed by simply adding zero or more
180 * labels to the left-hand side of the name satisfies the name constraint.
181 * For example, www.host.example.com would satisfy the constraint but
182 * host1.example.com would not.
183 * <p>
184 * draft-ietf-pkix-new-part1-00.txt: DNSName restrictions are expressed as foo.bar.com.
185 * Any DNSName that
186 * can be constructed by simply adding to the left hand side of the name
187 * satisfies the name constraint. For example, www.foo.bar.com would
188 * satisfy the constraint but foo1.bar.com would not.
189 * <p>
190 * RFC1034: By convention, domain names can be stored with arbitrary case, but
191 * domain name comparisons for all present domain functions are done in a
192 * case-insensitive manner, assuming an ASCII character set, and a high
193 * order zero bit.
194 * <p>
195 * @param inputName to be checked for being constrained
196 * @returns constraint type above
197 * @throws UnsupportedOperationException if name is not exact match, but narrowing and widening are
198 * not supported for this name type.
199 */
200 public int constrains(GeneralNameInterface inputName) throws UnsupportedOperationException {
201 int constraintType;
202 if (inputName == null)
203 constraintType = NAME_DIFF_TYPE;
204 else if (inputName.getType() != NAME_DNS)
205 constraintType = NAME_DIFF_TYPE;
|