2
|
1 |
/*
|
|
2 |
* Copyright 1997-2000 Sun Microsystems, Inc. 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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.security.x509;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
|
|
30 |
import sun.security.util.*;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* This class implements the DNSName as required by the GeneralNames
|
|
34 |
* ASN.1 object.
|
|
35 |
* <p>
|
|
36 |
* [RFC2459] When the subjectAltName extension contains a domain name service
|
|
37 |
* label, the domain name MUST be stored in the dNSName (an IA5String).
|
|
38 |
* The name MUST be in the "preferred name syntax," as specified by RFC
|
|
39 |
* 1034 [RFC 1034]. Note that while upper and lower case letters are
|
|
40 |
* allowed in domain names, no signifigance is attached to the case. In
|
|
41 |
* addition, while the string " " is a legal domain name, subjectAltName
|
|
42 |
* extensions with a dNSName " " are not permitted. Finally, the use of
|
|
43 |
* the DNS representation for Internet mail addresses (wpolk.nist.gov
|
|
44 |
* instead of wpolk@nist.gov) is not permitted; such identities are to
|
|
45 |
* be encoded as rfc822Name.
|
|
46 |
* <p>
|
|
47 |
* @author Amit Kapoor
|
|
48 |
* @author Hemma Prafullchandra
|
|
49 |
*/
|
|
50 |
public class DNSName implements GeneralNameInterface {
|
|
51 |
private String name;
|
|
52 |
|
|
53 |
private static final String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
54 |
private static final String digitsAndHyphen = "0123456789-";
|
|
55 |
private static final String alphaDigitsAndHyphen = alpha + digitsAndHyphen;
|
|
56 |
|
|
57 |
/**
|
|
58 |
* Create the DNSName object from the passed encoded Der value.
|
|
59 |
*
|
|
60 |
* @param derValue the encoded DER DNSName.
|
|
61 |
* @exception IOException on error.
|
|
62 |
*/
|
|
63 |
public DNSName(DerValue derValue) throws IOException {
|
|
64 |
name = derValue.getIA5String();
|
|
65 |
}
|
|
66 |
|
|
67 |
/**
|
|
68 |
* Create the DNSName object with the specified name.
|
|
69 |
*
|
|
70 |
* @param name the DNSName.
|
|
71 |
* @throws IOException if the name is not a valid DNSName subjectAltName
|
|
72 |
*/
|
|
73 |
public DNSName(String name) throws IOException {
|
|
74 |
if (name == null || name.length() == 0)
|
|
75 |
throw new IOException("DNS name must not be null");
|
|
76 |
if (name.indexOf(' ') != -1)
|
|
77 |
throw new IOException("DNS names or NameConstraints with blank components are not permitted");
|
|
78 |
if (name.charAt(0) == '.' || name.charAt(name.length() -1) == '.')
|
|
79 |
throw new IOException("DNS names or NameConstraints may not begin or end with a .");
|
|
80 |
//Name will consist of label components separated by "."
|
|
81 |
//startIndex is the index of the first character of a component
|
|
82 |
//endIndex is the index of the last character of a component plus 1
|
|
83 |
for (int endIndex,startIndex=0; startIndex < name.length(); startIndex = endIndex+1) {
|
|
84 |
endIndex = name.indexOf('.', startIndex);
|
|
85 |
if (endIndex < 0) {
|
|
86 |
endIndex = name.length();
|
|
87 |
}
|
|
88 |
if ((endIndex-startIndex) < 1)
|
|
89 |
throw new IOException("DNSName SubjectAltNames with empty components are not permitted");
|
|
90 |
|
|
91 |
//DNSName components must begin with a letter A-Z or a-z
|
|
92 |
if (alpha.indexOf(name.charAt(startIndex)) < 0)
|
|
93 |
throw new IOException("DNSName components must begin with a letter");
|
|
94 |
//nonStartIndex: index for characters in the component beyond the first one
|
|
95 |
for (int nonStartIndex=startIndex+1; nonStartIndex < endIndex; nonStartIndex++) {
|
|
96 |
char x = name.charAt(nonStartIndex);
|
|
97 |
if ((alphaDigitsAndHyphen).indexOf(x) < 0)
|
|
98 |
throw new IOException("DNSName components must consist of letters, digits, and hyphens");
|
|
99 |
}
|
|
100 |
}
|
|
101 |
this.name = name;
|
|
102 |
}
|
|
103 |
|
|
104 |
/**
|
|
105 |
* Return the type of the GeneralName.
|
|
106 |
*/
|
|
107 |
public int getType() {
|
|
108 |
return (GeneralNameInterface.NAME_DNS);
|
|
109 |
}
|
|
110 |
|
|
111 |
/**
|
|
112 |
* Return the actual name value of the GeneralName.
|
|
113 |
*/
|
|
114 |
public String getName() {
|
|
115 |
return name;
|
|
116 |
}
|
|
117 |
|
|
118 |
/**
|
|
119 |
* Encode the DNS name into the DerOutputStream.
|
|
120 |
*
|
|
121 |
* @param out the DER stream to encode the DNSName to.
|
|
122 |
* @exception IOException on encoding errors.
|
|
123 |
*/
|
|
124 |
public void encode(DerOutputStream out) throws IOException {
|
|
125 |
out.putIA5String(name);
|
|
126 |
}
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Convert the name into user readable string.
|
|
130 |
*/
|
|
131 |
public String toString() {
|
|
132 |
return ("DNSName: " + name);
|
|
133 |
}
|
|
134 |
|
|
135 |
/**
|
|
136 |
* Compares this name with another, for equality.
|
|
137 |
*
|
|
138 |
* @return true iff the names are equivalent
|
|
139 |
* according to RFC2459.
|
|
140 |
*/
|
|
141 |
public boolean equals(Object obj) {
|
|
142 |
if (this == obj)
|
|
143 |
return true;
|
|
144 |
|
|
145 |
if (!(obj instanceof DNSName))
|
|
146 |
return false;
|
|
147 |
|
|
148 |
DNSName other = (DNSName)obj;
|
|
149 |
|
|
150 |
// RFC2459 mandates that these names are
|
|
151 |
// not case-sensitive
|
|
152 |
return name.equalsIgnoreCase(other.name);
|
|
153 |
}
|
|
154 |
|
|
155 |
/**
|
|
156 |
* Returns the hash code value for this object.
|
|
157 |
*
|
|
158 |
* @return a hash code value for this object.
|
|
159 |
*/
|
|
160 |
public int hashCode() {
|
|
161 |
return name.toUpperCase().hashCode();
|
|
162 |
}
|
|
163 |
|
|
164 |
/**
|
|
165 |
* Return type of constraint inputName places on this name:<ul>
|
|
166 |
* <li>NAME_DIFF_TYPE = -1: input name is different type from name (i.e. does not constrain).
|
|
167 |
* <li>NAME_MATCH = 0: input name matches name.
|
|
168 |
* <li>NAME_NARROWS = 1: input name narrows name (is lower in the naming subtree)
|
|
169 |
* <li>NAME_WIDENS = 2: input name widens name (is higher in the naming subtree)
|
|
170 |
* <li>NAME_SAME_TYPE = 3: input name does not match or narrow name, but is same type.
|
|
171 |
* </ul>. These results are used in checking NameConstraints during
|
|
172 |
* certification path verification.
|
|
173 |
* <p>
|
|
174 |
* RFC2459: DNS name restrictions are expressed as foo.bar.com. Any subdomain
|
|
175 |
* satisfies the name constraint. For example, www.foo.bar.com would
|
|
176 |
* satisfy the constraint but bigfoo.bar.com would not.
|
|
177 |
* <p>
|
|
178 |
* draft-ietf-pkix-new-part1-00.txt: DNS name restrictions are expressed as foo.bar.com.
|
|
179 |
* Any DNS name that
|
|
180 |
* can be constructed by simply adding to the left hand side of the name
|
|
181 |
* satisfies the name constraint. For example, www.foo.bar.com would
|
|
182 |
* satisfy the constraint but foo1.bar.com would not.
|
|
183 |
* <p>
|
|
184 |
* RFC1034: By convention, domain names can be stored with arbitrary case, but
|
|
185 |
* domain name comparisons for all present domain functions are done in a
|
|
186 |
* case-insensitive manner, assuming an ASCII character set, and a high
|
|
187 |
* order zero bit.
|
|
188 |
* <p>
|
|
189 |
* @param inputName to be checked for being constrained
|
|
190 |
* @returns constraint type above
|
|
191 |
* @throws UnsupportedOperationException if name is not exact match, but narrowing and widening are
|
|
192 |
* not supported for this name type.
|
|
193 |
*/
|
|
194 |
public int constrains(GeneralNameInterface inputName) throws UnsupportedOperationException {
|
|
195 |
int constraintType;
|
|
196 |
if (inputName == null)
|
|
197 |
constraintType = NAME_DIFF_TYPE;
|
|
198 |
else if (inputName.getType() != NAME_DNS)
|
|
199 |
constraintType = NAME_DIFF_TYPE;
|
|
200 |
else {
|
|
201 |
String inName = (((DNSName)inputName).getName()).toLowerCase();
|
|
202 |
String thisName = name.toLowerCase();
|
|
203 |
if (inName.equals(thisName))
|
|
204 |
constraintType = NAME_MATCH;
|
|
205 |
else if (thisName.endsWith(inName)) {
|
|
206 |
int inNdx = thisName.lastIndexOf(inName);
|
|
207 |
if (thisName.charAt(inNdx-1) == '.' )
|
|
208 |
constraintType = NAME_WIDENS;
|
|
209 |
else
|
|
210 |
constraintType = NAME_SAME_TYPE;
|
|
211 |
} else if (inName.endsWith(thisName)) {
|
|
212 |
int ndx = inName.lastIndexOf(thisName);
|
|
213 |
if (inName.charAt(ndx-1) == '.' )
|
|
214 |
constraintType = NAME_NARROWS;
|
|
215 |
else
|
|
216 |
constraintType = NAME_SAME_TYPE;
|
|
217 |
} else {
|
|
218 |
constraintType = NAME_SAME_TYPE;
|
|
219 |
}
|
|
220 |
}
|
|
221 |
return constraintType;
|
|
222 |
}
|
|
223 |
|
|
224 |
/**
|
|
225 |
* Return subtree depth of this name for purposes of determining
|
|
226 |
* NameConstraints minimum and maximum bounds and for calculating
|
|
227 |
* path lengths in name subtrees.
|
|
228 |
*
|
|
229 |
* @returns distance of name from root
|
|
230 |
* @throws UnsupportedOperationException if not supported for this name type
|
|
231 |
*/
|
|
232 |
public int subtreeDepth() throws UnsupportedOperationException {
|
|
233 |
String subtree=name;
|
|
234 |
int i=1;
|
|
235 |
|
|
236 |
/* count dots */
|
|
237 |
for (; subtree.lastIndexOf('.') >= 0; i++) {
|
|
238 |
subtree=subtree.substring(0,subtree.lastIndexOf('.'));
|
|
239 |
}
|
|
240 |
|
|
241 |
return i;
|
|
242 |
}
|
|
243 |
|
|
244 |
}
|