Avatar of tropocolo
tropocolo

asked on 

Problem connecting TSA using https

I want to know how use TSA to connect to https.
Actually I do this (I resume it)

To connect with the Server I try to include automatically the server's certificate when I receive it to elude this Exception:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

If you need, I can send all the project.

Thanks
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;
}

Open in new window

Java

Avatar of undefined
Last Comment
tropocolo

8/22/2022 - Mon