1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.geronimo.util.asn1.x509;
20
21 import org.apache.geronimo.util.asn1.ASN1EncodableVector;
22 import org.apache.geronimo.util.asn1.DERInteger;
23 import org.apache.geronimo.util.asn1.DERSequence;
24 import org.apache.geronimo.util.asn1.DERTaggedObject;
25 import org.apache.geronimo.util.asn1.DERUTCTime;
26
27 /**
28 * Generator for Version 3 TBSCertificateStructures.
29 * <pre>
30 * TBSCertificate ::= SEQUENCE {
31 * version [ 0 ] Version DEFAULT v1(0),
32 * serialNumber CertificateSerialNumber,
33 * signature AlgorithmIdentifier,
34 * issuer Name,
35 * validity Validity,
36 * subject Name,
37 * subjectPublicKeyInfo SubjectPublicKeyInfo,
38 * issuerUniqueID [ 1 ] IMPLICIT UniqueIdentifier OPTIONAL,
39 * subjectUniqueID [ 2 ] IMPLICIT UniqueIdentifier OPTIONAL,
40 * extensions [ 3 ] Extensions OPTIONAL
41 * }
42 * </pre>
43 *
44 */
45 public class V3TBSCertificateGenerator
46 {
47 DERTaggedObject version = new DERTaggedObject(0, new DERInteger(2));
48
49 DERInteger serialNumber;
50 AlgorithmIdentifier signature;
51 X509Name issuer;
52 Time startDate, endDate;
53 X509Name subject;
54 SubjectPublicKeyInfo subjectPublicKeyInfo;
55 X509Extensions extensions;
56
57 public V3TBSCertificateGenerator()
58 {
59 }
60
61 public void setSerialNumber(
62 DERInteger serialNumber)
63 {
64 this.serialNumber = serialNumber;
65 }
66
67 public void setSignature(
68 AlgorithmIdentifier signature)
69 {
70 this.signature = signature;
71 }
72
73 public void setIssuer(
74 X509Name issuer)
75 {
76 this.issuer = issuer;
77 }
78
79 public void setStartDate(
80 DERUTCTime startDate)
81 {
82 this.startDate = new Time(startDate);
83 }
84
85 public void setStartDate(
86 Time startDate)
87 {
88 this.startDate = startDate;
89 }
90
91 public void setEndDate(
92 DERUTCTime endDate)
93 {
94 this.endDate = new Time(endDate);
95 }
96
97 public void setEndDate(
98 Time endDate)
99 {
100 this.endDate = endDate;
101 }
102
103 public void setSubject(
104 X509Name subject)
105 {
106 this.subject = subject;
107 }
108
109 public void setSubjectPublicKeyInfo(
110 SubjectPublicKeyInfo pubKeyInfo)
111 {
112 this.subjectPublicKeyInfo = pubKeyInfo;
113 }
114
115 public void setExtensions(
116 X509Extensions extensions)
117 {
118 this.extensions = extensions;
119 }
120
121 public TBSCertificateStructure generateTBSCertificate()
122 {
123 if ((serialNumber == null) || (signature == null)
124 || (issuer == null) || (startDate == null) || (endDate == null)
125 || (subject == null) || (subjectPublicKeyInfo == null))
126 {
127 throw new IllegalStateException("not all mandatory fields set in V3 TBScertificate generator");
128 }
129
130 ASN1EncodableVector v = new ASN1EncodableVector();
131
132 v.add(version);
133 v.add(serialNumber);
134 v.add(signature);
135 v.add(issuer);
136
137
138
139
140 ASN1EncodableVector validity = new ASN1EncodableVector();
141
142 validity.add(startDate);
143 validity.add(endDate);
144
145 v.add(new DERSequence(validity));
146
147 v.add(subject);
148
149 v.add(subjectPublicKeyInfo);
150
151 if (extensions != null)
152 {
153 v.add(new DERTaggedObject(3, extensions));
154 }
155
156 return new TBSCertificateStructure(new DERSequence(v));
157 }
158 }