javaandroidokhttpspongycastle

Timestamp request with OkHttp


I'm trying to make a post call to a Timestamp server but getting a 500. This is my code:

        Security.addProvider(new BouncyCastleProvider());

        TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator();
        reqGen.setCertReq(true);

        MessageDigest digest = MessageDigest.getInstance("SHA256");
        digest.update(myData);
        TimeStampRequest request = reqGen.generate(TSPAlgorithms.SHA256, digest.digest(), BigInteger.valueOf(100));

        MediaType type = MediaType.parse("application/ocsp-request");
        RequestBody requestBody = RequestBody.create(type, request.getEncoded());

        Request request = new Request.Builder()
                .url(myUrl)
                .post(requestBody)
                .addHeader("Content-type", "application/timestamp-query")
                .addHeader("Content-Transfer-Encoding", "binary")
                .addHeader("Authorization", Credentials.basic(myUser, myPass))
                .build();

        httpClient.newCall(request).enqueue(new Callback() {
            public void onFailure(Call call, IOException e) {
                Log.e(myTAG, "Error connecting to timestamp server");
            }

            public void onResponse(Call call, Response response) {
                Log.e(myTAG, "Success");
            }
        });

I know the headers, the credentials and the url are all correct, but I'm not sure if I'm adding the TimeStampRequest correctly to the OkHttp request, or even if the MediaType is the one it should. Anyone knows if it is correct?

Note: the TimeStamp classes belong to the spongycastle library.


Solution

  • Turns out the incorrect part is the MediaType, which has to be binary for the server to correctly read the data. Code corrected:

    MediaType type = MediaType.parse("binary");