KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > x509 > V2TBSCertListGenerator


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 package org.apache.geronimo.util.asn1.x509;
19
20 import java.io.IOException JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 import org.apache.geronimo.util.asn1.ASN1EncodableVector;
25 import org.apache.geronimo.util.asn1.ASN1Sequence;
26 import org.apache.geronimo.util.asn1.DERGeneralizedTime;
27 import org.apache.geronimo.util.asn1.DERInteger;
28 import org.apache.geronimo.util.asn1.DEROctetString;
29 import org.apache.geronimo.util.asn1.DERSequence;
30 import org.apache.geronimo.util.asn1.DERTaggedObject;
31 import org.apache.geronimo.util.asn1.DERUTCTime;
32
33 /**
34  * Generator for Version 2 TBSCertList structures.
35  * <pre>
36  * TBSCertList ::= SEQUENCE {
37  * version Version OPTIONAL,
38  * -- if present, shall be v2
39  * signature AlgorithmIdentifier,
40  * issuer Name,
41  * thisUpdate Time,
42  * nextUpdate Time OPTIONAL,
43  * revokedCertificates SEQUENCE OF SEQUENCE {
44  * userCertificate CertificateSerialNumber,
45  * revocationDate Time,
46  * crlEntryExtensions Extensions OPTIONAL
47  * -- if present, shall be v2
48  * } OPTIONAL,
49  * crlExtensions [0] EXPLICIT Extensions OPTIONAL
50  * -- if present, shall be v2
51  * }
52  * </pre>
53  *
54  * <b>Note: This class may be subject to change</b>
55  */

56 public class V2TBSCertListGenerator
57 {
58     DERInteger version = new DERInteger(1);
59
60     AlgorithmIdentifier signature;
61     X509Name issuer;
62     Time thisUpdate, nextUpdate=null;
63     X509Extensions extensions=null;
64     private Vector JavaDoc crlentries=null;
65
66     public V2TBSCertListGenerator()
67     {
68     }
69
70
71     public void setSignature(
72         AlgorithmIdentifier signature)
73     {
74         this.signature = signature;
75     }
76
77     public void setIssuer(
78         X509Name issuer)
79     {
80         this.issuer = issuer;
81     }
82
83     public void setThisUpdate(
84         DERUTCTime thisUpdate)
85     {
86         this.thisUpdate = new Time(thisUpdate);
87     }
88
89     public void setNextUpdate(
90         DERUTCTime nextUpdate)
91     {
92         this.nextUpdate = new Time(nextUpdate);
93     }
94
95     public void setThisUpdate(
96         Time thisUpdate)
97     {
98         this.thisUpdate = thisUpdate;
99     }
100
101     public void setNextUpdate(
102         Time nextUpdate)
103     {
104         this.nextUpdate = nextUpdate;
105     }
106
107     public void addCRLEntry(
108         ASN1Sequence crlEntry)
109     {
110         if (crlentries == null)
111             crlentries = new Vector JavaDoc();
112         crlentries.addElement(crlEntry);
113     }
114
115     public void addCRLEntry(DERInteger userCertificate, DERUTCTime revocationDate, int reason)
116     {
117         addCRLEntry(userCertificate, new Time(revocationDate), reason);
118     }
119
120     public void addCRLEntry(DERInteger userCertificate, Time revocationDate, int reason)
121     {
122         addCRLEntry(userCertificate, revocationDate, reason, null);
123     }
124
125     public void addCRLEntry(DERInteger userCertificate, Time revocationDate, int reason, DERGeneralizedTime invalidityDate)
126     {
127         ASN1EncodableVector v = new ASN1EncodableVector();
128
129         v.add(userCertificate);
130         v.add(revocationDate);
131
132         Vector JavaDoc extOids = new Vector JavaDoc();
133         Vector JavaDoc extValues = new Vector JavaDoc();
134
135         if (reason != 0)
136         {
137             CRLReason crlReason = new CRLReason(reason);
138
139             try
140             {
141                 extOids.addElement(X509Extensions.ReasonCode);
142                 extValues.addElement(new X509Extension(false, new DEROctetString(crlReason.getEncoded())));
143             }
144             catch (IOException JavaDoc e)
145             {
146                 throw new IllegalArgumentException JavaDoc("error encoding reason: " + e);
147             }
148         }
149
150         if (invalidityDate != null)
151         {
152             try
153             {
154                 extOids.addElement(X509Extensions.InvalidityDate);
155                 extValues.addElement(new X509Extension(false, new DEROctetString(invalidityDate.getEncoded())));
156             }
157             catch (IOException JavaDoc e)
158             {
159                 throw new IllegalArgumentException JavaDoc("error encoding invalidityDate: " + e);
160             }
161         }
162
163         if (extOids.size() != 0)
164         {
165             X509Extensions ex = new X509Extensions(extOids, extValues);
166             v.add(ex);
167         }
168
169         if (crlentries == null)
170         {
171             crlentries = new Vector JavaDoc();
172         }
173
174         crlentries.addElement(new DERSequence(v));
175     }
176
177     public void setExtensions(
178         X509Extensions extensions)
179     {
180         this.extensions = extensions;
181     }
182
183     public TBSCertList generateTBSCertList()
184     {
185         if ((signature == null) || (issuer == null) || (thisUpdate == null))
186         {
187             throw new IllegalStateException JavaDoc("Not all mandatory fields set in V2 TBSCertList generator.");
188         }
189
190         ASN1EncodableVector v = new ASN1EncodableVector();
191
192         v.add(version);
193         v.add(signature);
194         v.add(issuer);
195
196         v.add(thisUpdate);
197         if (nextUpdate != null)
198         {
199             v.add(nextUpdate);
200         }
201
202         // Add CRLEntries if they exist
203
if (crlentries != null)
204         {
205             ASN1EncodableVector certs = new ASN1EncodableVector();
206             Enumeration JavaDoc it = crlentries.elements();
207             while( it.hasMoreElements() )
208             {
209                 certs.add((ASN1Sequence)it.nextElement());
210             }
211             v.add(new DERSequence(certs));
212         }
213
214         if (extensions != null)
215         {
216             v.add(new DERTaggedObject(0, extensions));
217         }
218
219         return new TBSCertList(new DERSequence(v));
220     }
221 }
222
Popular Tags