Path: blob/master/src/java.base/share/classes/sun/security/timestamp/TSRequest.java
41159 views
/*1* Copyright (c) 2003, 2020, 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.security.MessageDigest;30import java.security.NoSuchAlgorithmException;31import java.security.cert.X509Extension;32import sun.security.util.DerValue;33import sun.security.util.DerOutputStream;34import sun.security.util.ObjectIdentifier;35import sun.security.x509.AlgorithmId;3637/**38* This class provides a timestamp request, as defined in39* <a href="http://www.ietf.org/rfc/rfc3161.txt">RFC 3161</a>.40*41* The TimeStampReq ASN.1 type has the following definition:42* <pre>43*44* TimeStampReq ::= SEQUENCE {45* version INTEGER { v1(1) },46* messageImprint MessageImprint47* -- a hash algorithm OID and the hash value of the data to be48* -- time-stamped.49* reqPolicy TSAPolicyId OPTIONAL,50* nonce INTEGER OPTIONAL,51* certReq BOOLEAN DEFAULT FALSE,52* extensions [0] IMPLICIT Extensions OPTIONAL }53*54* MessageImprint ::= SEQUENCE {55* hashAlgorithm AlgorithmIdentifier,56* hashedMessage OCTET STRING }57*58* TSAPolicyId ::= OBJECT IDENTIFIER59*60* </pre>61*62* @since 1.563* @author Vincent Ryan64* @see Timestamper65*/6667public class TSRequest {6869private int version = 1;7071private AlgorithmId hashAlgorithmId = null;7273private byte[] hashValue;7475private String policyId = null;7677private BigInteger nonce = null;7879private boolean returnCertificate = false;8081private X509Extension[] extensions = null;8283/**84* Constructs a timestamp request for the supplied data.85*86* @param toBeTimeStamped The data to be timestamped.87* @param messageDigest The MessageDigest of the hash algorithm to use.88* @throws NoSuchAlgorithmException if the hash algorithm is not supported89*/90public TSRequest(String tSAPolicyID, byte[] toBeTimeStamped, MessageDigest messageDigest)91throws NoSuchAlgorithmException {9293this.policyId = tSAPolicyID;94this.hashAlgorithmId = AlgorithmId.get(messageDigest.getAlgorithm());95this.hashValue = messageDigest.digest(toBeTimeStamped);96}9798public byte[] getHashedMessage() {99return hashValue.clone();100}101102/**103* Sets the Time-Stamp Protocol version.104*105* @param version The TSP version.106*/107public void setVersion(int version) {108this.version = version;109}110111/**112* Sets an object identifier for the Time-Stamp Protocol policy.113*114* @param policyId The policy object identifier.115*/116public void setPolicyId(String policyId) {117this.policyId = policyId;118}119120/**121* Sets a nonce.122* A nonce is a single-use random number.123*124* @param nonce The nonce value.125*/126public void setNonce(BigInteger nonce) {127this.nonce = nonce;128}129130/**131* Request that the TSA include its signing certificate in the response.132*133* @param returnCertificate True if the TSA should return its signing134* certificate. By default it is not returned.135*/136public void requestCertificate(boolean returnCertificate) {137this.returnCertificate = returnCertificate;138}139140/**141* Sets the Time-Stamp Protocol extensions.142*143* @param extensions The protocol extensions.144*/145public void setExtensions(X509Extension[] extensions) {146this.extensions = extensions;147}148149public byte[] encode() throws IOException {150151DerOutputStream request = new DerOutputStream();152153// encode version154request.putInteger(version);155156// encode messageImprint157DerOutputStream messageImprint = new DerOutputStream();158hashAlgorithmId.encode(messageImprint);159messageImprint.putOctetString(hashValue);160request.write(DerValue.tag_Sequence, messageImprint);161162// encode optional elements163164if (policyId != null) {165request.putOID(ObjectIdentifier.of(policyId));166}167if (nonce != null) {168request.putInteger(nonce);169}170if (returnCertificate) {171request.putBoolean(true);172}173174DerOutputStream out = new DerOutputStream();175out.write(DerValue.tag_Sequence, request);176return out.toByteArray();177}178}179180181