KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > jce > provider > X509CRLEntryObject


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.geronimo.util.jce.provider;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.math.BigInteger JavaDoc;
24 import java.security.cert.CRLException JavaDoc;
25 import java.security.cert.X509CRLEntry JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import org.apache.geronimo.util.asn1.DERObjectIdentifier;
32 import org.apache.geronimo.util.asn1.DEROutputStream;
33 import org.apache.geronimo.util.asn1.x509.TBSCertList;
34 import org.apache.geronimo.util.asn1.x509.X509Extension;
35 import org.apache.geronimo.util.asn1.x509.X509Extensions;
36
37 /**
38  * The following extensions are listed in RFC 2459 as relevant to CRL Entries
39  *
40  * ReasonCode
41  * Hode Instruction Code
42  * Invalidity Date
43  * Certificate Issuer (critical)
44  */

45 public class X509CRLEntryObject extends X509CRLEntry JavaDoc
46 {
47     private TBSCertList.CRLEntry c;
48
49     public X509CRLEntryObject(
50         TBSCertList.CRLEntry c)
51     {
52         this.c = c;
53     }
54
55     /**
56      * Will return true if any extensions are present and marked
57      * as critical as we currently dont handle any extensions!
58      */

59     public boolean hasUnsupportedCriticalExtension()
60     {
61         Set JavaDoc extns = getCriticalExtensionOIDs();
62         if ( extns != null && !extns.isEmpty() )
63         {
64             return true;
65         }
66
67         return false;
68     }
69
70     private Set JavaDoc getExtensionOIDs(boolean critical)
71     {
72         X509Extensions extensions = c.getExtensions();
73
74         if ( extensions != null )
75         {
76             HashSet JavaDoc set = new HashSet JavaDoc();
77             Enumeration JavaDoc e = extensions.oids();
78
79             while (e.hasMoreElements())
80             {
81                 DERObjectIdentifier oid = (DERObjectIdentifier)e.nextElement();
82                 X509Extension ext = extensions.getExtension(oid);
83
84                 if (critical == ext.isCritical())
85                 {
86                     set.add(oid.getId());
87                 }
88             }
89
90             return set;
91         }
92
93         return null;
94     }
95
96     public Set JavaDoc getCriticalExtensionOIDs()
97     {
98         return getExtensionOIDs(true);
99     }
100
101     public Set JavaDoc getNonCriticalExtensionOIDs()
102     {
103         return getExtensionOIDs(false);
104     }
105
106     public byte[] getExtensionValue(String JavaDoc oid)
107     {
108         X509Extensions exts = c.getExtensions();
109
110         if (exts != null)
111         {
112             X509Extension ext = exts.getExtension(new DERObjectIdentifier(oid));
113
114             if (ext != null)
115             {
116                 try
117                 {
118                     return ext.getValue().getEncoded();
119                 }
120                 catch (Exception JavaDoc e)
121                 {
122                     throw new RuntimeException JavaDoc("error encoding " + e.toString());
123                 }
124             }
125         }
126
127         return null;
128     }
129
130     public byte[] getEncoded()
131         throws CRLException JavaDoc
132     {
133         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
134         DEROutputStream dOut = new DEROutputStream(bOut);
135
136         try
137         {
138             dOut.writeObject(c);
139
140             return bOut.toByteArray();
141         }
142         catch (IOException JavaDoc e)
143         {
144             throw new CRLException JavaDoc(e.toString());
145         }
146     }
147
148     public BigInteger JavaDoc getSerialNumber()
149     {
150         return c.getUserCertificate().getValue();
151     }
152
153     public Date JavaDoc getRevocationDate()
154     {
155         return c.getRevocationDate().getDate();
156     }
157
158     public boolean hasExtensions()
159     {
160         return c.getExtensions() != null;
161     }
162
163     public String JavaDoc toString()
164     {
165         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
166         String JavaDoc nl = System.getProperty("line.separator");
167
168         buf.append(" userCertificate: " + this.getSerialNumber() + nl);
169         buf.append(" revocationDate: " + this.getRevocationDate() + nl);
170
171
172         X509Extensions extensions = c.getExtensions();
173
174         if ( extensions != null )
175         {
176             Enumeration JavaDoc e = extensions.oids();
177             if ( e.hasMoreElements() )
178             {
179                 buf.append(" crlEntryExtensions:" + nl);
180
181                 while ( e.hasMoreElements() )
182                 {
183                     DERObjectIdentifier oid = (DERObjectIdentifier)e.nextElement();
184                     X509Extension ext = extensions.getExtension(oid);
185                     buf.append(ext);
186                 }
187             }
188         }
189
190         return buf.toString();
191     }
192 }
193
Popular Tags