1 package org.bouncycastle.asn1.x509;
6 import org.bouncycastle.asn1.*;
9 implements DEREncodable
12 * country code - StringType(SIZE(2))
14 public static final DERObjectIdentifier C = new DERObjectIdentifier("2.5.4.6");
17 * organization - StringType(SIZE(1..64))
19 public static final DERObjectIdentifier O = new DERObjectIdentifier("2.5.4.10");
22 * organizational unit name - StringType(SIZE(1..64))
24 public static final DERObjectIdentifier OU = new DERObjectIdentifier("2.5.4.11");
27 * common name - StringType(SIZE(1..64))
29 public static final DERObjectIdentifier CN = new DERObjectIdentifier("2.5.4.3");
32 * device serial number name - StringType(SIZE(1..64))
34 public static final DERObjectIdentifier SN = new DERObjectIdentifier("2.5.4.5");
37 * locality name - StringType(SIZE(1..64))
39 public static final DERObjectIdentifier L = new DERObjectIdentifier("2.5.4.7");
42 * state, or province name - StringType(SIZE(1..64))
44 public static final DERObjectIdentifier ST = new DERObjectIdentifier("2.5.4.8");
47 * email address (RSA PKCS#9 extension) - IA5String
49 * note: if you're trying to be ultra orthodox, don't use this! It shouldn't be in here.
51 public static final DERObjectIdentifier EmailAddress = new DERObjectIdentifier("1.2.840.113549.1.9.1");
54 * look up table translating OID values into their common symbols.
56 public static Hashtable OIDLookUp = new Hashtable();
59 * look up table translating common symbols into their OIDS.
61 public static Hashtable SymbolLookUp = new Hashtable();
65 OIDLookUp.put(C, "C");
66 OIDLookUp.put(O, "O");
67 OIDLookUp.put(OU, "OU");
68 OIDLookUp.put(CN, "CN");
69 OIDLookUp.put(L, "L");
70 OIDLookUp.put(ST, "ST");
71 OIDLookUp.put(SN, "SN");
72 OIDLookUp.put(EmailAddress, "EmailAddress");
74 SymbolLookUp.put("C", C);
75 SymbolLookUp.put("O", O);
76 SymbolLookUp.put("OU", OU);
77 SymbolLookUp.put("CN", CN);
78 SymbolLookUp.put("L", L);
79 SymbolLookUp.put("ST", ST);
80 SymbolLookUp.put("SN", SN);
81 SymbolLookUp.put("EmailAddress", EmailAddress);
84 private Vector ordering = new Vector();
85 private Hashtable attributes = new Hashtable();
86 private DERConstructedSequence seq = null;
89 * Constructor from DERConstructedSequence.
91 * the principal will be a list of constructed sets, each containing an (OID, String) pair.
94 DERConstructedSequence seq)
98 Enumeration e = seq.getObjects();
100 while (e.hasMoreElements())
102 DERSet set = (DERSet)e.nextElement();
103 DERConstructedSequence s = (DERConstructedSequence)set.getSequence();
105 ordering.addElement(s.getObjectAt(0));
106 attributes.put(s.getObjectAt(0), ((DERString)s.getObjectAt(1)).getString());
111 * constructor from a table of attributes.
113 * it's is assumed the table contains OID/String pairs, and the contents
114 * of the table are copied into an internal table as part of the
115 * construction process.
117 * <b>Note:</b> if the name you are trying to generate should be
118 * following a specific ordering, you should use the constructor
119 * with the ordering specified below.
122 Hashtable attributes)
124 this(null, attributes);
128 * constructor from a table of attributes with ordering.
130 * it's is assumed the table contains OID/String pairs, and the contents
131 * of the table are copied into an internal table as part of the
132 * construction process. The ordering vector should contain the OIDs
133 * in the order they are meant to be encoded or printed in toString.
137 Hashtable attributes)
139 if (ordering != null)
141 for (int i = 0; i != ordering.size(); i++)
143 this.ordering.addElement(ordering.elementAt(i));
148 Enumeration e = attributes.keys();
150 while (e.hasMoreElements())
152 this.ordering.addElement(e.nextElement());
156 for (int i = 0; i != this.ordering.size(); i++)
158 DERObjectIdentifier oid = (DERObjectIdentifier)this.ordering.elementAt(i);
160 if (OIDLookUp.get(oid) == null)
162 throw new IllegalArgumentException("Unknown object id - " + oid.getId() + " - passed to distinguished name");
165 if (attributes.get(oid) == null)
167 throw new IllegalArgumentException("No attribute for object id - " + oid.getId() + " - passed to distinguished name");
170 this.attributes.put(oid, attributes.get(oid)); // copy the hash table
175 * takes an X509 dir name as a string of the format "C=AU, ST=Victoria", or
176 * some such, converting it into an ordered set of name attributes.
181 X509NameTokenizer nTok = new X509NameTokenizer(dirName);
183 while (nTok.hasMoreTokens())
185 String token = nTok.nextToken();
186 int index = token.indexOf('=');
190 throw new IllegalArgumentException("badly formated directory string");
193 String name = token.substring(0, index);
194 String value = token.substring(index + 1);
196 DERObjectIdentifier oid = (DERObjectIdentifier)SymbolLookUp.get(name);
199 throw new IllegalArgumentException("Unknown object id - " + oid.getId() + " - passed to distinguished name");
202 this.ordering.addElement(oid);
203 this.attributes.put(oid, value);
207 public DERObject getDERObject()
211 seq = new DERConstructedSequence();
213 for (int i = 0; i != ordering.size(); i++)
215 DERConstructedSequence s = new DERConstructedSequence();
216 DERObjectIdentifier oid = (DERObjectIdentifier)ordering.elementAt(i);
219 if (oid.equals(EmailAddress))
221 s.addObject(new DERIA5String((String)attributes.get(oid)));
225 s.addObject(new DERPrintableString((String)attributes.get(oid)));
228 seq.addObject(new DERSet(s));
235 public int hashCode()
237 Enumeration e = attributes.keys();
240 while (e.hasMoreElements())
242 Object o = e.nextElement();
244 hashCode ^= o.hashCode();
245 hashCode ^= attributes.get(o).hashCode();
248 for (int i = 0; i != ordering.size(); i++)
250 hashCode ^= ordering.elementAt(i).hashCode();
256 public boolean equals(
259 if (o == null || !(o instanceof X509Name))
264 X509Name other = (X509Name)o;
266 if (ordering.size() != other.ordering.size())
271 for (int i = 0; i != ordering.size(); i++)
273 if (!ordering.elementAt(i).equals(other.ordering.elementAt(i)))
279 Enumeration e1 = attributes.keys();
280 Enumeration e2 = other.attributes.keys();
282 while (e1.hasMoreElements() && e2.hasMoreElements())
284 Object o1 = e1.nextElement();
285 Object o2 = e2.nextElement();
293 if (e1.hasMoreElements() || e2.hasMoreElements())
301 public String toString()
303 StringBuffer buf = new StringBuffer();
304 boolean first = true;
306 for (int i = 0; i != ordering.size(); i++)
308 Object oid = ordering.elementAt(i);
309 String sym = (String)OIDLookUp.get(oid);
324 buf.append((String)attributes.get(oid));
328 buf.append(((DERObjectIdentifier)oid).getId());
330 buf.append((String)attributes.get(oid));
334 return buf.toString();