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