jdk/src/share/classes/sun/security/timestamp/TimestampToken.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 17161 df1ec0e2f0e7
child 18224 95b6fb2f35a0
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 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 java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.ObjectIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.x509.AlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This class provides the timestamp token info resulting from a successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * timestamp request, as defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <a href="http://www.ietf.org/rfc/rfc3161.txt">RFC 3161</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The timestampTokenInfo ASN.1 type has the following definition:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *     TSTInfo ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *         version                INTEGER  { v1(1) },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *         policy                 TSAPolicyId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *         messageImprint         MessageImprint,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *           -- MUST have the same value as the similar field in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *           -- TimeStampReq
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *         serialNumber           INTEGER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *          -- Time-Stamping users MUST be ready to accommodate integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *          -- up to 160 bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *         genTime                GeneralizedTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *         accuracy               Accuracy                 OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *         ordering               BOOLEAN             DEFAULT FALSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *         nonce                  INTEGER                  OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *           -- MUST be present if the similar field was present
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *           -- in TimeStampReq.  In that case it MUST have the same value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *         tsa                    [0] GeneralName          OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *         extensions             [1] IMPLICIT Extensions  OPTIONAL }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *     Accuracy ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *         seconds        INTEGER           OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *         millis     [0] INTEGER  (1..999) OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *         micros     [1] INTEGER  (1..999) OPTIONAL  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @see Timestamper
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @author Vincent Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
public class TimestampToken {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private int version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private ObjectIdentifier policy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private BigInteger serialNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private AlgorithmId hashAlgorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private byte[] hashedMessage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private Date genTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private BigInteger nonce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Constructs an object to store a timestamp token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param status A buffer containing the ASN.1 BER encoding of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *               TSTInfo element defined in RFC 3161.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public TimestampToken(byte[] timestampTokenInfo) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (timestampTokenInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new IOException("No timestamp token info");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        parse(timestampTokenInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Extract the date and time from the timestamp token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @return The date and time when the timestamp was generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public Date getDate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return genTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public AlgorithmId getHashAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return hashAlgorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // should only be used internally, otherwise return a clone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public byte[] getHashedMessage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return hashedMessage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public BigInteger getNonce() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return nonce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Parses the timestamp token info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param timestampTokenInfo A buffer containing an ASN.1 BER encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *                           TSTInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @throws IOException The exception is thrown if a problem is encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *         while parsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private void parse(byte[] timestampTokenInfo) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        DerValue tstInfo = new DerValue(timestampTokenInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (tstInfo.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new IOException("Bad encoding for timestamp token info");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // Parse version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        version = tstInfo.data.getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // Parse policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        policy = tstInfo.data.getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // Parse messageImprint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        DerValue messageImprint = tstInfo.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        hashAlgorithm = AlgorithmId.parse(messageImprint.data.getDerValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        hashedMessage = messageImprint.data.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // Parse serialNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        serialNumber = tstInfo.data.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // Parse genTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        genTime = tstInfo.data.getGeneralizedTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // Parse optional elements, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        while (tstInfo.data.available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            DerValue d = tstInfo.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (d.tag == DerValue.tag_Integer) {    // must be the nonce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                nonce = d.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            // Additional fields:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            // Parse accuracy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            // Parse ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            // Parse tsa
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            // Parse extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
}