author | jjg |
Mon, 15 Aug 2011 11:48:20 -0700 | |
changeset 10336 | 0bb1999251f8 |
parent 5506 | 202f599c92aa |
child 10788 | 680a3dbfcaba |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.timestamp; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import sun.security.pkcs.PKCS7; |
|
30 |
import sun.security.util.DerValue; |
|
31 |
||
32 |
/** |
|
33 |
* This class provides the response corresponding to a timestamp request, |
|
34 |
* as defined in |
|
35 |
* <a href="http://www.ietf.org/rfc/rfc3161.txt">RFC 3161</a>. |
|
36 |
* |
|
37 |
* The TimeStampResp ASN.1 type has the following definition: |
|
38 |
* <pre> |
|
39 |
* |
|
40 |
* TimeStampResp ::= SEQUENCE { |
|
41 |
* status PKIStatusInfo, |
|
42 |
* timeStampToken TimeStampToken OPTIONAL ] |
|
43 |
* |
|
44 |
* PKIStatusInfo ::= SEQUENCE { |
|
45 |
* status PKIStatus, |
|
46 |
* statusString PKIFreeText OPTIONAL, |
|
47 |
* failInfo PKIFailureInfo OPTIONAL } |
|
48 |
* |
|
49 |
* PKIStatus ::= INTEGER { |
|
50 |
* granted (0), |
|
51 |
* -- when the PKIStatus contains the value zero a TimeStampToken, as |
|
52 |
* -- requested, is present. |
|
53 |
* grantedWithMods (1), |
|
54 |
* -- when the PKIStatus contains the value one a TimeStampToken, |
|
55 |
* -- with modifications, is present. |
|
56 |
* rejection (2), |
|
57 |
* waiting (3), |
|
58 |
* revocationWarning (4), |
|
59 |
* -- this message contains a warning that a revocation is |
|
60 |
* -- imminent |
|
61 |
* revocationNotification (5) |
|
62 |
* -- notification that a revocation has occurred } |
|
63 |
* |
|
64 |
* PKIFreeText ::= SEQUENCE SIZE (1..MAX) OF UTF8String |
|
65 |
* -- text encoded as UTF-8 String (note: each UTF8String SHOULD |
|
66 |
* -- include an RFC 1766 language tag to indicate the language |
|
67 |
* -- of the contained text) |
|
68 |
* |
|
69 |
* PKIFailureInfo ::= BIT STRING { |
|
70 |
* badAlg (0), |
|
71 |
* -- unrecognized or unsupported Algorithm Identifier |
|
72 |
* badRequest (2), |
|
73 |
* -- transaction not permitted or supported |
|
74 |
* badDataFormat (5), |
|
75 |
* -- the data submitted has the wrong format |
|
76 |
* timeNotAvailable (14), |
|
77 |
* -- the TSA's time source is not available |
|
78 |
* unacceptedPolicy (15), |
|
79 |
* -- the requested TSA policy is not supported by the TSA |
|
80 |
* unacceptedExtension (16), |
|
81 |
* -- the requested extension is not supported by the TSA |
|
82 |
* addInfoNotAvailable (17) |
|
83 |
* -- the additional information requested could not be understood |
|
84 |
* -- or is not available |
|
85 |
* systemFailure (25) |
|
86 |
* -- the request cannot be handled due to system failure } |
|
87 |
* |
|
88 |
* TimeStampToken ::= ContentInfo |
|
89 |
* -- contentType is id-signedData |
|
90 |
* -- content is SignedData |
|
91 |
* -- eContentType within SignedData is id-ct-TSTInfo |
|
92 |
* -- eContent within SignedData is TSTInfo |
|
93 |
* |
|
94 |
* </pre> |
|
95 |
* |
|
96 |
* @since 1.5 |
|
97 |
* @author Vincent Ryan |
|
98 |
* @see Timestamper |
|
99 |
*/ |
|
100 |
||
101 |
public class TSResponse { |
|
102 |
||
103 |
// Status codes (from RFC 3161) |
|
104 |
||
105 |
/** |
|
106 |
* The requested timestamp was granted. |
|
107 |
*/ |
|
108 |
public static final int GRANTED = 0; |
|
109 |
||
110 |
/** |
|
111 |
* The requested timestamp was granted with some modifications. |
|
112 |
*/ |
|
113 |
public static final int GRANTED_WITH_MODS = 1; |
|
114 |
||
115 |
/** |
|
116 |
* The requested timestamp was not granted. |
|
117 |
*/ |
|
118 |
public static final int REJECTION = 2; |
|
119 |
||
120 |
/** |
|
121 |
* The requested timestamp has not yet been processed. |
|
122 |
*/ |
|
123 |
public static final int WAITING = 3; |
|
124 |
||
125 |
/** |
|
126 |
* A warning that a certificate revocation is imminent. |
|
127 |
*/ |
|
128 |
public static final int REVOCATION_WARNING = 4; |
|
129 |
||
130 |
/** |
|
131 |
* Notification that a certificate revocation has occurred. |
|
132 |
*/ |
|
133 |
public static final int REVOCATION_NOTIFICATION = 5; |
|
134 |
||
135 |
// Failure codes (from RFC 3161) |
|
136 |
||
137 |
/** |
|
138 |
* Unrecognized or unsupported algorithm identifier. |
|
139 |
*/ |
|
140 |
public static final int BAD_ALG = 0; |
|
141 |
||
142 |
/** |
|
143 |
* The requested transaction is not permitted or supported. |
|
144 |
*/ |
|
145 |
public static final int BAD_REQUEST = 2; |
|
146 |
||
147 |
/** |
|
148 |
* The data submitted has the wrong format. |
|
149 |
*/ |
|
150 |
public static final int BAD_DATA_FORMAT = 5; |
|
151 |
||
152 |
/** |
|
153 |
* The TSA's time source is not available. |
|
154 |
*/ |
|
155 |
public static final int TIME_NOT_AVAILABLE = 14; |
|
156 |
||
157 |
/** |
|
158 |
* The requested TSA policy is not supported by the TSA. |
|
159 |
*/ |
|
160 |
public static final int UNACCEPTED_POLICY = 15; |
|
161 |
||
162 |
/** |
|
163 |
* The requested extension is not supported by the TSA. |
|
164 |
*/ |
|
165 |
public static final int UNACCEPTED_EXTENSION = 16; |
|
166 |
||
167 |
/** |
|
168 |
* The additional information requested could not be understood or is not |
|
169 |
* available. |
|
170 |
*/ |
|
171 |
public static final int ADD_INFO_NOT_AVAILABLE = 17; |
|
172 |
||
173 |
/** |
|
174 |
* The request cannot be handled due to system failure. |
|
175 |
*/ |
|
176 |
public static final int SYSTEM_FAILURE = 25; |
|
177 |
||
178 |
private static final boolean DEBUG = false; |
|
179 |
||
180 |
private int status; |
|
181 |
||
182 |
private String[] statusString = null; |
|
183 |
||
184 |
private int failureInfo = -1; |
|
185 |
||
186 |
private byte[] encodedTsToken = null; |
|
187 |
||
188 |
private PKCS7 tsToken = null; |
|
189 |
||
190 |
/** |
|
191 |
* Constructs an object to store the response to a timestamp request. |
|
192 |
* |
|
193 |
* @param status A buffer containing the ASN.1 BER encoded response. |
|
194 |
* @throws IOException The exception is thrown if a problem is encountered |
|
195 |
* parsing the timestamp response. |
|
196 |
*/ |
|
197 |
TSResponse(byte[] tsReply) throws IOException { |
|
198 |
parse(tsReply); |
|
199 |
} |
|
200 |
||
201 |
/** |
|
202 |
* Retrieve the status code returned by the TSA. |
|
203 |
*/ |
|
204 |
public int getStatusCode() { |
|
205 |
return status; |
|
206 |
} |
|
207 |
||
208 |
/** |
|
209 |
* Retrieve the status messages returned by the TSA. |
|
210 |
* |
|
211 |
* @return If null then no status messages were received. |
|
212 |
*/ |
|
213 |
public String[] getStatusMessages() { |
|
214 |
return statusString; |
|
215 |
} |
|
216 |
||
217 |
/** |
|
218 |
* Retrieve the failure code returned by the TSA. |
|
219 |
* |
|
220 |
* @return If -1 then no failure code was received. |
|
221 |
*/ |
|
222 |
public int getFailureCode() { |
|
223 |
return failureInfo; |
|
224 |
} |
|
225 |
||
226 |
public String getStatusCodeAsText() { |
|
227 |
||
228 |
switch (status) { |
|
229 |
case GRANTED: |
|
230 |
return "the timestamp request was granted."; |
|
231 |
||
232 |
case GRANTED_WITH_MODS: |
|
233 |
return |
|
234 |
"the timestamp request was granted with some modifications."; |
|
235 |
||
236 |
case REJECTION: |
|
237 |
return "the timestamp request was rejected."; |
|
238 |
||
239 |
case WAITING: |
|
240 |
return "the timestamp request has not yet been processed."; |
|
241 |
||
242 |
case REVOCATION_WARNING: |
|
243 |
return "warning: a certificate revocation is imminent."; |
|
244 |
||
245 |
case REVOCATION_NOTIFICATION: |
|
246 |
return "notification: a certificate revocation has occurred."; |
|
247 |
||
248 |
default: |
|
249 |
return ("unknown status code " + status + "."); |
|
250 |
} |
|
251 |
} |
|
252 |
||
253 |
public String getFailureCodeAsText() { |
|
254 |
||
255 |
if (failureInfo == -1) { |
|
256 |
return null; |
|
257 |
} |
|
258 |
||
259 |
switch (failureInfo) { |
|
260 |
||
261 |
case BAD_ALG: |
|
262 |
return "Unrecognized or unsupported alrorithm identifier."; |
|
263 |
||
264 |
case BAD_REQUEST: |
|
265 |
return "The requested transaction is not permitted or supported."; |
|
266 |
||
267 |
case BAD_DATA_FORMAT: |
|
268 |
return "The data submitted has the wrong format."; |
|
269 |
||
270 |
case TIME_NOT_AVAILABLE: |
|
271 |
return "The TSA's time source is not available."; |
|
272 |
||
273 |
case UNACCEPTED_POLICY: |
|
274 |
return "The requested TSA policy is not supported by the TSA."; |
|
275 |
||
276 |
case UNACCEPTED_EXTENSION: |
|
277 |
return "The requested extension is not supported by the TSA."; |
|
278 |
||
279 |
case ADD_INFO_NOT_AVAILABLE: |
|
280 |
return "The additional information requested could not be " + |
|
281 |
"understood or is not available."; |
|
282 |
||
283 |
case SYSTEM_FAILURE: |
|
284 |
return "The request cannot be handled due to system failure."; |
|
285 |
||
286 |
default: |
|
287 |
return ("unknown status code " + status); |
|
288 |
} |
|
289 |
} |
|
290 |
||
291 |
/** |
|
292 |
* Retrieve the timestamp token returned by the TSA. |
|
293 |
* |
|
294 |
* @return If null then no token was received. |
|
295 |
*/ |
|
296 |
public PKCS7 getToken() { |
|
297 |
return tsToken; |
|
298 |
} |
|
299 |
||
300 |
/** |
|
301 |
* Retrieve the ASN.1 BER encoded timestamp token returned by the TSA. |
|
302 |
* |
|
303 |
* @return If null then no token was received. |
|
304 |
*/ |
|
305 |
public byte[] getEncodedToken() { |
|
306 |
return encodedTsToken; |
|
307 |
} |
|
308 |
||
309 |
/* |
|
310 |
* Parses the timestamp response. |
|
311 |
* |
|
312 |
* @param status A buffer containing the ASN.1 BER encoded response. |
|
313 |
* @throws IOException The exception is thrown if a problem is encountered |
|
314 |
* parsing the timestamp response. |
|
315 |
*/ |
|
316 |
private void parse(byte[] tsReply) throws IOException { |
|
317 |
// Decode TimeStampResp |
|
318 |
||
319 |
DerValue derValue = new DerValue(tsReply); |
|
320 |
if (derValue.tag != DerValue.tag_Sequence) { |
|
321 |
throw new IOException("Bad encoding for timestamp response"); |
|
322 |
} |
|
323 |
||
324 |
// Parse status |
|
325 |
||
326 |
DerValue status = derValue.data.getDerValue(); |
|
327 |
// Parse status |
|
328 |
this.status = status.data.getInteger(); |
|
329 |
if (DEBUG) { |
|
330 |
System.out.println("timestamp response: status=" + this.status); |
|
331 |
} |
|
332 |
// Parse statusString, if present |
|
333 |
if (status.data.available() > 0) { |
|
334 |
DerValue[] strings = status.data.getSequence(1); |
|
335 |
statusString = new String[strings.length]; |
|
336 |
for (int i = 0; i < strings.length; i++) { |
|
337 |
statusString[i] = strings[i].data.getUTF8String(); |
|
338 |
} |
|
339 |
} |
|
340 |
// Parse failInfo, if present |
|
341 |
if (status.data.available() > 0) { |
|
342 |
byte[] failInfo = status.data.getBitString(); |
|
343 |
int failureInfo = (new Byte(failInfo[0])).intValue(); |
|
344 |
if (failureInfo < 0 || failureInfo > 25 || failInfo.length != 1) { |
|
345 |
throw new IOException("Bad encoding for timestamp response: " + |
|
346 |
"unrecognized value for the failInfo element"); |
|
347 |
} |
|
348 |
this.failureInfo = failureInfo; |
|
349 |
} |
|
350 |
||
351 |
// Parse timeStampToken, if present |
|
352 |
if (derValue.data.available() > 0) { |
|
353 |
DerValue timestampToken = derValue.data.getDerValue(); |
|
354 |
encodedTsToken = timestampToken.toByteArray(); |
|
355 |
tsToken = new PKCS7(encodedTsToken); |
|
356 |
} |
|
357 |
||
358 |
// Check the format of the timestamp response |
|
359 |
if (this.status == 0 || this.status == 1) { |
|
360 |
if (tsToken == null) { |
|
361 |
throw new TimestampException( |
|
362 |
"Bad encoding for timestamp response: " + |
|
363 |
"expected a timeStampToken element to be present"); |
|
364 |
} |
|
365 |
} else if (tsToken != null) { |
|
366 |
throw new TimestampException( |
|
367 |
"Bad encoding for timestamp response: " + |
|
368 |
"expected no timeStampToken element to be present"); |
|
369 |
} |
|
370 |
} |
|
371 |
||
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
372 |
final static class TimestampException extends IOException { |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
373 |
private static final long serialVersionUID = -1631631794891940953L; |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
374 |
|
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
375 |
TimestampException(String message) { |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
376 |
super(message); |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
377 |
} |
2 | 378 |
} |
379 |
} |