001: package org.bouncycastle.asn1.x509;
002:
003: import java.io.IOException;
004: import java.util.Enumeration;
005: import java.util.Vector;
006:
007: import org.bouncycastle.asn1.ASN1EncodableVector;
008: import org.bouncycastle.asn1.ASN1Sequence;
009: import org.bouncycastle.asn1.DERGeneralizedTime;
010: import org.bouncycastle.asn1.DERInteger;
011: import org.bouncycastle.asn1.DEROctetString;
012: import org.bouncycastle.asn1.DERSequence;
013: import org.bouncycastle.asn1.DERTaggedObject;
014: import org.bouncycastle.asn1.DERUTCTime;
015:
016: /**
017: * Generator for Version 2 TBSCertList structures.
018: * <pre>
019: * TBSCertList ::= SEQUENCE {
020: * version Version OPTIONAL,
021: * -- if present, shall be v2
022: * signature AlgorithmIdentifier,
023: * issuer Name,
024: * thisUpdate Time,
025: * nextUpdate Time OPTIONAL,
026: * revokedCertificates SEQUENCE OF SEQUENCE {
027: * userCertificate CertificateSerialNumber,
028: * revocationDate Time,
029: * crlEntryExtensions Extensions OPTIONAL
030: * -- if present, shall be v2
031: * } OPTIONAL,
032: * crlExtensions [0] EXPLICIT Extensions OPTIONAL
033: * -- if present, shall be v2
034: * }
035: * </pre>
036: *
037: * <b>Note: This class may be subject to change</b>
038: */
039: public class V2TBSCertListGenerator {
040: DERInteger version = new DERInteger(1);
041:
042: AlgorithmIdentifier signature;
043: X509Name issuer;
044: Time this Update, nextUpdate = null;
045: X509Extensions extensions = null;
046: private Vector crlentries = null;
047:
048: public V2TBSCertListGenerator() {
049: }
050:
051: public void setSignature(AlgorithmIdentifier signature) {
052: this .signature = signature;
053: }
054:
055: public void setIssuer(X509Name issuer) {
056: this .issuer = issuer;
057: }
058:
059: public void setThisUpdate(DERUTCTime this Update) {
060: this .this Update = new Time(this Update);
061: }
062:
063: public void setNextUpdate(DERUTCTime nextUpdate) {
064: this .nextUpdate = new Time(nextUpdate);
065: }
066:
067: public void setThisUpdate(Time this Update) {
068: this .this Update = this Update;
069: }
070:
071: public void setNextUpdate(Time nextUpdate) {
072: this .nextUpdate = nextUpdate;
073: }
074:
075: public void addCRLEntry(ASN1Sequence crlEntry) {
076: if (crlentries == null) {
077: crlentries = new Vector();
078: }
079:
080: crlentries.addElement(crlEntry);
081: }
082:
083: public void addCRLEntry(DERInteger userCertificate,
084: DERUTCTime revocationDate, int reason) {
085: addCRLEntry(userCertificate, new Time(revocationDate), reason);
086: }
087:
088: public void addCRLEntry(DERInteger userCertificate,
089: Time revocationDate, int reason) {
090: addCRLEntry(userCertificate, revocationDate, reason, null);
091: }
092:
093: public void addCRLEntry(DERInteger userCertificate,
094: Time revocationDate, int reason,
095: DERGeneralizedTime invalidityDate) {
096: Vector extOids = new Vector();
097: Vector extValues = new Vector();
098:
099: if (reason != 0) {
100: CRLReason crlReason = new CRLReason(reason);
101:
102: try {
103: extOids.addElement(X509Extensions.ReasonCode);
104: extValues.addElement(new X509Extension(false,
105: new DEROctetString(crlReason.getEncoded())));
106: } catch (IOException e) {
107: throw new IllegalArgumentException(
108: "error encoding reason: " + e);
109: }
110: }
111:
112: if (invalidityDate != null) {
113: try {
114: extOids.addElement(X509Extensions.InvalidityDate);
115: extValues
116: .addElement(new X509Extension(false,
117: new DEROctetString(invalidityDate
118: .getEncoded())));
119: } catch (IOException e) {
120: throw new IllegalArgumentException(
121: "error encoding invalidityDate: " + e);
122: }
123: }
124:
125: if (extOids.size() != 0) {
126: addCRLEntry(userCertificate, revocationDate,
127: new X509Extensions(extOids, extValues));
128: } else {
129: addCRLEntry(userCertificate, revocationDate, null);
130: }
131: }
132:
133: public void addCRLEntry(DERInteger userCertificate,
134: Time revocationDate, X509Extensions extensions) {
135: ASN1EncodableVector v = new ASN1EncodableVector();
136:
137: v.add(userCertificate);
138: v.add(revocationDate);
139:
140: if (extensions != null) {
141: v.add(extensions);
142: }
143:
144: addCRLEntry(new DERSequence(v));
145: }
146:
147: public void setExtensions(X509Extensions extensions) {
148: this .extensions = extensions;
149: }
150:
151: public TBSCertList generateTBSCertList() {
152: if ((signature == null) || (issuer == null)
153: || (this Update == null)) {
154: throw new IllegalStateException(
155: "Not all mandatory fields set in V2 TBSCertList generator.");
156: }
157:
158: ASN1EncodableVector v = new ASN1EncodableVector();
159:
160: v.add(version);
161: v.add(signature);
162: v.add(issuer);
163:
164: v.add(this Update);
165: if (nextUpdate != null) {
166: v.add(nextUpdate);
167: }
168:
169: // Add CRLEntries if they exist
170: if (crlentries != null) {
171: ASN1EncodableVector certs = new ASN1EncodableVector();
172: Enumeration it = crlentries.elements();
173: while (it.hasMoreElements()) {
174: certs.add((ASN1Sequence) it.nextElement());
175: }
176: v.add(new DERSequence(certs));
177: }
178:
179: if (extensions != null) {
180: v.add(new DERTaggedObject(0, extensions));
181: }
182:
183: return new TBSCertList(new DERSequence(v));
184: }
185: }
|