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 RFC822Name as required by the GeneralNames
|
|
34 |
* ASN.1 object.
|
|
35 |
*
|
|
36 |
* @author Amit Kapoor
|
|
37 |
* @author Hemma Prafullchandra
|
|
38 |
* @see GeneralName
|
|
39 |
* @see GeneralNames
|
|
40 |
* @see GeneralNameInterface
|
|
41 |
*/
|
|
42 |
public class RFC822Name implements GeneralNameInterface
|
|
43 |
{
|
|
44 |
private String name;
|
|
45 |
|
|
46 |
/**
|
|
47 |
* Create the RFC822Name object from the passed encoded Der value.
|
|
48 |
*
|
|
49 |
* @param derValue the encoded DER RFC822Name.
|
|
50 |
* @exception IOException on error.
|
|
51 |
*/
|
|
52 |
public RFC822Name(DerValue derValue) throws IOException {
|
|
53 |
name = derValue.getIA5String();
|
|
54 |
parseName(name);
|
|
55 |
}
|
|
56 |
|
|
57 |
/**
|
|
58 |
* Create the RFC822Name object with the specified name.
|
|
59 |
*
|
|
60 |
* @param name the RFC822Name.
|
|
61 |
* @throws IOException on invalid input name
|
|
62 |
*/
|
|
63 |
public RFC822Name(String name) throws IOException {
|
|
64 |
parseName(name);
|
|
65 |
this.name = name;
|
|
66 |
}
|
|
67 |
|
|
68 |
/**
|
|
69 |
* Parse an RFC822Name string to see if it is a valid
|
|
70 |
* addr-spec according to IETF RFC822 and RFC2459:
|
|
71 |
* [local-part@]domain
|
|
72 |
* <p>
|
|
73 |
* local-part@ could be empty for an RFC822Name NameConstraint,
|
|
74 |
* but the domain at least must be non-empty. Case is not
|
|
75 |
* significant.
|
|
76 |
*
|
|
77 |
* @param name the RFC822Name string
|
|
78 |
* @throws IOException if name is not valid
|
|
79 |
*/
|
|
80 |
public void parseName(String name) throws IOException {
|
|
81 |
if (name == null || name.length() == 0) {
|
|
82 |
throw new IOException("RFC822Name may not be null or empty");
|
|
83 |
}
|
|
84 |
// See if domain is a valid domain name
|
|
85 |
String domain = name.substring(name.indexOf('@')+1);
|
|
86 |
if (domain.length() == 0) {
|
|
87 |
throw new IOException("RFC822Name may not end with @");
|
|
88 |
} else {
|
|
89 |
//An RFC822 NameConstraint could start with a ., although
|
|
90 |
//a DNSName may not
|
|
91 |
if (domain.startsWith(".")) {
|
|
92 |
if (domain.length() == 1)
|
|
93 |
throw new IOException("RFC822Name domain may not be just .");
|
|
94 |
}
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
|
98 |
/**
|
|
99 |
* Return the type of the GeneralName.
|
|
100 |
*/
|
|
101 |
public int getType() {
|
|
102 |
return (GeneralNameInterface.NAME_RFC822);
|
|
103 |
}
|
|
104 |
|
|
105 |
/**
|
|
106 |
* Return the actual name value of the GeneralName.
|
|
107 |
*/
|
|
108 |
public String getName() {
|
|
109 |
return name;
|
|
110 |
}
|
|
111 |
|
|
112 |
/**
|
|
113 |
* Encode the RFC822 name into the DerOutputStream.
|
|
114 |
*
|
|
115 |
* @param out the DER stream to encode the RFC822Name to.
|
|
116 |
* @exception IOException on encoding errors.
|
|
117 |
*/
|
|
118 |
public void encode(DerOutputStream out) throws IOException {
|
|
119 |
out.putIA5String(name);
|
|
120 |
}
|
|
121 |
|
|
122 |
/**
|
|
123 |
* Convert the name into user readable string.
|
|
124 |
*/
|
|
125 |
public String toString() {
|
|
126 |
return ("RFC822Name: " + name);
|
|
127 |
}
|
|
128 |
|
|
129 |
/**
|
|
130 |
* Compares this name with another, for equality.
|
|
131 |
*
|
|
132 |
* @return true iff the names are equivalent
|
|
133 |
* according to RFC2459.
|
|
134 |
*/
|
|
135 |
public boolean equals(Object obj) {
|
|
136 |
if (this == obj)
|
|
137 |
return true;
|
|
138 |
|
|
139 |
if (!(obj instanceof RFC822Name))
|
|
140 |
return false;
|
|
141 |
|
|
142 |
RFC822Name other = (RFC822Name)obj;
|
|
143 |
|
|
144 |
// RFC2459 mandates that these names are
|
|
145 |
// not case-sensitive
|
|
146 |
return name.equalsIgnoreCase(other.name);
|
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* Returns the hash code value for this object.
|
|
151 |
*
|
|
152 |
* @return a hash code value for this object.
|
|
153 |
*/
|
|
154 |
public int hashCode() {
|
|
155 |
return name.toUpperCase().hashCode();
|
|
156 |
}
|
|
157 |
|
|
158 |
/**
|
|
159 |
* Return constraint type:<ul>
|
|
160 |
* <li>NAME_DIFF_TYPE = -1: input name is different type from name (i.e. does not constrain)
|
|
161 |
* <li>NAME_MATCH = 0: input name matches name
|
|
162 |
* <li>NAME_NARROWS = 1: input name narrows name
|
|
163 |
* <li>NAME_WIDENS = 2: input name widens name
|
|
164 |
* <li>NAME_SAME_TYPE = 3: input name does not match or narrow name, but is same type
|
|
165 |
* </ul>. These results are used in checking NameConstraints during
|
|
166 |
* certification path verification.
|
|
167 |
* <p>
|
|
168 |
* [RFC2459] When the subjectAltName extension contains an Internet mail address,
|
|
169 |
* the address MUST be included as an rfc822Name. The format of an
|
|
170 |
* rfc822Name is an "addr-spec" as defined in RFC 822 [RFC 822]. An
|
|
171 |
* addr-spec has the form "local-part@domain". Note that an addr-spec
|
|
172 |
* has no phrase (such as a common name) before it, has no comment (text
|
|
173 |
* surrounded in parentheses) after it, and is not surrounded by "<" and
|
|
174 |
* ">". Note that while upper and lower case letters are allowed in an
|
|
175 |
* RFC 822 addr-spec, no significance is attached to the case.
|
|
176 |
* <p>
|
|
177 |
* @param inputName to be checked for being constrained
|
|
178 |
* @returns constraint type above
|
|
179 |
* @throws UnsupportedOperationException if name is not exact match, but narrowing and widening are
|
|
180 |
* not supported for this name type.
|
|
181 |
*/
|
|
182 |
public int constrains(GeneralNameInterface inputName) throws UnsupportedOperationException {
|
|
183 |
int constraintType;
|
|
184 |
if (inputName == null)
|
|
185 |
constraintType = NAME_DIFF_TYPE;
|
|
186 |
else if (inputName.getType() != (GeneralNameInterface.NAME_RFC822)) {
|
|
187 |
constraintType = NAME_DIFF_TYPE;
|
|
188 |
} else {
|
|
189 |
//RFC2459 specifies that case is not significant in RFC822Names
|
|
190 |
String inName = (((RFC822Name)inputName).getName()).toLowerCase();
|
|
191 |
String thisName = name.toLowerCase();
|
|
192 |
if (inName.equals(thisName)) {
|
|
193 |
constraintType = NAME_MATCH;
|
|
194 |
} else if (thisName.endsWith(inName)) {
|
|
195 |
/* if both names contain @, then they had to match exactly */
|
|
196 |
if (inName.indexOf('@') != -1) {
|
|
197 |
constraintType = NAME_SAME_TYPE;
|
|
198 |
} else if (inName.startsWith(".")) {
|
|
199 |
constraintType = NAME_WIDENS;
|
|
200 |
} else {
|
|
201 |
int inNdx = thisName.lastIndexOf(inName);
|
|
202 |
if (thisName.charAt(inNdx-1) == '@' ) {
|
|
203 |
constraintType = NAME_WIDENS;
|
|
204 |
} else {
|
|
205 |
constraintType = NAME_SAME_TYPE;
|
|
206 |
}
|
|
207 |
}
|
|
208 |
} else if (inName.endsWith(thisName)) {
|
|
209 |
/* if thisName contains @, then they had to match exactly */
|
|
210 |
if (thisName.indexOf('@') != -1) {
|
|
211 |
constraintType = NAME_SAME_TYPE;
|
|
212 |
} else if (thisName.startsWith(".")) {
|
|
213 |
constraintType = NAME_NARROWS;
|
|
214 |
} else {
|
|
215 |
int ndx = inName.lastIndexOf(thisName);
|
|
216 |
if (inName.charAt(ndx-1) == '@') {
|
|
217 |
constraintType = NAME_NARROWS;
|
|
218 |
} else {
|
|
219 |
constraintType = NAME_SAME_TYPE;
|
|
220 |
}
|
|
221 |
}
|
|
222 |
} else {
|
|
223 |
constraintType = NAME_SAME_TYPE;
|
|
224 |
}
|
|
225 |
}
|
|
226 |
return constraintType;
|
|
227 |
}
|
|
228 |
|
|
229 |
/**
|
|
230 |
* Return subtree depth of this name for purposes of determining
|
|
231 |
* NameConstraints minimum and maximum bounds.
|
|
232 |
*
|
|
233 |
* @returns distance of name from root
|
|
234 |
* @throws UnsupportedOperationException if not supported for this name type
|
|
235 |
*/
|
|
236 |
public int subtreeDepth() throws UnsupportedOperationException {
|
|
237 |
String subtree=name;
|
|
238 |
int i=1;
|
|
239 |
|
|
240 |
/* strip off name@ portion */
|
|
241 |
int atNdx = subtree.lastIndexOf('@');
|
|
242 |
if (atNdx >= 0) {
|
|
243 |
i++;
|
|
244 |
subtree=subtree.substring(atNdx+1);
|
|
245 |
}
|
|
246 |
|
|
247 |
/* count dots in dnsname, adding one if dnsname preceded by @ */
|
|
248 |
for (; subtree.lastIndexOf('.') >= 0; i++) {
|
|
249 |
subtree=subtree.substring(0,subtree.lastIndexOf('.'));
|
|
250 |
}
|
|
251 |
|
|
252 |
return i;
|
|
253 |
}
|
|
254 |
}
|