Ainur
Ainur

Reputation: 25

timestamp error bouncycastle

Im trying to get a timestamp from a tsa server and I got this error. I dont know where is the problem.

java.lang.IllegalArgumentException: unknown object in 'TimeStampResp' factory : org.bouncycastle.asn1.DERUnknownTag

In this statement

TimeStampResp resp = TimeStampResp.getInstance(new ASN1InputStream(in).readObject());

This is my code:

    String TSA_URL    = "http://tsa.starfieldtech.com/";
    //String TSA_URL = "http://ca.signfiles.com/TSAServer.aspx";
    //String TSA_URL = "http://timestamping.edelweb.fr/service/tsp";
    try {
        //byte[] digest = calcularMessageDigest(leerByteFichero("C:\\deskSign.txt"));
        byte[] digest = leerByteFichero("C:\\deskSign.txt");

        TimeStampRequestGenerator reqgen = new TimeStampRequestGenerator();
        TimeStampRequest req = reqgen.generate(TSPAlgorithms.SHA1, digest);
        byte request[] = req.getEncoded();

        URL url = new URL(TSA_URL);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        con.setDoOutput(true);
        con.setDoInput(true);
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-type:", "application/timestamp-query");
        con.setRequestProperty("Content-length:", String.valueOf(request.length));

        if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new IOException("Received HTTP error: " + con.getResponseCode() + " - " + con.getResponseMessage());
        }
        InputStream in = con.getInputStream();
        TimeStampResp resp = TimeStampResp.getInstance(new ASN1InputStream(in).readObject());
        TimeStampResponse response = new TimeStampResponse(resp);
        response.validate(req);
        System.out.println(response.getTimeStampToken().getTimeStampInfo().getGenTime());

Anyone can help me?

Upvotes: 0

Views: 1306

Answers (1)

mat
mat

Reputation: 1717

You made two errors:

  1. No colons after Content-type and Content-length
  2. You forgot to actually send the request

Here is and adaptation of your code that works:

TimeStampRequest request;
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/timestamp-query");
con.setRequestProperty("Content-length", String.valueOf(request.getEncoded().length));
Authenticator.setDefault(new Authenticator() { 
con.getOutputStream().write(request.getEncoded());

if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new IOException("Received HTTP error: " + con.getResponseCode() + " - " + con.getResponseMessage());
}
InputStream in = con.getInputStream();

TimeStampResp resp = TimeStampResp.getInstance(new ASN1InputStream(in).readObject());
TimeStampResponse response = new TimeStampResponse(resp);

Upvotes: 1

Related Questions