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