001: package org.bouncycastle.asn1.tsp;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1EncodableVector;
005: import org.bouncycastle.asn1.ASN1Sequence;
006: import org.bouncycastle.asn1.ASN1TaggedObject;
007: import org.bouncycastle.asn1.DERBoolean;
008: import org.bouncycastle.asn1.DERInteger;
009: import org.bouncycastle.asn1.DERObject;
010: import org.bouncycastle.asn1.DERObjectIdentifier;
011: import org.bouncycastle.asn1.DERSequence;
012: import org.bouncycastle.asn1.DERTaggedObject;
013: import org.bouncycastle.asn1.x509.X509Extensions;
014:
015: public class TimeStampReq extends ASN1Encodable {
016: DERInteger version;
017:
018: MessageImprint messageImprint;
019:
020: DERObjectIdentifier tsaPolicy;
021:
022: DERInteger nonce;
023:
024: DERBoolean certReq;
025:
026: X509Extensions extensions;
027:
028: public static TimeStampReq getInstance(Object o) {
029: if (o == null || o instanceof TimeStampReq) {
030: return (TimeStampReq) o;
031: } else if (o instanceof ASN1Sequence) {
032: return new TimeStampReq((ASN1Sequence) o);
033: }
034:
035: throw new IllegalArgumentException(
036: "Unknown object in 'TimeStampReq' factory : "
037: + o.getClass().getName() + ".");
038: }
039:
040: public TimeStampReq(ASN1Sequence seq) {
041: int nbObjects = seq.size();
042:
043: int seqStart = 0;
044:
045: // version
046: version = DERInteger.getInstance(seq.getObjectAt(seqStart));
047:
048: seqStart++;
049:
050: // messageImprint
051: messageImprint = MessageImprint.getInstance(seq
052: .getObjectAt(seqStart));
053:
054: seqStart++;
055:
056: for (int opt = seqStart; opt < nbObjects; opt++) {
057: // tsaPolicy
058: if (seq.getObjectAt(opt) instanceof DERObjectIdentifier) {
059: tsaPolicy = DERObjectIdentifier.getInstance(seq
060: .getObjectAt(opt));
061: }
062: // nonce
063: else if (seq.getObjectAt(opt) instanceof DERInteger) {
064: nonce = DERInteger.getInstance(seq.getObjectAt(opt));
065: }
066: // certReq
067: else if (seq.getObjectAt(opt) instanceof DERBoolean) {
068: certReq = DERBoolean.getInstance(seq.getObjectAt(opt));
069: }
070: // extensions
071: else if (seq.getObjectAt(opt) instanceof ASN1TaggedObject) {
072: ASN1TaggedObject tagged = (ASN1TaggedObject) seq
073: .getObjectAt(opt);
074: if (tagged.getTagNo() == 0) {
075: extensions = X509Extensions.getInstance(tagged,
076: false);
077: }
078: }
079: }
080: }
081:
082: public TimeStampReq(MessageImprint messageImprint,
083: DERObjectIdentifier tsaPolicy, DERInteger nonce,
084: DERBoolean certReq, X509Extensions extensions) {
085: // default
086: version = new DERInteger(1);
087:
088: this .messageImprint = messageImprint;
089: this .tsaPolicy = tsaPolicy;
090: this .nonce = nonce;
091: this .certReq = certReq;
092: this .extensions = extensions;
093: }
094:
095: public DERInteger getVersion() {
096: return version;
097: }
098:
099: public MessageImprint getMessageImprint() {
100: return messageImprint;
101: }
102:
103: public DERObjectIdentifier getReqPolicy() {
104: return tsaPolicy;
105: }
106:
107: public DERInteger getNonce() {
108: return nonce;
109: }
110:
111: public DERBoolean getCertReq() {
112: return certReq;
113: }
114:
115: public X509Extensions getExtensions() {
116: return extensions;
117: }
118:
119: /**
120: * <pre>
121: * TimeStampReq ::= SEQUENCE {
122: * version INTEGER { v1(1) },
123: * messageImprint MessageImprint,
124: * --a hash algorithm OID and the hash value of the data to be
125: * --time-stamped
126: * reqPolicy TSAPolicyId OPTIONAL,
127: * nonce INTEGER OPTIONAL,
128: * certReq BOOLEAN DEFAULT FALSE,
129: * extensions [0] IMPLICIT Extensions OPTIONAL
130: * }
131: * </pre>
132: */
133: public DERObject toASN1Object() {
134: ASN1EncodableVector v = new ASN1EncodableVector();
135:
136: v.add(version);
137: v.add(messageImprint);
138:
139: if (tsaPolicy != null) {
140: v.add(tsaPolicy);
141: }
142:
143: if (nonce != null) {
144: v.add(nonce);
145: }
146:
147: if (certReq != null && certReq.isTrue()) {
148: v.add(certReq);
149: }
150:
151: if (extensions != null) {
152: v.add(new DERTaggedObject(false, 0, extensions));
153: }
154:
155: return new DERSequence(v);
156: }
157: }
|