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