1 package org.bouncycastle.asn1.ocsp;
2 
3 import org.bouncycastle.asn1.ASN1EncodableVector;
4 import org.bouncycastle.asn1.ASN1Integer;
5 import org.bouncycastle.asn1.ASN1Object;
6 import org.bouncycastle.asn1.ASN1Primitive;
7 import org.bouncycastle.asn1.ASN1Sequence;
8 import org.bouncycastle.asn1.ASN1TaggedObject;
9 import org.bouncycastle.asn1.DERSequence;
10 import org.bouncycastle.asn1.DERTaggedObject;
11 import org.bouncycastle.asn1.x509.Extensions;
12 import org.bouncycastle.asn1.x509.GeneralName;
13 import org.bouncycastle.asn1.x509.X509Extensions;
14 
15 public class TBSRequest
16     extends ASN1Object
17 {
18     private static final ASN1Integer V1 = new ASN1Integer(0);
19 
20     ASN1Integer      version;
21     GeneralName     requestorName;
22     ASN1Sequence    requestList;
23     Extensions  requestExtensions;
24 
25     boolean         versionSet;
26 
27     /**
28      * @deprecated use method taking Extensions
29      * @param requestorName
30      * @param requestList
31      * @param requestExtensions
32      */
TBSRequest( GeneralName requestorName, ASN1Sequence requestList, X509Extensions requestExtensions)33     public TBSRequest(
34         GeneralName     requestorName,
35         ASN1Sequence    requestList,
36         X509Extensions requestExtensions)
37     {
38         this.version = V1;
39         this.requestorName = requestorName;
40         this.requestList = requestList;
41         this.requestExtensions = Extensions.getInstance(requestExtensions);
42     }
43 
TBSRequest( GeneralName requestorName, ASN1Sequence requestList, Extensions requestExtensions)44     public TBSRequest(
45         GeneralName     requestorName,
46         ASN1Sequence    requestList,
47         Extensions  requestExtensions)
48     {
49         this.version = V1;
50         this.requestorName = requestorName;
51         this.requestList = requestList;
52         this.requestExtensions = requestExtensions;
53     }
54 
TBSRequest( ASN1Sequence seq)55     private TBSRequest(
56         ASN1Sequence    seq)
57     {
58         int    index = 0;
59 
60         if (seq.getObjectAt(0) instanceof ASN1TaggedObject)
61         {
62             ASN1TaggedObject    o = (ASN1TaggedObject)seq.getObjectAt(0);
63 
64             if (o.getTagNo() == 0)
65             {
66                 versionSet = true;
67                 version = ASN1Integer.getInstance((ASN1TaggedObject)seq.getObjectAt(0), true);
68                 index++;
69             }
70             else
71             {
72                 version = V1;
73             }
74         }
75         else
76         {
77             version = V1;
78         }
79 
80         if (seq.getObjectAt(index) instanceof ASN1TaggedObject)
81         {
82             requestorName = GeneralName.getInstance((ASN1TaggedObject)seq.getObjectAt(index++), true);
83         }
84 
85         requestList = (ASN1Sequence)seq.getObjectAt(index++);
86 
87         if (seq.size() == (index + 1))
88         {
89             requestExtensions = Extensions.getInstance((ASN1TaggedObject)seq.getObjectAt(index), true);
90         }
91     }
92 
getInstance( ASN1TaggedObject obj, boolean explicit)93     public static TBSRequest getInstance(
94         ASN1TaggedObject obj,
95         boolean          explicit)
96     {
97         return getInstance(ASN1Sequence.getInstance(obj, explicit));
98     }
99 
getInstance( Object obj)100     public static TBSRequest getInstance(
101         Object  obj)
102     {
103         if (obj instanceof TBSRequest)
104         {
105             return (TBSRequest)obj;
106         }
107         else if (obj != null)
108         {
109             return new TBSRequest(ASN1Sequence.getInstance(obj));
110         }
111 
112         return null;
113     }
114 
getVersion()115     public ASN1Integer getVersion()
116     {
117         return version;
118     }
119 
getRequestorName()120     public GeneralName getRequestorName()
121     {
122         return requestorName;
123     }
124 
getRequestList()125     public ASN1Sequence getRequestList()
126     {
127         return requestList;
128     }
129 
getRequestExtensions()130     public Extensions getRequestExtensions()
131     {
132         return requestExtensions;
133     }
134 
135     /**
136      * Produce an object suitable for an ASN1OutputStream.
137      * <pre>
138      * TBSRequest      ::=     SEQUENCE {
139      *     version             [0]     EXPLICIT Version DEFAULT v1,
140      *     requestorName       [1]     EXPLICIT GeneralName OPTIONAL,
141      *     requestList                 SEQUENCE OF Request,
142      *     requestExtensions   [2]     EXPLICIT Extensions OPTIONAL }
143      * </pre>
144      */
toASN1Primitive()145     public ASN1Primitive toASN1Primitive()
146     {
147         ASN1EncodableVector    v = new ASN1EncodableVector();
148 
149         //
150         // if default don't include - unless explicitly provided. Not strictly correct
151         // but required for some requests
152         //
153         if (!version.equals(V1) || versionSet)
154         {
155             v.add(new DERTaggedObject(true, 0, version));
156         }
157 
158         if (requestorName != null)
159         {
160             v.add(new DERTaggedObject(true, 1, requestorName));
161         }
162 
163         v.add(requestList);
164 
165         if (requestExtensions != null)
166         {
167             v.add(new DERTaggedObject(true, 2, requestExtensions));
168         }
169 
170         return new DERSequence(v);
171     }
172 }
173