Path: blob/master/src/java.base/share/classes/sun/security/timestamp/TimestampToken.java
41159 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.timestamp;2627import java.io.IOException;28import java.math.BigInteger;29import java.util.Date;30import sun.security.util.DerValue;31import sun.security.util.ObjectIdentifier;32import sun.security.x509.AlgorithmId;3334/**35* This class provides the timestamp token info resulting from a successful36* timestamp request, as defined in37* <a href="http://www.ietf.org/rfc/rfc3161.txt">RFC 3161</a>.38*39* The timestampTokenInfo ASN.1 type has the following definition:40* <pre>41*42* TSTInfo ::= SEQUENCE {43* version INTEGER { v1(1) },44* policy TSAPolicyId,45* messageImprint MessageImprint,46* -- MUST have the same value as the similar field in47* -- TimeStampReq48* serialNumber INTEGER,49* -- Time-Stamping users MUST be ready to accommodate integers50* -- up to 160 bits.51* genTime GeneralizedTime,52* accuracy Accuracy OPTIONAL,53* ordering BOOLEAN DEFAULT FALSE,54* nonce INTEGER OPTIONAL,55* -- MUST be present if the similar field was present56* -- in TimeStampReq. In that case it MUST have the same value.57* tsa [0] GeneralName OPTIONAL,58* extensions [1] IMPLICIT Extensions OPTIONAL }59*60* Accuracy ::= SEQUENCE {61* seconds INTEGER OPTIONAL,62* millis [0] INTEGER (1..999) OPTIONAL,63* micros [1] INTEGER (1..999) OPTIONAL }64*65* </pre>66*67* @since 1.568* @see Timestamper69* @author Vincent Ryan70*/7172public class TimestampToken {7374private int version;75private ObjectIdentifier policy;76private BigInteger serialNumber;77private AlgorithmId hashAlgorithm;78private byte[] hashedMessage;79private Date genTime;80private BigInteger nonce;8182/**83* Constructs an object to store a timestamp token.84*85* @param timestampTokenInfo A buffer containing the ASN.1 BER encoding of the86* TSTInfo element defined in RFC 3161.87*/88public TimestampToken(byte[] timestampTokenInfo) throws IOException {89if (timestampTokenInfo == null) {90throw new IOException("No timestamp token info");91}92parse(timestampTokenInfo);93}9495/**96* Extract the date and time from the timestamp token.97*98* @return The date and time when the timestamp was generated.99*/100public Date getDate() {101return genTime;102}103104public AlgorithmId getHashAlgorithm() {105return hashAlgorithm;106}107108// should only be used internally, otherwise return a clone109public byte[] getHashedMessage() {110return hashedMessage;111}112113public BigInteger getNonce() {114return nonce;115}116117public String getPolicyID() {118return policy.toString();119}120121public BigInteger getSerialNumber() {122return serialNumber;123}124125/*126* Parses the timestamp token info.127*128* @param timestampTokenInfo A buffer containing an ASN.1 BER encoded129* TSTInfo.130* @throws IOException The exception is thrown if a problem is encountered131* while parsing.132*/133private void parse(byte[] timestampTokenInfo) throws IOException {134135DerValue tstInfo = new DerValue(timestampTokenInfo);136if (tstInfo.tag != DerValue.tag_Sequence) {137throw new IOException("Bad encoding for timestamp token info");138}139// Parse version140version = tstInfo.data.getInteger();141142// Parse policy143policy = tstInfo.data.getOID();144145// Parse messageImprint146DerValue messageImprint = tstInfo.data.getDerValue();147hashAlgorithm = AlgorithmId.parse(messageImprint.data.getDerValue());148hashedMessage = messageImprint.data.getOctetString();149150// Parse serialNumber151serialNumber = tstInfo.data.getBigInteger();152153// Parse genTime154genTime = tstInfo.data.getGeneralizedTime();155156// Parse optional elements, if present157while (tstInfo.data.available() > 0) {158DerValue d = tstInfo.data.getDerValue();159if (d.tag == DerValue.tag_Integer) { // must be the nonce160nonce = d.getBigInteger();161break;162}163164// Additional fields:165// Parse accuracy166// Parse ordering167// Parse tsa168// Parse extensions169}170}171}172173174