BCStyle.java revision a198e1ecc615e26a167d0f2dca9fa7e5fc62de10
1package org.bouncycastle.asn1.x500.style;
2
3import java.io.IOException;
4import java.util.Hashtable;
5
6import org.bouncycastle.asn1.ASN1Encodable;
7import org.bouncycastle.asn1.ASN1GeneralizedTime;
8import org.bouncycastle.asn1.ASN1ObjectIdentifier;
9import org.bouncycastle.asn1.DERIA5String;
10import org.bouncycastle.asn1.DERPrintableString;
11import org.bouncycastle.asn1.DERUTF8String;
12import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
13import org.bouncycastle.asn1.x500.AttributeTypeAndValue;
14import org.bouncycastle.asn1.x500.RDN;
15import org.bouncycastle.asn1.x500.X500Name;
16import org.bouncycastle.asn1.x500.X500NameStyle;
17import org.bouncycastle.asn1.x509.X509ObjectIdentifiers;
18
19public class BCStyle
20    implements X500NameStyle
21{
22    public static final X500NameStyle INSTANCE = new BCStyle();
23
24    /**
25     * country code - StringType(SIZE(2))
26     */
27    public static final ASN1ObjectIdentifier C = new ASN1ObjectIdentifier("2.5.4.6");
28
29    /**
30     * organization - StringType(SIZE(1..64))
31     */
32    public static final ASN1ObjectIdentifier O = new ASN1ObjectIdentifier("2.5.4.10");
33
34    /**
35     * organizational unit name - StringType(SIZE(1..64))
36     */
37    public static final ASN1ObjectIdentifier OU = new ASN1ObjectIdentifier("2.5.4.11");
38
39    /**
40     * Title
41     */
42    public static final ASN1ObjectIdentifier T = new ASN1ObjectIdentifier("2.5.4.12");
43
44    /**
45     * common name - StringType(SIZE(1..64))
46     */
47    public static final ASN1ObjectIdentifier CN = new ASN1ObjectIdentifier("2.5.4.3");
48
49    /**
50     * device serial number name - StringType(SIZE(1..64))
51     */
52    public static final ASN1ObjectIdentifier SN = new ASN1ObjectIdentifier("2.5.4.5");
53
54    /**
55     * street - StringType(SIZE(1..64))
56     */
57    public static final ASN1ObjectIdentifier STREET = new ASN1ObjectIdentifier("2.5.4.9");
58
59    /**
60     * device serial number name - StringType(SIZE(1..64))
61     */
62    public static final ASN1ObjectIdentifier SERIALNUMBER = SN;
63
64    /**
65     * locality name - StringType(SIZE(1..64))
66     */
67    public static final ASN1ObjectIdentifier L = new ASN1ObjectIdentifier("2.5.4.7");
68
69    /**
70     * state, or province name - StringType(SIZE(1..64))
71     */
72    public static final ASN1ObjectIdentifier ST = new ASN1ObjectIdentifier("2.5.4.8");
73
74    /**
75     * Naming attributes of type X520name
76     */
77    public static final ASN1ObjectIdentifier SURNAME = new ASN1ObjectIdentifier("2.5.4.4");
78    public static final ASN1ObjectIdentifier GIVENNAME = new ASN1ObjectIdentifier("2.5.4.42");
79    public static final ASN1ObjectIdentifier INITIALS = new ASN1ObjectIdentifier("2.5.4.43");
80    public static final ASN1ObjectIdentifier GENERATION = new ASN1ObjectIdentifier("2.5.4.44");
81    public static final ASN1ObjectIdentifier UNIQUE_IDENTIFIER = new ASN1ObjectIdentifier("2.5.4.45");
82
83    /**
84     * businessCategory - DirectoryString(SIZE(1..128)
85     */
86    public static final ASN1ObjectIdentifier BUSINESS_CATEGORY = new ASN1ObjectIdentifier(
87        "2.5.4.15");
88
89    /**
90     * postalCode - DirectoryString(SIZE(1..40)
91     */
92    public static final ASN1ObjectIdentifier POSTAL_CODE = new ASN1ObjectIdentifier(
93        "2.5.4.17");
94
95    /**
96     * dnQualifier - DirectoryString(SIZE(1..64)
97     */
98    public static final ASN1ObjectIdentifier DN_QUALIFIER = new ASN1ObjectIdentifier(
99        "2.5.4.46");
100
101    /**
102     * RFC 3039 Pseudonym - DirectoryString(SIZE(1..64)
103     */
104    public static final ASN1ObjectIdentifier PSEUDONYM = new ASN1ObjectIdentifier(
105        "2.5.4.65");
106
107
108    /**
109     * RFC 3039 DateOfBirth - GeneralizedTime - YYYYMMDD000000Z
110     */
111    public static final ASN1ObjectIdentifier DATE_OF_BIRTH = new ASN1ObjectIdentifier(
112        "1.3.6.1.5.5.7.9.1");
113
114    /**
115     * RFC 3039 PlaceOfBirth - DirectoryString(SIZE(1..128)
116     */
117    public static final ASN1ObjectIdentifier PLACE_OF_BIRTH = new ASN1ObjectIdentifier(
118        "1.3.6.1.5.5.7.9.2");
119
120    /**
121     * RFC 3039 Gender - PrintableString (SIZE(1)) -- "M", "F", "m" or "f"
122     */
123    public static final ASN1ObjectIdentifier GENDER = new ASN1ObjectIdentifier(
124        "1.3.6.1.5.5.7.9.3");
125
126    /**
127     * RFC 3039 CountryOfCitizenship - PrintableString (SIZE (2)) -- ISO 3166
128     * codes only
129     */
130    public static final ASN1ObjectIdentifier COUNTRY_OF_CITIZENSHIP = new ASN1ObjectIdentifier(
131        "1.3.6.1.5.5.7.9.4");
132
133    /**
134     * RFC 3039 CountryOfResidence - PrintableString (SIZE (2)) -- ISO 3166
135     * codes only
136     */
137    public static final ASN1ObjectIdentifier COUNTRY_OF_RESIDENCE = new ASN1ObjectIdentifier(
138        "1.3.6.1.5.5.7.9.5");
139
140
141    /**
142     * ISIS-MTT NameAtBirth - DirectoryString(SIZE(1..64)
143     */
144    public static final ASN1ObjectIdentifier NAME_AT_BIRTH = new ASN1ObjectIdentifier("1.3.36.8.3.14");
145
146    /**
147     * RFC 3039 PostalAddress - SEQUENCE SIZE (1..6) OF
148     * DirectoryString(SIZE(1..30))
149     */
150    public static final ASN1ObjectIdentifier POSTAL_ADDRESS = new ASN1ObjectIdentifier("2.5.4.16");
151
152    /**
153     * RFC 2256 dmdName
154     */
155    public static final ASN1ObjectIdentifier DMD_NAME = new ASN1ObjectIdentifier("2.5.4.54");
156
157    /**
158     * id-at-telephoneNumber
159     */
160    public static final ASN1ObjectIdentifier TELEPHONE_NUMBER = X509ObjectIdentifiers.id_at_telephoneNumber;
161
162    /**
163     * id-at-name
164     */
165    public static final ASN1ObjectIdentifier NAME = X509ObjectIdentifiers.id_at_name;
166
167    /**
168     * Email address (RSA PKCS#9 extension) - IA5String.
169     * <p>Note: if you're trying to be ultra orthodox, don't use this! It shouldn't be in here.
170     */
171    public static final ASN1ObjectIdentifier EmailAddress = PKCSObjectIdentifiers.pkcs_9_at_emailAddress;
172
173    /**
174     * more from PKCS#9
175     */
176    public static final ASN1ObjectIdentifier UnstructuredName = PKCSObjectIdentifiers.pkcs_9_at_unstructuredName;
177    public static final ASN1ObjectIdentifier UnstructuredAddress = PKCSObjectIdentifiers.pkcs_9_at_unstructuredAddress;
178
179    /**
180     * email address in Verisign certificates
181     */
182    public static final ASN1ObjectIdentifier E = EmailAddress;
183
184    /*
185    * others...
186    */
187    public static final ASN1ObjectIdentifier DC = new ASN1ObjectIdentifier("0.9.2342.19200300.100.1.25");
188
189    /**
190     * LDAP User id.
191     */
192    public static final ASN1ObjectIdentifier UID = new ASN1ObjectIdentifier("0.9.2342.19200300.100.1.1");
193
194    /**
195     * default look up table translating OID values into their common symbols following
196     * the convention in RFC 2253 with a few extras
197     */
198    private static final Hashtable DefaultSymbols = new Hashtable();
199
200    /**
201     * look up table translating common symbols into their OIDS.
202     */
203    private static final Hashtable DefaultLookUp = new Hashtable();
204
205    static
206    {
207        DefaultSymbols.put(C, "C");
208        DefaultSymbols.put(O, "O");
209        DefaultSymbols.put(T, "T");
210        DefaultSymbols.put(OU, "OU");
211        DefaultSymbols.put(CN, "CN");
212        DefaultSymbols.put(L, "L");
213        DefaultSymbols.put(ST, "ST");
214        DefaultSymbols.put(SN, "SERIALNUMBER");
215        DefaultSymbols.put(EmailAddress, "E");
216        DefaultSymbols.put(DC, "DC");
217        DefaultSymbols.put(UID, "UID");
218        DefaultSymbols.put(STREET, "STREET");
219        DefaultSymbols.put(SURNAME, "SURNAME");
220        DefaultSymbols.put(GIVENNAME, "GIVENNAME");
221        DefaultSymbols.put(INITIALS, "INITIALS");
222        DefaultSymbols.put(GENERATION, "GENERATION");
223        DefaultSymbols.put(UnstructuredAddress, "unstructuredAddress");
224        DefaultSymbols.put(UnstructuredName, "unstructuredName");
225        DefaultSymbols.put(UNIQUE_IDENTIFIER, "UniqueIdentifier");
226        DefaultSymbols.put(DN_QUALIFIER, "DN");
227        DefaultSymbols.put(PSEUDONYM, "Pseudonym");
228        DefaultSymbols.put(POSTAL_ADDRESS, "PostalAddress");
229        DefaultSymbols.put(NAME_AT_BIRTH, "NameAtBirth");
230        DefaultSymbols.put(COUNTRY_OF_CITIZENSHIP, "CountryOfCitizenship");
231        DefaultSymbols.put(COUNTRY_OF_RESIDENCE, "CountryOfResidence");
232        DefaultSymbols.put(GENDER, "Gender");
233        DefaultSymbols.put(PLACE_OF_BIRTH, "PlaceOfBirth");
234        DefaultSymbols.put(DATE_OF_BIRTH, "DateOfBirth");
235        DefaultSymbols.put(POSTAL_CODE, "PostalCode");
236        DefaultSymbols.put(BUSINESS_CATEGORY, "BusinessCategory");
237        DefaultSymbols.put(TELEPHONE_NUMBER, "TelephoneNumber");
238        DefaultSymbols.put(NAME, "Name");
239
240        DefaultLookUp.put("c", C);
241        DefaultLookUp.put("o", O);
242        DefaultLookUp.put("t", T);
243        DefaultLookUp.put("ou", OU);
244        DefaultLookUp.put("cn", CN);
245        DefaultLookUp.put("l", L);
246        DefaultLookUp.put("st", ST);
247        DefaultLookUp.put("sn", SN);
248        DefaultLookUp.put("serialnumber", SN);
249        DefaultLookUp.put("street", STREET);
250        DefaultLookUp.put("emailaddress", E);
251        DefaultLookUp.put("dc", DC);
252        DefaultLookUp.put("e", E);
253        DefaultLookUp.put("uid", UID);
254        DefaultLookUp.put("surname", SURNAME);
255        DefaultLookUp.put("givenname", GIVENNAME);
256        DefaultLookUp.put("initials", INITIALS);
257        DefaultLookUp.put("generation", GENERATION);
258        DefaultLookUp.put("unstructuredaddress", UnstructuredAddress);
259        DefaultLookUp.put("unstructuredname", UnstructuredName);
260        DefaultLookUp.put("uniqueidentifier", UNIQUE_IDENTIFIER);
261        DefaultLookUp.put("dn", DN_QUALIFIER);
262        DefaultLookUp.put("pseudonym", PSEUDONYM);
263        DefaultLookUp.put("postaladdress", POSTAL_ADDRESS);
264        DefaultLookUp.put("nameofbirth", NAME_AT_BIRTH);
265        DefaultLookUp.put("countryofcitizenship", COUNTRY_OF_CITIZENSHIP);
266        DefaultLookUp.put("countryofresidence", COUNTRY_OF_RESIDENCE);
267        DefaultLookUp.put("gender", GENDER);
268        DefaultLookUp.put("placeofbirth", PLACE_OF_BIRTH);
269        DefaultLookUp.put("dateofbirth", DATE_OF_BIRTH);
270        DefaultLookUp.put("postalcode", POSTAL_CODE);
271        DefaultLookUp.put("businesscategory", BUSINESS_CATEGORY);
272        DefaultLookUp.put("telephonenumber", TELEPHONE_NUMBER);
273        DefaultLookUp.put("name", NAME);
274    }
275
276    protected BCStyle()
277    {
278
279    }
280
281    public ASN1Encodable stringToValue(ASN1ObjectIdentifier oid, String value)
282    {
283        if (value.length() != 0 && value.charAt(0) == '#')
284        {
285            try
286            {
287                return IETFUtils.valueFromHexString(value, 1);
288            }
289            catch (IOException e)
290            {
291                throw new RuntimeException("can't recode value for oid " + oid.getId());
292            }
293        }
294        else
295        {
296            if (value.length() != 0 && value.charAt(0) == '\\')
297            {
298                value = value.substring(1);
299            }
300            if (oid.equals(EmailAddress) || oid.equals(DC))
301            {
302                return new DERIA5String(value);
303            }
304            else if (oid.equals(DATE_OF_BIRTH))  // accept time string as well as # (for compatibility)
305            {
306                return new ASN1GeneralizedTime(value);
307            }
308            else if (oid.equals(C) || oid.equals(SN) || oid.equals(DN_QUALIFIER)
309                || oid.equals(TELEPHONE_NUMBER))
310            {
311                return new DERPrintableString(value);
312            }
313        }
314
315        return new DERUTF8String(value);
316    }
317
318    public String oidToDisplayName(ASN1ObjectIdentifier oid)
319    {
320        return (String)DefaultSymbols.get(oid);
321    }
322
323    public String[] oidToAttrNames(ASN1ObjectIdentifier oid)
324    {
325        return IETFUtils.findAttrNamesForOID(oid, DefaultLookUp);
326    }
327
328    public ASN1ObjectIdentifier attrNameToOID(String attrName)
329    {
330        return IETFUtils.decodeAttrName(attrName, DefaultLookUp);
331    }
332
333    public boolean areEqual(X500Name name1, X500Name name2)
334    {
335        RDN[] rdns1 = name1.getRDNs();
336        RDN[] rdns2 = name2.getRDNs();
337
338        if (rdns1.length != rdns2.length)
339        {
340            return false;
341        }
342
343        boolean reverse = false;
344
345        if (rdns1[0].getFirst() != null && rdns2[0].getFirst() != null)
346        {
347            reverse = !rdns1[0].getFirst().getType().equals(rdns2[0].getFirst().getType());  // guess forward
348        }
349
350        for (int i = 0; i != rdns1.length; i++)
351        {
352            if (!foundMatch(reverse, rdns1[i], rdns2))
353            {
354                return false;
355            }
356        }
357
358        return true;
359    }
360
361    private boolean foundMatch(boolean reverse, RDN rdn, RDN[] possRDNs)
362    {
363        if (reverse)
364        {
365            for (int i = possRDNs.length - 1; i >= 0; i--)
366            {
367                if (possRDNs[i] != null && rdnAreEqual(rdn, possRDNs[i]))
368                {
369                    possRDNs[i] = null;
370                    return true;
371                }
372            }
373        }
374        else
375        {
376            for (int i = 0; i != possRDNs.length; i++)
377            {
378                if (possRDNs[i] != null && rdnAreEqual(rdn, possRDNs[i]))
379                {
380                    possRDNs[i] = null;
381                    return true;
382                }
383            }
384        }
385
386        return false;
387    }
388
389    protected boolean rdnAreEqual(RDN rdn1, RDN rdn2)
390    {
391        return IETFUtils.rDNAreEqual(rdn1, rdn2);
392    }
393
394    public RDN[] fromString(String dirName)
395    {
396        return IETFUtils.rDNsFromString(dirName, this);
397    }
398
399    public int calculateHashCode(X500Name name)
400    {
401        int hashCodeValue = 0;
402        RDN[] rdns = name.getRDNs();
403
404        // this needs to be order independent, like equals
405        for (int i = 0; i != rdns.length; i++)
406        {
407            if (rdns[i].isMultiValued())
408            {
409                AttributeTypeAndValue[] atv = rdns[i].getTypesAndValues();
410
411                for (int j = 0; j != atv.length; j++)
412                {
413                    hashCodeValue ^= atv[j].getType().hashCode();
414                    hashCodeValue ^= calcHashCode(atv[j].getValue());
415                }
416            }
417            else
418            {
419                hashCodeValue ^= rdns[i].getFirst().getType().hashCode();
420                hashCodeValue ^= calcHashCode(rdns[i].getFirst().getValue());
421            }
422        }
423
424        return hashCodeValue;
425    }
426
427    private int calcHashCode(ASN1Encodable enc)
428    {
429        String value = IETFUtils.valueToString(enc);
430
431        value = IETFUtils.canonicalize(value);
432
433        return value.hashCode();
434    }
435
436    public String toString(X500Name name)
437    {
438        StringBuffer buf = new StringBuffer();
439        boolean first = true;
440
441        RDN[] rdns = name.getRDNs();
442
443        for (int i = 0; i < rdns.length; i++)
444        {
445            if (first)
446            {
447                first = false;
448            }
449            else
450            {
451                buf.append(',');
452            }
453
454            IETFUtils.appendRDN(buf, rdns[i], DefaultSymbols);
455        }
456
457        return buf.toString();
458    }
459}
460