11package org .bouncycastle .tsp ;
22
3- import java .io .ByteArrayInputStream ;
43import java .io .IOException ;
54import java .io .InputStream ;
65
7- import org .bouncycastle .asn1 .ASN1Encodable ;
86import org .bouncycastle .asn1 .ASN1Encoding ;
97import org .bouncycastle .asn1 .ASN1InputStream ;
8+ import org .bouncycastle .asn1 .ASN1Object ;
109import org .bouncycastle .asn1 .DLSequence ;
1110import org .bouncycastle .asn1 .cmp .PKIFailureInfo ;
1211import org .bouncycastle .asn1 .cmp .PKIFreeText ;
2221 */
2322public class TimeStampResponse
2423{
25- TimeStampResp resp ;
26- TimeStampToken timeStampToken ;
24+ private static TimeStampResp parseTimeStampResp (byte [] encoding )
25+ throws IOException , TSPException
26+ {
27+ try
28+ {
29+ return TimeStampResp .getInstance (encoding );
30+ }
31+ catch (IllegalArgumentException e )
32+ {
33+ throw new TSPException ("malformed timestamp response: " + e , e );
34+ }
35+ catch (ClassCastException e )
36+ {
37+ throw new TSPException ("malformed timestamp response: " + e , e );
38+ }
39+ }
40+
41+ private static TimeStampResp parseTimeStampResp (InputStream in )
42+ throws IOException , TSPException
43+ {
44+ try
45+ {
46+ return TimeStampResp .getInstance (new ASN1InputStream (in ).readObject ());
47+ }
48+ catch (IllegalArgumentException e )
49+ {
50+ throw new TSPException ("malformed timestamp response: " + e , e );
51+ }
52+ catch (ClassCastException e )
53+ {
54+ throw new TSPException ("malformed timestamp response: " + e , e );
55+ }
56+ }
57+
58+ private final TimeStampResp resp ;
59+ private final TimeStampToken timeStampToken ;
2760
2861 public TimeStampResponse (TimeStampResp resp )
2962 throws TSPException , IOException
3063 {
3164 this .resp = resp ;
32-
33- if (resp .getTimeStampToken () != null )
34- {
35- timeStampToken = new TimeStampToken (resp .getTimeStampToken ());
36- }
65+
66+ ContentInfo timeStampToken = resp .getTimeStampToken ();
67+ this .timeStampToken = timeStampToken == null ? null : new TimeStampToken (timeStampToken );
3768 }
3869
3970 /**
@@ -46,7 +77,7 @@ public TimeStampResponse(TimeStampResp resp)
4677 public TimeStampResponse (byte [] resp )
4778 throws TSPException , IOException
4879 {
49- this (new ByteArrayInputStream (resp ));
80+ this (parseTimeStampResp (resp ));
5081 }
5182
5283 /**
@@ -59,7 +90,7 @@ public TimeStampResponse(byte[] resp)
5990 public TimeStampResponse (InputStream in )
6091 throws TSPException , IOException
6192 {
62- this (readTimeStampResp (in ));
93+ this (parseTimeStampResp (in ));
6394 }
6495
6596 TimeStampResponse (DLSequence dlSequence )
@@ -80,45 +111,25 @@ public TimeStampResponse(InputStream in)
80111 }
81112 }
82113
83- private static TimeStampResp readTimeStampResp (
84- InputStream in )
85- throws IOException , TSPException
86- {
87- try
88- {
89- return TimeStampResp .getInstance (new ASN1InputStream (in ).readObject ());
90- }
91- catch (IllegalArgumentException e )
92- {
93- throw new TSPException ("malformed timestamp response: " + e , e );
94- }
95- catch (ClassCastException e )
96- {
97- throw new TSPException ("malformed timestamp response: " + e , e );
98- }
99- }
100-
101114 public int getStatus ()
102115 {
103- return resp .getStatus ().getStatus ().intValue ();
116+ return resp .getStatus ().getStatusObject ().intValueExact ();
104117 }
105118
106119 public String getStatusString ()
107120 {
108- if (resp .getStatus ().getStatusString () ! = null )
121+ if (resp .getStatus ().getStatusString () = = null )
109122 {
110- StringBuffer statusStringBuf = new StringBuffer ();
111- PKIFreeText text = resp .getStatus ().getStatusString ();
112- for (int i = 0 ; i != text .size (); i ++)
113- {
114- statusStringBuf .append (text .getStringAtUTF8 (i ).getString ());
115- }
116- return statusStringBuf .toString ();
123+ return null ;
117124 }
118- else
125+
126+ StringBuffer statusStringBuf = new StringBuffer ();
127+ PKIFreeText text = resp .getStatus ().getStatusString ();
128+ for (int i = 0 ; i != text .size (); i ++)
119129 {
120- return null ;
130+ statusStringBuf . append ( text . getStringAtUTF8 ( i ). getString ()) ;
121131 }
132+ return statusStringBuf .toString ();
122133 }
123134
124135 public PKIFailureInfo getFailInfo ()
@@ -152,7 +163,7 @@ public void validate(
152163
153164 if (tok != null )
154165 {
155- TimeStampTokenInfo tstInfo = tok .getTimeStampInfo ();
166+ TimeStampTokenInfo tstInfo = tok .getTimeStampInfo ();
156167
157168 if (request .getNonce () != null && !request .getNonce ().equals (tstInfo .getNonce ()))
158169 {
@@ -163,17 +174,18 @@ public void validate(
163174 {
164175 throw new TSPValidationException ("time stamp token found in failed request." );
165176 }
166-
167- if (!Arrays .constantTimeAreEqual (request .getMessageImprintDigest (), tstInfo .getMessageImprintDigest ()))
168- {
169- throw new TSPValidationException ("response for different message imprint digest." );
170- }
171-
177+
178+ // TODO Should be (absent-parameters-flexible) equality of the whole AlgorithmIdentifier?
172179 if (!tstInfo .getMessageImprintAlgOID ().equals (request .getMessageImprintAlgOID ()))
173180 {
174181 throw new TSPValidationException ("response for different message imprint algorithm." );
175182 }
176183
184+ if (!Arrays .constantTimeAreEqual (request .getMessageImprintDigest (), tstInfo .getMessageImprintDigest ()))
185+ {
186+ throw new TSPValidationException ("response for different message imprint digest." );
187+ }
188+
177189 Attribute scV1 = tok .getSignedAttributes ().get (PKCSObjectIdentifiers .id_aa_signingCertificate );
178190 Attribute scV2 = tok .getSignedAttributes ().get (PKCSObjectIdentifiers .id_aa_signingCertificateV2 );
179191
@@ -216,16 +228,13 @@ public byte[] getEncoded() throws IOException
216228 */
217229 public byte [] getEncoded (String encoding ) throws IOException
218230 {
231+ ASN1Object asn1Object = resp ;
219232 if (ASN1Encoding .DL .equals (encoding ))
220233 {
221- if (timeStampToken == null )
222- {
223- return new DLSequence (resp .getStatus ()).getEncoded (encoding );
224- }
225-
226- return new DLSequence (new ASN1Encodable [] { resp .getStatus (),
227- timeStampToken .toCMSSignedData ().toASN1Structure () }).getEncoded (encoding );
234+ asn1Object = timeStampToken == null
235+ ? new DLSequence (resp .getStatus ())
236+ : new DLSequence (resp .getStatus (), timeStampToken .toCMSSignedData ().toASN1Structure ());
228237 }
229- return resp .getEncoded (encoding );
238+ return asn1Object .getEncoded (encoding );
230239 }
231240}
0 commit comments