2
|
1 |
/*
|
|
2 |
* Copyright 2002-2006 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.lang.reflect.*;
|
|
29 |
import java.io.IOException;
|
|
30 |
import java.io.StringReader;
|
|
31 |
import java.security.PrivilegedExceptionAction;
|
|
32 |
import java.security.AccessController;
|
|
33 |
import java.security.Principal;
|
|
34 |
import java.util.*;
|
|
35 |
|
|
36 |
import sun.security.util.*;
|
|
37 |
import sun.security.pkcs.PKCS9Attribute;
|
|
38 |
import javax.security.auth.x500.X500Principal;
|
|
39 |
|
|
40 |
/**
|
|
41 |
* RDNs are a set of {attribute = value} assertions. Some of those
|
|
42 |
* attributes are "distinguished" (unique w/in context). Order is
|
|
43 |
* never relevant.
|
|
44 |
*
|
|
45 |
* Some X.500 names include only a single distinguished attribute
|
|
46 |
* per RDN. This style is currently common.
|
|
47 |
*
|
|
48 |
* Note that DER-encoded RDNs sort AVAs by assertion OID ... so that
|
|
49 |
* when we parse this data we don't have to worry about canonicalizing
|
|
50 |
* it, but we'll need to sort them when we expose the RDN class more.
|
|
51 |
* <p>
|
|
52 |
* The ASN.1 for RDNs is:
|
|
53 |
* <pre>
|
|
54 |
* RelativeDistinguishedName ::=
|
|
55 |
* SET OF AttributeTypeAndValue
|
|
56 |
*
|
|
57 |
* AttributeTypeAndValue ::= SEQUENCE {
|
|
58 |
* type AttributeType,
|
|
59 |
* value AttributeValue }
|
|
60 |
*
|
|
61 |
* AttributeType ::= OBJECT IDENTIFIER
|
|
62 |
*
|
|
63 |
* AttributeValue ::= ANY DEFINED BY AttributeType
|
|
64 |
* </pre>
|
|
65 |
*
|
|
66 |
* Note that instances of this class are immutable.
|
|
67 |
*
|
|
68 |
*/
|
|
69 |
public class RDN {
|
|
70 |
|
|
71 |
// currently not private, accessed directly from X500Name
|
|
72 |
final AVA[] assertion;
|
|
73 |
|
|
74 |
// cached immutable List of the AVAs
|
|
75 |
private volatile List<AVA> avaList;
|
|
76 |
|
|
77 |
// cache canonical String form
|
|
78 |
private volatile String canonicalString;
|
|
79 |
|
|
80 |
/**
|
|
81 |
* Constructs an RDN from its printable representation.
|
|
82 |
*
|
|
83 |
* An RDN may consist of one or multiple Attribute Value Assertions (AVAs),
|
|
84 |
* using '+' as a separator.
|
|
85 |
* If the '+' should be considered part of an AVA value, it must be
|
|
86 |
* preceded by '\'.
|
|
87 |
*
|
|
88 |
* @param name String form of RDN
|
|
89 |
* @throws IOException on parsing error
|
|
90 |
*/
|
|
91 |
public RDN(String name) throws IOException {
|
|
92 |
this(name, Collections.<String, String>emptyMap());
|
|
93 |
}
|
|
94 |
|
|
95 |
/**
|
|
96 |
* Constructs an RDN from its printable representation.
|
|
97 |
*
|
|
98 |
* An RDN may consist of one or multiple Attribute Value Assertions (AVAs),
|
|
99 |
* using '+' as a separator.
|
|
100 |
* If the '+' should be considered part of an AVA value, it must be
|
|
101 |
* preceded by '\'.
|
|
102 |
*
|
|
103 |
* @param name String form of RDN
|
|
104 |
* @param keyword an additional mapping of keywords to OIDs
|
|
105 |
* @throws IOException on parsing error
|
|
106 |
*/
|
|
107 |
public RDN(String name, Map<String, String> keywordMap) throws IOException {
|
|
108 |
int quoteCount = 0;
|
|
109 |
int searchOffset = 0;
|
|
110 |
int avaOffset = 0;
|
|
111 |
List<AVA> avaVec = new ArrayList<AVA>(3);
|
|
112 |
int nextPlus = name.indexOf('+');
|
|
113 |
while (nextPlus >= 0) {
|
|
114 |
quoteCount += X500Name.countQuotes(name, searchOffset, nextPlus);
|
|
115 |
/*
|
|
116 |
* We have encountered an AVA delimiter (plus sign).
|
|
117 |
* If the plus sign in the RDN under consideration is
|
|
118 |
* preceded by a backslash (escape), or by a double quote, it
|
|
119 |
* is part of the AVA. Otherwise, it is used as a separator, to
|
|
120 |
* delimit the AVA under consideration from any subsequent AVAs.
|
|
121 |
*/
|
|
122 |
if (nextPlus > 0 && name.charAt(nextPlus - 1) != '\\'
|
|
123 |
&& quoteCount != 1) {
|
|
124 |
/*
|
|
125 |
* Plus sign is a separator
|
|
126 |
*/
|
|
127 |
String avaString = name.substring(avaOffset, nextPlus);
|
|
128 |
if (avaString.length() == 0) {
|
|
129 |
throw new IOException("empty AVA in RDN \"" + name + "\"");
|
|
130 |
}
|
|
131 |
|
|
132 |
// Parse AVA, and store it in vector
|
|
133 |
AVA ava = new AVA(new StringReader(avaString), keywordMap);
|
|
134 |
avaVec.add(ava);
|
|
135 |
|
|
136 |
// Increase the offset
|
|
137 |
avaOffset = nextPlus + 1;
|
|
138 |
|
|
139 |
// Set quote counter back to zero
|
|
140 |
quoteCount = 0;
|
|
141 |
}
|
|
142 |
searchOffset = nextPlus + 1;
|
|
143 |
nextPlus = name.indexOf('+', searchOffset);
|
|
144 |
}
|
|
145 |
|
|
146 |
// parse last or only AVA
|
|
147 |
String avaString = name.substring(avaOffset);
|
|
148 |
if (avaString.length() == 0) {
|
|
149 |
throw new IOException("empty AVA in RDN \"" + name + "\"");
|
|
150 |
}
|
|
151 |
AVA ava = new AVA(new StringReader(avaString), keywordMap);
|
|
152 |
avaVec.add(ava);
|
|
153 |
|
|
154 |
assertion = avaVec.toArray(new AVA[avaVec.size()]);
|
|
155 |
}
|
|
156 |
|
|
157 |
/*
|
|
158 |
* Constructs an RDN from its printable representation.
|
|
159 |
*
|
|
160 |
* An RDN may consist of one or multiple Attribute Value Assertions (AVAs),
|
|
161 |
* using '+' as a separator.
|
|
162 |
* If the '+' should be considered part of an AVA value, it must be
|
|
163 |
* preceded by '\'.
|
|
164 |
*
|
|
165 |
* @param name String form of RDN
|
|
166 |
* @throws IOException on parsing error
|
|
167 |
*/
|
|
168 |
RDN(String name, String format) throws IOException {
|
|
169 |
this(name, format, Collections.<String, String>emptyMap());
|
|
170 |
}
|
|
171 |
|
|
172 |
/*
|
|
173 |
* Constructs an RDN from its printable representation.
|
|
174 |
*
|
|
175 |
* An RDN may consist of one or multiple Attribute Value Assertions (AVAs),
|
|
176 |
* using '+' as a separator.
|
|
177 |
* If the '+' should be considered part of an AVA value, it must be
|
|
178 |
* preceded by '\'.
|
|
179 |
*
|
|
180 |
* @param name String form of RDN
|
|
181 |
* @param keyword an additional mapping of keywords to OIDs
|
|
182 |
* @throws IOException on parsing error
|
|
183 |
*/
|
|
184 |
RDN(String name, String format, Map<String, String> keywordMap)
|
|
185 |
throws IOException {
|
|
186 |
if (format.equalsIgnoreCase("RFC2253") == false) {
|
|
187 |
throw new IOException("Unsupported format " + format);
|
|
188 |
}
|
|
189 |
int searchOffset = 0;
|
|
190 |
int avaOffset = 0;
|
|
191 |
List<AVA> avaVec = new ArrayList<AVA>(3);
|
|
192 |
int nextPlus = name.indexOf('+');
|
|
193 |
while (nextPlus >= 0) {
|
|
194 |
/*
|
|
195 |
* We have encountered an AVA delimiter (plus sign).
|
|
196 |
* If the plus sign in the RDN under consideration is
|
|
197 |
* preceded by a backslash (escape), or by a double quote, it
|
|
198 |
* is part of the AVA. Otherwise, it is used as a separator, to
|
|
199 |
* delimit the AVA under consideration from any subsequent AVAs.
|
|
200 |
*/
|
|
201 |
if (nextPlus > 0 && name.charAt(nextPlus - 1) != '\\' ) {
|
|
202 |
/*
|
|
203 |
* Plus sign is a separator
|
|
204 |
*/
|
|
205 |
String avaString = name.substring(avaOffset, nextPlus);
|
|
206 |
if (avaString.length() == 0) {
|
|
207 |
throw new IOException("empty AVA in RDN \"" + name + "\"");
|
|
208 |
}
|
|
209 |
|
|
210 |
// Parse AVA, and store it in vector
|
|
211 |
AVA ava = new AVA
|
|
212 |
(new StringReader(avaString), AVA.RFC2253, keywordMap);
|
|
213 |
avaVec.add(ava);
|
|
214 |
|
|
215 |
// Increase the offset
|
|
216 |
avaOffset = nextPlus + 1;
|
|
217 |
}
|
|
218 |
searchOffset = nextPlus + 1;
|
|
219 |
nextPlus = name.indexOf('+', searchOffset);
|
|
220 |
}
|
|
221 |
|
|
222 |
// parse last or only AVA
|
|
223 |
String avaString = name.substring(avaOffset);
|
|
224 |
if (avaString.length() == 0) {
|
|
225 |
throw new IOException("empty AVA in RDN \"" + name + "\"");
|
|
226 |
}
|
|
227 |
AVA ava = new AVA(new StringReader(avaString), AVA.RFC2253, keywordMap);
|
|
228 |
avaVec.add(ava);
|
|
229 |
|
|
230 |
assertion = avaVec.toArray(new AVA[avaVec.size()]);
|
|
231 |
}
|
|
232 |
|
|
233 |
/*
|
|
234 |
* Constructs an RDN from an ASN.1 encoded value. The encoding
|
|
235 |
* of the name in the stream uses DER (a BER/1 subset).
|
|
236 |
*
|
|
237 |
* @param value a DER-encoded value holding an RDN.
|
|
238 |
* @throws IOException on parsing error.
|
|
239 |
*/
|
|
240 |
RDN(DerValue rdn) throws IOException {
|
|
241 |
if (rdn.tag != DerValue.tag_Set) {
|
|
242 |
throw new IOException("X500 RDN");
|
|
243 |
}
|
|
244 |
DerInputStream dis = new DerInputStream(rdn.toByteArray());
|
|
245 |
DerValue[] avaset = dis.getSet(5);
|
|
246 |
|
|
247 |
assertion = new AVA[avaset.length];
|
|
248 |
for (int i = 0; i < avaset.length; i++) {
|
|
249 |
assertion[i] = new AVA(avaset[i]);
|
|
250 |
}
|
|
251 |
}
|
|
252 |
|
|
253 |
/*
|
|
254 |
* Creates an empty RDN with slots for specified
|
|
255 |
* number of AVAs.
|
|
256 |
*
|
|
257 |
* @param i number of AVAs to be in RDN
|
|
258 |
*/
|
|
259 |
RDN(int i) { assertion = new AVA[i]; }
|
|
260 |
|
|
261 |
public RDN(AVA ava) {
|
|
262 |
if (ava == null) {
|
|
263 |
throw new NullPointerException();
|
|
264 |
}
|
|
265 |
assertion = new AVA[] { ava };
|
|
266 |
}
|
|
267 |
|
|
268 |
public RDN(AVA[] avas) {
|
|
269 |
assertion = avas.clone();
|
|
270 |
for (int i = 0; i < assertion.length; i++) {
|
|
271 |
if (assertion[i] == null) {
|
|
272 |
throw new NullPointerException();
|
|
273 |
}
|
|
274 |
}
|
|
275 |
}
|
|
276 |
|
|
277 |
/**
|
|
278 |
* Return an immutable List of the AVAs in this RDN.
|
|
279 |
*/
|
|
280 |
public List<AVA> avas() {
|
|
281 |
List<AVA> list = avaList;
|
|
282 |
if (list == null) {
|
|
283 |
list = Collections.unmodifiableList(Arrays.asList(assertion));
|
|
284 |
avaList = list;
|
|
285 |
}
|
|
286 |
return list;
|
|
287 |
}
|
|
288 |
|
|
289 |
/**
|
|
290 |
* Return the number of AVAs in this RDN.
|
|
291 |
*/
|
|
292 |
public int size() {
|
|
293 |
return assertion.length;
|
|
294 |
}
|
|
295 |
|
|
296 |
public boolean equals(Object obj) {
|
|
297 |
if (this == obj) {
|
|
298 |
return true;
|
|
299 |
}
|
|
300 |
if (obj instanceof RDN == false) {
|
|
301 |
return false;
|
|
302 |
}
|
|
303 |
RDN other = (RDN)obj;
|
|
304 |
if (this.assertion.length != other.assertion.length) {
|
|
305 |
return false;
|
|
306 |
}
|
|
307 |
String thisCanon = this.toRFC2253String(true);
|
|
308 |
String otherCanon = other.toRFC2253String(true);
|
|
309 |
return thisCanon.equals(otherCanon);
|
|
310 |
}
|
|
311 |
|
|
312 |
/*
|
|
313 |
* Calculates a hash code value for the object. Objects
|
|
314 |
* which are equal will also have the same hashcode.
|
|
315 |
*
|
|
316 |
* @returns int hashCode value
|
|
317 |
*/
|
|
318 |
public int hashCode() {
|
|
319 |
return toRFC2253String(true).hashCode();
|
|
320 |
}
|
|
321 |
|
|
322 |
/*
|
|
323 |
* return specified attribute value from RDN
|
|
324 |
*
|
|
325 |
* @params oid ObjectIdentifier of attribute to be found
|
|
326 |
* @returns DerValue of attribute value; null if attribute does not exist
|
|
327 |
*/
|
|
328 |
DerValue findAttribute(ObjectIdentifier oid) {
|
|
329 |
for (int i = 0; i < assertion.length; i++) {
|
|
330 |
if (assertion[i].oid.equals(oid)) {
|
|
331 |
return assertion[i].value;
|
|
332 |
}
|
|
333 |
}
|
|
334 |
return null;
|
|
335 |
}
|
|
336 |
|
|
337 |
/*
|
|
338 |
* Encode the RDN in DER-encoded form.
|
|
339 |
*
|
|
340 |
* @param out DerOutputStream to which RDN is to be written
|
|
341 |
* @throws IOException on error
|
|
342 |
*/
|
|
343 |
void encode(DerOutputStream out) throws IOException {
|
|
344 |
out.putOrderedSetOf(DerValue.tag_Set, assertion);
|
|
345 |
}
|
|
346 |
|
|
347 |
/*
|
|
348 |
* Returns a printable form of this RDN, using RFC 1779 style catenation
|
|
349 |
* of attribute/value assertions, and emitting attribute type keywords
|
|
350 |
* from RFCs 1779, 2253, and 3280.
|
|
351 |
*/
|
|
352 |
public String toString() {
|
|
353 |
if (assertion.length == 1) {
|
|
354 |
return assertion[0].toString();
|
|
355 |
}
|
|
356 |
|
|
357 |
StringBuilder sb = new StringBuilder();
|
|
358 |
for (int i = 0; i < assertion.length; i++) {
|
|
359 |
if (i != 0) {
|
|
360 |
sb.append(" + ");
|
|
361 |
}
|
|
362 |
sb.append(assertion[i].toString());
|
|
363 |
}
|
|
364 |
return sb.toString();
|
|
365 |
}
|
|
366 |
|
|
367 |
/*
|
|
368 |
* Returns a printable form of this RDN using the algorithm defined in
|
|
369 |
* RFC 1779. Only RFC 1779 attribute type keywords are emitted.
|
|
370 |
*/
|
|
371 |
public String toRFC1779String() {
|
|
372 |
return toRFC1779String(Collections.<String, String>emptyMap());
|
|
373 |
}
|
|
374 |
|
|
375 |
/*
|
|
376 |
* Returns a printable form of this RDN using the algorithm defined in
|
|
377 |
* RFC 1779. RFC 1779 attribute type keywords are emitted, as well
|
|
378 |
* as keywords contained in the OID/keyword map.
|
|
379 |
*/
|
|
380 |
public String toRFC1779String(Map<String, String> oidMap) {
|
|
381 |
if (assertion.length == 1) {
|
|
382 |
return assertion[0].toRFC1779String(oidMap);
|
|
383 |
}
|
|
384 |
|
|
385 |
StringBuilder sb = new StringBuilder();
|
|
386 |
for (int i = 0; i < assertion.length; i++) {
|
|
387 |
if (i != 0) {
|
|
388 |
sb.append(" + ");
|
|
389 |
}
|
|
390 |
sb.append(assertion[i].toRFC1779String(oidMap));
|
|
391 |
}
|
|
392 |
return sb.toString();
|
|
393 |
}
|
|
394 |
|
|
395 |
/*
|
|
396 |
* Returns a printable form of this RDN using the algorithm defined in
|
|
397 |
* RFC 2253. Only RFC 2253 attribute type keywords are emitted.
|
|
398 |
*/
|
|
399 |
public String toRFC2253String() {
|
|
400 |
return toRFC2253StringInternal
|
|
401 |
(false, Collections.<String, String>emptyMap());
|
|
402 |
}
|
|
403 |
|
|
404 |
/*
|
|
405 |
* Returns a printable form of this RDN using the algorithm defined in
|
|
406 |
* RFC 2253. RFC 2253 attribute type keywords are emitted, as well as
|
|
407 |
* keywords contained in the OID/keyword map.
|
|
408 |
*/
|
|
409 |
public String toRFC2253String(Map<String, String> oidMap) {
|
|
410 |
return toRFC2253StringInternal(false, oidMap);
|
|
411 |
}
|
|
412 |
|
|
413 |
/*
|
|
414 |
* Returns a printable form of this RDN using the algorithm defined in
|
|
415 |
* RFC 2253. Only RFC 2253 attribute type keywords are emitted.
|
|
416 |
* If canonical is true, then additional canonicalizations
|
|
417 |
* documented in X500Principal.getName are performed.
|
|
418 |
*/
|
|
419 |
public String toRFC2253String(boolean canonical) {
|
|
420 |
if (canonical == false) {
|
|
421 |
return toRFC2253StringInternal
|
|
422 |
(false, Collections.<String, String>emptyMap());
|
|
423 |
}
|
|
424 |
String c = canonicalString;
|
|
425 |
if (c == null) {
|
|
426 |
c = toRFC2253StringInternal
|
|
427 |
(true, Collections.<String, String>emptyMap());
|
|
428 |
canonicalString = c;
|
|
429 |
}
|
|
430 |
return c;
|
|
431 |
}
|
|
432 |
|
|
433 |
private String toRFC2253StringInternal
|
|
434 |
(boolean canonical, Map<String, String> oidMap) {
|
|
435 |
/*
|
|
436 |
* Section 2.2: When converting from an ASN.1 RelativeDistinguishedName
|
|
437 |
* to a string, the output consists of the string encodings of each
|
|
438 |
* AttributeTypeAndValue (according to 2.3), in any order.
|
|
439 |
*
|
|
440 |
* Where there is a multi-valued RDN, the outputs from adjoining
|
|
441 |
* AttributeTypeAndValues are separated by a plus ('+' ASCII 43)
|
|
442 |
* character.
|
|
443 |
*/
|
|
444 |
|
|
445 |
// normally, an RDN only contains one AVA
|
|
446 |
if (assertion.length == 1) {
|
|
447 |
return canonical ? assertion[0].toRFC2253CanonicalString() :
|
|
448 |
assertion[0].toRFC2253String(oidMap);
|
|
449 |
}
|
|
450 |
|
|
451 |
StringBuilder relname = new StringBuilder();
|
|
452 |
if (!canonical) {
|
|
453 |
for (int i = 0; i < assertion.length; i++) {
|
|
454 |
if (i > 0) {
|
|
455 |
relname.append('+');
|
|
456 |
}
|
|
457 |
relname.append(assertion[i].toRFC2253String(oidMap));
|
|
458 |
}
|
|
459 |
} else {
|
|
460 |
// order the string type AVA's alphabetically,
|
|
461 |
// followed by the oid type AVA's numerically
|
|
462 |
List<AVA> avaList = new ArrayList<AVA>(assertion.length);
|
|
463 |
for (int i = 0; i < assertion.length; i++) {
|
|
464 |
avaList.add(assertion[i]);
|
|
465 |
}
|
|
466 |
java.util.Collections.sort(avaList, AVAComparator.getInstance());
|
|
467 |
|
|
468 |
for (int i = 0; i < avaList.size(); i++) {
|
|
469 |
if (i > 0) {
|
|
470 |
relname.append('+');
|
|
471 |
}
|
|
472 |
relname.append(avaList.get(i).toRFC2253CanonicalString());
|
|
473 |
}
|
|
474 |
}
|
|
475 |
return relname.toString();
|
|
476 |
}
|
|
477 |
|
|
478 |
}
|
|
479 |
|
|
480 |
class AVAComparator implements Comparator<AVA> {
|
|
481 |
|
|
482 |
private static final Comparator<AVA> INSTANCE = new AVAComparator();
|
|
483 |
|
|
484 |
private AVAComparator() {
|
|
485 |
// empty
|
|
486 |
}
|
|
487 |
|
|
488 |
static Comparator<AVA> getInstance() {
|
|
489 |
return INSTANCE;
|
|
490 |
}
|
|
491 |
|
|
492 |
/**
|
|
493 |
* AVA's containing a standard keyword are ordered alphabetically,
|
|
494 |
* followed by AVA's containing an OID keyword, ordered numerically
|
|
495 |
*/
|
|
496 |
public int compare(AVA a1, AVA a2) {
|
|
497 |
boolean a1Has2253 = a1.hasRFC2253Keyword();
|
|
498 |
boolean a2Has2253 = a2.hasRFC2253Keyword();
|
|
499 |
|
|
500 |
if (a1Has2253 == a2Has2253) {
|
|
501 |
return a1.toRFC2253CanonicalString().compareTo
|
|
502 |
(a2.toRFC2253CanonicalString());
|
|
503 |
} else {
|
|
504 |
if (a1Has2253) {
|
|
505 |
return -1;
|
|
506 |
} else {
|
|
507 |
return 1;
|
|
508 |
}
|
|
509 |
}
|
|
510 |
}
|
|
511 |
|
|
512 |
}
|