1    package org.bouncycastle.asn1.x509;
2    
3    import org.bouncycastle.asn1.*;
4    import org.bouncycastle.asn1.pkcs.*;
5    
6    /**
7     * Generator for Version 3 TBSCertificateStructures.
8     * <pre>
9     * TBSCertificate ::= SEQUENCE {
10    *      version          [ 0 ]  Version DEFAULT v1(0),
11    *      serialNumber            CertificateSerialNumber,
12    *      signature               AlgorithmIdentifier,
13    *      issuer                  Name,
14    *      validity                Validity,
15    *      subject                 Name,
16    *      subjectPublicKeyInfo    SubjectPublicKeyInfo,
17    *      issuerUniqueID    [ 1 ] IMPLICIT UniqueIdentifier OPTIONAL,
18    *      subjectUniqueID   [ 2 ] IMPLICIT UniqueIdentifier OPTIONAL,
19    *      extensions        [ 3 ] Extensions OPTIONAL
20    *      }
21    * </pre>
22    *
23    */
24   public class V3TBSCertificateGenerator
25   {
26       DERTaggedObject         version = new DERTaggedObject(0, new DERInteger(2));
27   
28       DERInteger              serialNumber;
29       AlgorithmIdentifier     signature;
30       X509Name                issuer;
31       TimeTime                startDate, endDate;
32       X509Name                subject;
33       SubjectPublicKeyInfo    subjectPublicKeyInfo;
34       X509Extensions          extensions;
35   
36       public V3TBSCertificateGenerator()
37       {
38       }
39   
40       public void setSerialNumber(
41           DERInteger  serialNumber)
42       {
43           this.serialNumber = serialNumber;
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 setStartDate(
59           DERUTCTime startDate)
60       {
61           this.startDate = new Time(startDate);
62       }
63   
64       public void setStartDate(
65           Time startDate)
66       {
67           this.startDate = startDate;
68       }
69   
70       public void setEndDate(
71           DERUTCTime endDate)
72       {
73           this.endDate = new Time(endDate);
74       }
75   
76       public void setEndDate(
77           Time endDate)
78       {
79           this.endDate = endDate;
80       }
81   
82       public void setSubject(
83           X509Name    subject)
84       {
85           this.subject = subject;
86       }
87   
88       public void setSubjectPublicKeyInfo(
89           SubjectPublicKeyInfo    pubKeyInfo)
90       {
91           this.subjectPublicKeyInfo = pubKeyInfo;
92       }
93   
94       public void setExtensions(
95           X509Extensions    extensions)
96       {
97           this.extensions = extensions;
98       }
99   
100      public TBSCertificateStructure generateTBSCertificate()
101      {
102          if ((serialNumber == null) || (signature == null)
103              || (issuer == null) || (startDate == null) || (endDate == null)
104              || (subject == null) || (subjectPublicKeyInfo == null))
105          {
106              throw new IllegalStateException("not all mandatory fields set in V3 TBScertificate generator");
107          }
108  
109          DERConstructedSequence  seq = new DERConstructedSequence();
110  
111          seq.addObject(version);
112          seq.addObject(serialNumber);
113          seq.addObject(signature);
114          seq.addObject(issuer);
115  
116          //
117          // before and after dates
118          //
119          DERConstructedSequence  validity = new DERConstructedSequence();
120  
121          validity.addObject(startDate);
122          validity.addObject(endDate);
123  
124          seq.addObject(validity);
125  
126          seq.addObject(subject);
127  
128          seq.addObject(subjectPublicKeyInfo);
129  
130          if (extensions != null)
131          {
132              seq.addObject(new DERTaggedObject(3, extensions));
133          }
134  
135          return new TBSCertificateStructure(seq);
136      }
137  }
138