001: package org.bouncycastle.asn1.tsp;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1EncodableVector;
005: import org.bouncycastle.asn1.ASN1InputStream;
006: import org.bouncycastle.asn1.ASN1OctetString;
007: import org.bouncycastle.asn1.ASN1Sequence;
008: import org.bouncycastle.asn1.ASN1TaggedObject;
009: import org.bouncycastle.asn1.DERBoolean;
010: import org.bouncycastle.asn1.DERGeneralizedTime;
011: import org.bouncycastle.asn1.DERInteger;
012: import org.bouncycastle.asn1.DERObject;
013: import org.bouncycastle.asn1.DERObjectIdentifier;
014: import org.bouncycastle.asn1.DERSequence;
015: import org.bouncycastle.asn1.DERTaggedObject;
016: import org.bouncycastle.asn1.x509.GeneralName;
017: import org.bouncycastle.asn1.x509.X509Extensions;
018:
019: import java.io.IOException;
020: import java.util.Enumeration;
021:
022: public class TSTInfo extends ASN1Encodable {
023: DERInteger version;
024:
025: DERObjectIdentifier tsaPolicyId;
026:
027: MessageImprint messageImprint;
028:
029: DERInteger serialNumber;
030:
031: DERGeneralizedTime genTime;
032:
033: Accuracy accuracy;
034:
035: DERBoolean ordering;
036:
037: DERInteger nonce;
038:
039: GeneralName tsa;
040:
041: X509Extensions extensions;
042:
043: public static TSTInfo getInstance(Object o) {
044: if (o == null || o instanceof TSTInfo) {
045: return (TSTInfo) o;
046: } else if (o instanceof ASN1Sequence) {
047: return new TSTInfo((ASN1Sequence) o);
048: } else if (o instanceof ASN1OctetString) {
049: try {
050: return getInstance(new ASN1InputStream(
051: ((ASN1OctetString) o).getOctets()).readObject());
052: } catch (IOException ioEx) {
053: throw new IllegalArgumentException(
054: "Bad object format in 'TSTInfo' factory.");
055: }
056: }
057:
058: throw new IllegalArgumentException(
059: "Unknown object in 'TSTInfo' factory : "
060: + o.getClass().getName() + ".");
061: }
062:
063: public TSTInfo(ASN1Sequence seq) {
064: Enumeration e = seq.getObjects();
065:
066: // version
067: version = DERInteger.getInstance(e.nextElement());
068:
069: // tsaPolicy
070: tsaPolicyId = DERObjectIdentifier.getInstance(e.nextElement());
071:
072: // messageImprint
073: messageImprint = MessageImprint.getInstance(e.nextElement());
074:
075: // serialNumber
076: serialNumber = DERInteger.getInstance(e.nextElement());
077:
078: // genTime
079: genTime = DERGeneralizedTime.getInstance(e.nextElement());
080:
081: // default for ordering
082: ordering = new DERBoolean(false);
083:
084: while (e.hasMoreElements()) {
085: DERObject o = (DERObject) e.nextElement();
086:
087: if (o instanceof ASN1TaggedObject) {
088: DERTaggedObject tagged = (DERTaggedObject) o;
089:
090: switch (tagged.getTagNo()) {
091: case 0:
092: tsa = GeneralName.getInstance(tagged, true);
093: break;
094: case 1:
095: extensions = X509Extensions.getInstance(tagged,
096: false);
097: break;
098: default:
099: throw new IllegalArgumentException(
100: "Unknown tag value " + tagged.getTagNo());
101: }
102: } else if (o instanceof DERSequence) {
103: accuracy = Accuracy.getInstance(o);
104: } else if (o instanceof DERBoolean) {
105: ordering = DERBoolean.getInstance(o);
106: } else if (o instanceof DERInteger) {
107: nonce = DERInteger.getInstance(o);
108: }
109:
110: }
111: }
112:
113: public TSTInfo(DERObjectIdentifier tsaPolicyId,
114: MessageImprint messageImprint, DERInteger serialNumber,
115: DERGeneralizedTime genTime, Accuracy accuracy,
116: DERBoolean ordering, DERInteger nonce, GeneralName tsa,
117: X509Extensions extensions) {
118: version = new DERInteger(1);
119: this .tsaPolicyId = tsaPolicyId;
120: this .messageImprint = messageImprint;
121: this .serialNumber = serialNumber;
122: this .genTime = genTime;
123:
124: this .accuracy = accuracy;
125: this .ordering = ordering;
126: this .nonce = nonce;
127: this .tsa = tsa;
128: this .extensions = extensions;
129: }
130:
131: public MessageImprint getMessageImprint() {
132: return messageImprint;
133: }
134:
135: public DERObjectIdentifier getPolicy() {
136: return tsaPolicyId;
137: }
138:
139: public DERInteger getSerialNumber() {
140: return serialNumber;
141: }
142:
143: public Accuracy getAccuracy() {
144: return accuracy;
145: }
146:
147: public DERGeneralizedTime getGenTime() {
148: return genTime;
149: }
150:
151: public DERBoolean getOrdering() {
152: return ordering;
153: }
154:
155: public DERInteger getNonce() {
156: return nonce;
157: }
158:
159: public GeneralName getTsa() {
160: return tsa;
161: }
162:
163: public X509Extensions getExtensions() {
164: return extensions;
165: }
166:
167: /**
168: * <pre>
169: *
170: * TSTInfo ::= SEQUENCE {
171: * version INTEGER { v1(1) },
172: * policy TSAPolicyId,
173: * messageImprint MessageImprint,
174: * -- MUST have the same value as the similar field in
175: * -- TimeStampReq
176: * serialNumber INTEGER,
177: * -- Time-Stamping users MUST be ready to accommodate integers
178: * -- up to 160 bits.
179: * genTime GeneralizedTime,
180: * accuracy Accuracy OPTIONAL,
181: * ordering BOOLEAN DEFAULT FALSE,
182: * nonce INTEGER OPTIONAL,
183: * -- MUST be present if the similar field was present
184: * -- in TimeStampReq. In that case it MUST have the same value.
185: * tsa [0] GeneralName OPTIONAL,
186: * extensions [1] IMPLICIT Extensions OPTIONAL }
187: *
188: * </pre>
189: */
190: public DERObject toASN1Object() {
191: ASN1EncodableVector seq = new ASN1EncodableVector();
192: seq.add(version);
193:
194: seq.add(tsaPolicyId);
195: seq.add(messageImprint);
196: seq.add(serialNumber);
197: seq.add(genTime);
198:
199: if (accuracy != null) {
200: seq.add(accuracy);
201: }
202:
203: if (ordering != null && ordering.isTrue()) {
204: seq.add(ordering);
205: }
206:
207: if (nonce != null) {
208: seq.add(nonce);
209: }
210:
211: if (tsa != null) {
212: seq.add(new DERTaggedObject(true, 0, tsa));
213: }
214:
215: if (extensions != null) {
216: seq.add(new DERTaggedObject(false, 1, extensions));
217: }
218:
219: return new DERSequence(seq);
220: }
221: }
|