|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package sun.security.timestamp; |
|
|
|
import java.io.IOException; |
|
import java.math.BigInteger; |
|
import java.security.MessageDigest; |
|
import java.security.NoSuchAlgorithmException; |
|
import java.security.cert.X509Extension; |
|
import sun.security.util.DerValue; |
|
import sun.security.util.DerOutputStream; |
|
import sun.security.util.ObjectIdentifier; |
|
import sun.security.x509.AlgorithmId; |
|
|
|
/** |
|
* This class provides a timestamp request, as defined in |
|
* <a href="http://www.ietf.org/rfc/rfc3161.txt">RFC 3161</a>. |
|
* |
|
* The TimeStampReq ASN.1 type has the following definition: |
|
* <pre> |
|
* |
|
* TimeStampReq ::= SEQUENCE { |
|
* version INTEGER { v1(1) }, |
|
* messageImprint MessageImprint |
|
* -- a hash algorithm OID and the hash value of the data to be |
|
* -- time-stamped. |
|
* reqPolicy TSAPolicyId OPTIONAL, |
|
* nonce INTEGER OPTIONAL, |
|
* certReq BOOLEAN DEFAULT FALSE, |
|
* extensions [0] IMPLICIT Extensions OPTIONAL } |
|
* |
|
* MessageImprint ::= SEQUENCE { |
|
* hashAlgorithm AlgorithmIdentifier, |
|
* hashedMessage OCTET STRING } |
|
* |
|
* TSAPolicyId ::= OBJECT IDENTIFIER |
|
* |
|
* </pre> |
|
* |
|
* @since 1.5 |
|
* @author Vincent Ryan |
|
* @see Timestamper |
|
*/ |
|
|
|
public class TSRequest { |
|
|
|
private int version = 1; |
|
|
|
private AlgorithmId hashAlgorithmId = null; |
|
|
|
private byte[] hashValue; |
|
|
|
private String policyId = null; |
|
|
|
private BigInteger nonce = null; |
|
|
|
private boolean returnCertificate = false; |
|
|
|
private X509Extension[] extensions = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public TSRequest(String tSAPolicyID, byte[] toBeTimeStamped, MessageDigest messageDigest) |
|
throws NoSuchAlgorithmException { |
|
|
|
this.policyId = tSAPolicyID; |
|
this.hashAlgorithmId = AlgorithmId.get(messageDigest.getAlgorithm()); |
|
this.hashValue = messageDigest.digest(toBeTimeStamped); |
|
} |
|
|
|
public byte[] getHashedMessage() { |
|
return hashValue.clone(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setVersion(int version) { |
|
this.version = version; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setPolicyId(String policyId) { |
|
this.policyId = policyId; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setNonce(BigInteger nonce) { |
|
this.nonce = nonce; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void requestCertificate(boolean returnCertificate) { |
|
this.returnCertificate = returnCertificate; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setExtensions(X509Extension[] extensions) { |
|
this.extensions = extensions; |
|
} |
|
|
|
public byte[] encode() throws IOException { |
|
|
|
DerOutputStream request = new DerOutputStream(); |
|
|
|
|
|
request.putInteger(version); |
|
|
|
|
|
DerOutputStream messageImprint = new DerOutputStream(); |
|
hashAlgorithmId.encode(messageImprint); |
|
messageImprint.putOctetString(hashValue); |
|
request.write(DerValue.tag_Sequence, messageImprint); |
|
|
|
// encode optional elements |
|
|
|
if (policyId != null) { |
|
request.putOID(ObjectIdentifier.of(policyId)); |
|
} |
|
if (nonce != null) { |
|
request.putInteger(nonce); |
|
} |
|
if (returnCertificate) { |
|
request.putBoolean(true); |
|
} |
|
|
|
DerOutputStream out = new DerOutputStream(); |
|
out.write(DerValue.tag_Sequence, request); |
|
return out.toByteArray(); |
|
} |
|
} |