J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.security.timestamp; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.math.BigInteger; |
| 30 | import java.util.Date; |
| 31 | import sun.security.util.DerValue; |
| 32 | import sun.security.util.ObjectIdentifier; |
| 33 | import sun.security.x509.AlgorithmId; |
| 34 | |
| 35 | /** |
| 36 | * This class provides the timestamp token info resulting from a successful |
| 37 | * timestamp request, as defined in |
| 38 | * <a href="http://www.ietf.org/rfc/rfc3161.txt">RFC 3161</a>. |
| 39 | * |
| 40 | * The timestampTokenInfo ASN.1 type has the following definition: |
| 41 | * <pre> |
| 42 | * |
| 43 | * TSTInfo ::= SEQUENCE { |
| 44 | * version INTEGER { v1(1) }, |
| 45 | * policy TSAPolicyId, |
| 46 | * messageImprint MessageImprint, |
| 47 | * -- MUST have the same value as the similar field in |
| 48 | * -- TimeStampReq |
| 49 | * serialNumber INTEGER, |
| 50 | * -- Time-Stamping users MUST be ready to accommodate integers |
| 51 | * -- up to 160 bits. |
| 52 | * genTime GeneralizedTime, |
| 53 | * accuracy Accuracy OPTIONAL, |
| 54 | * ordering BOOLEAN DEFAULT FALSE, |
| 55 | * nonce INTEGER OPTIONAL, |
| 56 | * -- MUST be present if the similar field was present |
| 57 | * -- in TimeStampReq. In that case it MUST have the same value. |
| 58 | * tsa [0] GeneralName OPTIONAL, |
| 59 | * extensions [1] IMPLICIT Extensions OPTIONAL } |
| 60 | * |
| 61 | * Accuracy ::= SEQUENCE { |
| 62 | * seconds INTEGER OPTIONAL, |
| 63 | * millis [0] INTEGER (1..999) OPTIONAL, |
| 64 | * micros [1] INTEGER (1..999) OPTIONAL } |
| 65 | * |
| 66 | * </pre> |
| 67 | * |
| 68 | * @since 1.5 |
| 69 | * @see Timestamper |
| 70 | * @author Vincent Ryan |
| 71 | */ |
| 72 | |
| 73 | public class TimestampToken { |
| 74 | |
| 75 | private int version; |
| 76 | private ObjectIdentifier policy; |
| 77 | private BigInteger serialNumber; |
| 78 | private AlgorithmId hashAlgorithm; |
| 79 | private byte[] hashedMessage; |
| 80 | private Date genTime; |
| 81 | private BigInteger nonce; |
| 82 | |
| 83 | /** |
| 84 | * Constructs an object to store a timestamp token. |
| 85 | * |
| 86 | * @param status A buffer containing the ASN.1 BER encoding of the |
| 87 | * TSTInfo element defined in RFC 3161. |
| 88 | */ |
| 89 | public TimestampToken(byte[] timestampTokenInfo) throws IOException { |
| 90 | if (timestampTokenInfo == null) { |
| 91 | throw new IOException("No timestamp token info"); |
| 92 | } |
| 93 | parse(timestampTokenInfo); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Extract the date and time from the timestamp token. |
| 98 | * |
| 99 | * @return The date and time when the timestamp was generated. |
| 100 | */ |
| 101 | public Date getDate() { |
| 102 | return genTime; |
| 103 | } |
| 104 | |
| 105 | public AlgorithmId getHashAlgorithm() { |
| 106 | return hashAlgorithm; |
| 107 | } |
| 108 | |
| 109 | // should only be used internally, otherwise return a clone |
| 110 | public byte[] getHashedMessage() { |
| 111 | return hashedMessage; |
| 112 | } |
| 113 | |
| 114 | public BigInteger getNonce() { |
| 115 | return nonce; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Parses the timestamp token info. |
| 120 | * |
| 121 | * @param timestampTokenInfo A buffer containing an ASN.1 BER encoded |
| 122 | * TSTInfo. |
| 123 | * @throws IOException The exception is thrown if a problem is encountered |
| 124 | * while parsing. |
| 125 | */ |
| 126 | private void parse(byte[] timestampTokenInfo) throws IOException { |
| 127 | |
| 128 | DerValue tstInfo = new DerValue(timestampTokenInfo); |
| 129 | if (tstInfo.tag != DerValue.tag_Sequence) { |
| 130 | throw new IOException("Bad encoding for timestamp token info"); |
| 131 | } |
| 132 | // Parse version |
| 133 | version = tstInfo.data.getInteger(); |
| 134 | |
| 135 | // Parse policy |
| 136 | policy = tstInfo.data.getOID(); |
| 137 | |
| 138 | // Parse messageImprint |
| 139 | DerValue messageImprint = tstInfo.data.getDerValue(); |
| 140 | hashAlgorithm = AlgorithmId.parse(messageImprint.data.getDerValue()); |
| 141 | hashedMessage = messageImprint.data.getOctetString(); |
| 142 | |
| 143 | // Parse serialNumber |
| 144 | serialNumber = tstInfo.data.getBigInteger(); |
| 145 | |
| 146 | // Parse genTime |
| 147 | genTime = tstInfo.data.getGeneralizedTime(); |
| 148 | |
| 149 | // Parse optional elements, if present |
| 150 | while (tstInfo.data.available() > 0) { |
| 151 | DerValue d = tstInfo.data.getDerValue(); |
| 152 | if (d.tag == DerValue.tag_Integer) { // must be the nonce |
| 153 | nonce = d.getBigInteger(); |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | // Additional fields: |
| 158 | // Parse accuracy |
| 159 | // Parse ordering |
| 160 | // Parse tsa |
| 161 | // Parse extensions |
| 162 | } |
| 163 | } |
| 164 | } |