asked on
TimeStampRequester respond =new TimeStampRequester(url);--> To process
the response of the TSA server
byte[] request = null;
request = respuesta.getTimeStampRequest(document,
"1.3.14.3.2.26");-->getTimeStampRequest explain below
byte[] response = null;
TimeStampResponse tsResponse = null;
response = respond.getTimeStampResponseFromRequest(request);-->
getTimeStampResponseFromRequest explain below
tsResponse = new TimeStampResponse(response);
File f = new File(this.parameters.tokenPath);
FileOutputStream fos = new FileOutputStream(f);
fos.write(tsResponse.getTimeStampToken().getEncoded());
fos.flush();
fos.close();
public byte[] getTimeStampRequest(byte[] inputDigest, String
hashAlgorithmOID) throws Exception {
// Hash of date to stamp
SHA1Digest digest = new SHA1Digest();
digest.update(inputDigest, 0, inputDigest.length);
byte[] digestValue = new byte[digest.getDigestSize()];
digest.doFinal(digestValue, 0);
DERObjectIdentifier sha1OID = new DERObjectIdentifier(hashAlgorithmOID);
AlgorithmIdentifier algorithmId = new AlgorithmIdentifier(sha1OID);
MessageImprint imprint = new MessageImprint(algorithmId, digestValue);
Random random = new Random(new Date().getTime());
long nonceLong = random.nextLong();
BigInteger nonce = BigInteger.valueOf(nonceLong);
String policyOID = "1.1.2";
TimeStampReq request = new TimeStampReq(imprint, new
DERObjectIdentifier(policyOID),
new DERInteger(nonce), new DERBoolean(true), null);
byte[] req = request.toASN1Object().getDEREncoded();
return req;
}
public byte[] getTimeStampResponseFromRequest(byte[] req) throws Exception {
DERSequence der = getDERSequence(req);
TimeStampReq request = new TimeStampReq(der);
PostMethod postMethod = new PostMethod(tsaUrl);
ByteArrayRequestEntity requestByte = new
ByteArrayRequestEntity(request.getEncoded(),
"application/timestamp-query");
postMethod.setRequestEntity(requestByte);
HttpClient http_client = new HttpClient();
http_client.executeMethod(postMethod);
int status = postMethod.getStatusCode();
if (status != 200) {
throw new HttpException("Server error. Error code is " + status);
}
InputStream in = postMethod.getResponseBodyAsStream();
ASN1InputStream asnIS = new ASN1InputStream(in);
DERSequence s = (DERSequence) asnIS.readObject();
//read TSP response
TimeStampResp response = new TimeStampResp(s);
byte[] resp = response.toASN1Object().getDEREncoded();
return resp;
}