1

I am using bouncycastle version 15on to get ocspResponse from OcspServer as follows:

public OCSPResp getOcspResponse(OCSPReq request, String urlStr){
    HttpURLConnection con = null;
    OutputStream out = null;
    DataOutputStream dataOut = null;
    try {
        byte[] array = request.getEncoded();
        URL url = new URL(urlStr);
        con = (HttpURLConnection) url.openConnection();
        con.setRequestProperty("Content-Type", "application/ocsp-request");
        con.setRequestProperty("Accept", "application/ocsp-response");
        con.setDoOutput(true);
        out = con.getOutputStream();
        dataOut = new DataOutputStream(new BufferedOutputStream(out));
        dataOut.write(array);
        dataOut.flush();
        if (con.getResponseCode() / 100 != 2)         
            throw new Exception(...);
        InputStream in = (InputStream) con.getContent();
        if (in == null) 
            throw new Exception(...);
        byte[] byteArrayInputStream = IOUtils.toByteArray(in);
        return new OCSPResp(byteArrayInputStream); 

    } catch (IOException e) {
    ...
    }finally {
    ...
    }
}

Then I convert this OCSPResp to a jsonString using Gson version 2.2.4, however due to no-args constructor issue I could not revert this jsonString back to original bouncycastle object, and I have got an error (same solution1 error). Googling guide me to develop two ways to retrieve this OCSPResp as follows, but no one works for me:

Solution1: Register an InstanceCreator with Gson

public class OCSPRespInstanceCreator implements InstanceCreator<OCSPResp> {    
    byte[] byteArrayInputStream = {48, -126, 6, ... , 27, 6, 67};    
    @Override
    public OCSPResp createInstance(Type type) {
        try {
            OCSPResp ocspResp = new OCSPResp(byteArrayInputStream);
            return ocspResp;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Applying Solution1:

public static void main(String[] args) {
    try {            
        String ocspJson = "{\"resp\":{\"responseStatus\":{\"value\":{\"bytes\":[0]}},\"responseBytes\":{\"responseType\":{\"identifier\":\"1.3.6.1.5.5.7.48.1.1\",\"body\":[43,6,1,5,5,7,48,1,1]},\"response\":{\"string\":[48,-126,6,51,48,...81,27,6,67]}}}}";                 
        Gson gson = new GsonBuilder().registerTypeAdapter(OCSPResp.class, new OCSPRespInstanceCreator()).create();
        OCSPResp ocspResp3 = gson.fromJson(ocspJson, OCSPResp.class);      
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Result of Solution1:

java.lang.RuntimeException: Unable to invoke no-args constructor for class org.bouncycastle.asn1.ASN1OctetString. Register an InstanceCreator with Gson for this type may fix this problem.

Solution2: Using flexjson version 3.2

public static void main(String[] args) {
    try {
        String ocspJson = "{\"resp\":{\"responseStatus\":{\"value\":{\"bytes\":[0]}},\"responseBytes\":{\"responseType\":{\"identifier\":\"1.3.6.1.5.5.7.48.1.1\",\"body\":[43,6,1,5,5,7,48,1,1]},\"response\":{\"string\":[48,-126,6,51,48,-127,...,-46,108,81,27,6,67]}}}}";
        OCSPResp ocspResp = new JSONDeserializer<OCSPResp>().deserialize(ocspJson);        
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Result of Solution2:

java.lang.ClassCastException: java.util.HashMap cannot be cast to org.bouncycastle.cert.ocsp.OCSPResp

What are the problems of these solutions? Is there a third solution that revert jsonString back to the original OCSPResp object of bouncycastle correctly?

Hosein Aqajani
  • 1,553
  • 4
  • 26
  • 46
  • Is it possible to have a reproducable code without `...`? – pirho Oct 16 '18 at 19:20
  • Dear @pirho I have to emit a part of `ocspResponse` due to large amount of data and some security reason of my company. However, you can replace that with another `ocspResponse`, see this https://pkijs.org/examples/OCSP_resp_complex_example.html , Also I used `. . .` in Exception argument which could be removed. Please upvote this question to observe by others. Thanks in advance – Hosein Aqajani Oct 17 '18 at 06:56
  • I thought so. But edit the question to have the needed information how to generate the test data. There are some options on that page to choose (do those matter?) and how to convert the response to test data. – pirho Oct 17 '18 at 09:01

0 Answers0