2002/03/21 01:19:32
[org.ibex.core.git] / src / org / bouncycastle / asn1 / x509 / V2TBSCertListGenerator.java
1 package org.bouncycastle.asn1.x509;
2
3 import java.util.Vector;
4 import java.util.Enumeration;
5
6 import org.bouncycastle.asn1.*;
7
8 /**
9  * Generator for Version 2 TBSCertList structures.
10  * <pre>
11  *  TBSCertList  ::=  SEQUENCE  {
12  *       version                 Version OPTIONAL,
13  *                                    -- if present, shall be v2
14  *       signature               AlgorithmIdentifier,
15  *       issuer                  Name,
16  *       thisUpdate              Time,
17  *       nextUpdate              Time OPTIONAL,
18  *       revokedCertificates     SEQUENCE OF SEQUENCE  {
19  *            userCertificate         CertificateSerialNumber,
20  *            revocationDate          Time,
21  *            crlEntryExtensions      Extensions OPTIONAL
22  *                                          -- if present, shall be v2
23  *                                 }  OPTIONAL,
24  *       crlExtensions           [0]  EXPLICIT Extensions OPTIONAL
25  *                                          -- if present, shall be v2
26  *                                 }
27  * </pre>
28  *
29  * <b>Note: This class may be subject to change</b>
30  */
31 public class V2TBSCertListGenerator
32 {
33     DERInteger version = new DERInteger(1);
34
35     AlgorithmIdentifier     signature;
36     X509Name                issuer;
37     DERUTCTime              thisUpdate, nextUpdate=null;
38     X509Extensions          extensions=null;
39     private Vector          crlentries=null;
40
41     public V2TBSCertListGenerator()
42     {
43     }
44
45
46     public void setSignature(
47         AlgorithmIdentifier    signature)
48     {
49         this.signature = signature;
50     }
51
52     public void setIssuer(
53         X509Name    issuer)
54     {
55         this.issuer = issuer;
56     }
57
58     public void setThisUpdate(
59         DERUTCTime thisUpdate)
60     {
61         this.thisUpdate = thisUpdate;
62     }
63
64     public void setNextUpdate(
65         DERUTCTime nextUpdate)
66     {
67         this.nextUpdate = nextUpdate;
68     }
69
70
71     public void addCRLEntry(
72         DERConstructedSequence crlEntry)
73     {
74         if (crlentries == null)
75             crlentries = new Vector();
76         crlentries.addElement(crlEntry);
77     }
78
79     public void addCRLEntry(DERInteger userCertificate, DERUTCTime revocationDate, int reason)
80     {
81         DERConstructedSequence seq = new DERConstructedSequence();
82         seq.addObject(userCertificate);
83         seq.addObject(revocationDate);
84         if (reason != 0)
85         {
86             ReasonFlags rf = new ReasonFlags(reason);
87             DERConstructedSequence eseq = new DERConstructedSequence();
88             eseq.addObject(X509Extensions.ReasonCode);
89             eseq.addObject(rf);
90             X509Extensions ex = new X509Extensions(eseq);
91             seq.addObject(ex);
92         }
93         if (crlentries == null)
94             crlentries = new Vector();
95         crlentries.addElement(seq);
96     }
97
98     public void setExtensions(
99         X509Extensions    extensions)
100     {
101         this.extensions = extensions;
102     }
103
104     public TBSCertList generateTBSCertList()
105     {
106         if ((signature == null) || (issuer == null) || (thisUpdate == null))
107         {
108             throw new IllegalStateException("Not all mandatory fields set in V2 TBSCertList generator.");
109         }
110
111         DERConstructedSequence  seq = new DERConstructedSequence();
112
113         seq.addObject(version);
114         seq.addObject(signature);
115         seq.addObject(issuer);
116
117         seq.addObject(thisUpdate);
118         if (nextUpdate != null)
119             seq.addObject(nextUpdate);
120
121         // Add CRLEntries if they exist
122         if (crlentries != null) {
123             DERConstructedSequence certseq = new DERConstructedSequence();
124             Enumeration it = crlentries.elements();
125             while( it.hasMoreElements() ) {
126                 certseq.addObject((DERConstructedSequence)it.nextElement());
127             }
128             seq.addObject(certseq);
129         }
130
131         if (extensions != null)
132         {
133             seq.addObject(new DERTaggedObject(0, extensions.getDERObject()));
134         }
135
136         return new TBSCertList(seq);
137     }
138 }