001: package org.bouncycastle.asn1.x509;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1Sequence;
005: import org.bouncycastle.asn1.ASN1TaggedObject;
006: import org.bouncycastle.asn1.DERBitString;
007: import org.bouncycastle.asn1.DERInteger;
008: import org.bouncycastle.asn1.DERObject;
009: import org.bouncycastle.asn1.DERTaggedObject;
010: import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
011:
012: /**
013: * The TBSCertificate object.
014: * <pre>
015: * TBSCertificate ::= SEQUENCE {
016: * version [ 0 ] Version DEFAULT v1(0),
017: * serialNumber CertificateSerialNumber,
018: * signature AlgorithmIdentifier,
019: * issuer Name,
020: * validity Validity,
021: * subject Name,
022: * subjectPublicKeyInfo SubjectPublicKeyInfo,
023: * issuerUniqueID [ 1 ] IMPLICIT UniqueIdentifier OPTIONAL,
024: * subjectUniqueID [ 2 ] IMPLICIT UniqueIdentifier OPTIONAL,
025: * extensions [ 3 ] Extensions OPTIONAL
026: * }
027: * </pre>
028: * <p>
029: * Note: issuerUniqueID and subjectUniqueID are both deprecated by the IETF. This class
030: * will parse them, but you really shouldn't be creating new ones.
031: */
032: public class TBSCertificateStructure extends ASN1Encodable implements
033: X509ObjectIdentifiers, PKCSObjectIdentifiers {
034: ASN1Sequence seq;
035:
036: DERInteger version;
037: DERInteger serialNumber;
038: AlgorithmIdentifier signature;
039: X509Name issuer;
040: Time startDate, endDate;
041: X509Name subject;
042: SubjectPublicKeyInfo subjectPublicKeyInfo;
043: DERBitString issuerUniqueId;
044: DERBitString subjectUniqueId;
045: X509Extensions extensions;
046:
047: public static TBSCertificateStructure getInstance(
048: ASN1TaggedObject obj, boolean explicit) {
049: return getInstance(ASN1Sequence.getInstance(obj, explicit));
050: }
051:
052: public static TBSCertificateStructure getInstance(Object obj) {
053: if (obj instanceof TBSCertificateStructure) {
054: return (TBSCertificateStructure) obj;
055: } else if (obj instanceof ASN1Sequence) {
056: return new TBSCertificateStructure((ASN1Sequence) obj);
057: }
058:
059: throw new IllegalArgumentException("unknown object in factory");
060: }
061:
062: public TBSCertificateStructure(ASN1Sequence seq) {
063: int seqStart = 0;
064:
065: this .seq = seq;
066:
067: //
068: // some certficates don't include a version number - we assume v1
069: //
070: if (seq.getObjectAt(0) instanceof DERTaggedObject) {
071: version = DERInteger.getInstance(seq.getObjectAt(0));
072: } else {
073: seqStart = -1; // field 0 is missing!
074: version = new DERInteger(0);
075: }
076:
077: serialNumber = DERInteger.getInstance(seq
078: .getObjectAt(seqStart + 1));
079:
080: signature = AlgorithmIdentifier.getInstance(seq
081: .getObjectAt(seqStart + 2));
082: issuer = X509Name.getInstance(seq.getObjectAt(seqStart + 3));
083:
084: //
085: // before and after dates
086: //
087: ASN1Sequence dates = (ASN1Sequence) seq
088: .getObjectAt(seqStart + 4);
089:
090: startDate = Time.getInstance(dates.getObjectAt(0));
091: endDate = Time.getInstance(dates.getObjectAt(1));
092:
093: subject = X509Name.getInstance(seq.getObjectAt(seqStart + 5));
094:
095: //
096: // public key info.
097: //
098: subjectPublicKeyInfo = SubjectPublicKeyInfo.getInstance(seq
099: .getObjectAt(seqStart + 6));
100:
101: for (int extras = seq.size() - (seqStart + 6) - 1; extras > 0; extras--) {
102: DERTaggedObject extra = (DERTaggedObject) seq
103: .getObjectAt(seqStart + 6 + extras);
104:
105: switch (extra.getTagNo()) {
106: case 1:
107: issuerUniqueId = DERBitString.getInstance(extra, false);
108: break;
109: case 2:
110: subjectUniqueId = DERBitString
111: .getInstance(extra, false);
112: break;
113: case 3:
114: extensions = X509Extensions.getInstance(extra);
115: }
116: }
117: }
118:
119: public int getVersion() {
120: return version.getValue().intValue() + 1;
121: }
122:
123: public DERInteger getVersionNumber() {
124: return version;
125: }
126:
127: public DERInteger getSerialNumber() {
128: return serialNumber;
129: }
130:
131: public AlgorithmIdentifier getSignature() {
132: return signature;
133: }
134:
135: public X509Name getIssuer() {
136: return issuer;
137: }
138:
139: public Time getStartDate() {
140: return startDate;
141: }
142:
143: public Time getEndDate() {
144: return endDate;
145: }
146:
147: public X509Name getSubject() {
148: return subject;
149: }
150:
151: public SubjectPublicKeyInfo getSubjectPublicKeyInfo() {
152: return subjectPublicKeyInfo;
153: }
154:
155: public DERBitString getIssuerUniqueId() {
156: return issuerUniqueId;
157: }
158:
159: public DERBitString getSubjectUniqueId() {
160: return subjectUniqueId;
161: }
162:
163: public X509Extensions getExtensions() {
164: return extensions;
165: }
166:
167: public DERObject toASN1Object() {
168: return seq;
169: }
170: }
|