001: package org.bouncycastle.asn1.x509;
002:
003: import org.bouncycastle.asn1.ASN1EncodableVector;
004: import org.bouncycastle.asn1.DERInteger;
005: import org.bouncycastle.asn1.DERSequence;
006: import org.bouncycastle.asn1.DERTaggedObject;
007: import org.bouncycastle.asn1.DERUTCTime;
008:
009: /**
010: * Generator for Version 3 TBSCertificateStructures.
011: * <pre>
012: * TBSCertificate ::= SEQUENCE {
013: * version [ 0 ] Version DEFAULT v1(0),
014: * serialNumber CertificateSerialNumber,
015: * signature AlgorithmIdentifier,
016: * issuer Name,
017: * validity Validity,
018: * subject Name,
019: * subjectPublicKeyInfo SubjectPublicKeyInfo,
020: * issuerUniqueID [ 1 ] IMPLICIT UniqueIdentifier OPTIONAL,
021: * subjectUniqueID [ 2 ] IMPLICIT UniqueIdentifier OPTIONAL,
022: * extensions [ 3 ] Extensions OPTIONAL
023: * }
024: * </pre>
025: *
026: */
027: public class V3TBSCertificateGenerator {
028: DERTaggedObject version = new DERTaggedObject(0, new DERInteger(2));
029:
030: DERInteger serialNumber;
031: AlgorithmIdentifier signature;
032: X509Name issuer;
033: Time startDate, endDate;
034: X509Name subject;
035: SubjectPublicKeyInfo subjectPublicKeyInfo;
036: X509Extensions extensions;
037:
038: private boolean altNamePresentAndCritical;
039:
040: public V3TBSCertificateGenerator() {
041: }
042:
043: public void setSerialNumber(DERInteger serialNumber) {
044: this .serialNumber = serialNumber;
045: }
046:
047: public void setSignature(AlgorithmIdentifier signature) {
048: this .signature = signature;
049: }
050:
051: public void setIssuer(X509Name issuer) {
052: this .issuer = issuer;
053: }
054:
055: public void setStartDate(DERUTCTime startDate) {
056: this .startDate = new Time(startDate);
057: }
058:
059: public void setStartDate(Time startDate) {
060: this .startDate = startDate;
061: }
062:
063: public void setEndDate(DERUTCTime endDate) {
064: this .endDate = new Time(endDate);
065: }
066:
067: public void setEndDate(Time endDate) {
068: this .endDate = endDate;
069: }
070:
071: public void setSubject(X509Name subject) {
072: this .subject = subject;
073: }
074:
075: public void setSubjectPublicKeyInfo(SubjectPublicKeyInfo pubKeyInfo) {
076: this .subjectPublicKeyInfo = pubKeyInfo;
077: }
078:
079: public void setExtensions(X509Extensions extensions) {
080: this .extensions = extensions;
081: if (extensions != null) {
082: X509Extension altName = extensions
083: .getExtension(X509Extensions.SubjectAlternativeName);
084:
085: if (altName != null && altName.isCritical()) {
086: altNamePresentAndCritical = true;
087: }
088: }
089: }
090:
091: public TBSCertificateStructure generateTBSCertificate() {
092: if ((serialNumber == null) || (signature == null)
093: || (issuer == null) || (startDate == null)
094: || (endDate == null)
095: || (subject == null && !altNamePresentAndCritical)
096: || (subjectPublicKeyInfo == null)) {
097: throw new IllegalStateException(
098: "not all mandatory fields set in V3 TBScertificate generator");
099: }
100:
101: ASN1EncodableVector v = new ASN1EncodableVector();
102:
103: v.add(version);
104: v.add(serialNumber);
105: v.add(signature);
106: v.add(issuer);
107:
108: //
109: // before and after dates
110: //
111: ASN1EncodableVector validity = new ASN1EncodableVector();
112:
113: validity.add(startDate);
114: validity.add(endDate);
115:
116: v.add(new DERSequence(validity));
117:
118: if (subject != null) {
119: v.add(subject);
120: } else {
121: v.add(new DERSequence());
122: }
123:
124: v.add(subjectPublicKeyInfo);
125:
126: if (extensions != null) {
127: v.add(new DERTaggedObject(3, extensions));
128: }
129:
130: return new TBSCertificateStructure(new DERSequence(v));
131: }
132: }
|