KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > asn1 > x509 > X509Extensions


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.asn1.x509;
21
22 import java.util.Enumeration JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import com.maverick.crypto.asn1.ASN1EncodableVector;
27 import com.maverick.crypto.asn1.ASN1OctetString;
28 import com.maverick.crypto.asn1.ASN1Sequence;
29 import com.maverick.crypto.asn1.ASN1TaggedObject;
30 import com.maverick.crypto.asn1.DERBoolean;
31 import com.maverick.crypto.asn1.DEREncodable;
32 import com.maverick.crypto.asn1.DERObject;
33 import com.maverick.crypto.asn1.DERObjectIdentifier;
34 import com.maverick.crypto.asn1.DERSequence;
35
36 public class X509Extensions
37     implements DEREncodable
38 {
39     /**
40      * Subject Key Identifier
41      */

42     public static final DERObjectIdentifier SubjectKeyIdentifier = new DERObjectIdentifier("2.5.29.14");
43
44     /**
45      * Key Usage
46      */

47     public static final DERObjectIdentifier KeyUsage = new DERObjectIdentifier("2.5.29.15");
48
49     /**
50      * Private Key Usage Period
51      */

52     public static final DERObjectIdentifier PrivateKeyUsagePeriod = new DERObjectIdentifier("2.5.29.16");
53
54     /**
55      * Subject Alternative Name
56      */

57     public static final DERObjectIdentifier SubjectAlternativeName = new DERObjectIdentifier("2.5.29.17");
58
59     /**
60      * Issuer Alternative Name
61      */

62     public static final DERObjectIdentifier IssuerAlternativeName = new DERObjectIdentifier("2.5.29.18");
63
64     /**
65      * Basic Constraints
66      */

67     public static final DERObjectIdentifier BasicConstraints = new DERObjectIdentifier("2.5.29.19");
68
69     /**
70      * CRL Number
71      */

72     public static final DERObjectIdentifier CRLNumber = new DERObjectIdentifier("2.5.29.20");
73
74     /**
75      * Reason code
76      */

77     public static final DERObjectIdentifier ReasonCode = new DERObjectIdentifier("2.5.29.21");
78
79     /**
80      * Hold Instruction Code
81      */

82     public static final DERObjectIdentifier InstructionCode = new DERObjectIdentifier("2.5.29.23");
83
84     /**
85      * Invalidity Date
86      */

87     public static final DERObjectIdentifier InvalidityDate = new DERObjectIdentifier("2.5.29.24");
88
89     /**
90      * Delta CRL indicator
91      */

92     public static final DERObjectIdentifier DeltaCRLIndicator = new DERObjectIdentifier("2.5.29.27");
93
94     /**
95      * Issuing Distribution Point
96      */

97     public static final DERObjectIdentifier IssuingDistributionPoint = new DERObjectIdentifier("2.5.29.28");
98
99     /**
100      * Certificate Issuer
101      */

102     public static final DERObjectIdentifier CertificateIssuer = new DERObjectIdentifier("2.5.29.29");
103
104     /**
105      * Name Constraints
106      */

107     public static final DERObjectIdentifier NameConstraints = new DERObjectIdentifier("2.5.29.30");
108
109     /**
110      * CRL Distribution Points
111      */

112     public static final DERObjectIdentifier CRLDistributionPoints = new DERObjectIdentifier("2.5.29.31");
113
114     /**
115      * Certificate Policies
116      */

117     public static final DERObjectIdentifier CertificatePolicies = new DERObjectIdentifier("2.5.29.32");
118
119     /**
120      * Policy Mappings
121      */

122     public static final DERObjectIdentifier PolicyMappings = new DERObjectIdentifier("2.5.29.33");
123
124     /**
125      * Authority Key Identifier
126      */

127     public static final DERObjectIdentifier AuthorityKeyIdentifier = new DERObjectIdentifier("2.5.29.35");
128
129     /**
130      * Policy Constraints
131      */

132     public static final DERObjectIdentifier PolicyConstraints = new DERObjectIdentifier("2.5.29.36");
133
134     /**
135      * Extended Key Usage
136      */

137     public static final DERObjectIdentifier ExtendedKeyUsage = new DERObjectIdentifier("2.5.29.37");
138
139     /**
140      * Inhibit Any Policy
141      */

142     public static final DERObjectIdentifier InhibitAnyPolicy = new DERObjectIdentifier("2.5.29.54");
143
144     /**
145      * Authority Info Access
146      */

147     public static final DERObjectIdentifier AuthorityInfoAccess= new DERObjectIdentifier("1.3.6.1.5.5.7.1.1");
148
149     private Hashtable JavaDoc extensions = new Hashtable JavaDoc();
150     private Vector JavaDoc ordering = new Vector JavaDoc();
151
152     public static X509Extensions getInstance(
153         ASN1TaggedObject obj,
154         boolean explicit)
155     {
156         return getInstance(ASN1Sequence.getInstance(obj, explicit));
157     }
158
159     public static X509Extensions getInstance(
160         Object JavaDoc obj)
161     {
162         if (obj == null || obj instanceof X509Extensions)
163         {
164             return (X509Extensions)obj;
165         }
166
167         if (obj instanceof ASN1Sequence)
168         {
169             return new X509Extensions((ASN1Sequence)obj);
170         }
171
172         if (obj instanceof ASN1TaggedObject)
173         {
174             return getInstance(((ASN1TaggedObject)obj).getObject());
175         }
176
177         throw new IllegalArgumentException JavaDoc("illegal object in getInstance: " + obj.getClass().getName());
178     }
179
180     /**
181      * Constructor from ASN1Sequence.
182      *
183      * the extensions are a list of constructed sequences, either with (OID, OctetString) or (OID, Boolean, OctetString)
184      */

185     public X509Extensions(
186         ASN1Sequence seq)
187     {
188         Enumeration JavaDoc e = seq.getObjects();
189
190         while (e.hasMoreElements())
191         {
192             ASN1Sequence s = (ASN1Sequence)e.nextElement();
193
194             if (s.size() == 3)
195             {
196                 extensions.put(s.getObjectAt(0), new X509Extension((DERBoolean)s.getObjectAt(1), (ASN1OctetString)s.getObjectAt(2)));
197             }
198             else
199             {
200                 extensions.put(s.getObjectAt(0), new X509Extension(false, (ASN1OctetString)s.getObjectAt(1)));
201             }
202
203             ordering.addElement(s.getObjectAt(0));
204         }
205     }
206
207     /**
208      * constructor from a table of extensions.
209      * <p>
210      * it's is assumed the table contains OID/String pairs.
211      */

212     public X509Extensions(
213         Hashtable JavaDoc extensions)
214     {
215         this(null, extensions);
216     }
217
218     /**
219      * Constructor from a table of extensions with ordering.
220      * <p>
221      * It's is assumed the table contains OID/String pairs.
222      */

223     public X509Extensions(
224         Vector JavaDoc ordering,
225         Hashtable JavaDoc extensions)
226     {
227         Enumeration JavaDoc e;
228
229         if (ordering == null)
230         {
231             e = extensions.keys();
232         }
233         else
234         {
235             e = ordering.elements();
236         }
237
238         while (e.hasMoreElements())
239         {
240             this.ordering.addElement(e.nextElement());
241         }
242
243         e = this.ordering.elements();
244
245         while (e.hasMoreElements())
246         {
247             DERObjectIdentifier oid = (DERObjectIdentifier)e.nextElement();
248             X509Extension ext = (X509Extension)extensions.get(oid);
249
250             this.extensions.put(oid, ext);
251         }
252     }
253
254     /**
255      * return an Enumeration of the extension field's object ids.
256      */

257     public Enumeration JavaDoc oids()
258     {
259         return ordering.elements();
260     }
261
262     /**
263      * return the extension represented by the object identifier
264      * passed in.
265      *
266      * @return the extension if it's present, null otherwise.
267      */

268     public X509Extension getExtension(
269         DERObjectIdentifier oid)
270     {
271         return (X509Extension)extensions.get(oid);
272     }
273
274     public DERObject getDERObject()
275     {
276         ASN1EncodableVector vec = new ASN1EncodableVector();
277         Enumeration JavaDoc e = ordering.elements();
278
279         while (e.hasMoreElements())
280         {
281             DERObjectIdentifier oid = (DERObjectIdentifier)e.nextElement();
282             X509Extension ext = (X509Extension)extensions.get(oid);
283             ASN1EncodableVector v = new ASN1EncodableVector();
284
285             v.add(oid);
286
287             if (ext.isCritical())
288             {
289                 v.add(new DERBoolean(true));
290             }
291
292             v.add(ext.getValue());
293
294             vec.add(new DERSequence(v));
295         }
296
297         return new DERSequence(vec);
298     }
299
300     public int hashCode()
301     {
302         Enumeration JavaDoc e = extensions.keys();
303         int hashCode = 0;
304
305         while (e.hasMoreElements())
306         {
307             Object JavaDoc o = e.nextElement();
308
309             hashCode ^= o.hashCode();
310             hashCode ^= extensions.get(o).hashCode();
311         }
312
313         return hashCode;
314     }
315
316     public boolean equals(
317         Object JavaDoc o)
318     {
319         if (o == null || !(o instanceof X509Extensions))
320         {
321             return false;
322         }
323
324         X509Extensions other = (X509Extensions)o;
325
326         Enumeration JavaDoc e1 = extensions.keys();
327         Enumeration JavaDoc e2 = other.extensions.keys();
328
329         while (e1.hasMoreElements() && e2.hasMoreElements())
330         {
331             Object JavaDoc o1 = e1.nextElement();
332             Object JavaDoc o2 = e2.nextElement();
333
334             if (!o1.equals(o2))
335             {
336                 return false;
337             }
338         }
339
340         if (e1.hasMoreElements() || e2.hasMoreElements())
341         {
342             return false;
343         }
344
345         return true;
346     }
347 }
348
Popular Tags