jdk/src/java.base/share/classes/sun/security/timestamp/TSResponse.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 25859 3317bb8137f4
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
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;
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    30
import sun.security.util.Debug;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class provides the response corresponding to a timestamp request,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * as defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <a href="http://www.ietf.org/rfc/rfc3161.txt">RFC 3161</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The TimeStampResp ASN.1 type has the following definition:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *     TimeStampResp ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *         status            PKIStatusInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *         timeStampToken    TimeStampToken OPTIONAL ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *     PKIStatusInfo ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *         status        PKIStatus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *         statusString  PKIFreeText OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *         failInfo      PKIFailureInfo OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *     PKIStatus ::= INTEGER {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *         granted                (0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *           -- when the PKIStatus contains the value zero a TimeStampToken, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *           -- requested, is present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *         grantedWithMods        (1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *           -- when the PKIStatus contains the value one a TimeStampToken,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *           -- with modifications, is present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *         rejection              (2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *         waiting                (3),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *         revocationWarning      (4),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *           -- this message contains a warning that a revocation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *           -- imminent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *         revocationNotification (5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *           -- notification that a revocation has occurred }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *     PKIFreeText ::= SEQUENCE SIZE (1..MAX) OF UTF8String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *           -- text encoded as UTF-8 String (note:  each UTF8String SHOULD
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *           -- include an RFC 1766 language tag to indicate the language
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *           -- of the contained text)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *     PKIFailureInfo ::= BIT STRING {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *         badAlg              (0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *           -- unrecognized or unsupported Algorithm Identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *         badRequest          (2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *           -- transaction not permitted or supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *         badDataFormat       (5),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *           -- the data submitted has the wrong format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *         timeNotAvailable    (14),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *           -- the TSA's time source is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *         unacceptedPolicy    (15),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *           -- the requested TSA policy is not supported by the TSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *         unacceptedExtension (16),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *           -- the requested extension is not supported by the TSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *         addInfoNotAvailable (17)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *           -- the additional information requested could not be understood
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *           -- or is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *         systemFailure       (25)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *           -- the request cannot be handled due to system failure }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *     TimeStampToken ::= ContentInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *         -- contentType is id-signedData
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *         -- content is SignedData
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *         -- eContentType within SignedData is id-ct-TSTInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *         -- eContent within SignedData is TSTInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * @author Vincent Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * @see Timestamper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
public class TSResponse {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // Status codes (from RFC 3161)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * The requested timestamp was granted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public static final int GRANTED = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * The requested timestamp was granted with some modifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public static final int GRANTED_WITH_MODS = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * The requested timestamp was not granted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public static final int REJECTION = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * The requested timestamp has not yet been processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public static final int WAITING = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * A warning that a certificate revocation is imminent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public static final int REVOCATION_WARNING = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Notification that a certificate revocation has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public static final int REVOCATION_NOTIFICATION = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    // Failure codes (from RFC 3161)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Unrecognized or unsupported algorithm identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public static final int BAD_ALG = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * The requested transaction is not permitted or supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public static final int BAD_REQUEST = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * The data submitted has the wrong format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public static final int BAD_DATA_FORMAT = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * The TSA's time source is not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public static final int TIME_NOT_AVAILABLE = 14;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * The requested TSA policy is not supported by the TSA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public static final int UNACCEPTED_POLICY = 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * The requested extension is not supported by the TSA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public static final int UNACCEPTED_EXTENSION = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * The additional information requested could not be understood or is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public static final int ADD_INFO_NOT_AVAILABLE = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * The request cannot be handled due to system failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public static final int SYSTEM_FAILURE = 25;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   179
    private static final Debug debug = Debug.getInstance("ts");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private int status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private String[] statusString = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   185
    private boolean[] failureInfo = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private byte[] encodedTsToken = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private PKCS7 tsToken = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   191
    private TimestampToken tstInfo;
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   192
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Constructs an object to store the response to a timestamp request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param status A buffer containing the ASN.1 BER encoded response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @throws IOException The exception is thrown if a problem is encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *         parsing the timestamp response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    TSResponse(byte[] tsReply) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        parse(tsReply);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Retrieve the status code returned by the TSA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public int getStatusCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        return status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Retrieve the status messages returned by the TSA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @return If null then no status messages were received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public String[] getStatusMessages() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return statusString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   221
     * Retrieve the failure info returned by the TSA.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   223
     * @return the failure info, or null if no failure code was received.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   225
    public boolean[] getFailureInfo() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return failureInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public String getStatusCodeAsText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        switch (status)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        case GRANTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return "the timestamp request was granted.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        case GRANTED_WITH_MODS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                "the timestamp request was granted with some modifications.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        case REJECTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            return "the timestamp request was rejected.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        case WAITING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            return "the timestamp request has not yet been processed.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        case REVOCATION_WARNING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            return "warning: a certificate revocation is imminent.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        case REVOCATION_NOTIFICATION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            return "notification: a certificate revocation has occurred.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return ("unknown status code " + status + ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   256
    private boolean isSet(int position) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   257
        return failureInfo[position];
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   258
    }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   259
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public String getFailureCodeAsText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   262
        if (failureInfo == null) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   263
            return "";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   266
        try {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   267
            if (isSet(BAD_ALG))
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   268
                return "Unrecognized or unsupported algorithm identifier.";
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   269
            if (isSet(BAD_REQUEST))
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   270
                return "The requested transaction is not permitted or " +
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   271
                       "supported.";
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   272
            if (isSet(BAD_DATA_FORMAT))
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   273
                return "The data submitted has the wrong format.";
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   274
            if (isSet(TIME_NOT_AVAILABLE))
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   275
                return "The TSA's time source is not available.";
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   276
            if (isSet(UNACCEPTED_POLICY))
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   277
                return "The requested TSA policy is not supported by the TSA.";
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   278
            if (isSet(UNACCEPTED_EXTENSION))
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   279
                return "The requested extension is not supported by the TSA.";
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   280
            if (isSet(ADD_INFO_NOT_AVAILABLE))
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   281
                return "The additional information requested could not be " +
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   282
                       "understood or is not available.";
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   283
            if (isSet(SYSTEM_FAILURE))
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   284
                return "The request cannot be handled due to system failure.";
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   285
        } catch (ArrayIndexOutOfBoundsException ex) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   287
        return ("unknown failure code");
2
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
     * Retrieve the timestamp token returned by the TSA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @return If null then no token was received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public PKCS7 getToken() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return tsToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   299
    public TimestampToken getTimestampToken() {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   300
        return tstInfo;
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   301
    }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   302
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Retrieve the ASN.1 BER encoded timestamp token returned by the TSA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @return If null then no token was received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public byte[] getEncodedToken() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return encodedTsToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Parses the timestamp response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @param status A buffer containing the ASN.1 BER encoded response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @throws IOException The exception is thrown if a problem is encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *         parsing the timestamp response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private void parse(byte[] tsReply) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        // Decode TimeStampResp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        DerValue derValue = new DerValue(tsReply);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (derValue.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throw new IOException("Bad encoding for timestamp response");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        // Parse status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   329
        DerValue statusInfo = derValue.data.getDerValue();
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   330
        this.status = statusInfo.data.getInteger();
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   331
        if (debug != null) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   332
            debug.println("timestamp response: status=" + this.status);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        // Parse statusString, if present
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   335
        if (statusInfo.data.available() > 0) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   336
            byte tag = (byte)statusInfo.data.peekByte();
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   337
            if (tag == DerValue.tag_SequenceOf) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   338
                DerValue[] strings = statusInfo.data.getSequence(1);
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   339
                statusString = new String[strings.length];
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   340
                for (int i = 0; i < strings.length; i++) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   341
                    statusString[i] = strings[i].getUTF8String();
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   342
                    if (debug != null) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   343
                        debug.println("timestamp response: statusString=" +
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   344
                                      statusString[i]);
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   345
                    }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   346
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        // Parse failInfo, if present
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   350
        if (statusInfo.data.available() > 0) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   351
            this.failureInfo
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   352
                = statusInfo.data.getUnalignedBitString().toBooleanArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        // Parse timeStampToken, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (derValue.data.available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            DerValue timestampToken = derValue.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            encodedTsToken = timestampToken.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            tsToken = new PKCS7(encodedTsToken);
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   360
            tstInfo = new TimestampToken(tsToken.getContentInfo().getData());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        // Check the format of the timestamp response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (this.status == 0 || this.status == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            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 a timeStampToken element to be present");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        } else if (tsToken != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            throw new TimestampException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                "Bad encoding for timestamp response: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                "expected no timeStampToken element to be present");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   377
    static final class TimestampException extends IOException {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   378
        private static final long serialVersionUID = -1631631794891940953L;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   379
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   380
        TimestampException(String message) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   381
            super(message);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   382
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
}