< prev index next >
src/share/classes/sun/security/timestamp/TSResponse.java
Print this page
rev 1518 : 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
Reviewed-by: mchung
@@ -25,10 +25,11 @@
package sun.security.timestamp;
import java.io.IOException;
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
@@ -173,22 +174,24 @@
/**
* The request cannot be handled due to system failure.
*/
public static final int SYSTEM_FAILURE = 25;
- private static final boolean DEBUG = false;
+ private static final Debug debug = Debug.getInstance("ts");
private int status;
private String[] statusString = null;
- private int failureInfo = -1;
+ 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 status A buffer containing the ASN.1 BER encoded response.
* @throws IOException The exception is thrown if a problem is encountered
@@ -213,15 +216,15 @@
public String[] getStatusMessages() {
return statusString;
}
/**
- * Retrieve the failure code returned by the TSA.
+ * Retrieve the failure info returned by the TSA.
*
- * @return If -1 then no failure code was received.
+ * @return the failure info, or null if no failure code was received.
*/
- public int getFailureCode() {
+ public boolean[] getFailureInfo() {
return failureInfo;
}
public String getStatusCodeAsText() {
@@ -248,46 +251,42 @@
default:
return ("unknown status code " + status + ".");
}
}
- public String getFailureCodeAsText() {
-
- if (failureInfo == -1) {
- return null;
+ private boolean isSet(int position) {
+ return failureInfo[position];
}
- switch (failureInfo) {
-
- case BAD_ALG:
- return "Unrecognized or unsupported alrorithm identifier.";
+ public String getFailureCodeAsText() {
- case BAD_REQUEST:
- return "The requested transaction is not permitted or supported.";
+ if (failureInfo == null) {
+ return "";
+ }
- case BAD_DATA_FORMAT:
+ 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.";
-
- case TIME_NOT_AVAILABLE:
+ if (isSet(TIME_NOT_AVAILABLE))
return "The TSA's time source is not available.";
-
- case UNACCEPTED_POLICY:
+ if (isSet(UNACCEPTED_POLICY))
return "The requested TSA policy is not supported by the TSA.";
-
- case UNACCEPTED_EXTENSION:
+ if (isSet(UNACCEPTED_EXTENSION))
return "The requested extension is not supported by the TSA.";
-
- case ADD_INFO_NOT_AVAILABLE:
+ if (isSet(ADD_INFO_NOT_AVAILABLE))
return "The additional information requested could not be " +
"understood or is not available.";
-
- case SYSTEM_FAILURE:
+ if (isSet(SYSTEM_FAILURE))
return "The request cannot be handled due to system failure.";
+ } catch (ArrayIndexOutOfBoundsException ex) {}
- default:
- return ("unknown status code " + status);
- }
+ return ("unknown failure code");
}
/**
* Retrieve the timestamp token returned by the TSA.
*
@@ -295,10 +294,14 @@
*/
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.
*/
@@ -321,40 +324,42 @@
throw new IOException("Bad encoding for timestamp response");
}
// Parse status
- DerValue status = derValue.data.getDerValue();
- // Parse status
- this.status = status.data.getInteger();
- if (DEBUG) {
- System.out.println("timestamp response: status=" + this.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 (status.data.available() > 0) {
- DerValue[] strings = status.data.getSequence(1);
+ 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].data.getUTF8String();
+ statusString[i] = strings[i].getUTF8String();
+ if (debug != null) {
+ debug.println("timestamp response: statusString=" +
+ statusString[i]);
}
}
- // Parse failInfo, if present
- if (status.data.available() > 0) {
- byte[] failInfo = status.data.getBitString();
- int failureInfo = (new Byte(failInfo[0])).intValue();
- if (failureInfo < 0 || failureInfo > 25 || failInfo.length != 1) {
- throw new IOException("Bad encoding for timestamp response: " +
- "unrecognized value for the failInfo element");
}
- this.failureInfo = failureInfo;
+ }
+ // 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);
+ tstInfo = new TimestampToken(tsToken.getContentInfo().getData());
}
// Check the format of the timestamp response
if (this.status == 0 || this.status == 1) {
if (tsToken == null) {
< prev index next >