resolve darcs stupidity
[org.ibex.core.git] / src / org / bouncycastle / asn1 / x509 / X509Extensions.java
1 package org.bouncycastle.asn1.x509;
2
3 import java.util.*;
4
5 import org.bouncycastle.asn1.*;
6
7 public class X509Extensions
8     implements DEREncodable
9 {
10     /**
11      * Subject Key Identifier 
12      */
13     public static final DERObjectIdentifier SubjectKeyIdentifier = new DERObjectIdentifier("2.5.29.14");
14
15     /**
16      * Key Usage 
17      */
18     public static final DERObjectIdentifier KeyUsage = new DERObjectIdentifier("2.5.29.15");
19
20     /**
21      * Private Key Usage Period 
22      */
23     public static final DERObjectIdentifier PrivateKeyUsagePeriod = new DERObjectIdentifier("2.5.29.16");
24
25     /**
26      * Subject Alternative Name 
27      */
28     public static final DERObjectIdentifier SubjectAlternativeName = new DERObjectIdentifier("2.5.29.17");
29
30     /**
31      * Issuer Alternative Name 
32      */
33     public static final DERObjectIdentifier IssuerAlternativeName = new DERObjectIdentifier("2.5.29.18");
34
35     /**
36      * Basic Constraints 
37      */
38     public static final DERObjectIdentifier BasicConstraints = new DERObjectIdentifier("2.5.29.19");
39
40     /**
41      * CRL Number 
42      */
43     public static final DERObjectIdentifier CRLNumber = new DERObjectIdentifier("2.5.29.20");
44
45     /**
46      * Reason code 
47      */
48     public static final DERObjectIdentifier ReasonCode = new DERObjectIdentifier("2.5.29.21");
49
50     /**
51      * Hold Instruction Code 
52      */
53     public static final DERObjectIdentifier InstructionCode = new DERObjectIdentifier("2.5.29.23");
54
55     /**
56      * Invalidity Date 
57      */
58     public static final DERObjectIdentifier InvalidityDate = new DERObjectIdentifier("2.5.29.24");
59
60     /**
61      * Delta CRL indicator 
62      */
63     public static final DERObjectIdentifier DeltaCRLIndicator = new DERObjectIdentifier("2.5.29.27");
64
65     /**
66      * Issuing Distribution Point 
67      */
68     public static final DERObjectIdentifier IssuingDistributionPoint = new DERObjectIdentifier("2.5.29.28");
69
70     /**
71      * Certificate Issuer 
72      */
73     public static final DERObjectIdentifier CertificateIssuer = new DERObjectIdentifier("2.5.29.29");
74
75     /**
76      * Name Constraints 
77      */
78     public static final DERObjectIdentifier NameConstraints = new DERObjectIdentifier("2.5.29.30");
79
80     /**
81      * CRL Distribution Points 
82      */
83     public static final DERObjectIdentifier CRLDistributionPoints = new DERObjectIdentifier("2.5.29.31");
84
85     /**
86      * Certificate Policies 
87      */
88     public static final DERObjectIdentifier CertificatePolicies = new DERObjectIdentifier("2.5.29.32");
89
90     /**
91      * Policy Mappings 
92      */
93     public static final DERObjectIdentifier PolicyMappings = new DERObjectIdentifier("2.5.29.33");
94
95     /**
96      * Authority Key Identifier 
97      */
98     public static final DERObjectIdentifier AuthorityKeyIdentifier = new DERObjectIdentifier("2.5.29.35");
99
100     /**
101      * Policy Constraints 
102      */
103     public static final DERObjectIdentifier PolicyConstraints = new DERObjectIdentifier("2.5.29.36");
104
105     /**
106      * Extended Key Usage 
107      */
108     public static final DERObjectIdentifier ExtendedKeyUsage = new DERObjectIdentifier("2.5.29.37");
109
110     private Hashtable               extensions = new Hashtable();
111     private Vector                  ordering = new Vector();
112
113     public static X509Extensions getInstance(
114         ASN1TaggedObject obj,
115         boolean          explicit)
116     {
117         return getInstance(ASN1Sequence.getInstance(obj, explicit));
118     }
119
120     public static X509Extensions getInstance(
121         Object  obj)
122     {
123         if (obj == null || obj instanceof X509Extensions)
124         {
125             return (X509Extensions)obj;
126         }
127
128         if (obj instanceof ASN1Sequence)
129         {
130             return new X509Extensions((ASN1Sequence)obj);
131         }
132
133         if (obj instanceof ASN1TaggedObject)
134         {
135             return getInstance(((ASN1TaggedObject)obj).getObject());
136         }
137
138         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
139     }
140
141     /**
142      * Constructor from DERConstructedSequence.
143      *
144      * the extensions are a list of constructed sequences, either with (OID, OctetString) or (OID, Boolean, OctetString)
145      */
146     public X509Extensions(
147         ASN1Sequence  seq)
148     {
149         Enumeration e = seq.getObjects();
150
151         while (e.hasMoreElements())
152         {
153             ASN1Sequence            s = (ASN1Sequence)e.nextElement();
154             Enumeration             e1 = s.getObjects();
155
156             if (s.size() == 3)
157             {
158                 extensions.put(s.getObjectAt(0), new X509Extension((DERBoolean)s.getObjectAt(1), (DEROctetString)s.getObjectAt(2)));
159             }
160             else
161             {
162                 extensions.put(s.getObjectAt(0), new X509Extension(false, (DEROctetString)s.getObjectAt(1)));
163             }
164
165             ordering.addElement(s.getObjectAt(0));
166         }
167     }
168
169     /**
170      * constructor from a table of extensions.
171      * <p>
172      * it's is assumed the table contains OID/String pairs.
173      */
174     public X509Extensions(
175         Hashtable  extensions)
176     {
177         this(null, extensions);
178     }
179
180     /**
181      * constructor from a table of extensions with ordering
182      * <p>
183      * it's is assumed the table contains OID/String pairs.
184      */
185     public X509Extensions(
186         Vector      ordering,
187         Hashtable   extensions)
188     {
189         Enumeration e;
190
191         if (ordering == null)
192         {
193             e = extensions.keys();
194         }
195         else
196         {
197             e = ordering.elements();
198         }
199
200         while (e.hasMoreElements())
201         {
202             this.ordering.addElement(e.nextElement()); 
203         }
204
205         e = this.ordering.elements();
206
207         while (e.hasMoreElements())
208         {
209             DERObjectIdentifier     oid = (DERObjectIdentifier)e.nextElement();
210             X509Extension           ext = (X509Extension)extensions.get(oid);
211
212             this.extensions.put(oid, ext);
213         }
214     }
215
216     /**
217      * return an Enumeration of the extension field's object ids.
218      */
219     public Enumeration oids()
220     {
221         return ordering.elements();
222     }
223
224     /**
225      * return the extension represented by the object identifier
226      * passed in.
227      *
228      * @return the extension if it's present, null otherwise.
229      */
230     public X509Extension getExtension(
231         DERObjectIdentifier oid)
232     {
233         return (X509Extension)extensions.get(oid);
234     }
235
236     public DERObject getDERObject()
237     {
238         DEREncodableVector      vec = new DEREncodableVector();
239         Enumeration             e = ordering.elements();
240
241         while (e.hasMoreElements())
242         {
243             DERObjectIdentifier     oid = (DERObjectIdentifier)e.nextElement();
244             X509Extension           ext = (X509Extension)extensions.get(oid);
245             DEREncodableVector      v = new DEREncodableVector();
246
247             v.add(oid);
248
249             if (ext.isCritical())
250             {
251                 v.add(new DERBoolean(true));
252             }
253
254             v.add(ext.getValue());
255
256             vec.add(new DERSequence(v));
257         }
258
259         return new DERSequence(vec);
260     }
261
262     public int hashCode()
263     {
264         Enumeration     e = extensions.keys();
265         int             hashCode = 0;
266
267         while (e.hasMoreElements())
268         {
269             Object  o = e.nextElement();
270
271             hashCode ^= o.hashCode();
272             hashCode ^= extensions.get(o).hashCode();
273         }
274
275         return hashCode;
276     }
277
278     public boolean equals(
279         Object o)
280     {
281         if (o == null || !(o instanceof X509Extensions))
282         {
283             return false;
284         }
285
286         X509Extensions  other = (X509Extensions)o;
287
288         Enumeration     e1 = extensions.keys();
289         Enumeration     e2 = other.extensions.keys();
290
291         while (e1.hasMoreElements() && e2.hasMoreElements())
292         {
293             Object  o1 = e1.nextElement();
294             Object  o2 = e2.nextElement();
295             
296             if (!o1.equals(o2))
297             {
298                 return false;
299             }
300         }
301
302         if (e1.hasMoreElements() || e2.hasMoreElements())
303         {
304             return false;
305         }
306
307         return true;
308     }
309 }