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