1 package org.bouncycastle.asn1.test;
2 
3 import java.io.IOException;
4 
5 import org.bouncycastle.asn1.ASN1Encodable;
6 import org.bouncycastle.asn1.ASN1EncodableVector;
7 import org.bouncycastle.asn1.ASN1GeneralizedTime;
8 import org.bouncycastle.asn1.ASN1ObjectIdentifier;
9 import org.bouncycastle.asn1.ASN1Primitive;
10 import org.bouncycastle.asn1.ASN1Sequence;
11 import org.bouncycastle.asn1.ASN1Set;
12 import org.bouncycastle.asn1.ASN1String;
13 import org.bouncycastle.asn1.ASN1TaggedObject;
14 import org.bouncycastle.asn1.DERIA5String;
15 import org.bouncycastle.asn1.DERPrintableString;
16 import org.bouncycastle.asn1.DERSequence;
17 import org.bouncycastle.asn1.DERSet;
18 import org.bouncycastle.asn1.DERTaggedObject;
19 import org.bouncycastle.asn1.DERUTF8String;
20 import org.bouncycastle.asn1.x500.RDN;
21 import org.bouncycastle.asn1.x500.X500Name;
22 import org.bouncycastle.asn1.x500.X500NameBuilder;
23 import org.bouncycastle.asn1.x500.style.BCStrictStyle;
24 import org.bouncycastle.asn1.x500.style.BCStyle;
25 import org.bouncycastle.asn1.x500.style.IETFUtils;
26 import org.bouncycastle.asn1.x509.X509DefaultEntryConverter;
27 import org.bouncycastle.util.encoders.Hex;
28 import org.bouncycastle.util.test.SimpleTest;
29 
30 public class X500NameTest
31     extends SimpleTest
32 {
33    String[] subjects =
34    {
35        "C=AU,ST=Victoria,L=South Melbourne,O=Connect 4 Pty Ltd,OU=Webserver Team,CN=www2.connect4.com.au,E=webmaster@connect4.com.au",
36        "C=AU,ST=Victoria,L=South Melbourne,O=Connect 4 Pty Ltd,OU=Certificate Authority,CN=Connect 4 CA,E=webmaster@connect4.com.au",
37        "C=AU,ST=QLD,CN=SSLeay/rsa test cert",
38        "C=US,O=National Aeronautics and Space Administration,SERIALNUMBER=16+CN=Steve Schoch",
39        "E=cooke@issl.atl.hp.com,C=US,OU=Hewlett Packard Company (ISSL),CN=Paul A. Cooke",
40        "O=Sun Microsystems Inc,CN=store.sun.com",
41        "unstructuredAddress=192.168.1.33,unstructuredName=pixfirewall.ciscopix.com,CN=pixfirewall.ciscopix.com",
42        "CN=*.canal-plus.com,OU=Provided by TBS INTERNET https://www.tbs-certificats.com/,OU=\\ CANAL \\+,O=CANAL\\+DISTRIBUTION,L=issy les moulineaux,ST=Hauts de Seine,C=FR",
43        "O=Bouncy Castle,CN=www.bouncycastle.org\\ ",
44        "O=Bouncy Castle,CN=c:\\\\fred\\\\bob",
45        "C=0,O=1,OU=2,T=3,CN=4,SERIALNUMBER=5,STREET=6,SERIALNUMBER=7,L=8,ST=9,SURNAME=10,GIVENNAME=11,INITIALS=12," +
46            "GENERATION=13,UniqueIdentifier=14,BusinessCategory=15,PostalCode=16,DN=17,Pseudonym=18,PlaceOfBirth=19," +
47            "Gender=20,CountryOfCitizenship=21,CountryOfResidence=22,NameAtBirth=23,PostalAddress=24,2.5.4.54=25," +
48            "TelephoneNumber=26,Name=27,E=28,unstructuredName=29,unstructuredAddress=30,E=31,DC=32,UID=33"
49 
50     };
51 
52     String[] hexSubjects =
53     {
54         "CN=\\20Test\\20X,O=\\20Test,C=GB",    // input
55         "CN=\\ Test X,O=\\ Test,C=GB",          // expected
56         "CN=\\20Test\\20X\\20,O=\\20Test,C=GB",    // input
57         "CN=\\ Test X\\ ,O=\\ Test,C=GB"          // expected
58     };
59 
getName()60     public String getName()
61     {
62         return "X500Name";
63     }
64 
fromBytes(byte[] bytes)65     private static X500Name fromBytes(byte[] bytes) throws IOException
66     {
67         return X500Name.getInstance(ASN1Primitive.fromByteArray(bytes));
68     }
69 
createEntryValue(ASN1ObjectIdentifier oid, String value)70     private ASN1Encodable createEntryValue(ASN1ObjectIdentifier oid, String value)
71     {
72         X500NameBuilder builder = new X500NameBuilder(BCStyle.INSTANCE);
73 
74         builder.addRDN(oid, value);
75 
76         X500Name name = builder.build();
77 
78         ASN1Sequence seq = (ASN1Sequence)name.toASN1Primitive();
79         ASN1Set set = ASN1Set.getInstance(seq.getObjectAt(0).toASN1Primitive());
80         seq = (ASN1Sequence)set.getObjectAt(0);
81 
82         return seq.getObjectAt(1);
83     }
84 
createEntryValueFromString(ASN1ObjectIdentifier oid, String value)85     private ASN1Encodable createEntryValueFromString(ASN1ObjectIdentifier oid, String value)
86     {
87         X500NameBuilder builder = new X500NameBuilder(BCStyle.INSTANCE);
88 
89         builder.addRDN(oid, value);
90 
91         X500Name name = new X500Name(builder.build().toString());
92 
93         ASN1Sequence seq = (ASN1Sequence)name.toASN1Primitive();
94         ASN1Set set = ASN1Set.getInstance(seq.getObjectAt(0).toASN1Primitive());
95         seq = (ASN1Sequence)set.getObjectAt(0);
96 
97         return seq.getObjectAt(1);
98     }
99 
testEncodingPrintableString(ASN1ObjectIdentifier oid, String value)100     private void testEncodingPrintableString(ASN1ObjectIdentifier oid, String value)
101     {
102         ASN1Encodable converted = createEntryValue(oid, value);
103         if (!(converted instanceof DERPrintableString))
104         {
105             fail("encoding for " + oid + " not printable string");
106         }
107     }
108 
testEncodingIA5String(ASN1ObjectIdentifier oid, String value)109     private void testEncodingIA5String(ASN1ObjectIdentifier oid, String value)
110     {
111         ASN1Encodable converted = createEntryValue(oid, value);
112         if (!(converted instanceof DERIA5String))
113         {
114             fail("encoding for " + oid + " not IA5String");
115         }
116     }
117 
testEncodingUTF8String(ASN1ObjectIdentifier oid, String value)118     private void testEncodingUTF8String(ASN1ObjectIdentifier oid, String value)
119         throws IOException
120     {
121         ASN1Encodable converted = createEntryValue(oid, value);
122         if (!(converted instanceof DERUTF8String))
123         {
124             fail("encoding for " + oid + " not IA5String");
125         }
126         if (!value.equals((DERUTF8String.getInstance(converted.toASN1Primitive().getEncoded()).getString())))
127         {
128             fail("decoding not correct");
129         }
130     }
131 
testEncodingGeneralizedTime(ASN1ObjectIdentifier oid, String value)132     private void testEncodingGeneralizedTime(ASN1ObjectIdentifier oid, String value)
133     {
134         ASN1Encodable converted = createEntryValue(oid, value);
135         if (!(converted instanceof ASN1GeneralizedTime))
136         {
137             fail("encoding for " + oid + " not GeneralizedTime");
138         }
139         converted = createEntryValueFromString(oid, value);
140         if (!(converted instanceof ASN1GeneralizedTime))
141         {
142             fail("encoding for " + oid + " not GeneralizedTime");
143         }
144     }
145 
performTest()146     public void performTest()
147         throws Exception
148     {
149         ietfUtilsTest();
150 
151         testEncodingPrintableString(BCStyle.C, "AU");
152         testEncodingPrintableString(BCStyle.SERIALNUMBER, "123456");
153         testEncodingPrintableString(BCStyle.DN_QUALIFIER, "123456");
154         testEncodingIA5String(BCStyle.EmailAddress, "test@test.com");
155         testEncodingIA5String(BCStyle.DC, "test");
156         // correct encoding
157         testEncodingGeneralizedTime(BCStyle.DATE_OF_BIRTH, "#180F32303032303132323132323232305A");
158         // compatibility encoding
159         testEncodingGeneralizedTime(BCStyle.DATE_OF_BIRTH, "20020122122220Z");
160         testEncodingUTF8String(BCStyle.CN, "Mörsky");
161         testEncodingUTF8String(BCStyle.ORGANIZATION_IDENTIFIER, "Mörsky");
162 
163         //
164         // composite
165         //
166         X500NameBuilder builder = new X500NameBuilder(BCStyle.INSTANCE);
167 
168         builder.addRDN(BCStyle.C, "AU");
169         builder.addRDN(BCStyle.O, "The Legion of the Bouncy Castle");
170         builder.addRDN(BCStyle.L, "Melbourne");
171         builder.addRDN(BCStyle.ST, "Victoria");
172         builder.addRDN(BCStyle.E, "feedback-crypto@bouncycastle.org");
173 
174         X500Name    name1 = builder.build();
175 
176         if (!name1.equals(name1))
177         {
178             fail("Failed same object test");
179         }
180 
181 //        if (!name1.equals(name1, true))
182 //        {
183 //            fail("Failed same object test - in Order");
184 //        }
185 
186         builder = new X500NameBuilder(BCStyle.INSTANCE);
187 
188         builder.addRDN(BCStyle.C, "AU");
189         builder.addRDN(BCStyle.O, "The Legion of the Bouncy Castle");
190         builder.addRDN(BCStyle.L, "Melbourne");
191         builder.addRDN(BCStyle.ST, "Victoria");
192         builder.addRDN(BCStyle.E, "feedback-crypto@bouncycastle.org");
193 
194         X500Name    name2 = builder.build();
195 
196         if (!name1.equals(name2))
197         {
198             fail("Failed same name test");
199         }
200 
201 //        if (!name1.equals(name2, true))
202 //        {
203 //            fail("Failed same name test - in Order");
204 //        }
205 
206         if (name1.hashCode() != name2.hashCode())
207         {
208             fail("Failed same name test - in Order");
209         }
210 
211         X500NameBuilder builder1 = new X500NameBuilder(BCStyle.INSTANCE);
212 
213         builder.addRDN(BCStyle.C, "AU");
214         builder.addRDN(BCStyle.O, "The Legion of the Bouncy Castle");
215         builder.addRDN(BCStyle.L, "Melbourne");
216         builder.addRDN(BCStyle.ST, "Victoria");
217         builder.addRDN(BCStyle.E, "feedback-crypto@bouncycastle.org");
218 
219         X500NameBuilder builder2 = new X500NameBuilder(BCStyle.INSTANCE);
220 
221         builder.addRDN(BCStyle.E, "feedback-crypto@bouncycastle.org");
222         builder.addRDN(BCStyle.C, "AU");
223         builder.addRDN(BCStyle.O, "The Legion of the Bouncy Castle");
224         builder.addRDN(BCStyle.L, "Melbourne");
225         builder.addRDN(BCStyle.ST, "Victoria");
226 
227         name1 = builder1.build();
228         name2 = builder2.build();
229 
230         if (!name1.equals(name2))
231         {
232             fail("Failed reverse name test");
233         }
234 
235         if (name1.hashCode() != name2.hashCode())
236         {
237             fail("Failed reverse name test hashCode");
238         }
239 
240 //        if (name1.equals(name2, true))
241 //        {
242 //            fail("Failed reverse name test - in Order");
243 //        }
244 //
245 //        if (!name1.equals(name2, false))
246 //        {
247 //            fail("Failed reverse name test - in Order false");
248 //        }
249 
250 //        Vector oids = name1.getOIDs();
251 //        if (!compareVectors(oids, ord1))
252 //        {
253 //            fail("oid comparison test");
254 //        }
255        /*
256         Vector val1 = new Vector();
257 
258         val1.addElement("AU");
259         val1.addElement("The Legion of the Bouncy Castle");
260         val1.addElement("Melbourne");
261         val1.addElement("Victoria");
262         val1.addElement("feedback-crypto@bouncycastle.org");
263 
264         name1 = new X500Name(ord1, val1);
265 
266         Vector values = name1.getValues();
267         if (!compareVectors(values, val1))
268         {
269             fail("value comparison test");
270         }
271 
272         ord2 = new Vector();
273 
274         ord2.addElement(X500Name.ST);
275         ord2.addElement(X500Name.ST);
276         ord2.addElement(X500Name.L);
277         ord2.addElement(X500Name.O);
278         ord2.addElement(X500Name.C);
279 
280         name1 = new X500Name(ord1, attrs);
281         name2 = new X500Name(ord2, attrs);
282 
283         if (name1.equals(name2))
284         {
285             fail("Failed different name test");
286         }
287 
288         ord2 = new Vector();
289 
290         ord2.addElement(X500Name.ST);
291         ord2.addElement(X500Name.L);
292         ord2.addElement(X500Name.O);
293         ord2.addElement(X500Name.C);
294 
295         name1 = new X500Name(ord1, attrs);
296         name2 = new X500Name(ord2, attrs);
297 
298         if (name1.equals(name2))
299         {
300             fail("Failed subset name test");
301         }
302 
303         compositeTest();
304          */
305        /*
306         //
307         // getValues test
308         //
309         Vector v1 = name1.getValues(X500Name.O);
310 
311         if (v1.size() != 1 || !v1.elementAt(0).equals("The Legion of the Bouncy Castle"))
312         {
313             fail("O test failed");
314         }
315 
316         Vector v2 = name1.getValues(X500Name.L);
317 
318         if (v2.size() != 1 || !v2.elementAt(0).equals("Melbourne"))
319         {
320             fail("L test failed");
321         }
322        */
323         //
324         // general subjects test
325         //
326         for (int i = 0; i != subjects.length; i++)
327         {
328             X500Name name = new X500Name(subjects[i]);
329             name = X500Name.getInstance(ASN1Primitive.fromByteArray(name.getEncoded()));
330             if (!name.toString().equals(subjects[i]))
331             {
332                 fail("failed regeneration test " + i + " got: " + name.toString() + " expected " + subjects[i]);
333             }
334         }
335 
336         for (int i = 0; i < hexSubjects.length; i += 2)
337         {
338             X500Name name = new X500Name(hexSubjects[i]);
339             name = X500Name.getInstance(ASN1Primitive.fromByteArray(name.getEncoded()));
340             if (!name.toString().equals(hexSubjects[i + 1]))
341             {
342                 fail("failed hex regeneration test " + i + " got: " + name.toString() + " expected " + subjects[i]);
343             }
344         }
345 
346         //
347         // sort test
348         //
349         X500Name unsorted = new X500Name("SERIALNUMBER=BBB + CN=AA");
350 
351         if (!fromBytes(unsorted.getEncoded()).toString().equals("CN=AA+SERIALNUMBER=BBB"))
352         {
353             fail("failed sort test 1");
354         }
355 
356         unsorted = new X500Name("CN=AA + SERIALNUMBER=BBB");
357 
358         if (!fromBytes(unsorted.getEncoded()).toString().equals("CN=AA+SERIALNUMBER=BBB"))
359         {
360             fail("failed sort test 2");
361         }
362 
363         unsorted = new X500Name("SERIALNUMBER=B + CN=AA");
364 
365         if (!fromBytes(unsorted.getEncoded()).toString().equals("SERIALNUMBER=B+CN=AA"))
366         {
367             fail("failed sort test 3");
368         }
369 
370         unsorted = new X500Name("CN=AA + SERIALNUMBER=B");
371 
372         if (!fromBytes(unsorted.getEncoded()).toString().equals("SERIALNUMBER=B+CN=AA"))
373         {
374             fail("failed sort test 4");
375         }
376 
377         //
378         // equality tests
379         //
380         equalityTest(new X500Name("CN=The     Legion"), new X500Name("CN=The Legion"));
381         equalityTest(new X500Name("CN=   The Legion"), new X500Name("CN=The Legion"));
382         equalityTest(new X500Name("CN=The Legion   "), new X500Name("CN=The Legion"));
383         equalityTest(new X500Name("CN=  The     Legion "), new X500Name("CN=The Legion"));
384         equalityTest(new X500Name("CN=  the     legion "), new X500Name("CN=The Legion"));
385 
386         equalityTest(new X500Name("CN=  the     legion+C=AU, O=Legion "), new X500Name("CN=The Legion+C=AU, O=Legion"));
387         // # test
388 
389         X500Name n1 = new X500Name("SERIALNUMBER=8,O=ABC,CN=ABC Class 3 CA,C=LT");
390         X500Name n2 = new X500Name("2.5.4.5=8,O=ABC,CN=ABC Class 3 CA,C=LT");
391         X500Name n3 = new X500Name("2.5.4.5=#130138,O=ABC,CN=ABC Class 3 CA,C=LT");
392 
393         equalityTest(n1, n2);
394         equalityTest(n2, n3);
395         equalityTest(n3, n1);
396 
397         n1 = new X500Name("2.5.4.5=#130138,CN=SSC Class 3 CA,O=UAB Skaitmeninio sertifikavimo centras,C=LT");
398         n2 = new X500Name("SERIALNUMBER=#130138,CN=SSC Class 3 CA,O=UAB Skaitmeninio sertifikavimo centras,C=LT");
399         n3 = X500Name.getInstance(ASN1Primitive.fromByteArray(Hex.decode("3063310b3009060355040613024c54312f302d060355040a1326"
400             + "55414220536b6169746d656e696e696f20736572746966696b6176696d6f2063656e74726173311730150603550403130e53534320436c6173732033204341310a30080603550405130138")));
401 
402         equalityTest(n1, n2);
403         equalityTest(n2, n3);
404         equalityTest(n3, n1);
405 
406         n1 = new X500Name("SERIALNUMBER=8,O=XX,CN=ABC Class 3 CA,C=LT");
407         n2 = new X500Name("2.5.4.5=8,O=,CN=ABC Class 3 CA,C=LT");
408 
409 //        if (n1.equals(n2))
410 //        {
411 //            fail("empty inequality check failed");
412 //        }
413 
414         n1 = new X500Name("SERIALNUMBER=8,O=,CN=ABC Class 3 CA,C=LT");
415         n2 = new X500Name("2.5.4.5=8,O=,CN=ABC Class 3 CA,C=LT");
416 
417         equalityTest(n1, n2);
418 
419         equalityTest(X500Name.getInstance(BCStrictStyle.INSTANCE, n1), X500Name.getInstance(BCStrictStyle.INSTANCE, n2));
420 
421         n2 = new X500Name("C=LT,2.5.4.5=8,O=,CN=ABC Class 3 CA");
422 
423         equalityTest(n1, n2);
424 
425         if (X500Name.getInstance(BCStrictStyle.INSTANCE, n1).equals(X500Name.getInstance(BCStrictStyle.INSTANCE, n2)))
426         {
427             fail("strict comparison failed");
428         }
429 
430         //
431         // inequality to sequences
432         //
433         name1 = new X500Name("CN=The Legion");
434 
435         if (name1.equals(new DERSequence()))
436         {
437             fail("inequality test with sequence");
438         }
439 
440         if (name1.equals(new DERSequence(new DERSet())))
441         {
442             fail("inequality test with sequence and set");
443         }
444 
445         ASN1EncodableVector v = new ASN1EncodableVector();
446 
447         v.add(new ASN1ObjectIdentifier("1.1"));
448         v.add(new ASN1ObjectIdentifier("1.1"));
449         if (name1.equals(new DERSequence(new DERSet(new DERSet(v)))))
450         {
451             fail("inequality test with sequence and bad set");
452         }
453 
454         if (name1.equals(new DERSequence(new DERSet(new DERSet(v)))))
455         {
456             fail("inequality test with sequence and bad set");
457         }
458 
459         if (name1.equals(new DERSequence(new DERSet(new DERSequence()))))
460         {
461             fail("inequality test with sequence and short sequence");
462         }
463 
464         if (name1.equals(new DERSequence(new DERSet(new DERSequence()))))
465         {
466             fail("inequality test with sequence and short sequence");
467         }
468 
469         v = new ASN1EncodableVector();
470 
471         v.add(new ASN1ObjectIdentifier("1.1"));
472         v.add(new DERSequence());
473 
474         if (name1.equals(new DERSequence(new DERSet(new DERSequence(v)))))
475         {
476             fail("inequality test with sequence and bad sequence");
477         }
478 
479         if (name1.equals(null))
480         {
481             fail("inequality test with null");
482         }
483 
484 //        if (name1.equals(null, true))
485 //        {
486 //            fail("inequality test with null");
487 //        }
488 
489         //
490         // this is contrived but it checks sorting of sets with equal elements
491         //
492         unsorted = new X500Name("CN=AA + CN=AA + CN=AA");
493 
494         ASN1ObjectIdentifier[] types = unsorted.getAttributeTypes();
495         if (types.length != 3 || !types[0].equals(BCStyle.CN) || !types[1].equals(BCStyle.CN) || !types[2].equals(BCStyle.CN))
496         {
497             fail("types not matched correctly");
498         }
499 
500         // general type test
501         X500Name nested = new X500Name("CN=AA + CN=AA, C=AU");
502 
503         types = nested.getAttributeTypes();
504         if (types.length != 3 || !types[0].equals(BCStyle.CN) || !types[1].equals(BCStyle.CN) || !types[2].equals(BCStyle.C))
505         {
506             fail("nested types not matched correctly");
507         }
508         //
509         // tagging test - only works if CHOICE implemented
510         //
511         ASN1TaggedObject tag = new DERTaggedObject(false, 1, new X500Name("CN=AA"));
512 
513         if (!tag.isExplicit())
514         {
515             fail("failed to explicitly tag CHOICE object");
516         }
517 
518         X500Name name = X500Name.getInstance(tag, false);
519 
520         if (!name.equals(new X500Name("CN=AA")))
521         {
522             fail("failed to recover tagged name");
523         }
524 
525         DERUTF8String testString = new DERUTF8String("The Legion of the Bouncy Castle");
526         byte[] encodedBytes = testString.getEncoded();
527         byte[] hexEncodedBytes = Hex.encode(encodedBytes);
528         String hexEncodedString = "#" + new String(hexEncodedBytes);
529 
530         DERUTF8String converted = (DERUTF8String)
531             new X509DefaultEntryConverter().getConvertedValue(
532                 BCStyle.L , hexEncodedString);
533 
534         if (!converted.equals(testString))
535         {
536             fail("failed X509DefaultEntryConverter test");
537         }
538 
539         //
540         // try escaped.
541         //
542         converted = (DERUTF8String)
543             new X509DefaultEntryConverter().getConvertedValue(
544                 BCStyle.L , "\\" + hexEncodedString);
545 
546         if (!converted.equals(new DERUTF8String(hexEncodedString)))
547         {
548             fail("failed X509DefaultEntryConverter test got " + converted + " expected: " + hexEncodedString);
549         }
550 
551         //
552         // try a weird value
553         //
554         X500Name n = new X500Name("CN=\\#nothex#string");
555 
556         if (!n.toString().equals("CN=\\#nothex#string"))
557         {
558             fail("# string not properly escaped.");
559         }
560 
561         RDN[] vls = n.getRDNs(BCStyle.CN);
562         if (vls.length != 1 || !getValue(vls[0]).equals("#nothex#string"))
563         {
564             fail("escaped # not reduced properly");
565         }
566 
567         types = n.getAttributeTypes();
568         if (types.length != 1 || !types[0].equals(BCStyle.CN))
569         {
570             fail("type not matched correctly");
571         }
572 
573         n = new X500Name("CN=\"a+b\"");
574 
575         vls = n.getRDNs(BCStyle.CN);
576         if (vls.length != 1 || !getValue(vls[0]).equals("a+b"))
577         {
578             fail("escaped + not reduced properly");
579         }
580 
581         n = new X500Name("CN=a\\+b");
582 
583         vls = n.getRDNs(BCStyle.CN);
584         if (vls.length != 1 || !getValue(vls[0]).equals("a+b"))
585         {
586             fail("escaped + not reduced properly");
587         }
588 
589         if (!n.toString().equals("CN=a\\+b"))
590         {
591             fail("+ in string not properly escaped.");
592         }
593 
594         n = new X500Name("CN=a\\=b");
595 
596         vls = n.getRDNs(BCStyle.CN);
597         if (vls.length != 1 || !getValue(vls[0]).equals("a=b"))
598         {
599             fail("escaped = not reduced properly");
600         }
601 
602         if (!n.toString().equals("CN=a\\=b"))
603         {
604             fail("= in string not properly escaped.");
605         }
606 
607         n = new X500Name("TELEPHONENUMBER=\"+61999999999\"");
608 
609         vls = n.getRDNs(BCStyle.TELEPHONE_NUMBER);
610         if (vls.length != 1 || !getValue(vls[0]).equals("+61999999999"))
611         {
612             fail("telephonenumber escaped + not reduced properly");
613         }
614 
615         n = new X500Name("TELEPHONENUMBER=\\+61999999999");
616 
617         vls = n.getRDNs(BCStyle.TELEPHONE_NUMBER);
618         if (vls.length != 1 || !getValue(vls[0]).equals("+61999999999"))
619         {
620             fail("telephonenumber escaped + not reduced properly");
621         }
622 
623         // test query methods
624         if (!"E".equals(BCStyle.INSTANCE.oidToDisplayName(BCStyle.EmailAddress)))
625         {
626             fail("display name for E incorrect");
627         }
628 
629         String[] aliases = BCStyle.INSTANCE.oidToAttrNames(BCStyle.EmailAddress);
630         if (aliases.length != 2)
631         {
632             fail("no aliases found");
633         }
634         if (!("e".equals(aliases[0]) || "e".equals(aliases[1])))
635         {
636             fail("first alias name for E incorrect");
637         }
638         if (!("emailaddress".equals(aliases[0]) || "emailaddress".equals(aliases[1])))
639         {
640             fail("second alias name for E incorrect");
641         }
642 
643         if (BCStyle.INSTANCE.oidToDisplayName(new ASN1ObjectIdentifier("1.2.1")) != null)
644         {
645             fail("unknown oid matched!");
646         }
647 
648         if (BCStyle.INSTANCE.oidToAttrNames(new ASN1ObjectIdentifier("1.2.1")).length != 0)
649         {
650             fail("unknown oid matched aliases!");
651         }
652 
653         if (!new X500Name("CN=\"  CA1 -   CP.04.03\", OU=Testing, OU=Dod, O=U.S. Government, C=US")
654             .equals(new X500Name("CN=\"ca1 - CP.04.03  \", OU=Testing, OU=Dod, O=U.S. Government, C=US")))
655         {
656             fail("padded equality test failed");
657         }
658     }
659 
getValue(RDN vl)660     private String getValue(RDN vl)
661     {
662         return ((ASN1String)vl.getFirst().getValue()).getString();
663     }
664 
ietfUtilsTest()665     private void ietfUtilsTest()
666         throws Exception
667     {
668         IETFUtils.valueToString(new DERUTF8String(" "));
669     }
670 
671     /*
672   private boolean compareVectors(Vector a, Vector b)    // for compatibility with early JDKs
673   {
674       if (a.size() != b.size())
675       {
676           return false;
677       }
678 
679       for (int i = 0; i != a.size(); i++)
680       {
681           if (!a.elementAt(i).equals(b.elementAt(i)))
682           {
683               return false;
684           }
685       }
686 
687       return true;
688   }
689 
690   private void compositeTest()
691       throws IOException
692   {
693       //
694       // composite test
695       //
696       byte[]  enc = Hex.decode("305e310b300906035504061302415531283026060355040a0c1f546865204c6567696f6e206f662074686520426f756e637920436173746c653125301006035504070c094d656c626f75726e653011060355040b0c0a4173636f742056616c65");
697       ASN1InputStream aIn = new ASN1InputStream(new ByteArrayInputStream(enc));
698 
699       X500Name    n = X500Name.getInstance(aIn.readObject());
700 
701       if (!n.toString().equals("C=AU,O=The Legion of the Bouncy Castle,L=Melbourne+OU=Ascot Vale"))
702       {
703           fail("Failed composite to string test got: " + n.toString());
704       }
705 
706       if (!n.toString(true, X500Name.DefaultSymbols).equals("L=Melbourne+OU=Ascot Vale,O=The Legion of the Bouncy Castle,C=AU"))
707       {
708           fail("Failed composite to string test got: " + n.toString(true, X500Name.DefaultSymbols));
709       }
710 
711       n = new X500Name(true, "L=Melbourne+OU=Ascot Vale,O=The Legion of the Bouncy Castle,C=AU");
712       if (!n.toString().equals("C=AU,O=The Legion of the Bouncy Castle,L=Melbourne+OU=Ascot Vale"))
713       {
714           fail("Failed composite to string reversal test got: " + n.toString());
715       }
716 
717       n = new X500Name("C=AU, O=The Legion of the Bouncy Castle, L=Melbourne + OU=Ascot Vale");
718 
719       ByteArrayOutputStream bOut = new ByteArrayOutputStream();
720       ASN1OutputStream aOut = new ASN1OutputStream(bOut);
721 
722       aOut.writeObject(n);
723 
724       byte[]  enc2 = bOut.toByteArray();
725 
726       if (!Arrays.areEqual(enc, enc2))
727       {
728           fail("Failed composite string to encoding test");
729       }
730 
731       //
732       // dud name test - handle empty DN without barfing.
733       //
734       n = new X500Name("C=CH,O=,OU=dummy,CN=mail@dummy.com");
735 
736       n = X500Name.getInstance(ASN1Object.fromByteArray(n.getEncoded()));
737   }
738     */
equalityTest(X500Name name1, X500Name name2)739     private void equalityTest(X500Name name1, X500Name name2)
740     {
741         if (!name1.equals(name2))
742         {
743             fail("equality test failed for " + name1 + " : " + name2);
744         }
745 
746         if (name1.hashCode() != name2.hashCode())
747         {
748             fail("hashCodeTest test failed for " + name1 + " : " + name2);
749         }
750     }
751 
752 
main( String[] args)753     public static void main(
754         String[]    args)
755     {
756         runTest(new X500NameTest());
757     }
758 }
759