Skip to content

Commit ae332c0

Browse files
committed
Add direct TBSCertificate constructor
1 parent a1ee07b commit ae332c0

File tree

3 files changed

+64
-35
lines changed

3 files changed

+64
-35
lines changed

core/src/main/java/org/bouncycastle/asn1/x509/TBSCertificate.java

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,49 @@ else if (!version.hasValue(2))
146146
}
147147
}
148148

149+
public TBSCertificate(ASN1Integer version, ASN1Integer serialNumber, AlgorithmIdentifier signature,
150+
X500Name issuer, Validity validity, X500Name subject, SubjectPublicKeyInfo subjectPublicKeyInfo,
151+
ASN1BitString issuerUniqueId, ASN1BitString subjectUniqueId, Extensions extensions)
152+
{
153+
if (serialNumber == null)
154+
{
155+
throw new NullPointerException("'serialNumber' cannot be null");
156+
}
157+
if (signature == null)
158+
{
159+
throw new NullPointerException("'signature' cannot be null");
160+
}
161+
if (issuer == null)
162+
{
163+
throw new NullPointerException("'issuer' cannot be null");
164+
}
165+
if (validity == null)
166+
{
167+
throw new NullPointerException("'validity' cannot be null");
168+
}
169+
if (subject == null)
170+
{
171+
throw new NullPointerException("'subject' cannot be null");
172+
}
173+
if (subjectPublicKeyInfo == null)
174+
{
175+
throw new NullPointerException("'subjectPublicKeyInfo' cannot be null");
176+
}
177+
178+
this.version = version != null ? version : new ASN1Integer(0);
179+
this.serialNumber = serialNumber;
180+
this.signature = signature;
181+
this.issuer = issuer;
182+
this.validity = validity;
183+
this.subject = subject;
184+
this.subjectPublicKeyInfo = subjectPublicKeyInfo;
185+
this.issuerUniqueId = issuerUniqueId;
186+
this.subjectUniqueId = subjectUniqueId;
187+
this.extensions = extensions;
188+
189+
this.seq = null;
190+
}
191+
149192
public int getVersionNumber()
150193
{
151194
return version.intValueExact() + 1;
@@ -213,17 +256,20 @@ public Extensions getExtensions()
213256

214257
public ASN1Primitive toASN1Primitive()
215258
{
216-
if (Properties.getPropertyValue("org.bouncycastle.x509.allow_non-der_tbscert") != null)
259+
if (seq != null)
217260
{
218-
if (Properties.isOverrideSet("org.bouncycastle.x509.allow_non-der_tbscert"))
261+
if (Properties.getPropertyValue("org.bouncycastle.x509.allow_non-der_tbscert") != null)
262+
{
263+
if (Properties.isOverrideSet("org.bouncycastle.x509.allow_non-der_tbscert"))
264+
{
265+
return seq;
266+
}
267+
}
268+
else
219269
{
220270
return seq;
221271
}
222272
}
223-
else
224-
{
225-
return seq;
226-
}
227273

228274
ASN1EncodableVector v = new ASN1EncodableVector(10);
229275

core/src/main/java/org/bouncycastle/asn1/x509/V1TBSCertificateGenerator.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package org.bouncycastle.asn1.x509;
22

3-
import org.bouncycastle.asn1.ASN1EncodableVector;
43
import org.bouncycastle.asn1.ASN1Integer;
54
import org.bouncycastle.asn1.ASN1UTCTime;
6-
import org.bouncycastle.asn1.DERSequence;
75
import org.bouncycastle.asn1.DERTaggedObject;
86
import org.bouncycastle.asn1.x500.X500Name;
97

@@ -124,16 +122,8 @@ public TBSCertificate generateTBSCertificate()
124122
throw new IllegalStateException("not all mandatory fields set in V1 TBScertificate generator");
125123
}
126124

127-
ASN1EncodableVector seq = new ASN1EncodableVector(6);
128-
129-
// seq.add(version); - not required as default value.
130-
seq.add(serialNumber);
131-
seq.add(signature);
132-
seq.add(issuer);
133-
seq.add(validity != null ? validity : new Validity(startDate, endDate));
134-
seq.add(subject);
135-
seq.add(subjectPublicKeyInfo);
136-
137-
return TBSCertificate.getInstance(new DERSequence(seq));
125+
return new TBSCertificate(new ASN1Integer(0), serialNumber, signature, issuer,
126+
validity != null ? validity : new Validity(startDate, endDate), subject, subjectPublicKeyInfo, null,
127+
null, null);
138128
}
139129
}

core/src/main/java/org/bouncycastle/asn1/x509/V3TBSCertificateGenerator.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
*/
3030
public class V3TBSCertificateGenerator
3131
{
32-
DERTaggedObject version = new DERTaggedObject(true, 0, new ASN1Integer(2));
32+
private static final DERTaggedObject VERSION = new DERTaggedObject(true, 0, new ASN1Integer(2));
3333

34-
ASN1Integer serialNumber;
34+
ASN1Integer serialNumber;
3535
AlgorithmIdentifier signature;
3636
X500Name issuer;
3737
Validity validity;
@@ -175,21 +175,11 @@ public ASN1Sequence generatePreTBSCertificate()
175175
throw new IllegalStateException("not all mandatory fields set in V3 TBScertificate generator");
176176
}
177177

178-
return generateTBSStructure();
179-
}
180-
181-
private ASN1Sequence generateTBSStructure()
182-
{
183-
ASN1EncodableVector v = new ASN1EncodableVector(10);
178+
ASN1EncodableVector v = new ASN1EncodableVector(9);
184179

185-
v.add(version);
180+
v.add(VERSION);
186181
v.add(serialNumber);
187-
188-
if (signature != null)
189-
{
190-
v.add(signature);
191-
}
192-
182+
// No signature
193183
v.add(issuer);
194184
v.add(validity != null ? validity : new Validity(startDate, endDate));
195185
v.add(subject != null ? subject : X500Name.getInstance(new DERSequence()));
@@ -222,6 +212,9 @@ public TBSCertificate generateTBSCertificate()
222212
throw new IllegalStateException("not all mandatory fields set in V3 TBScertificate generator");
223213
}
224214

225-
return TBSCertificate.getInstance(generateTBSStructure());
215+
return new TBSCertificate(new ASN1Integer(2), serialNumber, signature, issuer,
216+
validity != null ? validity : new Validity(startDate, endDate),
217+
subject != null ? subject : X500Name.getInstance(new DERSequence()), subjectPublicKeyInfo,
218+
issuerUniqueID, subjectUniqueID, extensions);
226219
}
227220
}

0 commit comments

Comments
 (0)